[haiku-commits] r33996 - haiku/trunk/src/preferences/network

  • From: axeld@xxxxxxxxxxxxxxxx
  • To: haiku-commits@xxxxxxxxxxxxx
  • Date: Wed, 11 Nov 2009 10:06:59 +0100 (CET)

Author: axeld
Date: 2009-11-11 10:06:59 +0100 (Wed, 11 Nov 2009)
New Revision: 33996
Changeset: http://dev.haiku-os.org/changeset/33996/haiku

Added:
   haiku/trunk/src/preferences/network/Settings.cpp
   haiku/trunk/src/preferences/network/Settings.h
Removed:
   haiku/trunk/src/preferences/network/settings.cpp
   haiku/trunk/src/preferences/network/settings.h
Modified:
   haiku/trunk/src/preferences/network/EthernetSettingsView.cpp
   haiku/trunk/src/preferences/network/EthernetSettingsView.h
   haiku/trunk/src/preferences/network/Jamfile
Log:
* Renamed settings.{cpp|h} to Settings.*.
* Got rid of some superfluous Get*() in method names.


Modified: haiku/trunk/src/preferences/network/EthernetSettingsView.cpp
===================================================================
--- haiku/trunk/src/preferences/network/EthernetSettingsView.cpp        
2009-11-11 09:03:28 UTC (rev 33995)
+++ haiku/trunk/src/preferences/network/EthernetSettingsView.cpp        
2009-11-11 09:06:59 UTC (rev 33996)
@@ -12,7 +12,7 @@
 
 
 #include "EthernetSettingsView.h"
-#include "settings.h"
+#include "Settings.h"
 
 #include <Application.h>
 #include <Alert.h>
@@ -295,13 +295,13 @@
 
        if (settings) {
                BMenuItem* item = fDeviceMenuField->Menu()->FindItem(
-                       settings->GetName());
+                       settings->Name());
                if (item)
                        item->SetMarked(true);
 
-               fIPTextControl->SetText(settings->GetIP());
-               fGatewayTextControl->SetText(settings->GetGateway());
-               fNetMaskTextControl->SetText(settings->GetNetmask());
+               fIPTextControl->SetText(settings->IP());
+               fGatewayTextControl->SetText(settings->Gateway());
+               fNetMaskTextControl->SetText(settings->Netmask());
 
                if (settings->AutoConfigure() == true)
                        item = fTypeMenuField->Menu()->FindItem("DHCP");
@@ -369,7 +369,7 @@
        _SaveDNSConfiguration();
        _SaveAdaptersConfiguration();
        if (fCurrentSettings->AutoConfigure())
-               _TriggerAutoConfig(fCurrentSettings->GetName());
+               _TriggerAutoConfig(fCurrentSettings->Name());
 }
 
 
@@ -431,14 +431,14 @@
                }
 
                fprintf(fp, "interface %s {\n\t\taddress {\n",
-                       fSettings.ItemAt(i)->GetName());
+                       fSettings.ItemAt(i)->Name());
                fprintf(fp, "\t\t\tfamily\tinet\n");
                fprintf(fp, "\t\t\taddress\t%s\n",
-                       fSettings.ItemAt(i)->GetIP());
+                       fSettings.ItemAt(i)->IP());
                fprintf(fp, "\t\t\tgateway\t%s\n",
-                       fSettings.ItemAt(i)->GetGateway());
+                       fSettings.ItemAt(i)->Gateway());
                fprintf(fp, "\t\t\tmask\t%s\n",
-                       fSettings.ItemAt(i)->GetNetmask());
+                       fSettings.ItemAt(i)->Netmask());
                fprintf(fp, "\t\t}\n}\n\n");
        }
        if (fp) {
@@ -550,7 +550,7 @@
                                break;
                        for (int32 i = 0; i < fSettings.CountItems(); i++) {
                                Settings* settings = fSettings.ItemAt(i);
-                               if (strcmp(settings->GetName(), name) == 0) {
+                               if (strcmp(settings->Name(), name) == 0) {
                                        _ShowConfiguration(settings);
                                        break;
                                }

Modified: haiku/trunk/src/preferences/network/EthernetSettingsView.h
===================================================================
--- haiku/trunk/src/preferences/network/EthernetSettingsView.h  2009-11-11 
09:03:28 UTC (rev 33995)
+++ haiku/trunk/src/preferences/network/EthernetSettingsView.h  2009-11-11 
09:06:59 UTC (rev 33996)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2004-2007 Haiku Inc. All rights reserved.
+ * Copyright 2004-2009 Haiku Inc. All rights reserved.
  * Distributed under the terms of the MIT License.
  *
  * Author:
@@ -11,29 +11,29 @@
 #define ETHERNET_SETTINGS_VIEW_H
 
 
-#include "settings.h"
-
 #include <ObjectList.h>
 #include <View.h>
 
 #include <posix/regex.h>
 
+
 class BButton;
 class BMenuField;
 class BPath;
 class BTextControl;
 class BStringView;
+class Settings;
 
 
 class EthernetSettingsView : public BView {
 public:
                                                                
EthernetSettingsView();
                virtual                                 ~EthernetSettingsView();
-               
+
                virtual void                    MessageReceived(BMessage* 
message);
                virtual void                    AttachedToWindow();
                virtual void                    DetachedFromWindow();
-               
+
                                void                    SaveProfile(BString 
profileName);
                                void                    LoadProfile(BString 
profileName);
 
@@ -52,7 +52,7 @@
 
                                bool                    
_ValidateControl(BTextControl* control);
 private:
-               
+
                                BButton*                fApplyButton;
                                BButton*                fRevertButton;
                                        // TODO: buttons should be moved to 
window instead

Modified: haiku/trunk/src/preferences/network/Jamfile
===================================================================
--- haiku/trunk/src/preferences/network/Jamfile 2009-11-11 09:03:28 UTC (rev 
33995)
+++ haiku/trunk/src/preferences/network/Jamfile 2009-11-11 09:06:59 UTC (rev 
33996)
@@ -7,8 +7,8 @@
        NetworkApp.cpp
        NetworkWindow.cpp
        EthernetSettingsView.cpp
-       settings.cpp
-       : be root $(HAIKU_NETWORK_LIBS) $(TARGET_LIBSUPC++)
+       Settings.cpp
+
+       : be $(HAIKU_NETWORK_LIBS) $(TARGET_LIBSUPC++)
        : Network.rdef
-       ;
-
+;

Copied: haiku/trunk/src/preferences/network/Settings.cpp (from rev 33995, 
haiku/trunk/src/preferences/network/settings.cpp)
===================================================================
--- haiku/trunk/src/preferences/network/Settings.cpp                            
(rev 0)
+++ haiku/trunk/src/preferences/network/Settings.cpp    2009-11-11 09:06:59 UTC 
(rev 33996)
@@ -0,0 +1,163 @@
+/*
+ * Copyright 2004-2009 Haiku Inc. All rights reserved.
+ * Distributed under the terms of the MIT License.
+ *
+ * Authors:
+ *             Andre Alves Garzia, andre@xxxxxxxxxxxxxxx
+ *             Axel Dörfler, axeld@xxxxxxxxxxxxxxxxx
+ */
+
+
+#include "Settings.h"
+
+#include <arpa/inet.h>
+#include <errno.h>
+#include <net/if.h>
+#include <netinet/in.h>
+#include <resolv.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <sys/sockio.h>
+#include <unistd.h>
+
+#include <File.h>
+#include <Path.h>
+#include <String.h>
+
+#include <AutoDeleter.h>
+
+
+Settings::Settings(const char* name)
+       :
+       fAuto(true),
+       fNameServers(5, true)
+{
+       fSocket = socket(AF_INET, SOCK_DGRAM, 0);
+       fName = name;
+
+       ReadConfiguration();
+}
+
+
+Settings::~Settings()
+{
+       close(fSocket);
+}
+
+
+bool
+Settings::_PrepareRequest(struct ifreq& request)
+{
+       // This function is used for talking direct to the stack.
+       // It´s used by _ShowConfiguration.
+
+       const char* name = fName.String();
+
+       if (strlen(name) > IF_NAMESIZE)
+               return false;
+
+       strcpy(request.ifr_name, name);
+       return true;
+}
+
+
+void
+Settings::ReadConfiguration()
+{
+       ifreq request;
+       if (!_PrepareRequest(request))
+               return;
+
+       BString text = "dummy";
+       char address[32];
+       sockaddr_in* inetAddress = NULL;
+
+       // 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;
+       }
+
+       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;
+
+       // Obtain gateway
+       ifconf config;
+       config.ifc_len = sizeof(config.ifc_value);
+       if (ioctl(fSocket, SIOCGRTSIZE, &config, sizeof(struct ifconf)) < 0)
+               return;
+
+       uint32 size = (uint32)config.ifc_value;
+       if (size == 0)
+               return;
+
+       void* buffer = malloc(size);
+       if (buffer == NULL)
+               return;
+
+       MemoryDeleter bufferDeleter(buffer);
+       config.ifc_len = size;
+       config.ifc_buf = buffer;
+
+       if (ioctl(fSocket, SIOCGRTTABLE, &config, sizeof(struct ifconf)) < 0)
+               return;
+
+       ifreq* interface = (ifreq*)buffer;
+       ifreq* end = (ifreq*)((uint8*)buffer + size);
+
+       while (interface < end) {
+               route_entry& route = interface->ifr_route;
+
+               if ((route.flags & RTF_GATEWAY) != 0) {
+                       inetAddress = (sockaddr_in*)route.gateway;
+                       fGateway = inet_ntoa(inetAddress->sin_addr);
+               }
+
+               int32 addressSize = 0;
+               if (route.destination != NULL)
+                       addressSize += route.destination->sa_len;
+               if (route.mask != NULL)
+                       addressSize += route.mask->sa_len;
+               if (route.gateway != NULL)
+                       addressSize += route.gateway->sa_len;
+
+               interface = (ifreq *)((addr_t)interface + IF_NAMESIZE
+                       + sizeof(route_entry) + addressSize);
+       }
+
+       uint32 flags = 0;
+       if (ioctl(fSocket, SIOCGIFFLAGS, &request, sizeof(struct ifreq)) == 0)
+               flags = request.ifr_flags;
+
+       fAuto = (flags & IFF_AUTO_CONFIGURED) != 0;
+
+       // read resolv.conf for the dns.
+       fNameServers.MakeEmpty();
+
+       res_init();
+       res_state state = __res_state();
+
+       if (state != NULL) {
+               for (int i = 0; i < state->nscount; i++) {
+                       fNameServers.AddItem(
+                               new 
BString(inet_ntoa(state->nsaddr_list[i].sin_addr)));
+               }
+       }
+}

Copied: haiku/trunk/src/preferences/network/Settings.h (from rev 33995, 
haiku/trunk/src/preferences/network/settings.h)
===================================================================
--- haiku/trunk/src/preferences/network/Settings.h                              
(rev 0)
+++ haiku/trunk/src/preferences/network/Settings.h      2009-11-11 09:06:59 UTC 
(rev 33996)
@@ -0,0 +1,49 @@
+/*
+ * Copyright 2004-2009 Haiku Inc. All rights reserved.
+ * Distributed under the terms of the MIT License.
+ *
+ * Author:
+ *             Andre Alves Garzia, andre@xxxxxxxxxxxxxxx
+ */
+#ifndef SETTINGS_H
+#define SETTINGS_H
+
+
+#include <ObjectList.h>
+#include <String.h>
+
+
+class Settings {
+public:
+                                                               Settings(const 
char* name);
+       virtual                                         ~Settings();
+
+                       void                            SetIP(BString ip) { fIP 
= ip; }
+                       void                            SetGateway(BString ip) 
{ fGateway = ip; }
+                       void                            SetNetmask(BString ip) 
{ fNetmask = ip; }
+                       void                            SetAutoConfigure(bool 
autoConfigure)
+                                                                       { fAuto 
= autoConfigure; }
+
+                       const char*                     IP()  { return 
fIP.String(); }
+                       const char*                     Gateway()  { return 
fGateway.String(); }
+                       const char*                     Netmask()  { return 
fNetmask.String(); }
+                       const char*                     Name()  { return 
fName.String(); }
+                       bool                            AutoConfigure() { 
return fAuto; }
+
+                       BObjectList<BString>& NameServers() { return 
fNameServers; }
+
+                       void                            ReadConfiguration();
+
+private:
+                       bool                            _PrepareRequest(struct 
ifreq& request);
+
+                       BString                         fIP;
+                       BString                         fGateway;
+                       BString                         fNetmask;
+                       BString                         fName;
+                       int                                     fSocket;
+                       bool                            fAuto;
+                       BObjectList<BString> fNameServers;
+};
+
+#endif /* SETTINGS_H */


Other related posts:

  • » [haiku-commits] r33996 - haiku/trunk/src/preferences/network - axeld