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