From 49bffeb3fe6e0b8642c453c81417e5ee4a062375 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Sun, 4 Aug 2013 12:45:53 +0000 Subject: [PATCH] [IP] - Do a better job of selecting an interface to use when none was specified - Fix build svn path=/trunk/; revision=59633 --- reactos/lib/drivers/ip/network/interface.c | 32 +++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/reactos/lib/drivers/ip/network/interface.c b/reactos/lib/drivers/ip/network/interface.c index 0d127ca9bcb..7efb3ac14cb 100644 --- a/reactos/lib/drivers/ip/network/interface.c +++ b/reactos/lib/drivers/ip/network/interface.c @@ -11,6 +11,8 @@ #include "precomp.h" +ULONG NextDefaultAdapter = 0; + NTSTATUS GetInterfaceIPv4Address( PIP_INTERFACE Interface, ULONG TargetType, PULONG Address ) { @@ -143,18 +145,46 @@ BOOLEAN HasPrefix( return TRUE; } -static PIP_INTERFACE GetDefaultInterface(VOID) +PIP_INTERFACE GetDefaultInterface(VOID) { KIRQL OldIrql; + ULONG Index = 0; + IF_LIST_ITER(CurrentIF); TcpipAcquireSpinLock(&InterfaceListLock, &OldIrql); + /* DHCP hack: Always return the adapter without an IP address */ ForEachInterface(CurrentIF) { if (CurrentIF->Context) { + if (AddrIsUnspecified(&CurrentIF->Unicast)) { + TcpipReleaseSpinLock(&InterfaceListLock, OldIrql); + return CurrentIF; + } + } + } EndFor(CurrentIF); + + /* Try to continue from the next adapter */ + ForEachInterface(CurrentIF) { + if (CurrentIF->Context) { + if (Index++ == NextDefaultAdapter) { + NextDefaultAdapter++; + TcpipReleaseSpinLock(&InterfaceListLock, OldIrql); + return CurrentIF; + } + } + } EndFor(CurrentIF); + + /* No luck, so we'll choose the first adapter this time */ + ForEachInterface(CurrentIF) { + if (CurrentIF->Context) { + NextDefaultAdapter = 1; TcpipReleaseSpinLock(&InterfaceListLock, OldIrql); return CurrentIF; } } EndFor(CurrentIF); + + /* Even that didn't work, so we'll just go with loopback */ + NextDefaultAdapter = 0; TcpipReleaseSpinLock(&InterfaceListLock, OldIrql); /* There are no physical interfaces on the system