]>
git.ipfire.org Git - thirdparty/iw.git/blob - util.c
2 #include <netlink/attr.h>
8 int 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
]);
25 int mac_addr_a2n(unsigned char *mac_addr
, char *arg
)
29 for (i
= 0; i
< ETH_ALEN
; i
++) {
31 char *cp
= strchr(arg
, ':');
36 if (sscanf(arg
, "%x", &temp
) != 1)
38 if (temp
< 0 || temp
> 255)
52 static const char *ifmodes
[NL80211_IFTYPE_MAX
+ 1] = {
63 static char modebuf
[100];
65 const char *iftype_name(enum nl80211_iftype iftype
)
67 if (iftype
<= NL80211_IFTYPE_MAX
)
68 return ifmodes
[iftype
];
69 sprintf(modebuf
, "Unknown mode (%d)", iftype
);
73 int ieee80211_channel_to_frequency(int chan
)
76 return 2407 + chan
* 5;
81 /* FIXME: dot11ChannelStartingFactor (802.11-2007 17.3.8.3.2) */
82 return (chan
+ 1000) * 5;
85 int ieee80211_frequency_to_channel(int freq
)
91 return (freq
- 2407) / 5;
93 /* FIXME: dot11ChannelStartingFactor (802.11-2007 17.3.8.3.2) */
97 void print_ssid_escaped(const uint8_t len
, const uint8_t *data
)
101 for (i
= 0; i
< len
; i
++) {
102 if (isprint(data
[i
]))
103 printf("%c", data
[i
]);
105 printf("\\x%.2x", data
[i
]);
109 static int hex2num(char digit
)
111 if (!isxdigit(digit
))
115 return tolower(digit
) - 'a' + 10;
118 static int hex2byte(char *hex
)
122 d1
= hex2num(hex
[0]);
125 d2
= hex2num(hex
[1]);
128 return (d1
<< 4) | d2
;
131 static char *hex2bin(char *hex
, char *buf
)
148 int parse_keys(struct nl_msg
*msg
, char **argv
, int argc
)
157 NLA_PUT_FLAG(msg
, NL80211_ATTR_PRIVACY
);
159 keys
= nla_nest_start(msg
, NL80211_ATTR_KEYS
);
166 struct nlattr
*key
= nla_nest_start(msg
, ++i
);
172 if (arg
[pos
] == 'd') {
173 NLA_PUT_FLAG(msg
, NL80211_KEY_DEFAULT
);
179 if (!isdigit(arg
[pos
]))
181 NLA_PUT_U8(msg
, NL80211_KEY_IDX
, arg
[pos
++] - '0');
182 if (arg
[pos
++] != ':')
185 switch (strlen(keydata
)) {
187 keydata
= hex2bin(keydata
, keybuf
);
189 NLA_PUT_U32(msg
, NL80211_KEY_CIPHER
, 0x000FAC01);
193 keydata
= hex2bin(keydata
, keybuf
);
195 NLA_PUT_U32(msg
, NL80211_KEY_CIPHER
, 0x000FAC05);
205 NLA_PUT(msg
, NL80211_KEY_DATA
, keylen
, keydata
);
207 nla_nest_end(msg
, key
);
212 nla_nest_end(msg
, keys
);
218 fprintf(stderr
, "key must be [d:]index:data where\n"
219 " 'd:' means default (transmit) key\n"
220 " 'index:' is a single digit (0-3)\n"
221 " 'data' must be 5 or 13 ascii chars\n"
222 " or 10 or 26 hex digits\n"
223 "for example: d:2:6162636465 is the same as d:2:abcde\n");