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, struct sockaddr_storage *sa) {
43 unsigned char buf[4], *rad = NULL;
44 struct sockaddr_storage from;
45 socklen_t fromlen = sizeof(from);
47 struct list_node *node;
57 if (select(s + 1, &readfds, NULL, NULL, NULL) < 1)
59 cnt = recvfrom(s, buf, 4, MSG_PEEK | MSG_TRUNC, (struct sockaddr *)&from, &fromlen);
61 debug(DBG_WARN, "radudpget: recv failed");
65 debug(DBG_WARN, "radudpget: length too small");
71 ? find_clconf(RAD_UDP, (struct sockaddr *)&from, NULL)
72 : find_srvconf(RAD_UDP, (struct sockaddr *)&from, NULL);
74 debug(DBG_WARN, "radudpget: got packet from wrong or unknown UDP peer %s, ignoring", addr2string((struct sockaddr *)&from, fromlen));
81 debug(DBG_WARN, "radudpget: length too small");
88 debug(DBG_ERR, "radudpget: malloc failed");
93 cnt = recv(s, rad, len, MSG_TRUNC);
94 debug(DBG_DBG, "radudpget: got %d bytes from %s", cnt, addr2string((struct sockaddr *)&from, fromlen));
97 debug(DBG_WARN, "radudpget: packet smaller than length field in radius header");
101 debug(DBG_DBG, "radudpget: packet was padded with %d bytes", cnt - len);
104 node = list_first(p->clients);
105 *client = node ? (struct client *)node->data : addclient(p);
109 *server = p->servers;
117 int clientradputudp(struct server *server, unsigned char *rad) {
119 struct sockaddr_storage sa;
120 struct sockaddr *sap;
121 struct clsrvconf *conf = server->conf;
122 in_port_t *port = NULL;
126 if (*rad == RAD_Accounting_Request) {
127 sap = (struct sockaddr *)&sa;
128 memcpy(sap, conf->addrinfo->ai_addr, conf->addrinfo->ai_addrlen);
130 sap = conf->addrinfo->ai_addr;
132 switch (sap->sa_family) {
134 port = &((struct sockaddr_in *)sap)->sin_port;
137 port = &((struct sockaddr_in6 *)sap)->sin6_port;
143 if (*rad == RAD_Accounting_Request)
144 *port = htons(ntohs(*port) + 1);
146 if (sendto(server->sock, rad, len, 0, sap, conf->addrinfo->ai_addrlen) >= 0) {
147 debug(DBG_DBG, "clienradputudp: sent UDP of length %d to %s port %d", len, conf->host, ntohs(*port));
151 debug(DBG_WARN, "clientradputudp: send failed");
155 void *udpclientrd(void *arg) {
156 struct server *server;
162 buf = radudpget(*s, NULL, &server, NULL);
163 if (!replyh(server, buf))
168 void *udpserverrd(void *arg) {
170 int *sp = (int *)arg;
173 memset(&rq, 0, sizeof(struct request));
174 rq.buf = radudpget(*sp, &rq.from, NULL, &rq.fromsa);
175 rq.fromudpsock = *sp;
181 void *udpserverwr(void *arg) {
182 struct queue *replyq = (struct queue *)arg;
186 pthread_mutex_lock(&replyq->mutex);
187 while (!(reply = (struct reply *)list_shift(replyq->entries))) {
188 debug(DBG_DBG, "udp server writer, waiting for signal");
189 pthread_cond_wait(&replyq->cond, &replyq->mutex);
190 debug(DBG_DBG, "udp server writer, got signal");
192 pthread_mutex_unlock(&replyq->mutex);
194 if (sendto(reply->toudpsock, reply->buf, RADLEN(reply->buf), 0,
195 (struct sockaddr *)&reply->tosa, SOCKADDR_SIZE(reply->tosa)) < 0)
196 debug(DBG_WARN, "sendudp: send failed");
202 void addclientudp(struct client *client) {
203 client->replyq = server_replyq;
206 void addserverextraudp(struct clsrvconf *conf) {
207 switch (conf->addrinfo->ai_family) {
209 if (client4_sock < 0) {
210 client4_sock = bindtoaddr(getsrcprotores(RAD_UDP), AF_INET, 0, 1);
211 if (client4_sock < 0)
212 debugx(1, DBG_ERR, "addserver: failed to create client socket for server %s", conf->host);
214 conf->servers->sock = client4_sock;
217 if (client6_sock < 0) {
218 client6_sock = bindtoaddr(getsrcprotores(RAD_UDP), AF_INET6, 0, 1);
219 if (client6_sock < 0)
220 debugx(1, DBG_ERR, "addserver: failed to create client socket for server %s", conf->host);
222 conf->servers->sock = client6_sock;
225 debugx(1, DBG_ERR, "addserver: unsupported address family");
229 void initextraudp() {
230 pthread_t cl4th, cl6th, srvth;
232 if (client4_sock >= 0)
233 if (pthread_create(&cl4th, NULL, udpclientrd, (void *)&client4_sock))
234 debugx(1, DBG_ERR, "pthread_create failed");
235 if (client6_sock >= 0)
236 if (pthread_create(&cl6th, NULL, udpclientrd, (void *)&client6_sock))
237 debugx(1, DBG_ERR, "pthread_create failed");
239 if (find_clconf_type(RAD_UDP, NULL)) {
240 server_replyq = newqueue();
241 if (pthread_create(&srvth, NULL, udpserverwr, (void *)server_replyq))
242 debugx(1, DBG_ERR, "pthread_create failed");