copy trunk to buildroot-ng branch
[openwrt.git] / package / wiviz / src / wl_access.c
1 /*
2 This file is part of Wi-viz (http://wiviz.natetrue.com).
3
4 Wi-viz is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License v2 as published by
6 the Free Software Foundation.
7
8 Wi-viz is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with Wi-viz; if not, write to the Free Software
15 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16 */
17 #include <stdio.h>
18 #include <unistd.h>
19 #include <string.h>
20 #include <errno.h>
21 #include <sys/ioctl.h>
22 #include <net/if.h>
23
24 #include "wl_access.h"
25
26 int wl_ioctl(char *name, int cmd, void *buf, int len)
27 {
28         struct ifreq ifr;
29         wl_ioctl_t ioc;
30         int ret = 0;
31         int s;
32
33         /* open socket to kernel */
34         if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
35                 perror("socket");
36                 return errno;
37         }
38
39         /* do it */
40         ioc.cmd = cmd;
41         ioc.buf = buf;
42         ioc.len = len;
43         strncpy(ifr.ifr_name, name, IFNAMSIZ);
44         ifr.ifr_data = (caddr_t) &ioc;
45         ret = ioctl(s, SIOCDEVPRIVATE, &ifr);
46
47         /* cleanup */
48         close(s);
49         return ret;
50 }
51
52 int get_mac(char *name, void *buf)
53 {
54         struct ifreq ifr;
55         int ret = 0;
56         int s;
57
58         /* open socket to kernel */
59         if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
60                 perror("socket");
61                 return errno;
62         }
63
64         strncpy(ifr.ifr_name, name, IFNAMSIZ);
65         //ifr.ifr_data = (caddr_t) buf;
66         if ((ret = ioctl(s, SIOCGIFHWADDR, &ifr)) < 0)
67                         perror(ifr.ifr_name);
68
69         /* cleanup */
70         close(s);
71         memcpy(buf, &ifr.ifr_hwaddr.sa_data, 6);
72         return ret;
73 }