Ticket #2531: dhcp.2.diff
File dhcp.2.diff, 5.8 KB (added by , 16 years ago) |
---|
-
src/servers/net/DHCPClient.cpp
21 21 #include <sys/time.h> 22 22 23 23 24 #define LND do { ktrace_printf("DHCP: %s: %s: %d\n", __FILE__, __func__, __LINE__); } while(0) 25 24 26 // See RFC 2131 for DHCP, see RFC 1533 for BOOTP/DHCP options 25 27 26 28 #define DHCP_CLIENT_PORT 68 … … 336 338 fRunner(NULL), 337 339 fLeaseTime(0) 338 340 { 341 LND; 339 342 fStartTime = system_time(); 340 343 fTransactionID = (uint32)fStartTime; 341 344 342 345 fStatus = get_mac_address(device, fMAC); 343 if (fStatus < B_OK) 346 if (fStatus < B_OK) { 347 LND; 344 348 return; 349 } 345 350 346 351 memset(&fServer, 0, sizeof(struct sockaddr_in)); 347 352 fServer.sin_family = AF_INET; 348 353 fServer.sin_len = sizeof(struct sockaddr_in); 349 354 fServer.sin_port = htons(DHCP_SERVER_PORT); 355 LND; 350 356 } 351 357 352 358 353 359 DHCPClient::~DHCPClient() 354 360 { 361 LND; 355 362 if (fStatus != B_OK) 356 363 return; 357 364 … … 374 381 status_t 375 382 DHCPClient::Initialize() 376 383 { 384 LND; 377 385 fStatus = _Negotiate(INIT); 378 386 printf("DHCP for %s, status: %s\n", fDevice.String(), strerror(fStatus)); 379 387 return fStatus; … … 383 391 status_t 384 392 DHCPClient::_Negotiate(dhcp_state state) 385 393 { 394 LND; 386 395 int socket = ::socket(AF_INET, SOCK_DGRAM, 0); 387 396 if (socket < 0) 388 397 return errno; … … 453 462 454 463 // receive loop until we've got an offer and acknowledged it 455 464 465 LND; 466 456 467 while (state != ACKNOWLEDGED) { 468 LND; 457 469 char buffer[2048]; 458 470 ssize_t bytesReceived = recvfrom(socket, buffer, sizeof(buffer), 459 471 0, NULL, NULL); 460 472 if (bytesReceived < 0 && errno == B_TIMED_OUT) { 473 LND; 461 474 // depending on the state, we'll just try again 462 475 if (!_TimeoutShift(socket, timeout, tries)) { 476 LND; 463 477 close(socket); 464 478 return B_TIMED_OUT; 465 479 } … … 467 481 if (state == INIT) 468 482 status = _SendMessage(socket, discover, broadcast); 469 483 else { 484 LND; 470 485 status = _SendMessage(socket, request, state != RENEWAL 471 486 ? broadcast : fServer); 472 487 } 473 488 474 489 if (status < B_OK) 475 490 break; 491 LND; 476 492 } else if (bytesReceived < B_OK) 477 493 break; 494 LND; 478 495 479 496 dhcp_message *message = (dhcp_message *)buffer; 480 497 if (message->transaction_id != htonl(fTransactionID) 481 498 || !message->HasOptions() 482 499 || memcmp(message->mac_address, discover.mac_address, 483 500 discover.hardware_address_length)) { 501 LND; 484 502 // this message is not for us 485 503 continue; 486 504 } … … 488 506 switch (message->Type()) { 489 507 case DHCP_NONE: 490 508 default: 509 LND; 491 510 // ignore this message 492 511 break; 493 512 494 513 case DHCP_OFFER: 495 514 { 515 LND; 496 516 // first offer wins 497 517 if (state != INIT) 498 518 break; … … 526 546 527 547 case DHCP_ACK: 528 548 { 549 LND; 529 550 if (state != REQUESTING && state != REBINDING 530 551 && state != RENEWAL) 531 552 continue; 553 LND; 532 554 533 555 // TODO: we might want to configure the stuff, don't we? 534 556 BMessage address; … … 548 570 } 549 571 550 572 case DHCP_NACK: 573 LND; 551 574 if (state != REQUESTING) 552 575 continue; 576 LND; 553 577 554 578 // try again (maybe we should prefer other servers if this 555 579 // happens more than once) … … 559 583 break; 560 584 } 561 585 } 586 LND; 562 587 563 588 close(socket); 564 589 … … 582 607 fRenewalTime = (fLeaseTime - now) * 2/3 + now; 583 608 fRebindingTime = (fLeaseTime - now) * 5/6 + now; 584 609 } 610 LND; 585 611 586 612 return status; 587 613 } … … 819 845 dhcp_state state = _CurrentState(); 820 846 821 847 bigtime_t next; 848 LND; 822 849 if (_Negotiate(state) == B_OK) { 823 850 switch (state) { 824 851 case RENEWAL: -
src/servers/net/NetServer.cpp
37 37 #include <string.h> 38 38 #include <unistd.h> 39 39 40 #define LND do { ktrace_printf("DHCP: %s: %s: %d\n", __FILE__, __func__, __LINE__); } while(0) 40 41 42 41 43 static const char *kSignature = "application/x-vnd.haiku-net_server"; 42 44 43 45 … … 485 487 status_t 486 488 NetServer::_ConfigureInterface(int socket, BMessage& interface, bool fromMessage) 487 489 { 490 LND; 491 488 492 const char *device; 489 493 if (interface.FindString("device", &device) != B_OK) 490 494 return B_BAD_VALUE; … … 675 679 } 676 680 } 677 681 682 LND; 678 683 if (startAutoConfig) { 679 684 // start auto configuration 685 LND; 680 686 AutoconfigLooper* looper = new AutoconfigLooper(this, device); 681 687 looper->Run(); 682 688 … … 725 731 address.AddBool("auto config", true); 726 732 interface.AddMessage("address", &address); 727 733 734 LND; 735 728 736 return _ConfigureInterface(socket, interface); 729 737 } 730 738 … … 746 754 continue; 747 755 748 756 if (S_ISBLK(stat.st_mode) || S_ISCHR(stat.st_mode)) { 757 LND; 749 758 if (suggestedInterface != NULL 750 759 && suggestedInterface->RemoveName("device") == B_OK 751 760 && suggestedInterface->AddString("device", path.Path()) == B_OK … … 782 791 } 783 792 } 784 793 794 LND; 785 795 _ConfigureInterface(socket, interface); 786 796 } 787 797 } … … 817 827 address.AddString("address", "127.0.0.1"); 818 828 interface.AddMessage("address", &address); 819 829 830 LND; 820 831 _ConfigureInterface(socket, interface); 821 832 } 822 833 -
src/servers/net/AutoconfigLooper.cpp
18 18 #include <sys/socket.h> 19 19 #include <sys/sockio.h> 20 20 21 #define LND do { ktrace_printf("DHCP: %s: %s: %d\n", __FILE__, __func__, __LINE__); } while(0) 21 22 23 22 24 static const uint32 kMsgReadyToRun = 'rdyr'; 23 25 24 26 … … 27 29 fTarget(target), 28 30 fDevice(device) 29 31 { 32 LND; 30 33 BMessage ready(kMsgReadyToRun); 31 34 PostMessage(&ready); 32 35 } … … 40 43 void 41 44 AutoconfigLooper::_ReadyToRun() 42 45 { 46 LND; 43 47 ifreq request; 44 48 if (!prepare_request(request, fDevice.String())) 45 49 return; 50 LND; 46 51 47 52 // set IFF_CONFIGURING flag on interface 48 53 … … 58 63 close(socket); 59 64 60 65 // start with DHCP 66 LND; 61 67 62 68 DHCPClient* client = new DHCPClient(fTarget, fDevice.String()); 63 69 AddHandler(client);