Ticket #7346: net_server.patch
File net_server.patch, 8.4 KB (added by , 13 years ago) |
---|
-
DHCPClient.cpp
422 422 fConfiguration(kMsgConfigureInterface), 423 423 fResolverConfiguration(kMsgConfigureResolver), 424 424 fRunner(NULL), 425 fAssignedAddress(0), 425 426 fServer(AF_INET, NULL, DHCP_SERVER_PORT), 426 427 fLeaseTime(0) 427 428 { … … 436 437 437 438 memcpy(fMAC, link.LinkLevelAddress(), sizeof(fMAC)); 438 439 440 if ((interface.Flags() & IFF_AUTO_CONFIGURED) != 0) { 441 // Check for interface previous auto-configured address, if any. 442 BNetworkInterfaceAddress interfaceAddress; 443 int index = interface.FindFirstAddress(AF_INET); 444 if (index >= 0 445 && interface.GetAddressAt(index, interfaceAddress) == B_OK) { 446 BNetworkAddress address = interfaceAddress.Address(); 447 const sockaddr_in& addr = (sockaddr_in&)address.SockAddr(); 448 fAssignedAddress = addr.sin_addr.s_addr; 449 } 450 } 451 439 452 openlog_thread("DHCP", 0, LOG_DAEMON); 440 453 } 441 454 … … 466 479 status_t 467 480 DHCPClient::Initialize() 468 481 { 469 fStatus = _Negotiate( INIT);470 syslog(LOG_DEBUG, " DHCP for %s, status:%s\n", Device(), strerror(fStatus));482 fStatus = _Negotiate(fAssignedAddress == 0 ? INIT : INIT_REBOOT); 483 syslog(LOG_DEBUG, "%s: DHCP status = %s\n", Device(), strerror(fStatus)); 471 484 return fStatus; 472 485 } 473 486 … … 527 540 528 541 // send discover/request message 529 542 _SendMessage(socket, state == INIT ? discover : request, 530 state != RENEW AL? broadcast : fServer);543 state != RENEWING ? broadcast : fServer); 531 544 // no need to check the status; in case of an error we'll just send 532 545 // the message again 533 546 534 547 // receive loop until we've got an offer and acknowledged it 535 548 536 while (state != ACKNOWLEDGED) {549 while (state != BOUND) { 537 550 char buffer[2048]; 538 551 struct sockaddr_in from; 539 552 socklen_t fromLength = sizeof(from); … … 546 559 return B_TIMED_OUT; 547 560 } 548 561 549 if (state == INIT) 550 _SendMessage(socket, discover, broadcast); 551 else { 552 _SendMessage(socket, request, 553 state != RENEWAL ? broadcast : fServer); 554 } 562 _SendMessage(socket, state == INIT ? discover : request, 563 state != RENEWING ? broadcast : fServer); 555 564 556 565 continue; 557 566 } else if (bytesReceived < 0) … … 566 575 continue; 567 576 } 568 577 569 syslog(LOG_DEBUG, "Received %s from %s for %s\n", 570 dhcp_message::TypeToString(message->Type()), 571 _AddressToString(from.sin_addr.s_addr).String(), Device()); 578 // advance from startup state 579 if (state == INIT) 580 state = SELECTING; 581 else if (state == INIT_REBOOT) 582 state = REBOOTING; 572 583 584 syslog(LOG_DEBUG, "%s: Received %s from %s\n", 585 Device(), dhcp_message::TypeToString(message->Type()), 586 _AddressToString(from.sin_addr.s_addr).String()); 587 573 588 switch (message->Type()) { 574 589 case DHCP_NONE: 575 590 default: … … 579 594 case DHCP_OFFER: 580 595 { 581 596 // first offer wins 582 if (state != INIT)597 if (state != SELECTING) 583 598 break; 584 599 585 600 // collect interface options … … 590 605 591 606 fConfiguration.MakeEmpty(); 592 607 fConfiguration.AddString("device", Device()); 593 fConfiguration.AddBool("auto ", true);608 fConfiguration.AddBool("auto_configured", true); 594 609 595 610 BMessage address; 596 611 address.AddString("family", "inet"); … … 614 629 615 630 case DHCP_ACK: 616 631 { 617 if (state != REQUESTING && state != REBINDING 618 && state != RENEWAL) 632 if (state != REQUESTING 633 && state != REBOOTING 634 && state != REBINDING 635 && state != RENEWING) 619 636 continue; 620 637 621 638 // TODO: we might want to configure the stuff, don't we? … … 626 643 // way 627 644 628 645 // our address request has been acknowledged 629 state = ACKNOWLEDGED;646 state = BOUND; 630 647 631 648 // configure interface 632 649 BMessage reply; … … 644 661 } 645 662 646 663 case DHCP_NACK: 647 if (state != REQUESTING) 664 if (state != REQUESTING 665 && state != REBOOTING 666 && state != REBINDING 667 && state != RENEWING) 648 668 continue; 649 669 670 if (state == REBOOTING) { 671 // server reject our request on previous assigned address 672 // back to square one... 673 fAssignedAddress = 0; 674 } 675 650 676 // try again (maybe we should prefer other servers if this 651 677 // happens more than once) 652 678 status = _SendMessage(socket, discover, broadcast); … … 819 845 (uint32)server.sin_addr.s_addr); 820 846 } 821 847 822 if (state == INIT || state == REQUESTING) { 848 if (state == INIT || state == INIT_REBOOT 849 || state == REQUESTING) { 823 850 next = message.PutOption(next, OPTION_REQUEST_IP_ADDRESS, 824 851 (uint32)fAssignedAddress); 825 852 } else … … 866 893 if (++tries > 2) 867 894 return false; 868 895 } 869 syslog(LOG_DEBUG, " Timeout shift for %s: %lu secs (try %lu)\n",896 syslog(LOG_DEBUG, "%s: Timeout shift: %lu secs (try %lu)\n", 870 897 Device(), timeout, tries); 871 898 872 899 struct timeval value; … … 898 925 DHCPClient::_SendMessage(int socket, dhcp_message& message, 899 926 const BNetworkAddress& address) const 900 927 { 901 syslog(LOG_DEBUG, "Send %s to %s on %s\n", 902 dhcp_message::TypeToString(message.Type()), 903 address.ToString().String(), Device()); 928 message_type type = message.Type(); 929 BString text; 930 text << dhcp_message::TypeToString(type); 931 932 const uint8* requestAddress = message.FindOption(OPTION_REQUEST_IP_ADDRESS); 933 if (type == DHCP_REQUEST && requestAddress != NULL) 934 text << " for " << _AddressToString(requestAddress).String(); 935 936 syslog(LOG_DEBUG, "%s: Send %s to %s\n", Device(), text.String(), 937 address.ToString().String()); 904 938 905 939 ssize_t bytesSent = sendto(socket, &message, message.Size(), 906 940 address.IsBroadcast() ? MSG_BCAST : 0, address, address.Length()); … … 921 955 if (now >= fRebindingTime) 922 956 return REBINDING; 923 957 if (now >= fRenewalTime) 924 return RENEW AL;958 return RENEWING; 925 959 926 960 return BOUND; 927 961 } … … 938 972 bigtime_t next; 939 973 if (_Negotiate(state) == B_OK) { 940 974 switch (state) { 941 case RENEW AL:975 case RENEWING: 942 976 next = fRebindingTime; 943 977 break; 944 978 case REBINDING: … … 948 982 } 949 983 } else { 950 984 switch (state) { 951 case RENEW AL:985 case RENEWING: 952 986 next = (fLeaseTime - fRebindingTime) / 4 + system_time(); 953 987 break; 954 988 case REBINDING: -
NetServer.cpp
443 443 flags = IFF_UP; 444 444 445 445 bool autoConfigured; 446 if (message.FindBool("auto ", &autoConfigured) == B_OK && autoConfigured)446 if (message.FindBool("auto_configured", &autoConfigured) == B_OK && autoConfigured) 447 447 flags |= IFF_AUTO_CONFIGURED; 448 448 449 449 int32 mtu; … … 532 532 if (addressMessage.FindString("broadcast", &string) == B_OK) 533 533 parse_address(family, string, broadcast); 534 534 } 535 535 536 536 if (autoConfig) { 537 537 _QuitLooperForDevice(name); 538 538 startAutoConfig = true; -
AutoconfigLooper.cpp
58 58 void 59 59 AutoconfigLooper::_Configure() 60 60 { 61 // start with DHCP 62 63 if (fCurrentClient == NULL) { 64 fCurrentClient = new DHCPClient(fTarget, fDevice.String()); 65 AddHandler(fCurrentClient); 66 } 67 61 68 // set IFF_CONFIGURING flag on interface 62 69 63 70 BNetworkInterface interface(fDevice.String()); 64 71 int32 flags = interface.Flags() & ~IFF_AUTO_CONFIGURED; 65 72 interface.SetFlags(flags | IFF_CONFIGURING); 66 73 67 // remove current handler68 69 _RemoveClient();70 71 // start with DHCP72 73 fCurrentClient = new DHCPClient(fTarget, fDevice.String());74 AddHandler(fCurrentClient);75 76 74 if (fCurrentClient->Initialize() == B_OK) 77 75 return; 78 76 … … 91 89 92 90 BMessage message(kMsgConfigureInterface); 93 91 message.AddString("device", fDevice.String()); 94 message.AddBool("auto ", true);92 message.AddBool("auto_configured", true); 95 93 96 94 BNetworkAddress link; 97 95 uint8 last = 56; -
DHCPClient.h
22 22 23 23 enum dhcp_state { 24 24 INIT, 25 SELECTING, 26 INIT_REBOOT, 27 REBOOTING, 25 28 REQUESTING, 26 29 BOUND, 27 RENEW AL,30 RENEWING, 28 31 REBINDING, 29 ACKNOWLEDGED,30 32 }; 31 33 32 34 … … 55 57 bool _TimeoutShift(int socket, time_t& timeout, 56 58 uint32& tries); 57 59 void _RestartLease(bigtime_t lease); 58 60 59 61 static BString _AddressToString(const uint8* data); 60 static BString _AddressToString(in_addr_t address);62 static BString _AddressToString(in_addr_t address); 61 63 62 64 private: 63 65 BMessage fConfiguration;