4ccd03248e2ce28581a96fe053d1f3b8086e643f
[libradsec.git] / lib / radius / id.c
1 /*
2 Copyright (c) 2011, Network RADIUS SARL
3 All rights reserved.
4
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.
15
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.
26  */
27
28 #include        "client.h"
29
30 #ifdef HAVE_UNISTD_H
31 #include        <unistd.h>
32 #endif
33
34 /** \file id.c
35  *  \brief Handling of ID allocation / freeing
36  *
37  */
38
39 static int find_id(nr_server_t *s)
40 {
41         int i;
42         uint32_t lvalue;
43
44         if ((s->used < 0) || (s->used > 256)) return -RSE_INTERNAL;
45
46         /*
47          *      Ensure that the ID allocation is random.
48          */
49         lvalue = nr_rand();
50
51         for (i = 0; i < 256; i++) {
52                 int offset = (i + lvalue) & 0xff;
53
54                 if (!s->ids[offset]) return offset;
55         }
56
57         nr_strerror_printf("Out of IDs for server");
58         return -1;
59 }
60
61 int nr_server_id_alloc(nr_server_t *s, RADIUS_PACKET *packet)
62 {
63         int new_id;
64
65         if (!s || !packet) return -RSE_INVAL;
66
67         new_id = find_id(s);
68         if (new_id < 0) return -new_id;
69
70         s->ids[new_id] = packet;
71         s->used++;
72         packet->sockfd = s->sockfd;
73         packet->code = s->code;
74         packet->src = s->src;
75         packet->dst = s->dst;
76         packet->id = new_id;
77
78         return 0;
79 }
80
81 int nr_server_id_free(nr_server_t *s, RADIUS_PACKET *packet)
82 {
83         if (!s || !packet) return -RSE_INVAL;
84
85         if ((packet->id < 0) || (packet->id > 255) || !s->ids[packet->id]) {
86                 return -RSE_INVAL;
87         }
88
89         if (s->ids[packet->id] != packet) return -RSE_INTERNAL;
90
91         s->ids[packet->id] = NULL;
92         s->used--;
93         packet->sockfd = -1;
94
95         return 0;
96 }
97
98 int nr_server_id_realloc(nr_server_t *s, RADIUS_PACKET *packet)
99 {
100         int new_id;
101
102         if (!s || !packet) return -RSE_INVAL;
103
104         if ((packet->id < 0) || (packet->id > 255) || !s->ids[packet->id]) {
105                 return -RSE_INVAL;
106         }
107
108         if (s->ids[packet->id] != packet) return -RSE_INTERNAL;
109
110         new_id = find_id(s);
111         if (new_id < 0) return new_id;
112
113         s->ids[packet->id] = NULL;
114         packet->id = new_id;
115         s->ids[packet->id] = packet;
116
117         return 0;
118 }
119
120
121 int nr_server_init(nr_server_t *s, int code, const char *secret)
122 {
123         if (!s || !secret || !*secret ||
124             (code == 0) || (code > RS_MAX_PACKET_CODE)) {
125                 return -RSE_INVAL;
126         }
127
128         memset(s, 0, sizeof(*s));
129
130         s->sockfd = -1;
131         s->code = code;
132         s->secret = secret;
133         s->sizeof_secret = strlen(secret);
134         s->src.ss_family = AF_UNSPEC;
135         s->dst.ss_family = AF_UNSPEC;
136
137         return 0;
138 }
139
140
141 int nr_server_close(const nr_server_t *s)
142 {
143         if (!s) return -RSE_INVAL;
144
145         if (s->used > 0) return -RSE_INUSE;
146
147         if (s->sockfd >= 0) evutil_closesocket(s->sockfd);
148
149         return 0;
150 }
151
152 int nr_server_packet_alloc(const nr_server_t *s, RADIUS_PACKET **packet_p)
153 {
154         int rcode;
155         RADIUS_PACKET *packet;
156
157         if (!packet_p) return -RSE_INVAL;
158
159         packet = malloc(sizeof(*packet) + RS_MAX_PACKET_LEN);
160         if (!packet) return -RSE_NOMEM;
161
162         memset(packet, 0, sizeof(*packet));
163
164         if (!s) {
165                 packet->data = (uint8_t *)(packet + 1);
166                 packet->sizeof_data = RS_MAX_PACKET_LEN;
167
168                 *packet_p = packet;
169                 return 0;
170         }
171
172         rcode = nr_packet_init(packet, NULL, s->secret, s->code,
173                                (uint8_t *)(packet + 1), RS_MAX_PACKET_LEN);
174         if (rcode < 0) {
175                 free(packet);
176                 return rcode;
177         }
178
179         *packet_p = packet;
180         return 0;
181 }