From 36bde12db20c9852b6283ccfa0d49e61ffd52da7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Sun, 12 Oct 2008 23:34:08 +0000 Subject: [PATCH] * DHCP now actually uses the new SO_BINDTODEVICE socket option to make sure the request is sent only on the device it should. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@28017 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/servers/net/DHCPClient.cpp | 21 ++++++++++++++++++++- src/servers/net/NetServer.cpp | 10 ++-------- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/servers/net/DHCPClient.cpp b/src/servers/net/DHCPClient.cpp index c2a8c18f95..441dec468f 100644 --- a/src/servers/net/DHCPClient.cpp +++ b/src/servers/net/DHCPClient.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2006-2007, Haiku, Inc. All Rights Reserved. + * Copyright 2006-2008, Haiku, Inc. All Rights Reserved. * Distributed under the terms of the MIT License. * * Authors: @@ -17,6 +17,7 @@ #include #include #include +#include #include @@ -408,6 +409,24 @@ DHCPClient::_Negotiate(dhcp_state state) int option = 1; setsockopt(socket, SOL_SOCKET, SO_BROADCAST, &option, sizeof(option)); + if (state == INIT) { + // The local interface does not have an address yet, bind the socket + // to the device directly. + int linkSocket = ::socket(AF_LINK, SOCK_DGRAM, 0); + if (linkSocket >= 0) { + // we need to know the index of the device to be able to bind to it + ifreq request; + prepare_request(request, fDevice.String()); + if (ioctl(linkSocket, SIOCGIFINDEX, &request, sizeof(struct ifreq)) + == 0) { + setsockopt(socket, SOL_SOCKET, SO_BINDTODEVICE, + &request.ifr_index, sizeof(int)); + } + + close(linkSocket); + } + } + bigtime_t previousLeaseTime = fLeaseTime; fLeaseTime = 0; fRenewalTime = 0; diff --git a/src/servers/net/NetServer.cpp b/src/servers/net/NetServer.cpp index 71df7323b4..888a44f838 100644 --- a/src/servers/net/NetServer.cpp +++ b/src/servers/net/NetServer.cpp @@ -588,14 +588,8 @@ NetServer::_ConfigureInterface(int socket, BMessage& interface, bool fromMessage request.ifr_route = route; if (autoConfig) { - // add a default route to make the interface accessible, even without an address - if (ioctl(socket, SIOCADDRT, &request, sizeof(request)) < 0) { - fprintf(stderr, "%s: Could not add route for %s: %s\n", - Name(), device, strerror(errno)); - } else { - _QuitLooperForDevice(device); - startAutoConfig = true; - } + _QuitLooperForDevice(device); + startAutoConfig = true; } else if (addressMessage.FindString("gateway", &string) == B_OK && parse_address(familyIndex, string, gateway)) { // add gateway route, if we're asked for it