| 1 |
|
| 2 |
#include "protoNet.h" |
| 3 |
#include "protoList.h" |
| 4 |
#include "protoString.h" // for ProtoTokenator |
| 5 |
#include "protoDebug.h" |
| 6 |
|
| 7 |
#include <stdio.h> |
| 8 |
#include <netinet/in.h> |
| 9 |
#include <linux/netlink.h> |
| 10 |
#include <linux/rtnetlink.h> |
| 11 |
#include <net/if.h> |
| 12 |
#include <unistd.h> // for getpid() |
| 13 |
#include <sys/ioctl.h> |
| 14 |
|
| 15 |
// Note that the remainder of the Linux ProtoNet stuff is |
| 16 |
// implemented in the "src/unix/unixNet.cpp" file |
| 17 |
// in the Protolib source tree and the common stuff is |
| 18 |
// in "src/common/protoNet.cpp" |
| 19 |
|
| 20 |
|
| 21 |
// This class wraps around a netlink socket to provide methods for |
| 22 |
// sending/receiving netlink messages for different purposes. |
| 23 |
class ProtoNetlink |
| 24 |
{ |
| 25 |
public: |
| 26 |
ProtoNetlink(); |
| 27 |
~ProtoNetlink(); |
| 28 |
|
| 29 |
bool Open(); |
| 30 |
void Close(); |
| 31 |
|
| 32 |
UINT32 GetPortId() const |
| 33 |
{return port_id;} |
| 34 |
|
| 35 |
bool SendRequest(void* req, size_t len); |
| 36 |
bool RecvResponse(UINT32 seq, struct nlmsghdr** bufferHandle, int* msgSize); |
| 37 |
|
| 38 |
private: |
| 39 |
int descriptor; |
| 40 |
UINT32 port_id; // aka netlink pid (not process id) |
| 41 |
}; // end class ProtoNetlink |
| 42 |
|
| 43 |
|
| 44 |
ProtoNetlink::ProtoNetlink() |
| 45 |
: descriptor(-1) |
| 46 |
{ |
| 47 |
} |
| 48 |
|
| 49 |
ProtoNetlink::~ProtoNetlink() |
| 50 |
{ |
| 51 |
Close(); |
| 52 |
} |
| 53 |
|
| 54 |
bool ProtoNetlink::Open() |
| 55 |
{ |
| 56 |
Close(); // in case already open |
| 57 |
descriptor = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE); |
| 58 |
if (descriptor < 0) |
| 59 |
{ |
| 60 |
PLOG(PL_ERROR, "ProtoNetlink::Open() socket() error: %s\n", GetErrorString()); |
| 61 |
return false; |
| 62 |
} |
| 63 |
|
| 64 |
// Here we use "bind()" to get a unique netlink port id (pid) from the kernel |
| 65 |
// (with localAddr.nl_pid passed into bind() set as '0', kernel assigns us one |
| 66 |
struct sockaddr_nl localAddr; |
| 67 |
memset(&localAddr, 0, sizeof(localAddr)); |
| 68 |
localAddr.nl_family = AF_NETLINK; |
| 69 |
if (0 > bind(descriptor, (struct sockaddr*) &localAddr, sizeof(localAddr))) |
| 70 |
{ |
| 71 |
PLOG(PL_ERROR, "ProtoNetlink::Open() bind() error: %s\n", GetErrorString()); |
| 72 |
Close(); |
| 73 |
return false; |
| 74 |
} |
| 75 |
// Get socket name so we know our port number (i.e. netlink pid) |
| 76 |
socklen_t addrLen = sizeof(localAddr); |
| 77 |
if (getsockname(descriptor, (struct sockaddr*)&localAddr, &addrLen) < 0) |
| 78 |
{ |
| 79 |
PLOG(PL_ERROR, "ProtoNetlink::Open() getsockname() error: %s\n", GetErrorString()); |
| 80 |
Close(); |
| 81 |
return false; |
| 82 |
} |
| 83 |
if (AF_NETLINK != localAddr.nl_family) |
| 84 |
{ |
| 85 |
PLOG(PL_ERROR, "ProtoNetlink::Open() error: invalid socket type?!\n"); |
| 86 |
Close(); |
| 87 |
return false; |
| 88 |
} |
| 89 |
port_id = localAddr.nl_pid; |
| 90 |
return true; |
| 91 |
} // end ProtoNetlink::Open() |
| 92 |
|
| 93 |
void ProtoNetlink::Close() |
| 94 |
{ |
| 95 |
if (descriptor >= 0) |
| 96 |
{ |
| 97 |
close(descriptor); |
| 98 |
descriptor = -1; |
| 99 |
} |
| 100 |
} // end ProtoNetlink::Close() |
| 101 |
|
| 102 |
bool ProtoNetlink::SendRequest(void* req, size_t len) |
| 103 |
{ |
| 104 |
// init iovec structure for sendmsg() |
| 105 |
struct iovec io; |
| 106 |
io.iov_base = req; |
| 107 |
io.iov_len = len; |
| 108 |
// init netlink sockaddr (addressed to "kernel") |
| 109 |
struct sockaddr_nl kernel; |
| 110 |
memset(&kernel, 0, sizeof(kernel)); |
| 111 |
kernel.nl_family = AF_NETLINK; |
| 112 |
// init msghdr struct for sendmsg() |
| 113 |
struct msghdr msg; |
| 114 |
memset(&msg, 0, sizeof(msg)); |
| 115 |
msg.msg_iov = &io; |
| 116 |
msg.msg_iovlen = 1; |
| 117 |
msg.msg_name = &kernel; |
| 118 |
msg.msg_namelen = sizeof(kernel); |
| 119 |
// now sendmsg (handling EINTR) |
| 120 |
for (;;) |
| 121 |
{ |
| 122 |
if (0 > sendmsg(descriptor, &msg, 0)) |
| 123 |
{ |
| 124 |
if (EINTR == errno) continue; |
| 125 |
PLOG(PL_ERROR, "ProtoNetlink::SendRequest() sendmsg() error: %s\n", GetErrorString()); |
| 126 |
return false; |
| 127 |
} |
| 128 |
else |
| 129 |
{ |
| 130 |
return true; |
| 131 |
} |
| 132 |
} |
| 133 |
} // end ProtoNetlink::SendRequest() |
| 134 |
|
| 135 |
bool ProtoNetlink::RecvResponse(UINT32 seq, struct nlmsghdr** bufferHandle, int* msgSize) |
| 136 |
{ |
| 137 |
if (descriptor < 0) |
| 138 |
{ |
| 139 |
PLOG(PL_ERROR, "ProtoNetlink::RecvResponse() error: netlink socket not open\n"); |
| 140 |
return false; |
| 141 |
} |
| 142 |
if ((NULL == bufferHandle) || (NULL == msgSize)) |
| 143 |
{ |
| 144 |
PLOG(PL_ERROR, "ProtoNetlink::RecvResponse() error: invalid parameters\n"); |
| 145 |
return false; |
| 146 |
} |
| 147 |
*bufferHandle = NULL; |
| 148 |
*msgSize = 0; |
| 149 |
// TBD - we need to check this behavior regarding determining buffer size ... |
| 150 |
// (The "buffer increase" strategy doesn't work so initial size must be big enough for now. |
| 151 |
// I need to see what strategy works (e.g. multi-part receives, or re-send request, etc) |
| 152 |
size_t bufsize = 4096/sizeof(struct nlmsghdr); // initial size (will be increased if needed |
| 153 |
struct nlmsghdr* buffer = NULL; |
| 154 |
for (;;) |
| 155 |
{ |
| 156 |
if (NULL != buffer) |
| 157 |
delete[] buffer; |
| 158 |
if (NULL == (buffer = new struct nlmsghdr[bufsize])) |
| 159 |
{ |
| 160 |
PLOG(PL_ERROR, "ProtoNetlink::RecvResponse() new nlmsghdr[] error: %s\n", GetErrorString()); |
| 161 |
return false; |
| 162 |
} |
| 163 |
for (;;) |
| 164 |
{ |
| 165 |
// init iovec struct |
| 166 |
struct iovec io; |
| 167 |
io.iov_base = buffer; |
| 168 |
io.iov_len = bufsize*sizeof(struct nlmsghdr); |
| 169 |
struct sockaddr_nl addr; |
| 170 |
// init msghdr struct for recvmsg() |
| 171 |
struct msghdr msg; |
| 172 |
msg.msg_iov = &io; |
| 173 |
msg.msg_iovlen = 1; |
| 174 |
msg.msg_name = &addr; |
| 175 |
msg.msg_namelen = sizeof(addr); |
| 176 |
msg.msg_control = NULL; |
| 177 |
msg.msg_controllen = 0; |
| 178 |
msg.msg_flags = 0; |
| 179 |
|
| 180 |
ssize_t result = recvmsg(descriptor, &msg, 0); |
| 181 |
if (result < 0) |
| 182 |
{ |
| 183 |
if (EINTR == errno) continue; |
| 184 |
PLOG(PL_ERROR, "ProtoNetlink::RecvResponse() recvmsg() error: %s\n", GetErrorString()); |
| 185 |
delete[] buffer; |
| 186 |
return false; |
| 187 |
} |
| 188 |
else if (0 != (MSG_TRUNC & msg.msg_flags)) |
| 189 |
{ |
| 190 |
// buffer was too, small |
| 191 |
bufsize *= 2; |
| 192 |
break; |
| 193 |
} |
| 194 |
else |
| 195 |
{ |
| 196 |
int saveResult = result; |
| 197 |
// We got a complete response, parse and make sure it's OK |
| 198 |
for(struct nlmsghdr* hdr = buffer; NLMSG_OK(hdr, (unsigned int)result); hdr = NLMSG_NEXT(hdr, result)) |
| 199 |
{ |
| 200 |
// Is it the response I'm looking for? |
| 201 |
if ((hdr->nlmsg_pid != port_id) || (seq != hdr->nlmsg_seq)) |
| 202 |
continue; // not for me |
| 203 |
if (NLMSG_ERROR == hdr->nlmsg_type) |
| 204 |
{ |
| 205 |
PLOG(PL_ERROR, "ProtoNetlink::RecvResponse() error: NLMSG_ERROR\n"); |
| 206 |
delete[] buffer; |
| 207 |
return false; |
| 208 |
} |
| 209 |
} |
| 210 |
// A non-error response was received |
| 211 |
*bufferHandle = buffer; |
| 212 |
*msgSize = saveResult; |
| 213 |
return true; |
| 214 |
} |
| 215 |
} |
| 216 |
} |
| 217 |
} // end ProtoNetlink::RecvResponse() |
| 218 |
|
| 219 |
#ifdef ANDROID |
| 220 |
// Although these function would also work for Linux, we just have these defined for |
| 221 |
// Android where the getifaddrs() function is not available. The non-Android Linux |
| 222 |
// versions of these are implemented in the more general "unixNet.cpp" module using getifaddrs() |
| 223 |
|
| 224 |
unsigned int ProtoNet::GetInterfaceName(const ProtoAddress& ifAddr, char* buffer, unsigned int buflen) |
| 225 |
{ |
| 226 |
// For now on Android, we'll get our list of interface indices and check each until we find |
| 227 |
// one with a matching address and then get its name. The reason we have a different approach |
| 228 |
// for this for Linux is to handle interface alias names, but not sure that applies to Android anyway? |
| 229 |
unsigned int ifCount = GetInterfaceCount(); |
| 230 |
if (0 == ifCount) |
| 231 |
{ |
| 232 |
PLOG(PL_WARN, "ProtoNet::GetInterfaceName() warning: no interfaces?!\n"); |
| 233 |
return 0; |
| 234 |
} |
| 235 |
// Then allocate a buffer of the appropriate size |
| 236 |
unsigned int* ifIndices = new unsigned int[ifCount]; |
| 237 |
if (NULL == ifIndices) |
| 238 |
{ |
| 239 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceName() new ifIndices[] error: %s\n", GetErrorString()); |
| 240 |
return false; |
| 241 |
} |
| 242 |
// Now call with a buffer to get this list of indices |
| 243 |
ifCount = GetInterfaceIndices(ifIndices, ifCount); |
| 244 |
for (unsigned int i = 0; i < ifCount; i++) |
| 245 |
{ |
| 246 |
unsigned int ifIndex = ifIndices[i]; |
| 247 |
char ifName[IFNAMSIZ+1]; |
| 248 |
ifName[IFNAMSIZ] = '\0'; |
| 249 |
if (0 == GetInterfaceName(ifIndex, ifName, IFNAMSIZ+1)) |
| 250 |
{ |
| 251 |
PLOG(PL_WARN, "ProtoNet::GetInterfaceName() no name for interface index %u?\n", ifIndex); |
| 252 |
continue; |
| 253 |
} |
| 254 |
ProtoAddressList addrList; |
| 255 |
if (GetInterfaceAddressList(ifName, ifAddr.GetType(), addrList)) |
| 256 |
{ |
| 257 |
ProtoAddressList::Iterator iterator(addrList); |
| 258 |
ProtoAddress addr; |
| 259 |
while (iterator.GetNextAddress(addr)) |
| 260 |
{ |
| 261 |
if (addr.HostIsEqual(ifAddr)) |
| 262 |
{ |
| 263 |
delete[] ifIndices; |
| 264 |
strncpy(buffer, ifName, buflen); |
| 265 |
return strlen(ifName); |
| 266 |
} |
| 267 |
} |
| 268 |
} |
| 269 |
} |
| 270 |
delete[] ifIndices; |
| 271 |
return 0; |
| 272 |
} // end ProtoNet::GetInterfaceName() |
| 273 |
|
| 274 |
bool ProtoNet::GetInterfaceAddressList(const char* interfaceName, |
| 275 |
ProtoAddress::Type addressType, |
| 276 |
ProtoAddressList& addrList, |
| 277 |
unsigned int* interfaceIndex) |
| 278 |
{ |
| 279 |
if (ProtoAddress::ETH == addressType) |
| 280 |
{ |
| 281 |
struct ifreq req; |
| 282 |
memset(&req, 0, sizeof(struct ifreq)); |
| 283 |
strncpy(req.ifr_name, interfaceName, IFNAMSIZ); |
| 284 |
int socketFd = socket(PF_INET, SOCK_DGRAM, 0); |
| 285 |
if (socketFd < 0) |
| 286 |
{ |
| 287 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceAddressList() socket() error: %s\n", GetErrorString()); |
| 288 |
return false; |
| 289 |
} |
| 290 |
// Get hardware (MAC) address instead of IP address |
| 291 |
if (ioctl(socketFd, SIOCGIFHWADDR, &req) < 0) |
| 292 |
{ |
| 293 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceAddressList() ioctl(SIOCGIFHWADDR) error: %s\n", |
| 294 |
GetErrorString()); |
| 295 |
close(socketFd); |
| 296 |
return false; |
| 297 |
} |
| 298 |
else |
| 299 |
{ |
| 300 |
close(socketFd); |
| 301 |
if (NULL != interfaceIndex) |
| 302 |
*interfaceIndex = req.ifr_ifindex; |
| 303 |
ProtoAddress ethAddr; |
| 304 |
if (!ethAddr.SetRawHostAddress(ProtoAddress::ETH, |
| 305 |
(const char*)&req.ifr_hwaddr.sa_data, |
| 306 |
IFHWADDRLEN)) |
| 307 |
{ |
| 308 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceAddressList() error: invalid ETH addr?\n"); |
| 309 |
return false; |
| 310 |
} |
| 311 |
if (!addrList.Insert(ethAddr)) |
| 312 |
{ |
| 313 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceAddressList() error: unable to add ETH addr to list.\n"); |
| 314 |
return false; |
| 315 |
} |
| 316 |
return true; |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
unsigned int ifIndex = GetInterfaceIndex(interfaceName); |
| 321 |
if (0 == ifIndex) |
| 322 |
{ |
| 323 |
// Perhaps "interfaceName" is an address string? |
| 324 |
ProtoAddress ifAddr; |
| 325 |
if (ifAddr.ConvertFromString(interfaceName)) |
| 326 |
ifIndex = GetInterfaceIndex(ifAddr); |
| 327 |
} |
| 328 |
if (0 == ifIndex) |
| 329 |
{ |
| 330 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceAddressList() error: invalid interface name\n"); |
| 331 |
return false; |
| 332 |
} |
| 333 |
if (NULL != interfaceIndex) *interfaceIndex = ifIndex; |
| 334 |
// Instantiate a netlink socket and open it |
| 335 |
ProtoNetlink nlink; |
| 336 |
if (!nlink.Open()) |
| 337 |
{ |
| 338 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceAddressList() error: unable to open netlink socket\n"); |
| 339 |
return false; |
| 340 |
} |
| 341 |
|
| 342 |
// Construct request for interface addresses |
| 343 |
struct |
| 344 |
{ |
| 345 |
struct nlmsghdr msg; |
| 346 |
struct ifaddrmsg ifa; |
| 347 |
} req; |
| 348 |
memset(&req, 0, sizeof(req)); |
| 349 |
|
| 350 |
// fixed sequence number for single request |
| 351 |
UINT32 seq = 1; |
| 352 |
|
| 353 |
// netlink message header |
| 354 |
req.msg.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg)); |
| 355 |
req.msg.nlmsg_type = RTM_GETADDR; |
| 356 |
req.msg.nlmsg_flags = NLM_F_REQUEST | NLM_F_MATCH ; |
| 357 |
req.msg.nlmsg_seq = seq; // fixed sequence number for single request |
| 358 |
req.msg.nlmsg_pid = nlink.GetPortId(); |
| 359 |
|
| 360 |
// route dump request for given addressType and interface index |
| 361 |
unsigned int addrLength = 0; |
| 362 |
unsigned char addrFamily = AF_UNSPEC; |
| 363 |
switch (addressType) |
| 364 |
{ |
| 365 |
case ProtoAddress::IPv4: |
| 366 |
|
| 367 |
addrFamily = req.ifa.ifa_family = AF_INET; |
| 368 |
req.ifa.ifa_prefixlen = 32; |
| 369 |
addrLength = 4; |
| 370 |
break; |
| 371 |
#ifdef HAVE_IPV6 |
| 372 |
case ProtoAddress::IPv6: |
| 373 |
|
| 374 |
addrFamily = req.ifa.ifa_family = AF_INET6; |
| 375 |
req.ifa.ifa_prefixlen = 128; |
| 376 |
addrLength = 16; |
| 377 |
break; |
| 378 |
#endif // HAVE_IPV6 |
| 379 |
default: |
| 380 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceAddressList() invalid address type!\n"); |
| 381 |
return false; |
| 382 |
} |
| 383 |
req.ifa.ifa_flags = 0;//IFA_F_SECONDARY;//0;//IFA_F_PERMANENT; |
| 384 |
req.ifa.ifa_scope = RT_SCOPE_UNIVERSE; |
| 385 |
req.ifa.ifa_index = ifIndex; |
| 386 |
if (!nlink.SendRequest(&req, sizeof(req))) |
| 387 |
{ |
| 388 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceAddressList() error: unable to send netlink request\n"); |
| 389 |
return false; |
| 390 |
} |
| 391 |
// Request sent, now receive response(s) until done |
| 392 |
bool done = false; |
| 393 |
ProtoAddressList localAddrList; // keep link/site local addresses separate and add to "addrList" at end |
| 394 |
while (!done) |
| 395 |
{ |
| 396 |
struct nlmsghdr* buffer; |
| 397 |
int msgLen; |
| 398 |
if (nlink.RecvResponse(seq, &buffer, &msgLen)) |
| 399 |
{ |
| 400 |
struct nlmsghdr* msg = buffer; |
| 401 |
// Parse response, adding matching addresses for "addressType" and "ifIndex" |
| 402 |
for (; 0 != NLMSG_OK(msg, (unsigned int)msgLen); msg = NLMSG_NEXT(msg, msgLen)) |
| 403 |
{ |
| 404 |
// only pay attention to matching netlink responses received |
| 405 |
if ((msg->nlmsg_pid != nlink.GetPortId()) || (msg->nlmsg_seq != seq)) |
| 406 |
continue; |
| 407 |
switch (msg->nlmsg_type) |
| 408 |
{ |
| 409 |
case NLMSG_NOOP: |
| 410 |
//TRACE("recvd NLMSG_NOOP ...\n"); |
| 411 |
break; |
| 412 |
case NLMSG_ERROR: |
| 413 |
{ |
| 414 |
struct nlmsgerr* errorMsg = (struct nlmsgerr*)NLMSG_DATA(msg); |
| 415 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceAddressList() recvd NLMSG_ERROR error seq:%d code:%d...\n", |
| 416 |
msg->nlmsg_seq, errorMsg->error); |
| 417 |
delete[] buffer; |
| 418 |
return false; |
| 419 |
} |
| 420 |
case NLMSG_DONE: |
| 421 |
//TRACE("recvd NLMSG_DONE ...\n"); |
| 422 |
done = true; |
| 423 |
break; |
| 424 |
case RTM_NEWADDR: |
| 425 |
{ |
| 426 |
//TRACE("recvd RTM_NEWADDR ... \n"); |
| 427 |
struct ifaddrmsg* ifa = (struct ifaddrmsg*)NLMSG_DATA(msg); |
| 428 |
struct rtattr* rta = IFA_RTA(ifa); |
| 429 |
int rtaLen = msg->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifaddrmsg)); |
| 430 |
for (; RTA_OK(rta, rtaLen); rta = RTA_NEXT(rta, rtaLen)) |
| 431 |
{ |
| 432 |
switch (rta->rta_type) |
| 433 |
{ |
| 434 |
case IFA_ADDRESS: |
| 435 |
case IFA_LOCAL: |
| 436 |
{ |
| 437 |
if ((ifa->ifa_index == ifIndex) && (ifa->ifa_family == addrFamily)) |
| 438 |
{ |
| 439 |
switch (ifa->ifa_scope) |
| 440 |
{ |
| 441 |
case RT_SCOPE_UNIVERSE: |
| 442 |
{ |
| 443 |
ProtoAddress theAddress; |
| 444 |
theAddress.SetRawHostAddress(addressType, (char*)RTA_DATA(rta), addrLength); |
| 445 |
if (theAddress.IsValid()) |
| 446 |
{ |
| 447 |
if (!addrList.Insert(theAddress)) |
| 448 |
{ |
| 449 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceAddressList() error: couldn't add to addrList\n"); |
| 450 |
done = true; |
| 451 |
} |
| 452 |
} |
| 453 |
break; |
| 454 |
} |
| 455 |
case RT_SCOPE_SITE: |
| 456 |
case RT_SCOPE_LINK: |
| 457 |
case RT_SCOPE_HOST: |
| 458 |
{ |
| 459 |
// Keep site-local addresses in separate list and add at end. |
| 460 |
ProtoAddress theAddress; |
| 461 |
theAddress.SetRawHostAddress(addressType, (char*)RTA_DATA(rta), addrLength); |
| 462 |
if (theAddress.IsValid()) |
| 463 |
{ |
| 464 |
if (!localAddrList.Insert(theAddress)) |
| 465 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceAddressList() error: couldn't add to localAddrList\n"); |
| 466 |
} |
| 467 |
break; |
| 468 |
} |
| 469 |
default: |
| 470 |
// ignore other address types for now |
| 471 |
break; |
| 472 |
} |
| 473 |
} |
| 474 |
break; |
| 475 |
} |
| 476 |
case IFA_BROADCAST: |
| 477 |
{ |
| 478 |
//ProtoAddress addr; |
| 479 |
//addr.SetRawHostAddress(addrType, (char*)RTA_DATA(rta), addrLength); |
| 480 |
//TRACE("IFA_BROADCAST: %s\n", addr.GetHostString()); |
| 481 |
break; |
| 482 |
} |
| 483 |
default: |
| 484 |
//TRACE("ProtoNet::GetInterfaceAddressList() unhandled rtattr type:%d len:%d\n", |
| 485 |
// rta->rta_type, RTA_PAYLOAD(rta)); |
| 486 |
break; |
| 487 |
|
| 488 |
} // end switch(rta_type) |
| 489 |
} // end for(RTA_NEXT()) |
| 490 |
break; |
| 491 |
} |
| 492 |
default: |
| 493 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceAddressList() matching reply type:%d len:%d bytes\n", |
| 494 |
msg->nlmsg_type, msg->nlmsg_len); |
| 495 |
break; |
| 496 |
} // end switch(nlmsg_type) |
| 497 |
} |
| 498 |
ASSERT(NULL != buffer); |
| 499 |
delete[] buffer; |
| 500 |
} // end if (nlink.RecvResponse()) |
| 501 |
else |
| 502 |
{ |
| 503 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceAddressList() error:invalid netlink response\n"); |
| 504 |
done = true; |
| 505 |
} |
| 506 |
} // end while(!done) |
| 507 |
ProtoAddressList::Iterator iterator(localAddrList); |
| 508 |
ProtoAddress localAddr; |
| 509 |
while (iterator.GetNextAddress(localAddr)) |
| 510 |
{ |
| 511 |
if (!addrList.Insert(localAddr)) |
| 512 |
{ |
| 513 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceAddressList() error: couldn't add localAddr to addrList\n"); |
| 514 |
break; |
| 515 |
} |
| 516 |
} |
| 517 |
nlink.Close(); |
| 518 |
return true; |
| 519 |
} // end ProtoNet::GetInterfaceAddressList() |
| 520 |
|
| 521 |
|
| 522 |
unsigned int ProtoNet::GetInterfaceAddressMask(const char* ifaceName, const ProtoAddress& theAddr) |
| 523 |
{ |
| 524 |
unsigned int ifIndex = GetInterfaceIndex(ifaceName); |
| 525 |
if (0 == ifIndex) |
| 526 |
{ |
| 527 |
// Perhaps "interfaceName" is an address string? |
| 528 |
ProtoAddress ifAddr; |
| 529 |
if (ifAddr.ConvertFromString(ifaceName)) |
| 530 |
ifIndex = GetInterfaceIndex(ifAddr); |
| 531 |
} |
| 532 |
if (0 == ifIndex) |
| 533 |
{ |
| 534 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceAddressList() error: invalid interface name\n"); |
| 535 |
return false; |
| 536 |
} |
| 537 |
// Instantiate a netlink socket and open it |
| 538 |
ProtoNetlink nlink; |
| 539 |
if (!nlink.Open()) |
| 540 |
{ |
| 541 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceAddressList() error: unable to open netlink socket\n"); |
| 542 |
return false; |
| 543 |
} |
| 544 |
// Construct request for interface addresses |
| 545 |
struct |
| 546 |
{ |
| 547 |
struct nlmsghdr msg; |
| 548 |
struct ifaddrmsg ifa; |
| 549 |
} req; |
| 550 |
memset(&req, 0, sizeof(req)); |
| 551 |
|
| 552 |
// fixed sequence number for single request |
| 553 |
UINT32 seq = 1; |
| 554 |
|
| 555 |
// netlink message header |
| 556 |
req.msg.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg)); |
| 557 |
req.msg.nlmsg_type = RTM_GETADDR; |
| 558 |
req.msg.nlmsg_flags = NLM_F_REQUEST | NLM_F_MATCH ; |
| 559 |
req.msg.nlmsg_seq = seq; // fixed sequence number for single request |
| 560 |
req.msg.nlmsg_pid = nlink.GetPortId(); |
| 561 |
|
| 562 |
// route dump request for given addressType and interface index |
| 563 |
unsigned char addrFamily = AF_UNSPEC; |
| 564 |
switch (theAddr.GetType()) |
| 565 |
{ |
| 566 |
case ProtoAddress::IPv4: |
| 567 |
|
| 568 |
addrFamily = req.ifa.ifa_family = AF_INET; |
| 569 |
req.ifa.ifa_prefixlen = 32; |
| 570 |
break; |
| 571 |
#ifdef HAVE_IPV6 |
| 572 |
case ProtoAddress::IPv6: |
| 573 |
|
| 574 |
addrFamily = req.ifa.ifa_family = AF_INET6; |
| 575 |
req.ifa.ifa_prefixlen = 128; |
| 576 |
break; |
| 577 |
#endif // HAVE_IPV6 |
| 578 |
default: |
| 579 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceAddressList() invalid address type!\n"); |
| 580 |
return false; |
| 581 |
} |
| 582 |
req.ifa.ifa_flags = 0;//IFA_F_SECONDARY;//0;//IFA_F_PERMANENT; |
| 583 |
req.ifa.ifa_scope = RT_SCOPE_UNIVERSE; |
| 584 |
req.ifa.ifa_index = ifIndex; |
| 585 |
if (!nlink.SendRequest(&req, sizeof(req))) |
| 586 |
{ |
| 587 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceAddressList() error: unable to send netlink request\n"); |
| 588 |
return false; |
| 589 |
} |
| 590 |
// Request sent, now receive response(s) until done |
| 591 |
bool done = false; |
| 592 |
ProtoAddressList localAddrList; // keep link/site local addresses separate and add to "addrList" at end |
| 593 |
while (!done) |
| 594 |
{ |
| 595 |
struct nlmsghdr* buffer = NULL; |
| 596 |
int msgLen; |
| 597 |
if (nlink.RecvResponse(seq, &buffer, &msgLen)) |
| 598 |
{ |
| 599 |
struct nlmsghdr* msg = buffer; |
| 600 |
// Parse response, adding matching addresses for "addressType" and "ifIndex" |
| 601 |
for (; 0 != NLMSG_OK(msg, (unsigned int)msgLen); msg = NLMSG_NEXT(msg, msgLen)) |
| 602 |
{ |
| 603 |
// only pay attention to matching netlink responses received |
| 604 |
if ((msg->nlmsg_pid != nlink.GetPortId()) || (msg->nlmsg_seq != seq)) |
| 605 |
continue; |
| 606 |
switch (msg->nlmsg_type) |
| 607 |
{ |
| 608 |
case NLMSG_NOOP: |
| 609 |
//TRACE("recvd NLMSG_NOOP ...\n"); |
| 610 |
break; |
| 611 |
case NLMSG_ERROR: |
| 612 |
{ |
| 613 |
struct nlmsgerr* errorMsg = (struct nlmsgerr*)NLMSG_DATA(msg); |
| 614 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceAddressList() recvd NLMSG_ERROR error seq:%d code:%d...\n", |
| 615 |
msg->nlmsg_seq, errorMsg->error); |
| 616 |
delete[] buffer; |
| 617 |
return false; |
| 618 |
} |
| 619 |
case NLMSG_DONE: |
| 620 |
//TRACE("recvd NLMSG_DONE ...\n"); |
| 621 |
done = true; |
| 622 |
break; |
| 623 |
case RTM_NEWADDR: |
| 624 |
{ |
| 625 |
//TRACE("recvd RTM_NEWADDR ... \n"); |
| 626 |
struct ifaddrmsg* ifa = (struct ifaddrmsg*)NLMSG_DATA(msg); |
| 627 |
struct rtattr* rta = IFA_RTA(ifa); |
| 628 |
int rtaLen = msg->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifaddrmsg)); |
| 629 |
for (; RTA_OK(rta, rtaLen); rta = RTA_NEXT(rta, rtaLen)) |
| 630 |
{ |
| 631 |
switch (rta->rta_type) |
| 632 |
{ |
| 633 |
case IFA_ADDRESS: |
| 634 |
case IFA_LOCAL: |
| 635 |
case IFA_BROADCAST: |
| 636 |
{ |
| 637 |
if ((ifa->ifa_index == ifIndex) && (ifa->ifa_family == addrFamily)) |
| 638 |
{ |
| 639 |
ProtoAddress addr; |
| 640 |
addr.SetRawHostAddress(theAddr.GetType(), (char*)RTA_DATA(rta), theAddr.GetLength()); |
| 641 |
if (addr.HostIsEqual(theAddr)) |
| 642 |
{ |
| 643 |
// We have a match, return its prefix length |
| 644 |
unsigned int prefixLen = ifa->ifa_prefixlen; |
| 645 |
nlink.Close(); |
| 646 |
delete[] buffer; |
| 647 |
return prefixLen; |
| 648 |
} |
| 649 |
} |
| 650 |
break; |
| 651 |
} |
| 652 |
default: |
| 653 |
//TRACE("ProtoNet::GetInterfaceAddressList() unhandled rtattr type:%d len:%d\n", |
| 654 |
// rta->rta_type, RTA_PAYLOAD(rta)); |
| 655 |
break; |
| 656 |
|
| 657 |
} // end switch(rta_type) |
| 658 |
} // end for(RTA_NEXT()) |
| 659 |
break; |
| 660 |
} |
| 661 |
default: |
| 662 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceAddressList() matching reply type:%d len:%d bytes\n", |
| 663 |
msg->nlmsg_type, msg->nlmsg_len); |
| 664 |
break; |
| 665 |
} // end switch(nlmsg_type) |
| 666 |
} |
| 667 |
ASSERT(NULL != buffer); |
| 668 |
delete[] buffer; |
| 669 |
} // end if (nlink.RecvResponse() |
| 670 |
else |
| 671 |
{ |
| 672 |
PLOG(PL_ERROR, "ProtoNet::GetInterfaceAddressList() error:invalid netlink response\n"); |
| 673 |
done = true; |
| 674 |
} |
| 675 |
} // end while(!done) |
| 676 |
nlink.Close(); |
| 677 |
return 0; |
| 678 |
} // end ProtoNet::GetInterfaceAddressMask() |
| 679 |
|
| 680 |
#endif // ANDROID |
| 681 |
|
| 682 |
static unsigned int Readline(FILE* filePtr, char* buffer, unsigned int buflen) |
| 683 |
{ |
| 684 |
unsigned int index = 0; |
| 685 |
while (index < buflen) |
| 686 |
{ |
| 687 |
if (0 == fread(buffer+index, 1 , 1, filePtr)) |
| 688 |
return index; |
| 689 |
else if ('\n' == buffer[index]) |
| 690 |
return (index + 1); |
| 691 |
else |
| 692 |
index++; |
| 693 |
} |
| 694 |
return (index+1); // return value > buflen indicates line exceeds buflen |
| 695 |
} // end Readline() |
| 696 |
|
| 697 |
|
| 698 |
bool ProtoNet::GetGroupMemberships(const char* ifaceName, ProtoAddress::Type addrType, ProtoAddressList& addrList) |
| 699 |
{ |
| 700 |
if (ProtoAddress::IPv4 == addrType) |
| 701 |
{ |
| 702 |
// TBD - is there a netlink interface for this instead? |
| 703 |
FILE* filePtr = fopen("/proc/net/igmp", "r"); |
| 704 |
if (NULL == filePtr) |
| 705 |
{ |
| 706 |
PLOG(PL_ERROR, "ProtoNet::GetGroupMemberships() fopen(/proc/net/igmp) error: %s\n", GetErrorString()); |
| 707 |
return false; |
| 708 |
} |
| 709 |
bool seekingIface = true; |
| 710 |
unsigned int numGroups = 0; |
| 711 |
unsigned int skipCount = 1; // skip the first line (header line) |
| 712 |
unsigned len; |
| 713 |
char buffer[256]; |
| 714 |
while (0 != (len = Readline(filePtr, buffer, 255))) |
| 715 |
{ |
| 716 |
if (len > 255) |
| 717 |
{ |
| 718 |
PLOG(PL_ERROR, "ProtoNet::GetGroupMemberships() error: /proc/net/igmp line length exceeds max expected!\n"); |
| 719 |
fclose(filePtr); |
| 720 |
return false; |
| 721 |
} |
| 722 |
if (0 != skipCount) |
| 723 |
{ |
| 724 |
skipCount--; |
| 725 |
continue; |
| 726 |
} |
| 727 |
buffer[len+1] = '\0'; // apply NULL terminator |
| 728 |
if (seekingIface) |
| 729 |
{ |
| 730 |
ProtoTokenator tk(buffer); |
| 731 |
unsigned int index = 0; |
| 732 |
const char* next; |
| 733 |
while (NULL != (next = tk.GetNextItem())) |
| 734 |
{ |
| 735 |
if (1 == index) |
| 736 |
{ |
| 737 |
// Interface name is second item on line |
| 738 |
if (0 == strcmp(ifaceName, next)) |
| 739 |
seekingIface = false; // we found it |
| 740 |
} |
| 741 |
else if (3 == index) |
| 742 |
{ |
| 743 |
unsigned int count; |
| 744 |
int result; |
| 745 |
if (1 != (result = sscanf(next, "%u", &count))) |
| 746 |
{ |
| 747 |
if (seekingIface) |
| 748 |
{ |
| 749 |
skipCount = 0; |
| 750 |
} |
| 751 |
else |
| 752 |
{ |
| 753 |
PLOG(PL_ERROR, "ProtoNet::GetGroupMemberships() error: invalid 'Count' %s for iface %s?\n", |
| 754 |
next, ifaceName); |
| 755 |
fclose(filePtr); |
| 756 |
return false; |
| 757 |
} |
| 758 |
} |
| 759 |
else if (seekingIface) |
| 760 |
{ |
| 761 |
skipCount = count; |
| 762 |
} |
| 763 |
else |
| 764 |
{ |
| 765 |
if (0 == count) |
| 766 |
{ |
| 767 |
fclose(filePtr); |
| 768 |
return true; |
| 769 |
} |
| 770 |
numGroups = count; |
| 771 |
} |
| 772 |
} |
| 773 |
index++; |
| 774 |
} |
| 775 |
if (index < 3) |
| 776 |
{ |
| 777 |
PLOG(PL_ERROR, "ProtoNet::GetGroupMemberships() error: no 'Count' for iface %s?\n", ifaceName); |
| 778 |
fclose(filePtr); |
| 779 |
return false; |
| 780 |
} |
| 781 |
} |
| 782 |
else |
| 783 |
{ |
| 784 |
ASSERT(0 != numGroups); |
| 785 |
ProtoTokenator tk(buffer); |
| 786 |
const char* groupText = tk.GetNextItem(); // first token is group addr in hex |
| 787 |
UINT32 groupInt; // note it's already in Network Byte Order (Big Endian) |
| 788 |
if (1 != sscanf(groupText, "%x", &groupInt)) |
| 789 |
{ |
| 790 |
PLOG(PL_ERROR, "ProtoNet::GetGroupMemberships() error: invalid group addr for iface %s?\n", ifaceName); |
| 791 |
fclose(filePtr); |
| 792 |
return false; |
| 793 |
} |
| 794 |
ProtoAddress groupAddr; |
| 795 |
groupAddr.SetRawHostAddress(ProtoAddress::IPv4, (const char*)&groupInt, 4); |
| 796 |
if (!addrList.Insert(groupAddr)) |
| 797 |
{ |
| 798 |
PLOG(PL_ERROR, "ProtoNet::GetGroupMemberships() error: unable to insert IPv4 group addr to list!\n"); |
| 799 |
fclose(filePtr); |
| 800 |
return false; |
| 801 |
} |
| 802 |
numGroups--; |
| 803 |
if (0 == numGroups) |
| 804 |
{ |
| 805 |
fclose(filePtr); |
| 806 |
return true; |
| 807 |
} |
| 808 |
} |
| 809 |
} |
| 810 |
fclose(filePtr); |
| 811 |
if (0 != numGroups) |
| 812 |
{ |
| 813 |
PLOG(PL_ERROR, "ProtoNet::GetGroupMemberships() error: missing group listings for interface %s\n", ifaceName); |
| 814 |
return false; |
| 815 |
} |
| 816 |
else if (seekingIface) |
| 817 |
{ |
| 818 |
PLOG(PL_WARN, "ProtoNet::GetGroupMemberships() warning: requested interface %s not listed?!\n", ifaceName); |
| 819 |
} |
| 820 |
else |
| 821 |
{ |
| 822 |
ASSERT(0); // shouldn't get here |
| 823 |
} |
| 824 |
return true; |
| 825 |
} |
| 826 |
else if (ProtoAddress::IPv6 == addrType) |
| 827 |
{ |
| 828 |
// TBD - is there a netlink interface for this instead? |
| 829 |
FILE* filePtr = fopen("/proc/net/igmp6", "r"); |
| 830 |
if (NULL == filePtr) |
| 831 |
{ |
| 832 |
PLOG(PL_ERROR, "ProtoNet::GetGroupMemberships() fopen(/proc/net/igmp) error: %s\n", GetErrorString()); |
| 833 |
return false; |
| 834 |
} |
| 835 |
unsigned len; |
| 836 |
char buffer[256]; |
| 837 |
while (0 != (len = Readline(filePtr, buffer, 255))) |
| 838 |
{ |
| 839 |
// Format of lines are <index> <ifaceName> <address> ... |
| 840 |
ProtoTokenator tk(buffer); |
| 841 |
const char* next; |
| 842 |
bool match = false; |
| 843 |
unsigned int index = 0; |
| 844 |
while (NULL != (next = tk.GetNextItem())) |
| 845 |
{ |
| 846 |
if (1 == index) |
| 847 |
{ |
| 848 |
if (0 == strcmp(next, ifaceName)) |
| 849 |
match = true; |
| 850 |
else |
| 851 |
break; |
| 852 |
} |
| 853 |
else if (2 == index) |
| 854 |
{ |
| 855 |
ASSERT(match); |
| 856 |
break; // 'next' points to address string |
| 857 |
} |
| 858 |
index++; |
| 859 |
} // end while (tk.GetNextItem()) |
| 860 |
if (match) |
| 861 |
{ |
| 862 |
if (NULL == next) |
| 863 |
{ |
| 864 |
PLOG(PL_ERROR, "ProtoNet::GetGroupMemberships() error: missing IPv6 group addr %s for interface %s\n", |
| 865 |
next, ifaceName); |
| 866 |
fclose(filePtr); |
| 867 |
return false; |
| 868 |
} |
| 869 |
if (strlen(next) < 32) |
| 870 |
{ |
| 871 |
PLOG(PL_ERROR, "ProtoNet::GetGroupMemberships() error: incomplete IPv6 group addr %s for interface %s\n", |
| 872 |
next, ifaceName); |
| 873 |
fclose(filePtr); |
| 874 |
return false; |
| 875 |
} |
| 876 |
char addr[16]; // will contain IPv6 group addr |
| 877 |
for (int i = 0; i < 32; i += 8) |
| 878 |
{ |
| 879 |
//char wordText[9]; |
| 880 |
//wordText[8] = '\0'; |
| 881 |
//strncpy(wordText, next + i, 8); |
| 882 |
UINT32 word; |
| 883 |
if (1 != sscanf(next + i, "%8x", &word)) |
| 884 |
{ |
| 885 |
PLOG(PL_ERROR, "ProtoNet::GetGroupMemberships() error: invalid IPv6 group addr %s for interface %s\n", |
| 886 |
next, ifaceName); |
| 887 |
fclose(filePtr); |
| 888 |
return false; |
| 889 |
} |
| 890 |
word = htonl(word); |
| 891 |
memcpy(addr + i/2, &word, 4); |
| 892 |
} |
| 893 |
ProtoAddress groupAddr; |
| 894 |
groupAddr.SetRawHostAddress(ProtoAddress::IPv6, addr, 16); |
| 895 |
if (!addrList.Insert(groupAddr)) |
| 896 |
{ |
| 897 |
PLOG(PL_ERROR, "ProtoNet::GetGroupMemberships() error: unable to insert IPv6 group addr to list!\n"); |
| 898 |
fclose(filePtr); |
| 899 |
return false; |
| 900 |
} |
| 901 |
} |
| 902 |
} // end while (Readline()) |
| 903 |
return true; |
| 904 |
} |
| 905 |
else |
| 906 |
{ |
| 907 |
PLOG(PL_ERROR, "ProtoNet::GetGroupMemberships() error: invalid address typs\n"); |
| 908 |
return false; |
| 909 |
} |
| 910 |
} // end ProtoNet::GetGroupMemberships() |
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
class LinuxNetMonitor : public ProtoNet::Monitor |
| 915 |
{ |
| 916 |
public: |
| 917 |
LinuxNetMonitor(); |
| 918 |
~LinuxNetMonitor(); |
| 919 |
|
| 920 |
bool Open(); |
| 921 |
void Close(); |
| 922 |
bool GetNextEvent(Event& theEvent); |
| 923 |
|
| 924 |
private: |
| 925 |
// Since a Linux netlink message may have multiple |
| 926 |
// network interface events, we cache them in a linked |
| 927 |
// list for retrieval by the GetNextEvent() method |
| 928 |
class EventItem : public Event, public ProtoList::Item |
| 929 |
{ |
| 930 |
public: |
| 931 |
EventItem(); |
| 932 |
~EventItem(); |
| 933 |
}; // end class LinuxNetMonitor::EventItem |
| 934 |
class EventList : public ProtoListTemplate<EventItem> {}; |
| 935 |
|
| 936 |
class Interface : public ProtoTree::Item |
| 937 |
{ |
| 938 |
public: |
| 939 |
Interface(unsigned int index, const char* name); |
| 940 |
~Interface(); |
| 941 |
|
| 942 |
void SetName(const char* ifName) |
| 943 |
{strncpy(iface_name, ifName, IFNAMSIZ);} |
| 944 |
const char* GetName() const |
| 945 |
{return iface_name;} |
| 946 |
unsigned int GetIndex() const |
| 947 |
{return iface_index;} |
| 948 |
|
| 949 |
private: |
| 950 |
const char* GetKey() const |
| 951 |
{return ((const char*)&iface_index);} |
| 952 |
unsigned int GetKeysize() const |
| 953 |
{return (sizeof(unsigned int) << 3);} |
| 954 |
|
| 955 |
char iface_name[IFNAMSIZ+1]; |
| 956 |
unsigned int iface_name_bits; |
| 957 |
unsigned int iface_index; |
| 958 |
}; // end class LinuxNetMonitor::Interface |
| 959 |
class InterfaceList : public ProtoTreeTemplate<Interface> |
| 960 |
{ |
| 961 |
public: |
| 962 |
Interface* FindInterface(unsigned int ifIndex) |
| 963 |
{return Find((char*)&ifIndex, sizeof(unsigned int) << 3);} |
| 964 |
}; |
| 965 |
|
| 966 |
InterfaceList iface_list; |
| 967 |
EventList event_list; |
| 968 |
EventList event_pool; |
| 969 |
|
| 970 |
}; // end class LinuxNetMonitor |
| 971 |
|
| 972 |
|
| 973 |
LinuxNetMonitor::Interface::Interface(unsigned int index, const char* name) |
| 974 |
: iface_index(index) |
| 975 |
{ |
| 976 |
iface_name[IFNAMSIZ] = '\0'; |
| 977 |
SetName(name); |
| 978 |
} |
| 979 |
|
| 980 |
LinuxNetMonitor::Interface::~Interface() |
| 981 |
{ |
| 982 |
} |
| 983 |
|
| 984 |
// This is the implementation of the ProtoNet::Monitor::Create() |
| 985 |
// static method (our Linux-specific factory) |
| 986 |
ProtoNet::Monitor* ProtoNet::Monitor::Create() |
| 987 |
{ |
| 988 |
return static_cast<ProtoNet::Monitor*>(new LinuxNetMonitor); |
| 989 |
} // end ProtoNet::Monitor::Create() |
| 990 |
|
| 991 |
LinuxNetMonitor::LinuxNetMonitor() |
| 992 |
{ |
| 993 |
} |
| 994 |
|
| 995 |
LinuxNetMonitor::~LinuxNetMonitor() |
| 996 |
{ |
| 997 |
} |
| 998 |
|
| 999 |
bool LinuxNetMonitor::Open() |
| 1000 |
{ |
| 1001 |
if (IsOpen()) Close(); |
| 1002 |
if (0 > (descriptor = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE))) |
| 1003 |
{ |
| 1004 |
PLOG(PL_ERROR, "LinuxNetMonitor::Open() socket() error: %s\n", |
| 1005 |
GetErrorString()); |
| 1006 |
return false; |
| 1007 |
} |
| 1008 |
|
| 1009 |
// Send a netlink request message to subscribe to the |
| 1010 |
// RTMGRP_IPV4_IFADDR and RTMGRP_IPV6_IFADDR groups for |
| 1011 |
// network interface status update messages |
| 1012 |
struct sockaddr_nl localAddr; |
| 1013 |
localAddr.nl_family = AF_NETLINK; |
| 1014 |
localAddr.nl_pid = 0; // system will assign us a unique netlink port id |
| 1015 |
localAddr.nl_groups |= RTMGRP_IPV4_IFADDR | RTMGRP_IPV6_IFADDR;// | RTMGRP_IPV6_IFINFO; |
| 1016 |
if (0 > bind(descriptor, (struct sockaddr*) &localAddr, sizeof(localAddr))) |
| 1017 |
{ |
| 1018 |
PLOG(PL_ERROR, "LinuxNetMonitor::Open() bind() error: %s\n", |
| 1019 |
GetErrorString()); |
| 1020 |
Close(); |
| 1021 |
return false; |
| 1022 |
} |
| 1023 |
if (!ProtoNet::Monitor::Open()) |
| 1024 |
{ |
| 1025 |
Close(); |
| 1026 |
return false; |
| 1027 |
} |
| 1028 |
return true; |
| 1029 |
} // end LinuxNetMonitor::Open() |
| 1030 |
|
| 1031 |
void LinuxNetMonitor::Close() |
| 1032 |
{ |
| 1033 |
if (IsOpen()) |
| 1034 |
{ |
| 1035 |
ProtoNet::Monitor::Close(); |
| 1036 |
close(descriptor); |
| 1037 |
descriptor = INVALID_HANDLE; |
| 1038 |
} |
| 1039 |
event_list.Destroy(); |
| 1040 |
event_pool.Destroy(); |
| 1041 |
} // end LinuxNetMonitor::Close() |
| 1042 |
|
| 1043 |
bool LinuxNetMonitor::GetNextEvent(Event& theEvent) |
| 1044 |
{ |
| 1045 |
// 0) Initialize event instance |
| 1046 |
theEvent.SetType(Event::UNKNOWN_EVENT); |
| 1047 |
theEvent.SetInterfaceIndex(0); |
| 1048 |
theEvent.AccessAddress().Invalidate(); |
| 1049 |
|
| 1050 |
// 1) Get next event from list or recv() from netlink |
| 1051 |
EventItem* eventItem = event_list.RemoveHead(); |
| 1052 |
if (NULL == eventItem) |
| 1053 |
{ |
| 1054 |
// There were not any existing events in our list, so |
| 1055 |
// get more from netlink |
| 1056 |
char buffer[4096]; |
| 1057 |
struct nlmsghdr* nlh = (struct nlmsghdr*)buffer; |
| 1058 |
ssize_t result = recv(descriptor, buffer, 4096, 0); |
| 1059 |
if (result < 0) |
| 1060 |
{ |
| 1061 |
switch(errno) |
| 1062 |
{ |
| 1063 |
case EINTR: |
| 1064 |
case EAGAIN: |
| 1065 |
theEvent.SetType(Event::NULL_EVENT); |
| 1066 |
return true; |
| 1067 |
default: |
| 1068 |
PLOG(PL_ERROR, "BsdNetMonitor::GetNextEvent() recv(PEEK) error: %s", GetErrorString()); |
| 1069 |
return false; |
| 1070 |
} |
| 1071 |
} |
| 1072 |
else if (0 == result) |
| 1073 |
{ |
| 1074 |
theEvent.SetType(Event::NULL_EVENT); |
| 1075 |
return true; |
| 1076 |
} |
| 1077 |
unsigned int msgLen = (unsigned int)result; |
| 1078 |
for (;(NLMSG_OK(nlh, msgLen)) && (nlh->nlmsg_type != NLMSG_DONE); nlh = NLMSG_NEXT(nlh, msgLen)) |
| 1079 |
{ |
| 1080 |
eventItem = NULL; |
| 1081 |
switch (nlh->nlmsg_type) |
| 1082 |
{ |
| 1083 |
case RTM_NEWLINK: |
| 1084 |
case RTM_DELLINK: |
| 1085 |
{ |
| 1086 |
eventItem = event_pool.RemoveHead(); |
| 1087 |
if (NULL == eventItem) eventItem = new EventItem(); |
| 1088 |
if (NULL == eventItem) |
| 1089 |
{ |
| 1090 |
PLOG(PL_ERROR, "LinuxNetMonitor::GetNextEvent() new EventItem error: %s\n", GetErrorString()); |
| 1091 |
theEvent.SetType(Event::NULL_EVENT); |
| 1092 |
return false; |
| 1093 |
} |
| 1094 |
struct ifinfomsg* ifi = (struct ifinfomsg*)NLMSG_DATA(nlh); |
| 1095 |
if (RTM_NEWLINK == nlh->nlmsg_type) |
| 1096 |
{ |
| 1097 |
eventItem->SetType(Event::IFACE_UP); |
| 1098 |
} |
| 1099 |
else |
| 1100 |
{ |
| 1101 |
eventItem->SetType(Event::IFACE_DOWN); |
| 1102 |
} |
| 1103 |
eventItem->SetInterfaceIndex(ifi->ifi_index); |
| 1104 |
// TBD - look through the message RTA's (if any) for the iface name? |
| 1105 |
event_list.Append(*eventItem); |
| 1106 |
break; |
| 1107 |
} |
| 1108 |
case RTM_NEWADDR: |
| 1109 |
case RTM_DELADDR: |
| 1110 |
{ |
| 1111 |
struct ifaddrmsg* ifa = (struct ifaddrmsg*) NLMSG_DATA(nlh); |
| 1112 |
// For now, we only look for IPv4 or Ipv6 addresses (TBD - other addr families?) |
| 1113 |
if ((AF_INET != ifa->ifa_family) && (AF_INET6 != ifa->ifa_family)) continue; |
| 1114 |
struct rtattr* rth = IFA_RTA(ifa); |
| 1115 |
int rtl = IFA_PAYLOAD(nlh); |
| 1116 |
for (;rtl && RTA_OK(rth, rtl); rth = RTA_NEXT(rth,rtl)) |
| 1117 |
{ |
| 1118 |
if (IFA_LOCAL == rth->rta_type) |
| 1119 |
{ |
| 1120 |
eventItem = event_pool.RemoveHead(); |
| 1121 |
if (NULL == eventItem) eventItem = new EventItem(); |
| 1122 |
if (NULL == eventItem) |
| 1123 |
{ |
| 1124 |
PLOG(PL_ERROR, "LinuxNetMonitor::GetNextEvent() new EventItem error: %s\n", GetErrorString()); |
| 1125 |
theEvent.SetType(Event::NULL_EVENT); |
| 1126 |
return false; |
| 1127 |
} |
| 1128 |
if (RTM_NEWADDR == nlh->nlmsg_type) |
| 1129 |
eventItem->SetType(Event::IFACE_ADDR_NEW); |
| 1130 |
else |
| 1131 |
eventItem->SetType(Event::IFACE_ADDR_DELETE); |
| 1132 |
eventItem->SetInterfaceIndex(ifa->ifa_index); |
| 1133 |
if (AF_INET == ifa->ifa_family) |
| 1134 |
eventItem->AccessAddress().SetRawHostAddress(ProtoAddress::IPv4, (char*)RTA_DATA(rth), 4); |
| 1135 |
else //if (AF_INET6 == ifa->ifa_family) |
| 1136 |
eventItem->AccessAddress().SetRawHostAddress(ProtoAddress::IPv6, (char*)RTA_DATA(rth), 16); |
| 1137 |
event_list.Append(*eventItem); |
| 1138 |
} |
| 1139 |
else if (IFA_ADDRESS == rth->rta_type) |
| 1140 |
{ |
| 1141 |
// Note that Linux doesn't seem to reliably (or at all) issue RTM_DELLINK messages |
| 1142 |
// So - as a cheat hack, we're going to issue IFACE_DOWN event when the link local |
| 1143 |
// address is deleted (fingers crossed!) |
| 1144 |
if ((RTM_DELADDR == nlh->nlmsg_type) && |
| 1145 |
(RT_SCOPE_LINK == ifa->ifa_scope)) |
| 1146 |
{ |
| 1147 |
eventItem = event_pool.RemoveHead(); |
| 1148 |
if (NULL == eventItem) eventItem = new EventItem(); |
| 1149 |
if (NULL == eventItem) |
| 1150 |
{ |
| 1151 |
PLOG(PL_ERROR, "LinuxNetMonitor::GetNextEvent() new EventItem error: %s\n", GetErrorString()); |
| 1152 |
theEvent.SetType(Event::NULL_EVENT); |
| 1153 |
return false; |
| 1154 |
} |
| 1155 |
eventItem->SetType(Event::IFACE_DOWN); |
| 1156 |
eventItem->SetInterfaceIndex(ifa->ifa_index); |
| 1157 |
event_list.Append(*eventItem); |
| 1158 |
break; // we break because multiple link down indications aren't need |
| 1159 |
// BUT if we want to link local addr changes here ... |
| 1160 |
} |
| 1161 |
} |
| 1162 |
} |
| 1163 |
break; |
| 1164 |
} |
| 1165 |
default: |
| 1166 |
//TRACE("OTHER message type %d\n", nlh->nlmsg_type); |
| 1167 |
break; |
| 1168 |
} // end switch(nlh->nlmsg_type) |
| 1169 |
|
| 1170 |
if (NULL != eventItem) |
| 1171 |
{ |
| 1172 |
// We enqueued and event, we need to set its interface name if possible |
| 1173 |
// 1) Do we already know this interface? |
| 1174 |
unsigned int ifIndex = eventItem->GetInterfaceIndex(); |
| 1175 |
Interface* iface = iface_list.FindInterface(ifIndex); |
| 1176 |
if ((NULL == iface) || (Event::IFACE_DOWN != eventItem->GetType())) |
| 1177 |
{ |
| 1178 |
// Get the interface name (if it's new or possibly changed |
| 1179 |
char ifName[IFNAMSIZ+1]; |
| 1180 |
ifName[IFNAMSIZ] = '\0'; |
| 1181 |
if (ProtoNet::GetInterfaceName(ifIndex, ifName, IFNAMSIZ)) |
| 1182 |
{ |
| 1183 |
eventItem->SetInterfaceName(ifName); |
| 1184 |
if ((NULL == iface) && (Event::IFACE_DOWN != eventItem->GetType())) |
| 1185 |
{ |
| 1186 |
if (NULL != (iface = new Interface(ifIndex, ifName))) |
| 1187 |
iface_list.Insert(*iface); |
| 1188 |
else |
| 1189 |
PLOG(PL_ERROR, "LinuxNetMonitor::GetNextEvent() new Interface error: %s\n", GetErrorString()); |
| 1190 |
} |
| 1191 |
} |
| 1192 |
else |
| 1193 |
{ |
| 1194 |
if (NULL != iface) eventItem->SetInterfaceName(iface->GetName()); |
| 1195 |
PLOG(PL_ERROR, "LinuxNetMonitor::GetNextEvent() warning: unable to get interface name for index %d\n", ifIndex); |
| 1196 |
} |
| 1197 |
} |
| 1198 |
if ((Event::IFACE_DOWN == eventItem->GetType()) && (NULL != iface)) |
| 1199 |
{ |
| 1200 |
eventItem->SetInterfaceName(iface->GetName()); |
| 1201 |
iface_list.Remove(*iface); |
| 1202 |
delete iface; |
| 1203 |
} |
| 1204 |
} |
| 1205 |
|
| 1206 |
|
| 1207 |
} // end for NLMSG_OK ... |
| 1208 |
eventItem = event_list.RemoveHead(); |
| 1209 |
} // end if (NULL == eventItem) |
| 1210 |
if (NULL == eventItem) |
| 1211 |
{ |
| 1212 |
theEvent.SetType(Event::NULL_EVENT); |
| 1213 |
} |
| 1214 |
else |
| 1215 |
{ |
| 1216 |
theEvent = static_cast<Event&>(*eventItem); |
| 1217 |
event_pool.Append(*eventItem); |
| 1218 |
} |
| 1219 |
return true; |
| 1220 |
} // end LinuxNetMonitor::GetNextEvent() |
| 1221 |
|
| 1222 |
LinuxNetMonitor::EventItem::EventItem() |
| 1223 |
{ |
| 1224 |
} |
| 1225 |
|
| 1226 |
LinuxNetMonitor::EventItem::~EventItem() |
| 1227 |
{ |
| 1228 |
} |