-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathdhcpserver.c
1338 lines (1108 loc) · 38 KB
/
dhcpserver.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//#include "esp_common.h"
#include <stdlib.h>
#include <string.h>
#include "lwip/inet.h"
#include "lwip/err.h"
#include "lwip/pbuf.h"
#include "lwip/udp.h"
#include "lwip/mem.h"
#include "lwip/ip_addr.h"
#include "tcpip_adapter.h"
#include "dhcpserver/dhcpserver.h"
#include "dhcpserver/dhcpserver_options.h"
#if ESP_DHCP
#define BOOTP_BROADCAST 0x8000
#define DHCP_REQUEST 1
#define DHCP_REPLY 2
#define DHCP_HTYPE_ETHERNET 1
#define DHCP_HLEN_ETHERNET 6
#define DHCP_MSG_LEN 236
#define DHCPS_SERVER_PORT 67
#define DHCPS_CLIENT_PORT 68
#define DHCPDISCOVER 1
#define DHCPOFFER 2
#define DHCPREQUEST 3
#define DHCPDECLINE 4
#define DHCPACK 5
#define DHCPNAK 6
#define DHCPRELEASE 7
#define DHCP_OPTION_SUBNET_MASK 1
#define DHCP_OPTION_ROUTER 3
#define DHCP_OPTION_DNS_SERVER 6
#define DHCP_OPTION_REQ_IPADDR 50
#define DHCP_OPTION_LEASE_TIME 51
#define DHCP_OPTION_MSG_TYPE 53
#define DHCP_OPTION_SERVER_ID 54
#define DHCP_OPTION_INTERFACE_MTU 26
#define DHCP_OPTION_PERFORM_ROUTER_DISCOVERY 31
#define DHCP_OPTION_BROADCAST_ADDRESS 28
#define DHCP_OPTION_REQ_LIST 55
#define DHCP_OPTION_END 255
//#define USE_CLASS_B_NET 1
#define DHCPS_DEBUG 0
#define DHCPS_LOG printf
#define MAX_STATION_NUM CONFIG_LWIP_DHCPS_MAX_STATION_NUM
#define DHCPS_STATE_OFFER 1
#define DHCPS_STATE_DECLINE 2
#define DHCPS_STATE_ACK 3
#define DHCPS_STATE_NAK 4
#define DHCPS_STATE_IDLE 5
#define DHCPS_STATE_RELEASE 6
typedef struct _list_node {
void *pnode;
struct _list_node *pnext;
} list_node;
////////////////////////////////////////////////////////////////////////////////////
static const u32_t magic_cookie = 0x63538263;
static struct netif *dhcps_netif = NULL;
static ip4_addr_t broadcast_dhcps;
static ip4_addr_t server_address;
static ip4_addr_t dns_server = {0};
static ip4_addr_t client_address; //added
static ip4_addr_t client_address_plus;
static ip4_addr_t s_dhcps_mask = {
#ifdef USE_CLASS_B_NET
.addr = PP_HTONL(LWIP_MAKEU32(255, 240, 0, 0))
#else
.addr = PP_HTONL(LWIP_MAKEU32(255, 255, 255, 0))
#endif
};
static list_node *plist = NULL;
static bool renew = false;
static dhcps_lease_t dhcps_poll;
static dhcps_time_t dhcps_lease_time = DHCPS_LEASE_TIME_DEF; //minute
static dhcps_offer_t dhcps_offer = 0xFF;
static dhcps_offer_t dhcps_dns = 0x00;
static dhcps_cb_t dhcps_cb;
/******************************************************************************
* FunctionName : dhcps_option_info
* Description : get the DHCP message option info
* Parameters : op_id -- DHCP message option id
* opt_len -- DHCP message option length
* Returns : DHCP message option addr
*******************************************************************************/
void *dhcps_option_info(u8_t op_id, u32_t opt_len)
{
void *option_arg = NULL;
switch (op_id) {
case IP_ADDRESS_LEASE_TIME:
if (opt_len == sizeof(dhcps_time_t)) {
option_arg = &dhcps_lease_time;
}
break;
case REQUESTED_IP_ADDRESS:
if (opt_len == sizeof(dhcps_lease_t)) {
option_arg = &dhcps_poll;
}
break;
case ROUTER_SOLICITATION_ADDRESS:
if (opt_len == sizeof(dhcps_offer_t)) {
option_arg = &dhcps_offer;
}
break;
case DOMAIN_NAME_SERVER:
if (opt_len == sizeof(dhcps_offer_t)) {
option_arg = &dhcps_dns;
}
break;
case SUBNET_MASK:
if (opt_len == sizeof(s_dhcps_mask)) {
option_arg = &s_dhcps_mask;
}
break;
default:
break;
}
return option_arg;
}
/******************************************************************************
* FunctionName : dhcps_set_option_info
* Description : set the DHCP message option info
* Parameters : op_id -- DHCP message option id
* opt_info -- DHCP message option info
* opt_len -- DHCP message option length
* Returns : none
*******************************************************************************/
void dhcps_set_option_info(u8_t op_id, void *opt_info, u32_t opt_len)
{
if (opt_info == NULL) {
return;
}
switch (op_id) {
case IP_ADDRESS_LEASE_TIME:
if (opt_len == sizeof(dhcps_time_t)) {
dhcps_lease_time = *(dhcps_time_t *)opt_info;
}
break;
case REQUESTED_IP_ADDRESS:
if (opt_len == sizeof(dhcps_lease_t)) {
dhcps_poll = *(dhcps_lease_t *)opt_info;
}
break;
case ROUTER_SOLICITATION_ADDRESS:
if (opt_len == sizeof(dhcps_offer_t)) {
dhcps_offer = *(dhcps_offer_t *)opt_info;
}
break;
case DOMAIN_NAME_SERVER:
if (opt_len == sizeof(dhcps_offer_t)) {
dhcps_dns = *(dhcps_offer_t *)opt_info;
}
break;
case SUBNET_MASK:
if (opt_len == sizeof(s_dhcps_mask)) {
s_dhcps_mask = *(ip4_addr_t *)opt_info;
}
default:
break;
}
return;
}
/******************************************************************************
* FunctionName : node_insert_to_list
* Description : insert the node to the list
* Parameters : phead -- the head node of the list
* pinsert -- the insert node of the list
* Returns : none
*******************************************************************************/
static void node_insert_to_list(list_node **phead, list_node *pinsert)
{
list_node *plist = NULL;
struct dhcps_pool *pdhcps_pool = NULL;
struct dhcps_pool *pdhcps_node = NULL;
if (*phead == NULL) {
*phead = pinsert;
} else {
plist = *phead;
pdhcps_node = pinsert->pnode;
pdhcps_pool = plist->pnode;
if (pdhcps_node->ip.addr < pdhcps_pool->ip.addr) {
pinsert->pnext = plist;
*phead = pinsert;
} else {
while (plist->pnext != NULL) {
pdhcps_pool = plist->pnext->pnode;
if (pdhcps_node->ip.addr < pdhcps_pool->ip.addr) {
pinsert->pnext = plist->pnext;
plist->pnext = pinsert;
break;
}
plist = plist->pnext;
}
if (plist->pnext == NULL) {
plist->pnext = pinsert;
}
}
}
// pinsert->pnext = NULL;
}
/******************************************************************************
* FunctionName : node_delete_from_list
* Description : remove the node from list
* Parameters : phead -- the head node of the list
* pdelete -- the remove node of the list
* Returns : none
*******************************************************************************/
void node_remove_from_list(list_node **phead, list_node *pdelete)
{
list_node *plist = NULL;
plist = *phead;
if (plist == NULL) {
*phead = NULL;
} else {
if (plist == pdelete) {
// Note: Ignoring the "use after free" warnings, as it could only happen
// if the linked list contains loops
*phead = plist->pnext; // NOLINT(clang-analyzer-unix.Malloc)
pdelete->pnext = NULL;
} else {
while (plist != NULL) {
if (plist->pnext == pdelete) { // NOLINT(clang-analyzer-unix.Malloc)
plist->pnext = pdelete->pnext;
pdelete->pnext = NULL;
}
plist = plist->pnext;
}
}
}
}
/******************************************************************************
* FunctionName : add_msg_type
* Description : add TYPE option of DHCP message
* Parameters : optptr -- the addr of DHCP message option
* Returns : the addr of DHCP message option
*******************************************************************************/
static u8_t *add_msg_type(u8_t *optptr, u8_t type)
{
*optptr++ = DHCP_OPTION_MSG_TYPE;
*optptr++ = 1;
*optptr++ = type;
return optptr;
}
/******************************************************************************
* FunctionName : add_offer_options
* Description : add OFFER option of DHCP message
* Parameters : optptr -- the addr of DHCP message option
* Returns : the addr of DHCP message option
*******************************************************************************/
static u8_t *add_offer_options(u8_t *optptr)
{
ip4_addr_t ipadd;
ipadd.addr = *((u32_t *) &server_address);
*optptr++ = DHCP_OPTION_SUBNET_MASK;
*optptr++ = 4;
*optptr++ = ip4_addr1(&s_dhcps_mask);
*optptr++ = ip4_addr2(&s_dhcps_mask);
*optptr++ = ip4_addr3(&s_dhcps_mask);
*optptr++ = ip4_addr4(&s_dhcps_mask);
*optptr++ = DHCP_OPTION_LEASE_TIME;
*optptr++ = 4;
*optptr++ = ((dhcps_lease_time * DHCPS_LEASE_UNIT) >> 24) & 0xFF;
*optptr++ = ((dhcps_lease_time * DHCPS_LEASE_UNIT) >> 16) & 0xFF;
*optptr++ = ((dhcps_lease_time * DHCPS_LEASE_UNIT) >> 8) & 0xFF;
*optptr++ = ((dhcps_lease_time * DHCPS_LEASE_UNIT) >> 0) & 0xFF;
*optptr++ = DHCP_OPTION_SERVER_ID;
*optptr++ = 4;
*optptr++ = ip4_addr1(&ipadd);
*optptr++ = ip4_addr2(&ipadd);
*optptr++ = ip4_addr3(&ipadd);
*optptr++ = ip4_addr4(&ipadd);
if (dhcps_router_enabled(dhcps_offer)) {
tcpip_adapter_ip_info_t if_ip;
//bzero(&if_ip, sizeof(struct ip_info));
memset(&if_ip , 0x00, sizeof(tcpip_adapter_ip_info_t));
tcpip_adapter_get_ip_info(ESP_IF_WIFI_AP, &if_ip);
if (!ip4_addr_isany_val(if_ip.gw)) {
*optptr++ = DHCP_OPTION_ROUTER;
*optptr++ = 4;
*optptr++ = ip4_addr1(&if_ip.gw);
*optptr++ = ip4_addr2(&if_ip.gw);
*optptr++ = ip4_addr3(&if_ip.gw);
*optptr++ = ip4_addr4(&if_ip.gw);
}
}
*optptr++ = DHCP_OPTION_DNS_SERVER;
*optptr++ = 4;
if (dhcps_dns_enabled(dhcps_dns)) {
*optptr++ = ip4_addr1(&dns_server);
*optptr++ = ip4_addr2(&dns_server);
*optptr++ = ip4_addr3(&dns_server);
*optptr++ = ip4_addr4(&dns_server);
}else {
*optptr++ = ip4_addr1(&ipadd);
*optptr++ = ip4_addr2(&ipadd);
*optptr++ = ip4_addr3(&ipadd);
*optptr++ = ip4_addr4(&ipadd);
}
ip4_addr_t broadcast_addr = { .addr = (ipadd.addr & s_dhcps_mask.addr) | ~s_dhcps_mask.addr };
*optptr++ = DHCP_OPTION_BROADCAST_ADDRESS;
*optptr++ = 4;
*optptr++ = ip4_addr1(&broadcast_addr);
*optptr++ = ip4_addr2(&broadcast_addr);
*optptr++ = ip4_addr3(&broadcast_addr);
*optptr++ = ip4_addr4(&broadcast_addr);
*optptr++ = DHCP_OPTION_INTERFACE_MTU;
*optptr++ = 2;
*optptr++ = 0x05;
*optptr++ = 0xdc;
*optptr++ = DHCP_OPTION_PERFORM_ROUTER_DISCOVERY;
*optptr++ = 1;
*optptr++ = 0x00;
*optptr++ = 43;
*optptr++ = 6;
*optptr++ = 0x01;
*optptr++ = 4;
*optptr++ = 0x00;
*optptr++ = 0x00;
*optptr++ = 0x00;
*optptr++ = 0x02;
return optptr;
}
/******************************************************************************
* FunctionName : add_end
* Description : add end option of DHCP message
* Parameters : optptr -- the addr of DHCP message option
* Returns : the addr of DHCP message option
*******************************************************************************/
static u8_t *add_end(u8_t *optptr)
{
*optptr++ = DHCP_OPTION_END;
return optptr;
}
/******************************************************************************
* FunctionName : create_msg
* Description : create response message
* Parameters : m -- DHCP message info
* Returns : none
*******************************************************************************/
static void create_msg(struct dhcps_msg *m)
{
ip4_addr_t client;
client.addr = *((uint32_t *) &client_address);
m->op = DHCP_REPLY;
m->htype = DHCP_HTYPE_ETHERNET;
m->hlen = 6;
m->hops = 0;
// os_memcpy((char *) xid, (char *) m->xid, sizeof(m->xid));
m->secs = 0;
m->flags = htons(BOOTP_BROADCAST);
memcpy((char *) m->yiaddr, (char *) &client.addr, sizeof(m->yiaddr));
memset((char *) m->ciaddr, 0, sizeof(m->ciaddr));
memset((char *) m->siaddr, 0, sizeof(m->siaddr));
memset((char *) m->giaddr, 0, sizeof(m->giaddr));
memset((char *) m->sname, 0, sizeof(m->sname));
memset((char *) m->file, 0, sizeof(m->file));
memset((char *) m->options, 0, sizeof(m->options));
u32_t magic_cookie_temp = magic_cookie;
memcpy((char *) m->options, &magic_cookie_temp, sizeof(magic_cookie_temp));
}
struct pbuf * dhcps_pbuf_alloc(u16_t len)
{
u16_t mlen = sizeof(struct dhcps_msg);
if (len > mlen) {
#if DHCPS_DEBUG
DHCPS_LOG("dhcps: len=%d mlen=%d", len, mlen);
#endif
mlen = len;
}
return pbuf_alloc(PBUF_TRANSPORT, mlen, PBUF_RAM);
}
/******************************************************************************
* FunctionName : send_offer
* Description : DHCP message OFFER Response
* Parameters : m -- DHCP message info
* Returns : none
*******************************************************************************/
static void send_offer(struct dhcps_msg *m, u16_t len)
{
u8_t *end;
struct pbuf *p, *q;
u8_t *data;
u16_t cnt = 0;
u16_t i;
#if DHCPS_DEBUG
err_t SendOffer_err_t;
#endif
create_msg(m);
end = add_msg_type(&m->options[4], DHCPOFFER);
end = add_offer_options(end);
end = add_end(end);
p = dhcps_pbuf_alloc(len);
#if DHCPS_DEBUG
DHCPS_LOG("udhcp: send_offer>>p->ref = %d\n", p->ref);
#endif
if (p != NULL) {
#if DHCPS_DEBUG
DHCPS_LOG("dhcps: send_offer>>pbuf_alloc succeed\n");
DHCPS_LOG("dhcps: send_offer>>p->tot_len = %d\n", p->tot_len);
DHCPS_LOG("dhcps: send_offer>>p->len = %d\n", p->len);
#endif
q = p;
while (q != NULL) {
data = (u8_t *)q->payload;
for (i = 0; i < q->len; i++) {
data[i] = ((u8_t *) m)[cnt++];
#if DHCPS_DEBUG
DHCPS_LOG("%02x ", data[i]);
if ((i + 1) % 16 == 0) {
DHCPS_LOG("\n");
}
#endif
}
q = q->next;
}
} else {
#if DHCPS_DEBUG
DHCPS_LOG("dhcps: send_offer>>pbuf_alloc failed\n");
#endif
return;
}
ip_addr_t ip_temp = IPADDR4_INIT(0x0);
ip4_addr_set(ip_2_ip4(&ip_temp), &broadcast_dhcps);
struct udp_pcb *pcb_dhcps = dhcps_netif->dhcps_pcb;
#if DHCPS_DEBUG
SendOffer_err_t = udp_sendto(pcb_dhcps, p, &ip_temp, DHCPS_CLIENT_PORT);
DHCPS_LOG("dhcps: send_offer>>udp_sendto result %x\n", SendOffer_err_t);
#else
udp_sendto(pcb_dhcps, p, &ip_temp, DHCPS_CLIENT_PORT);
#endif
if (p->ref != 0) {
#if DHCPS_DEBUG
DHCPS_LOG("udhcp: send_offer>>free pbuf\n");
#endif
pbuf_free(p);
}
}
/******************************************************************************
* FunctionName : send_nak
* Description : DHCP message NACK Response
* Parameters : m -- DHCP message info
* Returns : none
*******************************************************************************/
static void send_nak(struct dhcps_msg *m, u16_t len)
{
u8_t *end;
struct pbuf *p, *q;
u8_t *data;
u16_t cnt = 0;
u16_t i;
#if DHCPS_DEBUG
err_t SendNak_err_t;
#endif
create_msg(m);
end = add_msg_type(&m->options[4], DHCPNAK);
end = add_end(end);
p = dhcps_pbuf_alloc(len);
#if DHCPS_DEBUG
DHCPS_LOG("udhcp: send_nak>>p->ref = %d\n", p->ref);
#endif
if (p != NULL) {
#if DHCPS_DEBUG
DHCPS_LOG("dhcps: send_nak>>pbuf_alloc succeed\n");
DHCPS_LOG("dhcps: send_nak>>p->tot_len = %d\n", p->tot_len);
DHCPS_LOG("dhcps: send_nak>>p->len = %d\n", p->len);
#endif
q = p;
while (q != NULL) {
data = (u8_t *)q->payload;
for (i = 0; i < q->len; i++) {
data[i] = ((u8_t *) m)[cnt++];
#if DHCPS_DEBUG
DHCPS_LOG("%02x ", data[i]);
if ((i + 1) % 16 == 0) {
DHCPS_LOG("\n");
}
#endif
}
q = q->next;
}
} else {
#if DHCPS_DEBUG
DHCPS_LOG("dhcps: send_nak>>pbuf_alloc failed\n");
#endif
return;
}
ip_addr_t ip_temp = IPADDR4_INIT(0x0);
ip4_addr_set(ip_2_ip4(&ip_temp), &broadcast_dhcps);
struct udp_pcb *pcb_dhcps = dhcps_netif->dhcps_pcb;
#if DHCPS_DEBUG
SendNak_err_t = udp_sendto(pcb_dhcps, p, &ip_temp, DHCPS_CLIENT_PORT);
DHCPS_LOG("dhcps: send_nak>>udp_sendto result %x\n", SendNak_err_t);
#else
udp_sendto(pcb_dhcps, p, &ip_temp, DHCPS_CLIENT_PORT);
#endif
if (p->ref != 0) {
#if DHCPS_DEBUG
DHCPS_LOG("udhcp: send_nak>>free pbuf\n");
#endif
pbuf_free(p);
}
}
/******************************************************************************
* FunctionName : send_ack
* Description : DHCP message ACK Response
* Parameters : m -- DHCP message info
* Returns : none
*******************************************************************************/
static void send_ack(struct dhcps_msg *m, u16_t len)
{
u8_t *end;
struct pbuf *p, *q;
u8_t *data;
u16_t cnt = 0;
u16_t i;
err_t SendAck_err_t;
create_msg(m);
end = add_msg_type(&m->options[4], DHCPACK);
end = add_offer_options(end);
end = add_end(end);
p = dhcps_pbuf_alloc(len);
#if DHCPS_DEBUG
DHCPS_LOG("udhcp: send_ack>>p->ref = %d\n", p->ref);
#endif
if (p != NULL) {
#if DHCPS_DEBUG
DHCPS_LOG("dhcps: send_ack>>pbuf_alloc succeed\n");
DHCPS_LOG("dhcps: send_ack>>p->tot_len = %d\n", p->tot_len);
DHCPS_LOG("dhcps: send_ack>>p->len = %d\n", p->len);
#endif
q = p;
while (q != NULL) {
data = (u8_t *)q->payload;
for (i = 0; i < q->len; i++) {
data[i] = ((u8_t *) m)[cnt++];
#if DHCPS_DEBUG
DHCPS_LOG("%02x ", data[i]);
if ((i + 1) % 16 == 0) {
DHCPS_LOG("\n");
}
#endif
}
q = q->next;
}
} else {
#if DHCPS_DEBUG
DHCPS_LOG("dhcps: send_ack>>pbuf_alloc failed\n");
#endif
return;
}
ip_addr_t ip_temp = IPADDR4_INIT(0x0);
ip4_addr_set(ip_2_ip4(&ip_temp), &broadcast_dhcps);
struct udp_pcb *pcb_dhcps = dhcps_netif->dhcps_pcb;
SendAck_err_t = udp_sendto(pcb_dhcps, p, &ip_temp, DHCPS_CLIENT_PORT);
#if DHCPS_DEBUG
DHCPS_LOG("dhcps: send_ack>>udp_sendto result %x\n", SendAck_err_t);
#endif
if (SendAck_err_t == ERR_OK) {
dhcps_cb(m->yiaddr);
}
if (p->ref != 0) {
#if DHCPS_DEBUG
DHCPS_LOG("udhcp: send_ack>>free pbuf\n");
#endif
pbuf_free(p);
}
}
/******************************************************************************
* FunctionName : parse_options
* Description : parse DHCP message options
* Parameters : optptr -- DHCP message option info
* len -- DHCP message option length
* Returns : none
*******************************************************************************/
static u8_t parse_options(u8_t *optptr, s16_t len)
{
ip4_addr_t client;
bool is_dhcp_parse_end = false;
struct dhcps_state s;
client.addr = *((uint32_t *) &client_address);
u8_t *end = optptr + len;
u16_t type = 0;
s.state = DHCPS_STATE_IDLE;
while (optptr < end) {
#if DHCPS_DEBUG
DHCPS_LOG("dhcps: (s16_t)*optptr = %d\n", (s16_t)*optptr);
#endif
switch ((s16_t) *optptr) {
case DHCP_OPTION_MSG_TYPE: //53
type = *(optptr + 2);
break;
case DHCP_OPTION_REQ_IPADDR://50
if (memcmp((char *) &client.addr, (char *) optptr + 2, 4) == 0) {
#if DHCPS_DEBUG
DHCPS_LOG("dhcps: DHCP_OPTION_REQ_IPADDR = 0 ok\n");
#endif
s.state = DHCPS_STATE_ACK;
} else {
#if DHCPS_DEBUG
DHCPS_LOG("dhcps: DHCP_OPTION_REQ_IPADDR != 0 err\n");
#endif
s.state = DHCPS_STATE_NAK;
}
break;
case DHCP_OPTION_END: {
is_dhcp_parse_end = true;
}
break;
}
if (is_dhcp_parse_end) {
break;
}
optptr += optptr[1] + 2;
}
switch (type) {
case DHCPDISCOVER://1
s.state = DHCPS_STATE_OFFER;
#if DHCPS_DEBUG
DHCPS_LOG("dhcps: DHCPD_STATE_OFFER\n");
#endif
break;
case DHCPREQUEST://3
if (!(s.state == DHCPS_STATE_ACK || s.state == DHCPS_STATE_NAK)) {
if (renew == true) {
s.state = DHCPS_STATE_ACK;
} else {
s.state = DHCPS_STATE_NAK;
}
#if DHCPS_DEBUG
DHCPS_LOG("dhcps: DHCPD_STATE_NAK\n");
#endif
}
break;
case DHCPDECLINE://4
s.state = DHCPS_STATE_IDLE;
#if DHCPS_DEBUG
DHCPS_LOG("dhcps: DHCPD_STATE_IDLE\n");
#endif
break;
case DHCPRELEASE://7
s.state = DHCPS_STATE_RELEASE;
#if DHCPS_DEBUG
DHCPS_LOG("dhcps: DHCPD_STATE_IDLE\n");
#endif
break;
}
#if DHCPS_DEBUG
DHCPS_LOG("dhcps: return s.state = %d\n", s.state);
#endif
return s.state;
}
/******************************************************************************
* FunctionName : parse_msg
* Description : parse DHCP message from netif
* Parameters : m -- DHCP message info
* len -- DHCP message length
* Returns : DHCP message type
*******************************************************************************/
static s16_t parse_msg(struct dhcps_msg *m, u16_t len)
{
u32_t lease_timer = (dhcps_lease_time * DHCPS_LEASE_UNIT)/DHCPS_COARSE_TIMER_SECS;
if (memcmp((char *)m->options, &magic_cookie, sizeof(magic_cookie)) == 0) {
#if DHCPS_DEBUG
DHCPS_LOG("dhcps: len = %d\n", len);
#endif
ip4_addr_t addr_tmp;
struct dhcps_pool *pdhcps_pool = NULL;
list_node *pnode = NULL;
list_node *pback_node = NULL;
ip4_addr_t first_address;
bool flag = false;
first_address.addr = dhcps_poll.start_ip.addr;
client_address.addr = client_address_plus.addr;
renew = false;
if (plist != NULL) {
for (pback_node = plist; pback_node != NULL; pback_node = pback_node->pnext) {
pdhcps_pool = pback_node->pnode;
if (memcmp(pdhcps_pool->mac, m->chaddr, sizeof(pdhcps_pool->mac)) == 0) {
if (memcmp(&pdhcps_pool->ip.addr, m->ciaddr, sizeof(pdhcps_pool->ip.addr)) == 0) {
renew = true;
}
client_address.addr = pdhcps_pool->ip.addr;
pdhcps_pool->lease_timer = lease_timer;
pnode = pback_node;
goto POOL_CHECK;
} else if (pdhcps_pool->ip.addr == client_address_plus.addr) {
addr_tmp.addr = htonl(client_address_plus.addr);
addr_tmp.addr++;
client_address_plus.addr = htonl(addr_tmp.addr);
client_address.addr = client_address_plus.addr;
}
if (flag == false) { // search the fisrt unused ip
if (first_address.addr < pdhcps_pool->ip.addr) {
flag = true;
} else {
addr_tmp.addr = htonl(first_address.addr);
addr_tmp.addr++;
first_address.addr = htonl(addr_tmp.addr);
}
}
}
} else {
client_address.addr = dhcps_poll.start_ip.addr;
}
if (client_address_plus.addr > dhcps_poll.end_ip.addr) {
client_address.addr = first_address.addr;
}
if (client_address.addr > dhcps_poll.end_ip.addr) {
client_address_plus.addr = dhcps_poll.start_ip.addr;
pdhcps_pool = NULL;
pnode = NULL;
} else {
pdhcps_pool = (struct dhcps_pool *)mem_malloc(sizeof(struct dhcps_pool));
memset(pdhcps_pool , 0x00 , sizeof(struct dhcps_pool));
pdhcps_pool->ip.addr = client_address.addr;
memcpy(pdhcps_pool->mac, m->chaddr, sizeof(pdhcps_pool->mac));
pdhcps_pool->lease_timer = lease_timer;
pnode = (list_node *)mem_malloc(sizeof(list_node));
memset(pnode , 0x00 , sizeof(list_node));
pnode->pnode = pdhcps_pool;
pnode->pnext = NULL;
node_insert_to_list(&plist, pnode);
if (client_address.addr == dhcps_poll.end_ip.addr) {
client_address_plus.addr = dhcps_poll.start_ip.addr;
} else {
addr_tmp.addr = htonl(client_address.addr);
addr_tmp.addr++;
client_address_plus.addr = htonl(addr_tmp.addr);
}
}
POOL_CHECK:
if ((client_address.addr > dhcps_poll.end_ip.addr) || (ip4_addr_isany(&client_address))) {
if (pnode != NULL) {
node_remove_from_list(&plist, pnode);
free(pnode);
pnode = NULL;
}
if (pdhcps_pool != NULL) {
free(pdhcps_pool);
pdhcps_pool = NULL;
}
return 4;
}
s16_t ret = parse_options(&m->options[4], len);;
if (ret == DHCPS_STATE_RELEASE) {
if (pnode != NULL) {
node_remove_from_list(&plist, pnode);
free(pnode);
pnode = NULL;
}
if (pdhcps_pool != NULL) {
free(pdhcps_pool);
pdhcps_pool = NULL;
}
memset(&client_address, 0x0, sizeof(client_address));
}
#if DHCPS_DEBUG
DHCPS_LOG("dhcps: xid changed\n");
DHCPS_LOG("dhcps: client_address.addr = %x\n", client_address.addr);
#endif
return ret;
}
return 0;
}
/******************************************************************************
* FunctionName : handle_dhcp
* Description : If an incoming DHCP message is in response to us, then trigger the state machine
* Parameters : arg -- arg user supplied argument (udp_pcb.recv_arg)
* pcb -- the udp_pcb which received data
* p -- the packet buffer that was received
* addr -- the remote IP address from which the packet was received
* port -- the remote port from which the packet was received
* Returns : none
*******************************************************************************/
static void handle_dhcp(void *arg,
struct udp_pcb *pcb,
struct pbuf *p,
const ip_addr_t *addr,
u16_t port)
{
struct dhcps_msg *pmsg_dhcps = NULL;
s16_t tlen, malloc_len;
u16_t i;
u16_t dhcps_msg_cnt = 0;
u8_t *p_dhcps_msg = NULL;
u8_t *data;
#if DHCPS_DEBUG
DHCPS_LOG("dhcps: handle_dhcp-> receive a packet\n");
#endif
if (p == NULL) {
return;
}
malloc_len = sizeof(struct dhcps_msg);
#if DHCPS_DEBUG
DHCPS_LOG("dhcps: handle_dhcp malloc_len=%d rx_len=%d", malloc_len, p->tot_len);
#endif
if (malloc_len < p->tot_len) {
malloc_len = p->tot_len;
}
pmsg_dhcps = (struct dhcps_msg *)mem_malloc(malloc_len);
if (NULL == pmsg_dhcps) {
pbuf_free(p);
return;
}
memset(pmsg_dhcps , 0x00 , malloc_len);
p_dhcps_msg = (u8_t *)pmsg_dhcps;
tlen = p->tot_len;
data = p->payload;
#if DHCPS_DEBUG
DHCPS_LOG("dhcps: handle_dhcp-> p->tot_len = %d\n", tlen);
DHCPS_LOG("dhcps: handle_dhcp-> p->len = %d\n", p->len);
#endif
for (i = 0; i < p->len; i++) {
p_dhcps_msg[dhcps_msg_cnt++] = data[i];