• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2008, The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <stdio.h>
18 #include <stdarg.h>
19 #include <string.h>
20 #include <netinet/in.h>
21 
22 #include "dhcpmsg.h"
23 
init_dhcp_msg(dhcp_msg * msg,int type,void * hwaddr,uint32_t xid)24 static void *init_dhcp_msg(dhcp_msg *msg, int type, void *hwaddr, uint32_t xid)
25 {
26     uint8_t *x;
27 
28     memset(msg, 0, sizeof(dhcp_msg));
29 
30     msg->op = OP_BOOTREQUEST;
31     msg->htype = HTYPE_ETHER;
32     msg->hlen = 6;
33     msg->hops = 0;
34 
35     msg->flags = htons(FLAGS_BROADCAST);
36 
37     msg->xid = xid;
38 
39     memcpy(msg->chaddr, hwaddr, 6);
40 
41     x = msg->options;
42 
43     *x++ = OPT_COOKIE1;
44     *x++ = OPT_COOKIE2;
45     *x++ = OPT_COOKIE3;
46     *x++ = OPT_COOKIE4;
47 
48     *x++ = OPT_MESSAGE_TYPE;
49     *x++ = 1;
50     *x++ = type;
51 
52     return x;
53 }
54 
init_dhcp_discover_msg(dhcp_msg * msg,void * hwaddr,uint32_t xid)55 int init_dhcp_discover_msg(dhcp_msg *msg, void *hwaddr, uint32_t xid)
56 {
57     uint8_t *x;
58 
59     x = init_dhcp_msg(msg, DHCPDISCOVER, hwaddr, xid);
60 
61     *x++ = OPT_PARAMETER_LIST;
62     *x++ = 4;
63     *x++ = OPT_SUBNET_MASK;
64     *x++ = OPT_GATEWAY;
65     *x++ = OPT_DNS;
66     *x++ = OPT_BROADCAST_ADDR;
67 
68     *x++ = OPT_END;
69 
70     return DHCP_MSG_FIXED_SIZE + (x - msg->options);
71 }
72 
init_dhcp_request_msg(dhcp_msg * msg,void * hwaddr,uint32_t xid,uint32_t ipaddr,uint32_t serveraddr)73 int init_dhcp_request_msg(dhcp_msg *msg, void *hwaddr, uint32_t xid,
74                           uint32_t ipaddr, uint32_t serveraddr)
75 {
76     uint8_t *x;
77 
78     x = init_dhcp_msg(msg, DHCPREQUEST, hwaddr, xid);
79 
80     *x++ = OPT_PARAMETER_LIST;
81     *x++ = 4;
82     *x++ = OPT_SUBNET_MASK;
83     *x++ = OPT_GATEWAY;
84     *x++ = OPT_DNS;
85     *x++ = OPT_BROADCAST_ADDR;
86 
87     *x++ = OPT_REQUESTED_IP;
88     *x++ = 4;
89     memcpy(x, &ipaddr, 4);
90     x +=  4;
91 
92     *x++ = OPT_SERVER_ID;
93     *x++ = 4;
94     memcpy(x, &serveraddr, 4);
95     x += 4;
96 
97     *x++ = OPT_END;
98 
99     return DHCP_MSG_FIXED_SIZE + (x - msg->options);
100 }
101