2 * iwinfo - Wireless Information Library - Shared utility routines
4 * Copyright (C) 2010 Jo-Philipp Wich <xm@subsignal.org>
6 * The iwinfo library is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation.
10 * The iwinfo library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with the iwinfo library. If not, see http://www.gnu.org/licenses/.
18 * The signal handling code is derived from the official madwifi tools,
19 * wlanconfig.c in particular. The encryption property handling was
20 * inspired by the hostapd madwifi driver.
23 #include "iwinfo/utils.h"
26 static int ioctl_socket = -1;
28 static int iwinfo_ioctl_socket(void)
31 if( ioctl_socket == -1 )
33 ioctl_socket = socket(AF_INET, SOCK_DGRAM, 0);
34 fcntl(ioctl_socket, F_SETFD, fcntl(ioctl_socket, F_GETFD) | FD_CLOEXEC);
40 int iwinfo_ioctl(int cmd, void *ifr)
42 int s = iwinfo_ioctl_socket();
43 return ioctl(s, cmd, ifr);
46 int iwinfo_dbm2mw(int in)
53 for(k = 0; k < ip; k++) res *= 10;
54 for(k = 0; k < fp; k++) res *= LOG10_MAGIC;
59 int iwinfo_mw2dbm(int in)
61 double fin = (double) in;
79 int iwinfo_ifup(const char *ifname)
83 strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
85 if( iwinfo_ioctl(SIOCGIFFLAGS, &ifr) )
88 ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
90 return !iwinfo_ioctl(SIOCSIFFLAGS, &ifr);
93 int iwinfo_ifdown(const char *ifname)
97 strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
99 if( iwinfo_ioctl(SIOCGIFFLAGS, &ifr) )
102 ifr.ifr_flags &= ~(IFF_UP | IFF_RUNNING);
104 return !iwinfo_ioctl(SIOCSIFFLAGS, &ifr);
107 int iwinfo_ifmac(const char *ifname)
111 strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
113 if( iwinfo_ioctl(SIOCGIFHWADDR, &ifr) )
116 ifr.ifr_hwaddr.sa_data[1]++;
117 ifr.ifr_hwaddr.sa_data[2]++;
119 return !iwinfo_ioctl(SIOCSIFHWADDR, &ifr);
122 void iwinfo_close(void)
124 if( ioctl_socket > -1 )