2 * Copyright (C) 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"
33 void *tcplistener(void *arg);
34 int tcpconnect(struct server *server, struct timeval *when, int timeout, char * text);
35 void *tcpclientrd(void *arg);
36 int clientradputtcp(struct server *server, unsigned char *rad);
37 void tcpsetsrcres(char *source);
39 static const struct protodefs protodefs = {
41 NULL, /* secretdefault */
42 SOCK_STREAM, /* socktype */
43 "1812", /* portdefault */
44 0, /* retrycountdefault */
45 0, /* retrycountmax */
46 REQUEST_RETRY_INTERVAL * REQUEST_RETRY_COUNT, /* retryintervaldefault */
47 60, /* retryintervalmax */
48 DUPLICATE_INTERVAL, /* duplicateintervaldefault */
49 tcplistener, /* listener */
50 tcpconnect, /* connecter */
51 tcpclientrd, /* clientconnreader */
52 clientradputtcp, /* clientradput */
54 NULL, /* addserverextra */
55 tcpsetsrcres, /* setsrcres */
59 static struct addrinfo *srcres = NULL;
60 static uint8_t handle;
62 const struct protodefs *tcpinit(uint8_t h) {
67 void tcpsetsrcres(char *source) {
69 srcres = resolve_hostport_addrinfo(handle, source);
72 int tcpconnect(struct server *server, struct timeval *when, int timeout, char *text) {
76 debug(DBG_DBG, "tcpconnect: called from %s", text);
77 pthread_mutex_lock(&server->lock);
78 if (when && memcmp(&server->lastconnecttry, when, sizeof(struct timeval))) {
79 /* already reconnected, nothing to do */
80 debug(DBG_DBG, "tcpconnect(%s): seems already reconnected", text);
81 pthread_mutex_unlock(&server->lock);
86 gettimeofday(&now, NULL);
87 elapsed = now.tv_sec - server->lastconnecttry.tv_sec;
88 if (timeout && server->lastconnecttry.tv_sec && elapsed > timeout) {
89 debug(DBG_DBG, "tcpconnect: timeout");
90 if (server->sock >= 0)
92 pthread_mutex_unlock(&server->lock);
95 if (server->connectionok) {
96 server->connectionok = 0;
98 } else if (elapsed < 1)
100 else if (elapsed < 60) {
101 debug(DBG_INFO, "tcpconnect: sleeping %lds", elapsed);
103 } else if (elapsed < 100000) {
104 debug(DBG_INFO, "tcpconnect: sleeping %ds", 60);
107 server->lastconnecttry.tv_sec = now.tv_sec; /* no sleep at startup */
108 debug(DBG_WARN, "tcpconnect: trying to open TCP connection to %s port %s", server->conf->host, server->conf->port);
109 if (server->sock >= 0)
111 if ((server->sock = connecttcp(server->conf->addrinfo, srcres)) >= 0)
113 debug(DBG_ERR, "tcpconnect: connecttcp failed");
115 debug(DBG_WARN, "tcpconnect: TCP connection to %s port %s up", server->conf->host, server->conf->port);
116 server->connectionok = 1;
117 gettimeofday(&server->lastconnecttry, NULL);
118 pthread_mutex_unlock(&server->lock);
122 /* timeout in seconds, 0 means no timeout (blocking), returns when num bytes have been read, or timeout */
123 /* returns 0 on timeout, -1 on error and num if ok */
124 int tcpreadtimeout(int s, unsigned char *buf, int num, int timeout) {
126 fd_set readfds, writefds;
127 struct timeval timer;
131 /* make socket non-blocking? */
132 for (len = 0; len < num; len += cnt) {
137 timer.tv_sec = timeout;
140 ndesc = select(s + 1, &readfds, &writefds, NULL, timeout ? &timer : NULL);
144 cnt = read(s, buf + len, num - len);
151 /* timeout in seconds, 0 means no timeout (blocking) */
152 unsigned char *radtcpget(int s, int timeout) {
154 unsigned char buf[4], *rad;
157 cnt = tcpreadtimeout(s, buf, 4, timeout);
159 debug(DBG_DBG, cnt ? "radtcpget: connection lost" : "radtcpget: timeout");
166 debug(DBG_ERR, "radtcpget: malloc failed");
171 cnt = tcpreadtimeout(s, rad + 4, len - 4, timeout);
173 debug(DBG_DBG, cnt ? "radtcpget: connection lost" : "radtcpget: timeout");
182 debug(DBG_WARN, "radtcpget: packet smaller than minimum radius size");
185 debug(DBG_DBG, "radtcpget: got %d bytes", len);
189 int clientradputtcp(struct server *server, unsigned char *rad) {
192 struct clsrvconf *conf = server->conf;
194 if (!server->connectionok)
197 if ((cnt = write(server->sock, rad, len)) <= 0) {
198 debug(DBG_ERR, "clientradputtcp: write error");
201 debug(DBG_DBG, "clientradputtcp: Sent %d bytes, Radius packet of length %d to TCP peer %s", cnt, len, conf->host);
205 void *tcpclientrd(void *arg) {
206 struct server *server = (struct server *)arg;
208 struct timeval lastconnecttry;
211 /* yes, lastconnecttry is really necessary */
212 lastconnecttry = server->lastconnecttry;
213 buf = radtcpget(server->sock, 0);
215 tcpconnect(server, &lastconnecttry, 0, "tcpclientrd");
221 server->clientrdgone = 1;
225 void *tcpserverwr(void *arg) {
227 struct client *client = (struct client *)arg;
228 struct queue *replyq;
229 struct request *reply;
231 debug(DBG_DBG, "tcpserverwr: starting for %s", addr2string(client->addr));
232 replyq = client->replyq;
234 pthread_mutex_lock(&replyq->mutex);
235 while (!list_first(replyq->entries)) {
236 if (client->sock >= 0) {
237 debug(DBG_DBG, "tcpserverwr: waiting for signal");
238 pthread_cond_wait(&replyq->cond, &replyq->mutex);
239 debug(DBG_DBG, "tcpserverwr: got signal");
241 if (client->sock < 0) {
242 /* s might have changed while waiting */
243 pthread_mutex_unlock(&replyq->mutex);
244 debug(DBG_DBG, "tcpserverwr: exiting as requested");
248 reply = (struct request *)list_shift(replyq->entries);
249 pthread_mutex_unlock(&replyq->mutex);
250 cnt = write(client->sock, reply->replybuf, RADLEN(reply->replybuf));
252 debug(DBG_DBG, "tcpserverwr: sent %d bytes, Radius packet of length %d to %s",
253 cnt, RADLEN(reply->replybuf), addr2string(client->addr));
255 debug(DBG_ERR, "tcpserverwr: write error for %s", addr2string(client->addr));
260 void tcpserverrd(struct client *client) {
263 pthread_t tcpserverwrth;
265 debug(DBG_DBG, "tcpserverrd: starting for %s", addr2string(client->addr));
267 if (pthread_create(&tcpserverwrth, NULL, tcpserverwr, (void *)client)) {
268 debug(DBG_ERR, "tcpserverrd: pthread_create failed");
273 buf = radtcpget(client->sock, 0);
275 debug(DBG_ERR, "tcpserverrd: connection from %s lost", addr2string(client->addr));
278 debug(DBG_DBG, "tcpserverrd: got Radius message from %s", addr2string(client->addr));
287 debug(DBG_ERR, "tcpserverrd: message authentication/validation failed, closing connection from %s", addr2string(client->addr));
292 /* stop writer by setting s to -1 and give signal in case waiting for data */
294 pthread_mutex_lock(&client->replyq->mutex);
295 pthread_cond_signal(&client->replyq->cond);
296 pthread_mutex_unlock(&client->replyq->mutex);
297 debug(DBG_DBG, "tcpserverrd: waiting for writer to end");
298 pthread_join(tcpserverwrth, NULL);
299 debug(DBG_DBG, "tcpserverrd: reader for %s exiting", addr2string(client->addr));
301 void *tcpservernew(void *arg) {
303 struct sockaddr_storage from;
304 socklen_t fromlen = sizeof(from);
305 struct clsrvconf *conf;
306 struct client *client;
309 if (getpeername(s, (struct sockaddr *)&from, &fromlen)) {
310 debug(DBG_DBG, "tcpservernew: getpeername failed, exiting");
313 debug(DBG_WARN, "tcpservernew: incoming TCP connection from %s", addr2string((struct sockaddr *)&from));
315 conf = find_clconf(handle, (struct sockaddr *)&from, NULL);
317 client = addclient(conf, 1);
320 client->addr = addr_copy((struct sockaddr *)&from);
322 removeclient(client);
324 debug(DBG_WARN, "tcpservernew: failed to create new client instance");
326 debug(DBG_WARN, "tcpservernew: ignoring request, no matching TCP client");
329 shutdown(s, SHUT_RDWR);
334 void *tcplistener(void *arg) {
335 pthread_t tcpserverth;
336 int s, *sp = (int *)arg;
337 struct sockaddr_storage from;
338 socklen_t fromlen = sizeof(from);
343 s = accept(*sp, (struct sockaddr *)&from, &fromlen);
345 debug(DBG_WARN, "accept failed");
348 if (pthread_create(&tcpserverth, NULL, tcpservernew, (void *)&s)) {
349 debug(DBG_ERR, "tcplistener: pthread_create failed");
350 shutdown(s, SHUT_RDWR);
354 pthread_detach(tcpserverth);