1 | /* |
---|
2 | * dhcpcd - DHCP client daemon |
---|
3 | * Copyright (c) 2009-2013 Roy Marples <roy@marples.name> |
---|
4 | * All rights reserved |
---|
5 | |
---|
6 | * Redistribution and use in source and binary forms, with or without |
---|
7 | * modification, are permitted provided that the following conditions |
---|
8 | * are met: |
---|
9 | * 1. Redistributions of source code must retain the above copyright |
---|
10 | * notice, this list of conditions and the following disclaimer. |
---|
11 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
12 | * notice, this list of conditions and the following disclaimer in the |
---|
13 | * documentation and/or other materials provided with the distribution. |
---|
14 | * |
---|
15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
---|
16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
---|
19 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
20 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
21 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
22 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
23 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
24 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
25 | * SUCH DAMAGE. |
---|
26 | */ |
---|
27 | |
---|
28 | /* |
---|
29 | * THIS IS A NASTY HACK THAT SHOULD NEVER HAVE HAPPENED |
---|
30 | * Basically we cannot include linux/if.h and net/if.h because |
---|
31 | * they have conflicting structures. |
---|
32 | * Sadly, linux/wireless.h includes linux/if.h all the time. |
---|
33 | * Some kernel-header installs fix this and some do not. |
---|
34 | * This file solely exists for those who do not. |
---|
35 | * |
---|
36 | * We *could* include wireless.h as that is designed for userspace, |
---|
37 | * but that then depends on the correct version of wireless-tools being |
---|
38 | * installed which isn't always the case. |
---|
39 | */ |
---|
40 | |
---|
41 | #include <sys/ioctl.h> |
---|
42 | #include <sys/socket.h> |
---|
43 | |
---|
44 | #include <linux/types.h> |
---|
45 | #include <linux/rtnetlink.h> |
---|
46 | /* Support older kernels */ |
---|
47 | #ifdef IFLA_WIRELESS |
---|
48 | # include <linux/if.h> |
---|
49 | # include <linux/wireless.h> |
---|
50 | #else |
---|
51 | # define IFLA_WIRELESS (IFLA_MASTER + 1) |
---|
52 | #endif |
---|
53 | |
---|
54 | #include <string.h> |
---|
55 | #include <unistd.h> |
---|
56 | |
---|
57 | #include "common.h" |
---|
58 | #include "config.h" |
---|
59 | |
---|
60 | /* We can't include net.h or dhcpcd.h because |
---|
61 | * they would pull in net/if.h, which defeats the purpose of this hack. */ |
---|
62 | #define IF_SSIDSIZE 33 |
---|
63 | int getifssid(const char *ifname, char *ssid); |
---|
64 | |
---|
65 | int |
---|
66 | getifssid(const char *ifname, char *ssid) |
---|
67 | { |
---|
68 | #ifdef SIOCGIWESSID |
---|
69 | int s, retval; |
---|
70 | struct iwreq iwr; |
---|
71 | |
---|
72 | if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1) |
---|
73 | return -1; |
---|
74 | memset(&iwr, 0, sizeof(iwr)); |
---|
75 | strlcpy(iwr.ifr_name, ifname, sizeof(iwr.ifr_name)); |
---|
76 | iwr.u.essid.pointer = ssid; |
---|
77 | iwr.u.essid.length = IF_SSIDSIZE - 1; |
---|
78 | |
---|
79 | if (ioctl(s, SIOCGIWESSID, &iwr) == 0) { |
---|
80 | retval = iwr.u.essid.length; |
---|
81 | ssid[retval] = '\0'; |
---|
82 | } else |
---|
83 | retval = -1; |
---|
84 | close(s); |
---|
85 | return retval; |
---|
86 | #else |
---|
87 | /* Stop gcc warning about unused paramters */ |
---|
88 | ifname = ssid; |
---|
89 | return -1; |
---|
90 | #endif |
---|
91 | } |
---|