forked from fritz0705/dhcpd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
packet.c
116 lines (82 loc) · 2.61 KB
/
packet.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
#include "packet.h"
#include <stdlib.h>
#include <errno.h>
#include "error.h"
struct sockaddr_in server_id;
struct sockaddr_in broadcast = {
.sin_family = AF_INET,
.sin_addr = {INADDR_BROADCAST},
};
bool send_offer(int socket, struct dhcp_msg *m, struct dhcp_lease *l) {
uint8_t *buf = malloc(DHCP_MSG_LEN);
if(!buf) {
dhcpd_error(ENOMEM, 1, "Could not send DHCPOFFER");
return false;
}
size_t send_len = 0;
uint8_t *options = NULL;
dhcp_msg_reply(buf, &options, &send_len, m, DHCPOFFER);
ARRAY_COPY(DHCP_MSG_F_YIADDR(buf), &l->address, 4);
dhcp_opt_insert_val(buf, DHCP_MSG_LEN, &send_len, &options, DHCP_OPT_SERVERID, uint32_t, m->sid->sin_addr.s_addr);
options = dhcp_opt_add_lease(options, &send_len, l);
*options = DHCP_OPT_END;
DHCP_OPT_CONT(options, send_len);
// if (debug)
// msg_debug(&((struct dhcp_msg){.data = send_buffer, .length = send_len }), 1);
int err = sendto(socket, buf, send_len, MSG_DONTWAIT,
(struct sockaddr *)&broadcast, sizeof broadcast);
if (err < 0) {
dhcpd_error(errno, 1, "Could not send DHCPOFFER");
return false;
}
free(buf);
return true;
}
bool send_ack(int socket, struct dhcp_msg *m, struct dhcp_lease *l) {
uint8_t *buf = malloc(DHCP_MSG_LEN);
if(!buf) {
dhcpd_error(ENOMEM, 1, "Could not send DHCPOFFER");
return false;
}
size_t send_len = 0;
uint8_t *options = NULL;
// ACK
dhcp_msg_reply(buf, &options, &send_len, m, DHCPACK);
ARRAY_COPY(DHCP_MSG_F_YIADDR(buf), &l->address, 4);
dhcp_opt_insert_val(buf, DHCP_MSG_LEN, &send_len, &options, DHCP_OPT_SERVERID, uint32_t, m->sid->sin_addr.s_addr);
options = dhcp_opt_add_lease(options, &send_len, l);
*options = DHCP_OPT_END;
DHCP_OPT_CONT(options, send_len);
// if (debug)
// msg_debug(&((struct dhcp_msg){.data = buf, .length = send_len }), 1);
int err = sendto(socket, buf, send_len, MSG_DONTWAIT,
(struct sockaddr *)&broadcast, sizeof broadcast);
if (err < 0) {
dhcpd_error(0, 1, "Could not send DHCPACK");
return false;
}
free(buf);
return true;
}
bool send_nak(int socket, struct dhcp_msg *m) {
uint8_t *buf = malloc(DHCP_MSG_LEN);
if(!buf) {
dhcpd_error(ENOMEM, 1, "Could not send DHCPOFFER");
return false;
}
size_t send_len = 0;
uint8_t *options = NULL;
dhcp_msg_reply(buf, &options, &send_len, m, DHCPNAK);
options[0] = DHCP_OPT_END;
DHCP_OPT_CONT(options, send_len);
// if (debug)
// msg_debug(&((struct dhcp_msg){.data = buf, .length = send_len }), 1);
int err = sendto(socket, buf, send_len, MSG_DONTWAIT,
(struct sockaddr *)&broadcast, sizeof broadcast);
if (err < 0) {
dhcpd_error(0, 1, "Could not send DHCPNAK");
return false;
}
free(buf);
return true;
}