Emeric Vigier | 2f62582 | 2012-08-06 11:09:52 -0400 | [diff] [blame] | 1 | // Copyright (C) 2002-2010 Christian Prochnow <cproch@seculogix.de> |
| 2 | // |
| 3 | // This program is free software; you can redistribute it and/or modify |
| 4 | // it under the terms of the GNU General Public License as published by |
| 5 | // the Free Software Foundation; either version 2 of the License, or |
| 6 | // (at your option) any later version. |
| 7 | // |
| 8 | // This program 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 this program; if not, write to the Free Software |
| 15 | // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 16 | // |
| 17 | // As a special exception, you may use this file as part of a free software |
| 18 | // library without restriction. Specifically, if other files instantiate |
| 19 | // templates or use macros or inline functions from this file, or you compile |
| 20 | // this file and link it with other files to produce an executable, this |
| 21 | // file does not by itself cause the resulting executable to be covered by |
| 22 | // the GNU General Public License. This exception does not however |
| 23 | // invalidate any other reasons why the executable file might be covered by |
| 24 | // the GNU General Public License. |
| 25 | // |
| 26 | // This exception applies only to the code released under the name GNU |
| 27 | // Common C++. If you copy code from other releases into a copy of GNU |
| 28 | // Common C++, as the General Public License permits, the exception does |
| 29 | // not apply to the code that you add in this way. To avoid misleading |
| 30 | // anyone as to the status of such modified files, you must delete |
| 31 | // this exception notice from them. |
| 32 | // |
| 33 | // If you write modifications of your own for GNU Common C++, it is your choice |
| 34 | // whether to permit this exception to apply to your modifications. |
| 35 | // If you do not wish that, delete this exception notice. |
| 36 | // |
| 37 | |
| 38 | #ifdef __QNX__ |
| 39 | #include <net/if.h> |
| 40 | #endif |
| 41 | |
| 42 | #include <cc++/config.h> |
| 43 | #include <cc++/thread.h> |
| 44 | #include <cc++/address.h> |
| 45 | #include <cc++/socket.h> |
| 46 | #include <cc++/export.h> |
| 47 | #include <cc++/network.h> |
| 48 | |
| 49 | #ifndef WIN32 |
| 50 | #ifdef HAVE_IOCTL_H |
| 51 | #include <ioctl.h> |
| 52 | #else |
| 53 | #include <sys/ioctl.h> |
| 54 | #endif |
| 55 | # ifdef HAVE_SYS_SOCKIO_H |
| 56 | # include <sys/sockio.h> |
| 57 | # endif |
| 58 | #ifdef HAVE_NET_IF_H |
| 59 | #include <net/if.h> |
| 60 | #endif |
| 61 | #endif |
| 62 | |
| 63 | #ifdef CCXX_NAMESPACES |
| 64 | namespace ost { |
| 65 | #endif |
| 66 | |
| 67 | #if defined(HAVE_NET_IF_H) || defined(WIN32) |
| 68 | #if !defined(_MSC_VER) || _MSC_VER >= 1300 |
| 69 | |
| 70 | using namespace std; |
| 71 | |
| 72 | NetworkDeviceInfo::NetworkDeviceInfo(const String& name, |
| 73 | const IPV4Host& addr, |
| 74 | const IPV4Broadcast& broadcast, |
| 75 | const IPV4Mask& netmask, int mtu) |
| 76 | : _name(name), _addr(addr), _broadcast(broadcast), _netmask(netmask), |
| 77 | _mtu(mtu) |
| 78 | {} |
| 79 | |
| 80 | NetworkDeviceInfo::NetworkDeviceInfo(const NetworkDeviceInfo& ndi) |
| 81 | : _name(ndi._name), _addr(ndi._addr), _broadcast(ndi._broadcast), |
| 82 | _netmask(ndi._netmask), _mtu(ndi._mtu) |
| 83 | {} |
| 84 | |
| 85 | NetworkDeviceInfo::~NetworkDeviceInfo() |
| 86 | {} |
| 87 | |
| 88 | bool enumNetworkDevices(vector<NetworkDeviceInfo>& devs) |
| 89 | { |
| 90 | devs.clear(); |
| 91 | |
| 92 | #ifndef WIN32 |
| 93 | char buffer[8192]; |
| 94 | struct ifconf ifc; |
| 95 | |
| 96 | int fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 97 | if(fd == -1) |
| 98 | return false; |
| 99 | |
| 100 | ifc.ifc_len = sizeof(buffer); |
| 101 | ifc.ifc_buf = buffer; |
| 102 | |
| 103 | if(ioctl(fd, SIOCGIFCONF, &ifc) == -1) |
| 104 | return false; |
| 105 | |
| 106 | IPV4Host addr; |
| 107 | IPV4Broadcast brdaddr; |
| 108 | IPV4Mask maskaddr("255.255.255.255"); |
| 109 | int mtu; |
| 110 | |
| 111 | int count = ifc.ifc_len / sizeof(ifreq); |
| 112 | for(int i = 0; i < count; ++i) { |
| 113 | if(ifc.ifc_req[i].ifr_addr.sa_family != AF_INET) |
| 114 | continue; |
| 115 | |
| 116 | addr = ((sockaddr_in&)ifc.ifc_req[i].ifr_addr).sin_addr; |
| 117 | |
| 118 | struct ifreq devifreq; |
| 119 | setString(devifreq.ifr_name, sizeof(devifreq.ifr_name), ifc.ifc_req[i].ifr_name); |
| 120 | |
| 121 | if(ioctl(fd, SIOCGIFBRDADDR, &devifreq) == -1) |
| 122 | (IPV4Address&)brdaddr = htonl(INADDR_ANY); |
| 123 | else |
| 124 | (IPV4Address&)brdaddr = ((sockaddr_in&)devifreq.ifr_broadaddr).sin_addr; |
| 125 | |
| 126 | if(ioctl(fd, SIOCGIFNETMASK, &devifreq) == -1) |
| 127 | maskaddr = htonl(INADDR_BROADCAST); |
| 128 | else |
| 129 | (IPV4Address&)maskaddr = ((sockaddr_in&)devifreq.ifr_addr).sin_addr; |
| 130 | |
| 131 | #if defined(SIOCGLIFMTU) && !defined(__hpux) |
| 132 | struct lifreq devlifreq; |
| 133 | if(ioctl(fd, SIOCGLIFMTU, &devlifreq) == -1) |
| 134 | mtu = 0; |
| 135 | else |
| 136 | mtu = devlifreq.lifr_mtu; |
| 137 | #else |
| 138 | if(ioctl(fd, SIOCGIFMTU, &devifreq) == -1) |
| 139 | mtu = 0; |
| 140 | else |
| 141 | #if defined(__hpux) |
| 142 | mtu = devifreq.ifr_metric; |
| 143 | #else |
| 144 | mtu = devifreq.ifr_mtu; |
| 145 | #endif |
| 146 | #endif |
| 147 | devs.push_back(NetworkDeviceInfo(ifc.ifc_req[i].ifr_name, addr, brdaddr, maskaddr, mtu)); |
| 148 | } |
| 149 | |
| 150 | close(fd); |
| 151 | #elif defined(SIO_GET_INTERFACE_LIST) // WIN32 |
| 152 | |
| 153 | SOCKET s = WSASocket(AF_INET, SOCK_DGRAM, 0, 0, 0, 0); |
| 154 | if(s == INVALID_SOCKET) { |
| 155 | if(WSAGetLastError() == WSANOTINITIALISED) { |
| 156 | WSADATA wsadata; |
| 157 | WSAStartup(MAKEWORD(2,0),&wsadata); |
| 158 | } |
| 159 | else |
| 160 | return false; |
| 161 | } |
| 162 | |
| 163 | char outbuff[8192]; |
| 164 | DWORD outlen; |
| 165 | DWORD ret = WSAIoctl(s, SIO_GET_INTERFACE_LIST, 0, 0, outbuff, sizeof(outbuff), &outlen, 0, 0); |
| 166 | if(ret == SOCKET_ERROR) |
| 167 | return false; |
| 168 | |
| 169 | INTERFACE_INFO* iflist = (INTERFACE_INFO*)outbuff; |
| 170 | int ifcount = outlen / sizeof(INTERFACE_INFO); |
| 171 | |
| 172 | IPV4Host addr; |
| 173 | IPV4Broadcast brdaddr; |
| 174 | IPV4Mask maskaddr("0.0.0.0"); |
| 175 | |
| 176 | for(int i = 0; i < ifcount; ++i) { |
| 177 | addr = ((sockaddr_in&)iflist[i].iiAddress).sin_addr; |
| 178 | (IPV4Address&)brdaddr = ((sockaddr_in&)iflist[i].iiBroadcastAddress).sin_addr; |
| 179 | (IPV4Address&)maskaddr = ((sockaddr_in&)iflist[i].iiNetmask).sin_addr; |
| 180 | devs.push_back(NetworkDeviceInfo("", addr, brdaddr, maskaddr, -1)); |
| 181 | } |
| 182 | |
| 183 | closesocket(s); |
| 184 | #endif |
| 185 | |
| 186 | return true; |
| 187 | } |
| 188 | |
| 189 | #endif |
| 190 | #endif |
| 191 | |
| 192 | #ifdef CCXX_NAMESPACES |
| 193 | } |
| 194 | #endif |
| 195 | /** EMACS ** |
| 196 | * Local variables: |
| 197 | * mode: c++ |
| 198 | * c-basic-offset: 4 |
| 199 | * End: |
| 200 | */ |