Convert DHCP option code numbers to an enum.

Convert many remaining /**/ comments to // where it makes for less visual
clutter.
This commit is contained in:
Nicholas J. Kain 2011-07-02 06:31:57 -04:00
parent 9224374d98
commit 801ec356f4
6 changed files with 68 additions and 69 deletions

View File

@ -31,22 +31,22 @@
#include "dhcp.h" #include "dhcp.h"
struct arpMsg { struct arpMsg {
/* Ethernet header */ // Ethernet header
uint8_t h_dest[6]; /* 00 destination ether addr */ uint8_t h_dest[6]; // 00 destination ether addr
uint8_t h_source[6]; /* 06 source ether addr */ uint8_t h_source[6]; // 06 source ether addr
uint16_t h_proto; /* 0c packet type ID field */ uint16_t h_proto; // 0c packet type ID field
/* ARP packet */ // ARP packet
uint16_t htype; /* 0e hardware type (must be ARPHRD_ETHER) */ uint16_t htype; // 0e hardware type (must be ARPHRD_ETHER)
uint16_t ptype; /* 10 protocol type (must be ETH_P_IP) */ uint16_t ptype; // 10 protocol type (must be ETH_P_IP)
uint8_t hlen; /* 12 hardware address length (must be 6) */ uint8_t hlen; // 12 hardware address length (must be 6)
uint8_t plen; /* 13 protocol address length (must be 4) */ uint8_t plen; // 13 protocol address length (must be 4)
uint16_t operation; /* 14 ARP opcode */ uint16_t operation; // 14 ARP opcode
uint8_t smac[6]; /* 16 sender's hardware address */ uint8_t smac[6]; // 16 sender's hardware address
uint8_t sip4[4]; /* 1c sender's IP address */ uint8_t sip4[4]; // 1c sender's IP address
uint8_t dmac[6]; /* 20 target's hardware address */ uint8_t dmac[6]; // 20 target's hardware address
uint8_t dip4[4]; /* 26 target's IP address */ uint8_t dip4[4]; // 26 target's IP address
uint8_t pad[18]; /* 2a pad for min. ethernet payload (60 bytes) */ uint8_t pad[18]; // 2a pad for min. ethernet payload (60 bytes)
}; };
int arp_close_fd(struct client_state_t *cs); int arp_close_fd(struct client_state_t *cs);

View File

@ -43,8 +43,8 @@
extern struct client_state_t cs; extern struct client_state_t cs;
static char router_set; static char router_set;
/* Fill buf with the ifchd command text of option 'option'. */ // Fill buf with the ifchd command text of option 'option'.
/* Returns 0 if successful, -1 if nothing was filled in. */ // Returns 0 if successful, -1 if nothing was filled in.
static int ifchd_cmd(char *buf, size_t buflen, uint8_t *option, ssize_t optlen, static int ifchd_cmd(char *buf, size_t buflen, uint8_t *option, ssize_t optlen,
uint8_t code) uint8_t code)
{ {

View File

@ -69,7 +69,6 @@ struct client_state_t cs = {
}; };
struct client_config_t client_config = { struct client_config_t client_config = {
/* Default options. */
.interface = "eth0", .interface = "eth0",
.arp = "\0\0\0\0\0\0", .arp = "\0\0\0\0\0\0",
}; };
@ -200,7 +199,6 @@ int main(int argc, char **argv)
{0, 0, 0, 0} {0, 0, 0, 0}
}; };
/* get options */
while (1) { while (1) {
int option_index = 0; int option_index = 0;
c = getopt_long(argc, argv, "c:fbp:H:h:i:np:l:qr:u:C:vV:", arg_options, c = getopt_long(argc, argv, "c:fbp:H:h:i:np:l:qr:u:C:vV:", arg_options,

View File

@ -84,12 +84,12 @@ static void sleep_if(struct client_state_t *cs)
cs->timeout = -1; cs->timeout = -1;
} }
static int data_attr_cb(const struct nlattr *attr, void *data) static int nl_process_msgs_attr(const struct nlattr *attr, void *data)
{ {
const struct nlattr **tb = data; const struct nlattr **tb = data;
int type = mnl_attr_get_type(attr); int type = mnl_attr_get_type(attr);
/* skip unsupported attribute in user-space */ // skip unsupported attribute in user-space
if (mnl_attr_type_valid(attr, IFLA_MAX) < 0) if (mnl_attr_type_valid(attr, IFLA_MAX) < 0)
return MNL_CB_OK; return MNL_CB_OK;
switch (type) { switch (type) {
@ -114,7 +114,7 @@ static void get_if_index_and_mac(const struct nlmsghdr *nlh,
struct ifinfomsg *ifm, struct ifinfomsg *ifm,
const struct nlattr **tb) const struct nlattr **tb)
{ {
mnl_attr_parse(nlh, sizeof(*ifm), data_attr_cb, tb); mnl_attr_parse(nlh, sizeof *ifm, nl_process_msgs_attr, tb);
if (!tb[IFLA_IFNAME]) if (!tb[IFLA_IFNAME])
return; return;
if (!strcmp(client_config.interface, mnl_attr_get_str(tb[IFLA_IFNAME]))) { if (!strcmp(client_config.interface, mnl_attr_get_str(tb[IFLA_IFNAME]))) {
@ -135,7 +135,7 @@ static void get_if_index_and_mac(const struct nlmsghdr *nlh,
} }
} }
static int data_cb(const struct nlmsghdr *nlh, void *data) static int nl_process_msgs(const struct nlmsghdr *nlh, void *data)
{ {
struct nlattr *tb[IFLA_MAX+1] = {0}; struct nlattr *tb[IFLA_MAX+1] = {0};
struct ifinfomsg *ifm = mnl_nlmsg_get_payload(nlh); struct ifinfomsg *ifm = mnl_nlmsg_get_payload(nlh);
@ -204,7 +204,7 @@ void handle_nl_message(struct client_state_t *cs)
assert(cs->nlFd != -1); assert(cs->nlFd != -1);
do { do {
ret = mnl_socket_recvfrom(mls, buf, sizeof buf); ret = mnl_socket_recvfrom(mls, buf, sizeof buf);
ret = mnl_cb_run(buf, ret, 0, 0, data_cb, cs); ret = mnl_cb_run(buf, ret, 0, 0, nl_process_msgs, cs);
} while (ret > 0); } while (ret > 0);
if (ret == -1) if (ret == -1)
log_line("nl callback function returned error: %s", strerror(errno)); log_line("nl callback function returned error: %s", strerror(errno));

View File

@ -22,50 +22,51 @@
#include "dhcp.h" #include "dhcp.h"
/* DHCP option codes (partial list) */ enum dhcp_codes {
#define DHCP_PADDING 0x00 DHCP_PADDING = 0x00,
#define DHCP_SUBNET 0x01 DHCP_SUBNET = 0x01,
#define DHCP_TIME_OFFSET 0x02 DHCP_TIME_OFFSET = 0x02,
#define DHCP_ROUTER 0x03 DHCP_ROUTER = 0x03,
#define DHCP_TIME_SERVER 0x04 DHCP_TIME_SERVER = 0x04,
#define DHCP_NAME_SERVER 0x05 DHCP_NAME_SERVER = 0x05,
#define DHCP_DNS_SERVER 0x06 DHCP_DNS_SERVER = 0x06,
#define DHCP_LOG_SERVER 0x07 DHCP_LOG_SERVER = 0x07,
#define DHCP_COOKIE_SERVER 0x08 DHCP_COOKIE_SERVER = 0x08,
#define DHCP_LPR_SERVER 0x09 DHCP_LPR_SERVER = 0x09,
#define DHCP_HOST_NAME 0x0c DHCP_HOST_NAME = 0x0c,
#define DHCP_BOOT_SIZE 0x0d DHCP_BOOT_SIZE = 0x0d,
#define DHCP_DOMAIN_NAME 0x0f DHCP_DOMAIN_NAME = 0x0f,
#define DHCP_SWAP_SERVER 0x10 DHCP_SWAP_SERVER = 0x10,
#define DHCP_ROOT_PATH 0x11 DHCP_ROOT_PATH = 0x11,
#define DHCP_IP_TTL 0x17 DHCP_IP_TTL = 0x17,
#define DHCP_MTU 0x1a DHCP_MTU = 0x1a,
#define DHCP_BROADCAST 0x1c DHCP_BROADCAST = 0x1c,
#define DHCP_NIS_DOMAIN 0x28 DHCP_NIS_DOMAIN = 0x28,
#define DHCP_NIS_SERVER 0x29 DHCP_NIS_SERVER = 0x29,
#define DHCP_NTP_SERVER 0x2a DHCP_NTP_SERVER = 0x2a,
#define DHCP_WINS_SERVER 0x2c DHCP_WINS_SERVER = 0x2c,
#define DHCP_REQUESTED_IP 0x32 DHCP_REQUESTED_IP = 0x32,
#define DHCP_LEASE_TIME 0x33 DHCP_LEASE_TIME = 0x33,
#define DHCP_OPTION_OVERLOAD 0x34 DHCP_OPTION_OVERLOAD = 0x34,
#define DHCP_MESSAGE_TYPE 0x35 DHCP_MESSAGE_TYPE = 0x35,
#define DHCP_SERVER_ID 0x36 DHCP_SERVER_ID = 0x36,
#define DHCP_PARAM_REQ 0x37 DHCP_PARAM_REQ = 0x37,
#define DHCP_MESSAGE 0x38 DHCP_MESSAGE = 0x38,
#define DHCP_MAX_SIZE 0x39 DHCP_MAX_SIZE = 0x39,
#define DHCP_T1 0x3a DHCP_T1 = 0x3a,
#define DHCP_T2 0x3b DHCP_T2 = 0x3b,
#define DHCP_VENDOR 0x3c DHCP_VENDOR = 0x3c,
#define DHCP_CLIENT_ID 0x3d DHCP_CLIENT_ID = 0x3d,
#define DHCP_TFTP_SERVER_NAME 0x42 DHCP_TFTP_SERVER_NAME = 0x42,
#define DHCP_BOOT_FILE 0x43 DHCP_BOOT_FILE = 0x43,
#define DHCP_USER_CLASS 0x4d DHCP_USER_CLASS = 0x4d,
#define DHCP_FQDN 0x51 DHCP_FQDN = 0x51,
#define DHCP_DOMAIN_SEARCH 0x77 DHCP_DOMAIN_SEARCH = 0x77,
#define DHCP_SIP_SERVERS 0x78 DHCP_SIP_SERVERS = 0x78,
#define DHCP_STATIC_ROUTES 0x79 DHCP_STATIC_ROUTES = 0x79,
#define DHCP_WPAD 0xfc DHCP_WPAD = 0xfc,
#define DHCP_END 0xff DHCP_END = 0xff,
};
enum option_type { enum option_type {
OPTION_NONE = 0, OPTION_NONE = 0,

View File

@ -57,7 +57,7 @@ void background(struct client_state_t *cs)
{ {
static char called; static char called;
if (!called) { if (!called) {
called = 1; /* Do not fork again. */ called = 1; // Do not fork again.
if (daemon(0, 0) == -1) { if (daemon(0, 0) == -1) {
perror("fork"); perror("fork");
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);