| 1 |
#include "protoNet.h" |
| 2 |
#include "protoDebug.h" |
| 3 |
|
| 4 |
|
| 5 |
// TBD - This code could be streamlined a bit and with moving stuff to "linuxNet.cpp" using NETLINK |
| 6 |
// for everything so Android is supported there, too and keep this stuff cleaner. Perhaps a |
| 7 |
// "bsdNet.cpp" for OSX/BSD and keep the more legacy Unix stuff here??? I can't go on supporting |
| 8 |
// the SOLARIS or IRIX stuff much longer anyway. |
| 9 |
|
| 10 |
#include <unistd.h> |
| 11 |
#include <stdlib.h> // for atoi() |
| 12 |
#include <stdio.h> // for sprintf() |
| 13 |
/* |
| 14 |
#ifdef HAVE_IPV6 |
| 15 |
|
| 16 |
#ifdef MACOSX |
| 17 |
#include <arpa/nameser.h> |
| 18 |
#endif // MACOSX |
| 19 |
#include <resolv.h> |
| 20 |
#endif // HAVE_IPV6 |
| 21 |
*/ |
| 22 |
#include <sys/ioctl.h> |
| 23 |
#include <arpa/inet.h> |
| 24 |
#include <net/if.h> |
| 25 |
#include <errno.h> |
| 26 |
#include <fcntl.h> |
| 27 |
|
| 28 |
#ifndef SIOCGIFHWADDR |
| 29 |
#if defined(SOLARIS) || defined(IRIX) |
| 30 |
#include <sys/sockio.h> // for SIOCGIFADDR ioctl |
| 31 |
#include <netdb.h> // for rest_init() |
| 32 |
#include <sys/dlpi.h> |
| 33 |
#include <stropts.h> |
| 34 |
#else |
| 35 |
#include <net/if.h> |
| 36 |
#include <net/if_dl.h> |
| 37 |
#include <net/if_types.h> |
| 38 |
#endif // if/else (SOLARIS || IRIX) |
| 39 |
#endif // !SIOCGIFHWADDR |
| 40 |
|
| 41 |
#ifndef ANDROID |
| 42 |
// Android doesn't have getifaddrs(), so we have some netlink stuff |
| 43 |
// in "linuxNet.cpp" to implement specific some functions for Android |
| 44 |
#include <ifaddrs.h> |
| 45 |
#endif // !ANDROID |
| 46 |
|
| 47 |
// Implementation of ProtoNet functions for Unix systems. These are the mechanisms that are common to |
| 48 |
// most Unix systems. Some additional ProtoNet mechanisms that have Linux- or MacOS-specific code are |
| 49 |
// implemented in "src/linux/linuxNet.cpp" or "src/macos/macosNet.cpp", etc. |
| 50 |
#ifndef ANDROID // Android version implemented in linuxNet.cpp because no Android getifaddrs() |
| 51 |
unsigned int ProtoNet::GetInterfaceName(const ProtoAddress& ifAddr, char* buffer, unsigned int buflen) |
| 52 |
{ |
| 53 |
int family; |
| 54 |
switch (ifAddr.GetType()) |
| 55 |
{ |
| 56 |
case ProtoAddress::IPv4: |
| 57 |
family = AF_INET; |
| 58 |
break; |
| 59 |
#ifdef HAVE_IPV6 |
| 60 |
case ProtoAddress::IPv6: |
| 61 |
family = AF_INET6; |
| 62 |
break; |
| 63 |
#endif // HAVE_IPV6 |
| 64 |
default: |
| 65 |
PLOG(PL_ERROR, "UnixNet::GetInterfaceName() error: invalid address type\n"); |
| 66 |
return 0; |
| 67 |
} |
| 68 |
struct ifaddrs* ifap; |
| 69 |
if (0 == getifaddrs(&ifap)) |
| 70 |
{ |
| 71 |
// Look for addrType address for given "interfaceName" |
| 72 |
struct ifaddrs* ptr = ifap; |
| 73 |
unsigned int namelen = 0; |
| 74 |
while (ptr) |
| 75 |
{ |
| 76 |
if ((NULL != ptr->ifa_addr) && (family == ptr->ifa_addr->sa_family)) |
| 77 |
{ |
| 78 |
ProtoAddress theAddr; |
| 79 |
theAddr.SetSockAddr(*(ptr->ifa_addr)); |
| 80 |
if (theAddr.HostIsEqual(ifAddr)) |
| 81 |
{ |
| 82 |
namelen = (unsigned int)strlen(ptr->ifa_name); |
| 83 |
if (namelen > IFNAMSIZ) namelen = IFNAMSIZ; |
| 84 |
if (NULL == buffer) break; |
| 85 |
unsigned int maxlen = (buflen > IFNAMSIZ) ? IFNAMSIZ : buflen; |
| 86 |
strncpy(buffer, ptr->ifa_name, maxlen); |
| 87 |
break; |
| 88 |
} |
| 89 |
} |
| 90 |
ptr = ptr->ifa_next; |
| 91 |
} |
| 92 |
freeifaddrs(ifap); |
| 93 |
if (0 == namelen) |
| 94 |
PLOG(PL_ERROR, "UnixNet::GetInterfaceName() error: unknown interface address\n"); |
| 95 |
return namelen; |
| 96 |
} |
| 97 |
else |
| 98 |
{ |
| 99 |
PLOG(PL_ERROR, "UnixNet::GetInterfaceName() getifaddrs() error: %s\n", GetErrorString()); |
| 100 |
return 0; |
| 101 |
} |
| 102 |
} // end ProtoNet::GetInterfaceName(by address) |
| 103 |
|
| 104 |
bool ProtoNet::GetInterfaceAddressList(const char* interfaceName, |
| 105 |
ProtoAddress::Type addressType, |
| 106 |
ProtoAddressList& addrList, |
| 107 |
unsigned int* interfaceIndex) |
| 108 |
{ |
| 109 |
struct ifreq req; |
| 110 |
memset(&req, 0, sizeof(struct ifreq)); |
| 111 |
strncpy(req.ifr_name, interfaceName, IFNAMSIZ); |
| 112 |
int socketFd = -1; |
| 113 |
switch (addressType) |
| 114 |
{ |
| 115 |
case ProtoAddress::IPv4: |
| 116 |
req.ifr_addr.sa_family = AF_INET; |
| 117 |
socketFd = socket(PF_INET, SOCK_DGRAM, 0); |
| 118 |
break; |
| 119 |
#ifdef HAVE_IPV6 |
| 120 |
case ProtoAddress::IPv6: |
| 121 |
req.ifr_addr.sa_family = AF_INET6; |
| 122 |
socketFd = socket(PF_INET6, SOCK_DGRAM, 0); |
| 123 |
break; |
| 124 |
#endif // HAVE_IPV6 |
| 125 |
default: |
| 126 |
req.ifr_addr.sa_family = AF_UNSPEC; |
| 127 |
socketFd = socket(PF_INET, SOCK_DGRAM, 0); |
| 128 |
break; |
| 129 |
} |
| 130 |
|
| 131 |
if (socketFd < 0) |
| 132 |
{ |
| 133 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceAddressList() socket() error: %s\n", GetErrorString()); |
| 134 |
return false; |
| 135 |
} |
| 136 |
|
| 137 |
if (ProtoAddress::ETH == addressType) |
| 138 |
{ |
| 139 |
#ifdef SIOCGIFHWADDR |
| 140 |
// Probably Linux |
| 141 |
// Get hardware (MAC) address instead of IP address |
| 142 |
if (ioctl(socketFd, SIOCGIFHWADDR, &req) < 0) |
| 143 |
{ |
| 144 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceAddressList() ioctl(SIOCGIFHWADDR) error: %s\n", |
| 145 |
GetErrorString()); |
| 146 |
close(socketFd); |
| 147 |
return false; |
| 148 |
} |
| 149 |
else |
| 150 |
{ |
| 151 |
close(socketFd); |
| 152 |
if (NULL != interfaceIndex) *interfaceIndex = req.ifr_ifindex; |
| 153 |
ProtoAddress ethAddr; |
| 154 |
if (!ethAddr.SetRawHostAddress(ProtoAddress::ETH, |
| 155 |
(const char*)&req.ifr_hwaddr.sa_data, |
| 156 |
IFHWADDRLEN)) |
| 157 |
{ |
| 158 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceAddressList() error: invalid ETH addr?\n"); |
| 159 |
return false; |
| 160 |
} |
| 161 |
if (!addrList.Insert(ethAddr)) |
| 162 |
{ |
| 163 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceAddressList() error: unable to add ETH addr to list.\n"); |
| 164 |
return false; |
| 165 |
} |
| 166 |
return true; |
| 167 |
} |
| 168 |
#else |
| 169 |
#if defined(SOLARIS) || defined(IRIX) |
| 170 |
// Use DLPI instead |
| 171 |
close(socketFd); |
| 172 |
char deviceName[32]; |
| 173 |
snprintf(deviceName, 32, "/dev/%s", interfaceName); |
| 174 |
char* ptr = strpbrk(deviceName, "0123456789"); |
| 175 |
if (NULL == ptr) |
| 176 |
{ |
| 177 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceAddressList() invalid interface\n"); |
| 178 |
return false; |
| 179 |
} |
| 180 |
int ifNumber = atoi(ptr); |
| 181 |
*ptr = '\0'; |
| 182 |
if ((socketFd = open(deviceName, O_RDWR)) < 0) |
| 183 |
{ |
| 184 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceAddressList() dlpi open() error: %s\n", |
| 185 |
GetErrorString()); |
| 186 |
return false; |
| 187 |
} |
| 188 |
// dlp device opened, now bind to specific interface |
| 189 |
UINT32 buffer[8192]; |
| 190 |
union DL_primitives* dlp = (union DL_primitives*)buffer; |
| 191 |
dlp->info_req.dl_primitive = DL_INFO_REQ; |
| 192 |
struct strbuf msg; |
| 193 |
msg.maxlen = 0; |
| 194 |
msg.len = DL_INFO_REQ_SIZE; |
| 195 |
msg.buf = (caddr_t)dlp; |
| 196 |
if (putmsg(socketFd, &msg, NULL, RS_HIPRI) < 0) |
| 197 |
{ |
| 198 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceAddressList() dlpi putmsg(1) error: %s\n", |
| 199 |
GetErrorString()); |
| 200 |
close(socketFd); |
| 201 |
return false; |
| 202 |
} |
| 203 |
msg.maxlen = 8192; |
| 204 |
msg.len = 0; |
| 205 |
int flags = 0; |
| 206 |
if (getmsg(socketFd, &msg, NULL, &flags) < 0) |
| 207 |
{ |
| 208 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceAddressList() dlpi getmsg(1) error: %s\n", |
| 209 |
GetErrorString()); |
| 210 |
close(socketFd); |
| 211 |
return false; |
| 212 |
} |
| 213 |
if ((DL_INFO_ACK != dlp->dl_primitive) || |
| 214 |
(msg.len < (int)DL_INFO_ACK_SIZE)) |
| 215 |
{ |
| 216 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceAddressList() dlpi getmsg(1) error: unexpected response\n"); |
| 217 |
close(socketFd); |
| 218 |
return false; |
| 219 |
} |
| 220 |
if (DL_STYLE2 == dlp->info_ack.dl_provider_style) |
| 221 |
{ |
| 222 |
dlp->attach_req.dl_primitive = DL_ATTACH_REQ; |
| 223 |
dlp->attach_req.dl_ppa = ifNumber; |
| 224 |
msg.maxlen = 0; |
| 225 |
msg.len = DL_ATTACH_REQ_SIZE; |
| 226 |
msg.buf = (caddr_t)dlp; |
| 227 |
if (putmsg(socketFd, &msg, NULL, RS_HIPRI) < 0) |
| 228 |
{ |
| 229 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceAddressList() dlpi putmsg(DL_ATTACH_REQ) error: %s\n", |
| 230 |
GetErrorString()); |
| 231 |
close(socketFd); |
| 232 |
return false; |
| 233 |
} |
| 234 |
msg.maxlen = 8192; |
| 235 |
msg.len = 0; |
| 236 |
flags = 0; |
| 237 |
if (getmsg(socketFd, &msg, NULL, &flags) < 0) |
| 238 |
{ |
| 239 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceAddressList() dlpi getmsg(DL_OK_ACK) error: %s\n", |
| 240 |
GetErrorString()); |
| 241 |
close(socketFd); |
| 242 |
return false; |
| 243 |
} |
| 244 |
if ((DL_OK_ACK != dlp->dl_primitive) || |
| 245 |
(msg.len < (int)DL_OK_ACK_SIZE)) |
| 246 |
{ |
| 247 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceAddressList() dlpi getmsg(DL_OK_ACK) error: unexpected response\n"); |
| 248 |
close(socketFd); |
| 249 |
return false; |
| 250 |
} |
| 251 |
} |
| 252 |
memset(&dlp->bind_req, 0, DL_BIND_REQ_SIZE); |
| 253 |
dlp->bind_req.dl_primitive = DL_BIND_REQ; |
| 254 |
#ifdef DL_HP_RAWDLS |
| 255 |
dlp->bind_req.dl_sap = 24; |
| 256 |
dlp->bind_req.dl_service_mode = DL_HP_RAWDLS; |
| 257 |
#else |
| 258 |
dlp->bind_req.dl_sap = DL_ETHER; |
| 259 |
dlp->bind_req.dl_service_mode = DL_CLDLS; |
| 260 |
#endif |
| 261 |
msg.maxlen = 0; |
| 262 |
msg.len = DL_BIND_REQ_SIZE; |
| 263 |
msg.buf = (caddr_t)dlp; |
| 264 |
if (putmsg(socketFd, &msg, NULL, RS_HIPRI) < 0) |
| 265 |
{ |
| 266 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceAddressList() dlpi putmsg(DL_BIND_REQ) error: %s\n", |
| 267 |
GetErrorString()); |
| 268 |
close(socketFd); |
| 269 |
return false; |
| 270 |
} |
| 271 |
msg.maxlen = 8192; |
| 272 |
msg.len = 0; |
| 273 |
flags = 0; |
| 274 |
if (getmsg(socketFd, &msg, NULL, &flags) < 0) |
| 275 |
{ |
| 276 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceAddressList() dlpi getmsg(DL_BIND_ACK) error: %s\n", |
| 277 |
GetErrorString()); |
| 278 |
close(socketFd); |
| 279 |
return false; |
| 280 |
} |
| 281 |
if ((DL_BIND_ACK != dlp->dl_primitive) || |
| 282 |
(msg.len < (int)DL_BIND_ACK_SIZE)) |
| 283 |
{ |
| 284 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceAddressList() dlpi getmsg(DL_BIND_ACK) error: unexpected response\n"); |
| 285 |
close(socketFd); |
| 286 |
return false; |
| 287 |
} |
| 288 |
// We're bound to the interface, now request interface address |
| 289 |
dlp->info_req.dl_primitive = DL_INFO_REQ; |
| 290 |
msg.maxlen = 0; |
| 291 |
msg.len = DL_INFO_REQ_SIZE; |
| 292 |
msg.buf = (caddr_t)dlp; |
| 293 |
if (putmsg(socketFd, &msg, NULL, RS_HIPRI) < 0) |
| 294 |
{ |
| 295 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceAddressList() dlpi putmsg() error: %s\n", |
| 296 |
GetErrorString()); |
| 297 |
close(socketFd); |
| 298 |
return false; |
| 299 |
} |
| 300 |
msg.maxlen = 8192; |
| 301 |
msg.len = 0; |
| 302 |
flags = 0; |
| 303 |
if (getmsg(socketFd, &msg, NULL, &flags) < 0) |
| 304 |
{ |
| 305 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceAddressList() dlpi getmsg() error: %s\n", |
| 306 |
GetErrorString()); |
| 307 |
close(socketFd); |
| 308 |
return false; |
| 309 |
} |
| 310 |
if ((DL_INFO_ACK != dlp->dl_primitive) || (msg.len < (int)DL_INFO_ACK_SIZE)) |
| 311 |
{ |
| 312 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceAddressList() dlpi getmsg() error: unexpected response\n"); |
| 313 |
close(socketFd); |
| 314 |
return false; |
| 315 |
} |
| 316 |
ProtoAddress macAddr; |
| 317 |
macAddr.SetRawHostAddress(addressType, (char*)(buffer + dlp->physaddr_ack.dl_addr_offset), 6); |
| 318 |
if (NULL != interfaceIndex) *interfaceIndex = ifNumber; |
| 319 |
close(socketFd); |
| 320 |
if (!addrList.Insert(macAddr)) |
| 321 |
{ |
| 322 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceAddressList() error: unable to add ETH addr to list.\n"); |
| 323 |
return false; |
| 324 |
} |
| 325 |
return true; |
| 326 |
#else |
| 327 |
// For now, assume we're BSD and use getifaddrs() |
| 328 |
close(socketFd); // don't need the socket |
| 329 |
struct ifaddrs* ifap; |
| 330 |
if (0 == getifaddrs(&ifap)) |
| 331 |
{ |
| 332 |
// Look for AF_LINK address for given "interfaceName" |
| 333 |
struct ifaddrs* ptr = ifap; |
| 334 |
while (NULL != ptr) |
| 335 |
{ |
| 336 |
if ((NULL != ptr->ifa_addr) && (AF_LINK == ptr->ifa_addr->sa_family)) |
| 337 |
{ |
| 338 |
if (!strcmp(interfaceName, ptr->ifa_name)) |
| 339 |
{ |
| 340 |
// note the (void*) cast here gets rid of cast-align mis-warning |
| 341 |
struct sockaddr_dl* sdl = (struct sockaddr_dl*)((void*)ptr->ifa_addr); |
| 342 |
if (IFT_ETHER != sdl->sdl_type) |
| 343 |
{ |
| 344 |
freeifaddrs(ifap); |
| 345 |
PLOG(PL_WARN, "ProtoNet::GetInterfaceAddressList() error: non-Ethertype iface: %s\n", interfaceName); |
| 346 |
return false; |
| 347 |
} |
| 348 |
ProtoAddress macAddr; |
| 349 |
macAddr.SetRawHostAddress(addressType, |
| 350 |
sdl->sdl_data + sdl->sdl_nlen, |
| 351 |
sdl->sdl_alen); |
| 352 |
if (NULL != interfaceIndex) |
| 353 |
*interfaceIndex = sdl->sdl_index; |
| 354 |
freeifaddrs(ifap); |
| 355 |
if (!addrList.Insert(macAddr)) |
| 356 |
{ |
| 357 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceAddressList() error: unable to add ETH addr to list.\n"); |
| 358 |
return false; |
| 359 |
} |
| 360 |
return true; |
| 361 |
} |
| 362 |
} |
| 363 |
ptr = ptr->ifa_next; |
| 364 |
} |
| 365 |
freeifaddrs(ifap); |
| 366 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceAddressList() unknown interface name\n"); |
| 367 |
return false; // change to true when implemented |
| 368 |
} |
| 369 |
else |
| 370 |
{ |
| 371 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceAddressList() getifaddrs() error: %s\n", |
| 372 |
GetErrorString()); |
| 373 |
return false; |
| 374 |
} |
| 375 |
#endif // if/else (SOLARIS || IRIX) |
| 376 |
#endif // if/else SIOCGIFHWADDR |
| 377 |
} // end if (ETH == addrType) |
| 378 |
|
| 379 |
#ifdef HAVE_IPV6 |
| 380 |
// Try using getifaddrs() to get all address else resort to ioctl(SIOCGIFADDR) below |
| 381 |
struct ifaddrs* ifap; |
| 382 |
if (0 == getifaddrs(&ifap)) |
| 383 |
{ |
| 384 |
close(socketFd); |
| 385 |
// Look for addrType address for given "interfaceName" |
| 386 |
struct ifaddrs* ptr = ifap; |
| 387 |
bool foundIface = false; |
| 388 |
while (NULL != ptr) |
| 389 |
{ |
| 390 |
char ifname[IFNAMSIZ+1]; |
| 391 |
ifname[IFNAMSIZ] = '\0'; |
| 392 |
strncpy(ifname, ptr->ifa_name, IFNAMSIZ); |
| 393 |
#ifdef LINUX |
| 394 |
// This lets us find alias addrs on Linux |
| 395 |
if (NULL == strchr(interfaceName, ':')) |
| 396 |
{ |
| 397 |
// "interfaceName" is a primary (non-alias) interface, |
| 398 |
// so match any aliases, too |
| 399 |
char* colon = strchr(ifname, ':'); |
| 400 |
if (NULL != colon) *colon = '\0'; |
| 401 |
} |
| 402 |
#endif // LINUX |
| 403 |
if (0 == strcmp(interfaceName, ifname)) |
| 404 |
{ |
| 405 |
if ((NULL != ptr->ifa_addr) && (ptr->ifa_addr->sa_family == req.ifr_addr.sa_family)) |
| 406 |
{ |
| 407 |
ProtoAddress ifAddr; |
| 408 |
if (!ifAddr.SetSockAddr(*(ptr->ifa_addr))) |
| 409 |
{ |
| 410 |
PLOG(PL_WARN, "ProtoNet::GetInterfaceAddressList() error: invalid address family\n"); |
| 411 |
ptr = ptr->ifa_next; |
| 412 |
continue; |
| 413 |
} |
| 414 |
if (!addrList.Insert(ifAddr)) |
| 415 |
{ |
| 416 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceAddressList() error: unable to add address to list!\n"); |
| 417 |
freeifaddrs(ifap); |
| 418 |
close(socketFd); |
| 419 |
return false; |
| 420 |
} |
| 421 |
} |
| 422 |
foundIface = true; |
| 423 |
} |
| 424 |
ptr = ptr->ifa_next; |
| 425 |
} |
| 426 |
freeifaddrs(ifap); |
| 427 |
|
| 428 |
if (foundIface) |
| 429 |
{ |
| 430 |
if (NULL != interfaceIndex) |
| 431 |
*interfaceIndex = GetInterfaceIndex(interfaceName); |
| 432 |
} |
| 433 |
else |
| 434 |
{ |
| 435 |
// Perhaps "interfaceName" is an address string? |
| 436 |
ProtoAddress ifAddr; |
| 437 |
if (ifAddr.ConvertFromString(interfaceName)) |
| 438 |
{ |
| 439 |
char nameBuffer[IFNAMSIZ+1]; |
| 440 |
if (GetInterfaceName(ifAddr, nameBuffer, IFNAMSIZ+1)) |
| 441 |
{ |
| 442 |
return GetInterfaceAddressList(nameBuffer, addressType, addrList, interfaceIndex); |
| 443 |
} |
| 444 |
else |
| 445 |
{ |
| 446 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceAddressList() error: unknown interface address\n"); |
| 447 |
} |
| 448 |
} |
| 449 |
return false; |
| 450 |
|
| 451 |
} |
| 452 |
} |
| 453 |
else |
| 454 |
#endif // HAVE_IPV6 |
| 455 |
if (ioctl(socketFd, SIOCGIFADDR, &req) < 0) |
| 456 |
{ |
| 457 |
// (TBD - more sophisticated warning logic here |
| 458 |
PLOG(PL_DEBUG, "ProtoNet::GetInterfaceAddressList() ioctl(SIOCGIFADDR) error for iface>%s: %s\n", |
| 459 |
interfaceName, GetErrorString()); |
| 460 |
close(socketFd); |
| 461 |
// Perhaps "interfaceName" is an address string? |
| 462 |
ProtoAddress ifAddr; |
| 463 |
if (ifAddr.ConvertFromString(interfaceName)) |
| 464 |
{ |
| 465 |
char nameBuffer[IFNAMSIZ+1]; |
| 466 |
if (GetInterfaceName(ifAddr, nameBuffer, IFNAMSIZ+1)) |
| 467 |
{ |
| 468 |
return GetInterfaceAddressList(nameBuffer, addressType, addrList, interfaceIndex); |
| 469 |
} |
| 470 |
else |
| 471 |
{ |
| 472 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceAddressList() error: unknown interface address\n"); |
| 473 |
} |
| 474 |
} |
| 475 |
return false; |
| 476 |
} |
| 477 |
else |
| 478 |
{ |
| 479 |
close(socketFd); |
| 480 |
ProtoAddress ifAddr; |
| 481 |
#ifdef MACOSX |
| 482 |
// (TBD) make this more general somehow??? |
| 483 |
if (0 == req.ifr_addr.sa_len) |
| 484 |
{ |
| 485 |
PLOG(PL_DEBUG, "ProtoNet::GetInterfaceAddressList() warning: no addresses for given family?\n"); |
| 486 |
return false; |
| 487 |
} |
| 488 |
else |
| 489 |
#endif // MACOSX |
| 490 |
if (ifAddr.SetSockAddr(req.ifr_addr)) |
| 491 |
{ |
| 492 |
if (addrList.Insert(ifAddr)) |
| 493 |
{ |
| 494 |
return true; |
| 495 |
} |
| 496 |
else |
| 497 |
{ |
| 498 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceAddressList() error: unable to add ifAddr to list\n"); |
| 499 |
return false; |
| 500 |
} |
| 501 |
} |
| 502 |
else |
| 503 |
{ |
| 504 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceAddressList() error: invalid address family\n"); |
| 505 |
return false; |
| 506 |
} |
| 507 |
if (NULL != interfaceIndex) |
| 508 |
*interfaceIndex = GetInterfaceIndex(req.ifr_name); |
| 509 |
} |
| 510 |
return true; |
| 511 |
} // end ProtoNet::GetInterfaceAddressList() |
| 512 |
|
| 513 |
unsigned int ProtoNet::GetInterfaceAddressMask(const char* ifaceName, const ProtoAddress& theAddr) |
| 514 |
{ |
| 515 |
int family; |
| 516 |
switch (theAddr.GetType()) |
| 517 |
{ |
| 518 |
case ProtoAddress::IPv4: |
| 519 |
family = AF_INET; |
| 520 |
break; |
| 521 |
case ProtoAddress::IPv6: |
| 522 |
family = AF_INET6; |
| 523 |
break; |
| 524 |
default: |
| 525 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceAddressMask() error: invalid address type\n"); |
| 526 |
return 0; |
| 527 |
} |
| 528 |
struct ifaddrs* ifap; |
| 529 |
if (0 == getifaddrs(&ifap)) |
| 530 |
{ |
| 531 |
// Look for addrType address for given "interfaceName" |
| 532 |
struct ifaddrs* ptr = ifap; |
| 533 |
bool foundIface = false; |
| 534 |
while (NULL != ptr) |
| 535 |
{ |
| 536 |
if ((NULL == ptr->ifa_addr) || (ptr->ifa_addr->sa_family != family)) |
| 537 |
{ |
| 538 |
ptr = ptr->ifa_next; |
| 539 |
continue; |
| 540 |
} |
| 541 |
char ifname[IFNAMSIZ+1]; |
| 542 |
ifname[IFNAMSIZ] = '\0'; |
| 543 |
strncpy(ifname, ptr->ifa_name, IFNAMSIZ); |
| 544 |
#ifdef LINUX |
| 545 |
// This lets us find alias addrs on Linux |
| 546 |
char* colon = strchr(ifname, ':'); |
| 547 |
if (NULL != colon) *colon = '\0'; |
| 548 |
#endif // LINUX |
| 549 |
if (0 == strcmp(ifaceName, ifname)) |
| 550 |
{ |
| 551 |
ProtoAddress ifAddr; |
| 552 |
if (!ifAddr.SetSockAddr(*(ptr->ifa_addr))) |
| 553 |
{ |
| 554 |
ptr = ptr->ifa_next; |
| 555 |
continue; |
| 556 |
} |
| 557 |
if (theAddr.HostIsEqual(ifAddr)) |
| 558 |
{ |
| 559 |
if (NULL == ptr->ifa_netmask) |
| 560 |
{ |
| 561 |
// No netmask, so assume full address length? |
| 562 |
freeifaddrs(ifap); |
| 563 |
return (ifAddr.GetLength() << 3); |
| 564 |
} |
| 565 |
ProtoAddress maskAddr; |
| 566 |
if (0 != ptr->ifa_netmask->sa_family) |
| 567 |
{ |
| 568 |
maskAddr.SetSockAddr(*(ptr->ifa_netmask)); |
| 569 |
} |
| 570 |
else |
| 571 |
{ |
| 572 |
// For some reason (at least on MacOSX), we sometimes get a null sa_family on the mask |
| 573 |
// So we assume netmask _is_ same family as the ifAddr |
| 574 |
// This seems to happen on "alias" addresses. (TBD - maybe this helps us know which are aliases |
| 575 |
// and which are not?) |
| 576 |
struct sockaddr maddr; |
| 577 |
memcpy(&maddr, ptr->ifa_netmask, sizeof(sockaddr)); |
| 578 |
maddr.sa_family = ptr->ifa_addr->sa_family; |
| 579 |
maskAddr.SetSockAddr(maddr); |
| 580 |
} |
| 581 |
freeifaddrs(ifap); |
| 582 |
return maskAddr.GetPrefixLength(); |
| 583 |
} |
| 584 |
foundIface = true; |
| 585 |
} |
| 586 |
ptr = ptr->ifa_next; |
| 587 |
} |
| 588 |
freeifaddrs(ifap); |
| 589 |
if (!foundIface) |
| 590 |
{ |
| 591 |
// Perhaps "interfaceName" is an address string instead? |
| 592 |
ProtoAddress ifAddr; |
| 593 |
if (ifAddr.ConvertFromString(ifaceName)) |
| 594 |
{ |
| 595 |
char nameBuffer[IFNAMSIZ+1]; |
| 596 |
if (GetInterfaceName(ifAddr, nameBuffer, IFNAMSIZ+1)) |
| 597 |
{ |
| 598 |
return GetInterfaceAddressMask(nameBuffer, theAddr); |
| 599 |
} |
| 600 |
else |
| 601 |
{ |
| 602 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceAddressMask() error: unknown interface name\n"); |
| 603 |
} |
| 604 |
} |
| 605 |
} |
| 606 |
} |
| 607 |
else |
| 608 |
{ |
| 609 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceAddressMask() getifaddrs() error: %s\n"); |
| 610 |
} |
| 611 |
return 0; |
| 612 |
} // end ProtoNet::GetInterfaceAddressMask() |
| 613 |
#endif // !ANDROID |
| 614 |
|
| 615 |
#if defined(SIOCGIFINDEX) && (defined(ANDROID) || !defined(HAVE_IPV6)) |
| 616 |
// Internal helper function for systems without getifaddrs() (e.g. Android or older stuff) |
| 617 |
static int GetInterfaceList(struct ifconf& conf) |
| 618 |
{ |
| 619 |
int sockFd = socket(PF_INET, SOCK_DGRAM, 0); |
| 620 |
if (sockFd < 0) |
| 621 |
{ |
| 622 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceList() socket() error: %s\n", GetErrorString()); |
| 623 |
return 0; |
| 624 |
} |
| 625 |
|
| 626 |
int ifNum = 32; // first guess for max # of interfaces |
| 627 |
#ifdef SIOCGIFNUM // Solaris has this, others might |
| 628 |
if (ioctl(sock, SIOCGIFNUM, &ifNum) >= 0) ifNum++; |
| 629 |
#endif // SIOCGIFNUM |
| 630 |
// We loop here until we get a fully successful SIOGIFCONF |
| 631 |
// This returns us a list of all interfaces |
| 632 |
int bufLen; |
| 633 |
conf.ifc_buf = NULL; |
| 634 |
do |
| 635 |
{ |
| 636 |
if (NULL != conf.ifc_buf) delete[] conf.ifc_buf; // remove previous buffer |
| 637 |
bufLen = ifNum * sizeof(struct ifreq); |
| 638 |
conf.ifc_len = bufLen; |
| 639 |
conf.ifc_buf = new char[bufLen]; |
| 640 |
if ((NULL == conf.ifc_buf)) |
| 641 |
{ |
| 642 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceList() new conf.ifc_buf error: %s\n", GetErrorString()); |
| 643 |
conf.ifc_len = 0; |
| 644 |
break; |
| 645 |
} |
| 646 |
if ((ioctl(sockFd, SIOCGIFCONF, &conf) < 0)) |
| 647 |
{ |
| 648 |
PLOG(PL_WARN, "ProtoNet::GetInterfaceList() ioctl(SIOCGIFCONF) warning: %s\n", GetErrorString()); |
| 649 |
conf.ifc_len = 0; // reset for fall-through below |
| 650 |
} |
| 651 |
ifNum *= 2; // last guess not big enough, so make bigger & try again |
| 652 |
} while (conf.ifc_len >= bufLen); |
| 653 |
close(sockFd); // done with socket (whether error or not) |
| 654 |
return (conf.ifc_len / sizeof(struct ifreq)); // number of interfaces (or 0) |
| 655 |
} // end ProtoNet::GetInterfaceList() |
| 656 |
#endif // SIOCGIFINDEX && (ANDROID || !HAVE_IPV6) |
| 657 |
|
| 658 |
unsigned int ProtoNet::GetInterfaceIndices(unsigned int* indexArray, unsigned int indexArraySize) |
| 659 |
{ |
| 660 |
unsigned int indexCount = 0; |
| 661 |
#if defined(HAVE_IPV6) && !defined(ANDROID) |
| 662 |
struct if_nameindex* ifdx = if_nameindex(); |
| 663 |
if (NULL == ifdx) return 0; // no interfaces found |
| 664 |
struct if_nameindex* ifPtr = ifdx; |
| 665 |
while ((0 != ifPtr->if_index)) |
| 666 |
{ |
| 667 |
// need to take into account (NULL, 0) input (see GetInterfaceName) |
| 668 |
if ((NULL != indexArray) && (indexCount < indexArraySize)) |
| 669 |
indexArray[indexCount] = ifPtr->if_index; |
| 670 |
indexCount++; |
| 671 |
ifPtr++; |
| 672 |
} |
| 673 |
if_freenameindex(ifdx); |
| 674 |
#else // !HAVE_IPV6 || ANDROID |
| 675 |
#ifdef SIOCGIFINDEX |
| 676 |
struct ifconf conf; |
| 677 |
conf.ifc_buf = NULL; // initialize |
| 678 |
indexCount = GetInterfaceList(conf); |
| 679 |
if (NULL != indexArray) |
| 680 |
{ |
| 681 |
if (indexCount < indexArraySize) indexArraySize = indexCount; |
| 682 |
for (unsigned int i = 0; i < indexArraySize; i++) |
| 683 |
indexArray[i] = GetInterfaceIndex(conf.ifc_req[i].ifr_name); |
| 684 |
} |
| 685 |
if (NULL != conf.ifc_buf) delete[] conf.ifc_buf; |
| 686 |
|
| 687 |
#else // !SIOCGIFINDEX |
| 688 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceIndices() error: interface indices not supported\n"); |
| 689 |
#endif // if/else SIOCGIFINDEX |
| 690 |
#endif // if/else HAVE_IPV6 && !ANDROID |
| 691 |
return indexCount; |
| 692 |
} // end ProtoNet::GetInterfaceIndices() |
| 693 |
|
| 694 |
unsigned int ProtoNet::GetInterfaceIndex(const char* interfaceName) |
| 695 |
{ |
| 696 |
unsigned int index = 0; |
| 697 |
#ifdef HAVE_IPV6 |
| 698 |
index = if_nametoindex(interfaceName); |
| 699 |
#else |
| 700 |
#ifdef SIOCGIFINDEX |
| 701 |
int sockFd = socket(PF_INET, SOCK_DGRAM, 0); |
| 702 |
if (sockFd < 0) |
| 703 |
{ |
| 704 |
PLOG(PL_WARN, "ProtoNet::GetInterfaceIndex() socket() error: %s\n", |
| 705 |
GetErrorString()); |
| 706 |
return 0; |
| 707 |
} |
| 708 |
struct ifreq req; |
| 709 |
strncpy(req.ifr_name, interfaceName, IFNAMSIZ); |
| 710 |
if (ioctl(sockFd, SIOCGIFINDEX, &req) < 0) |
| 711 |
PLOG(PL_WARN, "ProtoNet::GetInterfaceIndex() ioctl(SIOCGIFINDEX) error: %s\n", |
| 712 |
GetErrorString()); |
| 713 |
else |
| 714 |
index = req.ifr_ifindex; |
| 715 |
close(sockFd); |
| 716 |
#else |
| 717 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceIndex() error: interface indices not supported\n"); |
| 718 |
return 0; |
| 719 |
#endif // if/else SIOCGIFINDEX |
| 720 |
#endif // if/else HAVE_IPV6 |
| 721 |
if (0 == index) |
| 722 |
{ |
| 723 |
// Perhaps "interfaceName" was an address string? |
| 724 |
ProtoAddress ifAddr; |
| 725 |
if (ifAddr.ResolveFromString(interfaceName)) |
| 726 |
{ |
| 727 |
char nameBuffer[IFNAMSIZ+1]; |
| 728 |
if (GetInterfaceName(ifAddr, nameBuffer, IFNAMSIZ+1)) |
| 729 |
return GetInterfaceIndex(nameBuffer); |
| 730 |
} |
| 731 |
} |
| 732 |
return index; |
| 733 |
} // end ProtoNet::GetInterfaceIndex() |
| 734 |
|
| 735 |
unsigned int ProtoNet::GetInterfaceName(unsigned int index, char* buffer, unsigned int buflen) |
| 736 |
{ |
| 737 |
#ifdef HAVE_IPV6 |
| 738 |
char ifName[IFNAMSIZ+1]; |
| 739 |
if (NULL != if_indextoname(index, ifName)) |
| 740 |
{ |
| 741 |
strncpy(buffer, ifName, buflen); |
| 742 |
return (unsigned int)strlen(ifName); |
| 743 |
} |
| 744 |
else |
| 745 |
{ |
| 746 |
return 0; |
| 747 |
} |
| 748 |
#else |
| 749 |
#ifdef SIOCGIFNAME |
| 750 |
int sockFd = socket(PF_INET, SOCK_DGRAM, 0); |
| 751 |
if (sockFd < 0) |
| 752 |
{ |
| 753 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceName() socket() error: %s\n", |
| 754 |
GetErrorString()); |
| 755 |
return 0; |
| 756 |
} |
| 757 |
struct ifreq req; |
| 758 |
req.ifr_ifindex = index; |
| 759 |
if (ioctl(sockFd, SIOCGIFNAME, &req) < 0) |
| 760 |
{ |
| 761 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceName() ioctl(SIOCGIFNAME) error: %s\n", |
| 762 |
GetErrorString()); |
| 763 |
close(sockFd); |
| 764 |
return 0; |
| 765 |
} |
| 766 |
close(sockFd); |
| 767 |
if (NULL != buffer) |
| 768 |
{ |
| 769 |
if (buflen > IFNAMSIZ) |
| 770 |
{ |
| 771 |
buffer[IFNAMSIZ] = '\0'; |
| 772 |
buflen = IFNAMSIZ; |
| 773 |
} |
| 774 |
strncpy(buffer, req.ifr_name, buflen); |
| 775 |
} |
| 776 |
return strnlen(req.ifr_name, IFNAMSIZ); |
| 777 |
#else |
| 778 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceName() error: getting name by index not supported\n"); |
| 779 |
return 0; |
| 780 |
#endif // if/else SIOCGIFNAME |
| 781 |
#endif // if/else HAVE_IPV6 |
| 782 |
} // end ProtoNet::GetInterfaceName(by index) |
| 783 |
|
| 784 |
ProtoNet::InterfaceStatus ProtoNet::GetInterfaceStatus(const char* ifaceName) |
| 785 |
{ |
| 786 |
int fd = socket(PF_INET, SOCK_DGRAM, 0); |
| 787 |
if (fd < 0) |
| 788 |
{ |
| 789 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceStatus() socket() error: %s\n", GetErrorString()); |
| 790 |
return IFACE_UNKNOWN; |
| 791 |
} |
| 792 |
struct ifreq req; |
| 793 |
memset(&req, 0, sizeof(req)); |
| 794 |
strncpy(req.ifr_name, ifaceName, IFNAMSIZ); |
| 795 |
if (ioctl(fd, SIOCGIFFLAGS, &req) < 0) |
| 796 |
{ |
| 797 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceStatus() ioctl(SIOCGIFFLAGS) error: %s\n", GetErrorString()); |
| 798 |
close(fd); |
| 799 |
return IFACE_UNKNOWN; |
| 800 |
} |
| 801 |
close(fd); |
| 802 |
|
| 803 |
if (0 != (req.ifr_flags & IFF_UP)) |
| 804 |
return IFACE_UP; |
| 805 |
else |
| 806 |
return IFACE_DOWN; |
| 807 |
|
| 808 |
} // end ProtoNet::GetInterfaceStatus(by name) |
| 809 |
|
| 810 |
ProtoNet::InterfaceStatus ProtoNet::GetInterfaceStatus(unsigned int ifaceIndex) |
| 811 |
{ |
| 812 |
char ifaceName[IFNAMSIZ+1]; |
| 813 |
ifaceName[IFNAMSIZ] = '\0'; |
| 814 |
if (!GetInterfaceName(ifaceIndex, ifaceName, IFNAMSIZ)) |
| 815 |
{ |
| 816 |
PLOG(PL_ERROR, "ProtoNet::InterfaceIsUp() socket() error: %s\n", GetErrorString()); |
| 817 |
return IFACE_UNKNOWN; |
| 818 |
} |
| 819 |
return GetInterfaceStatus(ifaceName); |
| 820 |
} // end ProtoNet::GetInterfaceStatus(by index) |
| 821 |
|
| 822 |
// TBD - implement with proper system APIs instead of "ifconfig" command |
| 823 |
bool ProtoNet::AddInterfaceAddress(const char* ifaceName, const ProtoAddress& ifaceAddr, unsigned int maskLen) |
| 824 |
{ |
| 825 |
char cmd[1024]; |
| 826 |
#ifdef LINUX |
| 827 |
#ifdef __ANDROID__ |
| 828 |
sprintf(cmd, "ip addr add %s/%u dev %s", ifaceAddr.GetHostString(), maskLen, ifaceName); |
| 829 |
#else |
| 830 |
switch (ifaceAddr.GetType()) |
| 831 |
{ |
| 832 |
case ProtoAddress::IPv4: |
| 833 |
{ |
| 834 |
// Does the interface have any IPv4 addresses already assigned? |
| 835 |
ProtoAddressList addrList; |
| 836 |
GetInterfaceAddressList(ifaceName, ProtoAddress::IPv4, addrList); |
| 837 |
// See if we need to make an alias addr (or set this as primary) |
| 838 |
unsigned int addrCount = 0; |
| 839 |
bool hasPrimary = false; |
| 840 |
ProtoAddress addr; |
| 841 |
ProtoAddressList::Iterator iterator(addrList); |
| 842 |
while (iterator.GetNextAddress(addr)) |
| 843 |
{ |
| 844 |
addrCount++; |
| 845 |
if (!hasPrimary) |
| 846 |
{ |
| 847 |
char ifname[IFNAMSIZ+1]; |
| 848 |
ifname[IFNAMSIZ] = '\0'; |
| 849 |
if (!GetInterfaceName(addr, ifname, IFNAMSIZ)) |
| 850 |
{ |
| 851 |
PLOG(PL_ERROR, "ProtoNet::AddInterfaceAddress() error: unable to get interface name for addr %s\n", |
| 852 |
addr.GetHostString()); |
| 853 |
continue; |
| 854 |
} |
| 855 |
if (0 == strcmp(ifname, ifaceName)) hasPrimary = true; |
| 856 |
} |
| 857 |
} |
| 858 |
if (hasPrimary) |
| 859 |
{ |
| 860 |
char aliasName[IFNAMSIZ+1]; |
| 861 |
aliasName[IFNAMSIZ] = '\0'; |
| 862 |
strncpy(aliasName, ifaceName, IFNAMSIZ); |
| 863 |
unsigned int namelen = strlen(aliasName); |
| 864 |
if (namelen < IFNAMSIZ) |
| 865 |
{ |
| 866 |
strcat(aliasName, ":"); |
| 867 |
namelen++; |
| 868 |
} |
| 869 |
else |
| 870 |
{ |
| 871 |
PLOG(PL_ERROR, "ProtoNet::AddInterfaceAddress() error: interface name too long to alias\n"); |
| 872 |
return false; |
| 873 |
} |
| 874 |
char* iptr = aliasName + namelen; |
| 875 |
int space = IFNAMSIZ - namelen; |
| 876 |
int index = addrCount - 1; |
| 877 |
while (index < 10) |
| 878 |
{ |
| 879 |
int result = snprintf(iptr, space, "%d", index); |
| 880 |
if (result < 0) |
| 881 |
{ |
| 882 |
PLOG(PL_ERROR, "ProtoNet::AddInterfaceAddress() snprintf() error: %s\n", GetErrorString()); |
| 883 |
return false; |
| 884 |
} |
| 885 |
else if (result > space) |
| 886 |
{ |
| 887 |
PLOG(PL_ERROR, "ProtoNet::AddInterfaceAddress() error: alias exceeds max interface name length\n"); |
| 888 |
return false; |
| 889 |
} |
| 890 |
ProtoAddress ifAddr; |
| 891 |
if (!GetInterfaceAddress(aliasName, ProtoAddress::IPv4, ifAddr)) |
| 892 |
{ |
| 893 |
// This alias is available, so set address |
| 894 |
if (32 == maskLen) |
| 895 |
sprintf(cmd, "/sbin/ifconfig %s %s broadcast 0.0.0.0 netmask 255.255.255.255", aliasName, ifaceAddr.GetHostString()); |
| 896 |
else |
| 897 |
sprintf(cmd, "/sbin/ifconfig %s %s/%u", aliasName, ifaceAddr.GetHostString(), maskLen); |
| 898 |
break; |
| 899 |
} |
| 900 |
index++; |
| 901 |
} |
| 902 |
if (10 == index) return false; |
| 903 |
if (index < 0) |
| 904 |
{ |
| 905 |
PLOG(PL_ERROR, "ProtoNet::AddInterfaceAddress() error: no available alias found\n"); |
| 906 |
return false; |
| 907 |
} |
| 908 |
} |
| 909 |
else |
| 910 |
{ |
| 911 |
// Set primary address for interface ifaceName |
| 912 |
if (32 == maskLen) |
| 913 |
sprintf(cmd, "/sbin/ifconfig %s %s broadcast 0.0.0.0 netmask 255.255.255.255", ifaceName, ifaceAddr.GetHostString()); |
| 914 |
else |
| 915 |
sprintf(cmd, "/sbin/ifconfig %s %s/%u", ifaceName, ifaceAddr.GetHostString(), maskLen); |
| 916 |
} |
| 917 |
break; |
| 918 |
} |
| 919 |
case ProtoAddress::IPv6: |
| 920 |
{ |
| 921 |
sprintf(cmd, "/sbin/ifconfig %s add %s/%u", ifaceName, ifaceAddr.GetHostString(), maskLen); |
| 922 |
break; |
| 923 |
} |
| 924 |
default: |
| 925 |
{ |
| 926 |
PLOG(PL_ERROR, "ProtoNet::AddInterfaceAddress() error: invalid address type\n"); |
| 927 |
return false; |
| 928 |
} |
| 929 |
} |
| 930 |
#endif // if/else __ANDROID__ |
| 931 |
#else |
| 932 |
switch (ifaceAddr.GetType()) |
| 933 |
{ |
| 934 |
case ProtoAddress::IPv4: |
| 935 |
sprintf(cmd, "/sbin/ifconfig %s %s/%u alias", ifaceName, ifaceAddr.GetHostString(), maskLen); |
| 936 |
break; |
| 937 |
#ifdef HAVE_IPV6 |
| 938 |
case ProtoAddress::IPv6: |
| 939 |
sprintf(cmd, "/sbin/ifconfig %s inet6 %s/%u alias", ifaceName, ifaceAddr.GetHostString(), maskLen); |
| 940 |
break; |
| 941 |
#endif // HAVE_IPV6 |
| 942 |
default: |
| 943 |
PLOG(PL_ERROR, "ProtoNet::AddInterfaceAddress() error: invalid address type\n"); |
| 944 |
return false; |
| 945 |
} |
| 946 |
#endif // if/else LINUX / OTHER |
| 947 |
if (system(cmd) < 0) |
| 948 |
{ |
| 949 |
PLOG(PL_ERROR, "ProtoNet::AddInterfaceAddress() /sbin/ifconfig error: %s\n", GetErrorString()); |
| 950 |
return false; |
| 951 |
} |
| 952 |
return true; |
| 953 |
} // end ProtoNet::AddInterfaceAddress() |
| 954 |
|
| 955 |
bool ProtoNet::RemoveInterfaceAddress(const char* ifaceName, const ProtoAddress& ifaceAddr, unsigned int maskLen) |
| 956 |
{ |
| 957 |
char cmd[1024]; |
| 958 |
#ifdef LINUX |
| 959 |
#ifdef __ANDROID__ |
| 960 |
sprintf(cmd, "ip addr del %s/%u dev %s", ifaceAddr.GetHostString(), maskLen, ifaceName); |
| 961 |
#else |
| 962 |
switch (ifaceAddr.GetType()) |
| 963 |
{ |
| 964 |
case ProtoAddress::IPv4: |
| 965 |
{ |
| 966 |
// On linux we need to find the right interface alias |
| 967 |
char ifname[IFNAMSIZ+1]; |
| 968 |
ifname[IFNAMSIZ] = '\0'; |
| 969 |
if (!GetInterfaceName(ifaceAddr, ifname, IFNAMSIZ)) |
| 970 |
{ |
| 971 |
PLOG(PL_ERROR, "ProtoNet::RemoveInterfaceAddress() error: unknown interface address\n"); |
| 972 |
return false; |
| 973 |
} |
| 974 |
if (NULL == strchr(ifname, ':')) |
| 975 |
{ |
| 976 |
// Assign INADDR_NONE to remove address |
| 977 |
sprintf(cmd, "/sbin/ifconfig %s 0.0.0.0", ifname); |
| 978 |
} |
| 979 |
else |
| 980 |
{ |
| 981 |
// Bring down alias interface |
| 982 |
sprintf(cmd, "/sbin/ifconfig %s down", ifname); |
| 983 |
} |
| 984 |
break; |
| 985 |
} |
| 986 |
#ifdef HAVE_IPV6 |
| 987 |
case ProtoAddress::IPv6: |
| 988 |
{ |
| 989 |
// delete IPv6 address |
| 990 |
if (0 != maskLen) |
| 991 |
sprintf(cmd, "/sbin/ifconfig %s del %s/%d", ifaceName, ifaceAddr.GetHostString(), maskLen); |
| 992 |
else |
| 993 |
sprintf(cmd, "/sbin/ifconfig %s del %s", ifaceName, ifaceAddr.GetHostString()); |
| 994 |
break; |
| 995 |
} |
| 996 |
#endif // HAVE_IPV8 |
| 997 |
default: |
| 998 |
{ |
| 999 |
PLOG(PL_ERROR, "ProtoNet::RemoveInterfaceAddress() error: invalid address type\n"); |
| 1000 |
return false; |
| 1001 |
} |
| 1002 |
} |
| 1003 |
#endif // if/else __ANDROID__ |
| 1004 |
#else // BSD, MacOSX, etc |
| 1005 |
switch (ifaceAddr.GetType()) |
| 1006 |
{ |
| 1007 |
case ProtoAddress::IPv4: |
| 1008 |
sprintf(cmd, "/sbin/ifconfig %s %s -alias", ifaceName, ifaceAddr.GetHostString()); |
| 1009 |
break; |
| 1010 |
#ifdef HAVE_IPV6 |
| 1011 |
case ProtoAddress::IPv6: |
| 1012 |
sprintf(cmd, "/sbin/ifconfig %s inet6 %s -alias", ifaceName, ifaceAddr.GetHostString()); |
| 1013 |
break; |
| 1014 |
#endif // HAVE_IPV6 |
| 1015 |
default: |
| 1016 |
PLOG(PL_ERROR, "ProtoNet::RemoveInterfaceAddress() error: invalid address type\n"); |
| 1017 |
return false; |
| 1018 |
} |
| 1019 |
#endif // if/else LINUX/OTHER |
| 1020 |
if (system(cmd) < 0) |
| 1021 |
{ |
| 1022 |
PLOG(PL_ERROR, "ProtoNet::RemoveInterfaceAddress() /sbin/ifconfig error: %s\n", GetErrorString()); |
| 1023 |
return false; |
| 1024 |
} |
| 1025 |
return true; |
| 1026 |
} // end ProtoNet::RemoveInterfaceAddress() |
| 1027 |
|