本文整理汇总了C++中EthernetInterface::addAddress方法的典型用法代码示例。如果您正苦于以下问题:C++ EthernetInterface::addAddress方法的具体用法?C++ EthernetInterface::addAddress怎么用?C++ EthernetInterface::addAddress使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类EthernetInterface
的用法示例。
在下文中一共展示了EthernetInterface::addAddress方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: getLocalInterfaceList
int getLocalInterfaceList(InterfaceRefList& iflist, const bool onlyUp)
{
IP_ADAPTER_ADDRESSES *ipAA, *ipAA_head;
DWORD flags = GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER | GAA_FLAG_INCLUDE_PREFIX;
ULONG bufSize;
int num = 0;
// Figure out required buffer size
DWORD iReturn = GetAdaptersAddresses(AF_UNSPEC, flags, NULL, NULL, &bufSize);
if (iReturn != ERROR_BUFFER_OVERFLOW) {
return -1;
}
// Allocate the required buffer
ipAA_head = (IP_ADAPTER_ADDRESSES *)malloc(bufSize);
if (!ipAA_head) {
HAGGLE_ERR("Could not allocate IP_ADAPTER_ADDRESSES buffer\n");
return -1;
}
// Now, get the information for real
iReturn = GetAdaptersAddresses(AF_UNSPEC, flags, NULL, ipAA_head, &bufSize);
switch (iReturn) {
case ERROR_SUCCESS:
break;
case ERROR_NO_DATA:
case ERROR_BUFFER_OVERFLOW:
case ERROR_INVALID_PARAMETER:
default:
HAGGLE_ERR("ERROR: %s\n", STRERROR(ERRNO));
free(ipAA_head);
return -1;
}
// Loop through all the adapters and their addresses
for (ipAA = ipAA_head; ipAA; ipAA = ipAA->Next) {
// Ignore interfaces that are not Ethernet or 802.11
if (ipAA->IfType != IF_TYPE_IEEE80211 &&
ipAA->IfType != IF_TYPE_FASTETHER &&
ipAA->IfType != IF_TYPE_GIGABITETHERNET &&
ipAA->IfType != IF_TYPE_ETHERNET_CSMACD) {
//HAGGLE_DBG("Skipping interface of wrong type = %d\n", ipAA->IfType);
continue;
}
// Ignore non operational interfaces
if (onlyUp && ipAA->OperStatus != IfOperStatusUp) {
//HAGGLE_DBG("Skipping non operational interface status=%d\n", ipAA->OperStatus);
continue;
}
if (ipAA->Mtu > 1500) {
// Some weird MTU. Some discovered "Ethernet" interfaces have a really high MTU.
// These are probably virtual serial line interfaces enabled when synching the phone
// over, e.g., a USB cable.
continue;
}
// Ok, this interface seems to be interesting
unsigned char macaddr[ETH_MAC_LEN];
memcpy(macaddr, ipAA->PhysicalAddress, ETH_MAC_LEN);
EthernetAddress addr(macaddr);
EthernetInterface *ethIface = new EthernetInterface(macaddr,
ipAA->AdapterName, &addr,
IFFLAG_LOCAL | ((ipAA->OperStatus == IfOperStatusUp) ? IFFLAG_UP : 0));
if (!ethIface)
continue;
/*
HAGGLE_DBG("LOCAL Interface type=%d index=%d name=%s mtu=%d mac=%s\n",
ipAA->IfType, ipAA->IfIndex, ipAA->AdapterName, ipAA->Mtu, mac.getAddrStr());
*/
IP_ADAPTER_UNICAST_ADDRESS *ipAUA;
IP_ADAPTER_PREFIX *ipAP;
for (ipAUA = ipAA->FirstUnicastAddress, ipAP = ipAA->FirstPrefix; ipAUA && ipAP; ipAUA = ipAUA->Next, ipAP = ipAP->Next) {
if (ipAUA->Address.lpSockaddr->sa_family == AF_INET) {
struct in_addr bc;
struct sockaddr_in *saddr_v4 = (struct sockaddr_in *)ipAUA->Address.lpSockaddr;
DWORD mask = 0xFFFFFFFF;
memcpy(&bc, &saddr_v4->sin_addr, sizeof(bc));
// Create mask
mask >>= (32 - ipAP->PrefixLength);
// Create broadcast address
bc.S_un.S_addr = saddr_v4->sin_addr.S_un.S_addr | ~mask;
IPv4Address ipv4(*saddr_v4);
ethIface->addAddress(ipv4);
IPv4BroadcastAddress ipv4bc(bc);
//.........这里部分代码省略.........