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.
11 #include <sys/socket.h>
12 #include <netinet/in.h>
21 #include <sys/types.h>
22 #include <sys/select.h>
25 #include <arpa/inet.h>
28 #include <openssl/ssl.h>
32 #include "radsecproxy.h"
34 static void setprotoopts(struct commonprotoopts *opts);
35 static char **getlistenerargs();
36 void *tcplistener(void *arg);
37 int tcpconnect(struct server *server, struct timeval *when, int timeout, char * text);
38 void *tcpclientrd(void *arg);
39 int clientradputtcp(struct server *server, unsigned char *rad);
42 static const struct protodefs protodefs = {
44 NULL, /* secretdefault */
45 SOCK_STREAM, /* socktype */
46 "1812", /* portdefault */
47 0, /* retrycountdefault */
48 0, /* retrycountmax */
49 REQUEST_RETRY_INTERVAL * REQUEST_RETRY_COUNT, /* retryintervaldefault */
50 60, /* retryintervalmax */
51 DUPLICATE_INTERVAL, /* duplicateintervaldefault */
52 setprotoopts, /* setprotoopts */
53 getlistenerargs, /* getlistenerargs */
54 tcplistener, /* listener */
55 tcpconnect, /* connecter */
56 tcpclientrd, /* clientconnreader */
57 clientradputtcp, /* clientradput */
59 NULL, /* addserverextra */
60 tcpsetsrcres, /* setsrcres */
64 static struct addrinfo *srcres = NULL;
65 static uint8_t handle;
66 static struct commonprotoopts *protoopts = NULL;
67 const struct protodefs *tcpinit(uint8_t h) {
72 static void setprotoopts(struct commonprotoopts *opts) {
76 static char **getlistenerargs() {
77 return protoopts ? protoopts->listenargs : NULL;
82 srcres = resolve_hostport_addrinfo(handle, protoopts ? protoopts->sourcearg : NULL);
85 int tcpconnect(struct server *server, struct timeval *when, int timeout, char *text) {
89 debug(DBG_DBG, "tcpconnect: called from %s", text);
90 pthread_mutex_lock(&server->lock);
91 if (when && memcmp(&server->lastconnecttry, when, sizeof(struct timeval))) {
92 /* already reconnected, nothing to do */
93 debug(DBG_DBG, "tcpconnect(%s): seems already reconnected", text);
94 pthread_mutex_unlock(&server->lock);
99 gettimeofday(&now, NULL);
100 elapsed = now.tv_sec - server->lastconnecttry.tv_sec;
101 if (timeout && server->lastconnecttry.tv_sec && elapsed > timeout) {
102 debug(DBG_DBG, "tcpconnect: timeout");
103 if (server->sock >= 0)
105 pthread_mutex_unlock(&server->lock);
108 if (server->connectionok) {
109 server->connectionok = 0;
111 } else if (elapsed < 1)
113 else if (elapsed < 60) {
114 debug(DBG_INFO, "tcpconnect: sleeping %lds", elapsed);
116 } else if (elapsed < 100000) {
117 debug(DBG_INFO, "tcpconnect: sleeping %ds", 60);
120 server->lastconnecttry.tv_sec = now.tv_sec; /* no sleep at startup */
121 debug(DBG_WARN, "tcpconnect: trying to open TCP connection to %s port %s", server->conf->host, server->conf->port);
122 if (server->sock >= 0)
124 if ((server->sock = connecttcp(server->conf->addrinfo, srcres)) >= 0)
126 debug(DBG_ERR, "tcpconnect: connecttcp failed");
128 debug(DBG_WARN, "tcpconnect: TCP connection to %s port %s up", server->conf->host, server->conf->port);
129 server->connectionok = 1;
130 gettimeofday(&server->lastconnecttry, NULL);
131 pthread_mutex_unlock(&server->lock);
135 /* timeout in seconds, 0 means no timeout (blocking), returns when num bytes have been read, or timeout */
136 /* returns 0 on timeout, -1 on error and num if ok */
137 int tcpreadtimeout(int s, unsigned char *buf, int num, int timeout) {
139 fd_set readfds, writefds;
140 struct timeval timer;
144 /* make socket non-blocking? */
145 for (len = 0; len < num; len += cnt) {
150 timer.tv_sec = timeout;
153 ndesc = select(s + 1, &readfds, &writefds, NULL, timeout ? &timer : NULL);
157 cnt = read(s, buf + len, num - len);
164 /* timeout in seconds, 0 means no timeout (blocking) */
165 unsigned char *radtcpget(int s, int timeout) {
167 unsigned char buf[4], *rad;
170 cnt = tcpreadtimeout(s, buf, 4, timeout);
172 debug(DBG_DBG, cnt ? "radtcpget: connection lost" : "radtcpget: timeout");
179 debug(DBG_ERR, "radtcpget: malloc failed");
184 cnt = tcpreadtimeout(s, rad + 4, len - 4, timeout);
186 debug(DBG_DBG, cnt ? "radtcpget: connection lost" : "radtcpget: timeout");
195 debug(DBG_WARN, "radtcpget: packet smaller than minimum radius size");
198 debug(DBG_DBG, "radtcpget: got %d bytes", len);
202 int clientradputtcp(struct server *server, unsigned char *rad) {
205 struct clsrvconf *conf = server->conf;
207 if (!server->connectionok)
210 if ((cnt = write(server->sock, rad, len)) <= 0) {
211 debug(DBG_ERR, "clientradputtcp: write error");
214 debug(DBG_DBG, "clientradputtcp: Sent %d bytes, Radius packet of length %d to TCP peer %s", cnt, len, conf->host);
218 void *tcpclientrd(void *arg) {
219 struct server *server = (struct server *)arg;
221 struct timeval lastconnecttry;
224 /* yes, lastconnecttry is really necessary */
225 lastconnecttry = server->lastconnecttry;
226 buf = radtcpget(server->sock, 0);
228 tcpconnect(server, &lastconnecttry, 0, "tcpclientrd");
234 server->clientrdgone = 1;
238 void *tcpserverwr(void *arg) {
240 struct client *client = (struct client *)arg;
241 struct queue *replyq;
242 struct request *reply;
244 debug(DBG_DBG, "tcpserverwr: starting for %s", addr2string(client->addr));
245 replyq = client->replyq;
247 pthread_mutex_lock(&replyq->mutex);
248 while (!list_first(replyq->entries)) {
249 if (client->sock >= 0) {
250 debug(DBG_DBG, "tcpserverwr: waiting for signal");
251 pthread_cond_wait(&replyq->cond, &replyq->mutex);
252 debug(DBG_DBG, "tcpserverwr: got signal");
254 if (client->sock < 0) {
255 /* s might have changed while waiting */
256 pthread_mutex_unlock(&replyq->mutex);
257 debug(DBG_DBG, "tcpserverwr: exiting as requested");
261 reply = (struct request *)list_shift(replyq->entries);
262 pthread_mutex_unlock(&replyq->mutex);
263 cnt = write(client->sock, reply->replybuf, RADLEN(reply->replybuf));
265 debug(DBG_DBG, "tcpserverwr: sent %d bytes, Radius packet of length %d to %s",
266 cnt, RADLEN(reply->replybuf), addr2string(client->addr));
268 debug(DBG_ERR, "tcpserverwr: write error for %s", addr2string(client->addr));
273 void tcpserverrd(struct client *client) {
276 pthread_t tcpserverwrth;
278 debug(DBG_DBG, "tcpserverrd: starting for %s", addr2string(client->addr));
280 if (pthread_create(&tcpserverwrth, NULL, tcpserverwr, (void *)client)) {
281 debug(DBG_ERR, "tcpserverrd: pthread_create failed");
286 buf = radtcpget(client->sock, 0);
288 debug(DBG_ERR, "tcpserverrd: connection from %s lost", addr2string(client->addr));
291 debug(DBG_DBG, "tcpserverrd: got Radius message from %s", addr2string(client->addr));
300 debug(DBG_ERR, "tcpserverrd: message authentication/validation failed, closing connection from %s", addr2string(client->addr));
305 /* stop writer by setting s to -1 and give signal in case waiting for data */
307 pthread_mutex_lock(&client->replyq->mutex);
308 pthread_cond_signal(&client->replyq->cond);
309 pthread_mutex_unlock(&client->replyq->mutex);
310 debug(DBG_DBG, "tcpserverrd: waiting for writer to end");
311 pthread_join(tcpserverwrth, NULL);
312 debug(DBG_DBG, "tcpserverrd: reader for %s exiting", addr2string(client->addr));
314 void *tcpservernew(void *arg) {
316 struct sockaddr_storage from;
317 socklen_t fromlen = sizeof(from);
318 struct clsrvconf *conf;
319 struct client *client;
322 if (getpeername(s, (struct sockaddr *)&from, &fromlen)) {
323 debug(DBG_DBG, "tcpservernew: getpeername failed, exiting");
326 debug(DBG_WARN, "tcpservernew: incoming TCP connection from %s", addr2string((struct sockaddr *)&from));
328 conf = find_clconf(handle, (struct sockaddr *)&from, NULL);
330 client = addclient(conf, 1);
333 client->addr = addr_copy((struct sockaddr *)&from);
335 removeclient(client);
337 debug(DBG_WARN, "tcpservernew: failed to create new client instance");
339 debug(DBG_WARN, "tcpservernew: ignoring request, no matching TCP client");
342 shutdown(s, SHUT_RDWR);
347 void *tcplistener(void *arg) {
348 pthread_t tcpserverth;
349 int s, *sp = (int *)arg;
350 struct sockaddr_storage from;
351 socklen_t fromlen = sizeof(from);
356 s = accept(*sp, (struct sockaddr *)&from, &fromlen);
358 debug(DBG_WARN, "accept failed");
361 if (pthread_create(&tcpserverth, NULL, tcpservernew, (void *)&s)) {
362 debug(DBG_ERR, "tcplistener: pthread_create failed");
363 shutdown(s, SHUT_RDWR);
367 pthread_detach(tcpserverth);
373 const struct protodefs *tcpinit(uint8_t h) {