Changeset 15936 in vbox for trunk/src/VBox/Main
- Timestamp:
- Jan 14, 2009 12:37:54 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Main/linux/NetIfList-linux.cpp
r15658 r15936 41 41 #include "Logging.h" 42 42 43 int NetIfList(std::list <ComObjPtr <HostNetworkInterface> > &list)43 static int getInterfaceInfo(int iSocket, const char *pszName, PNETIFINFO pInfo) 44 44 { 45 int sock = socket(AF_INET, SOCK_DGRAM, 0); 46 if (sock >= 0) 45 memset(pInfo, 0, sizeof(*pInfo)); 46 struct ifreq Req; 47 memset(&Req, 0, sizeof(Req)); 48 strncpy(Req.ifr_name, pszName, sizeof(Req.ifr_name) - 1); 49 if (ioctl(iSocket, SIOCGIFHWADDR, &Req) >= 0) 47 50 { 48 char pBuffer[2048]; 49 struct ifconf ifConf; 50 ifConf.ifc_len = sizeof(pBuffer); 51 ifConf.ifc_buf = pBuffer; 52 if (ioctl(sock, SIOCGIFCONF, &ifConf) >= 0) 51 switch (Req.ifr_hwaddr.sa_family) 53 52 { 54 for (struct ifreq *pReq = ifConf.ifc_req; (char*)pReq < pBuffer + ifConf.ifc_len; pReq++) 53 case ARPHRD_ETHER: 54 pInfo->enmType = NETIF_T_ETHERNET; 55 break; 56 default: 57 pInfo->enmType = NETIF_T_UNKNOWN; 58 break; 59 } 60 /* Pick up some garbage from stack. */ 61 RTUUID uuid; 62 Assert(sizeof(uuid) <= sizeof(Req)); 63 memcpy(uuid.Gen.au8Node, &Req.ifr_hwaddr.sa_data, sizeof(uuid.Gen.au8Node)); 64 pInfo->Uuid = uuid; 65 memcpy(&pInfo->MACAddress, Req.ifr_hwaddr.sa_data, sizeof(pInfo->MACAddress)); 66 67 if (ioctl(iSocket, SIOCGIFADDR, &Req) >= 0) 68 memcpy(pInfo->IPAddress.au8, 69 &((struct sockaddr_in *)&Req.ifr_addr)->sin_addr.s_addr, 70 sizeof(pInfo->IPAddress.au8)); 71 72 if (ioctl(iSocket, SIOCGIFNETMASK, &Req) >= 0) 73 memcpy(pInfo->IPNetMask.au8, 74 &((struct sockaddr_in *)&Req.ifr_addr)->sin_addr.s_addr, 75 sizeof(pInfo->IPNetMask.au8)); 76 77 if (ioctl(iSocket, SIOCGIFFLAGS, &Req) >= 0) 78 pInfo->enmStatus = Req.ifr_flags & IFF_UP ? NETIF_S_UP : NETIF_S_DOWN; 79 80 FILE *fp = fopen("/proc/net/if_inet6", "r"); 81 if (fp) 82 { 83 RTNETADDRIPV6 IPv6Address; 84 unsigned uIndex, uLength, uScope, uTmp; 85 char szName[30]; 86 for (;;) 55 87 { 56 if (ioctl(sock, SIOCGIFHWADDR, pReq) >= 0) 88 memset(szName, 0, sizeof(szName)); 89 int n = fscanf(fp, 90 "%08x%08x%08x%08x" 91 " %02x %02x %02x %02x %20s\n", 92 &IPv6Address.au32[0], &IPv6Address.au32[1], 93 &IPv6Address.au32[2], &IPv6Address.au32[3], 94 &uIndex, &uLength, &uScope, &uTmp, szName); 95 if (n == EOF) 96 break; 97 if (n != 9 || uLength > 128) 57 98 { 58 if (pReq->ifr_hwaddr.sa_family == ARPHRD_ETHER) 59 { 60 NETIFINFO Info; 61 memset(&Info, 0, sizeof(Info)); 62 Info.enmType = NETIF_T_ETHERNET; 63 /* Pick up some garbage from stack. */ 64 RTUUID uuid; 65 Assert(sizeof(uuid) <= sizeof(*pReq)); 66 memcpy(uuid.Gen.au8Node, &pReq->ifr_hwaddr.sa_data, sizeof(uuid.Gen.au8Node)); 67 Info.Uuid = uuid; 68 memcpy(&Info.MACAddress, pReq->ifr_hwaddr.sa_data, sizeof(Info.MACAddress)); 69 70 if (ioctl(sock, SIOCGIFADDR, pReq) >= 0) 71 memcpy(Info.IPAddress.au8, 72 &((struct sockaddr_in *)&pReq->ifr_addr)->sin_addr.s_addr, 73 sizeof(Info.IPAddress.au8)); 74 75 if (ioctl(sock, SIOCGIFNETMASK, pReq) >= 0) 76 memcpy(Info.IPNetMask.au8, 77 &((struct sockaddr_in *)&pReq->ifr_addr)->sin_addr.s_addr, 78 sizeof(Info.IPNetMask.au8)); 79 80 if (ioctl(sock, SIOCGIFFLAGS, pReq) >= 0) 81 Info.enmStatus = pReq->ifr_flags & IFF_UP ? NETIF_S_UP : NETIF_S_DOWN; 82 83 FILE *fp = fopen("/proc/net/if_inet6", "r"); 84 if (fp) 85 { 86 RTNETADDRIPV6 IPv6Address; 87 unsigned uIndex, uLength, uScope, uTmp; 88 char szName[30]; 89 for (;;) 90 { 91 memset(szName, 0, sizeof(szName)); 92 int n = fscanf(fp, 93 "%08x%08x%08x%08x" 94 " %02x %02x %02x %02x %20s\n", 95 &IPv6Address.au32[0], &IPv6Address.au32[1], 96 &IPv6Address.au32[2], &IPv6Address.au32[3], 97 &uIndex, &uLength, &uScope, &uTmp, szName); 98 if (n == EOF) 99 break; 100 if (n != 9 || uLength > 128) 101 { 102 Log(("NetIfList: Error while reading /proc/net/if_inet6, n=%d uLength=%u\n", 103 n, uLength)); 104 break; 105 } 106 if (!strcmp(pReq->ifr_name, szName)) 107 { 108 Info.IPv6Address.au32[0] = htonl(IPv6Address.au32[0]); 109 Info.IPv6Address.au32[1] = htonl(IPv6Address.au32[1]); 110 Info.IPv6Address.au32[2] = htonl(IPv6Address.au32[2]); 111 Info.IPv6Address.au32[3] = htonl(IPv6Address.au32[3]); 112 ASMBitSetRange(&Info.IPv6NetMask, 0, uLength); 113 } 114 } 115 fclose(fp); 116 } 117 ComObjPtr<HostNetworkInterface> IfObj; 118 IfObj.createObject(); 119 if (SUCCEEDED(IfObj->init(Bstr(pReq->ifr_name), &Info))) 120 list.push_back(IfObj); 121 } 99 Log(("getInterfaceInfo: Error while reading /proc/net/if_inet6, n=%d uLength=%u\n", 100 n, uLength)); 101 break; 102 } 103 if (!strcmp(Req.ifr_name, szName)) 104 { 105 pInfo->IPv6Address.au32[0] = htonl(IPv6Address.au32[0]); 106 pInfo->IPv6Address.au32[1] = htonl(IPv6Address.au32[1]); 107 pInfo->IPv6Address.au32[2] = htonl(IPv6Address.au32[2]); 108 pInfo->IPv6Address.au32[3] = htonl(IPv6Address.au32[3]); 109 ASMBitSetRange(&pInfo->IPv6NetMask, 0, uLength); 122 110 } 123 111 } 112 fclose(fp); 124 113 } 125 close(sock);126 114 } 127 115 return VINF_SUCCESS; 128 116 } 129 117 118 int NetIfList(std::list <ComObjPtr <HostNetworkInterface> > &list) 119 { 120 int rc = VINF_SUCCESS; 121 int sock = socket(AF_INET, SOCK_DGRAM, 0); 122 if (sock >= 0) 123 { 124 FILE *fp = fopen("/proc/net/dev", "r"); 125 if (fp) 126 { 127 char buf[256]; 128 while (fgets(buf, sizeof(buf), fp)) 129 { 130 char *pszEndOfName = strchr(buf, ':'); 131 if (!pszEndOfName) 132 continue; 133 *pszEndOfName = 0; 134 int iFirstNonWS = strspn(buf, " "); 135 char *pszName = buf+iFirstNonWS; 136 NETIFINFO Info; 137 rc = getInterfaceInfo(sock, pszName, &Info); 138 if (RT_FAILURE(rc)) 139 break; 140 if (Info.enmType == NETIF_T_ETHERNET) 141 { 142 ComObjPtr<HostNetworkInterface> IfObj; 143 IfObj.createObject(); 144 if (SUCCEEDED(IfObj->init(Bstr(pszName), &Info))) 145 list.push_back(IfObj); 146 } 147 148 } 149 fclose(fp); 150 } 151 close(sock); 152 } 153 154 return rc; 155 } 156 157
Note:
See TracChangeset
for help on using the changeset viewer.