summaryrefslogtreecommitdiffstats
path: root/iw_if.h
blob: d443cf81c70bb69d90a679aabccf029a0293dc19 (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
220pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
# komro (Rosenheim)
# freq sr fec mod
[CHANNEL]
	DELIVERY_SYSTEM = DVBC/ANNEX_A
	FREQUENCY = 113000000
	SYMBOL_RATE = 6900000
	INNER_FEC = NONE
	MODULATION = QAM/64
	INVERSION = AUTO

[CHANNEL]
	DELIVERY_SYSTEM = DVBC/ANNEX_A
	FREQUENCY = 121000000
	SYMBOL_RATE = 6900000
	INNER_FEC = NONE
	MODULATION = QAM/64
	INVERSION = AUTO

[CHANNEL]
	DELIVERY_SYSTEM = DVBC/ANNEX_A
	FREQUENCY = 338000000
	SYMBOL_RATE = 6900000
	INNER_FEC = NONE
	MODULATION = QAM/256
	INVERSION = AUTO

[CHANNEL]
	DELIVERY_SYSTEM = DVBC/ANNEX_A
	FREQUENCY = 394000000
	SYMBOL_RATE = 6900000
	INNER_FEC = NONE
	MODULATION = QAM/256
	INVERSION = AUTO

[CHANNEL]
	DELIVERY_SYSTEM = DVBC/ANNEX_A
	FREQUENCY = 450000000
	SYMBOL_RATE = 6900000
	INNER_FEC = NONE
	MODULATION = QAM/64
	INVERSION = AUTO

[CHANNEL]
	DELIVERY_SYSTEM = DVBC/ANNEX_A
	FREQUENCY = 458000000
	SYMBOL_RATE = 6900000
	INNER_FEC = NONE
	MODULATION = QAM/64
	INVERSION = AUTO

[CHANNEL]
	DELIVERY_SYSTEM = DVBC/ANNEX_A
	FREQUENCY = 466000000
	SYMBOL_RATE = 6900000
	INNER_FEC = NONE
	MODULATION = QAM/64
	INVERSION = AUTO

[CHANNEL]
	DELIVERY_SYSTEM = DVBC/ANNEX_A
	FREQUENCY = 522000000
	SYMBOL_RATE = 6900000
	INNER_FEC = NONE
	MODULATION = QAM/256
	INVERSION = AUTO

[CHANNEL]
	DELIVERY_SYSTEM = DVBC/ANNEX_A
	FREQUENCY = 530000000
	SYMBOL_RATE = 6900000
	INNER_FEC = NONE
	MODULATION = QAM/256
	INVERSION = AUTO

[CHANNEL]
	DELIVERY_SYSTEM = DVBC/ANNEX_A
	FREQUENCY = 602000000
	SYMBOL_RATE = 6900000
	INNER_FEC = NONE
	MODULATION = QAM/256
	INVERSION = AUTO

[CHANNEL]
	DELIVERY_SYSTEM = DVBC/ANNEX_A
	FREQUENCY = 610000000
	SYMBOL_RATE = 6900000
	INNER_FEC = NONE
	MODULATION = QAM/256
	INVERSION = AUTO

[CHANNEL]
	DELIVERY_SYSTEM = DVBC/ANNEX_A
	FREQUENCY = 626000000
	SYMBOL_RATE = 6900000
	INNER_FEC = NONE
	MODULATION = QAM/256
	INVERSION = AUTO

[CHANNEL]
	DELIVERY_SYSTEM = DVBC/ANNEX_A
	FREQUENCY = 642000000
	SYMBOL_RATE = 6900000
	INNER_FEC = NONE
	MODULATION = QAM/256
	INVERSION = AUTO

[CHANNEL]
	DELIVERY_SYSTEM = DVBC/ANNEX_A
	FREQUENCY = 650000000
	SYMBOL_RATE = 6900000
	INNER_FEC = NONE
	MODULATION = QAM/256
	INVERSION = AUTO

[CHANNEL]
	DELIVERY_SYSTEM = DVBC/ANNEX_A
	FREQUENCY = 658000000
	SYMBOL_RATE = 6900000
	INNER_FEC = NONE
	MODULATION = QAM/64
	INVERSION = AUTO

[CHANNEL]
	DELIVERY_SYSTEM = DVBC/ANNEX_A
	FREQUENCY = 666000000
	SYMBOL_RATE = 6900000
	INNER_FEC = NONE
	MODULATION = QAM/64
	INVERSION = AUTO

[CHANNEL]
	DELIVERY_SYSTEM = DVBC/ANNEX_A
	FREQUENCY = 682000000
	SYMBOL_RATE = 6900000
	INNER_FEC = NONE
	MODULATION = QAM/64
	INVERSION = AUTO

[CHANNEL]
	DELIVERY_SYSTEM = DVBC/ANNEX_A
	FREQUENCY = 690000000
	SYMBOL_RATE = 6900000
	INNER_FEC = NONE
	MODULATION = QAM/256
	INVERSION = AUTO

[CHANNEL]
	DELIVERY_SYSTEM = DVBC/ANNEX_A
	FREQUENCY = 698000000
	SYMBOL_RATE = 6900000
	INNER_FEC = NONE
	MODULATION = QAM/256
	INVERSION = AUTO

[CHANNEL]
	DELIVERY_SYSTEM = DVBC/ANNEX_A
	FREQUENCY = 706000000
	SYMBOL_RATE = 6900000
	INNER_FEC = NONE
	MODULATION = QAM/256
	INVERSION = AUTO

[CHANNEL]
	DELIVERY_SYSTEM = DVBC/ANNEX_A
	FREQUENCY = 714000000
	SYMBOL_RATE = 6900000
	INNER_FEC = NONE
	MODULATION = QAM/256
	INVERSION = AUTO

[CHANNEL]
	DELIVERY_SYSTEM = DVBC/ANNEX_A
	FREQUENCY = 722000000
	SYMBOL_RATE = 6900000
	INNER_FEC = NONE
	MODULATION = QAM/256
	INVERSION = AUTO

[CHANNEL]
	DELIVERY_SYSTEM = DVBC/ANNEX_A
	FREQUENCY = 730000000
	SYMBOL_RATE = 6900000
	INNER_FEC = NONE
	MODULATION = QAM/64
	INVERSION = AUTO

[CHANNEL]
	DELIVERY_SYSTEM = DVBC/ANNEX_A
	FREQUENCY = 738000000
	SYMBOL_RATE = 6900000
	INNER_FEC = NONE
	MODULATION = QAM/64
	INVERSION = AUTO

[CHANNEL]
	DELIVERY_SYSTEM = DVBC/ANNEX_A
	FREQUENCY = 746000000
	SYMBOL_RATE = 6900000
	INNER_FEC = NONE
	MODULATION = QAM/64
	INVERSION = AUTO

[CHANNEL]
	DELIVERY_SYSTEM = DVBC/ANNEX_A
	FREQUENCY = 754000000
	SYMBOL_RATE = 6900000
	INNER_FEC = NONE
	MODULATION = QAM/64
	INVERSION = AUTO

[CHANNEL]
	DELIVERY_SYSTEM = DVBC/ANNEX_A
	FREQUENCY = 762000000
	SYMBOL_RATE = 6900000
	INNER_FEC = NONE
	MODULATION = QAM/256
	INVERSION = AUTO

[CHANNEL]
	DELIVERY_SYSTEM = DVBC/ANNEX_A
	FREQUENCY = 778000000
	SYMBOL_RATE = 6900000
	INNER_FEC = NONE
	MODULATION = QAM/64
	INVERSION = AUTO
n506'>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
/*
 * wavemon - a wireless network monitoring aplication
 *
 * Copyright (c) 2001-2002 Jan Morgenstern <jan@jm-music.de>
 *
 * wavemon is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2, or (at your option) any later
 * version.
 *
 * wavemon 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 wavemon; see the file COPYING.  If not, write to the Free Software
 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
#include "wavemon.h"
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/ether.h>
#include <net/if_arp.h>
#include <net/ethernet.h>
#include <sys/socket.h>
#include <linux/if.h>
#include <linux/wireless.h>

/* Definitions that appeared in more recent versions of wireless.h */
#ifndef IW_POWER_SAVING
#define IW_POWER_SAVING	0x4000		/* version 20 -> 21 */
#endif
#ifndef IW_MODE_MESH
#define IW_MODE_MESH	7		/* introduced in 2.6.26-rc1 */
#endif

/* Maximum length of a MAC address: 2 * 6 hex digits, 6 - 1 colons, plus '\0' */
#define MAC_ADDR_MAX	18

/*
 * Threshold for 'sane' noise levels.
 *
 * Some drivers simply set an arbitrary minimum noise level to mean 'invalid',
 * but do not set IW_QUAL_NOISE_INVALID so that the display gets stuck at a
 * "house number". The value below is suggested by and taken from the iwl3945
 * driver (constant IWL_NOISE_MEAS_NOT_AVAILABLE in iwl-3945.h).
 */
#define NOISE_DBM_SANE_MIN	-127

/**
 * struct if_info  -  wireless interface network information
 * @hwaddr:		MAC address
 * @addr:		IPv4 interface address
 * @netmask:		IPv4 interface netmask
 * @bcast:		IPv4 interface broadcast address
 * @mtu:		interface MTU
 * @txqlen:		tx queue length
 * @flags:		interface flags
 * See also netdevice(7)
 */
struct if_info {
	struct ether_addr	hwaddr;
	struct in_addr		addr,
				netmask,
				bcast;
	uint16_t		mtu;
	short			txqlen;
	short			flags;
};
extern bool if_is_up(int skfd, const char *ifname);
extern int  if_set_up(int skfd, const char *ifname);
extern void if_getinf(const char *ifname, struct if_info *info);

/**
 * struct iw_key  -  Encoding information
 * @key:	encryption key
 * @size:	length of @key in bytes
 * @flags:	flags reported by SIOCGIWENCODE
 */
struct iw_key {
	uint8_t		key[IW_ENCODING_TOKEN_MAX];
	uint16_t	size;
	uint16_t	flags;
};

/**
 * struct iw_dyn_info  -  modified iw_req
 * @name:	interface name
 * @mode:	current operation mode (IW_MODE_xxx)
 *
 * @cap_*:	indicating capability/presence
 *
 * @essid:	Extended Service Set ID (network name)
 * @essid_ct:	index number of the @essid (starts at 1, 0 = off)
 * @nickname:	optional station nickname
 * @nwid:	Network ID (pre-802.11 hardware only)
 * @ap_addr:	BSSID or IBSSID
 *
 * @retry:	MAC-retransmission retry behaviour
 * @rts:	minimum packet size for which to perform RTS/CTS handshake
 * @frag:	802.11 frame fragmentation threshold size
 * @txpower:	TX power information
 * @power	power management information
 *
 * @freq:	frequency in Hz
 * @sens:	sensitivity threshold of the card
 * @bitrate:	bitrate (client mode)
 *
 * @keys:	array of encryption keys
 * @nkeys:	length of @keys
 * @active_key:	index of current key into @keys (counting from 1)
 *
 */
struct iw_dyn_info {
	char		name[IFNAMSIZ];
	uint8_t		mode;

	bool		cap_essid:1,
			cap_nwid:1,
			cap_nickname:1,
			cap_freq:1,
			cap_sens:1,
			cap_txpower:1,
			cap_retry:1,
			cap_rts:1,
			cap_frag:1,
			cap_mode:1,
			cap_ap:1,
			cap_power:1,
			cap_aplist:1;

	char		essid[IW_ESSID_MAX_SIZE+2];
	uint8_t		essid_ct;
	char		nickname[IW_ESSID_MAX_SIZE+2];
	struct iw_param nwid;
	struct sockaddr ap_addr;

	struct iw_param retry;
	struct iw_param rts;
	struct iw_param frag;
	struct iw_param txpower;
	struct iw_param power;

	float		freq;
	int32_t		sens;
	unsigned long	bitrate;

	struct iw_key	*keys;
	uint8_t		nkeys;
	uint8_t		active_key;
};

/* Return the number of encryption keys marked 'active' in @info */
static inline uint8_t dyn_info_active_keys(struct iw_dyn_info *info)
{
	int i, num_active = 0;

	for (i = 0; i < info->nkeys; i++)
		num_active += info->keys[i].size &&
			      !(info->keys[i].flags & IW_ENCODE_DISABLED);
	return num_active;
}

/* Return the number of 40-bit/104-bit keys in @info */
static inline uint8_t dyn_info_wep_keys(struct iw_dyn_info *info)
{
	int i, num_wep = 0;

	for (i = 0; i < info->nkeys; i++)
		if (!(info->keys[i].flags & IW_ENCODE_DISABLED))
			num_wep += info->keys[i].size == 5 ||
				   info->keys[i].size == 13;
	return num_wep;
}
extern void dyn_info_get(struct iw_dyn_info *info,
			 const char *ifname, struct iw_range *ir);
extern void dyn_info_cleanup(struct iw_dyn_info *info);


/**
 * struct if_stat  -  Packet/byte counts for interfaces
 */
struct if_stat {
	unsigned long long	rx_packets,
				tx_packets;
	unsigned long long	rx_bytes,
				tx_bytes;
};

extern void if_getstat(const char *ifname, struct if_stat *stat);

/*
 *	 Structs to communicate WiFi statistics
 */
struct iw_levelstat {
	float	signal;		/* signal level in dBm */
	float	noise;		/* noise  level in dBm */
	uint8_t	flags;		/* level validity      */
};
#define IW_LSTAT_INIT { 0, 0, IW_QUAL_LEVEL_INVALID | IW_QUAL_NOISE_INVALID }

extern void iw_getinf_range(const char *ifname, struct iw_range *range);
extern void iw_sanitize(struct iw_range *range,
			struct iw_quality *qual,
			struct iw_levelstat *dbm);

/**
 * struct iw_stat - record current WiFi state
 * @range:	current range information
 * @stats:	current signal level statistics
 * @dbm:	the noise/signal of @stats in dBm
 */
struct iw_stat {
	struct iw_range		range;
	struct iw_statistics	stat;
	struct iw_levelstat	dbm;
};

/*
 * 	Periodic sampling of wireless statistics via timer alarm
 */
extern void iw_getstat(struct iw_stat *stat);
extern void iw_cache_update(struct iw_stat *stat);

extern void sampling_init(void (*sampling_handler)(int));
extern void sampling_do_poll(void);
static inline void sampling_stop(void)	{ alarm(0); }

/*
 *	Organization of scan results
 */
/**
 * struct scan_result  -  Ranked list of scan results
 * @ap_addr:	MAC address
 * @essid:	station SSID (may be empty)
 * @mode:	operation mode (type of station)
 * @freq:	frequency/channel information
 * @qual:	signal quality information
 * @has_key:	whether using encryption or not
 * @flags:	properties gathered from Information Elements
 * @next:	next, lower-ranking entry
 */
struct scan_result {
	struct ether_addr	ap_addr;
	char			essid[IW_ESSID_MAX_SIZE + 2];
	int			mode;
	double			freq;
	struct	iw_quality	qual;

	int 			has_key:1;
	uint32_t		flags;

	struct scan_result *next;
};
extern struct scan_result *get_scan_list(int skfd, const char *ifname, int we_version);
extern void free_scan_result(struct scan_result *head);

/**
 * struct cnt - count frequency of integer numbers
 * @val:	value to count
 * @count:	how often @val occurs
 */
struct cnt {
	int	val;
	int	count;
};
extern struct cnt *channel_stats(struct scan_result *head,
				 struct iw_range *iw_range, int *max_cnt);

/*
 *	General helper routines
 */
static inline const char *iw_opmode(const uint8_t mode)
{
	static char *modes[] = {
		[IW_MODE_AUTO]	  = "Auto",
		[IW_MODE_ADHOC]	  = "Ad-Hoc",
		[IW_MODE_INFRA]	  = "Managed",
		[IW_MODE_MASTER]  = "Master",
		[IW_MODE_REPEAT]  = "Repeater",
		[IW_MODE_SECOND]  = "Secondary",
		[IW_MODE_MONITOR] = "Monitor",
		[IW_MODE_MESH]	  = "Mesh"
	};

	return mode < ARRAY_SIZE(modes) ? modes[mode] : "Unknown/bug";
}

/* Print a mac-address, include leading zeroes (unlike ether_ntoa(3)) */
static inline char *ether_addr(const struct ether_addr *ea)
{
	static char mac[MAC_ADDR_MAX];
	char *d = mac, *a = ether_ntoa(ea);
next_chunk:
	if (a[0] == '\0' || a[1] == '\0' || a[1] == ':')
		*d++ = '0';
	while ((*d++ = conf.cisco_mac ? (*a == ':' ? '.' : *a) : toupper(*a)))
		if (*a++ == ':')
			goto next_chunk;
	return mac;
}

/* Print mac-address translation from /etc/ethers if available */
static inline char *ether_lookup(const struct ether_addr *ea)
{
	static char hostname[BUFSIZ];

	if (ether_ntohost(hostname, ea) == 0)
		return hostname;
	return ether_addr(ea);
}

/* Format an Ethernet mac address */
static inline char *mac_addr(const struct sockaddr *sa)
{
	if (sa->sa_family != ARPHRD_ETHER)
		return "00:00:00:00:00:00";
	return ether_lookup((const struct ether_addr *)sa->sa_data);
}

/* Format a (I)BSSID */
static inline char *format_bssid(const struct sockaddr *ap)
{
	uint8_t bcast_addr[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
	uint8_t  zero_addr[ETH_ALEN] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

	if (memcmp(ap->sa_data, zero_addr, ETH_ALEN) == 0)
		return "Not-Associated";
	if (memcmp(ap->sa_data, bcast_addr, ETH_ALEN) == 0)
		return "Invalid";
	return mac_addr(ap);
}

/* count bits set in @mask the Brian Kernighan way */
static inline uint8_t bit_count(uint32_t mask)
{
	uint8_t bits_set;

	for (bits_set = 0; mask; bits_set++)
		mask &= mask - 1;

	return bits_set;
}

/* netmask = contiguous 1's followed by contiguous 0's */
static inline uint8_t prefix_len(const struct in_addr *netmask)
{
	return bit_count(netmask->s_addr);
}

/* Absolute power measurement in dBm (IW_QUAL_DBM): map into -192 .. 63 range */
static inline int u8_to_dbm(const int power)
{
	return power > 63 ? power - 0x100 : power;
}
static inline uint8_t dbm_to_u8(const int dbm)
{
	return dbm < 0 ? dbm + 0x100 : dbm;
}

/* Convert log dBm values to linear mW */
static inline double dbm2mw(const double in)
{
	return pow(10.0, in / 10.0);
}

static inline char *dbm2units(const double in)
{
	static char with_units[0x100];
	double val = dbm2mw(in);

	if (val < 0.00000001) {
		sprintf(with_units, "%.2f pW", val * 1e9);
	} else if (val < 0.00001) {
		sprintf(with_units, "%.2f nW", val * 1e6);
	} else if (val < 0.01) {
		sprintf(with_units, "%.2f uW", val * 1e3);
	} else {
		sprintf(with_units, "%.2f mW", val);
	}
	return with_units;
}

/* Convert linear mW values to log dBm */
static inline double mw2dbm(const double in)
{
	return 10.0 * log10(in);
}

/* Format driver TX power information */
static inline char *format_txpower(const struct iw_param *txpwr)
{
	static char txline[0x40];

	if (txpwr->flags & IW_TXPOW_RELATIVE)
		snprintf(txline, sizeof(txline), "%d (no units)", txpwr->value);
	else if (txpwr->flags & IW_TXPOW_MWATT)
		snprintf(txline, sizeof(txline), "%.0f dBm (%d mW)",
				mw2dbm(txpwr->value), txpwr->value);
	else
		snprintf(txline, sizeof(txline), "%d dBm (%.2f mW)",
				txpwr->value, dbm2mw(txpwr->value));
	return txline;
}

/* Format driver Power Management information */
static inline char *format_power(const struct iw_param *pwr,
				 const struct iw_range *range)
{
	static char buf[0x80];
	double val = pwr->value;
	int len = 0;

	if (pwr->disabled)
		return "off";
	else if (pwr->flags == IW_POWER_ON)
		return "on";

	if (pwr->flags & IW_POWER_MIN)
		len += snprintf(buf + len, sizeof(buf) - len, "min ");
	if (pwr->flags & IW_POWER_MAX)
		len += snprintf(buf + len, sizeof(buf) - len, "max ");

	if (pwr->flags & IW_POWER_TIMEOUT)
		len += snprintf(buf + len, sizeof(buf) - len, "timeout ");
	else if (pwr->flags & IW_POWER_SAVING)
		len += snprintf(buf + len, sizeof(buf) - len, "saving ");
	else
		len += snprintf(buf + len, sizeof(buf) - len, "period ");

	if (pwr->flags & IW_POWER_RELATIVE && range->we_version_compiled < 21)
		len += snprintf(buf + len, sizeof(buf) - len, "%+g", val/1e6);
	else if (pwr->flags & IW_POWER_RELATIVE)
		len += snprintf(buf + len, sizeof(buf) - len, "%+g", val);
	else if (val > 1e6)
		len += snprintf(buf + len, sizeof(buf) - len, "%g s", val/1e6);
	else if (val > 1e3)
		len += snprintf(buf + len, sizeof(buf) - len, "%g ms", val/1e3);
	else
		len += snprintf(buf + len, sizeof(buf) - len, "%g us", val);

	switch (pwr->flags & IW_POWER_MODE) {
	case IW_POWER_UNICAST_R:
		len += snprintf(buf + len, sizeof(buf) - len, ", rcv unicast");
		break;
	case IW_POWER_MULTICAST_R:
		len += snprintf(buf + len, sizeof(buf) - len, ", rcv mcast");
		break;
	case IW_POWER_ALL_R:
		len += snprintf(buf + len, sizeof(buf) - len, ", rcv all");
		break;
	case IW_POWER_FORCE_S:
		len += snprintf(buf + len, sizeof(buf) - len, ", force send");
		break;
	case IW_POWER_REPEATER:
		len += snprintf(buf + len, sizeof(buf) - len, ", repeat mcast");
	}

	return buf;
}

/* See comments on 'struct iw_freq' in wireless.h */
static inline float freq_to_hz(const struct iw_freq *freq)
{
	return freq->m * pow(10, freq->e);
}

/* Return frequency or 0 on error. Based on iw_channel_to_freq() */
static inline double channel_to_freq(uint8_t chan, const struct iw_range *range)
{
	int c;

	for (c = 0; c < range->num_frequency; c++)
		/* Check if it actually has stored a frequency */
		if (range->freq[c].i == chan && range->freq[c].m > 1000)
			return freq_to_hz(&range->freq[c]);
	return 0.0;
}

/* Return channel number or -1 on error. Based on iw_freq_to_channel() */
static inline int freq_to_channel(double freq, const struct iw_range *range)
{
	int i;

	if (freq < 1.0e3)
		return -1;

	for (i = 0; i < range->num_frequency; i++)
		if (freq_to_hz(&range->freq[i]) == freq)
			return range->freq[i].i;
	return -1;
}

/* print @key in cleartext if it is in ASCII format, use hex format otherwise */
static inline char *format_key(const struct iw_key *const iwk)
{
	static char buf[128];
	int i, is_printable = 0, len = 0;

	/* Over-estimate key size: 2 chars per hex digit plus '-' */
	assert(iwk != NULL && iwk->size * 3 < sizeof(buf));

	for (i = 0; i < iwk->size && (is_printable = isprint(iwk->key[i])); i++)
		;

	if (is_printable)
		len += sprintf(buf, "\"");

	for (i = 0; i < iwk->size; i++)
		if (is_printable) {
			len += sprintf(buf + len, "%c", iwk->key[i]);
		} else {
			if (i > 0 && (i & 1) == 0)
				len += sprintf(buf + len, "-");
			len += sprintf(buf + len, "%02X", iwk->key[i]);
		}

	if (is_printable)
		len += sprintf(buf + len, "\"");

	sprintf(buf + len, " (%u bits)", iwk->size * 8);

	return buf;
}

/* Human-readable representation of IW_ENC_CAPA_ types */
static inline const char *format_enc_capab(const uint32_t capa, const char *sep)
{
	static char buf[32];
	size_t len = 0, max = sizeof(buf);

	if (capa & IW_ENC_CAPA_WPA)
		len = snprintf(buf, max, "WPA");
	if (capa & IW_ENC_CAPA_WPA2)
		len += snprintf(buf + len, max - len, "%sWPA2", len ? sep : "");
	if (capa & IW_ENC_CAPA_CIPHER_TKIP)
		len += snprintf(buf + len, max - len, "%sTKIP", len ? sep : "");
	if (capa & IW_ENC_CAPA_CIPHER_CCMP)
		len += snprintf(buf + len, max - len, "%sCCMP", len ? sep : "");
	buf[len] = '\0';
	return buf;
}

/* Display only the supported WPA type */
#define IW_WPA_MASK	(IW_ENC_CAPA_WPA|IW_ENC_CAPA_WPA2)
static inline const char *format_wpa(struct iw_range *ir)
{
	return format_enc_capab(ir->enc_capa & IW_WPA_MASK, "/");
}

static inline char *format_retry(const struct iw_param *retry,
				 const struct iw_range *range)
{
	static char buf[0x80];
	double val = retry->value;
	int len = 0;

	if (retry->disabled)
		return "off";
	else if (retry->flags == IW_RETRY_ON)
		return "on";

	if (retry->flags & IW_RETRY_MIN)
		len += snprintf(buf + len, sizeof(buf) - len, "min ");
	if (retry->flags & IW_RETRY_MAX)
		len += snprintf(buf + len, sizeof(buf) - len, "max ");
	if (retry->flags & IW_RETRY_SHORT)
		len += snprintf(buf + len, sizeof(buf) - len, "short ");
	if (retry->flags & IW_RETRY_LONG)
		len += snprintf(buf + len, sizeof(buf) - len, "long ");

	if (retry->flags & IW_RETRY_LIFETIME)
		len += snprintf(buf + len, sizeof(buf) - len, "lifetime ");
	else {
		snprintf(buf + len, sizeof(buf) - len, "limit %d", retry->value);
		return buf;
	}

	if (retry->flags & IW_RETRY_RELATIVE && range->we_version_compiled < 21)
		len += snprintf(buf + len, sizeof(buf) - len, "%+g", val/1e6);
	else if (retry->flags & IW_RETRY_RELATIVE)
		len += snprintf(buf + len, sizeof(buf) - len, "%+g", val);
	else if (val > 1e6)
		len += snprintf(buf + len, sizeof(buf) - len, "%g s", val/1e6);
	else if (val > 1e3)
		len += snprintf(buf + len, sizeof(buf) - len, "%g ms", val/1e3);
	else
		len += snprintf(buf + len, sizeof(buf) - len, "%g us", val);

	return buf;
}