Added send/recv CoA methods to the server.
[freeradius.git] / src / main / radclient.c
index f486508..5805455 100644 (file)
@@ -37,8 +37,9 @@ RCSID("$Id$")
 
 #include <assert.h>
 
-static int retries = 10;
-static float timeout = 3;
+static int success = 0;
+static int retries = 3;
+static float timeout = 5;
 static const char *secret = NULL;
 static int do_output = 1;
 static int totalapp = 0;
@@ -140,17 +141,17 @@ static void radclient_free(radclient_t *radclient)
 }
 
 /*
- *     Initialize a radclient data structure
+ *     Initialize a radclient data structure and add it to
+ *     the global linked list.
  */
-static radclient_t *radclient_init(const char *filename)
+static int radclient_init(const char *filename)
 {
        FILE *fp;
        VALUE_PAIR *vp;
-       radclient_t *start, *radclient, *prev = NULL;
+       radclient_t *radclient;
        int filedone = 0;
        int packet_number = 1;
 
-       start = NULL;
        assert(filename != NULL);
 
        /*
@@ -161,7 +162,7 @@ static radclient_t *radclient_init(const char *filename)
                if (!fp) {
                        fprintf(stderr, "radclient: Error opening %s: %s\n",
                                filename, strerror(errno));
-                       return NULL;
+                       return 0;
                }
        } else {
                fp = stdin;
@@ -178,16 +179,16 @@ static radclient_t *radclient_init(const char *filename)
                if (!radclient) {
                        perror("radclient: X");
                        if (fp != stdin) fclose(fp);
-                       return NULL; /* memory leak "start" */
+                       return 0;
                }
                memset(radclient, 0, sizeof(*radclient));
 
                radclient->request = rad_alloc(1);
                if (!radclient->request) {
                        fr_perror("radclient: Y");
-                       radclient_free(radclient);
+                       free(radclient);
                        if (fp != stdin) fclose(fp);
-                       return NULL; /* memory leak "start" */
+                       return 0;
                }
 
                radclient->filename = filename;
@@ -199,9 +200,10 @@ static radclient_t *radclient_init(const char *filename)
                 */
                radclient->request->vps = readvp2(fp, &filedone, "radclient:");
                if (!radclient->request->vps) {
-                       radclient_free(radclient);
+                       rad_free(&radclient->request);
+                       free(radclient);
                        if (fp != stdin) fclose(fp);
-                       return start; /* done: return the list */
+                       return 1;
                }
 
                /*
@@ -285,14 +287,21 @@ static radclient_t *radclient_init(const char *filename)
                        }
                } /* loop over the VP's we read in */
 
-               if (!start) {
-                       start = radclient;
-                       prev = start;
+               /*
+                *      Add it to the tail of the list.
+                */
+               if (!radclient_head) {
+                       assert(radclient_tail == NULL);
+                       radclient_head = radclient;
+                       radclient->prev = NULL;
                } else {
-                       prev->next = radclient;
-                       radclient->prev = prev;
-                       prev = radclient;
+                       assert(radclient_tail->next == NULL);
+                       radclient_tail->next = radclient;
+                       radclient->prev = radclient_tail;
                }
+               radclient_tail = radclient;
+               radclient->next = NULL;
+
        } while (!filedone); /* loop until the file is done. */
 
        if (fp != stdin) fclose(fp);
@@ -300,7 +309,7 @@ static radclient_t *radclient_init(const char *filename)
        /*
         *      And we're done.
         */
-       return start;
+       return 1;
 }
 
 
@@ -346,35 +355,16 @@ static int filename_cmp(const void *one, const void *two)
 static int filename_walk(void *context, void *data)
 {
        const char      *filename = data;
-       radclient_t     *radclient;
 
        context = context;      /* -Wunused */
 
        /*
-        *      Initialize the request we're about
-        *      to send.
+        *      Read request(s) from the file.
         */
-       radclient = radclient_init(filename);
-       if (!radclient) {
-               exit(1);
+       if (!radclient_init(filename)) {
+               return 1;       /* stop walking */
        }
 
-       if (!radclient_head) {
-               assert(radclient_tail == NULL);
-               radclient_head = radclient;
-       } else {
-               assert(radclient_tail->next == NULL);
-               radclient_tail->next = radclient;
-               radclient->prev = radclient_tail;
-       }
-
-       /*
-        *      We may have had a list of "radclient" structures
-        *      returned to us.
-        */
-       while (radclient->next) radclient = radclient->next;
-       radclient_tail = radclient;
-
        return 0;
 }
 
@@ -434,8 +424,7 @@ static void print_hex(RADIUS_PACKET *packet)
                ptr = packet->data + 20;
 
                while (total > 0) {
-                       int attrlen, vsa = 0;
-                       int mask = 16;
+                       int attrlen;
 
                        printf("\t\t");
                        if (total < 2) { /* too short */
@@ -450,64 +439,19 @@ static void print_hex(RADIUS_PACKET *packet)
                                break;
                        }
 
-                       if (ptr[0] == PW_VENDOR_SPECIFIC) vsa = 1;
-
                        printf("%02x  %02x  ", ptr[0], ptr[1]);
                        attrlen = ptr[1] - 2;
                        ptr += 2;
                        total -= 2;
 
-                       if (vsa && (attrlen > 4)) {
-                               int vendor, vsa_tlen, vsa_llen;
-                               DICT_VENDOR *dv;
-
-                               mask = 7;
-                               vendor = ((ptr[0] << 24) | (ptr[1] << 16) | 
-                                         (ptr[2] << 8) | ptr[3]);
-                               dv = dict_vendorbyvalue(vendor);
-                               vsa_tlen = vsa_llen = 1;
-                               if (dv) {
-                                       vsa_tlen = dv->type;
-                                       vsa_llen = dv->length;
-                               }
-
-                               printf("%02x%02x%02x%02x  ",
-                                      ptr[0], ptr[1], ptr[2], ptr[3]);
-                               ptr += 4;
-                               attrlen -= 4;
-                               total -= 4;                             
-
-                               if (attrlen >= (vsa_tlen + vsa_llen)) {
-                                       for (i = 0; i < vsa_tlen; i++) {
-                                               printf("%02x", ptr[i]);
-                                       }
-                                       printf("  ");
-                                       ptr += vsa_tlen;
-                                       attrlen -= vsa_tlen;
-                                       total -= vsa_tlen;
-
-                                       for (i = 0; i < vsa_llen; i++) {
-                                               printf("%02x", ptr[i]);
-                                       }
-                                       printf("  ");
-                                       ptr += vsa_llen;
-                                       attrlen -= vsa_llen;
-                                       total -= vsa_tlen;
-                               }
-
-                               printf("\n\t\t\t\t");
-                       }
-
                        for (i = 0; i < attrlen; i++) {
-                               if ((i > 0) && ((i & mask) == 0x00)) {
+                               if ((i > 0) && ((i & 0x0f) == 0x00))
                                        printf("\t\t\t");
-                                       if (mask == 7) printf("\t");
-                               }
                                printf("%02x ", ptr[i]);
-                               if ((i & mask) == mask) printf("\n");
+                               if ((i & 0x0f) == 0x0f) printf("\n");
                        }
 
-                       if ((attrlen & mask) != 0x00) printf("\n");
+                       if ((attrlen & 0x0f) != 0x00) printf("\n");
 
                        ptr += attrlen;
                        total -= attrlen;
@@ -680,7 +624,7 @@ static int send_one_packet(radclient_t *radclient)
         */
        if (rad_send(radclient->request, NULL, secret) < 0) {
                fprintf(stderr, "radclient: Failed to send packet for ID %d: %s\n",
-                       radclient->request->id, fr_strerror);
+                       radclient->request->id, fr_strerror());
        }
 
        if (fr_debug_flag > 2) print_hex(radclient->request);
@@ -725,7 +669,7 @@ static int recv_one_packet(int wait_time)
        reply = fr_packet_list_recv(pl, &set);
        if (!reply) {
                fprintf(stderr, "radclient: received bad packet: %s\n",
-                       fr_strerror);
+                       fr_strerror());
                return -1;      /* bad packet */
        }
 
@@ -763,11 +707,12 @@ static int recv_one_packet(int wait_time)
                                   reply->code);
        deallocate_id(radclient);
        radclient->reply = reply;
+       reply = NULL;
 
        /*
         *      If this fails, we're out of memory.
         */
-       if (rad_decode(reply, radclient->request, secret) != 0) {
+       if (rad_decode(radclient->reply, radclient->request, secret) != 0) {
                fr_perror("rad_decode");
                totallost++;
                goto packet_done;
@@ -776,10 +721,16 @@ static int recv_one_packet(int wait_time)
        /* libradius debug already prints out the value pairs for us */
        if (!fr_debug_flag && do_output) {
                printf("Received response ID %d, code %d, length = %d\n",
-                      reply->id, reply->code, reply->data_len);
-               vp_printlist(stdout, reply->vps);
+                      radclient->reply->id, radclient->reply->code,
+                      radclient->reply->data_len);
+               vp_printlist(stdout, radclient->reply->vps);
        }
-       if (reply->code != PW_AUTHENTICATION_REJECT) {
+
+       if ((radclient->reply->code == PW_AUTHENTICATION_ACK) ||
+           (radclient->reply->code == PW_ACCOUNTING_RESPONSE) ||
+           (radclient->reply->code == PW_COA_ACK) ||
+           (radclient->reply->code == PW_DISCONNECT_ACK)) {
+               success = 1;            /* have a good response */
                totalapp++;
        } else {
                totaldeny++;
@@ -791,6 +742,7 @@ static int recv_one_packet(int wait_time)
 
  packet_done:
        rad_free(&radclient->reply);
+       rad_free(&reply);       /* may be NULL */
 
        return 0;
 }
@@ -1014,11 +966,11 @@ int main(int argc, char **argv)
                packet_code = PW_STATUS_SERVER;
 
        } else if (strcmp(argv[2], "disconnect") == 0) {
-               if (server_port == 0) server_port = PW_POD_UDP_PORT;
+               if (server_port == 0) server_port = PW_COA_UDP_PORT;
                packet_code = PW_DISCONNECT_REQUEST;
 
        } else if (strcmp(argv[2], "coa") == 0) {
-               if (server_port == 0) server_port = PW_POD_UDP_PORT;
+               if (server_port == 0) server_port = PW_COA_UDP_PORT;
                packet_code = PW_COA_REQUEST;
 
        } else if (strcmp(argv[2], "auto") == 0) {
@@ -1073,7 +1025,7 @@ int main(int argc, char **argv)
        }
        sockfd = fr_socket(&client_ipaddr, client_port);
        if (sockfd < 0) {
-               fprintf(stderr, "radclient: socket: %s\n", fr_strerror);
+               fprintf(stderr, "radclient: socket: %s\n", fr_strerror());
                exit(1);
        }
 
@@ -1231,6 +1183,7 @@ int main(int argc, char **argv)
 
        rbtree_free(filename_tree);
        fr_packet_list_free(pl);
+       while (radclient_head) radclient_free(radclient_head);
        dict_free();
 
        if (do_summary) {
@@ -1239,5 +1192,7 @@ int main(int argc, char **argv)
                printf("\t       Total lost auths:  %d\n", totallost);
        }
 
-       return 0;
+       if (success) return 0;
+
+       return 1;
 }