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[ETH_ALEN] __attribute__((aligned(2))) = {
51 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
54 static const uint8_t MAC_ANY_ADDR[ETH_ALEN] __attribute__((aligned(2))) = {
55 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
59 #define DHCP_MAGIC 0x63825363
60 #define DHCP_OPTIONS_BUFSIZE 308
64 #define BROADCAST_FLAG 0x8000
83 uint8_t options[DHCP_OPTIONS_BUFSIZE + EXTEND_FOR_BUGGY_SERVERS];
84 } __attribute__((packed));
86 struct ip_udp_dhcp_packet {
89 struct dhcp_packet data;
90 } __attribute__((packed));
93 #define DHCP_PADDING 0x00
94 #define DHCP_SUBNET 0x01
95 #define DHCP_ROUTER 0x03
96 #define DHCP_TIME_SERVER 0x04
97 #define DHCP_NAME_SERVER 0x05
98 #define DHCP_DNS_SERVER 0x06
99 #define DHCP_HOST_NAME 0x0c
100 #define DHCP_DOMAIN_NAME 0x0f
101 #define DHCP_NTP_SERVER 0x2a
102 #define DHCP_REQUESTED_IP 0x32
103 #define DHCP_LEASE_TIME 0x33
104 #define DHCP_OPTION_OVERLOAD 0x34
105 #define DHCP_MESSAGE_TYPE 0x35
106 #define DHCP_SERVER_ID 0x36
107 #define DHCP_PARAM_REQ 0x37
108 #define DHCP_ERR_MESSAGE 0x38
109 #define DHCP_MAX_SIZE 0x39
110 #define DHCP_VENDOR 0x3c
111 #define DHCP_CLIENT_ID 0x3d
112 #define DHCP_END 0xff
117 #define OPTION_FIELD 0
119 #define SNAME_FIELD 2
121 /* DHCP_MESSAGE_TYPE values */
122 #define DHCPDISCOVER 1
124 #define DHCPREQUEST 3
125 #define DHCPDECLINE 4
128 #define DHCPRELEASE 7
130 #define DHCP_MINTYPE DHCPDISCOVER
131 #define DHCP_MAXTYPE DHCPINFORM
140 OPTION_TYPE_MASK = 0x0f,
144 typedef struct dhcp_option {
145 GDHCPOptionType type;
149 /* Length of the option types in binary form */
150 static const uint8_t dhcp_option_lengths[] = {
158 uint8_t *dhcp_get_option(struct dhcp_packet *packet, int code);
159 int dhcp_end_option(uint8_t *optionptr);
160 void dhcp_add_binary_option(struct dhcp_packet *packet, uint8_t *addopt);
161 void dhcp_add_simple_option(struct dhcp_packet *packet,
162 uint8_t code, uint32_t data);
163 GDHCPOptionType dhcp_get_code_type(uint8_t code);
165 uint16_t dhcp_checksum(void *addr, int count);
167 void dhcp_init_header(struct dhcp_packet *packet, char type);
169 int dhcp_send_raw_packet(struct dhcp_packet *dhcp_pkt,
170 uint32_t source_ip, int source_port,
171 uint32_t dest_ip, int dest_port,
172 const uint8_t *dest_arp, int ifindex);
173 int dhcp_send_kernel_packet(struct dhcp_packet *dhcp_pkt,
174 uint32_t source_ip, int source_port,
175 uint32_t dest_ip, int dest_port);
176 int dhcp_l3_socket(int port, const char *interface);
177 int dhcp_recv_l3_packet(struct dhcp_packet *packet, int fd);
178 char *get_interface_name(int index);
179 gboolean interface_is_up(int index);