1 /* 2 * dhcpcd - DHCP client daemon 3 * Copyright (c) 2009-2010 Roy Marples <roy (at) 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/rtnetlink.h> 45 /* Support older kernels */ 46 #ifdef IFLA_WIRELESS 47 # include <linux/if.h> 48 # include <linux/wireless.h> 49 #else 50 # define IFLA_WIRELESS (IFLA_MASTER + 1) 51 #endif 52 53 #include <string.h> 54 #include <unistd.h> 55 56 #include "common.h" 57 58 /* We can't include net.h or dhcpcd.h because 59 * they would pull in net/if.h, which defeats the purpose of this hack. */ 60 #define IF_SSIDSIZE 33 61 int getifssid(const char *ifname, char *ssid); 62 63 int 64 getifssid(const char *ifname, char *ssid) 65 { 66 #ifdef SIOCGIWESSID 67 int s, retval; 68 struct iwreq iwr; 69 70 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1) 71 return -1; 72 memset(&iwr, 0, sizeof(iwr)); 73 strlcpy(iwr.ifr_name, ifname, sizeof(iwr.ifr_name)); 74 iwr.u.essid.pointer = ssid; 75 iwr.u.essid.length = IF_SSIDSIZE - 1; 76 77 if (ioctl(s, SIOCGIWESSID, &iwr) == 0) { 78 retval = iwr.u.essid.length; 79 ssid[retval] = '\0'; 80 } else 81 retval = -1; 82 close(s); 83 return retval; 84 #else 85 /* Stop gcc warning about unused paramters */ 86 ifname = ssid; 87 return -1; 88 #endif 89 } 90