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.
35 * \brief Handling of ID allocation / freeing
39 static int find_id(nr_server_t *s)
44 if ((s->used < 0) || (s->used > 256)) return -RSE_INTERNAL;
47 * Ensure that the ID allocation is random.
51 for (i = 0; i < 256; i++) {
52 int offset = (i + lvalue) & 0xff;
54 if (!s->ids[offset]) return offset;
57 nr_strerror_printf("Out of IDs for server");
61 int nr_server_id_alloc(nr_server_t *s, RADIUS_PACKET *packet)
65 if (!s || !packet) return -RSE_INVAL;
68 if (new_id < 0) return -new_id;
70 s->ids[new_id] = packet;
72 packet->sockfd = s->sockfd;
73 packet->code = s->code;
81 int nr_server_id_free(nr_server_t *s, RADIUS_PACKET *packet)
83 if (!s || !packet) return -RSE_INVAL;
85 if ((packet->id < 0) || (packet->id > 255) || !s->ids[packet->id]) {
89 if (s->ids[packet->id] != packet) return -RSE_INTERNAL;
91 s->ids[packet->id] = NULL;
98 int nr_server_id_realloc(nr_server_t *s, RADIUS_PACKET *packet)
102 if (!s || !packet) return -RSE_INVAL;
104 if ((packet->id < 0) || (packet->id > 255) || !s->ids[packet->id]) {
108 if (s->ids[packet->id] != packet) return -RSE_INTERNAL;
111 if (new_id < 0) return new_id;
113 s->ids[packet->id] = NULL;
115 s->ids[packet->id] = packet;
121 int nr_server_init(nr_server_t *s, int code, const char *secret)
123 if (!s || !secret || !*secret ||
124 (code == 0) || (code > RS_MAX_PACKET_CODE)) {
128 memset(s, 0, sizeof(*s));
133 s->sizeof_secret = strlen(secret);
134 s->src.ss_family = AF_UNSPEC;
135 s->dst.ss_family = AF_UNSPEC;
141 int nr_server_close(const nr_server_t *s)
143 if (!s) return -RSE_INVAL;
145 if (s->used > 0) return -RSE_INUSE;
147 if (s->sockfd >= 0) evutil_closesocket(s->sockfd);
152 int nr_server_packet_alloc(const nr_server_t *s, RADIUS_PACKET **packet_p)
155 RADIUS_PACKET *packet;
157 if (!packet_p) return -RSE_INVAL;
159 packet = malloc(sizeof(*packet) + RS_MAX_PACKET_LEN);
160 if (!packet) return -RSE_NOMEM;
162 memset(packet, 0, sizeof(*packet));
165 packet->data = (uint8_t *)(packet + 1);
166 packet->sizeof_data = RS_MAX_PACKET_LEN;
172 rcode = nr_packet_init(packet, NULL, s->secret, s->code,
173 (uint8_t *)(packet + 1), RS_MAX_PACKET_LEN);