1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
|
/*
* Copyright (c) 2011 Tobias Markmann
* Licensed under the simplified BSD license.
* See Documentation/Licenses/BSD-simplified.txt for more information.
*/
/*
* Copyright (c) 2013-2016 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
#include <Swiften/Network/SolarisNetworkEnvironment.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <map>
#include <string>
#include <vector>
#include <boost/optional.hpp>
#include <boost/signals2.hpp>
#include <net/if.h>
#include <sys/socket.h>
#include <sys/sockio.h>
#include <sys/types.h>
#include <unistd.h>
#include <Swiften/Network/HostAddress.h>
#include <Swiften/Network/NetworkInterface.h>
/*
* Copyright (c) 2006 WIDE Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the project nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#undef ifa_broadaddr
#undef ifa_dstaddr
struct ifaddrs {
struct ifaddrs *ifa_next; /* Pointer to next struct */
char *ifa_name; /* Interface name */
uint64_t ifa_flags; /* Interface flags */
struct sockaddr *ifa_addr; /* Interface address */
struct sockaddr *ifa_netmask; /* Interface netmask */
struct sockaddr *ifa_dstaddr; /* P2P interface destination */
};
#define ifa_broadaddr ifa_dstaddr
static int
get_lifreq(int fd, struct lifreq **ifr_ret)
{
struct lifnum lifn;
struct lifconf lifc;
struct lifreq *lifrp;
lifn.lifn_family = AF_UNSPEC;
lifn.lifn_flags = 0;
if (ioctl(fd, SIOCGLIFNUM, &lifn) == -1)
lifn.lifn_count = 16;
else
lifn.lifn_count += 16;
for (;;) {
lifc.lifc_len = lifn.lifn_count * sizeof (*lifrp);
lifrp = (struct lifreq *) malloc(lifc.lifc_len);
if (lifrp == NULL)
return (-1);
lifc.lifc_family = AF_UNSPEC;
lifc.lifc_flags = 0;
lifc.lifc_buf = (char *)lifrp;
if (ioctl(fd, SIOCGLIFCONF, &lifc) == -1) {
free(lifrp);
if (errno == EINVAL) {
lifn.lifn_count <<= 1;
continue;
}
(void) close(fd);
return (-1);
}
if (lifc.lifc_len < (lifn.lifn_count - 1) * sizeof (*lifrp))
break;
free(lifrp);
lifn.lifn_count <<= 1;
}
(void) close(fd);
*ifr_ret = lifrp;
return (lifc.lifc_len / sizeof (*lifrp));
}
static size_t
nbytes(const struct lifreq *lifrp, int nlif, size_t socklen)
{
size_t len = 0;
size_t slen;
while (nlif > 0) {
slen = strlen(lifrp->lifr_name) + 1;
len += sizeof (struct ifaddrs) + ((slen + 3) & ~3);
len += 3 * socklen;
lifrp++;
nlif--;
}
return (len);
}
static struct sockaddr *
addrcpy(struct sockaddr_storage *addr, char **bufp)
{
char *buf = *bufp;
size_t len;
len = addr->ss_family == AF_INET ? sizeof (struct sockaddr_in) :
sizeof (struct sockaddr_in6);
(void) memcpy(buf, addr, len);
*bufp = buf + len;
return ((struct sockaddr *)buf);
}
static int
populate(struct ifaddrs *ifa, int fd, struct lifreq *lifrp, int nlif, int af,
char **bufp)
{
char *buf = *bufp;
size_t slen;
while (nlif > 0) {
ifa->ifa_next = (nlif > 1) ? ifa + 1 : NULL;
(void) strcpy(ifa->ifa_name = buf, lifrp->lifr_name);
slen = strlen(lifrp->lifr_name) + 1;
buf += (slen + 3) & ~3;
if (ioctl(fd, SIOCGLIFFLAGS, lifrp) == -1)
ifa->ifa_flags = 0;
else
ifa->ifa_flags = lifrp->lifr_flags;
if (ioctl(fd, SIOCGLIFADDR, lifrp) == -1)
ifa->ifa_addr = NULL;
else
ifa->ifa_addr = addrcpy(&lifrp->lifr_addr, &buf);
if (ioctl(fd, SIOCGLIFNETMASK, lifrp) == -1)
ifa->ifa_netmask = NULL;
else
ifa->ifa_netmask = addrcpy(&lifrp->lifr_addr, &buf);
if (ifa->ifa_flags & IFF_POINTOPOINT) {
if (ioctl(fd, SIOCGLIFDSTADDR, lifrp) == -1)
ifa->ifa_dstaddr = NULL;
else
ifa->ifa_dstaddr =
addrcpy(&lifrp->lifr_dstaddr, &buf);
} else if (ifa->ifa_flags & IFF_BROADCAST) {
if (ioctl(fd, SIOCGLIFBRDADDR, lifrp) == -1)
ifa->ifa_broadaddr = NULL;
else
ifa->ifa_broadaddr =
addrcpy(&lifrp->lifr_broadaddr, &buf);
} else {
ifa->ifa_dstaddr = NULL;
}
ifa++;
nlif--;
lifrp++;
}
*bufp = buf;
return (0);
}
static int
getifaddrs(struct ifaddrs **ifap)
{
int fd4, fd6;
int nif4, nif6 = 0;
struct lifreq *ifr4 = NULL;
struct lifreq *ifr6 = NULL;
struct ifaddrs *ifa = NULL;
char *buf;
if ((fd4 = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
return (-1);
if ((fd6 = socket(AF_INET6, SOCK_DGRAM, 0)) == -1 &&
errno != EAFNOSUPPORT) {
(void) close(fd4);
return (-1);
}
if ((nif4 = get_lifreq(fd4, &ifr4)) == -1 ||
(fd6 != -1 && (nif6 = get_lifreq(fd6, &ifr6)) == -1))
goto failure;
if (nif4 == 0 && nif6 == 0) {
*ifap = NULL;
return (0);
}
ifa = (struct ifaddrs *) malloc(nbytes(ifr4, nif4, sizeof (struct sockaddr_in)) +
nbytes(ifr6, nif6, sizeof (struct sockaddr_in6)));
if (ifa == NULL)
goto failure;
buf = (char *)(ifa + nif4 + nif6);
if (populate(ifa, fd4, ifr4, nif4, AF_INET, &buf) == -1)
goto failure;
if (nif4 > 0 && nif6 > 0)
ifa[nif4 - 1].ifa_next = ifa + nif4;
if (populate(ifa + nif4, fd6, ifr6, nif6, AF_INET6, &buf) == -1)
goto failure;
return (0);
failure:
free(ifa);
(void) close(fd4);
if (fd6 != -1)
(void) close(fd6);
free(ifr4);
free(ifr6);
return (-1);
}
static void
freeifaddrs(struct ifaddrs *ifa)
{
free(ifa);
}
/* End WIDE Project code */
namespace Swift {
std::vector<NetworkInterface> SolarisNetworkEnvironment::getNetworkInterfaces() const {
std::map<std::string, NetworkInterface> interfaces;
ifaddrs* addrs = 0;
int ret = getifaddrs(&addrs);
if (ret != 0) {
return std::vector<NetworkInterface>();
}
for (ifaddrs* a = addrs; a != 0; a = a->ifa_next) {
std::string name(a->ifa_name);
boost::optional<HostAddress> address;
if (a->ifa_addr->sa_family == PF_INET) {
sockaddr_in* sa = reinterpret_cast<sockaddr_in*>(a->ifa_addr);
address = HostAddress(reinterpret_cast<const unsigned char*>(&(sa->sin_addr)), 4);
}
else if (a->ifa_addr->sa_family == PF_INET6) {
sockaddr_in6* sa = reinterpret_cast<sockaddr_in6*>(a->ifa_addr);
address = HostAddress(reinterpret_cast<const unsigned char*>(&(sa->sin6_addr)), 16);
}
if (address) {
std::map<std::string, NetworkInterface>::iterator i = interfaces.insert(std::make_pair(name, NetworkInterface(name, a->ifa_flags & IFF_LOOPBACK))).first;
i->second.addAddress(*address);
}
}
freeifaddrs(addrs);
std::vector<NetworkInterface> result;
for (std::map<std::string,NetworkInterface>::const_iterator i = interfaces.begin(); i != interfaces.end(); ++i) {
result.push_back(i->second);
}
return result;
}
}
|