1 /* Copyright 2011,2013, NORDUnet A/S. All rights reserved. */
2 /* See LICENSE for licensing information. */
9 #include <event2/event.h>
10 #include <sys/socket.h>
11 #include <netinet/in.h>
12 #include <sys/types.h>
14 #include <sys/select.h>
16 #include "radius/client.h"
19 static struct addrinfo *
20 _resolve (const char *str)
23 static struct addrinfo hints, *result = NULL;
24 struct addrinfo *rp = NULL;
30 memset (&hints, 0, sizeof (hints));
31 hints.ai_family = AF_INET; /* AF_UNSPEC */
32 hints.ai_socktype = SOCK_DGRAM;
33 r = getaddrinfo (NULL, str, &hints, &result);
35 fprintf (stderr, "getaddrinfo: %s\n", gai_strerror (r));
41 result = result->ai_next;
48 udp_free_polldata (struct polldata *data)
58 /* @return if select() returns error or timeout, return select()
59 else return value from invoked callback function */
61 udp_poll (struct polldata *data)
67 uint8_t buf[RS_MAX_PACKET_LEN];
70 FD_SET (data->s, &rfds);
72 timeout = data->timeout->tv_sec; /* Save from destruction (Linux). */
73 //fprintf (stderr, "calling select with timeout %ld\n", timeout);
74 r = select (data->s + 1, &rfds, NULL, NULL, data->timeout);
76 data->timeout->tv_sec = timeout; /* Restore. */
77 //fprintf (stderr, "select returning %d\n", r);
80 len = recv (data->s, buf, sizeof (buf), 0);
82 return data->cb (buf, len);
88 udp_server (const char *bindto, struct timeval *timeout, data_cb cb)
93 for (res = _resolve (bindto); res; res = _resolve (bindto))
95 s = socket (res->ai_family, res->ai_socktype, res->ai_protocol);
98 if (bind (s, res->ai_addr, res->ai_addrlen) == 0)
110 struct polldata *data = malloc (sizeof (struct polldata));
112 memset (data, 0, sizeof (struct polldata));
117 data->timeout = malloc (sizeof (struct timeval));
118 assert (data->timeout);
119 memcpy (data->timeout, timeout, sizeof (struct timeval));
128 hd (const uint8_t *buf, ssize_t len)
132 printf ("# len: %ld\n", len);
133 for (i = 0; i < len; i++)
135 printf ("%02x%s", buf[i], (i+1) % 8 ? " " : " ");
136 if ((i + 1) % 16 == 0)