Browse Source

[1324]
Avoid use of externals.
Completed option processing modules, and gtests for options.
Work around GNU getopt() bug.
Bring all modules into conformity with BIND10 coding guidelines.

John DuBois 13 years ago
parent
commit
06d6be6930

+ 42 - 42
tests/tools/perfdhcp/cloptions.cc

@@ -19,7 +19,7 @@
 #include "perfdhcp.h"
 #include "cloptions.h"
 
-static void printHelp(const char *progName, const char *usage);
+static void printHelp(const char* progName, const char* usage);
 
 /*
  * Return value:
@@ -28,43 +28,43 @@ static void printHelp(const char *progName, const char *usage);
  * 1 if argument processing was successful and the program should continue.
  */
 int
-procArgs(int argc, const char *argv[], confdata_t *confdata,
-	const char **server)
-{
+procArgs(int argc, const char* argv[], confdata_t* confdata,
+         const char** server) {
     char usage[] =
-"Usage:\n\
+        "Usage:\n\
 perfdhcp [-hv] [-4|-6] [-r<rate>] [-n<num-request>] [-p<test-period>]\n\
          [-d<drop-time>] [-D<max-drop>] [-l<local-addr|interface>] [-i]\n\
          [-x<diagnostic-selector>] [server]\n";
     int v4 = 0;
     int v6 = 0;
-    char *maxDropOpt;
-    const char *localName;
+    const char* localName = NULL;
+    const char* msg;
+
+    char* maxDropOpt;
     double dropTime;
     double testPeriod;
-    const char *msg;
 
     /* Names of configuration variables, for defaults file processor */
     confvar_t optConf[] = {
-	{ 'h', NULL,		CF_SWITCH,	NULL,		0 },
-	{ 'v', NULL,		CF_SWITCH,	NULL,		0 },
-	{ '4', NULL,		CF_SWITCH,	&v4,		1 },
-	{ '6', NULL,		CF_SWITCH,	&v6,		1 },
-	{ 'i', NULL,		CF_SWITCH,	NULL,		1 },
-	{ 'l', NULL,		CF_NE_STRING,	&localName,	0 },
-	{ 'r', NULL,		CF_POS_INT,	NULL,		0 },
-	{ 'x', NULL,		CF_STRING,	NULL,		0 },
-	{ 'd', NULL,		CF_POS_FLOAT,	&dropTime,	0 },
-	{ 'D', NULL,		CF_STRING,	&maxDropOpt,	0 },
-	{ 'n', NULL,		CF_POS_INT,	NULL,		0 },
-	{ 'p', NULL,		CF_POS_FLOAT,	&testPeriod,	0 },
-        { '\0', NULL,           CF_ENDLIST,	NULL,		0 }
+        { 'h', NULL,        CF_SWITCH,  NULL,       0 },
+        { 'v', NULL,        CF_SWITCH,  NULL,       0 },
+        { '4', NULL,        CF_SWITCH,  &v4,        1 },
+        { '6', NULL,        CF_SWITCH,  &v6,        1 },
+        { 'i', NULL,        CF_SWITCH,  NULL,       1 },
+        { 'l', NULL,        CF_NE_STRING,   &localName, 0 },
+        { 'r', NULL,        CF_POS_INT, NULL,       0 },
+        { 'x', NULL,        CF_STRING,  NULL,       0 },
+        { 'd', NULL,        CF_POS_FLOAT,   &dropTime,  0 },
+        { 'D', NULL,        CF_NE_STRING,   &maxDropOpt,    0 },
+        { 'n', NULL,        CF_POS_INT, NULL,       0 },
+        { 'p', NULL,        CF_POS_FLOAT,   &testPeriod,    0 },
+        { '\0', NULL,           CF_ENDLIST, NULL,       0 }
     };
 
     /* Process command line options and config file */
     msg = procOpts(&argc, &argv, optConf, confdata, NULL, progName, NULL);
     if (msg != NULL) {
-        fprintf(stderr, "%s\n", msg);
+        fprintf(stderr, "%s: %s\n", progName, msg);
         return 2;
     }
 
@@ -78,37 +78,37 @@ perfdhcp [-hv] [-4|-6] [-r<rate>] [-n<num-request>] [-p<test-period>]\n\
     }
 
     if (v4 && v6) {
-        fprintf(stderr, "Must not give -4 and -6 together.\n");
+        fprintf(stderr, "%s: Must not give -4 and -6 together.\n", progName);
         return 2;
     }
     switch (argc) {
     case 0:
-	if (v6 && localName != NULL)
-	    *server = "all";
-	else {
-	    if (v6)
-		fprintf(stderr, "Use -l to specify an interface name.\n\%s\n",
-			usage);
-	    else
-		fprintf(stderr, "Must specify a DHCP server.\n\%s\n", usage);
-	    return 2;
-	}
-	break;
+        if (v6 && localName != NULL) {
+            *server = "all";
+        } else {
+            if (v6)
+                fprintf(stderr, "%s: Use -l to specify an interface name.\n\%s\n",
+                        progName, usage);
+            else {
+                fprintf(stderr, "%s: Must specify a DHCP server.\n\%s\n", progName, usage);
+            }
+            return 2;
+        }
+        break;
     case 1:
-	*server = argv[0];
-	break;
+        *server = argv[0];
+        break;
     default:
-	fprintf(stderr, "Too many arguments.\n\%s\n", usage);
-	return 2;
+        fprintf(stderr, "%s: Too many arguments.\n\%s\n", progName, usage);
+        return 2;
     }
     return 1;
 }
 
 static void
-printHelp(const char *progName, const char *usage)
-{
+printHelp(const char* progName, const char* usage) {
     printf(
-"%s: Execute a performance test against a DHCP server.\n\
+        "%s: Execute a performance test against a DHCP server.\n\
 %s\n\
 The [server] argument is the name/address of the DHCP server to contact. \n\
 For DHCPv4 operation, exchanges are initiated by transmitting a DHCP\n\
@@ -180,5 +180,5 @@ The exit status is:\n\
 2 if an error is found in the command line arguments.\n\
 3 if there are no general failures in operation, but one or more exchanges\n\
   are not successfully completed.\n",
-		    progName, usage);
+        progName, usage);
 }

+ 2 - 2
tests/tools/perfdhcp/cloptions.h

@@ -1,5 +1,5 @@
 #include <stdint.h>
 #include "procconf.h"
 
-int procArgs(int argc, const char *argv[], confdata_t *confdata,
-	const char **server);
+int procArgs(int argc, const char* argv[], confdata_t* confdata,
+             const char** server);

+ 16 - 18
tests/tools/perfdhcp/dkdebug.cc

@@ -21,26 +21,25 @@
 unsigned dk_diag_mask;
 
 int
-dk_setup(const char *diag_str, const struct dkdesc *diags)
-{
+dk_setup(const char* diag_str, const struct dkdesc* diags) {
     dk_diag_mask = 0;
     int i;
 
     for (; *diag_str != '\0'; diag_str++)
-	for (i = 0; diags[i].keyletter != '\0'; i++) {
-	    if (diags[i].keyletter == *diag_str) {
-		dk_diag_mask |= diags[i].mask;
-		break;
-	    }
-	    if (diags[i].keyletter == '\0')
-		return 0;
-	}
+        for (i = 0; diags[i].keyletter != '\0'; i++) {
+            if (diags[i].keyletter == *diag_str) {
+                dk_diag_mask |= diags[i].mask;
+                break;
+            }
+            if (diags[i].keyletter == '\0') {
+                return 0;
+            }
+        }
     return 1;
 }
 
 void
-dkprintf(unsigned diag_req, const char format[], ...)
-{
+dkprintf(unsigned diag_req, const char format[], ...) {
     va_list ap;
 
     va_start(ap,format);
@@ -49,14 +48,13 @@ dkprintf(unsigned diag_req, const char format[], ...)
 }
 
 void
-vdkprintf(unsigned diag_req, const char format[], va_list ap)
-{
-    if (diag_req & dk_diag_mask)
-	vfprintf(stderr, format, ap);
+vdkprintf(unsigned diag_req, const char format[], va_list ap) {
+    if (diag_req & dk_diag_mask) {
+        vfprintf(stderr, format, ap);
+    }
 }
 
 int
-dk_set(unsigned diag_req)
-{
+dk_set(unsigned diag_req) {
     return diag_req & dk_diag_mask;
 }

+ 2 - 2
tests/tools/perfdhcp/dkdebug.h

@@ -1,6 +1,6 @@
 #ifdef __cplusplus
 extern "C" {
-#endif 
+#endif
 
 #include <stdarg.h>
 
@@ -13,7 +13,7 @@ struct dkdesc {
 
 void dkprintf(unsigned diag_req, const char format[], ...);
 void vdkprintf(unsigned diag_req, const char format[], va_list ap);
-int dk_setup(const char *diag_str, const struct dkdesc *diags);
+int dk_setup(const char* diag_str, const struct dkdesc* diags);
 int dk_set(unsigned diag_req);
 
 #ifdef __cplusplus

+ 8 - 1
tests/tools/perfdhcp/packetdisp.c

@@ -27,7 +27,14 @@ static void printOption(int v6, unsigned optnum, size_t len, const unsigned char
 static void print_dhcpv4_packet(const struct dhcp_packet *pkt, size_t len);
 static void print_dhcpv6_packet(const struct dhcpv6_packet *pkt, size_t len);
 
-typedef enum { T_UNK, T_STRING, T_PERIOD, T_IPADDR, T_MTYPE, T_STATUS } opt_type;
+typedef enum {
+    T_UNK,
+    T_STRING,
+    T_PERIOD,
+    T_IPADDR,
+    T_MTYPE,
+    T_STATUS
+} opt_type;
 
 struct dhcp_option_desc {
     char *name;

+ 245 - 250
tests/tools/perfdhcp/perfdhcp.cc

@@ -36,27 +36,27 @@ struct duid {
     unsigned char hwaddr[6];
 };
 
-struct addrinfo *getaddr(int addr_fam, const char *hostname, const char *port);
-char *addrName(const struct sockaddr_storage *addr, char *buf, size_t bufsize);
+struct addrinfo* getaddr(int addr_fam, const char* hostname, const char* port);
+char* addrName(const struct sockaddr_storage* addr, char* buf, size_t bufsize);
 void add_option(int v6, unsigned optnum, unsigned count,
-	size_t size, int direct, unsigned char options[], size_t *buffer_used,
-	...);
-int get_linklocal_addr(const char if_name[], struct sockaddr_storage *addr);
-const char *optionName(int v6, unsigned optnum);
-const unsigned char *find_option(const struct dhcp_packet *pkt, int search_opt);
-int socket_setup(int addr_fam, const char *localAddr, const char *port,
-	const char *type, struct sockaddr_storage *l_addr);
-void gen_discover(struct dhcp_packet *discover_pkt, const struct in_addr *giaddr);
-void gen_request(struct dhcp_packet *dhcp_pkt, const struct in_addr *giaddr,
-	const struct in_addr *yiaddr, const unsigned char *server_id);
-void dhcp_recv(int v6, void *msg, int recv_fd, const struct sockaddr_storage *recv_laddr);
-void dora(const char *server, const char *localAddr);
-void sarr(const char *server, const char *if_name);
-void print_addrinfo(FILE *f, const struct addrinfo *addr);
-void print_sa6_info(FILE *f, const struct sockaddr_in6 *sa);
-void gen_solicit(struct dhcpv6_packet *dhcp_pkt, const struct duid *client_id);
-void dhcp_send(int v6, const unsigned char *msg, int send_fd, const struct
-	sockaddr *r_addr, const struct sockaddr_storage *send_laddr);
+                size_t size, int direct, unsigned char options[], size_t* buffer_used,
+                ...);
+int get_linklocal_addr(const char if_name[], struct sockaddr_storage* addr);
+const char* optionName(int v6, unsigned optnum);
+const unsigned char* find_option(const struct dhcp_packet* pkt, int search_opt);
+int socket_setup(int addr_fam, const char* localAddr, const char* port,
+                 const char* type, struct sockaddr_storage* l_addr);
+void gen_discover(struct dhcp_packet* discover_pkt, const struct in_addr* giaddr);
+void gen_request(struct dhcp_packet* dhcp_pkt, const struct in_addr* giaddr,
+                 const struct in_addr* yiaddr, const unsigned char* server_id);
+void dhcp_recv(int v6, void* msg, int recv_fd, const struct sockaddr_storage* recv_laddr);
+void dora(const char* server, const char* localAddr);
+void sarr(const char* server, const char* if_name);
+void print_addrinfo(FILE* f, const struct addrinfo* addr);
+void print_sa6_info(FILE* f, const struct sockaddr_in6* sa);
+void gen_solicit(struct dhcpv6_packet* dhcp_pkt, const struct duid* client_id);
+void dhcp_send(int v6, const unsigned char* msg, int send_fd, const struct
+               sockaddr* r_addr, const struct sockaddr_storage* send_laddr);
 
 static const struct dkdesc diagLetters[] = {
     { 's', DK_SOCK },
@@ -67,32 +67,35 @@ static const struct dkdesc diagLetters[] = {
 };
 
 int
-main(int argc, const char *argv[])
-{
+main(int argc, const char* argv[]) {
     int ret;
     int v6;
     // int initialOnly;
-    const char *localName = NULL;
+    const char* localName = NULL;
     // unsigned rate;
     // unsigned numRequest;
-    const char *server;
-    const char *diagSelector = "";
+    const char* server;
+    const char* diagSelector = "";
     confdata_t confdata;
 
-    if ((ret = procArgs(argc, argv, &confdata, &server)) != 1)
-	exit(ret);
+    if ((ret = procArgs(argc, argv, &confdata, &server)) != 1) {
+        exit(ret);
+    }
 
     v6 = confdata.map['6']->num > 0;
-    if (confdata.map['l']->num > 0)
+    if (confdata.map['l']->num > 0) {
         localName = confdata.map['l']->values[0]->value.string;
-    if (confdata.map['x']->num > 0)
+    }
+    if (confdata.map['x']->num > 0) {
         diagSelector = confdata.map['x']->values[0]->value.string;
+    }
 
     srand(time(NULL));
-    if (v6)
-	sarr(server, localName);
-    else
-	dora(server, localName);
+    if (v6) {
+        sarr(server, localName);
+    } else {
+        dora(server, localName);
+    }
     dk_setup(diagSelector, diagLetters);
 
     exit(0);
@@ -116,34 +119,33 @@ main(int argc, const char *argv[])
  * Return value: The network fd.
  */
 int
-socket_setup(int addr_fam, const char *localAddr, const char *port,
-	const char *type, struct sockaddr_storage *l_addr)
-{
+socket_setup(int addr_fam, const char* localAddr, const char* port,
+             const char* type, struct sockaddr_storage* l_addr) {
     char addrbuf[ADDR_NAME_BUFSIZE];
     int net_fd;
-    struct addrinfo *addrs;
+    struct addrinfo* addrs;
 
     if ((addrs = getaddr(addr_fam, localAddr, port)) == NULL) {
-	fprintf(stderr, "No addresses for %s\n", localAddr);
-	exit(1);
+        fprintf(stderr, "No addresses for %s\n", localAddr);
+        exit(1);
     }
     if (localAddr == NULL) {
-	if (dk_set(DK_SOCK)) {
-	    fprintf(stderr, "local address:\n");
-	    print_sa6_info(stderr, (struct sockaddr_in6 *)l_addr);
-	}
-	memcpy(&((struct sockaddr_in6 *)addrs->ai_addr)->sin6_addr,
-		&((struct sockaddr_in6 *)l_addr)->sin6_addr,
-		sizeof(struct in6_addr));
-	((struct sockaddr_in6 *)addrs->ai_addr)->sin6_flowinfo =
-		((struct sockaddr_in6 *)l_addr)->sin6_flowinfo;
-	((struct sockaddr_in6 *)addrs->ai_addr)->sin6_scope_id =
-		((struct sockaddr_in6 *)l_addr)->sin6_scope_id;
+        if (dk_set(DK_SOCK)) {
+            fprintf(stderr, "local address:\n");
+            print_sa6_info(stderr, (struct sockaddr_in6*)l_addr);
+        }
+        memcpy(&((struct sockaddr_in6*)addrs->ai_addr)->sin6_addr,
+               &((struct sockaddr_in6*)l_addr)->sin6_addr,
+               sizeof(struct in6_addr));
+        ((struct sockaddr_in6*)addrs->ai_addr)->sin6_flowinfo =
+            ((struct sockaddr_in6*)l_addr)->sin6_flowinfo;
+        ((struct sockaddr_in6*)addrs->ai_addr)->sin6_scope_id =
+            ((struct sockaddr_in6*)l_addr)->sin6_scope_id;
     }
     if (dk_set(DK_SOCK)) {
-	print_addrinfo(stderr, addrs);
-	fprintf(stderr, "Creating socket from addrinfo:\n");
-	print_addrinfo(stderr, addrs);
+        print_addrinfo(stderr, addrs);
+        fprintf(stderr, "Creating socket from addrinfo:\n");
+        print_addrinfo(stderr, addrs);
     }
     net_fd = socket(addrs->ai_family, addrs->ai_socktype, addrs->ai_protocol);
     if (net_fd < 0) {
@@ -151,13 +153,13 @@ socket_setup(int addr_fam, const char *localAddr, const char *port,
         exit(1);
     }
     if (bind(net_fd, addrs->ai_addr, addrs->ai_addrlen) == -1) {
-	int s_errno = errno;
-	fprintf(stderr, "Could not bind to %s: %s\n",
-		addrName((struct sockaddr_storage *)addrs->ai_addr, addrbuf,
-			sizeof(addrbuf)), strerror(s_errno));
-	exit(1);
+        int s_errno = errno;
+        fprintf(stderr, "Could not bind to %s: %s\n",
+                addrName((struct sockaddr_storage*)addrs->ai_addr, addrbuf,
+                         sizeof(addrbuf)), strerror(s_errno));
+        exit(1);
     }
-    dkprintf(DK_SOCK, "%s fd %d bound to %s\n", type, net_fd, addrName((struct sockaddr_storage *)addrs->ai_addr, addrbuf, sizeof(addrbuf)));
+    dkprintf(DK_SOCK, "%s fd %d bound to %s\n", type, net_fd, addrName((struct sockaddr_storage*)addrs->ai_addr, addrbuf, sizeof(addrbuf)));
     memcpy(l_addr, addrs->ai_addr, sizeof(struct sockaddr_storage));
     freeaddrinfo(addrs);
     return net_fd;
@@ -173,38 +175,38 @@ socket_setup(int addr_fam, const char *localAddr, const char *port,
  * discover_packet is a pointer to storage for the packet to be generated.
  */
 void
-gen_discover(struct dhcp_packet *discover_pkt, const struct in_addr *giaddr)
-{
+gen_discover(struct dhcp_packet* discover_pkt, const struct in_addr* giaddr) {
     size_t options_len;
 
-    bzero((char *) discover_pkt, sizeof(struct dhcp_packet));
+    bzero((char*) discover_pkt, sizeof(struct dhcp_packet));
     discover_pkt->op = BOOTREQUEST;
     discover_pkt->htype = HTYPE_ETHER;
     discover_pkt->hlen = 6;
     discover_pkt->hops = 1;
-    discover_pkt->xid = 0x12345678;	/* transaction id - fix */
+    discover_pkt->xid = 0x12345678; /* transaction id - fix */
     discover_pkt->secs = 0;
     discover_pkt->flags = 0;
     memcpy(&discover_pkt->giaddr, giaddr, sizeof((*discover_pkt).giaddr));
-    strncpy((char *)discover_pkt->chaddr, "\x12\x34\x56\x78\x9a\xbc", 6);	/* client hardware addr - fix */
+    strncpy((char*)discover_pkt->chaddr, "\x12\x34\x56\x78\x9a\xbc", 6);    /* client hardware addr - fix */
     memset(discover_pkt->options, DHO_PAD, DHCP_MAX_OPTION_LEN);
-    strncpy((char *)discover_pkt->options, "\x63\x82\x53\x63", 4);		/* magic cookie */
+    strncpy((char*)discover_pkt->options, "\x63\x82\x53\x63", 4);       /* magic cookie */
     options_len = 4;
     add_option(0, DHO_DHCP_MESSAGE_TYPE, 1, 1, 1, discover_pkt->options, &options_len,
-	    DHCPDISCOVER);
+               DHCPDISCOVER);
     add_option(0, DHO_DHCP_PARAMETER_REQUEST_LIST, 4, 1, 1, discover_pkt->options,
-	    &options_len, DHO_SUBNET_MASK, DHO_ROUTERS, DHO_DOMAIN_NAME,
-	    DHO_DOMAIN_NAME_SERVERS);
+               &options_len, DHO_SUBNET_MASK, DHO_ROUTERS, DHO_DOMAIN_NAME,
+               DHO_DOMAIN_NAME_SERVERS);
     add_option(0, DHO_DHCP_LEASE_TIME, 1, 4, 1, discover_pkt->options, &options_len,
-	    htonl(60));
+               htonl(60));
 
-    if (options_len < DHCP_MAX_OPTION_LEN)
-	discover_pkt->options[options_len++] = DHO_END;
+    if (options_len < DHCP_MAX_OPTION_LEN) {
+        discover_pkt->options[options_len++] = DHO_END;
+    }
 }
 
 /*
  * gen_request(): Generate a DHCPv4 request packet.
- * 
+ *
  * Input variables
  * giaddr is the address to copy into the giaddr element.
  * yiaddr is the address to store in the DHO_DHCP_REQUESTED_ADDRESS option.
@@ -214,36 +216,36 @@ gen_discover(struct dhcp_packet *discover_pkt, const struct in_addr *giaddr)
  * dhcp_pkt points to storage for the packet to be generated.
  */
 void
-gen_request(struct dhcp_packet *dhcp_pkt, const struct in_addr *giaddr,
-	const struct in_addr *yiaddr, const unsigned char *server_id)
-{
+gen_request(struct dhcp_packet* dhcp_pkt, const struct in_addr* giaddr,
+            const struct in_addr* yiaddr, const unsigned char* server_id) {
     size_t options_len;
 
-    bzero((char *) dhcp_pkt, sizeof(struct dhcp_packet));
+    bzero((char*) dhcp_pkt, sizeof(struct dhcp_packet));
     dhcp_pkt->op = BOOTREQUEST;
     dhcp_pkt->htype = HTYPE_ETHER;
     dhcp_pkt->hlen = 6;
     dhcp_pkt->hops = 1;
-    dhcp_pkt->xid = 0x12345678;	/* transaction id - fix */
+    dhcp_pkt->xid = 0x12345678; /* transaction id - fix */
     dhcp_pkt->secs = 0;
     dhcp_pkt->flags = 0;
     memcpy(&dhcp_pkt->giaddr, giaddr, sizeof((*dhcp_pkt).giaddr));
     /*memcpy(&dhcp_pkt->yiaddr, yiaddr, sizeof((*dhcp_pkt).yiaddr));*/
-    strncpy((char *)dhcp_pkt->chaddr, "\x12\x34\x56\x78\x9a\xbc", 6);	/* client hardware addr - fix */
+    strncpy((char*)dhcp_pkt->chaddr, "\x12\x34\x56\x78\x9a\xbc", 6);    /* client hardware addr - fix */
     memset(dhcp_pkt->options, DHO_PAD, DHCP_MAX_OPTION_LEN);
-    strncpy((char *)dhcp_pkt->options, "\x63\x82\x53\x63", 4);		/* magic cookie */
+    strncpy((char*)dhcp_pkt->options, "\x63\x82\x53\x63", 4);       /* magic cookie */
     options_len = 4;
     add_option(0, DHO_DHCP_MESSAGE_TYPE, 1, 1, 1, dhcp_pkt->options, &options_len,
-	    DHCPREQUEST);
+               DHCPREQUEST);
     add_option(0, DHO_DHCP_SERVER_IDENTIFIER, 1, 4, 0, dhcp_pkt->options,
-	    &options_len, server_id);
+               &options_len, server_id);
     add_option(0, DHO_DHCP_REQUESTED_ADDRESS, 1, 4, 1, dhcp_pkt->options,
-	    &options_len, *yiaddr);
+               &options_len, *yiaddr);
     add_option(0, DHO_DHCP_LEASE_TIME, 1, 4, 1, dhcp_pkt->options, &options_len,
-	    htonl(60));
+               htonl(60));
 
-    if (options_len < DHCP_MAX_OPTION_LEN)
-	dhcp_pkt->options[options_len++] = DHO_END;
+    if (options_len < DHCP_MAX_OPTION_LEN) {
+        dhcp_pkt->options[options_len++] = DHO_END;
+    }
 }
 
 /*
@@ -257,9 +259,9 @@ gen_request(struct dhcp_packet *dhcp_pkt, const struct in_addr *giaddr,
  * send_laddr: Local address of socket, for informational messages only.
  */
 void
-dhcp_send(int v6, const unsigned char *msg, int send_fd, const struct sockaddr *r_addr,
-	const struct sockaddr_storage *send_laddr) {
-    
+dhcp_send(int v6, const unsigned char* msg, int send_fd, const struct sockaddr* r_addr,
+          const struct sockaddr_storage* send_laddr) {
+
     size_t num_octets;
     ssize_t num_written;
     char addrbuf[ADDR_NAME_BUFSIZE];
@@ -267,22 +269,22 @@ dhcp_send(int v6, const unsigned char *msg, int send_fd, const struct sockaddr *
 
     num_octets = v6 ? sizeof(struct dhcpv6_packet) : sizeof(struct dhcp_packet);
     if (dk_set(DK_MSG)) {
-	fprintf(stderr, "Sending %zu octets to socket fd %u, local %s remote %s",
-		num_octets, send_fd,
-		addrName((struct sockaddr_storage *)send_laddr, addrbuf, sizeof(addrbuf)),
-		addrName((struct sockaddr_storage *)r_addr, addrbuf2, sizeof(addrbuf2)));
-	fprintf(stderr, "Packet contents:\n");
-	print_dhcp_packet(v6, msg, num_octets);
+        fprintf(stderr, "Sending %zu octets to socket fd %u, local %s remote %s",
+                num_octets, send_fd,
+                addrName((struct sockaddr_storage*)send_laddr, addrbuf, sizeof(addrbuf)),
+                addrName((struct sockaddr_storage*)r_addr, addrbuf2, sizeof(addrbuf2)));
+        fprintf(stderr, "Packet contents:\n");
+        print_dhcp_packet(v6, msg, num_octets);
     }
     num_written = sendto(send_fd, msg, num_octets, 0,
-	    r_addr, sizeof(struct sockaddr_storage));
+                         r_addr, sizeof(struct sockaddr_storage));
     if (num_written < 0) {
-	int s_errno = errno;
-	fprintf(stderr, "Send failed: %s\n", strerror(s_errno));
-	exit(1);
+        int s_errno = errno;
+        fprintf(stderr, "Send failed: %s\n", strerror(s_errno));
+        exit(1);
     }
     if ((size_t) num_written != num_octets) {
-	fprintf(stderr, "Only %d of %u octets written\n", (int) num_written, (unsigned) num_octets);
+        fprintf(stderr, "Only %d of %u octets written\n", (int) num_written, (unsigned) num_octets);
     }
 }
 
@@ -297,71 +299,70 @@ dhcp_send(int v6, const unsigned char *msg, int send_fd, const struct sockaddr *
  * msg points to storage for the received message.
  */
 void
-dhcp_recv(int v6, void *msg, int recv_fd,
-	const struct sockaddr_storage *recv_laddr) {
-    
+dhcp_recv(int v6, void* msg, int recv_fd,
+          const struct sockaddr_storage* recv_laddr) {
+
     ssize_t num_octets;
     struct sockaddr_storage sourceAddr;
     socklen_t addrSize;
     char addrbuf[ADDR_NAME_BUFSIZE];
 
     dkprintf(DK_SOCK, "Waiting for response on socket fd %u, %s",
-	    recv_fd,
-	    addrName(recv_laddr, addrbuf, sizeof(addrbuf)));
+             recv_fd,
+             addrName(recv_laddr, addrbuf, sizeof(addrbuf)));
     addrSize = sizeof(sourceAddr);
-    num_octets = recvfrom(recv_fd, msg, v6 ? sizeof(struct dhcpv6_packet) : sizeof(struct dhcp_packet), 0, (struct sockaddr *)&sourceAddr, &addrSize);
+    num_octets = recvfrom(recv_fd, msg, v6 ? sizeof(struct dhcpv6_packet) : sizeof(struct dhcp_packet), 0, (struct sockaddr*)&sourceAddr, &addrSize);
     /* TODO: check for recvfrom failure status here */
     if (dk_set(DK_MSG)) {
-	fprintf(stderr, "Got %zd octets from fd %u, %s", num_octets, recv_fd,
-		addrName(&sourceAddr, addrbuf, sizeof(addrbuf)));
-	fprintf(stderr, "Received packet contents:\n");
-	print_dhcp_packet(v6, msg, num_octets);
+        fprintf(stderr, "Got %zd octets from fd %u, %s", num_octets, recv_fd,
+                addrName(&sourceAddr, addrbuf, sizeof(addrbuf)));
+        fprintf(stderr, "Received packet contents:\n");
+        print_dhcp_packet(v6, msg, num_octets);
     }
 }
 
 void
-dora(const char *server, const char *localAddr)
-{
+dora(const char* server, const char* localAddr) {
     struct sockaddr_storage send_laddr, recv_laddr;
     struct dhcp_packet discover_pkt, offer_pkt, request_pkt, ack_pkt;
     int send_fd, recv_fd;
-    const unsigned char *type, *server_id;
+    const unsigned char* type, *server_id;
     aaddr_buf a_yiaddr;
-    struct addrinfo *remote;
-    struct in_addr *local_address;
+    struct addrinfo* remote;
+    struct in_addr* local_address;
 
     send_fd = socket_setup(AF_INET, localAddr, "bootpc", "Send", &send_laddr);
     recv_fd = socket_setup(AF_INET, localAddr, "bootps", "Recv", &recv_laddr);
 
     if ((remote = getaddr(AF_INET, server, "bootps")) == NULL) {
-	fprintf(stderr, "No addresses for %s\n", server);
-	exit(1);
+        fprintf(stderr, "No addresses for %s\n", server);
+        exit(1);
     }
 
-    local_address = &((struct sockaddr_in *)&send_laddr)->sin_addr;
+    local_address = &((struct sockaddr_in*)&send_laddr)->sin_addr;
     gen_discover(&discover_pkt, local_address);
 
-    dhcp_send(0, (unsigned char *)&discover_pkt, send_fd, remote->ai_addr, &send_laddr);
+    dhcp_send(0, (unsigned char*)&discover_pkt, send_fd, remote->ai_addr, &send_laddr);
     dhcp_recv(0, &offer_pkt, recv_fd, &recv_laddr);
     type = find_option(&offer_pkt, DHO_DHCP_MESSAGE_TYPE);
     if (type == NULL) {
-	fprintf(stderr, "DHCP reponse did not include message type option\n");
-	exit(1);
+        fprintf(stderr, "DHCP reponse did not include message type option\n");
+        exit(1);
     }
     if (type[2] != DHCPOFFER) {
-	fprintf(stderr, "DHCP reponse had message type %d; expecting DHCPOFFER\n", type[2]);
-	exit(1);
+        fprintf(stderr, "DHCP reponse had message type %d; expecting DHCPOFFER\n", type[2]);
+        exit(1);
     }
     server_id = find_option(&offer_pkt, DHO_DHCP_SERVER_IDENTIFIER);
     if (type == NULL) {
-	fprintf(stderr, "DHCP reponse did not include server identifier option\n");
-	exit(1);
+        fprintf(stderr, "DHCP reponse did not include server identifier option\n");
+        exit(1);
     }
     server_id += 2;
-    printf("Server identifier: %08x\n", ntohl(*(int *)server_id));
+    printf("Server identifier: %08x\n", ntohl(*(int*)server_id));
     printf("Offered address: %s\n", addrtoa(AF_INET, &offer_pkt.yiaddr, a_yiaddr));
     gen_request(&request_pkt, local_address, &offer_pkt.yiaddr, server_id);
-    dhcp_send(0, (unsigned char *)&request_pkt, send_fd, remote->ai_addr, &send_laddr);
+    dhcp_send(0, (unsigned char*)&request_pkt, send_fd, remote->ai_addr, &send_laddr);
     dhcp_recv(0, &ack_pkt, recv_fd, &recv_laddr);
 }
 
@@ -382,13 +383,12 @@ Discard REPLY messsages in whhich the "transaction-id" field in the message does
       used in the original message.
 */
 void
-sarr(const char *server, const char *if_name)
-{
+sarr(const char* server, const char* if_name) {
     struct sockaddr_storage send_laddr, recv_laddr;
     struct dhcpv6_packet solicit_pkt, advertise_pkt;
     int send_fd, recv_fd;
-    struct in6_addr *local_address;
-    struct addrinfo *remote;
+    struct in6_addr* local_address;
+    struct addrinfo* remote;
     struct duid client_id;
 
     get_linklocal_addr(if_name, &send_laddr);
@@ -400,48 +400,49 @@ sarr(const char *server, const char *if_name)
     recv_fd = send_fd;
 
     if (server != NULL) {
-	if (strcmp(server, "all"))
-	    server = All_DHCP_Relay_Agents_and_Servers;
-	else if (strcmp(server, "server"))
-	    server = All_DHCP_Servers;
+        if (strcmp(server, "all")) {
+            server = All_DHCP_Relay_Agents_and_Servers;
+        } else if (strcmp(server, "server")) {
+            server = All_DHCP_Servers;
+        }
     }
     if ((remote = getaddr(AF_INET6, server, "547")) == NULL) {
-	fprintf(stderr, "Conversion failed for %s\n", server);
-	exit(1);
+        fprintf(stderr, "Conversion failed for %s\n", server);
+        exit(1);
     }
 
-    local_address = &((struct sockaddr_in6 *)&send_laddr)->sin6_addr;
+    local_address = &((struct sockaddr_in6*)&send_laddr)->sin6_addr;
 
     client_id.duid_type = htons(DUID_LL);
     client_id.htype = htons(HTYPE_ETHER);
-    memset(client_id.hwaddr, 0xA, 6);	/* TEMPORARY - FIX */
+    memset(client_id.hwaddr, 0xA, 6);   /* TEMPORARY - FIX */
 
     gen_solicit(&solicit_pkt, &client_id);
-    dhcp_send(1, (unsigned char *)&solicit_pkt, send_fd, remote->ai_addr, &send_laddr);
+    dhcp_send(1, (unsigned char*)&solicit_pkt, send_fd, remote->ai_addr, &send_laddr);
     dhcp_recv(1, &advertise_pkt, recv_fd, &recv_laddr);
-/*
- *
- *    type = find_option(&offer_pkt, DHO_DHCP_MESSAGE_TYPE);
- *    if (type == NULL) {
- *	fprintf(stderr, "DHCP reponse did not include message type option\n");
- *	exit(1);
- *    }
- *    if (type[2] != DHCPOFFER) {
- *	fprintf(stderr, "DHCP reponse had message type %d; expecting DHCPOFFER\n", type[2]);
- *	exit(1);
- *    }
- *    server_id = find_option(&offer_pkt, DHO_DHCP_SERVER_IDENTIFIER);
- *    if (type == NULL) {
- *	fprintf(stderr, "DHCP reponse did not include server identifier option\n");
- *	exit(1);
- *    }
- *    server_id += 2;
- *    printf("Server identifier: %08x\n", ntohl(*(int *)server_id));
- *    printf("Offered address: %s\n", addrtoa(AF_INET, &offer_pkt.yiaddr, a_yiaddr));
- *    gen_request(&request_pkt, local_address, &offer_pkt.yiaddr, server_id);
- *    dhcp_send(&request_pkt, send_fd, remote->ai_addr, &send_laddr);
- *    dhcp_recv(&ack_pkt, recv_fd, &recv_laddr);
- */
+    /*
+     *
+     *    type = find_option(&offer_pkt, DHO_DHCP_MESSAGE_TYPE);
+     *    if (type == NULL) {
+     *  fprintf(stderr, "DHCP reponse did not include message type option\n");
+     *  exit(1);
+     *    }
+     *    if (type[2] != DHCPOFFER) {
+     *  fprintf(stderr, "DHCP reponse had message type %d; expecting DHCPOFFER\n", type[2]);
+     *  exit(1);
+     *    }
+     *    server_id = find_option(&offer_pkt, DHO_DHCP_SERVER_IDENTIFIER);
+     *    if (type == NULL) {
+     *  fprintf(stderr, "DHCP reponse did not include server identifier option\n");
+     *  exit(1);
+     *    }
+     *    server_id += 2;
+     *    printf("Server identifier: %08x\n", ntohl(*(int *)server_id));
+     *    printf("Offered address: %s\n", addrtoa(AF_INET, &offer_pkt.yiaddr, a_yiaddr));
+     *    gen_request(&request_pkt, local_address, &offer_pkt.yiaddr, server_id);
+     *    dhcp_send(&request_pkt, send_fd, remote->ai_addr, &send_laddr);
+     *    dhcp_recv(&ack_pkt, recv_fd, &recv_laddr);
+     */
 }
 
 /*
@@ -451,26 +452,25 @@ sarr(const char *server, const char *if_name)
  * Use IA_TA to request temporary addresses
  */
 void
-gen_solicit(struct dhcpv6_packet *dhcp_pkt, const struct duid *client_id)
-{
+gen_solicit(struct dhcpv6_packet* dhcp_pkt, const struct duid* client_id) {
     int tid;
     int i;
     size_t options_len = 0;
 
-    bzero((char *) dhcp_pkt, sizeof(struct dhcpv6_packet));
+    bzero((char*) dhcp_pkt, sizeof(struct dhcpv6_packet));
     dhcp_pkt->msg_type = DHCPV6_SOLICIT;
     tid = rand();
     for (i = 0; i < 2; i++) {
-	dhcp_pkt->transaction_id[i] = (unsigned char)tid;
-	tid >>= 8;
+        dhcp_pkt->transaction_id[i] = (unsigned char)tid;
+        tid >>= 8;
     }
     add_option(1, D6O_CLIENTID, 1, sizeof(struct duid), 0, dhcp_pkt->options,
-	    &options_len, client_id);
+               &options_len, client_id);
     add_option(1, D6O_IA_TA, 1, 4, 1, dhcp_pkt->options,
-	    &options_len, "0xabcd");	/* Temporary - FIX */
+               &options_len, "0xabcd");    /* Temporary - FIX */
     /* D60_ORO: Option Request Option */
     add_option(1, D6O_ORO, 1, 2, 1, dhcp_pkt->options,
-	    &options_len, D6O_NAME_SERVERS);
+               &options_len, D6O_NAME_SERVERS);
 }
 
 /*
@@ -494,37 +494,34 @@ gen_solicit(struct dhcpv6_packet *dhcp_pkt, const struct duid *client_id)
  */
 void
 add_option(int v6, unsigned optnum, unsigned count, size_t size,
-	int direct, unsigned char options[], size_t *buffer_used, ...)
-{
+           int direct, unsigned char options[], size_t* buffer_used, ...) {
     va_list ap;
     unsigned i;
     size_t buffer_size = v6 ? sizeof(struct dhcpv6_packet) : DHCP_MAX_OPTION_LEN;
 
     if ((*buffer_used + (v6 ? 4 : 2) + count * size) > buffer_size) {
-	fprintf(stderr, "%s: Insufficient option space\n", progName);
-	exit(1);
+        fprintf(stderr, "%s: Insufficient option space\n", progName);
+        exit(1);
     }
     if (v6) {
-	struct v6_option *opt = (struct v6_option *)&options[(*buffer_used)];
-	opt->code = htons(optnum);
-	opt->len = htons(count * size);
-	*buffer_used += 4;
-    }
-    else {
-	options[(*buffer_used)++] = optnum;
-	options[(*buffer_used)++] = count * size;
+        struct v6_option* opt = (struct v6_option*)&options[(*buffer_used)];
+        opt->code = htons(optnum);
+        opt->len = htons(count * size);
+        *buffer_used += 4;
+    } else {
+        options[(*buffer_used)++] = optnum;
+        options[(*buffer_used)++] = count * size;
     }
     va_start(ap,buffer_used);
     for (i = 1; i <= count; i++) {
-	if (direct) {
-	    int value = va_arg(ap, int);
-	    memcpy(&options[*buffer_used], (char *)&value, size);
-	}
-	else {
-	    char *p = va_arg(ap, char *);
-	    memcpy(&options[*buffer_used], p, size);
-	}
-	(*buffer_used) += size;
+        if (direct) {
+            int value = va_arg(ap, int);
+            memcpy(&options[*buffer_used], (char*)&value, size);
+        } else {
+            char* p = va_arg(ap, char*);
+            memcpy(&options[*buffer_used], p, size);
+        }
+        (*buffer_used) += size;
     }
     /* ap */
     va_end(ap);
@@ -534,11 +531,11 @@ add_option(int v6, unsigned optnum, unsigned count, size_t size,
  * Return value:
  * buf is returned.
  */
-char *
-addrtoa(int addr_fam, const struct in_addr *addr, aaddr_buf buf)
-{
-    if (inet_ntop(addr_fam, addr, buf, ADDR_NAME_BUFSIZE) == NULL)
-	strcpy(buf, "untranslatable");
+char*
+addrtoa(int addr_fam, const struct in_addr* addr, aaddr_buf buf) {
+    if (inet_ntop(addr_fam, addr, buf, ADDR_NAME_BUFSIZE) == NULL) {
+        strcpy(buf, "untranslatable");
+    }
     return buf;
 }
 
@@ -548,7 +545,7 @@ addrtoa(int addr_fam, const struct in_addr *addr, aaddr_buf buf)
  * is printed and the program exits with status 2.
  *
  * Input variables:
- * hostname: The host name to look up.  This can be either a name or an IPv4 
+ * hostname: The host name to look up.  This can be either a name or an IPv4
  *     dotted-quad address, or null to not fill in the address.
  * port: The port to include in addrinfo.  This can be either a service name or
  *     an ASCII decimal number, or null to not fill in the port number.
@@ -560,10 +557,9 @@ addrtoa(int addr_fam, const struct in_addr *addr, aaddr_buf buf)
  * A pointer to the addrinfo list.  This must be freed by the caller with
  * freeaddrinfo().
  */
-struct addrinfo *
-getaddr(int addr_fam, const char *hostname, const char *port)
-{
-    struct addrinfo *ai;
+struct addrinfo*
+getaddr(int addr_fam, const char* hostname, const char* port) {
+    struct addrinfo* ai;
     struct addrinfo hints;
     int ret;
 
@@ -573,9 +569,9 @@ getaddr(int addr_fam, const char *hostname, const char *port)
     hints.ai_protocol = IPPROTO_UDP;
 
     if ((ret = getaddrinfo(hostname, port, &hints, &ai)) != 0) {
-	fprintf(stderr, "%s: %s: getaddrinfo: %s/%s\n", progName,
-		hostname == NULL ? "" : hostname, port == NULL ? "" : port, gai_strerror(ret));
-	exit(2);
+        fprintf(stderr, "%s: %s: getaddrinfo: %s/%s\n", progName,
+                hostname == NULL ? "" : hostname, port == NULL ? "" : port, gai_strerror(ret));
+        exit(2);
     }
     return ai;
 }
@@ -594,19 +590,19 @@ getaddr(int addr_fam, const char *hostname, const char *port)
  * Return value:
  * buf is returned.
  */
-char *
-addrName(const struct sockaddr_storage *addr, char *name, size_t bufsize)
-{
-    char *buf = name;
+char*
+addrName(const struct sockaddr_storage* addr, char* name, size_t bufsize) {
+    char* buf = name;
     char servbuf[30];
 
-    if (getnameinfo((struct sockaddr *)addr, sizeof(struct sockaddr_storage),
-	  name, bufsize, servbuf, 30, 0) != 0)
-	strncpy(buf, "untranslatable", bufsize-1);
-    else {
-	size_t len = strlen(buf);
-	if (len < bufsize)
-	    snprintf(name + len, bufsize - len, " port %s", servbuf);
+    if (getnameinfo((struct sockaddr*)addr, sizeof(struct sockaddr_storage),
+                    name, bufsize, servbuf, 30, 0) != 0) {
+        strncpy(buf, "untranslatable", bufsize-1);
+    } else {
+        size_t len = strlen(buf);
+        if (len < bufsize) {
+            snprintf(name + len, bufsize - len, " port %s", servbuf);
+        }
     }
     return buf;
 }
@@ -618,7 +614,7 @@ addrName(const struct sockaddr_storage *addr, char *name, size_t bufsize)
  *
  * Output variables:
  * The link-local address for the interface is stored in addr.
- * 
+ *
  * Return value:
  * 1 on success, 0 if no link-local address is found.
  *
@@ -626,49 +622,49 @@ addrName(const struct sockaddr_storage *addr, char *name, size_t bufsize)
  * printed and the program is exited with status 2.
  */
 int
-get_linklocal_addr(const char if_name[], struct sockaddr_storage *addr)
-{
+get_linklocal_addr(const char if_name[], struct sockaddr_storage* addr) {
+
+    struct ifaddrs* ifaddr, *ifa;
 
-    struct ifaddrs *ifaddr, *ifa;
- 
     if (getifaddrs(&ifaddr) == -1) {
-	fprintf(stderr, "%s: Could not get interface addresses: %s\n",
-		progName, strerror(errno));
+        fprintf(stderr, "%s: Could not get interface addresses: %s\n",
+                progName, strerror(errno));
         exit(2);
     }
- 
+
     for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
-	if (ifa->ifa_addr->sa_family == AF_INET6 && strcmp(ifa->ifa_name, if_name) == 0 &&
-		(ntohs(((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr.__in6_u.__u6_addr16[0]) & 0xffc0) == 0xfe80)
-	    break;
+        if (ifa->ifa_addr->sa_family == AF_INET6 && strcmp(ifa->ifa_name, if_name) == 0 &&
+                (ntohs(((struct sockaddr_in6*)ifa->ifa_addr)->sin6_addr.__in6_u.__u6_addr16[0]) & 0xffc0) == 0xfe80) {
+            break;
+        }
+    }
+    if (ifa != NULL) {
+        memcpy(addr, ifa->ifa_addr, sizeof(struct sockaddr_storage));
     }
-    if (ifa != NULL)
-	memcpy(addr, ifa->ifa_addr, sizeof(struct sockaddr_storage));
     freeifaddrs(ifaddr);
     return ifa != NULL;
 }
 
 void
-print_addrinfo(FILE *f, const struct addrinfo *addr)
-{
+print_addrinfo(FILE* f, const struct addrinfo* addr) {
     fprintf(f, "Addrinfo:\n");
     fprintf(f, "flags: 0x%x;  family: %d;  socktype: %d;  proto: %d;\n",
-	    addr->ai_flags, addr->ai_family, addr->ai_socktype, addr->ai_protocol);
+            addr->ai_flags, addr->ai_family, addr->ai_socktype, addr->ai_protocol);
     fprintf(f, "addrlen: %u;  addr: %p;  canonname: %s;  next: %p\n",
-	    addr->ai_addrlen, addr->ai_addr, addr->ai_canonname, addr->ai_next);
-    if (addr->ai_family == AF_INET6)
-	print_sa6_info(f, (struct sockaddr_in6 *)addr->ai_addr);
+            addr->ai_addrlen, addr->ai_addr, addr->ai_canonname, addr->ai_next);
+    if (addr->ai_family == AF_INET6) {
+        print_sa6_info(f, (struct sockaddr_in6*)addr->ai_addr);
+    }
 }
 
 void
-print_sa6_info(FILE *f, const struct sockaddr_in6 *sa)
-{
+print_sa6_info(FILE* f, const struct sockaddr_in6* sa) {
     char addrbuf[ADDR_NAME_BUFSIZE];
 
     fprintf(f, "IPv6 sockaddr info:\n");
     fprintf(f, "family: %u;  flowinfo: 0x%x;  scope-id: %u  addr: %s\n",
-	    sa->sin6_family, sa->sin6_flowinfo, sa->sin6_scope_id,
-	    addrName((struct sockaddr_storage *)sa, addrbuf, sizeof(addrbuf)));
+            sa->sin6_family, sa->sin6_flowinfo, sa->sin6_scope_id,
+            addrName((struct sockaddr_storage*)sa, addrbuf, sizeof(addrbuf)));
 }
 
 /*
@@ -682,21 +678,20 @@ print_sa6_info(FILE *f, const struct sockaddr_in6 *sa)
  * If the packet contains the option, a pointer to its start (the option
  * number) is returned.  If not, NULL is returned.
  */
-const unsigned char *
-find_option(const struct dhcp_packet *pkt, int search_opt)
-{
-    const unsigned char *p;
+const unsigned char*
+find_option(const struct dhcp_packet* pkt, int search_opt) {
+    const unsigned char* p;
 
     p = &pkt->options[4];
     while ((p - pkt->options) < DHCP_MAX_OPTION_LEN && *p != DHO_END) {
-	if (*p == search_opt)
-	    return p;
-	else if (*p == DHO_PAD)
-	    p++;
-	else {
-	    size_t len = p[1];
-	    p += 2 + len;
-	}
+        if (*p == search_opt) {
+            return p;
+        } else if (*p == DHO_PAD) {
+            p++;
+        } else {
+            size_t len = p[1];
+            p += 2 + len;
+        }
     }
     return NULL;
 }

+ 3 - 3
tests/tools/perfdhcp/perfdhcp.h

@@ -1,6 +1,6 @@
 #ifdef __cplusplus
 extern "C" {
-#endif 
+#endif
 
 #define FLEXIBLE_ARRAY_MEMBER 500
 #define ADDR_NAME_BUFSIZE (NI_MAXHOST + 30)
@@ -23,8 +23,8 @@ struct v6_option {
 
 typedef char aaddr_buf[ADDR_NAME_BUFSIZE];
 
-char *addrtoa(int addr_fam, const struct in_addr *addr, aaddr_buf buf);
-void print_dhcp_packet(int v6, const void *pkt, size_t len);
+char* addrtoa(int addr_fam, const struct in_addr* addr, aaddr_buf buf);
+void print_dhcp_packet(int v6, const void* pkt, size_t len);
 
 const char progName[] = "dhcpperf";
 

+ 327 - 306
tests/tools/perfdhcp/procconf.cc

@@ -17,11 +17,11 @@
 #include <errno.h>
 #include "procconf.h"
 
-static char errmsg[256];	/* for returning error descriptions */
-static const char *pc_name;
-static const char *pc_usage;
-static unsigned debugLevel = 0;		/* set by debug option */
-static int readConf = 1;		/* should config file be read? */
+static char errmsg[256];    /* for returning error descriptions */
+static const char* pc_name;
+static const char* pc_usage;
+static unsigned debugLevel = 0;     /* set by debug option */
+static int readConf = 1;        /* should config file be read? */
 #define VERBOSE_DEBUG 7
 
 #define INTERNAL_ERROR -1
@@ -32,47 +32,47 @@ static int readConf = 1;		/* should config file be read? */
  * global errmsg.
  */
 static void
-error(const int errtype, const char *format, ...)
-{
+error(const int errtype, const char* format, ...) {
     va_list ap;
 
     va_start(ap,format);
-    if (pc_usage != NULL) {	/* error messages should be printed directly */
-	fprintf(stderr, "%s: ", pc_name);
-	vfprintf(stderr, format, ap);
-	putc('\n', stderr);
-	if (errtype == USAGE_ERROR)
-	    fputs(pc_usage, stderr);
-	exit(errtype == USAGE_ERROR ? 2 : 1);
+    if (pc_usage != NULL) { /* error messages should be printed directly */
+        fprintf(stderr, "%s: ", pc_name);
+        vfprintf(stderr, format, ap);
+        putc('\n', stderr);
+        if (errtype == USAGE_ERROR) {
+            fputs(pc_usage, stderr);
+        }
+        exit(errtype == USAGE_ERROR ? 2 : 1);
+    } else {
+        vsnprintf(errmsg, sizeof(errmsg), format, ap);
     }
-    else
-	vsnprintf(errmsg, sizeof(errmsg), format, ap);
     va_end(ap);
 }
 
-static void *
-pc_malloc(size_t size)
-{
-    void *ret = malloc(size);
-    if (ret == NULL)
-	error(INTERNAL_ERROR, "Out of memory");
+static void*
+pc_malloc(size_t size) {
+    void* ret = malloc(size);
+    if (ret == NULL) {
+        error(INTERNAL_ERROR, "Out of memory");
+    }
     return ret;
 }
 
 static void
-opterror(const char *expected, const char *value, const confvar_t *varDesc,
-	const char filename[], const char *detail)
-{
-    if (detail == NULL)
-	detail = "";
+opterror(const char* expected, const char* value, const confvar_t* varDesc,
+         const char filename[], const char* detail) {
+    if (detail == NULL) {
+        detail = "";
+    }
     if (filename == NULL)
-	error(USAGE_ERROR,
-		"Invalid value given for option -%c: expected %s, got: %s%s",
-		varDesc->outind, expected, value, detail);
+        error(USAGE_ERROR,
+              "Invalid value given for option -%c: expected %s, got: %s%s",
+              varDesc->outind, expected, value, detail);
     else
-	error(USAGE_ERROR, "Invalid value given in configuration file \"%s\""
-		" for option %s: expected %s, got: %s%s",
-		filename, varDesc->varname, expected, value, detail);
+        error(USAGE_ERROR, "Invalid value given in configuration file \"%s\""
+              " for option %s: expected %s, got: %s%s",
+              filename, varDesc->varname, expected, value, detail);
 }
 
 /*
@@ -100,165 +100,173 @@ opterror(const char *expected, const char *value, const confvar_t *varDesc,
  * -1 is returned.
  */
 static int
-addOptVal(const cf_source source, const char *value, const confvar_t *varDesc,
-	confval **first, confval **last, const char filename[])
-{
-    const void *addr;
+addOptVal(const cf_source source, const char* value, const confvar_t* varDesc,
+          confval** first, confval** last, const char filename[]) {
+    const void* addr;
     confval data, *ret_data;
     int seen = *first != NULL;
-    char *ptr;
+    char* ptr;
     int err;
 
-     /* if first instance of this option, store result to given addr */
+    /* if first instance of this option, store result to given addr */
     addr = seen ? NULL : varDesc->addr;
     switch (varDesc->type) {
     case CF_CHAR:
-	if (strlen(value) > 1) {	/* length 0 is OK; gives null char */
-	    opterror("a single character", value, varDesc, filename, NULL);
-	    return -1;
-	}
-	data.value.charval = *value;
-	if (addr != NULL)
-	    *(char *) addr = *value;
-	break;
+        if (strlen(value) > 1) {    /* length 0 is OK; gives null char */
+            opterror("a single character", value, varDesc, filename, NULL);
+            return -1;
+        }
+        data.value.charval = *value;
+        if (addr != NULL) {
+            *(char*) addr = *value;
+        }
+        break;
     case CF_STRING:
     case CF_NE_STRING:
-	if (varDesc->type == CF_NE_STRING && *value == '\0') {
-	    opterror("a non-empty string", value, varDesc, filename, NULL);
-	    return -1;
-	}
-	data.value.string = value;
-	if (addr != NULL)
-	    *(const char **) addr = value;
-	break;
+        if (varDesc->type == CF_NE_STRING && *value == '\0') {
+            opterror("a non-empty string", value, varDesc, filename, NULL);
+            return -1;
+        }
+        data.value.string = value;
+        if (addr != NULL) {
+            *(const char**) addr = value;
+        }
+        break;
     case CF_INT:
     case CF_NON_NEG_INT:
     case CF_POS_INT:
     case CF_PDEBUG:
-	/* todo: check for out-of-range result */
-	errno = 0;
-	data.value.intval = strtol(value, &ptr, 0);
-	if (errno == ERANGE) {
-	    opterror("an integer", value, varDesc, filename,
-		    " (out of range)");
-	    return -1;
-	}
-	err = *value == '\0' || *ptr != '\0';
-	switch (varDesc->type) {
-	case CF_INT:
-	    if (err) {
-		opterror("an integer", value, varDesc, filename, NULL);
-		return -1;
-	    }
-	    break;
-	case CF_NON_NEG_INT:
-	    if (err || data.value.intval < 0) {
-		opterror("a non-negative integer", value, varDesc, filename,
-			NULL);
-		return -1;
-	    }
-	    data.value.nnint = data.value.intval;
-	    break;
-	case CF_POS_INT:
-	case CF_PDEBUG:
-	    if (err || data.value.intval <= 0) {
-		opterror("a positive integer", value, varDesc, filename, NULL);
-		return -1;
-	    }
-	    data.value.nnint = data.value.intval;
-	    break;
-	default:
-	    /* To avoid complaints from -Wall */
-	    ;
-	}
-	if (addr != NULL)
-	    *(int *) addr = data.value.intval;
-	if (!seen && varDesc->type == CF_PDEBUG)
-	    debugLevel = data.value.intval;
-	break;
+        /* todo: check for out-of-range result */
+        errno = 0;
+        data.value.intval = strtol(value, &ptr, 0);
+        if (errno == ERANGE) {
+            opterror("an integer", value, varDesc, filename,
+                     " (out of range)");
+            return -1;
+        }
+        err = *value == '\0' || *ptr != '\0';
+        switch (varDesc->type) {
+        case CF_INT:
+            if (err) {
+                opterror("an integer", value, varDesc, filename, NULL);
+                return -1;
+            }
+            break;
+        case CF_NON_NEG_INT:
+            if (err || data.value.intval < 0) {
+                opterror("a non-negative integer", value, varDesc, filename,
+                         NULL);
+                return -1;
+            }
+            data.value.nnint = data.value.intval;
+            break;
+        case CF_POS_INT:
+        case CF_PDEBUG:
+            if (err || data.value.intval <= 0) {
+                opterror("a positive integer", value, varDesc, filename, NULL);
+                return -1;
+            }
+            data.value.nnint = data.value.intval;
+            break;
+        default:
+            /* To avoid complaints from -Wall */
+            ;
+        }
+        if (addr != NULL) {
+            *(int*) addr = data.value.intval;
+        }
+        if (!seen && varDesc->type == CF_PDEBUG) {
+            debugLevel = data.value.intval;
+        }
+        break;
     case CF_FLOAT:
     case CF_NON_NEG_FLOAT:
     case CF_POS_FLOAT:
-	/* todo: check for out-of-range result */
-	errno = 0;
-	data.value.floatval = strtod(value, &ptr);
-	if (errno == ERANGE) {
-	    opterror("a number", value, varDesc, filename, " (out of range)");
-	    return -1;
-	}
-	err = *value == '\0' || *ptr != '\0';
-	switch (varDesc->type) {
-	case CF_FLOAT:
-	    if (err) {
-		opterror("a number", value, varDesc, filename, NULL);
-		return -1;
-	    }
-	    break;
-	case CF_NON_NEG_FLOAT:
-	    if (err || data.value.floatval < 0) {
-		opterror("a non-negative number", value, varDesc, filename,
-			NULL);
-		return -1;
-	    }
-	    break;
-	case CF_POS_FLOAT:
-	    if (err || data.value.floatval <= 0) {
-		opterror("a positive number", value, varDesc, filename, NULL);
-		return -1;
-	    }
-	    break;
-	default:
-	    /* To avoid complaints from -Wall */
-	    ;
-	}
-	if (addr != NULL)
-	    *(double *) addr = data.value.floatval;
-	break;
+        /* todo: check for out-of-range result */
+        errno = 0;
+        data.value.floatval = strtod(value, &ptr);
+        if (errno == ERANGE) {
+            opterror("a number", value, varDesc, filename, " (out of range)");
+            return -1;
+        }
+        err = *value == '\0' || *ptr != '\0';
+        switch (varDesc->type) {
+        case CF_FLOAT:
+            if (err) {
+                opterror("a number", value, varDesc, filename, NULL);
+                return -1;
+            }
+            break;
+        case CF_NON_NEG_FLOAT:
+            if (err || data.value.floatval < 0) {
+                opterror("a non-negative number", value, varDesc, filename,
+                         NULL);
+                return -1;
+            }
+            break;
+        case CF_POS_FLOAT:
+            if (err || data.value.floatval <= 0) {
+                opterror("a positive number", value, varDesc, filename, NULL);
+                return -1;
+            }
+            break;
+        default:
+            /* To avoid complaints from -Wall */
+            ;
+        }
+        if (addr != NULL) {
+            *(double*) addr = data.value.floatval;
+        }
+        break;
     case CF_SWITCH:
     case CF_NOCONF:
     case CF_SDEBUG:
-	/* If option not turned on, ignore */
-	if (source == CF_FILE && *value != '1')
-	    return 0;
-	data.value.switchval = varDesc->value;
-	value = "1";	/* for debugging */
-	if (addr != NULL)
-	    *(int *) addr = varDesc->value;
-	if (!seen)
-	    switch (varDesc->type) {
-		case CF_NOCONF:
-		    readConf = 0;
-		    break;
-		case CF_SDEBUG:
-		    debugLevel = 9;
-		    break;
-		default:
-		    /* To avoid complaints from -Wall */
-		    ;
-	    }
-	break;
+        /* If option not turned on, ignore */
+        if (source == CF_FILE && *value != '1') {
+            return 0;
+        }
+        data.value.switchval = varDesc->value;
+        value = "1";    /* for debugging */
+        if (addr != NULL) {
+            *(int*) addr = varDesc->value;
+        }
+        if (!seen)
+            switch (varDesc->type) {
+            case CF_NOCONF:
+                readConf = 0;
+                break;
+            case CF_SDEBUG:
+                debugLevel = 9;
+                break;
+            default:
+                /* To avoid complaints from -Wall */
+                ;
+            }
+        break;
     case CF_ENDLIST:
-	/* To avoid complaints from -Wall */
-	;
+        /* To avoid complaints from -Wall */
+        ;
     }
     data.strval = value;
     data.source = source;
     data.next = NULL;
-    if ((ret_data = (confval *)pc_malloc(sizeof(confval))) == NULL)
-	return -1;
+    if ((ret_data = (confval*)pc_malloc(sizeof(confval))) == NULL) {
+        return -1;
+    }
     *ret_data = data;
-    if (seen)
-	(*last)->next = ret_data;
-    else
-	*first = ret_data;
+    if (seen) {
+        (*last)->next = ret_data;
+    } else {
+        *first = ret_data;
+    }
     *last = ret_data;
     if (debugLevel >= VERBOSE_DEBUG)
-	fprintf(stderr, "Option %c (%s) gets value \"%s\" from %s%s\n",
-		0 < varDesc->outind && varDesc->outind < 256 ?
-		varDesc->outind : '-',
-		varDesc->varname == NULL ? "-" : varDesc->varname, value,
-		source == CF_ARGS ? "command line" : "config file", seen ?
-			" (already seen)" : "");
+        fprintf(stderr, "Option %c (%s) gets value \"%s\" from %s%s\n",
+                0 < varDesc->outind && varDesc->outind < 256 ?
+                varDesc->outind : '-',
+                varDesc->varname == NULL ? "-" : varDesc->varname, value,
+                source == CF_ARGS ? "command line" : "config file", seen ?
+                " (already seen)" : "");
     return 1;
 }
 
@@ -287,61 +295,64 @@ addOptVal(const cf_source source, const char *value, const confvar_t *varDesc,
  */
 static int
 procConfFile(const char filename[], const confvar_t optConf[],
-	confval *first[], confval *last[]) {
+             confval* first[], confval* last[]) {
     int count;
-    char *home;
+    char* home;
     int ind;
 
     if (!strncmp(filename, "~/", 2)) {
-	char *path;
+        char* path;
 
-	if (!(home = getenv("HOME"))) {
-	    if (debugLevel > 2)
-		fprintf(stderr, "HOME environment variable not set.\n");
-	    return 0;
-	}
-	if ((path = pc_malloc(strlen(home) + strlen(filename))) == NULL)
-	    return -1;
-	strcpy(path, home);
-	strcat(path, filename+1);
-	if (defopen(path)) {
-	    free(path);
-	    return 0;
-	}
-	free(path);
-    }
-    else if (defopen((char *)filename)) {
-	if (debugLevel > 2)
-	    fprintf(stderr, "Config file '%s' not found.\n", filename);
-	return 0;
+        if (!(home = getenv("HOME"))) {
+            if (debugLevel > 2) {
+                fprintf(stderr, "HOME environment variable not set.\n");
+            }
+            return 0;
+        }
+        if ((path = pc_malloc(strlen(home) + strlen(filename))) == NULL) {
+            return -1;
+        }
+        strcpy(path, home);
+        strcat(path, filename+1);
+        if (defopen(path)) {
+            free(path);
+            return 0;
+        }
+        free(path);
+    } else if (defopen((char*)filename)) {
+        if (debugLevel > 2) {
+            fprintf(stderr, "Config file '%s' not found.\n", filename);
+        }
+        return 0;
     }
     count = 0;
     for (ind = 0; optConf[ind].type != CF_ENDLIST; ind++) {
-	char buf[128];
-	char *s;
+        char buf[128];
+        char* s;
 
-	if (optConf[ind].varname == NULL)
-	    continue;
-	strncpy(buf, optConf[ind].varname, 126);
-	strcat(buf, "=");
-	if ((s = defread(buf)) != NULL) {
-	    int ret;
+        if (optConf[ind].varname == NULL) {
+            continue;
+        }
+        strncpy(buf, optConf[ind].varname, 126);
+        strcat(buf, "=");
+        if ((s = defread(buf)) != NULL) {
+            int ret;
 
-	    if ((s = strdup(s)) == NULL) {
-		error(INTERNAL_ERROR, "Out of memory");
-		return -1;
-	    }
-	    switch ((ret = addOptVal(CF_FILE, s, &optConf[ind], &first[ind],
-		    &last[ind], filename))) {
-	    case 1:
-		count++;
-		break;
-	    case 0:
-		break;
-	    default:
-		return ret;
-	    }
-	}
+            if ((s = strdup(s)) == NULL) {
+                error(INTERNAL_ERROR, "Out of memory");
+                return -1;
+            }
+            switch ((ret = addOptVal(CF_FILE, s, &optConf[ind], &first[ind],
+                                     &last[ind], filename))) {
+            case 1:
+                count++;
+                break;
+            case 0:
+                break;
+            default:
+                return ret;
+            }
+        }
     }
     return count;
 }
@@ -363,62 +374,66 @@ procConfFile(const char filename[], const confvar_t optConf[],
  * -1 is returned.
  */
 static int
-procCmdLineArgs(int *argc, const char **argv[], const confvar_t optConf[],
-	confval **first, confval **last)
-{
-    char *p;
-    extern char *optarg;	/* For getopt */
-    extern int optind;		/* For getopt */
-    extern int optopt;		/* For getopt */
+procCmdLineArgs(int* argc, const char** argv[], const confvar_t optConf[],
+                confval** first, confval** last) {
+    char* p;
+    extern char* optarg;    /* For getopt */
+    extern int optind;      /* For getopt */
+    extern int optopt;      /* For getopt */
     char optstr[514];
     unsigned optCharToConf[256];
     int optchar;
     unsigned confNum;
     int count = 0;
 
-    optind = 1;
     p = optstr;
     *(p++) = ':';
     for (confNum = 0; optConf[confNum].type != CF_ENDLIST; confNum++) {
-	unsigned outind = optConf[confNum].outind;
-	if (outind < 256 && isprint(outind)) {
-	    *(p++) = (char) outind;
-	    switch (optConf[confNum].type) {
-	    case CF_SWITCH:
-	    case CF_NOCONF:
-	    case CF_SDEBUG:
-		break;
-	    default:
-		*(p++) = ':';
-		break;
-	    }
-	    optCharToConf[outind] = confNum;
-	}
+        unsigned outind = optConf[confNum].outind;
+        if (outind < 256 && isprint(outind)) {
+            *(p++) = (char) outind;
+            switch (optConf[confNum].type) {
+            case CF_SWITCH:
+            case CF_NOCONF:
+            case CF_SDEBUG:
+                break;
+            default:
+                *(p++) = ':';
+                break;
+            }
+            optCharToConf[outind] = confNum;
+        }
     }
+    int x;
+    for (x = 0; x < *argc; x++) {
+        printf("%d:%s\n", x, (*argv)[x]);
+    }
+
     *p = '\0';
-    while ((optchar = getopt(*argc, const_cast<char **>(*argv), optstr)) != -1) {
-	int ind;
-	int ret;
+    optind = 1;
+    while ((optchar = getopt(*argc, const_cast<char**>(*argv), optstr)) != -1) {
+        int ind;
+        int ret;
 
-	if (optchar == '?') {
-	    error(USAGE_ERROR, "Unknown option character '%c'", optopt);
-	    return -1;
-	}
-	else if (optchar == ':') {
-	    error(USAGE_ERROR, "No value given for option -%c", optopt);
-	    return -1;
-	}
-	ind = optCharToConf[optchar];
-	switch (ret = addOptVal(CF_ARGS, optarg, &optConf[ind], &first[ind],
-		&last[ind], NULL)) {
-	case 1:
-	    count++;
-	    break;
-	case 0:
-	    break;
-	default:
-	    return ret;
-	}
+        printf("## %c\n", optchar);
+        if (optchar == '?') {
+            error(USAGE_ERROR, "Unknown option character '%c'", optopt);
+            return -1;
+        } else if (optchar == ':') {
+            error(USAGE_ERROR, "No value given for option -%c", optopt);
+            return -1;
+        }
+        ind = optCharToConf[optchar];
+        switch (ret = addOptVal(CF_ARGS, optarg, &optConf[ind], &first[ind],
+                                &last[ind], NULL)) {
+        case 1:
+            count++;
+            break;
+        case 0:
+            break;
+        default:
+            return ret;
+        }
     }
     *argc -= optind;
     *argv += optind;
@@ -444,56 +459,59 @@ procCmdLineArgs(int *argc, const char **argv[], const confvar_t optConf[],
  * After processing, argc will have been adjusted to be the number of
  * non-option arguments and argv will have been adjusted to start with the
  * first non-option argument.
- * 
+ *
  * Return value:
  * On success, NULL.
  * On error, a message describing the problem.
  */
-const char *
-procOpts(int *argc, const char **argv[], const confvar_t optConf[],
-	confdata_t *confdata, const char confFile[], const char name[],
-	const char usage[])
-{
+const char*
+procOpts(int* argc, const char** argv[], const confvar_t optConf[],
+         confdata_t* confdata, const char confFile[], const char name[],
+         const char usage[]) {
     /* Number of configuration options given in optConf */
     unsigned numConf;
     /* First & last records in the linked list maintained for each option */
-    confval **first, **last;
-    unsigned maxOptIndex = 0;	/* The highest option index number seen */
+    confval** first, **last;
+    unsigned maxOptIndex = 0;   /* The highest option index number seen */
     /* number of option instances + assignments given */
     int numOptsFound;
     unsigned optNum;
     unsigned i;
-    confval **valuePointers;
+    confval** valuePointers;
 
     pc_name = name;
     pc_usage = usage;
     for (numConf = 0; optConf[numConf].type != CF_ENDLIST; numConf++) {
-	unsigned outind = optConf[numConf].outind;
+        unsigned outind = optConf[numConf].outind;
 
-	if ((outind & ~CF_NOTFLAG) > maxOptIndex)
-	    maxOptIndex = outind & ~CF_NOTFLAG;
+        if ((outind & ~CF_NOTFLAG) > maxOptIndex) {
+            maxOptIndex = outind & ~CF_NOTFLAG;
+        }
     }
-    if ((first = (confval **)pc_malloc(sizeof(confval *) * numConf)) == NULL ||
-	    (last =
-	    (confval **)pc_malloc(sizeof(confval *) * numConf)) == NULL)
-	return errmsg;
-    memset(first, '\0', sizeof(confval *) * numConf);
-    memset(last, '\0', sizeof(confval *) * numConf);
+    if ((first = (confval**)pc_malloc(sizeof(confval*) * numConf)) == NULL ||
+            (last =
+                 (confval**)pc_malloc(sizeof(confval*) * numConf)) == NULL) {
+        return errmsg;
+    }
+    memset(first, '\0', sizeof(confval*) * numConf);
+    memset(last, '\0', sizeof(confval*) * numConf);
 
     if ((numOptsFound =
-	    procCmdLineArgs(argc, argv, optConf, first, last)) < 0)
-	return errmsg;
+                procCmdLineArgs(argc, argv, optConf, first, last)) < 0) {
+        return errmsg;
+    }
     if (readConf && confFile != NULL) {
 #ifdef DEFREAD
-	int ret;
+        int ret;
 
-	if ((ret = procConfFile(confFile, optConf, first, last)) < 0)
-	    return errmsg;
-	else
-	    nummOptsFound += ret;
+        if ((ret = procConfFile(confFile, optConf, first, last)) < 0) {
+            return errmsg;
+        } else {
+            nummOptsFound += ret;
+        }
 #else
-	error(INTERNAL_ERROR, "Built without defread!");
-	return errmsg;
+        error(INTERNAL_ERROR, "Built without defread!");
+        return errmsg;
 #endif
     }
 
@@ -504,17 +522,19 @@ procOpts(int *argc, const char **argv[], const confvar_t optConf[],
      * An array of pointers is now generated for the options.
      */
     if ((valuePointers =
-	    (confval **)pc_malloc(sizeof(confval *) * numOptsFound)) == NULL ||
-	    (confdata->optVals =
-	    (cf_option *)pc_malloc(sizeof(cf_option) * numConf)) == NULL)
-	return errmsg;
+                (confval**)pc_malloc(sizeof(confval*) * numOptsFound)) == NULL ||
+            (confdata->optVals =
+                 (cf_option*)pc_malloc(sizeof(cf_option) * numConf)) == NULL) {
+        return errmsg;
+    }
     /* If option index numbers are used, allocate a map for them */
     if (maxOptIndex != 0) {
-	if ((confdata->map =
-		(cf_option **)pc_malloc(sizeof(cf_option) * (maxOptIndex+1)))
-		== NULL)
-	    return errmsg;
-	memset(confdata->map, '\0', sizeof(confval *) * (maxOptIndex+1));
+        if ((confdata->map =
+                    (cf_option**)pc_malloc(sizeof(cf_option) * (maxOptIndex+1)))
+                == NULL) {
+            return errmsg;
+        }
+        memset(confdata->map, '\0', sizeof(confval*) * (maxOptIndex+1));
     }
 
     /*
@@ -524,23 +544,24 @@ procOpts(int *argc, const char **argv[], const confvar_t optConf[],
      */
     i = 0;
     for (optNum = 0; optNum < numConf; optNum++) {
-	unsigned outind = optConf[optNum].outind;
-	confval *optval;
+        unsigned outind = optConf[optNum].outind;
+        confval* optval;
 
-	confdata->optVals[optNum].num = 0;
-	confdata->optVals[optNum].values = &valuePointers[i];
-	if (outind != 0)
-	    confdata->map[outind & ~CF_NOTFLAG] = &confdata->optVals[optNum];
-	for (optval = first[optNum]; optval != NULL; optval = optval->next) {
-	    confdata->optVals[optNum].num++;
-	    valuePointers[i++] = optval;
-	}
-	if (debugLevel > 5)
-	    fprintf(stderr, "Option %c (%s) got %d values\n",
-		    outind == 0 ? '-' : outind,
-		    optConf[optNum].varname == NULL ? "-" :
-		    optConf[optNum].varname,         
-		    confdata->optVals[optNum].num);
+        confdata->optVals[optNum].num = 0;
+        confdata->optVals[optNum].values = &valuePointers[i];
+        if (outind != 0) {
+            confdata->map[outind & ~CF_NOTFLAG] = &confdata->optVals[optNum];
+        }
+        for (optval = first[optNum]; optval != NULL; optval = optval->next) {
+            confdata->optVals[optNum].num++;
+            valuePointers[i++] = optval;
+        }
+        if (debugLevel > 5)
+            fprintf(stderr, "Option %c (%s) got %d values\n",
+                    outind == 0 ? '-' : outind,
+                    optConf[optNum].varname == NULL ? "-" :
+                    optConf[optNum].varname,
+                    confdata->optVals[optNum].num);
     }
     free(first);
     return NULL;

+ 33 - 31
tests/tools/perfdhcp/procconf.h

@@ -1,7 +1,7 @@
 #ifndef PROCCONF_H
 #define PROCCONF_H
 
-#include <limits.h>	/* for UINT_MAX */
+#include <limits.h> /* for UINT_MAX */
 
 typedef enum {
     CF_CHAR,
@@ -14,14 +14,16 @@ typedef enum {
     CF_NON_NEG_FLOAT,
     CF_POS_FLOAT,
     CF_SWITCH,
-    CF_NOCONF,	/* option to specify that config file should not be read */
-    CF_PDEBUG,	/* option to turn on debugging, with positive integer value */
-    CF_SDEBUG,	/* option to turn on debugging, without a value */
-    CF_ENDLIST	/* End of option list */
+    CF_NOCONF,  /* option to specify that config file should not be read */
+    CF_PDEBUG,  /* option to turn on debugging, with positive integer value */
+    CF_SDEBUG,  /* option to turn on debugging, without a value */
+    CF_ENDLIST  /* End of option list */
 } cf_type;
 
 typedef enum {
-    CF_NONE, CF_ARGS, CF_FILE
+    CF_NONE,
+    CF_ARGS,
+    CF_FILE
 } cf_source;
 
 /*
@@ -33,51 +35,51 @@ typedef enum {
 
 /*
  * Structure for passing varname/value pairs.
- * This gives the variable names to search for, and allows variable names to 
+ * This gives the variable names to search for, and allows variable names to
  * mapped to characters so that the characters may be used as indexes into the
  * results array.
  */
 typedef struct {
-    unsigned outind;	/* Single-character option, or option output index */
-    char *varname;	/* Long name, for config file and long option */
-    cf_type type;	/* Option type */
-    const void *addr;	/* Address of variable associated with this option */
-    int value;		/* Value to assign to switch options */
+    unsigned outind;    /* Single-character option, or option output index */
+    char* varname;  /* Long name, for config file and long option */
+    cf_type type;   /* Option type */
+    const void* addr;   /* Address of variable associated with this option */
+    int value;      /* Value to assign to switch options */
 } confvar_t;
 
 /*
  * Structure for returning assigned values.
  */
 typedef struct confval_struct {
-    const char *strval;	/* String form of value */
-    cf_source source;	/* Where value was taken from */
-    unsigned index;	/* Relative position of this instance */
+    const char* strval; /* String form of value */
+    cf_source source;   /* Where value was taken from */
+    unsigned index; /* Relative position of this instance */
     union {
-	int intval;
-	unsigned int nnint;
-	double floatval;
-	const char *string;
-	int switchval;
-	char charval;
+        int intval;
+        unsigned int nnint;
+        double floatval;
+        const char* string;
+        int switchval;
+        char charval;
     } value;
-    struct confval_struct *next;
+    struct confval_struct* next;
 } confval;
 
 /* Information about the values assigned to a particular option */
 typedef struct {
-    int num;		/* number of instances of this option */
-    confvar_t *confvar;	/* which option descriptor this corresponds to */
-    confval **values;	/* Start of pointers to values for this option */
+    int num;        /* number of instances of this option */
+    confvar_t* confvar; /* which option descriptor this corresponds to */
+    confval** values;   /* Start of pointers to values for this option */
 } cf_option;
 
 typedef struct {
-    cf_option *optVals;	/* All option values */
-    cf_option **map;	/* Option values indexed by option-char/option-index */
+    cf_option* optVals; /* All option values */
+    cf_option** map;    /* Option values indexed by option-char/option-index */
 } confdata_t;
 
-const char *
-procOpts(int *argc, const char **argv[], const confvar_t optConf[],
-	confdata_t *confdata, const char confFile[], const char name[],
-	const char usage[]);
+const char*
+procOpts(int* argc, const char** argv[], const confvar_t optConf[],
+         confdata_t* confdata, const char confFile[], const char name[],
+         const char usage[]);
 
 #endif

+ 118 - 267
tests/tools/perfdhcp/tests/cloptions_unittest.cc

@@ -18,126 +18,91 @@
 
 #include "../cloptions.h"
 
+int checkOption(const char* optflag, const char* optval,
+                confdata_t* confdata, int retExpected) {
+    const char* argv[] = { "perfdhcp", NULL, NULL, NULL };
+    int argc = 1;
+    const char* server;
+    int ret;
+
+    argv[argc++] = optflag;
+    if (optval != NULL) {
+        argv[argc++] = optval;
+    }
+    argv[argc++] = "foo";   /* server */
+    ret = procArgs(argc, argv, confdata, &server);
+    EXPECT_EQ(ret, retExpected);
+    return ret == retExpected;
+}
 
-///// \brief Check Non-Limit Options
-/////
-///// Checks that the options that are NOT related to the message are set to
-///// their default values.
-//void checkDefaultOtherValues() {
-//    EXPECT_STREQ("127.0.0.1", getAddress());
-//    EXPECT_EQ(53, getPort());
-//    EXPECT_EQ(500, getTimeout());
-//    EXPECT_STREQ("www.example.com", getQname());
-//}
-//
-///// \brief Checks the minimum and maximum value specified for an option
-/////
-///// Checks the values for one of the options whose values are stored in the
-///// class's options_ array.
-/////
-///// \param index Index of the option in the limits_ array
-///// \param minval Expected minimum value
-///// \param maxval Expected maximum value
-//void checkValuePair(int, uint32_t, uint32_t) {
-////void checkValuePair(int index, uint32_t minval = 0, uint32_t maxval = 0) {
-//    // Argument names commented out as they are not used, so this avoid a
-//    // compiler warning (which by default in BIND 10 is promoted to an
-//    // error).
-//    // maximum and minimum are not yet defined so following lines commented
-//    // out.
-//    // EXPECT_EQ(minimum(index), minval);
-//    // EXPECT_EQ(maximum(index), maxval);
-//}
-//
-///// \brief Checks that all options are at default values
-/////
-///// Checks that all options have both their maximum and minimum set to the
-///// default values.
-/////
-///// \param except Index not to check. (This allows options not being tested
-/////        to be checked to see that they are at the default value.)  As all
-/////        index values are positive, a negative value means check
-/////        everything.
-//void checkDefaultLimitsValues(int except = -1) {
-//    // Dummy use of except to avoid an error
-//    EXPECT_EQ(-1, except);
-//    // OptionInfo not yet defined!
-//    /*
-//    for (int i = 0; i < OptionInfo::SIZE; ++i) {
-//        if (i != except) {
-//            checkValuePair(i, OptionInfo::defval(i), OptionInfo::defval(i));
-//        }
-//    }
-//    */
-//}
-//
-///// \brief Check valid command option
-/////
-///// Checks that the command line specification of one of the options taking
-///// a value correctly processes the option.
-/////
-///// \param index Option index
-///// \param optflag Option flag (in the form '--option')
-///// \param optval Value to be passed to the option.
-///// \param minval Expected minimum value
-///// \param maxval Expected maximum value
-//void checkCommandValid(int index, const char* optflag, const char* optval,
-//                       uint32_t minval, uint32_t maxval) {
-//
-//    // Set up the command line and parse it.
-//    const char* argv[] = {"badpacket", NULL, NULL};
-//    argv[1] = optflag;
-//    argv[2] = optval;
-//    int argc = 3;
-//    parse(argc, const_cast<char**>(argv));
-//
-//    // Check the results.  Everything should be at the defaults except for
-//    // the specified option, where the minimum and maximum should be as
-//    // specified.
-//    checkDefaultOtherValues();
-//    checkDefaultLimitsValues(index);
-//    checkValuePair(index, minval, maxval);
-//}
-//
-/// \brief Check invalid command option
-///
-/// Passed a command with an invalid value, checks that the parsing indicates
-/// error.
-///
-/// \param optflag Option flag (in the form '-c')
-/// \param optval Value to be passed to the option.
 void checkOptionInvalid(const char* optflag, const char* optval) {
-    const char* argv[] = { "perfdhcp", NULL, NULL, "foo" };
-    int argc = sizeof(argv) / sizeof(const char* );
     confdata_t confdata;
-    const char *server;
 
-    argv[1] = optflag;
-    argv[2] = optval;
-    EXPECT_EQ(procArgs(argc, argv, &confdata, &server), 2);
+    checkOption(optflag, optval, &confdata, 2);
+}
+
+int checkOptionValid(const char* optflag, const char* optval,
+                     confdata_t* confdata) {
+
+    return checkOption(optflag, optval, confdata, 1);
 }
 
-void checkOptionValid(const char* optflag, const char* optval,
-	confdata_t *confdata) {
-    const char* argv[] = { "perfdhcp", NULL, NULL, "foo" };
-    int argc = sizeof(argv) / sizeof(const char* );
-    const char *server;
+void checkPosFloatOpt(const char* optflag) {
+    confdata_t confdata;
 
-    argv[1] = optflag;
-    argv[2] = optval;
-    EXPECT_EQ(procArgs(argc, argv, confdata, &server), 1);
+    if (checkOptionValid(optflag, "100.0", &confdata))
+        EXPECT_EQ(100.0,
+                  confdata.map[(unsigned)optflag[1]]->values[0]->value.floatval);
+    checkOptionInvalid(optflag, "0");
+    checkOptionInvalid(optflag, "-1");
+    checkOptionInvalid(optflag, "0.0");
+    checkOptionInvalid(optflag, "-1.0");
+    checkOptionInvalid(optflag, "x");
 }
 
-void checkPosIntOpt(const char *optflag) {
+void checkPosIntOpt(const char* optflag) {
     confdata_t confdata;
-    checkOptionValid(optflag, "100", &confdata);
-    EXPECT_EQ(100, confdata.map[(unsigned)optflag[1]]->values[0]->value.nnint);
+
+    if (checkOptionValid(optflag, "100", &confdata))
+        EXPECT_EQ(100,
+                  confdata.map[(unsigned)optflag[1]]->values[0]->value.nnint);
     checkOptionInvalid(optflag, "0");
     checkOptionInvalid(optflag, "-1");
     checkOptionInvalid(optflag, "1.0");
     checkOptionInvalid(optflag, "x");
 }
 
+void checkFlag(const char* optflag) {
+    confdata_t confdata;
+
+    if (checkOptionValid(optflag, NULL, &confdata))
+        EXPECT_EQ(1,
+                  confdata.map[(unsigned)optflag[1]]->values[0]->value.switchval);
+}
+
+void checkStrOpt(const char* optflag) {
+    confdata_t confdata;
+
+    if (checkOptionValid(optflag, "bar", &confdata))
+        EXPECT_EQ("bar",
+                  confdata.map[(unsigned)optflag[1]]->values[0]->value.string);
+}
+
+void checkNEStrOpt(const char* optflag) {
+    confdata_t confdata;
+
+    if (checkOptionValid(optflag, "bar", &confdata))
+        EXPECT_EQ("bar",
+                  confdata.map[(unsigned)optflag[1]]->values[0]->value.string);
+    checkOptionInvalid(optflag, "");
+}
+
+void checkFlagHandled(const char* optflag) {
+    confdata_t confdata;
+
+    checkOption(optflag, NULL, &confdata, 0);
+}
+
 TEST(CommandOptionsTest, numreq) {
     checkPosIntOpt("-n");
 }
@@ -146,166 +111,52 @@ TEST(CommandOptionsTest, rate) {
     checkPosIntOpt("-r");
 }
 
-///// \brief Check one-bit field
-/////
-///// Explicitly for those fields in the flags word that are one bit wide,
-///// perform a series of tests to check that they accept valid values and
-///// reject invalid ones.
-/////
-///// \param index Option index
-///// \param optflag Option flag (in the form '--option')
-//void checkOneBitField(int index, const char* optflag) {
-//    checkCommandValid(index, optflag, "0", 0, 0);
-//    checkCommandValid(index, optflag, "1", 1, 1);
-//    checkCommandValid(index, optflag, "0-1", 0, 1);
-//    checkCommandValid(index, optflag, "1-0", 0, 1);
-//    checkCommandInvalid(optflag, "0-3");
-//    checkCommandInvalid(optflag, "4");
-//    checkCommandInvalid(optflag, "xyz");
-//}
-//
-///// \brief Check four-bit field
-/////
-///// Explicitly for those fields in the flags word that are four bits wide,
-///// perform a series of tests to check that they accept valid values and
-///// reject invalid ones.
-/////
-///// \param index Option index
-///// \param optflag Option flag (in the form '--option')
-//void checkFourBitField(int index, const char* optflag) {
-//    checkCommandValid(index, optflag, "0", 0, 0);
-//    checkCommandValid(index, optflag, "15", 15, 15);
-//    checkCommandValid(index, optflag, "0-15", 0, 15);
-//    checkCommandValid(index, optflag, "15-0", 0, 15);
-//    checkCommandInvalid(optflag, "0-17");
-//    checkCommandInvalid(optflag, "24");
-//    checkCommandInvalid(optflag, "xyz");
-//}
-//
-///// \brief Check sixteen-bit field
-/////
-///// Explicitly test the parsing of the fields that can take a 16-bit
-///// value ranging from 0 to 65535.
-/////
-///// \param index Option index
-///// \param optflag Option flag (in the form '--option')
-//void checkSixteenBitField(int index, const char* optflag) {
-//    checkCommandValid(index, optflag, "0", 0, 0);
-//    checkCommandValid(index, optflag, "65535", 65535, 65535);
-//    checkCommandValid(index, optflag, "0-65535", 0, 65535);
-//    checkCommandValid(index, optflag, "65535-0", 0, 65535);
-//    checkCommandInvalid(optflag, "0-65536");
-//    checkCommandInvalid(optflag, "65537");
-//    checkCommandInvalid(optflag, "xyz");
-//}
-//
-//// Check that each of the non-message options will be recognised
-//
-//TEST(CommandOptionsTest, address) {
-//
-//    const char* argv[] = {"badpacket",  "--address", "192.0.2.1"};
-//    int argc = sizeof(argv) / sizeof(const char*);
-//
-//    // The conversion is ugly but it simplifies the process of entering the
-//    // string constant.  The cast throws away the "const"ness of the pointed-to
-//    // strings in order to conform to the function signature; however, the
-//    // called functions all treat the strings as const.
-//    parse(argc, const_cast<char**>(argv));
-//    EXPECT_STREQ("192.0.2.1", getAddress());
-//    EXPECT_EQ(53, getPort());
-//    EXPECT_EQ(500, getTimeout());
-//    EXPECT_STREQ("www.example.com", getQname());
-//    checkDefaultLimitsValues();
-//}
-
-//TEST(CommandOptionsTest, timeout) {
-//    const char* argv[] = {"badpacket",  "--timeout", "250"};
-//    int argc = sizeof(argv) / sizeof(const char*);
-//
-//    parse(argc, const_cast<char**>(argv));
-//    EXPECT_STREQ("127.0.0.1", getAddress());
-//    EXPECT_EQ(53, getPort());
-//    EXPECT_EQ(250, getTimeout());
-//    EXPECT_STREQ("www.example.com", getQname());
-//    checkDefaultLimitsValues();
-//}
-//
-//TEST(CommandOptionsTest, parameter) {
-//    const char* argv[] = {"badpacket",  "ftp.example.net"};
-//    int argc = sizeof(argv) / sizeof(const char*);
-//
-//    parse(argc, const_cast<char**>(argv));
-//    EXPECT_STREQ("127.0.0.1", getAddress());
-//    EXPECT_EQ(53, getPort());
-//    EXPECT_EQ(500, getTimeout());
-//    EXPECT_STREQ("ftp.example.net", getQname());
-//    checkDefaultLimitsValues();
-//}
-//
-//// Test options representing the flags fields.
-//
-//TEST(CommandOptionsTest, qr) {
-//    checkOneBitField(OptionInfo::QR, "--qr");
-//}
-//
-//TEST(CommandOptionsTest, op) {
-//    checkFourBitField(OptionInfo::OP, "--op");
-//}
-//
-//TEST(CommandOptionsTest, aa) {
-//    checkOneBitField(OptionInfo::AA, "--aa");
-//}
-//
-//TEST(CommandOptionsTest, tc) {
-//    checkOneBitField(OptionInfo::TC, "--tc");
-//}
-//
-//TEST(CommandOptionsTest, z) {
-//    checkOneBitField(OptionInfo::Z, "--z");
-//}
-//
-//TEST(CommandOptionsTest, ad) {
-//    checkOneBitField(OptionInfo::AD, "--ad");
-//}
-//
-//TEST(CommandOptionsTest, cd) {
-//    checkOneBitField(OptionInfo::CD, "--cd");
-//}
-//
-//TEST(CommandOptionsTest, rc) {
-//    checkFourBitField(OptionInfo::RC, "--rc");
-//}
-//
-//// Section count options
-//
-//TEST(CommandOptionsTest, qc) {
-//    checkSixteenBitField(OptionInfo::QC, "--qc");
-//}
-//
-//TEST(CommandOptionsTest, ac) {
-//    checkSixteenBitField(OptionInfo::AC, "--ac");
-//}
-//
-//TEST(CommandOptionsTest, uc) {
-//    checkSixteenBitField(OptionInfo::UC, "--uc");
-//}
-//
-//TEST(CommandOptionsTest, dc) {
-//    checkSixteenBitField(OptionInfo::DC, "--dc");
-//}
-//
-//// ... and the message size option
-//
-//TEST(CommandOptionsTest, ms) {
-//    int index = OptionInfo::MS;
-//    const char* optflag = "--ms";
-//
-//    checkCommandValid(index, optflag, "1", 1, 1);
-//    checkCommandValid(index, optflag, "65536", 65536, 65536);
-//    checkCommandValid(index, optflag, "1-65536", 1, 65536);
-//    checkCommandValid(index, optflag, "65536-1", 1, 65536);
-//    checkCommandInvalid(optflag, "0");
-//    checkCommandInvalid(optflag, "1-65537");
-//    checkCommandInvalid(optflag, "65538");
-//    checkCommandInvalid(optflag, "xyz");
-//}
+TEST(CommandOptionsTest, droptime) {
+    checkPosFloatOpt("-d");
+}
+
+TEST(CommandOptionsTest, period) {
+    checkPosFloatOpt("-p");
+}
+
+TEST(CommandOptionsTest, v4) {
+    checkFlag("-4");
+}
+
+TEST(CommandOptionsTest, v6) {
+    checkFlag("-6");
+}
+
+TEST(CommandOptionsTest, initial) {
+    checkFlag("-i");
+}
+
+TEST(CommandOptionsTest, help) {
+    checkFlagHandled("-h");
+}
+
+TEST(CommandOptionsTest, version) {
+    checkFlagHandled("-v");
+}
+
+TEST(CommandOptionsTest, localname) {
+    checkNEStrOpt("-l");
+}
+
+TEST(CommandOptionsTest, diagnostics) {
+    checkStrOpt("-x");
+}
+
+TEST(CommandOptionsTest, maxdrop) {
+    checkNEStrOpt("-D");
+}
+
+/*
+ * NOTE: GNU getopt() as of 2011-11-07 is subject to a bug that causes it to
+ * break if called with an invalid option followed by another call with optind
+ * re-initialiation.  The code in this module currently doesn't exercise the
+ * bug, but it is highly sensitive to run-time conditions (stack layout etc.)
+ */
+TEST(CommandOptionsTest, nosuchopt) {
+    checkOptionInvalid("-W", "");
+}

+ 0 - 0
tests/tools/perfdhcp/tests/packetdisp_unittest.cc