2 * Copyright 2008, The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <netinet/in.h>
24 static void *init_dhcp_msg(dhcp_msg *msg, int type, void *hwaddr, uint32_t xid)
28 memset(msg, 0, sizeof(dhcp_msg));
30 msg->op = OP_BOOTREQUEST;
31 msg->htype = HTYPE_ETHER;
35 msg->flags = htons(FLAGS_BROADCAST);
39 memcpy(msg->chaddr, hwaddr, 6);
48 *x++ = OPT_MESSAGE_TYPE;
55 int init_dhcp_discover_msg(dhcp_msg *msg, void *hwaddr, uint32_t xid)
59 x = init_dhcp_msg(msg, DHCPDISCOVER, hwaddr, xid);
61 *x++ = OPT_PARAMETER_LIST;
63 *x++ = OPT_SUBNET_MASK;
66 *x++ = OPT_BROADCAST_ADDR;
70 return DHCP_MSG_FIXED_SIZE + (x - msg->options);
73 int init_dhcp_request_msg(dhcp_msg *msg, void *hwaddr, uint32_t xid,
74 uint32_t ipaddr, uint32_t serveraddr)
78 x = init_dhcp_msg(msg, DHCPREQUEST, hwaddr, xid);
80 *x++ = OPT_PARAMETER_LIST;
82 *x++ = OPT_SUBNET_MASK;
85 *x++ = OPT_BROADCAST_ADDR;
87 *x++ = OPT_REQUESTED_IP;
89 memcpy(x, &ipaddr, 4);
94 memcpy(x, &serveraddr, 4);
99 return DHCP_MSG_FIXED_SIZE + (x - msg->options);