summaryrefslogtreecommitdiff
path: root/iwinfo_cli.c
blob: 5dcee9ad023f4311fe75a7283d7c0bb788b6f976 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
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
188
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
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
293
294
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
344
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
380
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
421
422
423
424
425
426
427
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
464
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
/*
 * iwinfo - Wireless Information Library - Command line frontend
 *
 *   Copyright (C) 2011 Jo-Philipp Wich <xm@subsignal.org>
 *
 * The iwinfo library is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation.
 *
 * The iwinfo library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with the iwinfo library. If not, see http://www.gnu.org/licenses/.
 */

#include <stdio.h>
#include <glob.h>

#include "iwinfo.h"


static char * format_bssid(unsigned char *mac)
{
	static char buf[18];

	snprintf(buf, sizeof(buf), "%02X:%02X:%02X:%02X:%02X:%02X",
		mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);

	return buf;
}

static char * format_ssid(char *ssid)
{
	static char buf[IWINFO_ESSID_MAX_SIZE+3];

	if (ssid && ssid[0])
		snprintf(buf, sizeof(buf), "\"%s\"", ssid);
	else
		snprintf(buf, sizeof(buf), "unknown");

	return buf;
}

static const char *format_band(int band)
{
	const char *name;

	name = iwinfo_band_name(band);
	if (name)
		return name;

	return "unknown";
}

static char * format_channel(int ch)
{
	static char buf[16];

	if (ch <= 0)
		snprintf(buf, sizeof(buf), "unknown");
	else
		snprintf(buf, sizeof(buf), "%d", ch);

	return buf;
}

static char * format_frequency(int freq)
{
	static char buf[11];

	if (freq <= 0)
		snprintf(buf, sizeof(buf), "unknown");
	else
		snprintf(buf, sizeof(buf), "%.3f GHz", ((float)freq / 1000.0));

	return buf;
}

static char * format_freqflags(uint32_t flags)
{
	static char str[512] = "[";
	char *pos = str + 1;
	int i;

	if (!flags)
		return "";

	for (i = 0; i < IWINFO_FREQ_FLAG_COUNT; i++)
		if (flags & (1 << i))
			pos += sprintf(pos, "%s, ", IWINFO_FREQ_FLAG_NAMES[i]);

	*(pos - 2) = ']';
	*(pos - 1) = 0;

	return str;
}

static char * format_txpower(int pwr)
{
	static char buf[16];

	if (pwr < 0)
		snprintf(buf, sizeof(buf), "unknown");
	else
		snprintf(buf, sizeof(buf), "%d dBm", pwr);

	return buf;
}

static char * format_quality(int qual)
{
	static char buf[16];

	if (qual < 0)
		snprintf(buf, sizeof(buf), "unknown");
	else
		snprintf(buf, sizeof(buf), "%d", qual);

	return buf;
}

static char * format_quality_max(int qmax)
{
	static char buf[16];

	if (qmax < 0)
		snprintf(buf, sizeof(buf), "unknown");
	else
		snprintf(buf, sizeof(buf), "%d", qmax);

	return buf;
}

static char * format_signal(int sig)
{
	static char buf[10];

	if (!sig)
		snprintf(buf, sizeof(buf), "unknown");
	else
		snprintf(buf, sizeof(buf), "%d dBm", sig);

	return buf;
}

static char * format_noise(int noise)
{
	static char buf[10];

	if (!noise)
		snprintf(buf, sizeof(buf), "unknown");
	else
		snprintf(buf, sizeof(buf), "%d dBm", noise);

	return buf;
}

static char * format_rate(int rate)
{
	static char buf[18];

	if (rate <= 0)
		snprintf(buf, sizeof(buf), "unknown");
	else
		snprintf(buf, sizeof(buf), "%d.%d MBit/s",
			rate / 1000, (rate % 1000) / 100);

	return buf;
}

static char * format_enc_ciphers(int ciphers)
{
	static char str[128] = { 0 };
	char *pos = str;
	int i;

	for (i = 0; i < IWINFO_CIPHER_COUNT; i++)
		if (ciphers & (1 << i))
			pos += sprintf(pos, "%s, ", IWINFO_CIPHER_NAMES[i]);

	*(pos - 2) = 0;

	return str;
}

static char * format_enc_suites(int suites)
{
	static char str[64] = { 0 };
	char *pos = str;
	int i;

	for (i = 0; i < IWINFO_KMGMT_COUNT; i++)
		if (suites & (1 << i))
			pos += sprintf(pos, "%s/", IWINFO_KMGMT_NAMES[i]);

	*(pos - 1) = 0;

	return str;
}

static char * format_encryption(struct iwinfo_crypto_entry *c)
{
	static char buf[512];
	char *pos = buf;
	int i, n;

	if (!c)
	{
		snprintf(buf, sizeof(buf), "unknown");
	}
	else if (c->enabled)
	{
		/* WEP */
		if (c->auth_algs && !c->wpa_version)
		{
			if ((c->auth_algs & IWINFO_AUTH_OPEN) &&
				(c->auth_algs & IWINFO_AUTH_SHARED))
			{
				snprintf(buf, sizeof(buf), "WEP Open/Shared (%s)",
					format_enc_ciphers(c->pair_ciphers));
			}
			else if (c->auth_algs & IWINFO_AUTH_OPEN)
			{
				snprintf(buf, sizeof(buf), "WEP Open System (%s)",
					format_enc_ciphers(c->pair_ciphers));
			}
			else if (c->auth_algs & IWINFO_AUTH_SHARED)
			{
				snprintf(buf, sizeof(buf), "WEP Shared Auth (%s)",
					format_enc_ciphers(c->pair_ciphers));
			}
		}

		/* WPA */
		else if (c->wpa_version)
		{
			for (i = 0, n = 0; i < 3; i++)
				if (c->wpa_version & (1 << i))
					n++;

			if (n > 1)
				pos += sprintf(pos, "mixed ");

			for (i = 0; i < 3; i++)
				if (c->wpa_version & (1 << i))
				{
					if (i)
						pos += sprintf(pos, "WPA%d/", i + 1);
					else
						pos += sprintf(pos, "WPA/");
				}

			pos--;

			sprintf(pos, " %s (%s)",
				format_enc_suites(c->auth_suites),
				format_enc_ciphers(c->pair_ciphers | c->group_ciphers));
		}
		else
		{
			snprintf(buf, sizeof(buf), "none");
		}
	}
	else
	{
		snprintf(buf, sizeof(buf), "none");
	}

	return buf;
}

static char * format_hwmodes(int modes)
{
	static char buf[32] = "802.11";

	if (iwinfo_format_hwmodes(modes, buf + 6, sizeof(buf) - 6) < 1)
		snprintf(buf, sizeof(buf), "unknown");

	return buf;
}

static char * format_assocrate(struct iwinfo_rate_entry *r)
{
	static char buf[80];
	char *p = buf;
	int l = sizeof(buf);

	if (r->rate <= 0)
	{
		snprintf(buf, sizeof(buf), "unknown");
	}
	else
	{
		p += snprintf(p, l, "%s", format_rate(r->rate));
		l = sizeof(buf) - (p - buf);

		if (r->is_ht)
		{
			p += snprintf(p, l, ", MCS %d, %dMHz", r->mcs, r->mhz);
			l = sizeof(buf) - (p - buf);
		}
		else if (r->is_vht)
		{
			p += snprintf(p, l, ", VHT-MCS %d, %dMHz", r->mcs, r->mhz);
			l = sizeof(buf) - (p - buf);

			if (r->nss)
			{
				p += snprintf(p, l, ", VHT-NSS %d", r->nss);
				l = sizeof(buf) - (p - buf);
			}
		}
		else if (r->is_he)
		{
			p += snprintf(p, l, ", HE-MCS %d, %dMHz", r->mcs, r->mhz);
			l = sizeof(buf) - (p - buf);

			p += snprintf(p, l, ", HE-NSS %d", r->nss);
			l = sizeof(buf) - (p - buf);

			p += snprintf(p, l, ", HE-GI %d", r->he_gi);
			l = sizeof(buf) - (p - buf);

			p += snprintf(p, l, ", HE-DCM %d", r->he_dcm);
			l = sizeof(buf) - (p - buf);
		}
	}

	return buf;
}

static const char* format_chan_width(bool vht, uint8_t width)
{
	if (!vht && width < ARRAY_SIZE(ht_chan_width))
		switch (ht_chan_width[width]) {
			case 20: return "20 MHz";
			case 2040: return "40 MHz or higher";
		}

	if (vht && width < ARRAY_SIZE(vht_chan_width))
		switch (vht_chan_width[width]) {
			case 40: return "20 or 40 MHz";
			case 80: return "80 MHz";
			case 8080: return "80+80 MHz";
			case 160: return "160 MHz";
		}

	return "unknown";
}


static const char * print_type(const struct iwinfo_ops *iw, const char *ifname)
{
	const char *type = iwinfo_type(ifname);
	return type ? type : "unknown";
}

static char * print_hardware_id(const struct iwinfo_ops *iw, const char *ifname)
{
	static char buf[20];
	struct iwinfo_hardware_id ids;

	if (!iw->hardware_id(ifname, (char *)&ids))
	{
		if (strlen(ids.compatible) > 0)
			snprintf(buf, sizeof(buf), "embedded");
		else if (ids.vendor_id == 0 && ids.device_id == 0 &&
			 ids.subsystem_vendor_id != 0 && ids.subsystem_device_id != 0)
			snprintf(buf, sizeof(buf), "USB %04X:%04X",
				ids.subsystem_vendor_id, ids.subsystem_device_id);
		else
			snprintf(buf, sizeof(buf), "%04X:%04X %04X:%04X",
				ids.vendor_id, ids.device_id,
				ids.subsystem_vendor_id, ids.subsystem_device_id);
	}
	else
	{
		snprintf(buf, sizeof(buf), "unknown");
	}

	return buf;
}

static char * print_hardware_name(const struct iwinfo_ops *iw, const char *ifname)
{
	static char buf[128];

	if (iw->hardware_name(ifname, buf))
		snprintf(buf, sizeof(buf), "unknown");

	return buf;
}

static char * print_txpower_offset(const struct iwinfo_ops *iw, const char *ifname)
{
	int off;
	static char buf[12];

	if (iw->txpower_offset(ifname, &off))
		snprintf(buf, sizeof(buf), "unknown");
	else if (off != 0)
		snprintf(buf, sizeof(buf), "%d dB", off);
	else
		snprintf(buf, sizeof(buf), "none");

	return buf;
}

static char * print_frequency_offset(const struct iwinfo_ops *iw, const char *ifname)
{
	int off;
	static char buf[12];

	if (iw->frequency_offset(ifname, &off))
		snprintf(buf, sizeof(buf), "unknown");
	else if (off != 0)
		snprintf(buf, sizeof(buf), "%.3f GHz", ((float)off / 1000.0));
	else
		snprintf(buf, sizeof(buf), "none");

	return buf;
}

static char * print_ssid(const struct iwinfo_ops *iw, const char *ifname)
{
	char buf[IWINFO_ESSID_MAX_SIZE+1] = { 0 };

	if (iw->ssid(ifname, buf))
		memset(buf, 0, sizeof(buf));

	return format_ssid(buf);
}

static char * print_bssid(const struct iwinfo_ops *iw, const char *ifname)
{
	static char buf[18] = { 0 };

	if (iw->bssid(ifname, buf))
		snprintf(buf, sizeof(buf), "00:00:00:00:00:00");

	return buf;
}

static char * print_mode(const struct iwinfo_ops *iw, const char *ifname)
{
	int mode;
	static char buf[128];

	if (iw->mode(ifname, &mode))
		mode = IWINFO_OPMODE_UNKNOWN;

	snprintf(buf, sizeof(buf), "%s", IWINFO_OPMODE_NAMES[mode]);

	return buf;
}

static char * print_channel(const struct iwinfo_ops *iw, const char *ifname)
{
	int ch;
	if (iw->channel(ifname, &ch))
		ch = -1;

	return format_channel(ch);
}

static char * print_center_chan1(const struct iwinfo_ops *iw, const char *ifname)
{
	int ch;
	if (iw->center_chan1(ifname, &ch))
		ch = -1;

	return format_channel(ch);
}

static char * print_center_chan2(const struct iwinfo_ops *iw, const char *ifname)
{
	int ch;
	if (iw->center_chan2(ifname, &ch))
		ch = -1;

	return format_channel(ch);
}

static char * print_frequency(const struct iwinfo_ops *iw, const char *ifname)
{
	int freq;
	if (iw->frequency(ifname, &freq))
		freq = -1;

	return format_frequency(freq);
}

static char * print_txpower(const struct iwinfo_ops *iw, const char *ifname)
{
	int pwr, off;
	if (iw->txpower_offset(ifname, &off))
		off = 0;

	if (iw->txpower(ifname, &pwr))
		pwr = -1;
	else
		pwr += off;

	return format_txpower(pwr);
}

static char * print_quality(const struct iwinfo_ops *iw, const char *ifname)
{
	int qual;
	if (iw->quality(ifname, &qual))
		qual = -1;

	return format_quality(qual);
}

static char * print_quality_max(const struct iwinfo_ops *iw, const char *ifname)
{
	int qmax;
	if (iw->quality_max(ifname, &qmax))
		qmax = -1;

	return format_quality_max(qmax);
}

static char * print_signal(const struct iwinfo_ops *iw, const char *ifname)
{
	int sig;
	if (iw->signal(ifname, &sig))
		sig = 0;

	return format_signal(sig);
}

static char * print_noise(const struct iwinfo_ops *iw, const char *ifname)
{
	int noise;
	if (iw->noise(ifname, &noise))
		noise = 0;

	return format_noise(noise);
}

static char * print_rate(const struct iwinfo_ops *iw, const char *ifname)
{
	int rate;
	if (iw->bitrate(ifname, &rate))
		rate = -1;

	return format_rate(rate);
}

static char * print_encryption(const struct iwinfo_ops *iw, const char *ifname)
{
	struct iwinfo_crypto_entry c = { 0 };
	if (iw->encryption(ifname, (char *)&c))
		return format_encryption(NULL);

	return format_encryption(&c);
}

static char * print_hwmodes(const struct iwinfo_ops *iw, const char *ifname)
{
	int modes;
	if (iw->hwmodelist(ifname, &modes))
		modes = -1;

	return format_hwmodes(modes);
}

static const char *print_htmode(const struct iwinfo_ops *iw, const char *ifname)
{
	int mode;
	const char *name;
	if (iw->htmode(ifname, &mode))
		mode = -1;

	name = iwinfo_htmode_name(mode);
	if (name)
		return name;

	return "unknown";
}

static char * print_mbssid_supp(const struct iwinfo_ops *iw, const char *ifname)
{
	int supp;
	static char buf[4];

	if (iw->mbssid_support(ifname, &supp))
		snprintf(buf, sizeof(buf), "no");
	else
		snprintf(buf, sizeof(buf), "%s", supp ? "yes" : "no");

	return buf;
}

static char * print_phyname(const struct iwinfo_ops *iw, const char *ifname)
{
	static char buf[32];

	if (!iw->phyname(ifname, buf))
		return buf;

	return "?";
}


static void print_info(const struct iwinfo_ops *iw, const char *ifname)
{
	printf("%-9s ESSID: %s\n",
		ifname,
		print_ssid(iw, ifname));
	printf("          Access Point: %s\n",
		print_bssid(iw, ifname));
	printf("          Mode: %s  Channel: %s (%s)  HT Mode: %s\n",
		print_mode(iw, ifname),
		print_channel(iw, ifname),
		print_frequency(iw, ifname),
		print_htmode(iw, ifname));
	if (iw->center_chan1 != NULL) {
		printf("          Center Channel 1: %s",
			print_center_chan1(iw, ifname));
		printf(" 2: %s\n", print_center_chan2(iw, ifname));
	}
	printf("          Tx-Power: %s  Link Quality: %s/%s\n",
		print_txpower(iw, ifname),
		print_quality(iw, ifname),
		print_quality_max(iw, ifname));
	printf("          Signal: %s  Noise: %s\n",
		print_signal(iw, ifname),
		print_noise(iw, ifname));
	printf("          Bit Rate: %s\n",
		print_rate(iw, ifname));
	printf("          Encryption: %s\n",
		print_encryption(iw, ifname));
	printf("          Type: %s  HW Mode(s): %s\n",
		print_type(iw, ifname),
		print_hwmodes(iw, ifname));
	printf("          Hardware: %s [%s]\n",
		print_hardware_id(iw, ifname),
		print_hardware_name(iw, ifname));
	printf("          TX power offset: %s\n",
		print_txpower_offset(iw, ifname));
	printf("          Frequency offset: %s\n",
		print_frequency_offset(iw, ifname));
	printf("          Supports VAPs: %s  PHY name: %s\n",
		print_mbssid_supp(iw, ifname),
		print_phyname(iw, ifname));
}


static void print_scanlist(const struct iwinfo_ops *iw, const char *ifname)
{
	int i, x, len;
	char buf[IWINFO_BUFSIZE];
	struct iwinfo_scanlist_entry *e;

	if (iw->scanlist(ifname, buf, &len))
	{
		printf("Scanning not possible\n\n");
		return;
	}
	else if (len <= 0)
	{
		printf("No scan results\n\n");
		return;
	}

	for (i = 0, x = 1; i < len; i += sizeof(struct iwinfo_scanlist_entry), x++)
	{
		e = (struct iwinfo_scanlist_entry *) &buf[i];

		printf("Cell %02d - Address: %s\n",
			x,
			format_bssid(e->mac));
		printf("          ESSID: %s\n",
			format_ssid(e->ssid));
		printf("          Mode: %s  Frequency: %s  Band: %s  Channel: %s\n",
			IWINFO_OPMODE_NAMES[e->mode],
			format_frequency(e->mhz),
			format_band(e->band),
			format_channel(e->channel));
		printf("          Signal: %s  Quality: %s/%s\n",
			format_signal(e->signal - 0x100),
			format_quality(e->quality),
			format_quality_max(e->quality_max));
		printf("          Encryption: %s\n",
			format_encryption(&e->crypto));
		printf("          HT Operation:\n");
		printf("                    Primary Channel: %d\n",
			e->ht_chan_info.primary_chan);
		printf("                    Secondary Channel Offset: %s\n",
			ht_secondary_offset[e->ht_chan_info.secondary_chan_off]);
		printf("                    Channel Width: %s\n",
			format_chan_width(false, e->ht_chan_info.chan_width));

		if (e->vht_chan_info.center_chan_1) {
			printf("          VHT Operation:\n");
			printf("                    Center Frequency 1: %d\n",
				 e->vht_chan_info.center_chan_1);
			printf("                    Center Frequency 2: %d\n",
				 e->vht_chan_info.center_chan_2);
			printf("                    Channel Width: %s\n",
				format_chan_width(true, e->vht_chan_info.chan_width));
		}

		printf("\n");
	}
}


static void print_txpwrlist(const struct iwinfo_ops *iw, const char *ifname)
{
	int len, pwr, off, i;
	char buf[IWINFO_BUFSIZE];
	struct iwinfo_txpwrlist_entry *e;

	if (iw->txpwrlist(ifname, buf, &len) || len <= 0)
	{
		printf("No TX power information available\n");
		return;
	}

	if (iw->txpower(ifname, &pwr))
		pwr = -1;

	if (iw->txpower_offset(ifname, &off))
		off = 0;

	for (i = 0; i < len; i += sizeof(struct iwinfo_txpwrlist_entry))
	{
		e = (struct iwinfo_txpwrlist_entry *) &buf[i];

		printf("%s%3d dBm (%4d mW)\n",
			(pwr == e->dbm) ? "*" : " ",
			e->dbm + off,
			iwinfo_dbm2mw(e->dbm + off));
	}
}


static void print_freqlist(const struct iwinfo_ops *iw, const char *ifname)
{
	int i, len, freq;
	char buf[IWINFO_BUFSIZE];
	struct iwinfo_freqlist_entry *e;

	if (iw->freqlist(ifname, buf, &len) || len <= 0)
	{
		printf("No frequency information available\n");
		return;
	}

	if (iw->frequency(ifname, &freq))
		freq = -1;

	for (i = 0; i < len; i += sizeof(struct iwinfo_freqlist_entry))
	{
		e = (struct iwinfo_freqlist_entry *) &buf[i];

		printf("%s %s (Band: %s, Channel %s) %s\n",
			(freq == e->mhz) ? "*" : " ",
			format_frequency(e->mhz),
			format_band(e->band),
			format_channel(e->channel),
			format_freqflags(e->flags));
	}
}


static void print_assoclist(const struct iwinfo_ops *iw, const char *ifname)
{
	int i, len;
	char buf[IWINFO_BUFSIZE];
	struct iwinfo_assoclist_entry *e;

	if (iw->assoclist(ifname, buf, &len))
	{
		printf("No information available\n");
		return;
	}
	else if (len <= 0)
	{
		printf("No station connected\n");
		return;
	}

	for (i = 0; i < len; i += sizeof(struct iwinfo_assoclist_entry))
	{
		e = (struct iwinfo_assoclist_entry *) &buf[i];

		printf("%s  %s / %s (SNR %d)  %d ms ago\n",
			format_bssid(e->mac),
			format_signal(e->signal),
			format_noise(e->noise),
			(e->signal - e->noise),
			e->inactive);

		printf("	RX: %-38s  %8d Pkts.\n",
			format_assocrate(&e->rx_rate),
			e->rx_packets
		);

		printf("	TX: %-38s  %8d Pkts.\n",
			format_assocrate(&e->tx_rate),
			e->tx_packets
		);

		printf("	expected throughput: %s\n\n",
			format_rate(e->thr));
	}
}


static char * lookup_country(char *buf, int len, int iso3166)
{
	int i;
	struct iwinfo_country_entry *c;

	for (i = 0; i < len; i += sizeof(struct iwinfo_country_entry))
	{
		c = (struct iwinfo_country_entry *) &buf[i];

		if (c->iso3166 == iso3166)
			return c->ccode;
	}

	return NULL;
}

static void print_countrylist(const struct iwinfo_ops *iw, const char *ifname)
{
	int len;
	char buf[IWINFO_BUFSIZE];
	char *ccode;
	char curcode[3];
	const struct iwinfo_iso3166_label *l;

	if (iw->countrylist(ifname, buf, &len))
	{
		printf("No country code information available\n");
		return;
	}

	if (iw->country(ifname, curcode))
		memset(curcode, 0, sizeof(curcode));

	for (l = IWINFO_ISO3166_NAMES; l->iso3166; l++)
	{
		if ((ccode = lookup_country(buf, len, l->iso3166)) != NULL)
		{
			printf("%s %4s	%c%c\n",
				strncmp(ccode, curcode, 2) ? " " : "*",
				ccode, (l->iso3166 / 256), (l->iso3166 % 256));
		}
	}
}

static void print_htmodelist(const struct iwinfo_ops *iw, const char *ifname)
{
	int i, htmodes = 0;

	if (iw->htmodelist(ifname, &htmodes))
	{
		printf("No HT mode information available\n");
		return;
	}

	for (i = 0; i < IWINFO_HTMODE_COUNT; i++)
		if (htmodes & (1 << i))
			printf("%s ", IWINFO_HTMODE_NAMES[i]);

	printf("\n");
}

static void lookup_phy(const struct iwinfo_ops *iw, const char *section)
{
	char buf[IWINFO_BUFSIZE];

	if (!iw->lookup_phy)
	{
		fprintf(stderr, "Not supported\n");
		return;
	}

	if (iw->lookup_phy(section, buf))
	{
		fprintf(stderr, "Phy not found\n");
		return;
	}

	printf("%s\n", buf);
}


static void lookup_path(const struct iwinfo_ops *iw, const char *phy)
{
	const char *path;

	if (!iw->phy_path || iw->phy_path(phy, &path) || !path)
		return;

	printf("%s\n", path);
}

int main(int argc, char **argv)
{
	int i, rv = 0;
	char *p;
	const struct iwinfo_ops *iw;
	glob_t globbuf;

	if (argc > 1 && argc < 3)
	{
		fprintf(stderr,
			"Usage:\n"
			"	iwinfo <device> info\n"
			"	iwinfo <device> scan\n"
			"	iwinfo <device> txpowerlist\n"
			"	iwinfo <device> freqlist\n"
			"	iwinfo <device> assoclist\n"
			"	iwinfo <device> countrylist\n"
			"	iwinfo <device> htmodelist\n"
			"	iwinfo <backend> phyname <section>\n"
		);

		return 1;
	}

	if (argc == 1)
	{
		glob("/sys/class/net/*", 0, NULL, &globbuf);

		for (i = 0; i < globbuf.gl_pathc; i++)
		{
			p = strrchr(globbuf.gl_pathv[i], '/');

			if (!p)
				continue;

			iw = iwinfo_backend(++p);

			if (!iw)
				continue;

			print_info(iw, p);
			printf("\n");
		}

		globfree(&globbuf);
		return 0;
	}

	if (argc > 3)
	{
		iw = iwinfo_backend_by_name(argv[1]);

		if (!iw)
		{
			fprintf(stderr, "No such wireless backend: %s\n", argv[1]);
			rv = 1;
		}
		else
		{
			if (!strcmp(argv[2], "path")) {
				lookup_path(iw, argv[3]);
				return 0;
			}
			switch (argv[2][0])
			{
			case 'p':
				lookup_phy(iw, argv[3]);
				break;

			default:
				fprintf(stderr, "Unknown command: %s\n", argv[2]);
				rv = 1;
			}
		}
	}
	else
	{
		iw = iwinfo_backend(argv[1]);

		if (!iw)
		{
			fprintf(stderr, "No such wireless device: %s\n", argv[1]);
			rv = 1;
		}
		else
		{
			for (i = 2; i < argc; i++)
			{
				switch(argv[i][0])
				{
				case 'i':
					print_info(iw, argv[1]);
					break;

				case 's':
					print_scanlist(iw, argv[1]);
					break;

				case 't':
					print_txpwrlist(iw, argv[1]);
					break;

				case 'f':
					print_freqlist(iw, argv[1]);
					break;

				case 'a':
					print_assoclist(iw, argv[1]);
					break;

				case 'c':
					print_countrylist(iw, argv[1]);
					break;

				case 'h':
					print_htmodelist(iw, argv[1]);
					break;

				default:
					fprintf(stderr, "Unknown command: %s\n", argv[i]);
					rv = 1;
				}
			}
		}
	}

	iwinfo_finish();

	return rv;
}