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