3 * DHCP client library with GLib integration
5 * Copyright (C) 2009-2010 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include <netinet/udp.h>
23 #include <netinet/ip.h>
29 #define dhcp_get_unaligned(ptr) \
31 struct __attribute__((packed)) { \
33 } *__p = (void *) (ptr); \
37 #define dhcp_put_unaligned(val, ptr) \
39 struct __attribute__((packed)) { \
41 } *__p = (void *) (ptr); \
45 #define CLIENT_PORT 68
46 #define SERVER_PORT 67
48 #define EXTEND_FOR_BUGGY_SERVERS 80
50 static const uint8_t MAC_BCAST_ADDR[6] __attribute__((aligned(2))) = {
51 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
55 #define DHCP_MAGIC 0x63825363
56 #define DHCP_OPTIONS_BUFSIZE 308
60 #define BROADCAST_FLAG 0x8000
79 uint8_t options[DHCP_OPTIONS_BUFSIZE + EXTEND_FOR_BUGGY_SERVERS];
80 } __attribute__((packed));
82 struct ip_udp_dhcp_packet {
85 struct dhcp_packet data;
86 } __attribute__((packed));
89 #define DHCP_PADDING 0x00
90 #define DHCP_SUBNET 0x01
91 #define DHCP_ROUTER 0x03
92 #define DHCP_TIME_SERVER 0x04
93 #define DHCP_NAME_SERVER 0x05
94 #define DHCP_DNS_SERVER 0x06
95 #define DHCP_HOST_NAME 0x0c
96 #define DHCP_DOMAIN_NAME 0x0f
97 #define DHCP_NTP_SERVER 0x2a
98 #define DHCP_REQUESTED_IP 0x32
99 #define DHCP_LEASE_TIME 0x33
100 #define DHCP_OPTION_OVERLOAD 0x34
101 #define DHCP_MESSAGE_TYPE 0x35
102 #define DHCP_SERVER_ID 0x36
103 #define DHCP_PARAM_REQ 0x37
104 #define DHCP_ERR_MESSAGE 0x38
105 #define DHCP_MAX_SIZE 0x39
106 #define DHCP_VENDOR 0x3c
107 #define DHCP_CLIENT_ID 0x3d
108 #define DHCP_END 0xff
113 #define OPTION_FIELD 0
115 #define SNAME_FIELD 2
117 /* DHCP_MESSAGE_TYPE values */
118 #define DHCPDISCOVER 1
120 #define DHCPREQUEST 3
121 #define DHCPDECLINE 4
124 #define DHCPRELEASE 7
126 #define DHCP_MINTYPE DHCPDISCOVER
127 #define DHCP_MAXTYPE DHCPINFORM
136 OPTION_TYPE_MASK = 0x0f,
140 typedef struct dhcp_option {
141 GDHCPOptionType type;
145 /* Length of the option types in binary form */
146 static const uint8_t dhcp_option_lengths[] = {
154 uint8_t *dhcp_get_option(struct dhcp_packet *packet, int code);
155 int dhcp_end_option(uint8_t *optionptr);
156 void dhcp_add_binary_option(struct dhcp_packet *packet, uint8_t *addopt);
157 void dhcp_add_simple_option(struct dhcp_packet *packet,
158 uint8_t code, uint32_t data);
159 GDHCPOptionType dhcp_get_code_type(uint8_t code);
161 uint16_t dhcp_checksum(void *addr, int count);
163 void dhcp_init_header(struct dhcp_packet *packet, char type);
165 int dhcp_send_raw_packet(struct dhcp_packet *dhcp_pkt,
166 uint32_t source_ip, int source_port,
167 uint32_t dest_ip, int dest_port,
168 const uint8_t *dest_arp, int ifindex) ;
169 int dhcp_send_kernel_packet(struct dhcp_packet *dhcp_pkt,
170 uint32_t source_ip, int source_port,
171 uint32_t dest_ip, int dest_port) ;
172 int dhcp_l3_socket(int port, const char *interface);
173 int dhcp_recv_l3_packet(struct dhcp_packet *packet, int fd);