]>
git.ipfire.org Git - thirdparty/iw.git/blob - util.c
2 #include <netlink/attr.h>
12 void mac_addr_n2a(char *mac_addr
, unsigned char *arg
)
17 for (i
= 0; i
< ETH_ALEN
; i
++) {
19 sprintf(mac_addr
+l
, "%02x", arg
[i
]);
22 sprintf(mac_addr
+l
, ":%02x", arg
[i
]);
28 int mac_addr_a2n(unsigned char *mac_addr
, char *arg
)
32 for (i
= 0; i
< ETH_ALEN
; i
++) {
34 char *cp
= strchr(arg
, ':');
39 if (sscanf(arg
, "%x", &temp
) != 1)
41 if (temp
< 0 || temp
> 255)
55 static const char *ifmodes
[NL80211_IFTYPE_MAX
+ 1] = {
66 static char modebuf
[100];
68 const char *iftype_name(enum nl80211_iftype iftype
)
70 if (iftype
<= NL80211_IFTYPE_MAX
)
71 return ifmodes
[iftype
];
72 sprintf(modebuf
, "Unknown mode (%d)", iftype
);
76 int ieee80211_channel_to_frequency(int chan
)
79 return 2407 + chan
* 5;
84 /* FIXME: dot11ChannelStartingFactor (802.11-2007 17.3.8.3.2) */
85 return (chan
+ 1000) * 5;
88 int ieee80211_frequency_to_channel(int freq
)
94 return (freq
- 2407) / 5;
96 /* FIXME: dot11ChannelStartingFactor (802.11-2007 17.3.8.3.2) */
100 void print_ssid_escaped(const uint8_t len
, const uint8_t *data
)
104 for (i
= 0; i
< len
; i
++) {
105 if (isprint(data
[i
]))
106 printf("%c", data
[i
]);
108 printf("\\x%.2x", data
[i
]);
112 static int hex2num(char digit
)
114 if (!isxdigit(digit
))
118 return tolower(digit
) - 'a' + 10;
121 static int hex2byte(char *hex
)
125 d1
= hex2num(hex
[0]);
128 d2
= hex2num(hex
[1]);
131 return (d1
<< 4) | d2
;
134 static char *hex2bin(char *hex
, char *buf
)
151 int parse_keys(struct nl_msg
*msg
, char **argv
, int argc
)
160 NLA_PUT_FLAG(msg
, NL80211_ATTR_PRIVACY
);
162 keys
= nla_nest_start(msg
, NL80211_ATTR_KEYS
);
169 struct nlattr
*key
= nla_nest_start(msg
, ++i
);
175 if (arg
[pos
] == 'd') {
176 NLA_PUT_FLAG(msg
, NL80211_KEY_DEFAULT
);
182 if (!isdigit(arg
[pos
]))
184 NLA_PUT_U8(msg
, NL80211_KEY_IDX
, arg
[pos
++] - '0');
185 if (arg
[pos
++] != ':')
188 switch (strlen(keydata
)) {
190 keydata
= hex2bin(keydata
, keybuf
);
192 NLA_PUT_U32(msg
, NL80211_KEY_CIPHER
, 0x000FAC01);
196 keydata
= hex2bin(keydata
, keybuf
);
198 NLA_PUT_U32(msg
, NL80211_KEY_CIPHER
, 0x000FAC05);
208 NLA_PUT(msg
, NL80211_KEY_DATA
, keylen
, keydata
);
210 nla_nest_end(msg
, key
);
215 nla_nest_end(msg
, keys
);
221 fprintf(stderr
, "key must be [d:]index:data where\n"
222 " 'd:' means default (transmit) key\n"
223 " 'index:' is a single digit (0-3)\n"
224 " 'data' must be 5 or 13 ascii chars\n"
225 " or 10 or 26 hex digits\n"
226 "for example: d:2:6162636465 is the same as d:2:abcde\n");
230 int set_interface_up(const char *ifname
)
233 int fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
239 memset(&ifr
, 0, sizeof(ifr
));
240 strncpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
241 if (ioctl(fd
, SIOCGIFFLAGS
, &ifr
)) {
246 if (ifr
.ifr_flags
& IFF_UP
)
249 ifr
.ifr_flags
|= IFF_UP
;
250 if (ioctl(fd
, SIOCSIFFLAGS
, &ifr
))