2 * Copyright (C) 2006-2008 Stig Venaas <venaas@uninett.no>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
10 #include <sys/socket.h>
11 #include <netinet/in.h>
20 #include <sys/types.h>
21 #include <sys/select.h>
24 #include <arpa/inet.h>
27 #include <openssl/ssl.h>
31 #include "radsecproxy.h"
34 static int client4_sock = -1;
35 static int client6_sock = -1;
36 static struct queue *server_replyq = NULL;
38 /* exactly one of client and server must be non-NULL */
39 /* return who we received from in *client or *server */
40 /* return from in sa if not NULL */
41 unsigned char *radudpget(int s, struct client **client, struct server **server, uint16_t *port) {
43 unsigned char buf[4], *rad = NULL;
44 struct sockaddr_storage from;
45 struct sockaddr *fromcopy;
46 socklen_t fromlen = sizeof(from);
48 struct list_node *node;
50 struct client *c = NULL;
59 if (select(s + 1, &readfds, NULL, NULL, NULL) < 1)
61 cnt = recvfrom(s, buf, 4, MSG_PEEK | MSG_TRUNC, (struct sockaddr *)&from, &fromlen);
63 debug(DBG_WARN, "radudpget: recv failed");
67 debug(DBG_WARN, "radudpget: length too small");
73 ? find_clconf(RAD_UDP, (struct sockaddr *)&from, NULL)
74 : find_srvconf(RAD_UDP, (struct sockaddr *)&from, NULL);
76 debug(DBG_WARN, "radudpget: got packet from wrong or unknown UDP peer %s, ignoring", addr2string((struct sockaddr *)&from));
83 debug(DBG_WARN, "radudpget: length too small");
90 debug(DBG_ERR, "radudpget: malloc failed");
95 cnt = recv(s, rad, len, MSG_TRUNC);
96 debug(DBG_DBG, "radudpget: got %d bytes from %s", cnt, addr2string((struct sockaddr *)&from));
99 debug(DBG_WARN, "radudpget: packet smaller than length field in radius header");
103 debug(DBG_DBG, "radudpget: packet was padded with %d bytes", cnt - len);
106 pthread_mutex_lock(p->lock);
107 for (node = list_first(p->clients); node; node = list_next(node)) {
108 c = (struct client *)node->data;
109 if (s == c->sock && addr_equal((struct sockaddr *)&from, c->addr))
113 fromcopy = addr_copy((struct sockaddr *)&from);
115 pthread_mutex_unlock(p->lock);
121 pthread_mutex_unlock(p->lock);
128 pthread_mutex_unlock(p->lock);
130 *server = p->servers;
134 *port = port_get((struct sockaddr *)&from);
138 int clientradputudp(struct server *server, unsigned char *rad) {
140 struct sockaddr_storage sa;
141 struct sockaddr *sap;
142 struct clsrvconf *conf = server->conf;
146 port = port_get(conf->addrinfo->ai_addr);
148 if (*rad == RAD_Accounting_Request) {
149 sap = (struct sockaddr *)&sa;
150 memcpy(sap, conf->addrinfo->ai_addr, conf->addrinfo->ai_addrlen);
151 port_set(sap, ++port);
153 sap = conf->addrinfo->ai_addr;
155 if (sendto(server->sock, rad, len, 0, sap, conf->addrinfo->ai_addrlen) >= 0) {
156 debug(DBG_DBG, "clienradputudp: sent UDP of length %d to %s port %d", len, conf->host, port);
160 debug(DBG_WARN, "clientradputudp: send failed");
164 void *udpclientrd(void *arg) {
165 struct server *server;
171 buf = radudpget(*s, NULL, &server, NULL);
176 void *udpserverrd(void *arg) {
178 int *sp = (int *)arg;
183 sleep(5); /* malloc failed */
186 rq->buf = radudpget(*sp, &rq->from, NULL, &rq->udpport);
193 void *udpserverwr(void *arg) {
194 struct queue *replyq = (struct queue *)arg;
195 struct request *reply;
196 struct sockaddr_storage to;
199 pthread_mutex_lock(&replyq->mutex);
200 while (!(reply = (struct request *)list_shift(replyq->entries))) {
201 debug(DBG_DBG, "udp server writer, waiting for signal");
202 pthread_cond_wait(&replyq->cond, &replyq->mutex);
203 debug(DBG_DBG, "udp server writer, got signal");
205 pthread_mutex_unlock(&replyq->mutex);
207 memcpy(&to, reply->from->addr, SOCKADDRP_SIZE(reply->from->addr));
208 port_set((struct sockaddr *)&to, reply->udpport);
209 if (sendto(reply->udpsock, reply->replybuf, RADLEN(reply->replybuf), 0, (struct sockaddr *)&to, SOCKADDR_SIZE(to)) < 0)
210 debug(DBG_WARN, "udpserverwr: send failed");
211 debug(DBG_DBG, "udpserverwr: refcount %d", reply->refcount);
216 void addclientudp(struct client *client) {
217 client->replyq = server_replyq;
220 void addserverextraudp(struct clsrvconf *conf) {
221 switch (conf->addrinfo->ai_family) {
223 if (client4_sock < 0) {
224 client4_sock = bindtoaddr(getsrcprotores(RAD_UDP), AF_INET, 0, 1);
225 if (client4_sock < 0)
226 debugx(1, DBG_ERR, "addserver: failed to create client socket for server %s", conf->host);
228 conf->servers->sock = client4_sock;
231 if (client6_sock < 0) {
232 client6_sock = bindtoaddr(getsrcprotores(RAD_UDP), AF_INET6, 0, 1);
233 if (client6_sock < 0)
234 debugx(1, DBG_ERR, "addserver: failed to create client socket for server %s", conf->host);
236 conf->servers->sock = client6_sock;
239 debugx(1, DBG_ERR, "addserver: unsupported address family");
243 void initextraudp() {
244 pthread_t cl4th, cl6th, srvth;
246 if (client4_sock >= 0)
247 if (pthread_create(&cl4th, NULL, udpclientrd, (void *)&client4_sock))
248 debugx(1, DBG_ERR, "pthread_create failed");
249 if (client6_sock >= 0)
250 if (pthread_create(&cl6th, NULL, udpclientrd, (void *)&client6_sock))
251 debugx(1, DBG_ERR, "pthread_create failed");
253 if (find_clconf_type(RAD_UDP, NULL)) {
254 server_replyq = newqueue();
255 if (pthread_create(&srvth, NULL, udpserverwr, (void *)server_replyq))
256 debugx(1, DBG_ERR, "pthread_create failed");