1fb81684fSAxel Dörfler /* 237a68cb1SAxel Dörfler * Copyright 2006-2010, Haiku, Inc. All Rights Reserved. 3fb81684fSAxel Dörfler * Distributed under the terms of the MIT License. 4fb81684fSAxel Dörfler * 5fb81684fSAxel Dörfler * Authors: 6fb81684fSAxel Dörfler * Axel Dörfler, axeld@pinc-software.de 70f87f52fSStephan Aßmus * Vegard Wærp, vegarwa@online.no 8fb81684fSAxel Dörfler */ 9fb81684fSAxel Dörfler 10fb81684fSAxel Dörfler 11fb81684fSAxel Dörfler #include "DHCPClient.h" 12fb81684fSAxel Dörfler 13f9af6566SAxel Dörfler #include <Message.h> 146cc7630fSAxel Dörfler #include <MessageRunner.h> 15af074561SAxel Dörfler #include <NetworkDevice.h> 16af074561SAxel Dörfler #include <NetworkInterface.h> 17f9af6566SAxel Dörfler 18f9af6566SAxel Dörfler #include <arpa/inet.h> 19fb81684fSAxel Dörfler #include <errno.h> 20fb81684fSAxel Dörfler #include <stdio.h> 21fb81684fSAxel Dörfler #include <string.h> 22293ed4feSAxel Dörfler #include <syslog.h> 2336bde12dSAxel Dörfler #include <sys/sockio.h> 24f9af6566SAxel Dörfler #include <sys/time.h> 25fb81684fSAxel Dörfler 26*4ac66051SAxel Dörfler #include <Debug.h> 27*4ac66051SAxel Dörfler #include <Message.h> 28*4ac66051SAxel Dörfler #include <MessageRunner.h> 29*4ac66051SAxel Dörfler 30*4ac66051SAxel Dörfler #include "NetServer.h" 31*4ac66051SAxel Dörfler 32fb81684fSAxel Dörfler 33fb81684fSAxel Dörfler // See RFC 2131 for DHCP, see RFC 1533 for BOOTP/DHCP options 34fb81684fSAxel Dörfler 35fb81684fSAxel Dörfler #define DHCP_CLIENT_PORT 68 36fb81684fSAxel Dörfler #define DHCP_SERVER_PORT 67 37fb81684fSAxel Dörfler 38f9af6566SAxel Dörfler #define DEFAULT_TIMEOUT 2 // secs 39f9af6566SAxel Dörfler #define MAX_TIMEOUT 15 // secs 40f9af6566SAxel Dörfler 41fb81684fSAxel Dörfler enum message_opcode { 42fb81684fSAxel Dörfler BOOT_REQUEST = 1, 43fb81684fSAxel Dörfler BOOT_REPLY 44fb81684fSAxel Dörfler }; 45fb81684fSAxel Dörfler 46fb81684fSAxel Dörfler enum message_option { 47fb81684fSAxel Dörfler OPTION_MAGIC = 0x63825363, 48fb81684fSAxel Dörfler 49fb81684fSAxel Dörfler // generic options 50fb81684fSAxel Dörfler OPTION_PAD = 0, 51f9af6566SAxel Dörfler OPTION_END = 255, 52f9af6566SAxel Dörfler OPTION_SUBNET_MASK = 1, 5365186fecSAxel Dörfler OPTION_TIME_OFFSET = 2, 54f9af6566SAxel Dörfler OPTION_ROUTER_ADDRESS = 3, 55f9af6566SAxel Dörfler OPTION_DOMAIN_NAME_SERVER = 6, 56f9af6566SAxel Dörfler OPTION_HOST_NAME = 12, 575782c5a3SAxel Dörfler OPTION_DOMAIN_NAME = 15, 58fb81684fSAxel Dörfler OPTION_DATAGRAM_SIZE = 22, 59fb81684fSAxel Dörfler OPTION_MTU = 26, 60fb81684fSAxel Dörfler OPTION_BROADCAST_ADDRESS = 28, 61fb81684fSAxel Dörfler OPTION_NETWORK_TIME_SERVERS = 42, 6265186fecSAxel Dörfler OPTION_NETBIOS_NAME_SERVER = 44, 6365186fecSAxel Dörfler OPTION_NETBIOS_SCOPE = 47, 64fb81684fSAxel Dörfler 65fb81684fSAxel Dörfler // DHCP specific options 66fb81684fSAxel Dörfler OPTION_REQUEST_IP_ADDRESS = 50, 67fb81684fSAxel Dörfler OPTION_ADDRESS_LEASE_TIME = 51, 68fb81684fSAxel Dörfler OPTION_OVERLOAD = 52, 69fb81684fSAxel Dörfler OPTION_MESSAGE_TYPE = 53, 70fb81684fSAxel Dörfler OPTION_SERVER_ADDRESS = 54, 71fb81684fSAxel Dörfler OPTION_REQUEST_PARAMETERS = 55, 72fb81684fSAxel Dörfler OPTION_ERROR_MESSAGE = 56, 73fb81684fSAxel Dörfler OPTION_MESSAGE_SIZE = 57, 74fb81684fSAxel Dörfler OPTION_RENEWAL_TIME = 58, 75fb81684fSAxel Dörfler OPTION_REBINDING_TIME = 59, 76fb81684fSAxel Dörfler OPTION_CLASS_IDENTIFIER = 60, 77fb81684fSAxel Dörfler OPTION_CLIENT_IDENTIFIER = 61, 78fb81684fSAxel Dörfler }; 79fb81684fSAxel Dörfler 80fb81684fSAxel Dörfler enum message_type { 81f9af6566SAxel Dörfler DHCP_NONE = 0, 82f9af6566SAxel Dörfler DHCP_DISCOVER, 83fb81684fSAxel Dörfler DHCP_OFFER, 84fb81684fSAxel Dörfler DHCP_REQUEST, 85fb81684fSAxel Dörfler DHCP_DECLINE, 86fb81684fSAxel Dörfler DHCP_ACK, 87f9af6566SAxel Dörfler DHCP_NACK, 88fb81684fSAxel Dörfler DHCP_RELEASE, 89fb81684fSAxel Dörfler DHCP_INFORM 90fb81684fSAxel Dörfler }; 91fb81684fSAxel Dörfler 92fb81684fSAxel Dörfler struct dhcp_option_cookie { 9315ab0bcfSAxel Dörfler dhcp_option_cookie() 9415ab0bcfSAxel Dörfler : 9515ab0bcfSAxel Dörfler state(0), 9615ab0bcfSAxel Dörfler file_has_options(false), 9715ab0bcfSAxel Dörfler server_name_has_options(false) 9815ab0bcfSAxel Dörfler { 9915ab0bcfSAxel Dörfler } 100fb81684fSAxel Dörfler 101fb81684fSAxel Dörfler const uint8* next; 102fb81684fSAxel Dörfler uint8 state; 103fb81684fSAxel Dörfler bool file_has_options; 104fb81684fSAxel Dörfler bool server_name_has_options; 105fb81684fSAxel Dörfler }; 106fb81684fSAxel Dörfler 107fb81684fSAxel Dörfler struct dhcp_message { 108fb81684fSAxel Dörfler dhcp_message(message_type type); 109fb81684fSAxel Dörfler 110fb81684fSAxel Dörfler uint8 opcode; 111fb81684fSAxel Dörfler uint8 hardware_type; 112fb81684fSAxel Dörfler uint8 hardware_address_length; 113fb81684fSAxel Dörfler uint8 hop_count; 114fb81684fSAxel Dörfler uint32 transaction_id; 11546ff5400SAxel Dörfler uint16 seconds_since_start; 116fb81684fSAxel Dörfler uint16 flags; 117fb81684fSAxel Dörfler in_addr_t client_address; 118fb81684fSAxel Dörfler in_addr_t your_address; 119fb81684fSAxel Dörfler in_addr_t server_address; 120fb81684fSAxel Dörfler in_addr_t gateway_address; 121fb81684fSAxel Dörfler uint8 mac_address[16]; 122fb81684fSAxel Dörfler uint8 server_name[64]; 123fb81684fSAxel Dörfler uint8 file[128]; 124fb81684fSAxel Dörfler uint32 options_magic; 125fb81684fSAxel Dörfler uint8 options[1260]; 126fb81684fSAxel Dörfler 127fb81684fSAxel Dörfler size_t MinSize() const { return 576; } 128fb81684fSAxel Dörfler size_t Size() const; 129fb81684fSAxel Dörfler 130fb81684fSAxel Dörfler bool HasOptions() const; 131fb81684fSAxel Dörfler bool NextOption(dhcp_option_cookie& cookie, message_option& option, 132fb81684fSAxel Dörfler const uint8*& data, size_t& size) const; 133f9af6566SAxel Dörfler message_type Type() const; 134fb81684fSAxel Dörfler const uint8* LastOption() const; 135fb81684fSAxel Dörfler 136a073ba1aSHugo Santos uint8* PrepareMessage(uint8 type); 137fb81684fSAxel Dörfler uint8* PutOption(uint8* options, message_option option); 138fb81684fSAxel Dörfler uint8* PutOption(uint8* options, message_option option, uint8 data); 139fb81684fSAxel Dörfler uint8* PutOption(uint8* options, message_option option, uint16 data); 140fb81684fSAxel Dörfler uint8* PutOption(uint8* options, message_option option, uint32 data); 14115ab0bcfSAxel Dörfler uint8* PutOption(uint8* options, message_option option, const uint8* data, 14215ab0bcfSAxel Dörfler uint32 size); 14337a68cb1SAxel Dörfler uint8* FinishOptions(uint8* options); 144*4ac66051SAxel Dörfler 145*4ac66051SAxel Dörfler static const char* TypeToString(message_type type); 146fb81684fSAxel Dörfler } _PACKED; 147fb81684fSAxel Dörfler 1480cf5e6acSAxel Dörfler #define DHCP_FLAG_BROADCAST 0x8000 149fb81684fSAxel Dörfler 150fb81684fSAxel Dörfler #define ARP_HARDWARE_TYPE_ETHER 1 151fb81684fSAxel Dörfler 1526cc7630fSAxel Dörfler const uint32 kMsgLeaseTime = 'lstm'; 1536cc7630fSAxel Dörfler 15437a68cb1SAxel Dörfler static const uint8 kRequestParameters[] = { 15565186fecSAxel Dörfler OPTION_SUBNET_MASK, OPTION_ROUTER_ADDRESS, 156bfd479b3SAxel Dörfler OPTION_DOMAIN_NAME_SERVER, OPTION_BROADCAST_ADDRESS, 157bfd479b3SAxel Dörfler OPTION_DOMAIN_NAME 1585d4d5313SHugo Santos }; 1595d4d5313SHugo Santos 160fb81684fSAxel Dörfler 161fb81684fSAxel Dörfler dhcp_message::dhcp_message(message_type type) 162fb81684fSAxel Dörfler { 163*4ac66051SAxel Dörfler ASSERT(this == offsetof(this, opcode)); 164fb81684fSAxel Dörfler memset(this, 0, sizeof(*this)); 165fb81684fSAxel Dörfler options_magic = htonl(OPTION_MAGIC); 166fb81684fSAxel Dörfler 167a073ba1aSHugo Santos uint8* next = PrepareMessage(type); 168a073ba1aSHugo Santos FinishOptions(next); 169fb81684fSAxel Dörfler } 170fb81684fSAxel Dörfler 171fb81684fSAxel Dörfler 172fb81684fSAxel Dörfler bool 173fb81684fSAxel Dörfler dhcp_message::HasOptions() const 174fb81684fSAxel Dörfler { 175fb81684fSAxel Dörfler return options_magic == htonl(OPTION_MAGIC); 176fb81684fSAxel Dörfler } 177fb81684fSAxel Dörfler 178fb81684fSAxel Dörfler 179fb81684fSAxel Dörfler bool 180fb81684fSAxel Dörfler dhcp_message::NextOption(dhcp_option_cookie& cookie, 181fb81684fSAxel Dörfler message_option& option, const uint8*& data, size_t& size) const 182fb81684fSAxel Dörfler { 183fb81684fSAxel Dörfler if (cookie.state == 0) { 184fb81684fSAxel Dörfler if (!HasOptions()) 185fb81684fSAxel Dörfler return false; 186fb81684fSAxel Dörfler 187fb81684fSAxel Dörfler cookie.state++; 188fb81684fSAxel Dörfler cookie.next = options; 189fb81684fSAxel Dörfler } 190fb81684fSAxel Dörfler 191fb81684fSAxel Dörfler uint32 bytesLeft = 0; 192fb81684fSAxel Dörfler 193fb81684fSAxel Dörfler switch (cookie.state) { 194fb81684fSAxel Dörfler case 1: 195fb81684fSAxel Dörfler // options from "options" 196fb81684fSAxel Dörfler bytesLeft = sizeof(options) + cookie.next - options; 197fb81684fSAxel Dörfler break; 198fb81684fSAxel Dörfler 199fb81684fSAxel Dörfler case 2: 200fb81684fSAxel Dörfler // options from "file" 201fb81684fSAxel Dörfler bytesLeft = sizeof(options) + cookie.next - options; 202fb81684fSAxel Dörfler break; 203fb81684fSAxel Dörfler 204fb81684fSAxel Dörfler case 3: 205fb81684fSAxel Dörfler // options from "server_name" 206fb81684fSAxel Dörfler bytesLeft = sizeof(options) + cookie.next - options; 207fb81684fSAxel Dörfler break; 208fb81684fSAxel Dörfler } 209fb81684fSAxel Dörfler 210fb81684fSAxel Dörfler while (true) { 211fb81684fSAxel Dörfler if (bytesLeft == 0) { 212fb81684fSAxel Dörfler // TODO: suppport OPTION_OVERLOAD! 213fb81684fSAxel Dörfler cookie.state = 4; 214fb81684fSAxel Dörfler return false; 215fb81684fSAxel Dörfler } 216fb81684fSAxel Dörfler 217fb81684fSAxel Dörfler option = (message_option)cookie.next[0]; 218fb81684fSAxel Dörfler if (option == OPTION_END) { 219fb81684fSAxel Dörfler cookie.state = 4; 220fb81684fSAxel Dörfler return false; 221fb81684fSAxel Dörfler } else if (option == OPTION_PAD) { 222fb81684fSAxel Dörfler bytesLeft--; 223fb81684fSAxel Dörfler cookie.next++; 224fb81684fSAxel Dörfler continue; 225fb81684fSAxel Dörfler } 226fb81684fSAxel Dörfler 227fb81684fSAxel Dörfler size = cookie.next[1]; 228fb81684fSAxel Dörfler data = &cookie.next[2]; 229fb81684fSAxel Dörfler cookie.next += 2 + size; 230fb81684fSAxel Dörfler 231fb81684fSAxel Dörfler if (option == OPTION_OVERLOAD) { 232fb81684fSAxel Dörfler cookie.file_has_options = data[0] & 1; 233fb81684fSAxel Dörfler cookie.server_name_has_options = data[0] & 2; 234fb81684fSAxel Dörfler continue; 235fb81684fSAxel Dörfler } 236fb81684fSAxel Dörfler 237fb81684fSAxel Dörfler return true; 238fb81684fSAxel Dörfler } 239fb81684fSAxel Dörfler } 240fb81684fSAxel Dörfler 241fb81684fSAxel Dörfler 242f9af6566SAxel Dörfler message_type 243f9af6566SAxel Dörfler dhcp_message::Type() const 244f9af6566SAxel Dörfler { 245f9af6566SAxel Dörfler dhcp_option_cookie cookie; 246f9af6566SAxel Dörfler message_option option; 247f9af6566SAxel Dörfler const uint8* data; 248f9af6566SAxel Dörfler size_t size; 249f9af6566SAxel Dörfler while (NextOption(cookie, option, data, size)) { 250f9af6566SAxel Dörfler // iterate through all options 251f9af6566SAxel Dörfler if (option == OPTION_MESSAGE_TYPE) 252f9af6566SAxel Dörfler return (message_type)data[0]; 253f9af6566SAxel Dörfler } 254f9af6566SAxel Dörfler 255f9af6566SAxel Dörfler return DHCP_NONE; 256f9af6566SAxel Dörfler } 257f9af6566SAxel Dörfler 258f9af6566SAxel Dörfler 259fb81684fSAxel Dörfler const uint8* 260fb81684fSAxel Dörfler dhcp_message::LastOption() const 261fb81684fSAxel Dörfler { 262fb81684fSAxel Dörfler dhcp_option_cookie cookie; 263fb81684fSAxel Dörfler message_option option; 264fb81684fSAxel Dörfler const uint8* data; 265fb81684fSAxel Dörfler size_t size; 266fb81684fSAxel Dörfler while (NextOption(cookie, option, data, size)) { 267fb81684fSAxel Dörfler // iterate through all options 268fb81684fSAxel Dörfler } 269fb81684fSAxel Dörfler 270fb81684fSAxel Dörfler return cookie.next; 271fb81684fSAxel Dörfler } 272fb81684fSAxel Dörfler 273fb81684fSAxel Dörfler 274fb81684fSAxel Dörfler size_t 275fb81684fSAxel Dörfler dhcp_message::Size() const 276fb81684fSAxel Dörfler { 277fb81684fSAxel Dörfler const uint8* last = LastOption(); 278fb81684fSAxel Dörfler return sizeof(dhcp_message) - sizeof(options) + last + 1 - options; 279fb81684fSAxel Dörfler } 280fb81684fSAxel Dörfler 281fb81684fSAxel Dörfler 282fb81684fSAxel Dörfler uint8* 283a073ba1aSHugo Santos dhcp_message::PrepareMessage(uint8 type) 284a073ba1aSHugo Santos { 285a073ba1aSHugo Santos uint8* next = options; 286a073ba1aSHugo Santos next = PutOption(next, OPTION_MESSAGE_TYPE, type); 28715ab0bcfSAxel Dörfler next = PutOption(next, OPTION_MESSAGE_SIZE, 28815ab0bcfSAxel Dörfler (uint16)htons(sizeof(dhcp_message))); 289a073ba1aSHugo Santos return next; 290a073ba1aSHugo Santos } 291a073ba1aSHugo Santos 29265186fecSAxel Dörfler 293a073ba1aSHugo Santos uint8* 294fb81684fSAxel Dörfler dhcp_message::PutOption(uint8* options, message_option option) 295fb81684fSAxel Dörfler { 296fb81684fSAxel Dörfler options[0] = option; 297fb81684fSAxel Dörfler return options + 1; 298fb81684fSAxel Dörfler } 299fb81684fSAxel Dörfler 300fb81684fSAxel Dörfler 301fb81684fSAxel Dörfler uint8* 302fb81684fSAxel Dörfler dhcp_message::PutOption(uint8* options, message_option option, uint8 data) 303fb81684fSAxel Dörfler { 304fb81684fSAxel Dörfler return PutOption(options, option, &data, 1); 305fb81684fSAxel Dörfler } 306fb81684fSAxel Dörfler 307fb81684fSAxel Dörfler 308fb81684fSAxel Dörfler uint8* 309fb81684fSAxel Dörfler dhcp_message::PutOption(uint8* options, message_option option, uint16 data) 310fb81684fSAxel Dörfler { 311fb81684fSAxel Dörfler return PutOption(options, option, (uint8*)&data, sizeof(data)); 312fb81684fSAxel Dörfler } 313fb81684fSAxel Dörfler 314fb81684fSAxel Dörfler 315fb81684fSAxel Dörfler uint8* 316fb81684fSAxel Dörfler dhcp_message::PutOption(uint8* options, message_option option, uint32 data) 317fb81684fSAxel Dörfler { 318fb81684fSAxel Dörfler return PutOption(options, option, (uint8*)&data, sizeof(data)); 319fb81684fSAxel Dörfler } 320fb81684fSAxel Dörfler 321fb81684fSAxel Dörfler 322fb81684fSAxel Dörfler uint8* 32315ab0bcfSAxel Dörfler dhcp_message::PutOption(uint8* options, message_option option, 32415ab0bcfSAxel Dörfler const uint8* data, uint32 size) 325fb81684fSAxel Dörfler { 326fb81684fSAxel Dörfler options[0] = option; 327fb81684fSAxel Dörfler options[1] = size; 328fb81684fSAxel Dörfler memcpy(&options[2], data, size); 329fb81684fSAxel Dörfler 330fb81684fSAxel Dörfler return options + 2 + size; 331fb81684fSAxel Dörfler } 332fb81684fSAxel Dörfler 333fb81684fSAxel Dörfler 334a073ba1aSHugo Santos uint8* 33537a68cb1SAxel Dörfler dhcp_message::FinishOptions(uint8* options) 336a073ba1aSHugo Santos { 33737a68cb1SAxel Dörfler return PutOption(options, OPTION_END); 338a073ba1aSHugo Santos } 339a073ba1aSHugo Santos 340a073ba1aSHugo Santos 341*4ac66051SAxel Dörfler /*static*/ const char* 342*4ac66051SAxel Dörfler dhcp_message::TypeToString(message_type type) 343*4ac66051SAxel Dörfler { 344*4ac66051SAxel Dörfler switch (type) { 345*4ac66051SAxel Dörfler #define CASE(x) case x: return #x; 346*4ac66051SAxel Dörfler CASE(DHCP_NONE) 347*4ac66051SAxel Dörfler CASE(DHCP_DISCOVER) 348*4ac66051SAxel Dörfler CASE(DHCP_OFFER) 349*4ac66051SAxel Dörfler CASE(DHCP_REQUEST) 350*4ac66051SAxel Dörfler CASE(DHCP_DECLINE) 351*4ac66051SAxel Dörfler CASE(DHCP_ACK) 352*4ac66051SAxel Dörfler CASE(DHCP_NACK) 353*4ac66051SAxel Dörfler CASE(DHCP_RELEASE) 354*4ac66051SAxel Dörfler CASE(DHCP_INFORM) 355*4ac66051SAxel Dörfler #undef CASE 356*4ac66051SAxel Dörfler } 357*4ac66051SAxel Dörfler 358*4ac66051SAxel Dörfler return "<unknown>"; 359*4ac66051SAxel Dörfler } 360*4ac66051SAxel Dörfler 361*4ac66051SAxel Dörfler 362fb81684fSAxel Dörfler // #pragma mark - 363fb81684fSAxel Dörfler 364fb81684fSAxel Dörfler 365f9af6566SAxel Dörfler DHCPClient::DHCPClient(BMessenger target, const char* device) 366af074561SAxel Dörfler : 367af074561SAxel Dörfler AutoconfigClient("dhcp", target, device), 3686cc7630fSAxel Dörfler fConfiguration(kMsgConfigureInterface), 36916e8f13aSAxel Dörfler fResolverConfiguration(kMsgConfigureResolver), 3706cc7630fSAxel Dörfler fRunner(NULL), 371af074561SAxel Dörfler fServer(AF_INET, NULL, DHCP_SERVER_PORT), 372c1264353SStephan Aßmus fLeaseTime(0) 373fb81684fSAxel Dörfler { 37446ff5400SAxel Dörfler fStartTime = system_time(); 37546ff5400SAxel Dörfler fTransactionID = (uint32)fStartTime; 376fb81684fSAxel Dörfler 377af074561SAxel Dörfler BNetworkAddress link; 378af074561SAxel Dörfler BNetworkInterface interface(device); 379af074561SAxel Dörfler fStatus = interface.GetHardwareAddress(link); 380af074561SAxel Dörfler if (fStatus != B_OK) 381fb81684fSAxel Dörfler return; 382fb81684fSAxel Dörfler 383af074561SAxel Dörfler memcpy(fMAC, link.LinkLevelAddress(), sizeof(fMAC)); 384293ed4feSAxel Dörfler 385293ed4feSAxel Dörfler openlog_thread("DHCP", 0, LOG_DAEMON); 3866cc7630fSAxel Dörfler } 3876cc7630fSAxel Dörfler 3886cc7630fSAxel Dörfler 3896cc7630fSAxel Dörfler DHCPClient::~DHCPClient() 3906cc7630fSAxel Dörfler { 3916cc7630fSAxel Dörfler if (fStatus != B_OK) 3926cc7630fSAxel Dörfler return; 3936cc7630fSAxel Dörfler 3946cc7630fSAxel Dörfler delete fRunner; 3950ce7725eSAxel Dörfler 396fb81684fSAxel Dörfler int socket = ::socket(AF_INET, SOCK_DGRAM, 0); 3976cc7630fSAxel Dörfler if (socket < 0) 398fb81684fSAxel Dörfler return; 3996cc7630fSAxel Dörfler 4006cc7630fSAxel Dörfler // release lease 4016cc7630fSAxel Dörfler 4026cc7630fSAxel Dörfler dhcp_message release(DHCP_RELEASE); 40346ff5400SAxel Dörfler _PrepareMessage(release, BOUND); 4046cc7630fSAxel Dörfler 4056cc7630fSAxel Dörfler _SendMessage(socket, release, fServer); 4066cc7630fSAxel Dörfler close(socket); 407293ed4feSAxel Dörfler 408293ed4feSAxel Dörfler closelog_thread(); 409fb81684fSAxel Dörfler } 410fb81684fSAxel Dörfler 4116cc7630fSAxel Dörfler 4126cc7630fSAxel Dörfler status_t 4136cc7630fSAxel Dörfler DHCPClient::Initialize() 4146cc7630fSAxel Dörfler { 4156cc7630fSAxel Dörfler fStatus = _Negotiate(INIT); 416293ed4feSAxel Dörfler syslog(LOG_DEBUG, "DHCP for %s, status: %s\n", Device(), strerror(fStatus)); 4176cc7630fSAxel Dörfler return fStatus; 4186cc7630fSAxel Dörfler } 4196cc7630fSAxel Dörfler 4206cc7630fSAxel Dörfler 4216cc7630fSAxel Dörfler status_t 4226cc7630fSAxel Dörfler DHCPClient::_Negotiate(dhcp_state state) 4236cc7630fSAxel Dörfler { 4246cc7630fSAxel Dörfler int socket = ::socket(AF_INET, SOCK_DGRAM, 0); 4256cc7630fSAxel Dörfler if (socket < 0) 4266cc7630fSAxel Dörfler return errno; 4276cc7630fSAxel Dörfler 428af074561SAxel Dörfler BNetworkAddress local(AF_INET, NULL, DHCP_CLIENT_PORT); 429fb81684fSAxel Dörfler 4302b829b04SBruno G. Albuquerque // Enable reusing the port . This is needed in case there is more 4312b829b04SBruno G. Albuquerque // than 1 interface that needs to be configured. Note that the only reason 4322b829b04SBruno G. Albuquerque // this works is because there is code below to bind to a specific 4332b829b04SBruno G. Albuquerque // interface. 4342b829b04SBruno G. Albuquerque int option = 1; 4352b829b04SBruno G. Albuquerque setsockopt(socket, SOL_SOCKET, SO_REUSEPORT, &option, sizeof(option)); 4362b829b04SBruno G. Albuquerque 437af074561SAxel Dörfler if (bind(socket, local, local.Length()) < 0) { 438fb81684fSAxel Dörfler close(socket); 4396cc7630fSAxel Dörfler return errno; 440fb81684fSAxel Dörfler } 441fb81684fSAxel Dörfler 442af074561SAxel Dörfler BNetworkAddress broadcast; 443af074561SAxel Dörfler broadcast.SetToBroadcast(AF_INET, DHCP_SERVER_PORT); 444fb81684fSAxel Dörfler 4452b829b04SBruno G. Albuquerque option = 1; 446fb81684fSAxel Dörfler setsockopt(socket, SOL_SOCKET, SO_BROADCAST, &option, sizeof(option)); 447fb81684fSAxel Dörfler 44836bde12dSAxel Dörfler if (state == INIT) { 44936bde12dSAxel Dörfler // The local interface does not have an address yet, bind the socket 45036bde12dSAxel Dörfler // to the device directly. 451af074561SAxel Dörfler BNetworkDevice device(Device()); 452af074561SAxel Dörfler int index = device.Index(); 45336bde12dSAxel Dörfler 454af074561SAxel Dörfler setsockopt(socket, SOL_SOCKET, SO_BINDTODEVICE, &index, sizeof(int)); 45536bde12dSAxel Dörfler } 45636bde12dSAxel Dörfler 4576cc7630fSAxel Dörfler bigtime_t previousLeaseTime = fLeaseTime; 4586cc7630fSAxel Dörfler fLeaseTime = 0; 45946ff5400SAxel Dörfler fRenewalTime = 0; 46046ff5400SAxel Dörfler fRebindingTime = 0; 461fb81684fSAxel Dörfler 4626cc7630fSAxel Dörfler status_t status = B_ERROR; 4636cc7630fSAxel Dörfler time_t timeout; 4646cc7630fSAxel Dörfler uint32 tries; 4656cc7630fSAxel Dörfler _ResetTimeout(socket, timeout, tries); 4666cc7630fSAxel Dörfler 4676cc7630fSAxel Dörfler dhcp_message discover(DHCP_DISCOVER); 46846ff5400SAxel Dörfler _PrepareMessage(discover, state); 4696cc7630fSAxel Dörfler 4706cc7630fSAxel Dörfler dhcp_message request(DHCP_REQUEST); 47146ff5400SAxel Dörfler _PrepareMessage(request, state); 4726cc7630fSAxel Dörfler 47346ff5400SAxel Dörfler // send discover/request message 474593a0d2dSAxel Dörfler _SendMessage(socket, state == INIT ? discover : request, 47546ff5400SAxel Dörfler state != RENEWAL ? broadcast : fServer); 476593a0d2dSAxel Dörfler // no need to check the status; in case of an error we'll just send 477593a0d2dSAxel Dörfler // the message again 478fb81684fSAxel Dörfler 479f9af6566SAxel Dörfler // receive loop until we've got an offer and acknowledged it 480f9af6566SAxel Dörfler 481f9af6566SAxel Dörfler while (state != ACKNOWLEDGED) { 482f9af6566SAxel Dörfler char buffer[2048]; 483f9af6566SAxel Dörfler ssize_t bytesReceived = recvfrom(socket, buffer, sizeof(buffer), 484f9af6566SAxel Dörfler 0, NULL, NULL); 4854b661a95SAxel Dörfler if (bytesReceived < 0 && errno == B_TIMED_OUT) { 486f9af6566SAxel Dörfler // depending on the state, we'll just try again 4876cc7630fSAxel Dörfler if (!_TimeoutShift(socket, timeout, tries)) { 4886cc7630fSAxel Dörfler close(socket); 4896cc7630fSAxel Dörfler return B_TIMED_OUT; 490f9af6566SAxel Dörfler } 491f9af6566SAxel Dörfler 492f9af6566SAxel Dörfler if (state == INIT) 493593a0d2dSAxel Dörfler _SendMessage(socket, discover, broadcast); 49415ab0bcfSAxel Dörfler else { 495593a0d2dSAxel Dörfler _SendMessage(socket, request, 496593a0d2dSAxel Dörfler state != RENEWAL ? broadcast : fServer); 49715ab0bcfSAxel Dörfler } 498f9af6566SAxel Dörfler 499d77fe260SAxel Dörfler continue; 500593a0d2dSAxel Dörfler } else if (bytesReceived < 0) 501f9af6566SAxel Dörfler break; 502f9af6566SAxel Dörfler 503f9af6566SAxel Dörfler dhcp_message* message = (dhcp_message*)buffer; 504f9af6566SAxel Dörfler if (message->transaction_id != htonl(fTransactionID) 505f9af6566SAxel Dörfler || !message->HasOptions() 506f9af6566SAxel Dörfler || memcmp(message->mac_address, discover.mac_address, 507f9af6566SAxel Dörfler discover.hardware_address_length)) { 508f9af6566SAxel Dörfler // this message is not for us 509f9af6566SAxel Dörfler continue; 510f9af6566SAxel Dörfler } 511f9af6566SAxel Dörfler 512*4ac66051SAxel Dörfler syslog(LOG_DEBUG, "DHCP received %s for %s\n", 513*4ac66051SAxel Dörfler dhcp_message::TypeToString(message->Type()), Device()); 514*4ac66051SAxel Dörfler 515f9af6566SAxel Dörfler switch (message->Type()) { 516f9af6566SAxel Dörfler case DHCP_NONE: 517f9af6566SAxel Dörfler default: 518f9af6566SAxel Dörfler // ignore this message 519f9af6566SAxel Dörfler break; 520f9af6566SAxel Dörfler 521f9af6566SAxel Dörfler case DHCP_OFFER: 522f9af6566SAxel Dörfler { 523f9af6566SAxel Dörfler // first offer wins 524f9af6566SAxel Dörfler if (state != INIT) 525f9af6566SAxel Dörfler break; 526f9af6566SAxel Dörfler 527f9af6566SAxel Dörfler // collect interface options 528f9af6566SAxel Dörfler 529f9af6566SAxel Dörfler fAssignedAddress = message->your_address; 530f9af6566SAxel Dörfler 53110cc12daSAxel Dörfler fConfiguration.MakeEmpty(); 532293ed4feSAxel Dörfler fConfiguration.AddString("device", Device()); 533f01106c3SAxel Dörfler fConfiguration.AddBool("auto", true); 534f9af6566SAxel Dörfler 535f9af6566SAxel Dörfler BMessage address; 536f9af6566SAxel Dörfler address.AddString("family", "inet"); 537f9af6566SAxel Dörfler address.AddString("address", _ToString(fAssignedAddress)); 53816e8f13aSAxel Dörfler fResolverConfiguration.MakeEmpty(); 53916e8f13aSAxel Dörfler _ParseOptions(*message, address, fResolverConfiguration); 540f9af6566SAxel Dörfler 54110cc12daSAxel Dörfler fConfiguration.AddMessage("address", &address); 542f9af6566SAxel Dörfler 54310cc12daSAxel Dörfler // request configuration from the server 544f9af6566SAxel Dörfler 5456cc7630fSAxel Dörfler _ResetTimeout(socket, timeout, tries); 546f9af6566SAxel Dörfler state = REQUESTING; 54746ff5400SAxel Dörfler _PrepareMessage(request, state); 548f9af6566SAxel Dörfler 5496cc7630fSAxel Dörfler status = _SendMessage(socket, request, broadcast); 55015ab0bcfSAxel Dörfler // we're sending a broadcast so that all potential offers 55115ab0bcfSAxel Dörfler // get an answer 55210cc12daSAxel Dörfler break; 553f9af6566SAxel Dörfler } 554f9af6566SAxel Dörfler 555f9af6566SAxel Dörfler case DHCP_ACK: 55610cc12daSAxel Dörfler { 55715ab0bcfSAxel Dörfler if (state != REQUESTING && state != REBINDING 55815ab0bcfSAxel Dörfler && state != RENEWAL) 559f9af6566SAxel Dörfler continue; 560f9af6566SAxel Dörfler 5616cc7630fSAxel Dörfler // TODO: we might want to configure the stuff, don't we? 5626cc7630fSAxel Dörfler BMessage address; 56316e8f13aSAxel Dörfler fResolverConfiguration.MakeEmpty(); 56416e8f13aSAxel Dörfler _ParseOptions(*message, address, fResolverConfiguration); 565*4ac66051SAxel Dörfler // TODO: currently, only lease time and DNS is updated this 566*4ac66051SAxel Dörfler // way 5676cc7630fSAxel Dörfler 568f9af6566SAxel Dörfler // our address request has been acknowledged 569f9af6566SAxel Dörfler state = ACKNOWLEDGED; 57010cc12daSAxel Dörfler 57110cc12daSAxel Dörfler // configure interface 57210cc12daSAxel Dörfler BMessage reply; 573d23fdd96SOliver Tappe status = Target().SendMessage(&fConfiguration, &reply); 574d23fdd96SOliver Tappe if (status == B_OK) 575d23fdd96SOliver Tappe status = reply.FindInt32("status", &fStatus); 57616e8f13aSAxel Dörfler 57716e8f13aSAxel Dörfler // configure resolver 57816e8f13aSAxel Dörfler reply.MakeEmpty(); 57916e8f13aSAxel Dörfler status = Target().SendMessage(&fResolverConfiguration, &reply); 58016e8f13aSAxel Dörfler if (status == B_OK) 58116e8f13aSAxel Dörfler status = reply.FindInt32("status", &fStatus); 582f9af6566SAxel Dörfler break; 58310cc12daSAxel Dörfler } 584f9af6566SAxel Dörfler 585f9af6566SAxel Dörfler case DHCP_NACK: 586f9af6566SAxel Dörfler if (state != REQUESTING) 587f9af6566SAxel Dörfler continue; 588f9af6566SAxel Dörfler 58915ab0bcfSAxel Dörfler // try again (maybe we should prefer other servers if this 59015ab0bcfSAxel Dörfler // happens more than once) 5916cc7630fSAxel Dörfler status = _SendMessage(socket, discover, broadcast); 5926cc7630fSAxel Dörfler if (status == B_OK) 593f9af6566SAxel Dörfler state = INIT; 594f9af6566SAxel Dörfler break; 595f9af6566SAxel Dörfler } 596f9af6566SAxel Dörfler } 597f9af6566SAxel Dörfler 598fb81684fSAxel Dörfler close(socket); 5996cc7630fSAxel Dörfler 6006cc7630fSAxel Dörfler if (status == B_OK && fLeaseTime > 0) { 6016cc7630fSAxel Dörfler // notify early enough when the lease is 60246ff5400SAxel Dörfler if (fRenewalTime == 0) 60346ff5400SAxel Dörfler fRenewalTime = fLeaseTime * 2/3; 60446ff5400SAxel Dörfler if (fRebindingTime == 0) 60546ff5400SAxel Dörfler fRebindingTime = fLeaseTime * 5/6; 60646ff5400SAxel Dörfler 607168cc3dfSRene Gollent bigtime_t now = system_time(); 60846ff5400SAxel Dörfler _RestartLease(fRenewalTime); 60946ff5400SAxel Dörfler 61046ff5400SAxel Dörfler fLeaseTime += now; 61146ff5400SAxel Dörfler fRenewalTime += now; 61246ff5400SAxel Dörfler fRebindingTime += now; 61346ff5400SAxel Dörfler // make lease times absolute 61446ff5400SAxel Dörfler } else { 6156cc7630fSAxel Dörfler fLeaseTime = previousLeaseTime; 61646ff5400SAxel Dörfler bigtime_t now = system_time(); 61746ff5400SAxel Dörfler fRenewalTime = (fLeaseTime - now) * 2/3 + now; 61846ff5400SAxel Dörfler fRebindingTime = (fLeaseTime - now) * 5/6 + now; 61946ff5400SAxel Dörfler } 6206cc7630fSAxel Dörfler 6216cc7630fSAxel Dörfler return status; 622fb81684fSAxel Dörfler } 623fb81684fSAxel Dörfler 624fb81684fSAxel Dörfler 6256cc7630fSAxel Dörfler void 6266cc7630fSAxel Dörfler DHCPClient::_RestartLease(bigtime_t leaseTime) 627fb81684fSAxel Dörfler { 6286cc7630fSAxel Dörfler if (leaseTime == 0) 629f9af6566SAxel Dörfler return; 630f9af6566SAxel Dörfler 6316cc7630fSAxel Dörfler BMessage lease(kMsgLeaseTime); 63246ff5400SAxel Dörfler fRunner = new BMessageRunner(this, &lease, leaseTime, 1); 633f9af6566SAxel Dörfler } 634f9af6566SAxel Dörfler 635f9af6566SAxel Dörfler 636f9af6566SAxel Dörfler void 63716e8f13aSAxel Dörfler DHCPClient::_ParseOptions(dhcp_message& message, BMessage& address, 63816e8f13aSAxel Dörfler BMessage& resolverConfiguration) 6390ce7725eSAxel Dörfler { 6400ce7725eSAxel Dörfler dhcp_option_cookie cookie; 6410ce7725eSAxel Dörfler message_option option; 6420ce7725eSAxel Dörfler const uint8* data; 6430ce7725eSAxel Dörfler size_t size; 6440ce7725eSAxel Dörfler while (message.NextOption(cookie, option, data, size)) { 6450ce7725eSAxel Dörfler // iterate through all options 6460ce7725eSAxel Dörfler switch (option) { 6470ce7725eSAxel Dörfler case OPTION_ROUTER_ADDRESS: 6480ce7725eSAxel Dörfler address.AddString("gateway", _ToString(data)); 6490ce7725eSAxel Dörfler break; 6500ce7725eSAxel Dörfler case OPTION_SUBNET_MASK: 6510ce7725eSAxel Dörfler address.AddString("mask", _ToString(data)); 6520ce7725eSAxel Dörfler break; 6535782c5a3SAxel Dörfler case OPTION_BROADCAST_ADDRESS: 6545782c5a3SAxel Dörfler address.AddString("broadcast", _ToString(data)); 6555782c5a3SAxel Dörfler break; 6560ce7725eSAxel Dörfler case OPTION_DOMAIN_NAME_SERVER: 657a552ec13SAxel Dörfler { 6580ce7725eSAxel Dörfler for (uint32 i = 0; i < size / 4; i++) { 65961729d93SAxel Dörfler syslog(LOG_INFO, "DHCP for %s got DNS: %s\n", Device(), 660293ed4feSAxel Dörfler _ToString(&data[i * 4]).String()); 66116e8f13aSAxel Dörfler resolverConfiguration.AddString("nameserver", 66215ab0bcfSAxel Dörfler _ToString(&data[i * 4]).String()); 66315ab0bcfSAxel Dörfler } 66416e8f13aSAxel Dörfler resolverConfiguration.AddInt32("nameserver_count", 66516e8f13aSAxel Dörfler size / 4); 6660ce7725eSAxel Dörfler break; 667a552ec13SAxel Dörfler } 6680ce7725eSAxel Dörfler case OPTION_SERVER_ADDRESS: 669af074561SAxel Dörfler fServer.SetAddress(*(in_addr_t*)data); 6700ce7725eSAxel Dörfler break; 67146ff5400SAxel Dörfler 6720ce7725eSAxel Dörfler case OPTION_ADDRESS_LEASE_TIME: 673293ed4feSAxel Dörfler syslog(LOG_INFO, "lease time of %lu seconds\n", 674293ed4feSAxel Dörfler htonl(*(uint32*)data)); 6756cc7630fSAxel Dörfler fLeaseTime = htonl(*(uint32*)data) * 1000000LL; 6760ce7725eSAxel Dörfler break; 6771a4e8e7bSAxel Dörfler case OPTION_RENEWAL_TIME: 678293ed4feSAxel Dörfler syslog(LOG_INFO, "renewal time of %lu seconds\n", 67946ff5400SAxel Dörfler htonl(*(uint32*)data)); 68046ff5400SAxel Dörfler fRenewalTime = htonl(*(uint32*)data) * 1000000LL; 68146ff5400SAxel Dörfler break; 6821a4e8e7bSAxel Dörfler case OPTION_REBINDING_TIME: 683293ed4feSAxel Dörfler syslog(LOG_INFO, "rebinding time of %lu seconds\n", 68446ff5400SAxel Dörfler htonl(*(uint32*)data)); 68546ff5400SAxel Dörfler fRebindingTime = htonl(*(uint32*)data) * 1000000LL; 6861a4e8e7bSAxel Dörfler break; 6871a4e8e7bSAxel Dörfler 6880ce7725eSAxel Dörfler case OPTION_HOST_NAME: 68916e8f13aSAxel Dörfler syslog(LOG_INFO, "DHCP host name: \"%.*s\"\n", (int)size, 69016e8f13aSAxel Dörfler (const char*)data); 6910ce7725eSAxel Dörfler break; 6925782c5a3SAxel Dörfler 6935782c5a3SAxel Dörfler case OPTION_DOMAIN_NAME: 6945782c5a3SAxel Dörfler { 69516e8f13aSAxel Dörfler char domain[256]; 69616e8f13aSAxel Dörfler strlcpy(domain, (const char*)data, 69716e8f13aSAxel Dörfler min_c(size + 1, sizeof(domain))); 6980f87f52fSStephan Aßmus 69916e8f13aSAxel Dörfler syslog(LOG_INFO, "DHCP domain name: \"%s\"\n", domain); 7000f87f52fSStephan Aßmus 70116e8f13aSAxel Dörfler resolverConfiguration.AddString("domain", domain); 7025782c5a3SAxel Dörfler break; 7035782c5a3SAxel Dörfler } 7040ce7725eSAxel Dörfler 7050ce7725eSAxel Dörfler case OPTION_MESSAGE_TYPE: 7060ce7725eSAxel Dörfler break; 7070ce7725eSAxel Dörfler 7080ce7725eSAxel Dörfler default: 709293ed4feSAxel Dörfler syslog(LOG_INFO, "unknown option %lu\n", (uint32)option); 7100ce7725eSAxel Dörfler break; 7110ce7725eSAxel Dörfler } 7120ce7725eSAxel Dörfler } 7130ce7725eSAxel Dörfler } 7140ce7725eSAxel Dörfler 7150ce7725eSAxel Dörfler 7160ce7725eSAxel Dörfler void 71746ff5400SAxel Dörfler DHCPClient::_PrepareMessage(dhcp_message& message, dhcp_state state) 7180ce7725eSAxel Dörfler { 7190ce7725eSAxel Dörfler message.opcode = BOOT_REQUEST; 7200ce7725eSAxel Dörfler message.hardware_type = ARP_HARDWARE_TYPE_ETHER; 7210ce7725eSAxel Dörfler message.hardware_address_length = 6; 7220ce7725eSAxel Dörfler message.transaction_id = htonl(fTransactionID); 723bcc8dadaSAxel Dörfler message.seconds_since_start = htons(min_c((system_time() - fStartTime) 72415ab0bcfSAxel Dörfler / 1000000LL, 65535)); 7250ce7725eSAxel Dörfler memcpy(message.mac_address, fMAC, 6); 7260ce7725eSAxel Dörfler 72746ff5400SAxel Dörfler message_type type = message.Type(); 72846ff5400SAxel Dörfler 72946ff5400SAxel Dörfler switch (type) { 7300ce7725eSAxel Dörfler case DHCP_REQUEST: 7310ce7725eSAxel Dörfler case DHCP_RELEASE: 7320ce7725eSAxel Dörfler { 7330ce7725eSAxel Dörfler // add server identifier option 734af074561SAxel Dörfler const sockaddr_in& server = (sockaddr_in&)fServer.SockAddr(); 735a073ba1aSHugo Santos uint8* next = message.PrepareMessage(type); 7360ce7725eSAxel Dörfler next = message.PutOption(next, OPTION_SERVER_ADDRESS, 737af074561SAxel Dörfler (uint32)server.sin_addr.s_addr); 73846ff5400SAxel Dörfler 73946ff5400SAxel Dörfler // In RENEWAL or REBINDING state, we must set the client_address field, and not 74046ff5400SAxel Dörfler // use OPTION_REQUEST_IP_ADDRESS for DHCP_REQUEST messages 7415d4d5313SHugo Santos if (type == DHCP_REQUEST && (state == INIT || state == REQUESTING)) { 74215ab0bcfSAxel Dörfler next = message.PutOption(next, OPTION_REQUEST_IP_ADDRESS, 74315ab0bcfSAxel Dörfler (uint32)fAssignedAddress); 7445d4d5313SHugo Santos next = message.PutOption(next, OPTION_REQUEST_PARAMETERS, 74537a68cb1SAxel Dörfler kRequestParameters, sizeof(kRequestParameters)); 7465d4d5313SHugo Santos } else 74746ff5400SAxel Dörfler message.client_address = fAssignedAddress; 74846ff5400SAxel Dörfler 749a073ba1aSHugo Santos message.FinishOptions(next); 750a073ba1aSHugo Santos break; 751a073ba1aSHugo Santos } 752a073ba1aSHugo Santos 753a073ba1aSHugo Santos case DHCP_DISCOVER: 754a073ba1aSHugo Santos { 755a073ba1aSHugo Santos uint8* next = message.PrepareMessage(type); 756a073ba1aSHugo Santos next = message.PutOption(next, OPTION_REQUEST_PARAMETERS, 75737a68cb1SAxel Dörfler kRequestParameters, sizeof(kRequestParameters)); 758a073ba1aSHugo Santos message.FinishOptions(next); 7590ce7725eSAxel Dörfler break; 7600ce7725eSAxel Dörfler } 7610ce7725eSAxel Dörfler 7620ce7725eSAxel Dörfler default: 7630ce7725eSAxel Dörfler // the default options are fine 7640ce7725eSAxel Dörfler break; 7650ce7725eSAxel Dörfler } 7660ce7725eSAxel Dörfler } 7670ce7725eSAxel Dörfler 7680ce7725eSAxel Dörfler 7690ce7725eSAxel Dörfler void 7706cc7630fSAxel Dörfler DHCPClient::_ResetTimeout(int socket, time_t& timeout, uint32& tries) 771f9af6566SAxel Dörfler { 7726cc7630fSAxel Dörfler timeout = DEFAULT_TIMEOUT; 7736cc7630fSAxel Dörfler tries = 0; 774f9af6566SAxel Dörfler 775f9af6566SAxel Dörfler struct timeval value; 7766cc7630fSAxel Dörfler value.tv_sec = timeout; 777f9af6566SAxel Dörfler value.tv_usec = 0; 778f9af6566SAxel Dörfler setsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, &value, sizeof(value)); 779f9af6566SAxel Dörfler } 780f9af6566SAxel Dörfler 781f9af6566SAxel Dörfler 782f9af6566SAxel Dörfler bool 7836cc7630fSAxel Dörfler DHCPClient::_TimeoutShift(int socket, time_t& timeout, uint32& tries) 784f9af6566SAxel Dörfler { 7856cc7630fSAxel Dörfler timeout += timeout; 7866cc7630fSAxel Dörfler if (timeout > MAX_TIMEOUT) { 7876cc7630fSAxel Dörfler timeout = DEFAULT_TIMEOUT; 788f9af6566SAxel Dörfler 7896cc7630fSAxel Dörfler if (++tries > 2) 790f9af6566SAxel Dörfler return false; 791f9af6566SAxel Dörfler } 79261729d93SAxel Dörfler syslog(LOG_DEBUG, "DHCP timeout shift for %s: %lu secs (try %lu)\n", 79361729d93SAxel Dörfler Device(), timeout, tries); 794f9af6566SAxel Dörfler 795f9af6566SAxel Dörfler struct timeval value; 7966cc7630fSAxel Dörfler value.tv_sec = timeout; 797f9af6566SAxel Dörfler value.tv_usec = 0; 798f9af6566SAxel Dörfler setsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, &value, sizeof(value)); 799f9af6566SAxel Dörfler 800f9af6566SAxel Dörfler return true; 801f9af6566SAxel Dörfler } 802f9af6566SAxel Dörfler 803f9af6566SAxel Dörfler 804f9af6566SAxel Dörfler BString 805f9af6566SAxel Dörfler DHCPClient::_ToString(const uint8* data) const 806f9af6566SAxel Dörfler { 807f9af6566SAxel Dörfler BString target = inet_ntoa(*(in_addr*)data); 808f9af6566SAxel Dörfler return target; 809f9af6566SAxel Dörfler } 810f9af6566SAxel Dörfler 811f9af6566SAxel Dörfler 812f9af6566SAxel Dörfler BString 813f9af6566SAxel Dörfler DHCPClient::_ToString(in_addr_t address) const 814f9af6566SAxel Dörfler { 815f9af6566SAxel Dörfler BString target = inet_ntoa(*(in_addr*)&address); 816f9af6566SAxel Dörfler return target; 817f9af6566SAxel Dörfler } 818f9af6566SAxel Dörfler 819f9af6566SAxel Dörfler 820f9af6566SAxel Dörfler status_t 8211a905a76SAxel Dörfler DHCPClient::_SendMessage(int socket, dhcp_message& message, 822af074561SAxel Dörfler const BNetworkAddress& address) const 823f9af6566SAxel Dörfler { 824*4ac66051SAxel Dörfler syslog(LOG_DEBUG, "DHCP send message %s for %s\n", 825*4ac66051SAxel Dörfler dhcp_message::TypeToString(message.Type()), Device()); 82661729d93SAxel Dörfler 827f9af6566SAxel Dörfler ssize_t bytesSent = sendto(socket, &message, message.Size(), 828af074561SAxel Dörfler address.IsBroadcast() ? MSG_BCAST : 0, address, address.Length()); 829f9af6566SAxel Dörfler if (bytesSent < 0) 830f9af6566SAxel Dörfler return errno; 831f9af6566SAxel Dörfler 832f9af6566SAxel Dörfler return B_OK; 833fb81684fSAxel Dörfler } 834fb81684fSAxel Dörfler 835fb81684fSAxel Dörfler 83646ff5400SAxel Dörfler dhcp_state 83746ff5400SAxel Dörfler DHCPClient::_CurrentState() const 83846ff5400SAxel Dörfler { 83946ff5400SAxel Dörfler bigtime_t now = system_time(); 84046ff5400SAxel Dörfler 84146ff5400SAxel Dörfler if (now > fLeaseTime || fStatus < B_OK) 84246ff5400SAxel Dörfler return INIT; 84346ff5400SAxel Dörfler if (now >= fRebindingTime) 84446ff5400SAxel Dörfler return REBINDING; 84546ff5400SAxel Dörfler if (now >= fRenewalTime) 84646ff5400SAxel Dörfler return RENEWAL; 84746ff5400SAxel Dörfler 84846ff5400SAxel Dörfler return BOUND; 84946ff5400SAxel Dörfler } 85046ff5400SAxel Dörfler 85146ff5400SAxel Dörfler 852fb81684fSAxel Dörfler void 853fb81684fSAxel Dörfler DHCPClient::MessageReceived(BMessage* message) 854fb81684fSAxel Dörfler { 8556cc7630fSAxel Dörfler switch (message->what) { 8566cc7630fSAxel Dörfler case kMsgLeaseTime: 85746ff5400SAxel Dörfler { 85846ff5400SAxel Dörfler dhcp_state state = _CurrentState(); 85946ff5400SAxel Dörfler 86046ff5400SAxel Dörfler bigtime_t next; 86146ff5400SAxel Dörfler if (_Negotiate(state) == B_OK) { 86246ff5400SAxel Dörfler switch (state) { 86346ff5400SAxel Dörfler case RENEWAL: 86446ff5400SAxel Dörfler next = fRebindingTime; 8656cc7630fSAxel Dörfler break; 86646ff5400SAxel Dörfler case REBINDING: 86746ff5400SAxel Dörfler default: 86846ff5400SAxel Dörfler next = fRenewalTime; 86946ff5400SAxel Dörfler break; 87046ff5400SAxel Dörfler } 87146ff5400SAxel Dörfler } else { 87246ff5400SAxel Dörfler switch (state) { 87346ff5400SAxel Dörfler case RENEWAL: 87446ff5400SAxel Dörfler next = (fLeaseTime - fRebindingTime) / 4 + system_time(); 87546ff5400SAxel Dörfler break; 87646ff5400SAxel Dörfler case REBINDING: 87746ff5400SAxel Dörfler default: 87846ff5400SAxel Dörfler next = (fLeaseTime - fRenewalTime) / 4 + system_time(); 87946ff5400SAxel Dörfler break; 88046ff5400SAxel Dörfler } 88146ff5400SAxel Dörfler } 88246ff5400SAxel Dörfler 88346ff5400SAxel Dörfler _RestartLease(next - system_time()); 88446ff5400SAxel Dörfler break; 88546ff5400SAxel Dörfler } 8866cc7630fSAxel Dörfler 8876cc7630fSAxel Dörfler default: 888fb81684fSAxel Dörfler BHandler::MessageReceived(message); 8896cc7630fSAxel Dörfler break; 8906cc7630fSAxel Dörfler } 891fb81684fSAxel Dörfler } 892