[haiku-commits] haiku: hrev45534 - src/preferences/network

  • From: stefano.ceccherini@xxxxxxxxx
  • To: haiku-commits@xxxxxxxxxxxxx
  • Date: Sun, 21 Apr 2013 15:48:16 +0200 (CEST)

hrev45534 adds 1 changeset to branch 'master'
old head: bf88d81ea66a93295ec06ec8668c38e28aa49957
new head: be1038406b35253e2b7320597b8f5a0d1e6006a3
overview: http://cgit.haiku-os.org/haiku/log/?qt=range&q=be10384+%5Ebf88d81

----------------------------------------------------------------------------

be10384: Use the network kit api instead of talking to the netstack directly.
  Use BNetworkInterface and BNetworkInterfaceAddress in the Settings class
  instead of using ioctls. This works for everything except the default
  route, for which there is no API yet.

                       [ Stefano Ceccherini <stefano.ceccherini@xxxxxxxxx> ]

----------------------------------------------------------------------------

Revision:    hrev45534
Commit:      be1038406b35253e2b7320597b8f5a0d1e6006a3
URL:         http://cgit.haiku-os.org/haiku/commit/?id=be10384
Author:      Stefano Ceccherini <stefano.ceccherini@xxxxxxxxx>
Date:        Sun Apr 21 13:45:38 2013 UTC

----------------------------------------------------------------------------

2 files changed, 37 insertions(+), 45 deletions(-)
src/preferences/network/Settings.cpp | 81 +++++++++++++++-----------------
src/preferences/network/Settings.h   |  1 -

----------------------------------------------------------------------------

diff --git a/src/preferences/network/Settings.cpp 
b/src/preferences/network/Settings.cpp
index 8601b56..9fcd81c 100644
--- a/src/preferences/network/Settings.cpp
+++ b/src/preferences/network/Settings.cpp
@@ -26,6 +26,7 @@
 #include <driver_settings.h>
 #include <File.h>
 #include <FindDirectory.h>
+#include <NetworkInterface.h>
 #include <Path.h>
 #include <String.h>
 
@@ -38,7 +39,6 @@ Settings::Settings(const char* name)
        fDisabled(false),
        fNameServers(5, true)
 {
-       fSocket = socket(AF_INET, SOCK_DGRAM, 0);
        fName = name;
 
        ReadConfiguration();
@@ -47,7 +47,6 @@ Settings::Settings(const char* name)
 
 Settings::~Settings()
 {
-       close(fSocket);
 }
 
 
@@ -67,61 +66,38 @@ Settings::_PrepareRequest(struct ifreq& request)
 }
 
 
-void
-Settings::ReadConfiguration()
+static status_t
+GetDefaultGateway(BString& gateway)
 {
-       ifreq request;
-       if (!_PrepareRequest(request))
-               return;
-
-       BString text = "dummy";
-       char address[32];
-       sockaddr_in* inetAddress = NULL;
+       // TODO: This method is here because BNetworkInterface
+       // doesn't yet have any route getting methods
 
-       // Obtain IP.
-       if (ioctl(fSocket, SIOCGIFADDR, &request, sizeof(request)) < 0)
-               return;
-
-       inetAddress = (sockaddr_in*)&request.ifr_addr;
-       if (inet_ntop(AF_INET, &inetAddress->sin_addr, address,
-                       sizeof(address)) == NULL) {
-               return;
-       }
+       int socket = ::socket(AF_INET, SOCK_DGRAM, 0);
+       if (socket < 0)
+               return errno;
 
-       fIP = address;
-
-       // Obtain netmask.
-       if (ioctl(fSocket, SIOCGIFNETMASK, &request, sizeof(request)) < 0)
-               return;
-
-       inetAddress = (sockaddr_in*)&request.ifr_mask;
-       if (inet_ntop(AF_INET, &inetAddress->sin_addr, address,
-                       sizeof(address)) == NULL) {
-               return;
-       }
-
-       fNetmask = address;
+       FileDescriptorCloser fdCloser(socket);
 
        // Obtain gateway
        ifconf config;
        config.ifc_len = sizeof(config.ifc_value);
-       if (ioctl(fSocket, SIOCGRTSIZE, &config, sizeof(struct ifconf)) < 0)
-               return;
+       if (ioctl(socket, SIOCGRTSIZE, &config, sizeof(struct ifconf)) < 0)
+               return errno;
 
        uint32 size = (uint32)config.ifc_value;
        if (size == 0)
-               return;
+               return B_ERROR;
 
        void* buffer = malloc(size);
        if (buffer == NULL)
-               return;
+               return B_NO_MEMORY;
 
        MemoryDeleter bufferDeleter(buffer);
        config.ifc_len = size;
        config.ifc_buf = buffer;
 
-       if (ioctl(fSocket, SIOCGRTTABLE, &config, sizeof(struct ifconf)) < 0)
-               return;
+       if (ioctl(socket, SIOCGRTTABLE, &config, sizeof(struct ifconf)) < 0)
+               return errno;
 
        ifreq* interface = (ifreq*)buffer;
        ifreq* end = (ifreq*)((uint8*)buffer + size);
@@ -130,8 +106,8 @@ Settings::ReadConfiguration()
                route_entry& route = interface->ifr_route;
 
                if ((route.flags & RTF_GATEWAY) != 0) {
-                       inetAddress = (sockaddr_in*)route.gateway;
-                       fGateway = inet_ntoa(inetAddress->sin_addr);
+                       sockaddr_in* inetAddress = (sockaddr_in*)route.gateway;
+                       gateway = inet_ntoa(inetAddress->sin_addr);
                }
 
                int32 addressSize = 0;
@@ -146,9 +122,26 @@ Settings::ReadConfiguration()
                        + sizeof(route_entry) + addressSize);
        }
 
-       uint32 flags = 0;
-       if (ioctl(fSocket, SIOCGIFFLAGS, &request, sizeof(struct ifreq)) == 0)
-               flags = request.ifr_flags;
+       return B_OK;
+}
+
+
+void
+Settings::ReadConfiguration()
+{
+       BNetworkInterface interface(fName);
+       BNetworkInterfaceAddress address;
+
+       if (interface.GetAddressAt(0, address) != B_OK)
+               return;
+
+       fIP = address.Address().ToString();
+       fNetmask = address.Mask().ToString();
+
+       if (GetDefaultGateway(fGateway) != B_OK)
+               return;
+
+       uint32 flags = interface.Flags();
 
        fAuto = (flags & (IFF_AUTO_CONFIGURED | IFF_CONFIGURING)) != 0;
        fDisabled = (flags & IFF_UP) == 0;
diff --git a/src/preferences/network/Settings.h 
b/src/preferences/network/Settings.h
index 2f54ca7..d47d730 100644
--- a/src/preferences/network/Settings.h
+++ b/src/preferences/network/Settings.h
@@ -55,7 +55,6 @@ private:
                        BString                         fNetmask;
                        BString                         fName;
                        BString                         fDomain;
-                       int                                     fSocket;
                        bool                            fAuto;
                        bool                            fDisabled;
                        BObjectList<BString> fNameServers;


Other related posts:

  • » [haiku-commits] haiku: hrev45534 - src/preferences/network - stefano . ceccherini