📄 tcptransportimpl.cc
字号:
staticvoid ifaddrs_get_ifinfo(omnivector<const char*>& addrs) { struct ifaddrs *ifa_list; if ( getifaddrs(&ifa_list) < 0 ) { if ( omniORB::trace(1) ) { omniORB::logger log; log << "Warning: getifaddrs() failed.\n" << "Unable to obtain the list of all interface addresses.\n"; } return; } struct ifaddrs *p; for (p = ifa_list; p != 0; p = p->ifa_next) { if (p->ifa_addr && p->ifa_addr->sa_family == AF_INET) { struct sockaddr_in* iaddr = (struct sockaddr_in*)p->ifa_addr; CORBA::String_var s; s = tcpConnection::ip4ToString(iaddr->sin_addr.s_addr); addrs.push_back(s._retn()); } } freeifaddrs(ifa_list); if ( orbParameters::dumpConfiguration || omniORB::trace(20) ) { omniORB::logger log; omnivector<const char*>::iterator i = addrs.begin(); omnivector<const char*>::iterator last = addrs.end(); log << "My addresses are: \n"; while ( i != last ) { log << "omniORB: " << (const char*)(*i) << "\n"; i++; } }}#elif defined(UnixArchitecture) && !defined(__vxWorks__)#ifdef __aix__# define OMNI_SIOCGIFCONF OSIOCGIFCONF#else# define OMNI_SIOCGIFCONF SIOCGIFCONF#endifstaticvoid unix_get_ifinfo(omnivector<const char*>& ifaddrs) { SocketHandle_t sock; sock = socket(INETSOCKET,SOCK_STREAM,0); int lastlen = 0; int len = 100 * sizeof(struct ifreq); struct ifconf ifc; // struct ifconf and ifreq are defined in net/if.h while ( 1 ) { // There is no way to know for sure the buffer is big enough to get // the info for all the interfaces. We work around this by calling // the ioctl 2 times and increases the buffer size in the 2nd call. // If both calls return the info with the same size, we know we have // got all the interfaces. char* buf = (char*) malloc(len); ifc.ifc_len = len; ifc.ifc_buf = buf; if ( ioctl(sock, OMNI_SIOCGIFCONF, &ifc) < 0 ) { if ( errno != EINVAL || lastlen != 0 ) { if ( omniORB::trace(1) ) { omniORB::logger log; log << "Warning: ioctl SIOCGICONF failed.\n" << "Unable to obtain the list of all interface addresses.\n"; } return; } } else { if ( ifc.ifc_len == lastlen ) break; // Success, len has not changed. lastlen = ifc.ifc_len; } len += 10 * sizeof(struct ifreq); free(buf); } close(sock); int total = ifc.ifc_len / sizeof(struct ifreq); struct ifreq* ifr = ifc.ifc_req; for (int i = 0; i < total; i++) { if ( ifr[i].ifr_addr.sa_family == AF_INET ) { struct sockaddr_in* iaddr = (struct sockaddr_in*)&ifr[i].ifr_addr; if ( iaddr->sin_addr.s_addr != 0 ) { CORBA::String_var s; s = tcpConnection::ip4ToString(iaddr->sin_addr.s_addr); ifaddrs.push_back(s._retn()); } } } free(ifc.ifc_buf); if ( orbParameters::dumpConfiguration || omniORB::trace(20) ) { omniORB::logger log; omnivector<const char*>::iterator i = ifaddrs.begin(); omnivector<const char*>::iterator last = ifaddrs.end(); log << "My addresses are: \n"; while ( i != last ) { log << "omniORB: " << (const char*)(*i) << "\n"; i++; } }}/////////////////////////////////////////////////////////////////////////#elif defined(__vxWorks__)void vxworks_get_ifinfo(omnivector<const char*>& ifaddrs) { const int iMAX_ADDRESS_ENTRIES = 50; // Max. number of interface addresses. There is 1 link layer address // (AF_LINK) and at least 1 internet address (more if ifAddrAdd has been // called) per configured network interface const int iMAX_IFREQ_SIZE=36; // ifreq entries have a name field (16 bytes) plus an address field, // AF_LINK addresses (sockaddr_dl = 20 bytes) // AF_INET addresses (sockaddr_in = 16 bytes) // buffer into which to copy retieved configuration char buffer[iMAX_ADDRESS_ENTRIES * iMAX_IFREQ_SIZE]; struct ifconf ifc; // used to retrieve interface configuration struct ifreq *ifr; // interface structure pointer int s; // socket file descriptor int entryLength; // size of ifreq entry char ifreqBuf[iMAX_IFREQ_SIZE]; // buffer for checking flags struct sockaddr_dl *pDataLinkAddr; int offset; // create socket to issue ioctl call if ((s = socket (AF_INET, SOCK_DGRAM,0)) == ERROR) { if (omniORB::trace(1)) { omniORB::logger log; log << "Warning : socket (AF_INET, SOCK_DGRAM,0) failed. " << "Unable to obtain the list of all interface addresses.\n"; } return; } // set up interface request structure array ifc.ifc_len = sizeof (buffer); ifc.ifc_buf = (char *)buffer; // get configuration if (ioctl (s, SIOCGIFCONF, (int)&ifc) < 0) { if (omniORB::trace(1)) { omniORB::logger log; log << "Warning: ioctl SIOCGICONF failed. " << "Unable to obtain the list of all interface addresses.\n"; } close (s); return; } ifr = ifc.ifc_req; // ioctl call changes ifc_len to the actual total bytes copied entryLength = ifc.ifc_len; for (entryLength = ifc.ifc_len; entryLength > 0;) { offset = sizeof (ifr->ifr_name) + sizeof (ifr->ifr_addr); bcopy ((caddr_t)ifr, ifreqBuf, offset); if (ioctl (s, SIOCGIFFLAGS, (int)ifreqBuf) < 0) { if (omniORB::trace(1)) { omniORB::logger log; log << "Warning: ioctl SIOCGIFFLAGS failed. " << "Unable to obtain the list of all interface addresses.\n"; } close (s); return; } if (((struct ifreq *)ifreqBuf)->ifr_flags & IFF_UP) { if (ifr->ifr_addr.sa_family == AF_INET) { // AF_INET entries are of type sockaddr_in = 16 bytes struct sockaddr_in* iaddr = (struct sockaddr_in*) &(ifr->ifr_addr); CORBA::String_var s; s = tcpConnection::ip4ToString(iaddr->sin_addr.s_addr); ifaddrs.push_back(s._retn()); } } // ifreq structures have variable lengths entryLength -= offset; ifr = (struct ifreq *)((char *)ifr + offset); } close (s);}#endif/////////////////////////////////////////////////////////////////////////#if defined(NTArchitecture)#if defined(__ETS_KERNEL__)extern "C" int WSAAPI ETS_WSAIoctl( SOCKET s, DWORD dwIoControlCode, LPVOID lpvInBuffer, DWORD cbInBuffer, LPVOID lpvOutBuffer, DWORD cbOutBuffer, LPDWORD lpcbBytesReturned, LPWSAOVERLAPPED lpOverlapped, LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine);#define WSAIoctl ETS_WSAIoctl#endifstaticvoid win32_get_ifinfo(omnivector<const char*>& ifaddrs) { SocketHandle_t sock; sock = socket(INETSOCKET,SOCK_STREAM,0); INTERFACE_INFO info[64]; // Assume max 64 interfaces DWORD retlen; if ( WSAIoctl(sock, SIO_GET_INTERFACE_LIST, NULL,0, (LPVOID)&info, sizeof(info), (LPDWORD)&retlen, NULL,NULL) == SOCKET_ERROR ) { if ( omniORB::trace(2) ) { omniORB::logger log; int err = WSAGetLastError(); log << "Warning: WSAIoctl SIO_GET_INTERFACE_LIST failed.\n" << "Unable to obtain the list of all interface addresses.\n" << "WSAGetLastError() = " << err << "\n"; } return; } CLOSESOCKET(sock); int numAddresses = retlen / sizeof(INTERFACE_INFO); for (int i = 0; i < numAddresses; i++) { // Only add the address if the interface is running if (info[i].iiFlags & IFF_UP) { if (info[i].iiAddress.Address.sa_family == INETSOCKET) { struct sockaddr_in* iaddr = &info[i].iiAddress.AddressIn; CORBA::String_var s; s = tcpConnection::ip4ToString(iaddr->sin_addr.s_addr); // Just to catch us out, Windows ME apparently sometimes // returns 0.0.0.0 as one of its interfaces... if (omni::strMatch((const char*)s, "0.0.0.0")) continue; ifaddrs.push_back(s._retn()); } } } if ( orbParameters::dumpConfiguration || omniORB::trace(20) ) { omniORB::logger log; omnivector<const char*>::iterator i = ifaddrs.begin(); omnivector<const char*>::iterator last = ifaddrs.end(); log << "My addresses are: \n"; while ( i != last ) { log << "omniORB: " << (const char*)(*i) << "\n"; i++; } }}#endifOMNI_NAMESPACE_END(omni)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -