Class: Lorcon::Device

Inherits:
Object
  • Object
show all
Defined in:
ext/Lorcon.c

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new(*args) ⇒ Object



428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
# File 'ext/Lorcon.c', line 428

static VALUE lorcon_device_open(int argc, VALUE *argv, VALUE self) {
	struct rldev *rld;
	int ret = 0;
	int drivertype = INJ_NODRIVER;
	char *driver, *intf;
	VALUE rbdriver, rbintf;
	VALUE obj;

	rb_scan_args(argc, argv, "2", &rbintf, &rbdriver);
	
	driver = STR2CSTR(rbdriver);
	intf   = STR2CSTR(rbintf);

	obj = Data_Make_Struct(cDevice, struct rldev, 0, lorcon_device_free, rld);

	drivertype = tx80211_resolvecard(driver);
	if (drivertype == INJ_NODRIVER) {
		rb_raise(rb_eArgError, "Lorcon did not recognize the specified driver");
		return(Qnil);
	}
	
	if (tx80211_init(&rld->in_tx, intf, drivertype) < 0) {
		rb_raise(rb_eRuntimeError, "Lorcon could not initialize the interface: %s", tx80211_geterrstr(&rld->in_tx));
		return(Qnil);
	}

	/* Open the interface to get a socket */
	ret = tx80211_open(&rld->in_tx);
	if (ret < 0) {
		rb_raise(rb_eRuntimeError, "Lorcon could not open the interface: %s", tx80211_geterrstr(&rld->in_tx));
		return(Qnil);
	}	

	rb_obj_call_init(obj, 0, 0);	
	return(obj);
}

Instance Method Details

#capabilitiesObject



422
423
424
425
426
# File 'ext/Lorcon.c', line 422

static VALUE lorcon_device_get_capabilities(VALUE self) {
	struct rldev *rld;
	Data_Get_Struct(self, struct rldev, rld);
	return(lorcon_cap_to_list(tx80211_getcapabilities(&rld->in_tx)));
}

#channelObject



128
129
130
131
132
# File 'ext/Lorcon.c', line 128

static VALUE lorcon_device_get_channel(VALUE self) {
	struct rldev *rld;
	Data_Get_Struct(self, struct rldev, rld);
	return INT2NUM(tx80211_getchannel(&rld->in_tx));
}

#channel=(channel) ⇒ Object



134
135
136
137
138
139
# File 'ext/Lorcon.c', line 134

static VALUE lorcon_device_set_channel(VALUE self, VALUE channel) {
	struct rldev *rld;
	Data_Get_Struct(self, struct rldev, rld);
	tx80211_setchannel(&rld->in_tx, NUM2INT(channel));
	return INT2NUM(tx80211_getchannel(&rld->in_tx));
}

#fmode=(rmode) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'ext/Lorcon.c', line 219

static VALUE lorcon_device_set_functional_mode(VALUE self, VALUE rmode) {
	struct rldev *rld;
	char *funcmode = StringValuePtr(rmode);
	int mode = -1;

	Data_Get_Struct(self, struct rldev, rld);
		
	if (strcmp(funcmode, "RFMON") == 0) {
		mode = TX80211_FUNCMODE_RFMON;
	} else if (strcmp(funcmode, "INJECT") == 0) {
		mode = TX80211_FUNCMODE_INJECT;
	} else if (strcmp(funcmode, "INJMON") == 0) {
		mode = TX80211_FUNCMODE_INJMON;
	} else {
		rb_raise(rb_eArgError, "Invalid mode specified: %s", tx80211_geterrstr(&rld->in_tx));
		return(Qnil);
	}

	if (tx80211_setfunctionalmode(&rld->in_tx, mode) != 0) {
		rb_raise(rb_eArgError, "Lorcon could not set the functional mode: %s", tx80211_geterrstr(&rld->in_tx));
		return(Qnil);
	}
	return Qtrue;
}

#modeObject



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'ext/Lorcon.c', line 149

static VALUE lorcon_device_get_mode(VALUE self) {
	struct rldev *rld;
	int mode;
	Data_Get_Struct(self, struct rldev, rld);
	

	mode = tx80211_getmode(&rld->in_tx);
	if (mode < 0) {
		rb_raise(rb_eArgError, "Lorcon could not determine the mode of this device: %s", tx80211_geterrstr(&rld->in_tx));
		return(Qnil);
	}
	
	switch (mode) {
		case TX80211_MODE_AUTO:
			return rb_str_new2("AUTO");
			break;
		case TX80211_MODE_ADHOC:
			return rb_str_new2("ADHOC");
			break;
		case TX80211_MODE_INFRA:
			return rb_str_new2("INFRA");
			break;
		case TX80211_MODE_MASTER:
			return rb_str_new2("MASTER");
			break;
		case TX80211_MODE_REPEAT:
			return rb_str_new2("REPEAT");
			break;
		case TX80211_MODE_SECOND:
			return rb_str_new2("SECOND");
			break;
		case TX80211_MODE_MONITOR:
			return rb_str_new2("MONITOR");
			break;
		default:
			return Qnil;
			break;
	}
}

#mode=(rmode) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'ext/Lorcon.c', line 189

static VALUE lorcon_device_set_mode(VALUE self, VALUE rmode) {
	struct rldev *rld;
	char *setmode = StringValuePtr(rmode);
	int mode = -1;

	Data_Get_Struct(self, struct rldev, rld);
	
	if (strcmp(setmode, "AUTO") == 0) {
		mode = TX80211_MODE_AUTO;
	} else if (strcmp(setmode, "ADHOC") == 0) {
		mode = TX80211_MODE_ADHOC;
	} else if (strcmp(setmode, "INFRA") == 0) {
		mode = TX80211_MODE_INFRA;
	} else if (strcmp(setmode, "MASTER") == 0) {
		mode = TX80211_MODE_MASTER;
	} else if (strcmp(setmode, "REPEAT") == 0) {
		mode = TX80211_MODE_REPEAT;
	} else if (strcmp(setmode, "SECOND") == 0) {
		mode = TX80211_MODE_SECOND;
	} else if (strcmp(setmode, "MONITOR") == 0) {
		mode = TX80211_MODE_MONITOR;
	} else {
		rb_raise(rb_eArgError, "Invalid mode specified: %s", tx80211_geterrstr(&rld->in_tx));
		return(Qnil);
	}

	return INT2NUM(tx80211_setmode(&rld->in_tx, mode));
}

#modulationObject



345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'ext/Lorcon.c', line 345

static VALUE lorcon_device_get_modulation(VALUE self) {
	struct rldev *rld;
	int mod;
	
	Data_Get_Struct(self, struct rldev, rld);

	mod = tx80211_getmodulation(&rld->in_packet);
	switch (mod) {
		case TX80211_MOD_DEFAULT:
			return rb_str_new2("DEFAULT");
			break;
		case TX80211_MOD_FHSS:
			return rb_str_new2("FHSS");
			break;
		case TX80211_MOD_DSSS:
			return rb_str_new2("DSSS");
			break;
		case TX80211_MOD_OFDM:
			return rb_str_new2("OFDM");
			break;
		case TX80211_MOD_TURBO:
			return rb_str_new2("TURBO");
			break;
		case TX80211_MOD_MIMO:
			return rb_str_new2("MIMO");
			break;
		case TX80211_MOD_MIMOGF:
			return rb_str_new2("MIMOGF");
			break;
		default:
		rb_raise(rb_eArgError, "Lorcon could not get the modulation value");
		return(Qnil);
	}
	return(Qnil);
}

#modulation=(rmod) ⇒ Object



381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# File 'ext/Lorcon.c', line 381

static VALUE lorcon_device_set_modulation(VALUE self, VALUE rmod) {
	struct rldev *rld;
	char *setmod = NULL;
	int mod;
	
	Data_Get_Struct(self, struct rldev, rld);

	if ((tx80211_getcapabilities(&rld->in_tx) & TX80211_CAP_SETMODULATION) == 0) {
		rb_raise(rb_eArgError, "Lorcon does not support setting the modulation for this card");
		return(Qnil);
	}

	setmod = StringValuePtr(rmod);

	if (strcmp(setmod, "DEFAULT") == 0) {
		mod = TX80211_MOD_DEFAULT;
	} else if (strcmp(setmod, "FHSS") == 0) {
		mod = TX80211_MOD_FHSS;
	} else if (strcmp(setmod, "DSSS") == 0) {
		mod = TX80211_MOD_DSSS;
	} else if (strcmp(setmod, "OFDM") == 0) {
		mod = TX80211_MOD_OFDM;
	} else if (strcmp(setmod, "TURBO") == 0) {
		mod = TX80211_MOD_TURBO;
	} else if (strcmp(setmod, "MIMO") == 0) {
		mod = TX80211_MOD_MIMO;
	} else if (strcmp(setmod, "MIMOGF") == 0) {
		mod = TX80211_MOD_MIMOGF;
	} else {
		rb_raise(rb_eArgError, "Lorcon does not support this modulation setting");
		return(Qnil);
	}

	if (tx80211_setmodulation(&rld->in_tx, &rld->in_packet, mod) < 0) {
		rb_raise(rb_eArgError, "Lorcon could not set the modulation: %s", tx80211_geterrstr(&rld->in_tx));
		return(Qnil);
	}
	
	return INT2NUM(mod);
}

#txrateObject



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'ext/Lorcon.c', line 245

static VALUE lorcon_device_get_txrate(VALUE self) {
	struct rldev *rld;
	int txrate;
	
	txrate = tx80211_gettxrate(&rld->in_packet);
	Data_Get_Struct(self, struct rldev, rld);

	switch (txrate) {
		case TX80211_RATE_DEFAULT:
			return UINT2NUM(0);
			break;
		case TX80211_RATE_1MB:
			return UINT2NUM(1);
			break;
		case TX80211_RATE_2MB:
			return UINT2NUM(2);
			break;
		case TX80211_RATE_5_5MB:
			return UINT2NUM(5);
			break;
		case TX80211_RATE_6MB:
			return UINT2NUM(6);
			break;
		case TX80211_RATE_9MB:
			return UINT2NUM(9);
			break;
		case TX80211_RATE_11MB:
			return UINT2NUM(11);
			break;
		case TX80211_RATE_24MB:
			return UINT2NUM(24);
			break;
		case TX80211_RATE_36MB:
			return UINT2NUM(36);
			break;
		case TX80211_RATE_48MB:
			return UINT2NUM(48);
			break;
		case TX80211_RATE_108MB:
			return UINT2NUM(108);
			break;
		default:
			rb_raise(rb_eArgError, "Lorcon could not determine the tx rate: %s", tx80211_geterrstr(&rld->in_tx));
			return(Qnil);
	}
	
	return Qnil;
}

#txrate=(rrate) ⇒ Object



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'ext/Lorcon.c', line 295

static VALUE lorcon_device_set_txrate(VALUE self, VALUE rrate) {
	struct rldev *rld;
	float settxrate = -1;
	int txrate = -1;
	
	Data_Get_Struct(self, struct rldev, rld);


	if ((tx80211_getcapabilities(&rld->in_tx) & TX80211_CAP_SETRATE) == 0) {
		rb_raise(rb_eArgError, "Lorcon does not support setting the tx rate for this card");
		return(Qnil);
	}

	settxrate = NUM2DBL(rrate);
	
	if (settxrate == -1) {
		txrate = TX80211_RATE_DEFAULT;
	} else if (settxrate == 1) {
		txrate = TX80211_RATE_1MB;
	} else if (settxrate == 2) {
		txrate = TX80211_RATE_2MB;
	} else if (settxrate == 5.5) {
		txrate = TX80211_RATE_5_5MB;
	} else if (settxrate == 6) {
		txrate = TX80211_RATE_6MB;
	} else if (settxrate == 9) {
		txrate = TX80211_RATE_9MB;
	} else if (settxrate == 11) {
		txrate = TX80211_RATE_11MB;
	} else if (settxrate == 24) {
		txrate = TX80211_RATE_24MB;
	} else if (settxrate == 36) {
		txrate = TX80211_RATE_36MB;
	} else if (settxrate == 48) {
		txrate = TX80211_RATE_48MB;
	} else if (settxrate == 108) {
		txrate = TX80211_RATE_108MB;
	} else {
		rb_raise(rb_eArgError, "Lorcon does not support this rate setting");
		return(Qnil);
	}

	if (tx80211_settxrate(&rld->in_tx, &rld->in_packet, txrate) < 0) {
		rb_raise(rb_eArgError, "Lorcon could not set the tx rate: %s", tx80211_geterrstr(&rld->in_tx));
		return(Qnil);
	}

	return INT2NUM(txrate);
}

#write(*args) ⇒ Object



465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
# File 'ext/Lorcon.c', line 465

static VALUE lorcon_device_write(int argc, VALUE *argv, VALUE self) {
	struct rldev *rld;
	int ret = 0;
	int cnt = 0;
	int dly = 0;
	
	VALUE rbbuff, rbcnt, rbdelay;
	
	Data_Get_Struct(self, struct rldev, rld);	
		
	switch(rb_scan_args(argc, argv, "12", &rbbuff, &rbcnt, &rbdelay)) {	
		case 1:
			rbdelay = INT2NUM(0);
		case 2:	
			rbcnt = INT2NUM(1);
		default:
			break;
	}

	cnt = NUM2INT(rbcnt);
	dly = NUM2INT(rbdelay);

	rld->in_packet.packet = StringValuePtr(rbbuff);
	rld->in_packet.plen = RSTRING(rbbuff)->len;

	for (; cnt > 0; cnt--) {
		ret = tx80211_txpacket(&rld->in_tx, &rld->in_packet);
		if (ret < 0) {
			rb_raise(rb_eRuntimeError, "Lorcon could not transmit packet: %s", tx80211_geterrstr(&rld->in_tx));			
			return(INT2NUM(ret));
		}
		if (dly > 0)
#ifdef _MSC_VER
			Sleep(dly);
#else
			usleep(dly);
#endif
	}

	return (rbcnt);
}