2 Copyright (c) 2011, Network RADIUS SARL
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in the
11 documentation and/or other materials provided with the distribution.
12 * Neither the name of the <organization> nor the
13 names of its contributors may be used to endorse or promote products
14 derived from this software without specific prior written permission.
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <networkradius-devel/client.h>
31 * \brief Sample code to initialize a RADIUS packet and a response to it.
33 * This example initializes a packet, and then adds User-Name and
34 * User-Password to it. The resulting packet is then printed to the
37 * As a next step, it then creates the response, and prints that,
41 static const char *secret = "testing123";
42 static uint8_t request_buffer[RS_MAX_PACKET_LEN];
43 static uint8_t response_buffer[RS_MAX_PACKET_LEN];
44 static RADIUS_PACKET request, response;
46 int main(int argc, const char *argv[])
49 const char *user = "bob";
50 const char *password = "password";
52 rcode = nr_packet_init(&request, NULL, secret, PW_ACCESS_REQUEST,
53 request_buffer, sizeof(request_buffer));
56 fprintf(stderr, "Error :%s\n", nr_strerror(rcode));
60 if (argc > 1) user = argv[1];
61 if (argc > 2) password = argv[2];
63 rcode = nr_packet_attr_append(&request, NULL,
66 if (rcode < 0) goto error;
68 rcode = nr_packet_attr_append(&request, NULL,
71 if (rcode < 0) goto error;
74 * ALWAYS call nr_packet_sign() before sending the packet
77 rcode = nr_packet_sign(&request, NULL);
78 if (rcode < 0) goto error;
80 nr_packet_print_hex(&request);
82 rcode = nr_packet_init(&response, &request, secret, PW_ACCESS_ACCEPT,
83 response_buffer, sizeof(response_buffer));
84 if (rcode < 0) goto error;
86 rcode = nr_packet_attr_append(&response, &request,
89 if (rcode < 0) goto error;
91 rcode = nr_packet_attr_append(&response, &request,
92 RS_DA_TUNNEL_PASSWORD,
94 if (rcode < 0) goto error;
95 rcode = nr_packet_sign(&response, &request);
96 if (rcode < 0) goto error;
98 nr_packet_print_hex(&response);
101 * Check that the response is well-formed. The
102 * nr_packet_verify() function also calls nr_packet_ok().
103 * However, it is sometimes useful to separate "malformed
104 * packet" errors from "packet is not a response to a
107 rcode = nr_packet_ok(&response);
108 if (rcode < 0) goto error;
111 * Double-check the signature of the response.
113 rcode = nr_packet_verify(&response, &request);
114 if (rcode < 0) goto error;
116 rcode = nr_packet_decode(&response, &request);
117 if (rcode < 0) goto error;
119 nr_vp_fprintf_list(stdout, response.vps);
120 nr_vp_free(&response.vps);