]>
git.ipfire.org Git - thirdparty/iw.git/blob - util.c
2 #include <netlink/attr.h>
8 void mac_addr_n2a(char *mac_addr
, unsigned char *arg
)
13 for (i
= 0; i
< ETH_ALEN
; i
++) {
15 sprintf(mac_addr
+l
, "%02x", arg
[i
]);
18 sprintf(mac_addr
+l
, ":%02x", arg
[i
]);
24 int mac_addr_a2n(unsigned char *mac_addr
, char *arg
)
28 for (i
= 0; i
< ETH_ALEN
; i
++) {
30 char *cp
= strchr(arg
, ':');
35 if (sscanf(arg
, "%x", &temp
) != 1)
37 if (temp
< 0 || temp
> 255)
51 static const char *ifmodes
[NL80211_IFTYPE_MAX
+ 1] = {
62 static char modebuf
[100];
64 const char *iftype_name(enum nl80211_iftype iftype
)
66 if (iftype
<= NL80211_IFTYPE_MAX
)
67 return ifmodes
[iftype
];
68 sprintf(modebuf
, "Unknown mode (%d)", iftype
);
72 static const char *commands
[NL80211_CMD_MAX
+ 1] = {
114 "michael_mic_failure",
125 static char cmdbuf
[100];
127 const char *command_name(enum nl80211_commands cmd
)
129 if (cmd
<= NL80211_CMD_MAX
)
130 return commands
[cmd
];
131 sprintf(cmdbuf
, "Unknown command (%d)", cmd
);
135 int ieee80211_channel_to_frequency(int chan
)
138 return 2407 + chan
* 5;
143 /* FIXME: dot11ChannelStartingFactor (802.11-2007 17.3.8.3.2) */
144 return (chan
+ 1000) * 5;
147 int ieee80211_frequency_to_channel(int freq
)
153 return (freq
- 2407) / 5;
155 /* FIXME: dot11ChannelStartingFactor (802.11-2007 17.3.8.3.2) */
156 return freq
/5 - 1000;
159 void print_ssid_escaped(const uint8_t len
, const uint8_t *data
)
163 for (i
= 0; i
< len
; i
++) {
164 if (isprint(data
[i
]))
165 printf("%c", data
[i
]);
167 printf("\\x%.2x", data
[i
]);
171 static int hex2num(char digit
)
173 if (!isxdigit(digit
))
177 return tolower(digit
) - 'a' + 10;
180 static int hex2byte(char *hex
)
184 d1
= hex2num(hex
[0]);
187 d2
= hex2num(hex
[1]);
190 return (d1
<< 4) | d2
;
193 static char *hex2bin(char *hex
, char *buf
)
210 int parse_keys(struct nl_msg
*msg
, char **argv
, int argc
)
214 bool have_default
= false;
220 NLA_PUT_FLAG(msg
, NL80211_ATTR_PRIVACY
);
222 keys
= nla_nest_start(msg
, NL80211_ATTR_KEYS
);
229 struct nlattr
*key
= nla_nest_start(msg
, ++i
);
235 if (arg
[pos
] == 'd') {
236 NLA_PUT_FLAG(msg
, NL80211_KEY_DEFAULT
);
243 if (!isdigit(arg
[pos
]))
245 NLA_PUT_U8(msg
, NL80211_KEY_IDX
, arg
[pos
++] - '0');
246 if (arg
[pos
++] != ':')
249 switch (strlen(keydata
)) {
251 keydata
= hex2bin(keydata
, keybuf
);
253 NLA_PUT_U32(msg
, NL80211_KEY_CIPHER
, 0x000FAC01);
257 keydata
= hex2bin(keydata
, keybuf
);
259 NLA_PUT_U32(msg
, NL80211_KEY_CIPHER
, 0x000FAC05);
269 NLA_PUT(msg
, NL80211_KEY_DATA
, keylen
, keydata
);
274 /* one key should be TX key */
275 if (!have_default
&& !argc
)
276 NLA_PUT_FLAG(msg
, NL80211_KEY_DEFAULT
);
278 nla_nest_end(msg
, key
);
281 nla_nest_end(msg
, keys
);
287 fprintf(stderr
, "key must be [d:]index:data where\n"
288 " 'd:' means default (transmit) key\n"
289 " 'index:' is a single digit (0-3)\n"
290 " 'data' must be 5 or 13 ascii chars\n"
291 " or 10 or 26 hex digits\n"
292 "for example: d:2:6162636465 is the same as d:2:abcde\n");
296 void print_mcs_set(const uint8_t *data
)
300 for (i
= 15; i
!= 0; i
--) {
301 printf(" %.2x", data
[i
]);