Delete trailing whitespace.
[freeradius.git] / src / main / radclient.c
index 8d0d585..fb8cd40 100644 (file)
 #include <freeradius-devel/ident.h>
 RCSID("$Id$")
 
-#include <freeradius-devel/autoconf.h>
-
-#include <stdio.h>
-#include <stdlib.h>
-
-#ifdef HAVE_UNISTD_H
-#      include <unistd.h>
-#endif
+#include <freeradius-devel/libradius.h>
+#include <freeradius-devel/conf.h>
+#include <freeradius-devel/radpaths.h>
 
-#include <string.h>
 #include <ctype.h>
-#include <netdb.h>
-
-#ifdef HAVE_SYS_SELECT_H
-#      include <sys/select.h>
-#endif
 
 #ifdef HAVE_GETOPT_H
 #      include <getopt.h>
@@ -48,11 +37,6 @@ RCSID("$Id$")
 
 #include <assert.h>
 
-#include <freeradius-devel/conf.h>
-#include <freeradius-devel/radpaths.h>
-#include <freeradius-devel/missing.h>
-#include <freeradius-devel/libradius.h>
-
 static int retries = 10;
 static float timeout = 3;
 static const char *secret = NULL;
@@ -66,6 +50,7 @@ static int packet_code = 0;
 static lrad_ipaddr_t server_ipaddr;
 static int resend_count = 1;
 static int done = 1;
+static int print_filename = 0;
 
 static lrad_ipaddr_t client_ipaddr;
 static int client_port = 0;
@@ -220,13 +205,13 @@ static radclient_t *radclient_init(const char *filename)
                 *      Keep a copy of the the User-Password attribute.
                 */
                if ((vp = pairfind(radclient->request->vps, PW_USER_PASSWORD)) != NULL) {
-                       strNcpy(radclient->password, vp->vp_strvalue,
+                       strlcpy(radclient->password, vp->vp_strvalue,
                                sizeof(radclient->password));
                        /*
                         *      Otherwise keep a copy of the CHAP-Password attribute.
                         */
                } else if ((vp = pairfind(radclient->request->vps, PW_CHAP_PASSWORD)) != NULL) {
-                       strNcpy(radclient->password, vp->vp_strvalue,
+                       strlcpy(radclient->password, vp->vp_strvalue,
                                sizeof(radclient->password));
                } else {
                        radclient->password[0] = '\0';
@@ -245,16 +230,16 @@ static radclient_t *radclient_init(const char *filename)
                                 *      the attributes read from the file.
                                 */
                        case PW_PACKET_TYPE:
-                               radclient->request->code = vp->lvalue;
+                               radclient->request->code = vp->vp_integer;
                                break;
 
                        case PW_PACKET_DST_PORT:
-                               radclient->request->dst_port = (vp->lvalue & 0xffff);
+                               radclient->request->dst_port = (vp->vp_integer & 0xffff);
                                break;
 
                        case PW_PACKET_DST_IP_ADDRESS:
                                radclient->request->dst_ipaddr.af = AF_INET;
-                               radclient->request->dst_ipaddr.ipaddr.ip4addr.s_addr = vp->lvalue;
+                               radclient->request->dst_ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
                                break;
 
                        case PW_PACKET_DST_IPV6_ADDRESS:
@@ -263,12 +248,12 @@ static radclient_t *radclient_init(const char *filename)
                                break;
 
                        case PW_PACKET_SRC_PORT:
-                               radclient->request->src_port = (vp->lvalue & 0xffff);
+                               radclient->request->src_port = (vp->vp_integer & 0xffff);
                                break;
 
                        case PW_PACKET_SRC_IP_ADDRESS:
                                radclient->request->src_ipaddr.af = AF_INET;
-                               radclient->request->src_ipaddr.ipaddr.ip4addr.s_addr = vp->lvalue;
+                               radclient->request->src_ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
                                break;
 
                        case PW_PACKET_SRC_IPV6_ADDRESS:
@@ -436,7 +421,7 @@ static void print_hex(RADIUS_PACKET *packet)
                printf("%02x", packet->data[i]);
        }
        printf("\n");
-       
+
        if (packet->data_len > 20) {
                int total;
                const uint8_t *ptr;
@@ -549,7 +534,7 @@ static int send_one_packet(radclient_t *radclient)
                        VALUE_PAIR *vp;
 
                        if ((vp = pairfind(radclient->request->vps, PW_USER_PASSWORD)) != NULL) {
-                               strNcpy(vp->vp_strvalue, radclient->password,
+                               strlcpy(vp->vp_strvalue, radclient->password,
                                        sizeof(vp->vp_strvalue));
                                vp->length = strlen(vp->vp_strvalue);
 
@@ -559,7 +544,7 @@ static int send_one_packet(radclient_t *radclient)
                           *           AND vp->length != 17
                           *           AND rad_chap_encode() != vp->vp_octets
                           */
-                               strNcpy(vp->vp_strvalue, radclient->password,
+                               strlcpy(vp->vp_strvalue, radclient->password,
                                        sizeof(vp->vp_strvalue));
                                vp->length = strlen(vp->vp_strvalue);
 
@@ -580,7 +565,7 @@ static int send_one_packet(radclient_t *radclient)
                if (!lrad_packet_list_insert(pl, &radclient->request)) {
                        assert(0 == 1);
                }
-               
+
 
        } else {                /* radclient->request->id >= 0 */
                time_t now = time(NULL);
@@ -613,7 +598,7 @@ static int send_one_packet(radclient_t *radclient)
                 */
                if (radclient->tries == retries) {
                        assert(radclient->request->id >= 0);
-                       
+
                        /*
                         *      Delete the request from the tree of
                         *      outstanding requests.
@@ -622,7 +607,7 @@ static int send_one_packet(radclient_t *radclient)
 
                        fprintf(stderr, "radclient: no response from server for ID %d socket %d\n", radclient->request->id, radclient->request->sockfd);
                        deallocate_id(radclient);
-                       
+
                        /*
                         *      Normally we mark it "done" when we've received
                         *      the response, but this is a special case.
@@ -717,6 +702,10 @@ static int recv_one_packet(int wait_time)
        }
 
        lrad_packet_list_yank(pl, radclient->request);
+       if (print_filename) printf("%s:%d %d\n",
+                                  radclient->filename,
+                                  radclient->packet_number,
+                                  reply->code);
        deallocate_id(radclient);
        radclient->reply = reply;
 
@@ -742,16 +731,16 @@ static int recv_one_packet(int wait_time)
        }
 
 packet_done:
-       rad_free(&radclient->reply);
-
        /*
         *      Once we've sent the packet as many times as requested,
         *      mark it done.
         */
-       if (radclient->resend == resend_count) {
+       if ((radclient->done) ||
+           (radclient->resend == resend_count)) {
                assert(lrad_packet_list_find(pl, radclient->request) == NULL);
                radclient->done = 1;
        }
+       rad_free(&radclient->reply);
 
        return 0;
 }
@@ -790,7 +779,7 @@ int main(int argc, char **argv)
                exit(1);
        }
 
-       while ((c = getopt(argc, argv, "46c:d:f:hi:n:p:qr:sS:t:vx")) != EOF) switch(c) {
+       while ((c = getopt(argc, argv, "46c:d:f:Fhi:n:p:qr:sS:t:vx")) != EOF) switch(c) {
                case '4':
                        force_af = AF_INET;
                        break;
@@ -808,6 +797,9 @@ int main(int argc, char **argv)
                case 'f':
                        rbtree_insert(filename_tree, optarg);
                        break;
+               case 'F':
+                       print_filename = 1;
+                       break;
                case 'i':       /* currently broken */
                        if (!isdigit((int) *optarg))
                                usage();
@@ -918,7 +910,7 @@ int main(int argc, char **argv)
                        if ((p - argv[1]) >= sizeof(buffer)) {
                                usage();
                        }
-                       
+
                        memcpy(buffer, argv[1] + 1, p - argv[1] - 1);
                        buffer[p - argv[1] - 1] = '\0';
 
@@ -1136,7 +1128,7 @@ int main(int argc, char **argv)
                                                tv.tv_sec = 0;
                                                tv.tv_usec = 1000000/persec;
                                        }
-                                       
+
                                        /*
                                         *      Sleep for milliseconds,
                                         *      portably.
@@ -1187,6 +1179,7 @@ int main(int argc, char **argv)
 
        rbtree_free(filename_tree);
        lrad_packet_list_free(pl);
+       dict_free();
 
        if (do_summary) {
                printf("\n\t   Total approved auths:  %d\n", totalapp);