Ticket #5636: netserver_resolver_conf.patch

File netserver_resolver_conf.patch, 8.4 KB (added by vegardw, 15 years ago)

Updated patch

  • src/servers/net/DHCPClient.cpp

     
    1111#include "DHCPClient.h"
    1212#include "NetServer.h"
    1313
    14 #include <FindDirectory.h>
    1514#include <Message.h>
    1615#include <MessageRunner.h>
    17 #include <Path.h>
    1816
    1917#include <arpa/inet.h>
    2018#include <errno.h>
     
    336334DHCPClient::DHCPClient(BMessenger target, const char* device)
    337335    : AutoconfigClient("dhcp", target, device),
    338336    fConfiguration(kMsgConfigureInterface),
     337    fResolverConfiguration(kMsgConfigureResolver),
    339338    fRunner(NULL),
    340339    fLeaseTime(0)
    341340{
     
    519518                BMessage address;
    520519                address.AddString("family", "inet");
    521520                address.AddString("address", _ToString(fAssignedAddress));
    522                 _ParseOptions(*message, address);
     521                fResolverConfiguration.MakeEmpty();
     522                _ParseOptions(*message, address, fResolverConfiguration);
    523523
    524524                fConfiguration.AddMessage("address", &address);
    525525
     
    543543
    544544                // TODO: we might want to configure the stuff, don't we?
    545545                BMessage address;
    546                 _ParseOptions(*message, address);
     546                fResolverConfiguration.MakeEmpty();
     547                _ParseOptions(*message, address, fResolverConfiguration);
    547548                    // TODO: currently, only lease time and DNS is updated this way
    548549
    549550                // our address request has been acknowledged
     
    554555                status = Target().SendMessage(&fConfiguration, &reply);
    555556                if (status == B_OK)
    556557                    status = reply.FindInt32("status", &fStatus);
     558                   
     559                // configure resolver
     560                reply.MakeEmpty();
     561                status = Target().SendMessage(&fResolverConfiguration, &reply);
     562                if (status == B_OK)
     563                    status = reply.FindInt32("status", &fStatus);
    557564                break;
    558565            }
    559566
     
    609616
    610617
    611618void
    612 DHCPClient::_ParseOptions(dhcp_message& message, BMessage& address)
     619DHCPClient::_ParseOptions(dhcp_message& message, BMessage& address,
     620    BMessage& resolverConfiguration)
    613621{
    614622    dhcp_option_cookie cookie;
    615623    message_option option;
    616624    const uint8* data;
    617625    size_t size;
    618     // TODO: resolv.conf should be parsed, all information should be
    619     // maintained and it should be distinguished between user entered
    620     // and auto-generated parts of the file, with this method only re-writing
    621     // the auto-generated parts of course.
    622     // TODO: We write resolv.conf once per _ParseOptions invokation, there
    623     // is the first DHCP_OFFER message and the final DHCP_ACK message
    624     // from the same server, which should contain all the final data.
    625     bool resolvConfCreated = false;
    626626    while (message.NextOption(cookie, option, data, size)) {
    627627        // iterate through all options
    628628        switch (option) {
     
    637637                break;
    638638            case OPTION_DOMAIN_NAME_SERVER:
    639639            {
    640                 BPath path;
    641                 if (find_directory(B_COMMON_SETTINGS_DIRECTORY, &path) != B_OK
    642                     || path.Append("network/resolv.conf") != B_OK) {
    643                     break;
    644                 }
    645 
    646                 const char* openMode = resolvConfCreated ? "a" : "w";
    647                 FILE* file = fopen(path.Path(), openMode);
    648640                for (uint32 i = 0; i < size / 4; i++) {
    649641                    syslog(LOG_INFO, "DNS: %s\n",
    650642                        _ToString(&data[i * 4]).String());
    651                     if (file != NULL) {
    652                         resolvConfCreated = true;
    653                         fprintf(file, "nameserver %s\n",
    654                             _ToString(&data[i * 4]).String());
    655                     }
     643                    resolverConfiguration.AddString("nameserver",
     644                        _ToString(&data[i * 4]).String());
    656645                }
    657                 fclose(file);
     646                resolverConfiguration.AddInt32("nameserver_count",
     647                    size / 4);
    658648                break;
    659649            }
    660650            case OPTION_SERVER_ADDRESS:
     
    684674
    685675            case OPTION_DOMAIN_NAME:
    686676            {
    687                 syslog(LOG_INFO, "DHCP domain name: \"%.*s\"\n",
    688                     (int)size, (const char*)data);
    689 
    690                 BPath path;
    691                 if (find_directory(B_COMMON_SETTINGS_DIRECTORY, &path) != B_OK
    692                     || path.Append("network/resolv.conf") != B_OK) {
    693                     break;
    694                 }
    695 
    696                 const char* openMode = resolvConfCreated ? "a" : "w";
    697                 FILE* file = fopen(path.Path(), openMode);
    698                 if (file != NULL) {
    699                     resolvConfCreated = true;
    700                     fprintf(file, "domain %.*s\n", (int)size,
    701                         (const char*)data);
    702                     fclose(file);
    703                 }
     677                char domain[size+1];
     678                memcpy(domain, data, size);
     679                domain[size] = '\0';
     680               
     681                syslog(LOG_INFO, "DHCP domain name: \"%s\"\n",
     682                    domain);
     683               
     684                resolverConfiguration.AddString("domain", domain); 
    704685                break;
    705686            }
    706687
  • src/servers/net/NetServer.cpp

     
    11/*
    2  * Copyright 2006-2009, Haiku, Inc. All Rights Reserved.
     2 * Copyright 2006-2010, Haiku, Inc. All Rights Reserved.
    33 * Distributed under the terms of the MIT License.
    44 *
    55 * Authors:
    66 *      Axel Dörfler, axeld@pinc-software.de
     7 *      Vegard Wærp, vegarwa@online.no
    78 */
    89
    910
     
    3334#include <Roster.h>
    3435#include <Server.h>
    3536#include <TextView.h>
     37#include <FindDirectory.h>
    3638
    3739#include "AutoconfigLooper.h"
    3840#include "Services.h"
     
    5759        bool _TestForInterface(int socket, const char* name);
    5860        status_t _ConfigureInterface(int socket, BMessage& interface,
    5961            bool fromMessage = false);
     62        status_t _ConfigureResolver(BMessage& resolverConfiguration);
    6063        bool _QuitLooperForDevice(const char* device);
    6164        AutoconfigLooper* _LooperForDevice(const char* device);
    6265        status_t _ConfigureDevice(int socket, const char* path);
     
    344347            close(socket);
    345348            break;
    346349        }
     350        case kMsgConfigureResolver:
     351        {
     352            status_t status = _ConfigureResolver(*message);
     353           
     354            BMessage reply(B_REPLY);
     355            reply.AddInt32("status", status);
     356            message->SendReply(&reply);
     357           
     358            break;
     359        }
    347360
    348361        default:
    349362            BApplication::MessageReceived(message);
     
    720733}
    721734
    722735
     736status_t
     737NetServer::_ConfigureResolver(BMessage& resolverConfiguration)
     738{
     739    bool addDomain = false;
     740    int32 nameserverCount;
     741   
     742    const char* domain;
     743    if (resolverConfiguration.FindString("domain", &domain) == B_OK)
     744        addDomain = true;
     745       
     746    if (resolverConfiguration.FindInt32("nameserver_count",
     747            &nameserverCount) != B_OK)
     748        nameserverCount = 0;
     749       
     750    // TODO: resolv.conf should be parsed, all information should be
     751    // maintained and it should be distinguished between user entered
     752    // and auto-generated parts of the file, with this method only re-writing
     753    // the auto-generated parts of course.
     754    BPath path;
     755    if (find_directory(B_COMMON_SETTINGS_DIRECTORY, &path) != B_OK
     756        || path.Append("network/resolv.conf") != B_OK) {
     757        return B_ERROR;
     758    }
     759    FILE* file = fopen(path.Path(), "w");
     760    if (file != NULL) {
     761        for (int32 i=0; i < nameserverCount; ++i) {
     762            const char* nameserver;
     763            if (resolverConfiguration.FindString("nameserver", i, &nameserver) == B_OK)
     764                fprintf(file, "nameserver %s\n", nameserver);
     765        }
     766       
     767        if (addDomain) {
     768            fprintf(file, "domain %s\n", domain);
     769        }
     770       
     771        fclose(file);
     772    }
     773    return B_OK;
     774}
     775
     776
    723777bool
    724778NetServer::_QuitLooperForDevice(const char* device)
    725779{
  • src/servers/net/DHCPClient.h

     
    3939private:
    4040            status_t            _Negotiate(dhcp_state state);
    4141            void                _ParseOptions(dhcp_message& message,
    42                                     BMessage& address);
     42                                    BMessage& address,
     43                                    BMessage& resolverConfiguration);
    4344            void                _PrepareMessage(dhcp_message& message,
    4445                                    dhcp_state state);
    4546            status_t            _SendMessage(int socket, dhcp_message& message,
     
    5556
    5657private:
    5758            BMessage            fConfiguration;
     59            BMessage            fResolverConfiguration;
    5860            BMessageRunner*     fRunner;
    5961            uint8               fMAC[6];
    6062            uint32              fTransactionID;
  • src/servers/net/NetServer.h

     
    11/*
    2  * Copyright 2006-2009, Haiku, Inc. All Rights Reserved.
     2 * Copyright 2006-2010, Haiku, Inc. All Rights Reserved.
    33 * Distributed under the terms of the MIT License.
    44 *
    55 * Authors:
     
    1515
    1616
    1717// NOTE: this header is used by other applications (such as ifconfig,
    18 // and Network) because of these two defines
     18// and Network) because of these three defines
    1919#define kNetServerSignature     "application/x-vnd.haiku-net_server"
    2020#define kMsgConfigureInterface  'COif'
     21#define kMsgConfigureResolver   'COrs'
    2122
    2223
    2324extern bool get_family_index(const char* name, int32& familyIndex);