2 * Copyright (C) 2008-2009 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 "radsecproxy.h"
33 static void setprotoopts(struct commonprotoopts *opts);
34 static char **getlistenerargs();
35 void *tcplistener(void *arg);
36 int tcpconnect(struct server *server, struct timeval *when, int timeout, char * text);
37 void *tcpclientrd(void *arg);
38 int clientradputtcp(struct server *server, unsigned char *rad);
41 static const struct protodefs protodefs = {
43 NULL, /* secretdefault */
44 SOCK_STREAM, /* socktype */
45 "1812", /* portdefault */
46 0, /* retrycountdefault */
47 0, /* retrycountmax */
48 REQUEST_RETRY_INTERVAL * REQUEST_RETRY_COUNT, /* retryintervaldefault */
49 60, /* retryintervalmax */
50 DUPLICATE_INTERVAL, /* duplicateintervaldefault */
51 setprotoopts, /* setprotoopts */
52 getlistenerargs, /* getlistenerargs */
53 tcplistener, /* listener */
54 tcpconnect, /* connecter */
55 tcpclientrd, /* clientconnreader */
56 clientradputtcp, /* clientradput */
58 NULL, /* addserverextra */
59 tcpsetsrcres, /* setsrcres */
63 static struct addrinfo *srcres = NULL;
64 static uint8_t handle;
65 static struct commonprotoopts *protoopts = NULL;
66 const struct protodefs *tcpinit(uint8_t h) {
71 static void setprotoopts(struct commonprotoopts *opts) {
75 static char **getlistenerargs() {
76 return protoopts ? protoopts->listenargs : NULL;
81 srcres = resolvepassiveaddrinfo(protoopts ? protoopts->sourcearg : NULL, NULL, protodefs.socktype);
84 int tcpconnect(struct server *server, struct timeval *when, int timeout, char *text) {
88 debug(DBG_DBG, "tcpconnect: called from %s", text);
89 pthread_mutex_lock(&server->lock);
90 if (when && memcmp(&server->lastconnecttry, when, sizeof(struct timeval))) {
91 /* already reconnected, nothing to do */
92 debug(DBG_DBG, "tcpconnect(%s): seems already reconnected", text);
93 pthread_mutex_unlock(&server->lock);
98 gettimeofday(&now, NULL);
99 elapsed = now.tv_sec - server->lastconnecttry.tv_sec;
100 if (timeout && server->lastconnecttry.tv_sec && elapsed > timeout) {
101 debug(DBG_DBG, "tcpconnect: timeout");
102 if (server->sock >= 0)
104 pthread_mutex_unlock(&server->lock);
107 if (server->connectionok) {
108 server->connectionok = 0;
110 } else if (elapsed < 1)
112 else if (elapsed < 60) {
113 debug(DBG_INFO, "tcpconnect: sleeping %lds", elapsed);
115 } else if (elapsed < 100000) {
116 debug(DBG_INFO, "tcpconnect: sleeping %ds", 60);
119 server->lastconnecttry.tv_sec = now.tv_sec; /* no sleep at startup */
121 if (server->sock >= 0)
123 if ((server->sock = connecttcphostlist(server->conf->hostports, srcres)) >= 0)
126 server->connectionok = 1;
127 gettimeofday(&server->lastconnecttry, NULL);
128 pthread_mutex_unlock(&server->lock);
132 /* timeout in seconds, 0 means no timeout (blocking), returns when num bytes have been read, or timeout */
133 /* returns 0 on timeout, -1 on error and num if ok */
134 int tcpreadtimeout(int s, unsigned char *buf, int num, int timeout) {
136 fd_set readfds, writefds;
137 struct timeval timer;
141 /* make socket non-blocking? */
142 for (len = 0; len < num; len += cnt) {
147 timer.tv_sec = timeout;
150 ndesc = select(s + 1, &readfds, &writefds, NULL, timeout ? &timer : NULL);
154 cnt = read(s, buf + len, num - len);
161 /* timeout in seconds, 0 means no timeout (blocking) */
162 unsigned char *radtcpget(int s, int timeout) {
164 unsigned char buf[4], *rad;
167 cnt = tcpreadtimeout(s, buf, 4, timeout);
169 debug(DBG_DBG, cnt ? "radtcpget: connection lost" : "radtcpget: timeout");
176 debug(DBG_ERR, "radtcpget: malloc failed");
181 cnt = tcpreadtimeout(s, rad + 4, len - 4, timeout);
183 debug(DBG_DBG, cnt ? "radtcpget: connection lost" : "radtcpget: timeout");
192 debug(DBG_WARN, "radtcpget: packet smaller than minimum radius size");
195 debug(DBG_DBG, "radtcpget: got %d bytes", len);
199 int clientradputtcp(struct server *server, unsigned char *rad) {
202 struct clsrvconf *conf = server->conf;
204 if (!server->connectionok)
207 if ((cnt = write(server->sock, rad, len)) <= 0) {
208 debug(DBG_ERR, "clientradputtcp: write error");
211 debug(DBG_DBG, "clientradputtcp: Sent %d bytes, Radius packet of length %d to TCP peer %s", cnt, len, conf->name);
215 void *tcpclientrd(void *arg) {
216 struct server *server = (struct server *)arg;
218 struct timeval lastconnecttry;
221 /* yes, lastconnecttry is really necessary */
222 lastconnecttry = server->lastconnecttry;
223 buf = radtcpget(server->sock, 0);
225 tcpconnect(server, &lastconnecttry, 0, "tcpclientrd");
231 server->clientrdgone = 1;
235 void *tcpserverwr(void *arg) {
237 struct client *client = (struct client *)arg;
238 struct gqueue *replyq;
239 struct request *reply;
241 debug(DBG_DBG, "tcpserverwr: starting for %s", addr2string(client->addr));
242 replyq = client->replyq;
244 pthread_mutex_lock(&replyq->mutex);
245 while (!list_first(replyq->entries)) {
246 if (client->sock >= 0) {
247 debug(DBG_DBG, "tcpserverwr: waiting for signal");
248 pthread_cond_wait(&replyq->cond, &replyq->mutex);
249 debug(DBG_DBG, "tcpserverwr: got signal");
251 if (client->sock < 0) {
252 /* s might have changed while waiting */
253 pthread_mutex_unlock(&replyq->mutex);
254 debug(DBG_DBG, "tcpserverwr: exiting as requested");
258 reply = (struct request *)list_shift(replyq->entries);
259 pthread_mutex_unlock(&replyq->mutex);
260 cnt = write(client->sock, reply->replybuf, RADLEN(reply->replybuf));
262 debug(DBG_DBG, "tcpserverwr: sent %d bytes, Radius packet of length %d to %s",
263 cnt, RADLEN(reply->replybuf), addr2string(client->addr));
265 debug(DBG_ERR, "tcpserverwr: write error for %s", addr2string(client->addr));
270 void tcpserverrd(struct client *client) {
273 pthread_t tcpserverwrth;
275 debug(DBG_DBG, "tcpserverrd: starting for %s", addr2string(client->addr));
277 if (pthread_create(&tcpserverwrth, NULL, tcpserverwr, (void *)client)) {
278 debug(DBG_ERR, "tcpserverrd: pthread_create failed");
283 buf = radtcpget(client->sock, 0);
285 debug(DBG_ERR, "tcpserverrd: connection from %s lost", addr2string(client->addr));
288 debug(DBG_DBG, "tcpserverrd: got Radius message from %s", addr2string(client->addr));
297 debug(DBG_ERR, "tcpserverrd: message authentication/validation failed, closing connection from %s", addr2string(client->addr));
302 /* stop writer by setting s to -1 and give signal in case waiting for data */
304 pthread_mutex_lock(&client->replyq->mutex);
305 pthread_cond_signal(&client->replyq->cond);
306 pthread_mutex_unlock(&client->replyq->mutex);
307 debug(DBG_DBG, "tcpserverrd: waiting for writer to end");
308 pthread_join(tcpserverwrth, NULL);
309 debug(DBG_DBG, "tcpserverrd: reader for %s exiting", addr2string(client->addr));
311 void *tcpservernew(void *arg) {
313 struct sockaddr_storage from;
314 socklen_t fromlen = sizeof(from);
315 struct clsrvconf *conf;
316 struct client *client;
319 if (getpeername(s, (struct sockaddr *)&from, &fromlen)) {
320 debug(DBG_DBG, "tcpservernew: getpeername failed, exiting");
323 debug(DBG_WARN, "tcpservernew: incoming TCP connection from %s", addr2string((struct sockaddr *)&from));
325 conf = find_clconf(handle, (struct sockaddr *)&from, NULL);
327 client = addclient(conf, 1);
330 client->addr = addr_copy((struct sockaddr *)&from);
332 removeclient(client);
334 debug(DBG_WARN, "tcpservernew: failed to create new client instance");
336 debug(DBG_WARN, "tcpservernew: ignoring request, no matching TCP client");
339 shutdown(s, SHUT_RDWR);
344 void *tcplistener(void *arg) {
345 pthread_t tcpserverth;
346 int s, *sp = (int *)arg;
347 struct sockaddr_storage from;
348 socklen_t fromlen = sizeof(from);
353 s = accept(*sp, (struct sockaddr *)&from, &fromlen);
355 debug(DBG_WARN, "accept failed");
358 if (pthread_create(&tcpserverth, NULL, tcpservernew, (void *)&s)) {
359 debug(DBG_ERR, "tcplistener: pthread_create failed");
360 shutdown(s, SHUT_RDWR);
364 pthread_detach(tcpserverth);
370 const struct protodefs *tcpinit(uint8_t h) {
375 /* Local Variables: */
376 /* c-file-style: "stroustrup" */