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>
32 * \brief Handling of ID allocation / freeing
36 static int find_id(nr_server_t *s)
41 if ((s->used < 0) || (s->used > 256)) return -NR_ERR_INTERNAL_FAILURE;
44 * Ensure that the ID allocation is random.
48 for (i = 0; i < 256; i++) {
49 int offset = (i + lvalue) & 0xff;
51 if (!s->ids[offset]) return offset;
54 nr_strerror_printf("Out of IDs for server");
58 int nr_server_id_alloc(nr_server_t *s, RADIUS_PACKET *packet)
62 if (!s || !packet) return -NR_ERR_INVALID_ARG;
65 if (new_id < 0) return -new_id;
67 s->ids[new_id] = packet;
69 packet->sockfd = s->sockfd;
70 packet->code = s->code;
78 int nr_server_id_free(nr_server_t *s, RADIUS_PACKET *packet)
80 if (!s || !packet) return -NR_ERR_INVALID_ARG;
82 if ((packet->id < 0) || (packet->id > 255) || !s->ids[packet->id]) {
83 return -NR_ERR_INVALID_ARG;
86 if (s->ids[packet->id] != packet) return -NR_ERR_INTERNAL_FAILURE;
88 s->ids[packet->id] = NULL;
95 int nr_server_id_realloc(nr_server_t *s, RADIUS_PACKET *packet)
99 if (!s || !packet) return -NR_ERR_INVALID_ARG;
101 if ((packet->id < 0) || (packet->id > 255) || !s->ids[packet->id]) {
102 return -NR_ERR_INVALID_ARG;
105 if (s->ids[packet->id] != packet) return -NR_ERR_INTERNAL_FAILURE;
108 if (new_id < 0) return new_id;
110 s->ids[packet->id] = NULL;
112 s->ids[packet->id] = packet;
118 int nr_server_init(nr_server_t *s, int code, const char *secret)
120 if (!s || !secret || !*secret ||
121 (code == 0) || (code > NR_MAX_PACKET_CODE)) {
122 return -NR_ERR_INVALID_ARG;
125 memset(s, 0, sizeof(*s));
130 s->sizeof_secret = strlen(secret);
131 s->src.ss_family = AF_UNSPEC;
132 s->dst.ss_family = AF_UNSPEC;
138 int nr_server_close(const nr_server_t *s)
140 if (!s) return -NR_ERR_INVALID_ARG;
142 if (s->used > 0) return -NR_ERR_IN_USE;
144 if (s->sockfd >= 0) close(s->sockfd);
149 int nr_server_packet_alloc(const nr_server_t *s, RADIUS_PACKET **packet_p)
152 RADIUS_PACKET *packet;
154 if (!packet_p) return -NR_ERR_INVALID_ARG;
156 packet = malloc(sizeof(*packet) + NR_MAX_PACKET_LEN);
157 if (!packet) return -NR_ERR_NO_MEM;
159 memset(packet, 0, sizeof(*packet));
162 packet->data = (uint8_t *)(packet + 1);
163 packet->sizeof_data = NR_MAX_PACKET_LEN;
169 rcode = nr_packet_init(packet, NULL, s->secret, s->code,
170 (uint8_t *)(packet + 1), NR_MAX_PACKET_LEN);