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

  • From: axeld@xxxxxxxxxxxxxxxx
  • To: haiku-commits@xxxxxxxxxxxxx
  • Date: Wed, 11 Nov 2009 09:45:36 +0100 (CET)

Author: axeld
Date: 2009-11-11 09:45:36 +0100 (Wed, 11 Nov 2009)
New Revision: 33993
Changeset: http://dev.haiku-os.org/changeset/33993/haiku
Ticket: http://dev.haiku-os.org/ticket/2603

Modified:
   haiku/trunk/src/preferences/network/EthernetSettingsView.cpp
   haiku/trunk/src/preferences/network/settings.cpp
Log:
* EthernetSettingsView now stores resolv.conf to the correct location.
* Settings::ReadConfiguration() now uses functions of resolv.h to retrieve the
  list of name servers instead of parsing resolv.conf itself.
* Thanks for the note, Siarzhuk - this closes ticket #2603.


Modified: haiku/trunk/src/preferences/network/EthernetSettingsView.cpp
===================================================================
--- haiku/trunk/src/preferences/network/EthernetSettingsView.cpp        
2009-11-11 07:05:08 UTC (rev 33992)
+++ haiku/trunk/src/preferences/network/EthernetSettingsView.cpp        
2009-11-11 08:45:36 UTC (rev 33993)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2004-2008 Haiku Inc. All rights reserved.
+ * Copyright 2004-2009 Haiku Inc. All rights reserved.
  * Distributed under the terms of the MIT License.
  *
  * Authors:
@@ -10,6 +10,7 @@
  *             Philippe Saint-Pierre
  */
 
+
 #include "EthernetSettingsView.h"
 #include "settings.h"
 
@@ -375,9 +376,14 @@
 void
 EthernetSettingsView::_SaveDNSConfiguration()
 {
-       BFile file("/etc/resolv.conf",
-               B_CREATE_FILE | B_ERASE_FILE | B_WRITE_ONLY);
-       if (file.InitCheck() < B_OK) {
+       BPath path;
+       if (find_directory(B_COMMON_SETTINGS_DIRECTORY, &path) != B_OK)
+               return;
+
+       path.Append("network/resolv.conf");
+
+       BFile file(path.Path(), B_CREATE_FILE | B_ERASE_FILE | B_WRITE_ONLY);
+       if (file.InitCheck() != B_OK) {
                fprintf(stderr, "failed to open /etc/resolv.conf for writing: 
%s\n",
                        strerror(file.InitCheck()));
                return;

Modified: haiku/trunk/src/preferences/network/settings.cpp
===================================================================
--- haiku/trunk/src/preferences/network/settings.cpp    2009-11-11 07:05:08 UTC 
(rev 33992)
+++ haiku/trunk/src/preferences/network/settings.cpp    2009-11-11 08:45:36 UTC 
(rev 33993)
@@ -1,54 +1,58 @@
 /*
- * 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:
+ * Authors:
  *             Andre Alves Garzia, andre@xxxxxxxxxxxxxxx
+ *             Axel Dörfler, axeld@xxxxxxxxxxxxxxxxx
  */
- 
-#include "settings.h"
 
-#include <String.h>
-#include <File.h>
-#include <Path.h>
- 
-#include <SupportDefs.h>
-#include <AutoDeleter.h>
 
+#include "settings.h"
+
 #include <arpa/inet.h>
+#include <errno.h>
 #include <net/if.h>
 #include <net/if_dl.h>
 #include <net/if_media.h>
 #include <net/if_types.h>
 #include <netinet/in.h>
-#include <sys/socket.h>
-#include <sys/sockio.h>
-
-#include <errno.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)
 {
        fSocket = socket(AF_INET, SOCK_DGRAM, 0);
        fName = name;
-       ReadConfiguration();    
+       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.
+       // This function is used for talking direct to the stack.
+       // It´s used by _ShowConfiguration.
 
        const char* name = fName.String();
 
@@ -71,16 +75,16 @@
        char address[32];
        sockaddr_in* inetAddress = NULL;
 
-       // Obtain IP.   
+       // 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, 
+       if (inet_ntop(AF_INET, &inetAddress->sin_addr, address,
                        sizeof(address)) == NULL) {
                return;
        }
-       
+
        fIP = address;
 
        // Obtain netmask.
@@ -121,7 +125,6 @@
        ifreq *interface = (ifreq *)buffer;
        ifreq *end = (ifreq *)((uint8 *)buffer + size);
 
-
        while (interface < end) {
                route_entry& route = interface->ifr_route;
 
@@ -138,7 +141,7 @@
                if (route.gateway != NULL)
                        addressSize += route.gateway->sa_len;
 
-               interface = (ifreq *)((addr_t)interface + 
+               interface = (ifreq *)((addr_t)interface +
                        IF_NAMESIZE + sizeof(route_entry) + addressSize);
        }
 
@@ -146,50 +149,18 @@
        if (ioctl(fSocket, SIOCGIFFLAGS, &request, sizeof(struct ifreq)) == 0)
                flags = request.ifr_flags;
 
-       fAuto = flags & IFF_AUTO_CONFIGURED;
+       fAuto = (flags & IFF_AUTO_CONFIGURED) != 0;
 
        // read resolv.conf for the dns.
        fNameservers.MakeEmpty();
 
-#define        MATCH(line, name) \
-       (!strncmp(line, name, sizeof(name) - 1) && \
-       (line[sizeof(name) - 1] == ' ' || \
-        line[sizeof(name) - 1] == '\t'))
+       res_init();
+       res_state state = __res_state();
 
-
-       register FILE *fp = fopen("/etc/resolv.conf", "r");
-       if (fp == NULL) {
-               fprintf(stderr, "failed to open '/etc/resolv.conf' to "
-                       "read nameservers: %s\n", strerror(errno));
-               return;
-       }
-
-       int nserv = 0;
-       char buf[1024];
-       register char *cp; //, **pp;
-//     register int n;
-       int MAXNS = 2;
-
-       // read the config file
-       while (fgets(buf, sizeof(buf), fp) != NULL) {
-               // skip comments
-               if (*buf == ';' || *buf == '#')
-                       continue;
-
-               // read nameservers to query
-               if (MATCH(buf, "nameserver") && nserv < MAXNS) {
-//                     char sbuf[2];
-                       cp = buf + sizeof("nameserver") - 1;
-                       while (*cp == ' ' || *cp == '\t')
-                               cp++;
-                       cp[strcspn(cp, ";# \t\n")] = '\0';
-                       if ((*cp != '\0') && (*cp != '\n')) {
-                               fNameservers.AddItem(new BString(cp));
-                               nserv++;
-                       }
+       if (state != NULL) {
+               for (int i = 0; i < state->nscount; i++) {
+                       fNameservers.AddItem(
+                               new 
BString(inet_ntoa(state->nsaddr_list[i].sin_addr)));
                }
-               continue; 
        }
-
-       fclose(fp);
 }


Other related posts:

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