Ticket #2531: dhcp.3.diff
File dhcp.3.diff, 6.5 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 26 void trace_dhcp_state(dhcp_state state) 27 { 28 char *state_name=NULL; 29 switch(state) 30 { 31 case INIT: state_name = "INIT"; break; 32 case REQUESTING: state_name = "REQUESTING"; break; 33 case BOUND: state_name = "BOUND"; break; 34 case RENEWAL: state_name="RENEWAL"; break; 35 case REBINDING: state_name="REBINDING"; break; 36 case ACKNOWLEDGED: state_name="ACKNOWLEDGED"; break; 37 default: break; 38 } 39 if (state_name == NULL) 40 ktrace_printf("DHCP: state 0x%x\n", (unsigned)state); 41 else 42 ktrace_printf("DHCP: state %s\n", state_name); 43 } 44 24 45 // See RFC 2131 for DHCP, see RFC 1533 for BOOTP/DHCP options 25 46 26 47 #define DHCP_CLIENT_PORT 68 … … 336 357 fRunner(NULL), 337 358 fLeaseTime(0) 338 359 { 360 LND; 339 361 fStartTime = system_time(); 340 362 fTransactionID = (uint32)fStartTime; 341 363 342 364 fStatus = get_mac_address(device, fMAC); 343 if (fStatus < B_OK) 365 if (fStatus < B_OK) { 366 LND; 344 367 return; 368 } 345 369 346 370 memset(&fServer, 0, sizeof(struct sockaddr_in)); 347 371 fServer.sin_family = AF_INET; 348 372 fServer.sin_len = sizeof(struct sockaddr_in); 349 373 fServer.sin_port = htons(DHCP_SERVER_PORT); 374 LND; 350 375 } 351 376 352 377 353 378 DHCPClient::~DHCPClient() 354 379 { 380 LND; 355 381 if (fStatus != B_OK) 356 382 return; 357 383 … … 374 400 status_t 375 401 DHCPClient::Initialize() 376 402 { 403 LND; 404 trace_dhcp_state(INIT); 377 405 fStatus = _Negotiate(INIT); 378 406 printf("DHCP for %s, status: %s\n", fDevice.String(), strerror(fStatus)); 379 407 return fStatus; 380 408 } 381 409 382 383 410 status_t 384 411 DHCPClient::_Negotiate(dhcp_state state) 385 412 { 413 LND; 414 trace_dhcp_state(state); 386 415 int socket = ::socket(AF_INET, SOCK_DGRAM, 0); 387 416 if (socket < 0) 388 417 return errno; … … 453 482 454 483 // receive loop until we've got an offer and acknowledged it 455 484 485 LND; 486 456 487 while (state != ACKNOWLEDGED) { 488 LND; 489 trace_dhcp_state(state); 457 490 char buffer[2048]; 458 491 ssize_t bytesReceived = recvfrom(socket, buffer, sizeof(buffer), 459 492 0, NULL, NULL); 460 493 if (bytesReceived < 0 && errno == B_TIMED_OUT) { 494 LND; 461 495 // depending on the state, we'll just try again 462 496 if (!_TimeoutShift(socket, timeout, tries)) { 497 LND; 463 498 close(socket); 464 499 return B_TIMED_OUT; 465 500 } … … 467 502 if (state == INIT) 468 503 status = _SendMessage(socket, discover, broadcast); 469 504 else { 505 LND; 470 506 status = _SendMessage(socket, request, state != RENEWAL 471 507 ? broadcast : fServer); 472 508 } 473 509 474 510 if (status < B_OK) 475 511 break; 512 LND; 476 513 } else if (bytesReceived < B_OK) 477 514 break; 515 LND; 478 516 479 517 dhcp_message *message = (dhcp_message *)buffer; 480 518 if (message->transaction_id != htonl(fTransactionID) 481 519 || !message->HasOptions() 482 520 || memcmp(message->mac_address, discover.mac_address, 483 521 discover.hardware_address_length)) { 522 LND; 484 523 // this message is not for us 485 524 continue; 486 525 } … … 488 527 switch (message->Type()) { 489 528 case DHCP_NONE: 490 529 default: 530 LND; 491 531 // ignore this message 492 532 break; 493 533 494 534 case DHCP_OFFER: 495 535 { 536 LND; 496 537 // first offer wins 497 538 if (state != INIT) 498 539 break; … … 526 567 527 568 case DHCP_ACK: 528 569 { 570 LND; 571 trace_dhcp_state(state); 529 572 if (state != REQUESTING && state != REBINDING 530 573 && state != RENEWAL) 531 574 continue; 575 LND; 532 576 533 577 // TODO: we might want to configure the stuff, don't we? 534 578 BMessage address; … … 548 592 } 549 593 550 594 case DHCP_NACK: 595 LND; 551 596 if (state != REQUESTING) 552 597 continue; 598 LND; 553 599 554 600 // try again (maybe we should prefer other servers if this 555 601 // happens more than once) … … 559 605 break; 560 606 } 561 607 } 608 LND; 609 trace_dhcp_state(state); 562 610 563 611 close(socket); 564 612 … … 582 630 fRenewalTime = (fLeaseTime - now) * 2/3 + now; 583 631 fRebindingTime = (fLeaseTime - now) * 5/6 + now; 584 632 } 633 LND; 585 634 586 635 return status; 587 636 } … … 819 868 dhcp_state state = _CurrentState(); 820 869 821 870 bigtime_t next; 871 LND; 872 trace_dhcp_state(state); 822 873 if (_Negotiate(state) == B_OK) { 823 874 switch (state) { 824 875 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);