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