BDHCPClientCore.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  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 <security/BRandom.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. memset(o->send_buf, 0, sizeof(*o->send_buf));
  77. o->send_buf->op = hton8(DHCP_OP_BOOTREQUEST);
  78. o->send_buf->htype = hton8(DHCP_HARDWARE_ADDRESS_TYPE_ETHERNET);
  79. o->send_buf->hlen = hton8(6);
  80. o->send_buf->xid = xid;
  81. o->send_buf->secs = hton16(0);
  82. memcpy(o->send_buf->chaddr, o->client_mac_addr, sizeof(o->client_mac_addr));
  83. o->send_buf->magic = hton32(DHCP_MAGIC);
  84. // write options
  85. struct dhcp_option_header *out = (void *)(o->send_buf + 1);
  86. // DHCP message type
  87. out->type = hton8(DHCP_OPTION_DHCP_MESSAGE_TYPE);
  88. out->len = hton8(sizeof(struct dhcp_option_dhcp_message_type));
  89. ((struct dhcp_option_dhcp_message_type *)(out + 1))->type = hton8(type);
  90. out = (void *)((uint8_t *)(out + 1) + ntoh8(out->len));
  91. if (have_requested_ip_address) {
  92. // requested IP address
  93. out->type = hton8(DHCP_OPTION_REQUESTED_IP_ADDRESS);
  94. out->len = hton8(sizeof(struct dhcp_option_addr));
  95. ((struct dhcp_option_addr *)(out + 1))->addr = requested_ip_address;
  96. out = (void *)((uint8_t *)(out + 1) + ntoh8(out->len));
  97. }
  98. if (have_dhcp_server_identifier) {
  99. // DHCP server identifier
  100. out->type = hton8(DHCP_OPTION_DHCP_SERVER_IDENTIFIER);
  101. out->len = hton8(sizeof(struct dhcp_option_dhcp_server_identifier));
  102. ((struct dhcp_option_dhcp_server_identifier *)(out + 1))->id = dhcp_server_identifier;
  103. out = (void *)((uint8_t *)(out + 1) + ntoh8(out->len));
  104. }
  105. // maximum message size
  106. out->type = hton8(DHCP_OPTION_MAXIMUM_MESSAGE_SIZE);
  107. out->len = hton8(sizeof(struct dhcp_option_maximum_message_size));
  108. ((struct dhcp_option_maximum_message_size *)(out + 1))->size = hton16(IP_UDP_HEADERS_SIZE + PacketRecvInterface_GetMTU(o->recv_if));
  109. out = (void *)((uint8_t *)(out + 1) + ntoh8(out->len));
  110. // parameter request list
  111. out->type = hton8(DHCP_OPTION_PARAMETER_REQUEST_LIST);
  112. out->len = hton8(4);
  113. ((uint8_t *)(out + 1))[0] = DHCP_OPTION_SUBNET_MASK;
  114. ((uint8_t *)(out + 1))[1] = DHCP_OPTION_ROUTER;
  115. ((uint8_t *)(out + 1))[2] = DHCP_OPTION_DOMAIN_NAME_SERVER;
  116. ((uint8_t *)(out + 1))[3] = DHCP_OPTION_IP_ADDRESS_LEASE_TIME;
  117. out = (void *)((uint8_t *)(out + 1) + ntoh8(out->len));
  118. if (o->hostname) {
  119. // host name
  120. out->type = hton8(DHCP_OPTION_HOST_NAME);
  121. out->len = hton8(strlen(o->hostname));
  122. memcpy(out + 1, o->hostname, strlen(o->hostname));
  123. out = (void *)((uint8_t *)(out + 1) + ntoh8(out->len));
  124. }
  125. if (o->vendorclassid) {
  126. // vendor class identifier
  127. out->type = hton8(DHCP_OPTION_VENDOR_CLASS_IDENTIFIER);
  128. out->len = hton8(strlen(o->vendorclassid));
  129. memcpy(out + 1, o->vendorclassid, strlen(o->vendorclassid));
  130. out = (void *)((uint8_t *)(out + 1) + ntoh8(out->len));
  131. }
  132. if (o->clientid) {
  133. // client identifier
  134. out->type = hton8(DHCP_OPTION_CLIENT_IDENTIFIER);
  135. out->len = hton8(o->clientid_len);
  136. memcpy(out + 1, o->clientid, o->clientid_len);
  137. out = (void *)((uint8_t *)(out + 1) + ntoh8(out->len));
  138. }
  139. // end option
  140. *((uint8_t *)out) = 0xFF;
  141. out = (void *)((uint8_t *)out + 1);
  142. // send it
  143. PacketPassInterface_Sender_Send(o->send_if, (uint8_t *)o->send_buf, (uint8_t *)out - (uint8_t *)o->send_buf);
  144. o->sending = 1;
  145. }
  146. static void send_handler_done (BDHCPClientCore *o)
  147. {
  148. ASSERT(o->sending)
  149. DebugObject_Access(&o->d_obj);
  150. o->sending = 0;
  151. }
  152. static void recv_handler_done (BDHCPClientCore *o, int data_len)
  153. {
  154. ASSERT(data_len >= 0)
  155. DebugObject_Access(&o->d_obj);
  156. // receive more packets
  157. PacketRecvInterface_Receiver_Recv(o->recv_if, (uint8_t *)o->recv_buf);
  158. if (o->state == STATE_RESETTING) {
  159. return;
  160. }
  161. // check header
  162. if (data_len < sizeof(*o->recv_buf)) {
  163. return;
  164. }
  165. if (ntoh8(o->recv_buf->op) != DHCP_OP_BOOTREPLY) {
  166. return;
  167. }
  168. if (ntoh8(o->recv_buf->htype) != DHCP_HARDWARE_ADDRESS_TYPE_ETHERNET) {
  169. return;
  170. }
  171. if (ntoh8(o->recv_buf->hlen) != 6) {
  172. return;
  173. }
  174. if (o->recv_buf->xid != o->xid) {
  175. return;
  176. }
  177. if (memcmp(o->recv_buf->chaddr, o->client_mac_addr, sizeof(o->client_mac_addr))) {
  178. return;
  179. }
  180. if (ntoh32(o->recv_buf->magic) != DHCP_MAGIC) {
  181. return;
  182. }
  183. // parse and check options
  184. uint8_t *pos = (uint8_t *)o->recv_buf + sizeof(*o->recv_buf);
  185. int len = data_len - sizeof(*o->recv_buf);
  186. int have_end = 0;
  187. int dhcp_message_type = -1;
  188. int have_dhcp_server_identifier = 0;
  189. uint32_t dhcp_server_identifier;
  190. int have_ip_address_lease_time = 0;
  191. uint32_t ip_address_lease_time;
  192. int have_subnet_mask = 0;
  193. uint32_t subnet_mask;
  194. int have_router = 0;
  195. uint32_t router;
  196. int domain_name_servers_count = 0;
  197. uint32_t domain_name_servers[BDHCPCLIENTCORE_MAX_DOMAIN_NAME_SERVERS];
  198. while (len > 0) {
  199. // padding option ?
  200. if (*pos == 0) {
  201. pos++;
  202. len--;
  203. continue;
  204. }
  205. if (have_end) {
  206. return;
  207. }
  208. // end option ?
  209. if (*pos == 0xff) {
  210. pos++;
  211. len--;
  212. have_end = 1;
  213. continue;
  214. }
  215. // check option header
  216. if (len < sizeof(struct dhcp_option_header)) {
  217. return;
  218. }
  219. struct dhcp_option_header *opt = (void *)pos;
  220. pos += sizeof(*opt);
  221. len -= sizeof(*opt);
  222. int opt_type = ntoh8(opt->type);
  223. int opt_len = ntoh8(opt->len);
  224. // check option payload
  225. if (opt_len > len) {
  226. return;
  227. }
  228. void *optval = pos;
  229. pos += opt_len;
  230. len -= opt_len;
  231. switch (opt_type) {
  232. case DHCP_OPTION_DHCP_MESSAGE_TYPE: {
  233. if (opt_len != sizeof(struct dhcp_option_dhcp_message_type)) {
  234. return;
  235. }
  236. struct dhcp_option_dhcp_message_type *val = optval;
  237. dhcp_message_type = ntoh8(val->type);
  238. } break;
  239. case DHCP_OPTION_DHCP_SERVER_IDENTIFIER: {
  240. if (opt_len != sizeof(struct dhcp_option_dhcp_server_identifier)) {
  241. return;
  242. }
  243. struct dhcp_option_dhcp_server_identifier *val = optval;
  244. dhcp_server_identifier = val->id;
  245. have_dhcp_server_identifier = 1;
  246. } break;
  247. case DHCP_OPTION_IP_ADDRESS_LEASE_TIME: {
  248. if (opt_len != sizeof(struct dhcp_option_time)) {
  249. return;
  250. }
  251. struct dhcp_option_time *val = optval;
  252. ip_address_lease_time = ntoh32(val->time);
  253. have_ip_address_lease_time = 1;
  254. } break;
  255. case DHCP_OPTION_SUBNET_MASK: {
  256. if (opt_len != sizeof(struct dhcp_option_addr)) {
  257. return;
  258. }
  259. struct dhcp_option_addr *val = optval;
  260. subnet_mask = val->addr;
  261. have_subnet_mask = 1;
  262. } break;
  263. case DHCP_OPTION_ROUTER: {
  264. if (opt_len != sizeof(struct dhcp_option_addr)) {
  265. return;
  266. }
  267. struct dhcp_option_addr *val = optval;
  268. router = val->addr;
  269. have_router = 1;
  270. } break;
  271. case DHCP_OPTION_DOMAIN_NAME_SERVER: {
  272. if (opt_len % 4) {
  273. return;
  274. }
  275. int num_servers = opt_len / 4;
  276. int i;
  277. for (i = 0; i < num_servers && i < BDHCPCLIENTCORE_MAX_DOMAIN_NAME_SERVERS; i++) {
  278. domain_name_servers[i] = ((struct dhcp_option_addr *)optval + i)->addr;
  279. }
  280. domain_name_servers_count = i;
  281. } break;
  282. }
  283. }
  284. if (!have_end) {
  285. return;
  286. }
  287. if (dhcp_message_type == -1) {
  288. return;
  289. }
  290. if (dhcp_message_type != DHCP_MESSAGE_TYPE_OFFER && dhcp_message_type != DHCP_MESSAGE_TYPE_ACK && dhcp_message_type != DHCP_MESSAGE_TYPE_NAK) {
  291. return;
  292. }
  293. if (!have_dhcp_server_identifier) {
  294. return;
  295. }
  296. if (dhcp_message_type == DHCP_MESSAGE_TYPE_NAK) {
  297. if (o->state != STATE_SENT_REQUEST && o->state != STATE_FINISHED && o->state != STATE_RENEWING) {
  298. return;
  299. }
  300. if (dhcp_server_identifier != o->offered.dhcp_server_identifier) {
  301. return;
  302. }
  303. if (o->state == STATE_SENT_REQUEST) {
  304. BLog(BLOG_INFO, "received NAK (in sent request)");
  305. // stop request timer
  306. BReactor_RemoveTimer(o->reactor, &o->request_timer);
  307. // start reset timer
  308. BReactor_SetTimer(o->reactor, &o->reset_timer);
  309. // set state
  310. o->state = STATE_RESETTING;
  311. }
  312. else if (o->state == STATE_FINISHED) {
  313. BLog(BLOG_INFO, "received NAK (in finished)");
  314. // stop renew timer
  315. BReactor_RemoveTimer(o->reactor, &o->renew_timer);
  316. // start reset timer
  317. BReactor_SetTimer(o->reactor, &o->reset_timer);
  318. // set state
  319. o->state = STATE_RESETTING;
  320. // report to user
  321. report_down(o);
  322. return;
  323. }
  324. else { // STATE_RENEWING
  325. BLog(BLOG_INFO, "received NAK (in renewing)");
  326. // stop renew request timer
  327. BReactor_RemoveTimer(o->reactor, &o->renew_request_timer);
  328. // stop lease timer
  329. BReactor_RemoveTimer(o->reactor, &o->lease_timer);
  330. // start reset timer
  331. BReactor_SetTimer(o->reactor, &o->reset_timer);
  332. // set state
  333. o->state = STATE_RESETTING;
  334. // report to user
  335. report_down(o);
  336. return;
  337. }
  338. return;
  339. }
  340. if (ntoh32(o->recv_buf->yiaddr) == 0) {
  341. return;
  342. }
  343. if (!have_ip_address_lease_time) {
  344. return;
  345. }
  346. if (!have_subnet_mask) {
  347. return;
  348. }
  349. if (o->state == STATE_SENT_DISCOVER && dhcp_message_type == DHCP_MESSAGE_TYPE_OFFER) {
  350. BLog(BLOG_INFO, "received OFFER");
  351. // remember offer
  352. o->offered.yiaddr = o->recv_buf->yiaddr;
  353. o->offered.dhcp_server_identifier = dhcp_server_identifier;
  354. // send request
  355. send_message(o, DHCP_MESSAGE_TYPE_REQUEST, o->xid, 1, o->offered.yiaddr, 1, o->offered.dhcp_server_identifier);
  356. // stop reset timer
  357. BReactor_RemoveTimer(o->reactor, &o->reset_timer);
  358. // start request timer
  359. BReactor_SetTimer(o->reactor, &o->request_timer);
  360. // set state
  361. o->state = STATE_SENT_REQUEST;
  362. // set request count
  363. o->request_count = 1;
  364. }
  365. else if (o->state == STATE_SENT_REQUEST && dhcp_message_type == DHCP_MESSAGE_TYPE_ACK) {
  366. if (o->recv_buf->yiaddr != o->offered.yiaddr) {
  367. return;
  368. }
  369. if (dhcp_server_identifier != o->offered.dhcp_server_identifier) {
  370. return;
  371. }
  372. BLog(BLOG_INFO, "received ACK (in sent request)");
  373. // remember stuff
  374. o->acked.ip_address_lease_time = ip_address_lease_time;
  375. o->acked.subnet_mask = subnet_mask;
  376. o->acked.have_router = have_router;
  377. if (have_router) {
  378. o->acked.router = router;
  379. }
  380. o->acked.domain_name_servers_count = domain_name_servers_count;
  381. memcpy(o->acked.domain_name_servers, domain_name_servers, domain_name_servers_count * sizeof(uint32_t));
  382. o->func_getsendermac(o->user, o->acked.server_mac);
  383. // stop request timer
  384. BReactor_RemoveTimer(o->reactor, &o->request_timer);
  385. // start renew timer
  386. BReactor_SetTimerAfter(o->reactor, &o->renew_timer, RENEW_TIMEOUT(o->acked.ip_address_lease_time));
  387. // set state
  388. o->state = STATE_FINISHED;
  389. // report to user
  390. report_up(o);
  391. return;
  392. }
  393. else if (o->state == STATE_RENEWING && dhcp_message_type == DHCP_MESSAGE_TYPE_ACK) {
  394. if (o->recv_buf->yiaddr != o->offered.yiaddr) {
  395. return;
  396. }
  397. if (dhcp_server_identifier != o->offered.dhcp_server_identifier) {
  398. return;
  399. }
  400. // TODO: check parameters?
  401. BLog(BLOG_INFO, "received ACK (in renewing)");
  402. // remember stuff
  403. o->acked.ip_address_lease_time = ip_address_lease_time;
  404. // stop renew request timer
  405. BReactor_RemoveTimer(o->reactor, &o->renew_request_timer);
  406. // stop lease timer
  407. BReactor_RemoveTimer(o->reactor, &o->lease_timer);
  408. // start renew timer
  409. BReactor_SetTimerAfter(o->reactor, &o->renew_timer, RENEW_TIMEOUT(o->acked.ip_address_lease_time));
  410. // set state
  411. o->state = STATE_FINISHED;
  412. }
  413. }
  414. static void start_process (BDHCPClientCore *o, int force_new_xid)
  415. {
  416. if (force_new_xid || o->xid_reuse_counter == XID_REUSE_MAX) {
  417. // generate xid
  418. BRandom_randomize((uint8_t *)&o->xid, sizeof(o->xid));
  419. // reset counter
  420. o->xid_reuse_counter = 0;
  421. }
  422. // increment counter
  423. o->xid_reuse_counter++;
  424. // send discover
  425. send_message(o, DHCP_MESSAGE_TYPE_DISCOVER, o->xid, 0, 0, 0, 0);
  426. // set timer
  427. BReactor_SetTimer(o->reactor, &o->reset_timer);
  428. // set state
  429. o->state = STATE_SENT_DISCOVER;
  430. }
  431. static void reset_timer_handler (BDHCPClientCore *o)
  432. {
  433. ASSERT(o->state == STATE_RESETTING || o->state == STATE_SENT_DISCOVER)
  434. DebugObject_Access(&o->d_obj);
  435. BLog(BLOG_INFO, "reset timer");
  436. start_process(o, (o->state == STATE_RESETTING));
  437. }
  438. static void request_timer_handler (BDHCPClientCore *o)
  439. {
  440. ASSERT(o->state == STATE_SENT_REQUEST)
  441. ASSERT(o->request_count >= 1)
  442. ASSERT(o->request_count <= MAX_REQUESTS)
  443. DebugObject_Access(&o->d_obj);
  444. // if we have sent enough requests, start again
  445. if (o->request_count == MAX_REQUESTS) {
  446. BLog(BLOG_INFO, "request timer, aborting");
  447. start_process(o, 0);
  448. return;
  449. }
  450. BLog(BLOG_INFO, "request timer, retrying");
  451. // send request
  452. send_message(o, DHCP_MESSAGE_TYPE_REQUEST, o->xid, 1, o->offered.yiaddr, 1, o->offered.dhcp_server_identifier);
  453. // start request timer
  454. BReactor_SetTimer(o->reactor, &o->request_timer);
  455. // increment request count
  456. o->request_count++;
  457. }
  458. static void renew_timer_handler (BDHCPClientCore *o)
  459. {
  460. ASSERT(o->state == STATE_FINISHED)
  461. DebugObject_Access(&o->d_obj);
  462. BLog(BLOG_INFO, "renew timer");
  463. // send request
  464. send_message(o, DHCP_MESSAGE_TYPE_REQUEST, o->xid, 1, o->offered.yiaddr, 0, 0);
  465. // start renew request timer
  466. BReactor_SetTimer(o->reactor, &o->renew_request_timer);
  467. // start lease timer
  468. BReactor_SetTimerAfter(o->reactor, &o->lease_timer, LEASE_TIMEOUT(o->acked.ip_address_lease_time));
  469. // set state
  470. o->state = STATE_RENEWING;
  471. }
  472. static void renew_request_timer_handler (BDHCPClientCore *o)
  473. {
  474. ASSERT(o->state == STATE_RENEWING)
  475. DebugObject_Access(&o->d_obj);
  476. BLog(BLOG_INFO, "renew request timer");
  477. // send request
  478. send_message(o, DHCP_MESSAGE_TYPE_REQUEST, o->xid, 1, o->offered.yiaddr, 0, 0);
  479. // start renew request timer
  480. BReactor_SetTimer(o->reactor, &o->renew_request_timer);
  481. }
  482. static void lease_timer_handler (BDHCPClientCore *o)
  483. {
  484. ASSERT(o->state == STATE_RENEWING)
  485. DebugObject_Access(&o->d_obj);
  486. BLog(BLOG_INFO, "lease timer");
  487. // stop renew request timer
  488. BReactor_RemoveTimer(o->reactor, &o->renew_request_timer);
  489. // start again now
  490. start_process(o, 1);
  491. // report to user
  492. report_down(o);
  493. return;
  494. }
  495. static bsize_t maybe_len (const char *str)
  496. {
  497. return bsize_fromsize(str ? strlen(str) : 0);
  498. }
  499. int BDHCPClientCore_Init (BDHCPClientCore *o, PacketPassInterface *send_if, PacketRecvInterface *recv_if, uint8_t *client_mac_addr, struct BDHCPClientCore_opts opts, BReactor *reactor, void *user,
  500. BDHCPClientCore_func_getsendermac func_getsendermac,
  501. BDHCPClientCore_handler handler)
  502. {
  503. ASSERT(PacketPassInterface_GetMTU(send_if) == PacketRecvInterface_GetMTU(recv_if))
  504. ASSERT(PacketPassInterface_GetMTU(send_if) >= 576 - IP_UDP_HEADERS_SIZE)
  505. ASSERT(func_getsendermac)
  506. ASSERT(handler)
  507. // init arguments
  508. o->send_if = send_if;
  509. o->recv_if = recv_if;
  510. memcpy(o->client_mac_addr, client_mac_addr, sizeof(o->client_mac_addr));
  511. o->reactor = reactor;
  512. o->user = user;
  513. o->func_getsendermac = func_getsendermac;
  514. o->handler = handler;
  515. o->hostname = NULL;
  516. o->vendorclassid = NULL;
  517. o->clientid = NULL;
  518. o->clientid_len = 0;
  519. // copy options
  520. if (opts.hostname && !(o->hostname = strdup(opts.hostname))) {
  521. BLog(BLOG_ERROR, "strdup failed");
  522. goto fail0;
  523. }
  524. if (opts.vendorclassid && !(o->vendorclassid = strdup(opts.vendorclassid))) {
  525. BLog(BLOG_ERROR, "strdup failed");
  526. goto fail0;
  527. }
  528. if (opts.clientid) {
  529. if (!(o->clientid = BAlloc(opts.clientid_len))) {
  530. BLog(BLOG_ERROR, "BAlloc failed");
  531. goto fail0;
  532. }
  533. memcpy(o->clientid, opts.clientid, opts.clientid_len);
  534. o->clientid_len = opts.clientid_len;
  535. }
  536. // make sure options aren't too long
  537. bsize_t opts_size = bsize_add(maybe_len(o->hostname), bsize_add(maybe_len(o->vendorclassid), bsize_fromsize(o->clientid_len)));
  538. if (opts_size.is_overflow || opts_size.value > 100) {
  539. BLog(BLOG_ERROR, "options too long together");
  540. goto fail0;
  541. }
  542. if (o->hostname && strlen(o->hostname) > 255) {
  543. BLog(BLOG_ERROR, "hostname too long");
  544. goto fail0;
  545. }
  546. if (o->vendorclassid && strlen(o->vendorclassid) > 255) {
  547. BLog(BLOG_ERROR, "vendorclassid too long");
  548. goto fail0;
  549. }
  550. if (o->clientid && o->clientid_len > 255) {
  551. BLog(BLOG_ERROR, "clientid too long");
  552. goto fail0;
  553. }
  554. // allocate buffers
  555. if (!(o->send_buf = BAlloc(PacketPassInterface_GetMTU(send_if)))) {
  556. BLog(BLOG_ERROR, "BAlloc send buf failed");
  557. goto fail0;
  558. }
  559. if (!(o->recv_buf = BAlloc(PacketRecvInterface_GetMTU(recv_if)))) {
  560. BLog(BLOG_ERROR, "BAlloc recv buf failed");
  561. goto fail1;
  562. }
  563. // init send interface
  564. PacketPassInterface_Sender_Init(o->send_if, (PacketPassInterface_handler_done)send_handler_done, o);
  565. // init receive interface
  566. PacketRecvInterface_Receiver_Init(o->recv_if, (PacketRecvInterface_handler_done)recv_handler_done, o);
  567. // set not sending
  568. o->sending = 0;
  569. // init timers
  570. BTimer_Init(&o->reset_timer, RESET_TIMEOUT, (BTimer_handler)reset_timer_handler, o);
  571. BTimer_Init(&o->request_timer, REQUEST_TIMEOUT, (BTimer_handler)request_timer_handler, o);
  572. BTimer_Init(&o->renew_timer, 0, (BTimer_handler)renew_timer_handler, o);
  573. BTimer_Init(&o->renew_request_timer, RENEW_REQUEST_TIMEOUT, (BTimer_handler)renew_request_timer_handler, o);
  574. BTimer_Init(&o->lease_timer, 0, (BTimer_handler)lease_timer_handler, o);
  575. // start receving
  576. PacketRecvInterface_Receiver_Recv(o->recv_if, (uint8_t *)o->recv_buf);
  577. // start
  578. start_process(o, 1);
  579. DebugObject_Init(&o->d_obj);
  580. return 1;
  581. fail1:
  582. BFree(o->send_buf);
  583. fail0:
  584. BFree(o->clientid);
  585. free(o->vendorclassid);
  586. free(o->hostname);
  587. return 0;
  588. }
  589. void BDHCPClientCore_Free (BDHCPClientCore *o)
  590. {
  591. DebugObject_Free(&o->d_obj);
  592. // free timers
  593. BReactor_RemoveTimer(o->reactor, &o->lease_timer);
  594. BReactor_RemoveTimer(o->reactor, &o->renew_request_timer);
  595. BReactor_RemoveTimer(o->reactor, &o->renew_timer);
  596. BReactor_RemoveTimer(o->reactor, &o->request_timer);
  597. BReactor_RemoveTimer(o->reactor, &o->reset_timer);
  598. // free buffers
  599. BFree(o->recv_buf);
  600. BFree(o->send_buf);
  601. // free options
  602. BFree(o->clientid);
  603. free(o->vendorclassid);
  604. free(o->hostname);
  605. }
  606. void BDHCPClientCore_GetClientIP (BDHCPClientCore *o, uint32_t *out_ip)
  607. {
  608. ASSERT(o->state == STATE_FINISHED || o->state == STATE_RENEWING)
  609. DebugObject_Access(&o->d_obj);
  610. *out_ip = o->offered.yiaddr;
  611. }
  612. void BDHCPClientCore_GetClientMask (BDHCPClientCore *o, uint32_t *out_mask)
  613. {
  614. ASSERT(o->state == STATE_FINISHED || o->state == STATE_RENEWING)
  615. DebugObject_Access(&o->d_obj);
  616. *out_mask = o->acked.subnet_mask;
  617. }
  618. int BDHCPClientCore_GetRouter (BDHCPClientCore *o, uint32_t *out_router)
  619. {
  620. ASSERT(o->state == STATE_FINISHED || o->state == STATE_RENEWING)
  621. DebugObject_Access(&o->d_obj);
  622. if (!o->acked.have_router) {
  623. return 0;
  624. }
  625. *out_router = o->acked.router;
  626. return 1;
  627. }
  628. int BDHCPClientCore_GetDNS (BDHCPClientCore *o, uint32_t *out_dns_servers, size_t max_dns_servers)
  629. {
  630. ASSERT(o->state == STATE_FINISHED || o->state == STATE_RENEWING)
  631. DebugObject_Access(&o->d_obj);
  632. int num_return = bmin_int(o->acked.domain_name_servers_count, max_dns_servers);
  633. memcpy(out_dns_servers, o->acked.domain_name_servers, num_return * sizeof(uint32_t));
  634. return num_return;
  635. }
  636. void BDHCPClientCore_GetServerMAC (BDHCPClientCore *o, uint8_t *out_mac)
  637. {
  638. DebugObject_Access(&o->d_obj);
  639. ASSERT(o->state == STATE_FINISHED || o->state == STATE_RENEWING)
  640. memcpy(out_mac, o->acked.server_mac, 6);
  641. }