BDHCPClientCore.c 24 KB

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