summaryrefslogtreecommitdiff
path: root/src/VBox/Main/linux/NetIf-linux.cpp
diff options
context:
space:
mode:
authorMichael Meskes <meskes@debian.org>2009-07-01 09:47:13 +0200
committerMichael Meskes <meskes@debian.org>2009-07-01 09:47:13 +0200
commit519cfe73fc8bfbb621340d6e1665edf7cc7c42e3 (patch)
treef8fcff39649e098ff27fa02c49815ca9fd6bb300 /src/VBox/Main/linux/NetIf-linux.cpp
parentffd803c5f75d470a7bd301d9fc899703546bedfb (diff)
downloadvirtualbox-519cfe73fc8bfbb621340d6e1665edf7cc7c42e3.tar.gz
Imported Upstream version 3.0.0-dfsgupstream/3.0.0-dfsg
Diffstat (limited to 'src/VBox/Main/linux/NetIf-linux.cpp')
-rw-r--r--src/VBox/Main/linux/NetIf-linux.cpp61
1 files changed, 58 insertions, 3 deletions
diff --git a/src/VBox/Main/linux/NetIf-linux.cpp b/src/VBox/Main/linux/NetIf-linux.cpp
index 8640be492..52c63484a 100644
--- a/src/VBox/Main/linux/NetIf-linux.cpp
+++ b/src/VBox/Main/linux/NetIf-linux.cpp
@@ -1,4 +1,4 @@
-/* $Id: NetIf-linux.cpp $ */
+/* $Id: NetIf-linux.cpp 20481 2009-06-11 19:30:12Z vboxsync $ */
/** @file
* Main - NetIfList, Linux implementation.
*/
@@ -31,6 +31,7 @@
#include <sys/ioctl.h>
#include <net/if.h>
#include <net/if_arp.h>
+#include <net/route.h>
#include <netinet/in.h>
#include <stdio.h>
#include <unistd.h>
@@ -40,6 +41,36 @@
#include "netif.h"
#include "Logging.h"
+static int getDefaultIfaceName(char *pszName)
+{
+ FILE *fp = fopen("/proc/net/route", "r");
+ char szBuf[1024];
+ char szIfName[17];
+ char szAddr[129];
+ char szGateway[129];
+ char szMask[129];
+ int iTmp;
+ int iFlags;
+
+ while (fgets(szBuf, sizeof(szBuf)-1, fp))
+ {
+ int n = sscanf(szBuf, "%16s %128s %128s %X %d %d %d %128s %d %d %d\n",
+ szIfName, szAddr, szGateway, &iFlags, &iTmp, &iTmp, &iTmp,
+ szMask, &iTmp, &iTmp, &iTmp);
+ if (n < 10 || !(iFlags & RTF_UP))
+ continue;
+
+ if (strcmp(szAddr, "00000000") == 0 && strcmp(szMask, "00000000") == 0)
+ {
+ fclose(fp);
+ strncpy(pszName, szIfName, 16);
+ pszName[16] = 0;
+ return VINF_SUCCESS;
+ }
+ }
+ return VERR_INTERNAL_ERROR;
+}
+
static int getInterfaceInfo(int iSocket, const char *pszName, PNETIFINFO pInfo)
{
memset(pInfo, 0, sizeof(*pInfo));
@@ -121,7 +152,13 @@ static int getInterfaceInfo(int iSocket, const char *pszName, PNETIFINFO pInfo)
int NetIfList(std::list <ComObjPtr <HostNetworkInterface> > &list)
{
- int rc = VINF_SUCCESS;
+ char szDefaultIface[256];
+ int rc = getDefaultIfaceName(szDefaultIface);
+ if (RT_FAILURE(rc))
+ {
+ Log(("NetIfList: Failed to find default interface.\n"));
+ szDefaultIface[0] = 0;
+ }
int sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock >= 0)
{
@@ -153,7 +190,12 @@ int NetIfList(std::list <ComObjPtr <HostNetworkInterface> > &list)
enmType = HostNetworkInterfaceType_HostOnly;
if (SUCCEEDED(IfObj->init(Bstr(pszName), enmType, &Info)))
- list.push_back(IfObj);
+ {
+ if (strcmp(pszName, szDefaultIface) == 0)
+ list.push_front(IfObj);
+ else
+ list.push_back(IfObj);
+ }
}
}
@@ -161,6 +203,19 @@ int NetIfList(std::list <ComObjPtr <HostNetworkInterface> > &list)
}
close(sock);
}
+ else
+ rc = VERR_INTERNAL_ERROR;
return rc;
}
+
+int NetIfGetConfigByName(PNETIFINFO pInfo)
+{
+ int rc = VINF_SUCCESS;
+ int sock = socket(AF_INET, SOCK_DGRAM, 0);
+ if (sock < 0)
+ return VERR_NOT_IMPLEMENTED;
+ rc = getInterfaceInfo(sock, pInfo->szShortName, pInfo);
+ close(sock);
+ return rc;
+}