BDHCPClientCore.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  1. /**
  2. * @file BDHCPClientCore.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <string.h>
  30. #include <stdlib.h>
  31. #include <misc/byteorder.h>
  32. #include <misc/minmax.h>
  33. #include <misc/balloc.h>
  34. #include <misc/bsize.h>
  35. #include <misc/dhcp_proto.h>
  36. #include <base/BLog.h>
  37. #include <dhcpclient/BDHCPClientCore.h>
  38. #include <generated/blog_channel_BDHCPClientCore.h>
  39. #define RESET_TIMEOUT 4000
  40. #define REQUEST_TIMEOUT 3000
  41. #define RENEW_REQUEST_TIMEOUT 20000
  42. #define MAX_REQUESTS 4
  43. #define RENEW_TIMEOUT(lease) ((btime_t)500 * (lease))
  44. #define XID_REUSE_MAX 8
  45. #define LEASE_TIMEOUT(lease) ((btime_t)1000 * (lease) - RENEW_TIMEOUT(lease))
  46. #define STATE_RESETTING 1
  47. #define STATE_SENT_DISCOVER 2
  48. #define STATE_SENT_REQUEST 3
  49. #define STATE_FINISHED 4
  50. #define STATE_RENEWING 5
  51. #define IP_UDP_HEADERS_SIZE 28
  52. static void report_up (BDHCPClientCore *o)
  53. {
  54. o->handler(o->user, BDHCPCLIENTCORE_EVENT_UP);
  55. return;
  56. }
  57. static void report_down (BDHCPClientCore *o)
  58. {
  59. o->handler(o->user, BDHCPCLIENTCORE_EVENT_DOWN);
  60. return;
  61. }
  62. static void send_message (
  63. BDHCPClientCore *o,
  64. int type,
  65. uint32_t xid,
  66. int have_requested_ip_address, uint32_t requested_ip_address,
  67. int have_dhcp_server_identifier, uint32_t dhcp_server_identifier
  68. )
  69. {
  70. ASSERT(type == DHCP_MESSAGE_TYPE_DISCOVER || type == DHCP_MESSAGE_TYPE_REQUEST)
  71. if (o->sending) {
  72. BLog(BLOG_ERROR, "already sending");
  73. return;
  74. }
  75. // write header
  76. struct dhcp_header header;
  77. memset(&header, 0, sizeof(header));
  78. header.op = hton8(DHCP_OP_BOOTREQUEST);
  79. header.htype = hton8(DHCP_HARDWARE_ADDRESS_TYPE_ETHERNET);
  80. header.hlen = hton8(6);
  81. header.xid = xid;
  82. header.secs = hton16(0);
  83. memcpy(header.chaddr, o->client_mac_addr, sizeof(o->client_mac_addr));
  84. header.magic = hton32(DHCP_MAGIC);
  85. memcpy(o->send_buf, &header, sizeof(header));
  86. // write options
  87. char *out = o->send_buf + sizeof(header);
  88. struct dhcp_option_header oh;
  89. // DHCP message type
  90. {
  91. oh.type = hton8(DHCP_OPTION_DHCP_MESSAGE_TYPE);
  92. oh.len = hton8(sizeof(struct dhcp_option_dhcp_message_type));
  93. struct dhcp_option_dhcp_message_type opt;
  94. opt.type = hton8(type);
  95. memcpy(out, &oh, sizeof(oh));
  96. memcpy(out + sizeof(oh), &opt, sizeof(opt));
  97. out += sizeof(oh) + sizeof(opt);
  98. }
  99. if (have_requested_ip_address) {
  100. // requested IP address
  101. oh.type = hton8(DHCP_OPTION_REQUESTED_IP_ADDRESS);
  102. oh.len = hton8(sizeof(struct dhcp_option_addr));
  103. struct dhcp_option_addr opt;
  104. opt.addr = requested_ip_address;
  105. memcpy(out, &oh, sizeof(oh));
  106. memcpy(out + sizeof(oh), &opt, sizeof(opt));
  107. out += sizeof(oh) + sizeof(opt);
  108. }
  109. if (have_dhcp_server_identifier) {
  110. // DHCP server identifier
  111. oh.type = hton8(DHCP_OPTION_DHCP_SERVER_IDENTIFIER);
  112. oh.len = hton8(sizeof(struct dhcp_option_dhcp_server_identifier));
  113. struct dhcp_option_dhcp_server_identifier opt;
  114. opt.id = dhcp_server_identifier;
  115. memcpy(out, &oh, sizeof(oh));
  116. memcpy(out + sizeof(oh), &opt, sizeof(opt));
  117. out += sizeof(oh) + sizeof(opt);
  118. }
  119. // maximum message size
  120. {
  121. oh.type = hton8(DHCP_OPTION_MAXIMUM_MESSAGE_SIZE);
  122. oh.len = hton8(sizeof(struct dhcp_option_maximum_message_size));
  123. struct dhcp_option_maximum_message_size opt;
  124. opt.size = hton16(IP_UDP_HEADERS_SIZE + PacketRecvInterface_GetMTU(o->recv_if));
  125. memcpy(out, &oh, sizeof(oh));
  126. memcpy(out + sizeof(oh), &opt, sizeof(opt));
  127. out += sizeof(oh) + sizeof(opt);
  128. }
  129. // parameter request list
  130. {
  131. oh.type = hton8(DHCP_OPTION_PARAMETER_REQUEST_LIST);
  132. oh.len = hton8(4);
  133. uint8_t opt[4];
  134. opt[0] = DHCP_OPTION_SUBNET_MASK;
  135. opt[1] = DHCP_OPTION_ROUTER;
  136. opt[2] = DHCP_OPTION_DOMAIN_NAME_SERVER;
  137. opt[3] = DHCP_OPTION_IP_ADDRESS_LEASE_TIME;
  138. memcpy(out, &oh, sizeof(oh));
  139. memcpy(out + sizeof(oh), &opt, sizeof(opt));
  140. out += sizeof(oh) + sizeof(opt);
  141. }
  142. if (o->hostname) {
  143. // host name
  144. oh.type = hton8(DHCP_OPTION_HOST_NAME);
  145. oh.len = hton8(strlen(o->hostname));
  146. memcpy(out, &oh, sizeof(oh));
  147. memcpy(out + sizeof(oh), o->hostname, strlen(o->hostname));
  148. out += sizeof(oh) + strlen(o->hostname);
  149. }
  150. if (o->vendorclassid) {
  151. // vendor class identifier
  152. oh.type = hton8(DHCP_OPTION_VENDOR_CLASS_IDENTIFIER);
  153. oh.len = hton8(strlen(o->vendorclassid));
  154. memcpy(out, &oh, sizeof(oh));
  155. memcpy(out + sizeof(oh), o->vendorclassid, strlen(o->vendorclassid));
  156. out += sizeof(oh) + strlen(o->vendorclassid);
  157. }
  158. if (o->clientid) {
  159. // client identifier
  160. oh.type = hton8(DHCP_OPTION_CLIENT_IDENTIFIER);
  161. oh.len = hton8(o->clientid_len);
  162. memcpy(out, &oh, sizeof(oh));
  163. memcpy(out + sizeof(oh), o->clientid, o->clientid_len);
  164. out += sizeof(oh) + o->clientid_len;
  165. }
  166. // end option
  167. uint8_t end = 0xFF;
  168. memcpy(out, &end, sizeof(end));
  169. out += sizeof(end);
  170. // send it
  171. PacketPassInterface_Sender_Send(o->send_if, (uint8_t *)o->send_buf, out - o->send_buf);
  172. o->sending = 1;
  173. }
  174. static void send_handler_done (BDHCPClientCore *o)
  175. {
  176. ASSERT(o->sending)
  177. DebugObject_Access(&o->d_obj);
  178. o->sending = 0;
  179. }
  180. static void recv_handler_done (BDHCPClientCore *o, int data_len)
  181. {
  182. ASSERT(data_len >= 0)
  183. DebugObject_Access(&o->d_obj);
  184. // receive more packets
  185. PacketRecvInterface_Receiver_Recv(o->recv_if, (uint8_t *)o->recv_buf);
  186. if (o->state == STATE_RESETTING) {
  187. return;
  188. }
  189. // check header
  190. if (data_len < sizeof(struct dhcp_header)) {
  191. return;
  192. }
  193. struct dhcp_header header;
  194. memcpy(&header, o->recv_buf, sizeof(header));
  195. if (ntoh8(header.op) != DHCP_OP_BOOTREPLY) {
  196. return;
  197. }
  198. if (ntoh8(header.htype) != DHCP_HARDWARE_ADDRESS_TYPE_ETHERNET) {
  199. return;
  200. }
  201. if (ntoh8(header.hlen) != 6) {
  202. return;
  203. }
  204. if (header.xid != o->xid) {
  205. return;
  206. }
  207. if (memcmp(header.chaddr, o->client_mac_addr, sizeof(o->client_mac_addr))) {
  208. return;
  209. }
  210. if (ntoh32(header.magic) != DHCP_MAGIC) {
  211. return;
  212. }
  213. // parse and check options
  214. uint8_t *pos = (uint8_t *)o->recv_buf + sizeof(header);
  215. int len = data_len - sizeof(header);
  216. int have_end = 0;
  217. int dhcp_message_type = -1;
  218. int have_dhcp_server_identifier = 0;
  219. uint32_t dhcp_server_identifier = 0; // to remove warning
  220. int have_ip_address_lease_time = 0;
  221. uint32_t ip_address_lease_time = 0; // to remove warning
  222. int have_subnet_mask = 0;
  223. uint32_t subnet_mask = 0; // to remove warning
  224. int have_router = 0;
  225. uint32_t router = 0; // to remove warning
  226. int domain_name_servers_count = 0;
  227. uint32_t domain_name_servers[BDHCPCLIENTCORE_MAX_DOMAIN_NAME_SERVERS];
  228. while (len > 0) {
  229. // padding option ?
  230. if (*pos == 0) {
  231. pos++;
  232. len--;
  233. continue;
  234. }
  235. if (have_end) {
  236. return;
  237. }
  238. // end option ?
  239. if (*pos == 0xff) {
  240. pos++;
  241. len--;
  242. have_end = 1;
  243. continue;
  244. }
  245. // check option header
  246. if (len < sizeof(struct dhcp_option_header)) {
  247. return;
  248. }
  249. struct dhcp_option_header opt;
  250. memcpy(&opt, pos, sizeof(opt));
  251. pos += sizeof(opt);
  252. len -= sizeof(opt);
  253. int opt_type = ntoh8(opt.type);
  254. int opt_len = ntoh8(opt.len);
  255. // check option payload
  256. if (opt_len > len) {
  257. return;
  258. }
  259. uint8_t *optval = pos;
  260. pos += opt_len;
  261. len -= opt_len;
  262. switch (opt_type) {
  263. case DHCP_OPTION_DHCP_MESSAGE_TYPE: {
  264. if (opt_len != sizeof(struct dhcp_option_dhcp_message_type)) {
  265. return;
  266. }
  267. struct dhcp_option_dhcp_message_type val;
  268. memcpy(&val, optval, sizeof(val));
  269. dhcp_message_type = ntoh8(val.type);
  270. } break;
  271. case DHCP_OPTION_DHCP_SERVER_IDENTIFIER: {
  272. if (opt_len != sizeof(struct dhcp_option_dhcp_server_identifier)) {
  273. return;
  274. }
  275. struct dhcp_option_dhcp_server_identifier val;
  276. memcpy(&val, optval, sizeof(val));
  277. dhcp_server_identifier = val.id;
  278. have_dhcp_server_identifier = 1;
  279. } break;
  280. case DHCP_OPTION_IP_ADDRESS_LEASE_TIME: {
  281. if (opt_len != sizeof(struct dhcp_option_time)) {
  282. return;
  283. }
  284. struct dhcp_option_time val;
  285. memcpy(&val, optval, sizeof(val));
  286. ip_address_lease_time = ntoh32(val.time);
  287. have_ip_address_lease_time = 1;
  288. } break;
  289. case DHCP_OPTION_SUBNET_MASK: {
  290. if (opt_len != sizeof(struct dhcp_option_addr)) {
  291. return;
  292. }
  293. struct dhcp_option_addr val;
  294. memcpy(&val, optval, sizeof(val));
  295. subnet_mask = val.addr;
  296. have_subnet_mask = 1;
  297. } break;
  298. case DHCP_OPTION_ROUTER: {
  299. if (opt_len != sizeof(struct dhcp_option_addr)) {
  300. return;
  301. }
  302. struct dhcp_option_addr val;
  303. memcpy(&val, optval, sizeof(val));
  304. router = val.addr;
  305. have_router = 1;
  306. } break;
  307. case DHCP_OPTION_DOMAIN_NAME_SERVER: {
  308. if (opt_len % sizeof(struct dhcp_option_addr)) {
  309. return;
  310. }
  311. int num_servers = opt_len / sizeof(struct dhcp_option_addr);
  312. int i;
  313. for (i = 0; i < num_servers && i < BDHCPCLIENTCORE_MAX_DOMAIN_NAME_SERVERS; i++) {
  314. struct dhcp_option_addr addr;
  315. memcpy(&addr, optval + i * sizeof(addr), sizeof(addr));
  316. domain_name_servers[i] = addr.addr;
  317. }
  318. domain_name_servers_count = i;
  319. } break;
  320. }
  321. }
  322. if (!have_end) {
  323. return;
  324. }
  325. if (dhcp_message_type == -1) {
  326. return;
  327. }
  328. if (dhcp_message_type != DHCP_MESSAGE_TYPE_OFFER && dhcp_message_type != DHCP_MESSAGE_TYPE_ACK && dhcp_message_type != DHCP_MESSAGE_TYPE_NAK) {
  329. return;
  330. }
  331. if (!have_dhcp_server_identifier) {
  332. return;
  333. }
  334. if (dhcp_message_type == DHCP_MESSAGE_TYPE_NAK) {
  335. if (o->state != STATE_SENT_REQUEST && o->state != STATE_FINISHED && o->state != STATE_RENEWING) {
  336. return;
  337. }
  338. if (dhcp_server_identifier != o->offered.dhcp_server_identifier) {
  339. return;
  340. }
  341. if (o->state == STATE_SENT_REQUEST) {
  342. BLog(BLOG_INFO, "received NAK (in sent request)");
  343. // stop request timer
  344. BReactor_RemoveTimer(o->reactor, &o->request_timer);
  345. // start reset timer
  346. BReactor_SetTimer(o->reactor, &o->reset_timer);
  347. // set state
  348. o->state = STATE_RESETTING;
  349. }
  350. else if (o->state == STATE_FINISHED) {
  351. BLog(BLOG_INFO, "received NAK (in finished)");
  352. // stop renew timer
  353. BReactor_RemoveTimer(o->reactor, &o->renew_timer);
  354. // start reset timer
  355. BReactor_SetTimer(o->reactor, &o->reset_timer);
  356. // set state
  357. o->state = STATE_RESETTING;
  358. // report to user
  359. report_down(o);
  360. return;
  361. }
  362. else { // STATE_RENEWING
  363. BLog(BLOG_INFO, "received NAK (in renewing)");
  364. // stop renew request timer
  365. BReactor_RemoveTimer(o->reactor, &o->renew_request_timer);
  366. // stop lease timer
  367. BReactor_RemoveTimer(o->reactor, &o->lease_timer);
  368. // start reset timer
  369. BReactor_SetTimer(o->reactor, &o->reset_timer);
  370. // set state
  371. o->state = STATE_RESETTING;
  372. // report to user
  373. report_down(o);
  374. return;
  375. }
  376. return;
  377. }
  378. if (ntoh32(header.yiaddr) == 0) {
  379. return;
  380. }
  381. if (!have_ip_address_lease_time) {
  382. return;
  383. }
  384. if (!have_subnet_mask) {
  385. return;
  386. }
  387. if (o->state == STATE_SENT_DISCOVER && dhcp_message_type == DHCP_MESSAGE_TYPE_OFFER) {
  388. BLog(BLOG_INFO, "received OFFER");
  389. // remember offer
  390. o->offered.yiaddr = header.yiaddr;
  391. o->offered.dhcp_server_identifier = dhcp_server_identifier;
  392. // send request
  393. send_message(o, DHCP_MESSAGE_TYPE_REQUEST, o->xid, 1, o->offered.yiaddr, 1, o->offered.dhcp_server_identifier);
  394. // stop reset timer
  395. BReactor_RemoveTimer(o->reactor, &o->reset_timer);
  396. // start request timer
  397. BReactor_SetTimer(o->reactor, &o->request_timer);
  398. // set state
  399. o->state = STATE_SENT_REQUEST;
  400. // set request count
  401. o->request_count = 1;
  402. }
  403. else if (o->state == STATE_SENT_REQUEST && dhcp_message_type == DHCP_MESSAGE_TYPE_ACK) {
  404. if (header.yiaddr != o->offered.yiaddr) {
  405. return;
  406. }
  407. if (dhcp_server_identifier != o->offered.dhcp_server_identifier) {
  408. return;
  409. }
  410. BLog(BLOG_INFO, "received ACK (in sent request)");
  411. // remember stuff
  412. o->acked.ip_address_lease_time = ip_address_lease_time;
  413. o->acked.subnet_mask = subnet_mask;
  414. o->acked.have_router = have_router;
  415. if (have_router) {
  416. o->acked.router = router;
  417. }
  418. o->acked.domain_name_servers_count = domain_name_servers_count;
  419. memcpy(o->acked.domain_name_servers, domain_name_servers, domain_name_servers_count * sizeof(uint32_t));
  420. o->func_getsendermac(o->user, o->acked.server_mac);
  421. // stop request timer
  422. BReactor_RemoveTimer(o->reactor, &o->request_timer);
  423. // start renew timer
  424. BReactor_SetTimerAfter(o->reactor, &o->renew_timer, RENEW_TIMEOUT(o->acked.ip_address_lease_time));
  425. // set state
  426. o->state = STATE_FINISHED;
  427. // report to user
  428. report_up(o);
  429. return;
  430. }
  431. else if (o->state == STATE_RENEWING && dhcp_message_type == DHCP_MESSAGE_TYPE_ACK) {
  432. if (header.yiaddr != o->offered.yiaddr) {
  433. return;
  434. }
  435. if (dhcp_server_identifier != o->offered.dhcp_server_identifier) {
  436. return;
  437. }
  438. // TODO: check parameters?
  439. BLog(BLOG_INFO, "received ACK (in renewing)");
  440. // remember stuff
  441. o->acked.ip_address_lease_time = ip_address_lease_time;
  442. // stop renew request timer
  443. BReactor_RemoveTimer(o->reactor, &o->renew_request_timer);
  444. // stop lease timer
  445. BReactor_RemoveTimer(o->reactor, &o->lease_timer);
  446. // start renew timer
  447. BReactor_SetTimerAfter(o->reactor, &o->renew_timer, RENEW_TIMEOUT(o->acked.ip_address_lease_time));
  448. // set state
  449. o->state = STATE_FINISHED;
  450. }
  451. }
  452. static void start_process (BDHCPClientCore *o, int force_new_xid)
  453. {
  454. if (force_new_xid || o->xid_reuse_counter == XID_REUSE_MAX) {
  455. // generate xid
  456. if (!BRandom2_GenBytes(o->random2, &o->xid, sizeof(o->xid))) {
  457. BLog(BLOG_ERROR, "BRandom2_GenBytes failed");
  458. o->xid = UINT32_C(3416960072);
  459. }
  460. // reset counter
  461. o->xid_reuse_counter = 0;
  462. }
  463. // increment counter
  464. o->xid_reuse_counter++;
  465. // send discover
  466. send_message(o, DHCP_MESSAGE_TYPE_DISCOVER, o->xid, 0, 0, 0, 0);
  467. // set timer
  468. BReactor_SetTimer(o->reactor, &o->reset_timer);
  469. // set state
  470. o->state = STATE_SENT_DISCOVER;
  471. }
  472. static void reset_timer_handler (BDHCPClientCore *o)
  473. {
  474. ASSERT(o->state == STATE_RESETTING || o->state == STATE_SENT_DISCOVER)
  475. DebugObject_Access(&o->d_obj);
  476. BLog(BLOG_INFO, "reset timer");
  477. start_process(o, (o->state == STATE_RESETTING));
  478. }
  479. static void request_timer_handler (BDHCPClientCore *o)
  480. {
  481. ASSERT(o->state == STATE_SENT_REQUEST)
  482. ASSERT(o->request_count >= 1)
  483. ASSERT(o->request_count <= MAX_REQUESTS)
  484. DebugObject_Access(&o->d_obj);
  485. // if we have sent enough requests, start again
  486. if (o->request_count == MAX_REQUESTS) {
  487. BLog(BLOG_INFO, "request timer, aborting");
  488. start_process(o, 0);
  489. return;
  490. }
  491. BLog(BLOG_INFO, "request timer, retrying");
  492. // send request
  493. send_message(o, DHCP_MESSAGE_TYPE_REQUEST, o->xid, 1, o->offered.yiaddr, 1, o->offered.dhcp_server_identifier);
  494. // start request timer
  495. BReactor_SetTimer(o->reactor, &o->request_timer);
  496. // increment request count
  497. o->request_count++;
  498. }
  499. static void renew_timer_handler (BDHCPClientCore *o)
  500. {
  501. ASSERT(o->state == STATE_FINISHED)
  502. DebugObject_Access(&o->d_obj);
  503. BLog(BLOG_INFO, "renew timer");
  504. // send request
  505. send_message(o, DHCP_MESSAGE_TYPE_REQUEST, o->xid, 1, o->offered.yiaddr, 0, 0);
  506. // start renew request timer
  507. BReactor_SetTimer(o->reactor, &o->renew_request_timer);
  508. // start lease timer
  509. BReactor_SetTimerAfter(o->reactor, &o->lease_timer, LEASE_TIMEOUT(o->acked.ip_address_lease_time));
  510. // set state
  511. o->state = STATE_RENEWING;
  512. }
  513. static void renew_request_timer_handler (BDHCPClientCore *o)
  514. {
  515. ASSERT(o->state == STATE_RENEWING)
  516. DebugObject_Access(&o->d_obj);
  517. BLog(BLOG_INFO, "renew request timer");
  518. // send request
  519. send_message(o, DHCP_MESSAGE_TYPE_REQUEST, o->xid, 1, o->offered.yiaddr, 0, 0);
  520. // start renew request timer
  521. BReactor_SetTimer(o->reactor, &o->renew_request_timer);
  522. }
  523. static void lease_timer_handler (BDHCPClientCore *o)
  524. {
  525. ASSERT(o->state == STATE_RENEWING)
  526. DebugObject_Access(&o->d_obj);
  527. BLog(BLOG_INFO, "lease timer");
  528. // stop renew request timer
  529. BReactor_RemoveTimer(o->reactor, &o->renew_request_timer);
  530. // start again now
  531. start_process(o, 1);
  532. // report to user
  533. report_down(o);
  534. return;
  535. }
  536. static bsize_t maybe_len (const char *str)
  537. {
  538. return bsize_fromsize(str ? strlen(str) : 0);
  539. }
  540. int BDHCPClientCore_Init (BDHCPClientCore *o, PacketPassInterface *send_if, PacketRecvInterface *recv_if,
  541. uint8_t *client_mac_addr, struct BDHCPClientCore_opts opts, BReactor *reactor,
  542. BRandom2 *random2, void *user,
  543. BDHCPClientCore_func_getsendermac func_getsendermac,
  544. BDHCPClientCore_handler handler)
  545. {
  546. ASSERT(PacketPassInterface_GetMTU(send_if) == PacketRecvInterface_GetMTU(recv_if))
  547. ASSERT(PacketPassInterface_GetMTU(send_if) >= 576 - IP_UDP_HEADERS_SIZE)
  548. ASSERT(func_getsendermac)
  549. ASSERT(handler)
  550. // init arguments
  551. o->send_if = send_if;
  552. o->recv_if = recv_if;
  553. memcpy(o->client_mac_addr, client_mac_addr, sizeof(o->client_mac_addr));
  554. o->reactor = reactor;
  555. o->random2 = random2;
  556. o->user = user;
  557. o->func_getsendermac = func_getsendermac;
  558. o->handler = handler;
  559. o->hostname = NULL;
  560. o->vendorclassid = NULL;
  561. o->clientid = NULL;
  562. o->clientid_len = 0;
  563. // copy options
  564. if (opts.hostname && !(o->hostname = strdup(opts.hostname))) {
  565. BLog(BLOG_ERROR, "strdup failed");
  566. goto fail0;
  567. }
  568. if (opts.vendorclassid && !(o->vendorclassid = strdup(opts.vendorclassid))) {
  569. BLog(BLOG_ERROR, "strdup failed");
  570. goto fail0;
  571. }
  572. if (opts.clientid) {
  573. if (!(o->clientid = BAlloc(opts.clientid_len))) {
  574. BLog(BLOG_ERROR, "BAlloc failed");
  575. goto fail0;
  576. }
  577. memcpy(o->clientid, opts.clientid, opts.clientid_len);
  578. o->clientid_len = opts.clientid_len;
  579. }
  580. // make sure options aren't too long
  581. bsize_t opts_size = bsize_add(maybe_len(o->hostname), bsize_add(maybe_len(o->vendorclassid), bsize_fromsize(o->clientid_len)));
  582. if (opts_size.is_overflow || opts_size.value > 100) {
  583. BLog(BLOG_ERROR, "options too long together");
  584. goto fail0;
  585. }
  586. if (o->hostname && strlen(o->hostname) > 255) {
  587. BLog(BLOG_ERROR, "hostname too long");
  588. goto fail0;
  589. }
  590. if (o->vendorclassid && strlen(o->vendorclassid) > 255) {
  591. BLog(BLOG_ERROR, "vendorclassid too long");
  592. goto fail0;
  593. }
  594. if (o->clientid && o->clientid_len > 255) {
  595. BLog(BLOG_ERROR, "clientid too long");
  596. goto fail0;
  597. }
  598. // allocate buffers
  599. if (!(o->send_buf = BAlloc(PacketPassInterface_GetMTU(send_if)))) {
  600. BLog(BLOG_ERROR, "BAlloc send buf failed");
  601. goto fail0;
  602. }
  603. if (!(o->recv_buf = BAlloc(PacketRecvInterface_GetMTU(recv_if)))) {
  604. BLog(BLOG_ERROR, "BAlloc recv buf failed");
  605. goto fail1;
  606. }
  607. // init send interface
  608. PacketPassInterface_Sender_Init(o->send_if, (PacketPassInterface_handler_done)send_handler_done, o);
  609. // init receive interface
  610. PacketRecvInterface_Receiver_Init(o->recv_if, (PacketRecvInterface_handler_done)recv_handler_done, o);
  611. // set not sending
  612. o->sending = 0;
  613. // init timers
  614. BTimer_Init(&o->reset_timer, RESET_TIMEOUT, (BTimer_handler)reset_timer_handler, o);
  615. BTimer_Init(&o->request_timer, REQUEST_TIMEOUT, (BTimer_handler)request_timer_handler, o);
  616. BTimer_Init(&o->renew_timer, 0, (BTimer_handler)renew_timer_handler, o);
  617. BTimer_Init(&o->renew_request_timer, RENEW_REQUEST_TIMEOUT, (BTimer_handler)renew_request_timer_handler, o);
  618. BTimer_Init(&o->lease_timer, 0, (BTimer_handler)lease_timer_handler, o);
  619. // start receving
  620. PacketRecvInterface_Receiver_Recv(o->recv_if, (uint8_t *)o->recv_buf);
  621. // start
  622. start_process(o, 1);
  623. DebugObject_Init(&o->d_obj);
  624. return 1;
  625. fail1:
  626. BFree(o->send_buf);
  627. fail0:
  628. BFree(o->clientid);
  629. free(o->vendorclassid);
  630. free(o->hostname);
  631. return 0;
  632. }
  633. void BDHCPClientCore_Free (BDHCPClientCore *o)
  634. {
  635. DebugObject_Free(&o->d_obj);
  636. // free timers
  637. BReactor_RemoveTimer(o->reactor, &o->lease_timer);
  638. BReactor_RemoveTimer(o->reactor, &o->renew_request_timer);
  639. BReactor_RemoveTimer(o->reactor, &o->renew_timer);
  640. BReactor_RemoveTimer(o->reactor, &o->request_timer);
  641. BReactor_RemoveTimer(o->reactor, &o->reset_timer);
  642. // free buffers
  643. BFree(o->recv_buf);
  644. BFree(o->send_buf);
  645. // free options
  646. BFree(o->clientid);
  647. free(o->vendorclassid);
  648. free(o->hostname);
  649. }
  650. void BDHCPClientCore_GetClientIP (BDHCPClientCore *o, uint32_t *out_ip)
  651. {
  652. ASSERT(o->state == STATE_FINISHED || o->state == STATE_RENEWING)
  653. DebugObject_Access(&o->d_obj);
  654. *out_ip = o->offered.yiaddr;
  655. }
  656. void BDHCPClientCore_GetClientMask (BDHCPClientCore *o, uint32_t *out_mask)
  657. {
  658. ASSERT(o->state == STATE_FINISHED || o->state == STATE_RENEWING)
  659. DebugObject_Access(&o->d_obj);
  660. *out_mask = o->acked.subnet_mask;
  661. }
  662. int BDHCPClientCore_GetRouter (BDHCPClientCore *o, uint32_t *out_router)
  663. {
  664. ASSERT(o->state == STATE_FINISHED || o->state == STATE_RENEWING)
  665. DebugObject_Access(&o->d_obj);
  666. if (!o->acked.have_router) {
  667. return 0;
  668. }
  669. *out_router = o->acked.router;
  670. return 1;
  671. }
  672. int BDHCPClientCore_GetDNS (BDHCPClientCore *o, uint32_t *out_dns_servers, size_t max_dns_servers)
  673. {
  674. ASSERT(o->state == STATE_FINISHED || o->state == STATE_RENEWING)
  675. DebugObject_Access(&o->d_obj);
  676. int num_return = bmin_int(o->acked.domain_name_servers_count, max_dns_servers);
  677. memcpy(out_dns_servers, o->acked.domain_name_servers, num_return * sizeof(uint32_t));
  678. return num_return;
  679. }
  680. void BDHCPClientCore_GetServerMAC (BDHCPClientCore *o, uint8_t *out_mac)
  681. {
  682. DebugObject_Access(&o->d_obj);
  683. ASSERT(o->state == STATE_FINISHED || o->state == STATE_RENEWING)
  684. memcpy(out_mac, o->acked.server_mac, 6);
  685. }