Changeset 19617 in vbox for trunk/src/VBox/Additions
- Timestamp:
- May 12, 2009 12:47:23 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Additions/common/VBoxService/VBoxServiceVMInfo.cpp
r19514 r19617 31 31 #include <Ntsecapi.h> 32 32 #else 33 #define __STDC_LIMIT_MACROS 34 # include <arpa/inet.h> 33 35 # include <errno.h> 36 # include <net/if.h> 37 # include <netinet/in.h> 38 # include <sys/ioctl.h> 39 # include <sys/socket.h> 34 40 # include <unistd.h> 35 41 # include <utmp.h> … … 234 240 && (strstr(szUserList, ut_user->ut_user) == NULL)) 235 241 { 242 /** @todo Do we really want to filter out double user names? (Same user logged in twice) */ 236 243 if (uiUserCount > 0) 237 244 strcat(szUserList, ","); … … 260 267 /* Get network configuration. */ 261 268 /** @todo Throw this code into a separate function/module? */ 269 int nNumInterfaces = 0; 262 270 #ifdef RT_OS_WINDOWS 263 271 SOCKET sd = WSASocket(AF_INET, SOCK_DGRAM, 0, 0, 0, 0); 264 if (sd == SOCKET_ERROR) 272 if (sd == SOCKET_ERROR) /* Socket invalid. */ 265 273 { 266 274 VBoxServiceError("Failed to get a socket: Error %d\n", WSAGetLastError()); … … 280 288 0) == SOCKET_ERROR) 281 289 { 282 VBoxServiceError("Failed calling WSAIoctl: Error: %d\n", WSAGetLastError());290 VBoxServiceError("Failed to WSAIoctl() on socket: Error: %d\n", WSAGetLastError()); 283 291 return -1; 284 292 } 285 286 char szPropPath [_MAX_PATH] = {0}; 287 char szTemp [_MAX_PATH] = {0}; 288 int nNumInterfaces = nBytesReturned / sizeof(INTERFACE_INFO); 293 nNumInterfaces = nBytesReturned / sizeof(INTERFACE_INFO); 294 #else 295 int sd = socket(AF_INET, SOCK_DGRAM, 0); 296 if (sd < 0) /* Socket invalid. */ 297 { 298 VBoxServiceError("Failed to get a socket: Error %d\n", errno); 299 return -1; 300 } 301 302 ifconf ifcfg; 303 char buffer[1024] = {0}; 304 ifcfg.ifc_len = sizeof(buffer); 305 ifcfg.ifc_buf = buffer; 306 if (ioctl(sd, SIOCGIFCONF, &ifcfg) < 0) 307 { 308 VBoxServiceError("Failed to ioctl(SIOCGIFCONF) on socket: Error %d\n", errno); 309 return -1; 310 } 311 312 ifreq* ifrequest = ifcfg.ifc_req; 313 ifreq *ifreqitem = NULL; 314 nNumInterfaces = ifcfg.ifc_len / sizeof(ifreq); 315 #endif 316 char szPropPath [FILENAME_MAX] = {0}; 317 char szTemp [FILENAME_MAX] = {0}; 289 318 int iCurIface = 0; 290 319 … … 293 322 294 323 /** @todo Use GetAdaptersInfo() and GetAdapterAddresses (IPv4 + IPv6) for more information. */ 295 296 324 for (int i = 0; i < nNumInterfaces; ++i) 297 325 { 298 if (InterfaceList[i].iiFlags & IFF_LOOPBACK) /* Skip loopback device. */ 326 sockaddr_in *pAddress; 327 u_long nFlags = 0; 328 #ifdef RT_OS_WINDOWS 329 if (InterfaceList[i].iiFlags & IFF_LOOPBACK) /* Skip loopback device. */ 299 330 continue; 300 301 sockaddr_in *pAddress; 302 pAddress = (sockaddr_in *) & (InterfaceList[i].iiAddress); 331 nFlags = InterfaceList[i].iiFlags; 332 pAddress = (sockaddr_in *)&(InterfaceList[i].iiAddress); 333 #else 334 if (ioctl(sd, SIOCGIFFLAGS, &ifrequest[i]) < 0) 335 { 336 VBoxServiceError("Failed to ioctl(SIOCGIFFLAGS) on socket: Error %d\n", errno); 337 return -1; 338 } 339 if (ifrequest[i].ifr_flags & IFF_LOOPBACK) /* Skip loopback device. */ 340 continue; 341 nFlags = ifrequest[i].ifr_flags; 342 pAddress = ((sockaddr_in *)&ifrequest[i].ifr_addr); 343 #endif 344 Assert(pAddress); 303 345 RTStrPrintf(szPropPath, sizeof(szPropPath), "GuestInfo/Net/%d/V4/IP", iCurIface); 304 346 VboxServiceWriteProp(g_VMInfoGuestPropSvcClientID, szPropPath, inet_ntoa(pAddress->sin_addr)); 305 347 348 #ifdef RT_OS_WINDOWS 306 349 pAddress = (sockaddr_in *) & (InterfaceList[i].iiBroadcastAddress); 350 #else 351 if (ioctl(sd, SIOCGIFBRDADDR, &ifrequest[i]) < 0) 352 { 353 VBoxServiceError("Failed to ioctl(SIOCGIFBRDADDR) on socket: Error %d\n", errno); 354 return -1; 355 } 356 pAddress = (sockaddr_in *)&ifrequest[i].ifr_broadaddr; 357 #endif 307 358 RTStrPrintf(szPropPath, sizeof(szPropPath), "GuestInfo/Net/%d/V4/Broadcast", iCurIface); 308 359 VboxServiceWriteProp(g_VMInfoGuestPropSvcClientID, szPropPath, inet_ntoa(pAddress->sin_addr)); 309 360 310 pAddress = (sockaddr_in *) & (InterfaceList[i].iiNetmask); 361 #ifdef RT_OS_WINDOWS 362 pAddress = (sockaddr_in *)&(InterfaceList[i].iiNetmask); 363 #else 364 if (ioctl(sd, SIOCGIFNETMASK, &ifrequest[i]) < 0) 365 { 366 VBoxServiceError("Failed to ioctl(SIOCGIFBRDADDR) on socket: Error %d\n", errno); 367 return -1; 368 } 369 pAddress = (sockaddr_in *)&ifrequest[i].ifr_netmask; 370 #endif 311 371 RTStrPrintf(szPropPath, sizeof(szPropPath), "GuestInfo/Net/%d/V4/Netmask", iCurIface); 312 372 VboxServiceWriteProp(g_VMInfoGuestPropSvcClientID, szPropPath, inet_ntoa(pAddress->sin_addr)); 313 314 u_long nFlags = InterfaceList[i].iiFlags;315 if (nFlags & IFF_UP)373 374 375 if (nFlags & IFF_UP) 316 376 RTStrPrintf(szTemp, sizeof(szTemp), "Up"); 317 377 else … … 323 383 iCurIface++; 324 384 } 325 326 closesocket(sd); 385 #ifdef RT_OS_WINDOWS 386 if (sd) closesocket(sd); 387 #else 388 if (sd) close(sd); 327 389 #endif /* !RT_OS_WINDOWS */ 328 390
Note:
See TracChangeset
for help on using the changeset viewer.