From 13d9119f3a36c898b5a42dd92f333c0ddba1a8dd Mon Sep 17 00:00:00 2001 From: "Nicholas J. Kain" Date: Wed, 30 Mar 2011 05:55:12 -0400 Subject: [PATCH] Remove dhcpd.h. --- ndhc/arp.c | 1 - ndhc/dhcpd.h | 58 ------------------------------------------------- ndhc/dhcpmsg.c | 23 +++++++++++--------- ndhc/dhcpmsg.h | 14 ++++++++++++ ndhc/ifchange.c | 1 - ndhc/ndhc.c | 1 - ndhc/options.c | 6 ++++- ndhc/options.h | 13 +++++++++++ ndhc/packet.c | 3 +-- ndhc/socket.c | 12 ++++------ 10 files changed, 50 insertions(+), 82 deletions(-) delete mode 100644 ndhc/dhcpd.h diff --git a/ndhc/arp.c b/ndhc/arp.c index 2feb67f..44a1c65 100644 --- a/ndhc/arp.c +++ b/ndhc/arp.c @@ -20,7 +20,6 @@ #include "socket.h" #include "sys.h" #include "ifchange.h" -#include "dhcpd.h" #include "log.h" #include "strl.h" #include "io.h" diff --git a/ndhc/dhcpd.h b/ndhc/dhcpd.h deleted file mode 100644 index 7a8431c..0000000 --- a/ndhc/dhcpd.h +++ /dev/null @@ -1,58 +0,0 @@ -/* dhcpd.h */ -#ifndef DHCPD_H_ -#define DHCPD_H_ - -#include -#include - -/*****************************************************************/ -/* Do not modify below here unless you know what you are doing!! */ -/*****************************************************************/ - -/* DHCP protocol -- see RFC 2131 */ -#define SERVER_PORT 67 -#define CLIENT_PORT 68 - -#define DHCP_OPTIONS_BUFSIZE 308 - -enum { - BOOTREQUEST = 1, - BOOTREPLY = 2 -}; - -#define ETH_10MB 1 -#define ETH_10MB_LEN 6 - -enum { - DHCPDISCOVER = 1, - DHCPOFFER = 2, - DHCPREQUEST = 3, - DHCPDECLINE = 4, - DHCPACK = 5, - DHCPNAK = 6, - DHCPRELEASE = 7, - DHCPINFORM = 8 -}; - -#define BROADCAST_FLAG 0x8000 - -enum { - OPTION_FIELD = 0, - FILE_FIELD = 1, - SNAME_FIELD = 2 -}; - -#define MAC_BCAST_ADDR (unsigned char *) "\xff\xff\xff\xff\xff\xff" - -enum { - OPT_CODE = 0, - OPT_LEN = 1, - OPT_DATA = 2 -}; - -struct option_set { - unsigned char *data; - struct option_set *next; -}; - -#endif diff --git a/ndhc/dhcpmsg.c b/ndhc/dhcpmsg.c index a171bf7..545e01b 100644 --- a/ndhc/dhcpmsg.c +++ b/ndhc/dhcpmsg.c @@ -37,7 +37,7 @@ #include #include -#include "dhcpd.h" +#include "dhcpmsg.h" #include "packet.h" #include "options.h" #include "config.h" @@ -74,9 +74,9 @@ uint32_t random_xid(void) static void init_header(struct dhcpMessage *packet, char type) { memset(packet, 0, DHCP_SIZE); - packet->op = BOOTREQUEST; /* client */ - packet->htype = ETH_10MB; - packet->hlen = ETH_10MB_LEN; + packet->op = 1; // BOOTREQUEST (client) + packet->htype = 1; // ETH_10MB + packet->hlen = 6; // ETH_10MB_LEN packet->cookie = htonl(DHCP_MAGIC); packet->options[0] = DHCP_END; add_simple_option(packet->options, DHCP_MESSAGE_TYPE, type); @@ -115,12 +115,14 @@ static void add_requests(struct dhcpMessage *packet) packet->options[end + OPT_DATA + len] = DHCP_END; } +#define MAC_BCAST_ADDR (unsigned char *) "\xff\xff\xff\xff\xff\xff" /* Wrapper that broadcasts a raw dhcp packet on the bound interface. */ static int bcast_raw_packet(struct dhcpMessage *packet) { - return raw_packet(packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST, - SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex); + return raw_packet(packet, INADDR_ANY, DHCP_CLIENT_PORT, INADDR_BROADCAST, + DHCP_SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex); } +#undef MAC_BCAST_ADDR /* Broadcast a DHCP discover packet to the network, with an optionally * requested IP */ @@ -170,8 +172,8 @@ int send_renew(uint32_t xid, uint32_t server, uint32_t ciaddr) add_requests(&packet); log_line("Sending renew..."); if (server) - return kernel_packet(&packet, ciaddr, CLIENT_PORT, - server, SERVER_PORT); + return kernel_packet(&packet, ciaddr, DHCP_CLIENT_PORT, server, + DHCP_SERVER_PORT); else return bcast_raw_packet(&packet); } @@ -213,7 +215,8 @@ int send_release(uint32_t server, uint32_t ciaddr) add_simple_option(packet.options, DHCP_SERVER_ID, server); log_line("Sending release..."); - return kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT); + return kernel_packet(&packet, ciaddr, DHCP_CLIENT_PORT, server, + DHCP_SERVER_PORT); } /* return -1 on errors that are fatal for the socket, @@ -252,7 +255,7 @@ int get_raw_packet(struct dhcpMessage *payload, int fd) sleep(1); return -2; } - if (packet.udp.dest != htons(CLIENT_PORT)) { + if (packet.udp.dest != htons(DHCP_CLIENT_PORT)) { log_line("UDP destination port incorrect: %d", ntohs(packet.udp.dest)); sleep(1); return -2; diff --git a/ndhc/dhcpmsg.h b/ndhc/dhcpmsg.h index d139665..f5ed50a 100644 --- a/ndhc/dhcpmsg.h +++ b/ndhc/dhcpmsg.h @@ -2,9 +2,23 @@ #define CLIENTPACKET_H_ #include +#include "packet.h" // for struct dhcpMessage +#define DHCP_SERVER_PORT 67 +#define DHCP_CLIENT_PORT 68 #define DHCP_MAGIC 0x63825363 +enum { + DHCPDISCOVER = 1, + DHCPOFFER = 2, + DHCPREQUEST = 3, + DHCPDECLINE = 4, + DHCPACK = 5, + DHCPNAK = 6, + DHCPRELEASE = 7, + DHCPINFORM = 8 +}; + uint32_t random_xid(void); int send_discover(uint32_t xid, uint32_t requested); int send_selecting(uint32_t xid, uint32_t server, uint32_t requested); diff --git a/ndhc/ifchange.c b/ndhc/ifchange.c index 16758a3..f6f90fd 100644 --- a/ndhc/ifchange.c +++ b/ndhc/ifchange.c @@ -33,7 +33,6 @@ #include #include "options.h" -#include "dhcpd.h" #include "config.h" #include "packet.h" #include "options.h" diff --git a/ndhc/ndhc.c b/ndhc/ndhc.c index 2d24f8b..9ef4c3f 100644 --- a/ndhc/ndhc.c +++ b/ndhc/ndhc.c @@ -41,7 +41,6 @@ #include #include "ndhc-defines.h" -#include "dhcpd.h" #include "config.h" #include "options.h" #include "dhcpmsg.h" diff --git a/ndhc/options.c b/ndhc/options.c index a72e5de..ea538ed 100644 --- a/ndhc/options.c +++ b/ndhc/options.c @@ -9,7 +9,6 @@ #include #include "log.h" -#include "dhcpd.h" #include "options.h" /* supported options are easily added here */ @@ -63,6 +62,11 @@ uint8_t* get_option(struct dhcpMessage *packet, int code) { uint8_t *optionptr; int len, rem, overload = 0; + enum { + OPTION_FIELD = 0, + FILE_FIELD = 1, + SNAME_FIELD = 2 + }; enum { FILE_FIELD101 = FILE_FIELD * 0x101, SNAME_FIELD101 = SNAME_FIELD * 0x101, diff --git a/ndhc/options.h b/ndhc/options.h index 4d9c6cf..92da936 100644 --- a/ndhc/options.h +++ b/ndhc/options.h @@ -6,6 +6,8 @@ #define TYPE_MASK 0x0F +#define DHCP_OPTIONS_BUFSIZE 308 + /* DHCP option codes (partial list) */ #define DHCP_PADDING 0x00 #define DHCP_SUBNET 0x01 @@ -66,12 +68,23 @@ enum { #define OPTION_REQ 0x10 /* have the client request this option */ #define OPTION_LIST 0x20 /* There can be a list of 1 or more of these */ +enum { + OPT_CODE = 0, + OPT_LEN = 1, + OPT_DATA = 2 +}; + struct dhcp_option { char name[10]; char flags; unsigned char code; }; +struct option_set { + unsigned char *data; + struct option_set *next; +}; + extern struct dhcp_option options[]; extern int option_lengths[]; diff --git a/ndhc/packet.c b/ndhc/packet.c index 19ad66f..05053bb 100644 --- a/ndhc/packet.c +++ b/ndhc/packet.c @@ -17,7 +17,6 @@ #include "sys.h" #include "log.h" #include "io.h" -#include "dhcpd.h" #include "options.h" /* Read a packet from socket fd, return -1 on read error, -2 on packet error */ @@ -187,7 +186,7 @@ void change_listen_mode(struct client_state_t *cs, int new_mode) cs->listenFd = -1; } if (new_mode == LM_KERNEL) { - cs->listenFd = listen_socket(INADDR_ANY, CLIENT_PORT, + cs->listenFd = listen_socket(INADDR_ANY, DHCP_CLIENT_PORT, client_config.interface); epoll_add(cs, cs->listenFd); } diff --git a/ndhc/socket.c b/ndhc/socket.c index 1c91b9f..4b58f98 100644 --- a/ndhc/socket.c +++ b/ndhc/socket.c @@ -36,7 +36,6 @@ #include #include "log.h" #include "strl.h" -#include "dhcpd.h" /* For SERVER_PORT and CLIENT_PORT */ int set_sock_nonblock(int fd) { @@ -142,13 +141,10 @@ int raw_socket(int ifindex) return -1; } - if (SERVER_PORT == 67 && CLIENT_PORT == 68) { - /* Use only if standard ports are in use */ - /* Ignoring error (kernel may lack support for this) */ - if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter_prog, - sizeof filter_prog) >= 0) - log_line("Attached filter to raw socket fd %d", fd); - } + /* Ignoring error (kernel may lack support for this) */ + if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter_prog, + sizeof filter_prog) >= 0) + log_line("Attached filter to raw socket fd %d", fd); set_sock_nonblock(fd);