void EnvironmentImpl::nodeIdImpl(NodeId& id) { std::memset(&id, 0, sizeof(id)); int ifIndex = 0; char ifName[32]; char ifAddr[4]; int s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (s == -1) return; for (;;) { if (ifIndexToIfName(ifIndex, ifName) == OK) { if (ifAddrGet(ifName, ifAddr) == OK) { struct arpreq ar; std::memset(&ar, 0, sizeof(ar)); struct sockaddr_in* pAddr = reinterpret_cast<struct sockaddr_in*>(&ar.arp_pa); pAddr->sin_family = AF_INET; std::memcpy(&pAddr->sin_addr, ifAddr, sizeof(struct in_addr)); int rc = ioctl(s, SIOCGARP, reinterpret_cast<int>(&ar)); if (rc < 0) continue; std::memcpy(&id, ar.arp_ha.sa_data, sizeof(id)); close(s); return; } } else break; } close(s); throw SystemException("cannot get Ethernet hardware address"); }
sw_result sw_ipv4_address_init_from_this_host( sw_ipv4_address * self) { #if defined(__VXWORKS__) sw_int8 current_addr[INET_ADDR_LEN]; if (ifAddrGet(sysEnetNameGet(), current_addr) != OK) { sw_debug(SW_LOG_ERROR, "sw_ipv4_address_init_from_this_host", "ifAddrGet() failed\n"); return SW_E_INIT; } self->m_addr = inet_addr(current_addr); #else struct sockaddr_in addr; sw_result err; sw_sockdesc_t desc; sw_socklen_t len; desc = socket(AF_INET, SOCK_DGRAM, 0); sw_check(desc != SW_INVALID_SOCKET, exit, err = SW_E_UNKNOWN); sw_memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_addr.s_addr = inet_addr("192.168.1.1"); addr.sin_port = htons(5555); err = connect(desc, (struct sockaddr*) &addr, sizeof(addr)); sw_check_okay(err, exit); sw_memset(&addr, 0, sizeof(addr)); len = sizeof(addr); err = getsockname(desc, (struct sockaddr*) &addr, &len); sw_check_okay(err, exit); self->m_addr = addr.sin_addr.s_addr; #endif exit: if (desc != SW_INVALID_SOCKET) { sw_close_socket(desc); } if (err != SW_OKAY) { err = sw_ipv4_address_init_from_address(self, sw_ipv4_address_loopback()); } return err; }
void OsSocket::getHostIp(UtlString* hostAddress) { socketInit(); UtlString thisHost; #ifdef _VXWORKS /* [ */ char ipAddr[100]; ipAddr[0] = '\0'; hostAddress->remove(0); if(!ifAddrGet("csp0", ipAddr)) hostAddress->append(ipAddr); #elif defined(__pingtel_on_posix__) /* ] [ */ #ifdef __linux__ char ipAddr[100]; unsigned int ip_int = ntohl(getExternalHostAddressLinux()); sprintf(ipAddr, "%d.%d.%d.%d", ip_int >> 24, (ip_int >> 16) & 255, (ip_int >> 8) & 255, ip_int & 255); hostAddress->remove(0); hostAddress->append(ipAddr); #else getHostName(&thisHost); getHostIpByName(thisHost.data(), hostAddress); #endif /* __linux__ */ #elif defined(_WIN32) /* ] [ */ unsigned long address_val = OsSocket::getDefaultBindAddress(); if (address_val == htonl(INADDR_ANY)) { getHostName(&thisHost); getHostIpByName(thisHost.data(), hostAddress); } else { struct in_addr in; char tmp[50]; in.S_un.S_addr = address_val; strcpy(tmp,inet_ntoa(in)); *hostAddress = tmp; } #else /* ] [ */ #error Unsupported target platform. #endif /* ] */ thisHost.remove(0); }
STATUS wdbEndPktDevInit ( WDB_END_PKT_DEV * pPktDev, /* device structure to init */ void (*stackRcv) (), /* receive packet callback (udpRcv) */ char * pDevice, /* Device (ln, ie, etc.) that we */ /* wish to bind to. */ int unit /* unit number (0, 1, etc.) */ ) { END_OBJ * pEnd; char ifname [20]; char inetAdrs [24]; /* initialize the wdbDrvIf field with driver info */ pPktDev->wdbDrvIf.mode = WDB_COMM_MODE_POLL| WDB_COMM_MODE_INT; pPktDev->wdbDrvIf.mtu = WDB_END_PKT_MTU; pPktDev->wdbDrvIf.stackRcv = stackRcv; /* udpRcv */ pPktDev->wdbDrvIf.devId = (WDB_END_PKT_DEV *)pPktDev; pPktDev->wdbDrvIf.pollRtn = wdbEndPoll; pPktDev->wdbDrvIf.pktTxRtn = wdbEndTx; pPktDev->wdbDrvIf.modeSetRtn = wdbEndModeSet; /* initialize the device specific fields in the driver structure */ pPktDev->inputBusy = FALSE; pPktDev->outputBusy = FALSE; #ifndef STANDALONE_AGENT /* * Here is where we bind to the lower layer. * We do not, as yet, provide for a shutdown routine, but perhaps * later. * We are a promiscous protocol. * The Int routine a fakeout. Interrupts are handled by the lower * layer but we use a similar mechanism to check packets for * the proper type and hand them off to the WDB agent from * the "interrupt" routine if it's appropriate to do so. */ #ifdef WDB_NPT_CAPABLE if (muxTkDrvCheck (pDevice) == TRUE) { if ((pPktDev->pCookie = muxTkBind (pDevice, unit, wdbNptInt, (FUNCPTR)wdbNptShutdown, NULL, NULL, MUX_PROTO_SNARF, "Wind Debug Agent", NULL, NULL, NULL)) == NULL) { if (wdbEndDebug) logMsg ("Could not bind to NPT Device %s, loading...\n", (int)pDevice, 2, 3, 4, 5, 6); return (ERROR); } } else /* END */ { if (wdbEndDebug) logMsg ("Not a NPT device! %s\n", (int)pDevice,2,3,4,5,6); #endif /* WDB_NPT_CAPABLE */ if ((pPktDev->pCookie = muxBind (pDevice, unit, wdbEndInt, NULL, NULL, NULL, MUX_PROTO_SNARF, "Wind Debug Agent", NULL)) == NULL) { if (wdbEndDebug) logMsg ("Could not bind to %s, loading...\n", (int)pDevice, 2, 3, 4, 5, 6); return (ERROR); } #ifdef WDB_NPT_CAPABLE } #endif /* WDB_NPT_CAPABLE */ pEnd = PCOOKIE_TO_ENDOBJ(pPktDev->pCookie); #else /* STANDALONE_AGENT */ /* * for standalone agent, we simply need to get the address of the * of the device. */ if ((pPktDev->pCookie = endFindByName (pDevice, unit)) == NULL) return (ERROR); #endif /* STANDALONE_AGENT */ /* build interface name */ sprintf (ifname, "%s%d", pDevice, unit); /* get interface inet address */ if (ifAddrGet (ifname, inetAdrs) != OK) { if (wdbEndDebug) logMsg ("Could not get inet address of %s interface...\n", (int) ifname, 2, 3, 4, 5, 6); return (ERROR); } pPktDev->ipAddr.s_addr = inet_addr (inetAdrs); pEnd = PCOOKIE_TO_ENDOBJ(pPktDev->pCookie); if ((pInPkt = memalign (4,pEnd->mib2Tbl.ifMtu)) == NULL) return (ERROR); if ((pPktDev->pInBlk = wdbEndMblkClGet (pEnd, pEnd->mib2Tbl.ifMtu)) == NULL) return (ERROR); pPktDev->pInBlk->mBlkHdr.mFlags |= M_PKTHDR; if ((pPktDev->pOutBlk = wdbEndMblkClGet (pEnd, pEnd->mib2Tbl.ifMtu)) == NULL) return (ERROR); pPktDev->pOutBlk->mBlkHdr.mFlags |= M_PKTHDR; if ((pPktDev->lastHAddr = wdbEndMblkClGet (pEnd, pEnd->mib2Tbl.ifMtu)) == NULL) return (ERROR); if ((pPktDev->srcAddr = wdbEndMblkClGet (pEnd, pEnd->mib2Tbl.ifMtu)) == NULL) return (ERROR); /* Set the length to the size of the buffer just allocated. */ pPktDev->pInBlk->mBlkHdr.mLen = pPktDev->pInBlk->pClBlk->clSize; pPktDev->lastHAddr->mBlkHdr.mLen = pEnd->mib2Tbl.ifPhysAddress.addrLength; memset (pPktDev->lastHAddr->mBlkHdr.mData, 0xff, pPktDev->lastHAddr->mBlkHdr.mLen); /* * Create a source address structure so we can send fully * qualified packets. */ muxIoctl (pPktDev->pCookie, EIOCGADDR, pPktDev->srcAddr->mBlkHdr.mData); pEndPktDev = pPktDev; return (OK); }