newvector should be a bool
[freeradius.git] / src / modules / proto_dhcp / dhcpclient.c
index e0311db..dad87a7 100644 (file)
@@ -43,12 +43,12 @@ static int success = 0;
 static int retries = 3;
 static float timeout = 5;
 
-static int server_port = 0;
+static uint16_t server_port = 0;
 static int packet_code = 0;
 static fr_ipaddr_t server_ipaddr;
 
 static fr_ipaddr_t client_ipaddr;
-static int client_port = 0;
+static uint16_t client_port = 0;
 
 static int sockfd;
 
@@ -63,7 +63,7 @@ static RADIUS_PACKET *reply = NULL;
 
 char const *dhcpclient_version = "dhcpclient version " RADIUSD_VERSION_STRING
 #ifdef RADIUSD_VERSION_COMMIT
-" (git #" RADIUSD_VERSION_COMMIT ")"
+" (git #" STRINGIFY(RADIUSD_VERSION_COMMIT) ")"
 #endif
 ", built on " __DATE__ " at " __TIME__;
 
@@ -89,8 +89,9 @@ static void NEVER_RETURNS usage(void)
 static int request_init(char const *filename)
 {
        FILE *fp;
+       vp_cursor_t cursor;
        VALUE_PAIR *vp;
-       int filedone = 0;
+       bool filedone = false;
 
        /*
         *      Determine where to read the VP's from.
@@ -99,20 +100,20 @@ static int request_init(char const *filename)
                fp = fopen(filename, "r");
                if (!fp) {
                        fprintf(stderr, "dhcpclient: Error opening %s: %s\n",
-                               filename, strerror(errno));
+                               filename, fr_syserror(errno));
                        return 0;
                }
        } else {
                fp = stdin;
        }
 
-       request = rad_alloc(NULL, 0);
+       request = rad_alloc(NULL, false);
 
        /*
         *      Read the VP's.
         */
-       request->vps = readvp2(NULL, fp, &filedone, "dhcpclient:");
-       if (!request->vps) {
+       if (readvp2(&request->vps, NULL, fp, &filedone) < 0) {
+               fr_perror("dhcpclient");
                rad_free(&request);
                if (fp != stdin) fclose(fp);
                return 1;
@@ -121,11 +122,11 @@ static int request_init(char const *filename)
        /*
         *      Fix / set various options
         */
-       for (vp = request->vps; vp != NULL; vp = vp->next) {
+       for (vp = fr_cursor_init(&cursor, &request->vps); vp; vp = fr_cursor_next(&cursor)) {
                switch (vp->da->attr) {
                default:
                        break;
-                       
+
                        /*
                         *      Allow it to set the packet type in
                         *      the attributes read from the file.
@@ -133,36 +134,36 @@ static int request_init(char const *filename)
                case PW_PACKET_TYPE:
                        request->code = vp->vp_integer;
                        break;
-                       
+
                case PW_PACKET_DST_PORT:
                        request->dst_port = (vp->vp_integer & 0xffff);
                        break;
-                       
+
                case PW_PACKET_DST_IP_ADDRESS:
                        request->dst_ipaddr.af = AF_INET;
                        request->dst_ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
                        break;
-                       
+
                case PW_PACKET_DST_IPV6_ADDRESS:
                        request->dst_ipaddr.af = AF_INET6;
                        request->dst_ipaddr.ipaddr.ip6addr = vp->vp_ipv6addr;
                        break;
-                       
+
                case PW_PACKET_SRC_PORT:
                        request->src_port = (vp->vp_integer & 0xffff);
                        break;
-                       
+
                case PW_PACKET_SRC_IP_ADDRESS:
                        request->src_ipaddr.af = AF_INET;
                        request->src_ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
                        break;
-                       
+
                case PW_PACKET_SRC_IPV6_ADDRESS:
                        request->src_ipaddr.af = AF_INET6;
                        request->src_ipaddr.ipaddr.ip6addr = vp->vp_ipv6addr;
                        break;
                } /* switch over the attribute */
-                       
+
        } /* loop over the VP's we read in */
 
        if (fp != stdin) fclose(fp);
@@ -205,7 +206,7 @@ static int dhcp_header_sizes[] = {
 static void print_hex(RADIUS_PACKET *packet)
 {
        int i, j;
-       const uint8_t *p, *a;
+       uint8_t const *p, *a;
 
        if (!packet->data) return;
 
@@ -222,7 +223,7 @@ static void print_hex(RADIUS_PACKET *packet)
                printf("%s = 0x", dhcp_header_names[i]);
                for (j = 0; j < dhcp_header_sizes[i]; j++) {
                        printf("%02x", p[j]);
-                       
+
                }
                printf("\n");
                p += dhcp_header_sizes[i];
@@ -243,16 +244,16 @@ static void print_hex(RADIUS_PACKET *packet)
 
                printf("%02x  %02x  ", p[0], p[1]);
                a = p + 2;
-               
+
                for (i = 0; i < p[1]; i++) {
                        if ((i > 0) && ((i & 0x0f) == 0x00))
                                printf("\t\t");
                        printf("%02x ", a[i]);
                        if ((i & 0x0f) == 0x0f) printf("\n");
                }
-               
+
                if ((p[1] & 0x0f) != 0x00) printf("\n");
-               
+
                p += p[1] + 2;
        }
        printf("\n----------------------------------------------------------------------\n");
@@ -339,8 +340,8 @@ int main(int argc, char **argv)
                        portname = NULL;
                }
 
-               if (ip_hton(hostname, AF_INET, &server_ipaddr) < 0) {
-                       fprintf(stderr, "dhcpclient: Failed to find IP address for host %s: %s\n", hostname, strerror(errno));
+               if (ip_hton(&server_ipaddr, AF_INET, hostname, false) < 0) {
+                       fprintf(stderr, "dhcpclient: Failed to find IP address for host %s: %s\n", hostname, fr_syserror(errno));
                        exit(1);
                }
 
@@ -421,16 +422,16 @@ int main(int argc, char **argv)
                exit(1);
        }
        if (fr_debug_flag) print_hex(request);
-       
+
        if (fr_dhcp_send(request) < 0) {
                fprintf(stderr, "dhcpclient: failed sending: %s\n",
-                       strerror(errno));
+                       fr_syserror(errno));
                exit(1);
        }
 
        reply = fr_dhcp_recv(sockfd);
        if (!reply) {
-               fprintf(stderr, "dhcpclient: no reply\n");
+               fprintf(stderr, "dhcpclient: Error receiving reply %s\n", fr_strerror());
                exit(1);
        }
        if (fr_debug_flag) print_hex(reply);