Ticket #5636: netserver_resolver_conf.patch
File netserver_resolver_conf.patch, 8.4 KB (added by , 15 years ago) |
---|
-
src/servers/net/DHCPClient.cpp
11 11 #include "DHCPClient.h" 12 12 #include "NetServer.h" 13 13 14 #include <FindDirectory.h>15 14 #include <Message.h> 16 15 #include <MessageRunner.h> 17 #include <Path.h>18 16 19 17 #include <arpa/inet.h> 20 18 #include <errno.h> … … 336 334 DHCPClient::DHCPClient(BMessenger target, const char* device) 337 335 : AutoconfigClient("dhcp", target, device), 338 336 fConfiguration(kMsgConfigureInterface), 337 fResolverConfiguration(kMsgConfigureResolver), 339 338 fRunner(NULL), 340 339 fLeaseTime(0) 341 340 { … … 519 518 BMessage address; 520 519 address.AddString("family", "inet"); 521 520 address.AddString("address", _ToString(fAssignedAddress)); 522 _ParseOptions(*message, address); 521 fResolverConfiguration.MakeEmpty(); 522 _ParseOptions(*message, address, fResolverConfiguration); 523 523 524 524 fConfiguration.AddMessage("address", &address); 525 525 … … 543 543 544 544 // TODO: we might want to configure the stuff, don't we? 545 545 BMessage address; 546 _ParseOptions(*message, address); 546 fResolverConfiguration.MakeEmpty(); 547 _ParseOptions(*message, address, fResolverConfiguration); 547 548 // TODO: currently, only lease time and DNS is updated this way 548 549 549 550 // our address request has been acknowledged … … 554 555 status = Target().SendMessage(&fConfiguration, &reply); 555 556 if (status == B_OK) 556 557 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); 557 564 break; 558 565 } 559 566 … … 609 616 610 617 611 618 void 612 DHCPClient::_ParseOptions(dhcp_message& message, BMessage& address) 619 DHCPClient::_ParseOptions(dhcp_message& message, BMessage& address, 620 BMessage& resolverConfiguration) 613 621 { 614 622 dhcp_option_cookie cookie; 615 623 message_option option; 616 624 const uint8* data; 617 625 size_t size; 618 // TODO: resolv.conf should be parsed, all information should be619 // maintained and it should be distinguished between user entered620 // and auto-generated parts of the file, with this method only re-writing621 // the auto-generated parts of course.622 // TODO: We write resolv.conf once per _ParseOptions invokation, there623 // is the first DHCP_OFFER message and the final DHCP_ACK message624 // from the same server, which should contain all the final data.625 bool resolvConfCreated = false;626 626 while (message.NextOption(cookie, option, data, size)) { 627 627 // iterate through all options 628 628 switch (option) { … … 637 637 break; 638 638 case OPTION_DOMAIN_NAME_SERVER: 639 639 { 640 BPath path;641 if (find_directory(B_COMMON_SETTINGS_DIRECTORY, &path) != B_OK642 || 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);648 640 for (uint32 i = 0; i < size / 4; i++) { 649 641 syslog(LOG_INFO, "DNS: %s\n", 650 642 _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()); 656 645 } 657 fclose(file); 646 resolverConfiguration.AddInt32("nameserver_count", 647 size / 4); 658 648 break; 659 649 } 660 650 case OPTION_SERVER_ADDRESS: … … 684 674 685 675 case OPTION_DOMAIN_NAME: 686 676 { 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); 704 685 break; 705 686 } 706 687 -
src/servers/net/NetServer.cpp
1 1 /* 2 * Copyright 2006-20 09, Haiku, Inc. All Rights Reserved.2 * Copyright 2006-2010, Haiku, Inc. All Rights Reserved. 3 3 * Distributed under the terms of the MIT License. 4 4 * 5 5 * Authors: 6 6 * Axel Dörfler, axeld@pinc-software.de 7 * Vegard Wærp, vegarwa@online.no 7 8 */ 8 9 9 10 … … 33 34 #include <Roster.h> 34 35 #include <Server.h> 35 36 #include <TextView.h> 37 #include <FindDirectory.h> 36 38 37 39 #include "AutoconfigLooper.h" 38 40 #include "Services.h" … … 57 59 bool _TestForInterface(int socket, const char* name); 58 60 status_t _ConfigureInterface(int socket, BMessage& interface, 59 61 bool fromMessage = false); 62 status_t _ConfigureResolver(BMessage& resolverConfiguration); 60 63 bool _QuitLooperForDevice(const char* device); 61 64 AutoconfigLooper* _LooperForDevice(const char* device); 62 65 status_t _ConfigureDevice(int socket, const char* path); … … 344 347 close(socket); 345 348 break; 346 349 } 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 } 347 360 348 361 default: 349 362 BApplication::MessageReceived(message); … … 720 733 } 721 734 722 735 736 status_t 737 NetServer::_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 723 777 bool 724 778 NetServer::_QuitLooperForDevice(const char* device) 725 779 { -
src/servers/net/DHCPClient.h
39 39 private: 40 40 status_t _Negotiate(dhcp_state state); 41 41 void _ParseOptions(dhcp_message& message, 42 BMessage& address); 42 BMessage& address, 43 BMessage& resolverConfiguration); 43 44 void _PrepareMessage(dhcp_message& message, 44 45 dhcp_state state); 45 46 status_t _SendMessage(int socket, dhcp_message& message, … … 55 56 56 57 private: 57 58 BMessage fConfiguration; 59 BMessage fResolverConfiguration; 58 60 BMessageRunner* fRunner; 59 61 uint8 fMAC[6]; 60 62 uint32 fTransactionID; -
src/servers/net/NetServer.h
1 1 /* 2 * Copyright 2006-20 09, Haiku, Inc. All Rights Reserved.2 * Copyright 2006-2010, Haiku, Inc. All Rights Reserved. 3 3 * Distributed under the terms of the MIT License. 4 4 * 5 5 * Authors: … … 15 15 16 16 17 17 // NOTE: this header is used by other applications (such as ifconfig, 18 // and Network) because of these t wodefines18 // and Network) because of these three defines 19 19 #define kNetServerSignature "application/x-vnd.haiku-net_server" 20 20 #define kMsgConfigureInterface 'COif' 21 #define kMsgConfigureResolver 'COrs' 21 22 22 23 23 24 extern bool get_family_index(const char* name, int32& familyIndex);