Allow src_ipaddr to be specified for home servers
[freeradius.git] / src / lib / packet.c
1 /*
2  * packet.c     Generic packet manipulation functions.
3  *
4  * Version:     $Id$
5  *
6  *   This library is free software; you can redistribute it and/or
7  *   modify it under the terms of the GNU Lesser General Public
8  *   License as published by the Free Software Foundation; either
9  *   version 2.1 of the License, or (at your option) any later version.
10  *
11  *   This library is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  *   Lesser General Public License for more details.
15  *
16  *   You should have received a copy of the GNU Lesser General Public
17  *   License along with this library; if not, write to the Free Software
18  *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  * Copyright 2000-2006  The FreeRADIUS server project
21  */
22
23 #include        <freeradius-devel/ident.h>
24 RCSID("$Id$")
25
26 #include        <freeradius-devel/libradius.h>
27
28 #ifdef WITH_UDPFROMTO
29 #include        <freeradius-devel/udpfromto.h>
30 #endif
31
32 /*
33  *      Take the key fields of a request packet, and convert it to a
34  *      hash.
35  */
36 uint32_t fr_request_packet_hash(const RADIUS_PACKET *packet)
37 {
38         uint32_t hash;
39
40         if (packet->hash) return packet->hash;
41
42         hash = fr_hash(&packet->sockfd, sizeof(packet->sockfd));
43         hash = fr_hash_update(&packet->src_port, sizeof(packet->src_port),
44                                 hash);
45         hash = fr_hash_update(&packet->dst_port,
46                                 sizeof(packet->dst_port), hash);
47         hash = fr_hash_update(&packet->src_ipaddr.af,
48                                 sizeof(packet->src_ipaddr.af), hash);
49
50         /*
51          *      The caller ensures that src & dst AF are the same.
52          */
53         switch (packet->src_ipaddr.af) {
54         case AF_INET:
55                 hash = fr_hash_update(&packet->src_ipaddr.ipaddr.ip4addr,
56                                         sizeof(packet->src_ipaddr.ipaddr.ip4addr),
57                                         hash);
58                 hash = fr_hash_update(&packet->dst_ipaddr.ipaddr.ip4addr,
59                                         sizeof(packet->dst_ipaddr.ipaddr.ip4addr),
60                                         hash);
61                 break;
62         case AF_INET6:
63                 hash = fr_hash_update(&packet->src_ipaddr.ipaddr.ip6addr,
64                                         sizeof(packet->src_ipaddr.ipaddr.ip6addr),
65                                         hash);
66                 hash = fr_hash_update(&packet->dst_ipaddr.ipaddr.ip6addr,
67                                         sizeof(packet->dst_ipaddr.ipaddr.ip6addr),
68                                         hash);
69                 break;
70         default:
71                 break;
72         }
73
74         return fr_hash_update(&packet->id, sizeof(packet->id), hash);
75 }
76
77
78 /*
79  *      Take the key fields of a reply packet, and convert it to a
80  *      hash.
81  *
82  *      i.e. take a reply packet, and find the hash of the request packet
83  *      that asked for the reply.  To do this, we hash the reverse fields
84  *      of the request.  e.g. where the request does (src, dst), we do
85  *      (dst, src)
86  */
87 uint32_t fr_reply_packet_hash(const RADIUS_PACKET *packet)
88 {
89         uint32_t hash;
90
91         hash = fr_hash(&packet->sockfd, sizeof(packet->sockfd));
92         hash = fr_hash_update(&packet->id, sizeof(packet->id), hash);
93         hash = fr_hash_update(&packet->src_port, sizeof(packet->src_port),
94                                 hash);
95         hash = fr_hash_update(&packet->dst_port,
96                                 sizeof(packet->dst_port), hash);
97         hash = fr_hash_update(&packet->src_ipaddr.af,
98                                 sizeof(packet->src_ipaddr.af), hash);
99
100         /*
101          *      The caller ensures that src & dst AF are the same.
102          */
103         switch (packet->src_ipaddr.af) {
104         case AF_INET:
105                 hash = fr_hash_update(&packet->dst_ipaddr.ipaddr.ip4addr,
106                                         sizeof(packet->dst_ipaddr.ipaddr.ip4addr),
107                                         hash);
108                 hash = fr_hash_update(&packet->src_ipaddr.ipaddr.ip4addr,
109                                         sizeof(packet->src_ipaddr.ipaddr.ip4addr),
110                                         hash);
111                 break;
112         case AF_INET6:
113                 hash = fr_hash_update(&packet->dst_ipaddr.ipaddr.ip6addr,
114                                         sizeof(packet->dst_ipaddr.ipaddr.ip6addr),
115                                         hash);
116                 hash = fr_hash_update(&packet->src_ipaddr.ipaddr.ip6addr,
117                                         sizeof(packet->src_ipaddr.ipaddr.ip6addr),
118                                         hash);
119                 break;
120         default:
121                 break;
122         }
123
124         return fr_hash_update(&packet->id, sizeof(packet->id), hash);
125 }
126
127
128 /*
129  *      See if two packets are identical.
130  *
131  *      Note that we do NOT compare the authentication vectors.
132  *      That's because if the authentication vector is different,
133  *      it means that the NAS has given up on the earlier request.
134  */
135 int fr_packet_cmp(const RADIUS_PACKET *a, const RADIUS_PACKET *b)
136 {
137         int rcode;
138
139         if (a->sockfd < b->sockfd) return -1;
140         if (a->sockfd > b->sockfd) return +1;
141
142         if (a->id < b->id) return -1;
143         if (a->id > b->id) return +1;
144
145         if (a->src_port < b->src_port) return -1;
146         if (a->src_port > b->src_port) return +1;
147
148         if (a->dst_port < b->dst_port) return -1;
149         if (a->dst_port > b->dst_port) return +1;
150
151         rcode = fr_ipaddr_cmp(&a->dst_ipaddr, &b->dst_ipaddr);
152         if (rcode != 0) return rcode;
153         return fr_ipaddr_cmp(&a->src_ipaddr, &b->src_ipaddr);
154 }
155
156
157 /*
158  *      Create a fake "request" from a reply, for later lookup.
159  */
160 void fr_request_from_reply(RADIUS_PACKET *request,
161                              const RADIUS_PACKET *reply)
162 {
163         request->sockfd = reply->sockfd;
164         request->id = reply->id;
165         request->src_port = reply->dst_port;
166         request->dst_port = reply->src_port;
167         request->src_ipaddr = reply->dst_ipaddr;
168         request->dst_ipaddr = reply->src_ipaddr;
169 }
170
171
172 /*
173  *      Open a socket on the given IP and port.
174  */
175 int fr_socket(fr_ipaddr_t *ipaddr, int port)
176 {
177         int sockfd;
178         struct sockaddr_storage salocal;
179         socklen_t       salen;
180
181         if ((port < 0) || (port > 65535)) {
182                 fr_strerror_printf("Port %d is out of allowed bounds", port);
183                 return -1;
184         }
185
186         sockfd = socket(ipaddr->af, SOCK_DGRAM, 0);
187         if (sockfd < 0) {
188                 fr_strerror_printf("cannot open socket: %s", strerror(errno));
189                 return sockfd;
190         }
191
192 #ifdef WITH_UDPFROMTO
193         /*
194          *      Initialize udpfromto for all sockets.
195          */
196         if (udpfromto_init(sockfd) != 0) {
197                 close(sockfd);
198                 fr_strerror_printf("cannot initialize udpfromto: %s", strerror(errno));
199                 return -1;
200         }
201 #endif
202
203
204         if (!fr_ipaddr2sockaddr(ipaddr, port, &salocal, &salen)) {
205                 return sockfd;
206         }
207
208 #ifdef HAVE_STRUCT_SOCKADDR_IN6
209         if (ipaddr->af == AF_INET6) {
210                 /*
211                  *      Listening on '::' does NOT get you IPv4 to
212                  *      IPv6 mapping.  You've got to listen on an IPv4
213                  *      address, too.  This makes the rest of the server
214                  *      design a little simpler.
215                  */
216 #ifdef IPV6_V6ONLY
217
218                 if (IN6_IS_ADDR_UNSPECIFIED(&ipaddr->ipaddr.ip6addr)) {
219                         int on = 1;
220
221                         setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY,
222                                    (char *)&on, sizeof(on));
223                 }
224 #endif /* IPV6_V6ONLY */
225         }
226 #endif /* HAVE_STRUCT_SOCKADDR_IN6 */
227
228         if (bind(sockfd, (struct sockaddr *) &salocal, salen) < 0) {
229                 close(sockfd);
230                 fr_strerror_printf("cannot bind socket: %s", strerror(errno));
231                 return -1;
232         }
233
234         return sockfd;
235 }
236
237
238 /*
239  *      We need to keep track of the socket & it's IP/port.
240  */
241 typedef struct fr_packet_socket_t {
242         int             sockfd;
243
244         int             num_outgoing;
245
246         int             offset; /* 0..31 */
247         int             inaddr_any;
248         fr_ipaddr_t     ipaddr;
249         int             port;
250 } fr_packet_socket_t;
251
252
253 #define FNV_MAGIC_PRIME (0x01000193)
254 #define MAX_SOCKETS (32)
255 #define SOCKOFFSET_MASK (MAX_SOCKETS - 1)
256 #define SOCK2OFFSET(sockfd) ((sockfd * FNV_MAGIC_PRIME) & SOCKOFFSET_MASK)
257
258 #define MAX_QUEUES (8)
259
260 /*
261  *      Structure defining a list of packets (incoming or outgoing)
262  *      that should be managed.
263  */
264 struct fr_packet_list_t {
265         fr_hash_table_t *ht;
266
267         fr_hash_table_t *dst2id_ht;
268
269         int             alloc_id;
270         int             num_outgoing;
271
272         uint32_t mask;
273         int last_recv;
274         fr_packet_socket_t sockets[MAX_SOCKETS];
275 };
276
277
278 /*
279  *      Ugh.  Doing this on every sent/received packet is not nice.
280  */
281 static fr_packet_socket_t *fr_socket_find(fr_packet_list_t *pl,
282                                              int sockfd)
283 {
284         int i, start;
285
286         i = start = SOCK2OFFSET(sockfd);
287
288         do {                    /* make this hack slightly more efficient */
289                 if (pl->sockets[i].sockfd == sockfd) return &pl->sockets[i];
290
291                 i = (i + 1) & SOCKOFFSET_MASK;
292         } while (i != start);
293
294         return NULL;
295 }
296
297 int fr_packet_list_socket_remove(fr_packet_list_t *pl, int sockfd)
298 {
299         fr_packet_socket_t *ps;
300
301         if (!pl) return 0;
302
303         ps = fr_socket_find(pl, sockfd);
304         if (!ps) return 0;
305
306         /*
307          *      FIXME: Allow the caller forcibly discard these?
308          */
309         if (ps->num_outgoing != 0) return 0;
310
311         ps->sockfd = -1;
312         pl->mask &= ~(1 << ps->offset);
313
314
315         return 1;
316 }
317
318 int fr_packet_list_socket_add(fr_packet_list_t *pl, int sockfd)
319 {
320         int i, start;
321         struct sockaddr_storage src;
322         socklen_t               sizeof_src = sizeof(src);
323         fr_packet_socket_t      *ps;
324
325         if (!pl) return 0;
326
327         ps = NULL;
328         i = start = SOCK2OFFSET(sockfd);
329
330         do {
331                 if (pl->sockets[i].sockfd == -1) {
332                         ps =  &pl->sockets[i];
333                         start = i;
334                         break;
335                 }
336
337                 i = (i + 1) & SOCKOFFSET_MASK;
338         } while (i != start);
339
340         if (!ps) {
341                 return 0;
342         }
343
344         memset(ps, 0, sizeof(*ps));
345         ps->sockfd = sockfd;
346         ps->offset = start;
347
348         /*
349          *      Get address family, etc. first, so we know if we
350          *      need to do udpfromto.
351          *
352          *      FIXME: udpfromto also does this, but it's not
353          *      a critical problem.
354          */
355         memset(&src, 0, sizeof_src);
356         if (getsockname(sockfd, (struct sockaddr *) &src,
357                         &sizeof_src) < 0) {
358                 return 0;
359         }
360
361         if (!fr_sockaddr2ipaddr(&src, sizeof_src, &ps->ipaddr, &ps->port)) {
362                 return 0;
363         }
364
365         /*
366          *      Grab IP addresses & ports from the sockaddr.
367          */
368         if (src.ss_family == AF_INET) {
369                 if (ps->ipaddr.ipaddr.ip4addr.s_addr == INADDR_ANY) {
370                         ps->inaddr_any = 1;
371                 }
372
373 #ifdef HAVE_STRUCT_SOCKADDR_IN6
374         } else if (src.ss_family == AF_INET6) {
375                 if (IN6_IS_ADDR_UNSPECIFIED(&ps->ipaddr.ipaddr.ip6addr)) {
376                         ps->inaddr_any = 1;
377                 }
378 #endif
379         } else {
380                 return 0;
381         }
382
383         pl->mask |= (1 << ps->offset);
384         return 1;
385 }
386
387 static uint32_t packet_entry_hash(const void *data)
388 {
389         return fr_request_packet_hash(*(const RADIUS_PACKET * const *) data);
390 }
391
392 static int packet_entry_cmp(const void *one, const void *two)
393 {
394         const RADIUS_PACKET * const *a = one;
395         const RADIUS_PACKET * const *b = two;
396
397         return fr_packet_cmp(*a, *b);
398 }
399
400 /*
401  *      A particular socket can have 256 RADIUS ID's outstanding to
402  *      any one destination IP/port.  So we have a structure that
403  *      manages destination IP & port, and has an array of 256 ID's.
404  *
405  *      The only magic here is that we map the socket number (0..256)
406  *      into an "internal" socket number 0..31, that we use to set
407  *      bits in the ID array.  If a bit is 1, then that ID is in use
408  *      for that socket, and the request MUST be in the packet hash!
409  *
410  *      Note that as a minor memory leak, we don't have an API to free
411  *      this structure, except when we discard the whole packet list.
412  *      This means that if destinations are added and removed, they
413  *      won't be removed from this tree.
414  */
415 typedef struct fr_packet_dst2id_t {
416         fr_ipaddr_t     dst_ipaddr;
417         int             dst_port;
418         uint32_t        id[1];  /* really id[256] */
419 } fr_packet_dst2id_t;
420
421
422 static uint32_t packet_dst2id_hash(const void *data)
423 {
424         uint32_t hash;
425         const fr_packet_dst2id_t *pd = data;
426
427         hash = fr_hash(&pd->dst_port, sizeof(pd->dst_port));
428
429         switch (pd->dst_ipaddr.af) {
430         case AF_INET:
431                 hash = fr_hash_update(&pd->dst_ipaddr.ipaddr.ip4addr,
432                                         sizeof(pd->dst_ipaddr.ipaddr.ip4addr),
433                                         hash);
434                 break;
435         case AF_INET6:
436                 hash = fr_hash_update(&pd->dst_ipaddr.ipaddr.ip6addr,
437                                         sizeof(pd->dst_ipaddr.ipaddr.ip6addr),
438                                         hash);
439                 break;
440         default:
441                 break;
442         }
443
444         return hash;
445 }
446
447 static int packet_dst2id_cmp(const void *one, const void *two)
448 {
449         const fr_packet_dst2id_t *a = one;
450         const fr_packet_dst2id_t *b = two;
451
452         if (a->dst_port < b->dst_port) return -1;
453         if (a->dst_port > b->dst_port) return +1;
454
455         return fr_ipaddr_cmp(&a->dst_ipaddr, &b->dst_ipaddr);
456 }
457
458 static void packet_dst2id_free(void *data)
459 {
460         free(data);
461 }
462
463
464 void fr_packet_list_free(fr_packet_list_t *pl)
465 {
466         if (!pl) return;
467
468         fr_hash_table_free(pl->ht);
469         fr_hash_table_free(pl->dst2id_ht);
470         free(pl);
471 }
472
473
474 /*
475  *      Caller is responsible for managing the packet entries.
476  */
477 fr_packet_list_t *fr_packet_list_create(int alloc_id)
478 {
479         int i;
480         fr_packet_list_t        *pl;
481
482         pl = malloc(sizeof(*pl));
483         if (!pl) return NULL;
484         memset(pl, 0, sizeof(*pl));
485
486         pl->ht = fr_hash_table_create(packet_entry_hash,
487                                         packet_entry_cmp,
488                                         NULL);
489         if (!pl->ht) {
490                 fr_packet_list_free(pl);
491                 return NULL;
492         }
493
494         for (i = 0; i < MAX_SOCKETS; i++) {
495                 pl->sockets[i].sockfd = -1;
496         }
497
498         if (alloc_id) {
499                 pl->alloc_id = 1;
500
501                 pl->dst2id_ht = fr_hash_table_create(packet_dst2id_hash,
502                                                        packet_dst2id_cmp,
503                                                        packet_dst2id_free);
504                 if (!pl->dst2id_ht) {
505                         fr_packet_list_free(pl);
506                         return NULL;
507                 }
508         }
509
510         return pl;
511 }
512
513
514 /*
515  *      If pl->alloc_id is set, then fr_packet_list_id_alloc() MUST
516  *      be called before inserting the packet into the list!
517  */
518 int fr_packet_list_insert(fr_packet_list_t *pl,
519                             RADIUS_PACKET **request_p)
520 {
521         if (!pl || !request_p || !*request_p) return 0;
522
523         (*request_p)->hash = fr_request_packet_hash(*request_p);
524
525         return fr_hash_table_insert(pl->ht, request_p);
526 }
527
528 RADIUS_PACKET **fr_packet_list_find(fr_packet_list_t *pl,
529                                       RADIUS_PACKET *request)
530 {
531         if (!pl || !request) return 0;
532
533         return fr_hash_table_finddata(pl->ht, &request);
534 }
535
536
537 /*
538  *      This presumes that the reply has dst_ipaddr && dst_port set up
539  *      correctly (i.e. real IP, or "*").
540  */
541 RADIUS_PACKET **fr_packet_list_find_byreply(fr_packet_list_t *pl,
542                                               RADIUS_PACKET *reply)
543 {
544         RADIUS_PACKET my_request, *request;
545         fr_packet_socket_t *ps;
546
547         if (!pl || !reply) return NULL;
548
549         ps = fr_socket_find(pl, reply->sockfd);
550         if (!ps) return NULL;
551
552         /*
553          *      Initialize request from reply, AND from the source
554          *      IP & port of this socket.  The client may have bound
555          *      the socket to 0, in which case it's some random port,
556          *      that is NOT in the original request->src_port.
557          */
558         my_request.sockfd = reply->sockfd;
559         my_request.id = reply->id;
560
561         if (ps->inaddr_any) {
562                 my_request.src_ipaddr = ps->ipaddr;
563         } else {
564                 my_request.src_ipaddr = reply->dst_ipaddr;
565         }
566         my_request.src_port = ps->port;;
567
568         my_request.dst_ipaddr = reply->src_ipaddr;
569         my_request.dst_port = reply->src_port;
570         my_request.hash = 0;
571
572         request = &my_request;
573
574         return fr_hash_table_finddata(pl->ht, &request);
575 }
576
577
578 RADIUS_PACKET **fr_packet_list_yank(fr_packet_list_t *pl,
579                                       RADIUS_PACKET *request)
580 {
581         if (!pl || !request) return NULL;
582
583         return fr_hash_table_yank(pl->ht, &request);
584 }
585
586 int fr_packet_list_num_elements(fr_packet_list_t *pl)
587 {
588         if (!pl) return 0;
589
590         return fr_hash_table_num_elements(pl->ht);
591 }
592
593
594 /*
595  *      1 == ID was allocated & assigned
596  *      0 == error allocating memory
597  *      -1 == all ID's are used, caller should open a new socket.
598  *
599  *      Note that this ALSO assigns a socket to use, and updates
600  *      packet->request->src_ipaddr && packet->request->src_port
601  *
602  *      In multi-threaded systems, the calls to id_alloc && id_free
603  *      should be protected by a mutex.  This does NOT have to be
604  *      the same mutex as the one protecting the insert/find/yank
605  *      calls!
606  */
607 int fr_packet_list_id_alloc(fr_packet_list_t *pl,
608                               RADIUS_PACKET *request)
609 {
610         int i, id, start, fd;
611         uint32_t free_mask;
612         fr_packet_dst2id_t my_pd, *pd;
613         fr_packet_socket_t *ps;
614
615         if (!pl || !pl->alloc_id || !request) return 0;
616
617         my_pd.dst_ipaddr = request->dst_ipaddr;
618         my_pd.dst_port = request->dst_port;
619
620         pd = fr_hash_table_finddata(pl->dst2id_ht, &my_pd);
621         if (!pd) {
622                 pd = malloc(sizeof(*pd) + 255 * sizeof(pd->id[0]));
623                 if (!pd) return 0;
624
625                 memset(pd, 0, sizeof(*pd) + 255 * sizeof(pd->id[0]));
626
627                 pd->dst_ipaddr = request->dst_ipaddr;
628                 pd->dst_port = request->dst_port;
629
630                 if (!fr_hash_table_insert(pl->dst2id_ht, pd)) {
631                         free(pd);
632                         return 0;
633                 }
634         }
635
636         /*
637          *      FIXME: Go to an LRU system.  This prevents ID re-use
638          *      for as long as possible.  The main problem with that
639          *      approach is that it requires us to populate the
640          *      LRU/FIFO when we add a new socket, or a new destination,
641          *      which can be expensive.
642          *
643          *      The LRU can be avoided if the caller takes care to free
644          *      Id's only when all responses have been received, OR after
645          *      a timeout.
646          */
647         id = start = (int) fr_rand() & 0xff;
648
649         while (pd->id[id] == pl->mask) { /* all sockets are using this ID */
650         redo:
651                 id++;
652                 id &= 0xff;
653                 if (id == start) return 0;
654         }
655
656         free_mask = ~((~pd->id[id]) & pl->mask);
657
658         /*
659          *      This ID has at least one socket free.  Check the sockets
660          *      to see if they are satisfactory for the caller.
661          */
662         fd = -1;
663         for (i = 0; i < MAX_SOCKETS; i++) {
664                 if (pl->sockets[i].sockfd == -1) continue; /* paranoia */
665
666                 /*
667                  *      This ID is allocated.
668                  */
669                 if ((free_mask & (1 << i)) != 0) continue;
670                 
671                 /*
672                  *      If the caller cares about the source address,
673                  *      try to re-use that.  This means that the
674                  *      requested source address is set, AND this
675                  *      socket wasn't bound to "*", AND the requested
676                  *      source address is the same as this socket
677                  *      address.
678                  */
679                 if ((request->src_ipaddr.af != AF_UNSPEC) &&
680                     !pl->sockets[i].inaddr_any &&
681                     (fr_ipaddr_cmp(&request->src_ipaddr, &pl->sockets[i].ipaddr) != 0)) continue;
682
683                 /*
684                  *      They asked for a specific address, and this socket
685                  *      is bound to a wildcard address.  Ignore this one, too.
686                  */
687                 if ((request->src_ipaddr.af != AF_UNSPEC) &&
688                     pl->sockets[i].inaddr_any) continue;
689                 
690                 fd = i;
691                 break;
692         }
693
694         if (fd < 0) {
695                 goto redo; /* keep searching IDs */
696         }
697
698         pd->id[id] |= (1 << fd);
699         ps = &pl->sockets[fd];
700
701         ps->num_outgoing++;
702         pl->num_outgoing++;
703
704         /*
705          *      Set the ID, source IP, and source port.
706          */
707         request->id = id;
708
709         request->sockfd = ps->sockfd;
710         request->src_ipaddr = ps->ipaddr;
711         request->src_port = ps->port;
712
713         return 1;
714 }
715
716 /*
717  *      Should be called AFTER yanking it from the list, so that
718  *      any newly inserted entries don't collide with this one.
719  */
720 int fr_packet_list_id_free(fr_packet_list_t *pl,
721                              RADIUS_PACKET *request)
722 {
723         fr_packet_socket_t *ps;
724         fr_packet_dst2id_t my_pd, *pd;
725
726         if (!pl || !request) return 0;
727
728         ps = fr_socket_find(pl, request->sockfd);
729         if (!ps) return 0;
730
731         my_pd.dst_ipaddr = request->dst_ipaddr;
732         my_pd.dst_port = request->dst_port;
733
734         pd = fr_hash_table_finddata(pl->dst2id_ht, &my_pd);
735         if (!pd) return 0;
736
737         pd->id[request->id] &= ~(1 << ps->offset);
738         request->hash = 0;      /* invalidate the cached hash */
739
740         ps->num_outgoing--;
741         pl->num_outgoing--;
742
743         return 1;
744 }
745
746 int fr_packet_list_walk(fr_packet_list_t *pl, void *ctx,
747                           fr_hash_table_walk_t callback)
748 {
749         if (!pl || !callback) return 0;
750
751         return fr_hash_table_walk(pl->ht, callback, ctx);
752 }
753
754 int fr_packet_list_fd_set(fr_packet_list_t *pl, fd_set *set)
755 {
756         int i, maxfd;
757
758         if (!pl || !set) return 0;
759
760         maxfd = -1;
761
762         for (i = 0; i < MAX_SOCKETS; i++) {
763                 if (pl->sockets[i].sockfd == -1) continue;
764                 FD_SET(pl->sockets[i].sockfd, set);
765                 if (pl->sockets[i].sockfd > maxfd) {
766                         maxfd = pl->sockets[i].sockfd;
767                 }
768         }
769
770         if (maxfd < 0) return -1;
771
772         return maxfd + 1;
773 }
774
775 /*
776  *      Round-robins the receivers, without priority.
777  *
778  *      FIXME: Add sockfd, if -1, do round-robin, else do sockfd
779  *              IF in fdset.
780  */
781 RADIUS_PACKET *fr_packet_list_recv(fr_packet_list_t *pl, fd_set *set)
782 {
783         int start;
784         RADIUS_PACKET *packet;
785
786         if (!pl || !set) return NULL;
787
788         start = pl->last_recv;
789         do {
790                 start++;
791                 start &= SOCKOFFSET_MASK;
792
793                 if (pl->sockets[start].sockfd == -1) continue;
794
795                 if (!FD_ISSET(pl->sockets[start].sockfd, set)) continue;
796
797                 packet = rad_recv(pl->sockets[start].sockfd, 0);
798                 if (!packet) continue;
799
800                 /*
801                  *      Call fr_packet_list_find_byreply().  If it
802                  *      doesn't find anything, discard the reply.
803                  */
804
805                 pl->last_recv = start;
806                 return packet;
807         } while (start != pl->last_recv);
808
809         return NULL;
810 }
811
812 int fr_packet_list_num_incoming(fr_packet_list_t *pl)
813 {
814         int num_elements;
815
816         if (!pl) return 0;
817
818         num_elements = fr_hash_table_num_elements(pl->ht);
819         if (num_elements < pl->num_outgoing) return 0; /* panic! */
820
821         return num_elements - pl->num_outgoing;
822 }
823
824 int fr_packet_list_num_outgoing(fr_packet_list_t *pl)
825 {
826         if (!pl) return 0;
827
828         return pl->num_outgoing;
829 }