BDHCPClientCore.c 25 KB

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