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;
82 resolvepassiveaddrinfo(protoopts ? protoopts->sourcearg : NULL,
83 AF_UNSPEC, NULL, protodefs.socktype);
86 int tcpconnect(struct server *server, struct timeval *when, int timeout, char *text) {
90 debug(DBG_DBG, "tcpconnect: called from %s", text);
91 pthread_mutex_lock(&server->lock);
92 if (when && memcmp(&server->lastconnecttry, when, sizeof(struct timeval))) {
93 /* already reconnected, nothing to do */
94 debug(DBG_DBG, "tcpconnect(%s): seems already reconnected", text);
95 pthread_mutex_unlock(&server->lock);
100 gettimeofday(&now, NULL);
101 elapsed = now.tv_sec - server->lastconnecttry.tv_sec;
102 if (timeout && server->lastconnecttry.tv_sec && elapsed > timeout) {
103 debug(DBG_DBG, "tcpconnect: timeout");
104 if (server->sock >= 0)
106 pthread_mutex_unlock(&server->lock);
109 if (server->connectionok) {
110 server->connectionok = 0;
112 } else if (elapsed < 1)
114 else if (elapsed < 60) {
115 debug(DBG_INFO, "tcpconnect: sleeping %lds", elapsed);
117 } else if (elapsed < 100000) {
118 debug(DBG_INFO, "tcpconnect: sleeping %ds", 60);
121 server->lastconnecttry.tv_sec = now.tv_sec; /* no sleep at startup */
123 if (server->sock >= 0)
125 if ((server->sock = connecttcphostlist(server->conf->hostports, srcres)) >= 0)
128 server->connectionok = 1;
129 gettimeofday(&server->lastconnecttry, NULL);
130 pthread_mutex_unlock(&server->lock);
134 /* timeout in seconds, 0 means no timeout (blocking), returns when num bytes have been read, or timeout */
135 /* returns 0 on timeout, -1 on error and num if ok */
136 int tcpreadtimeout(int s, unsigned char *buf, int num, int timeout) {
138 fd_set readfds, writefds;
139 struct timeval timer;
143 /* make socket non-blocking? */
144 for (len = 0; len < num; len += cnt) {
149 timer.tv_sec = timeout;
152 ndesc = select(s + 1, &readfds, &writefds, NULL, timeout ? &timer : NULL);
156 cnt = read(s, buf + len, num - len);
163 /* timeout in seconds, 0 means no timeout (blocking) */
164 unsigned char *radtcpget(int s, int timeout) {
166 unsigned char buf[4], *rad;
169 cnt = tcpreadtimeout(s, buf, 4, timeout);
171 debug(DBG_DBG, cnt ? "radtcpget: connection lost" : "radtcpget: timeout");
178 debug(DBG_ERR, "radtcpget: malloc failed");
183 cnt = tcpreadtimeout(s, rad + 4, len - 4, timeout);
185 debug(DBG_DBG, cnt ? "radtcpget: connection lost" : "radtcpget: timeout");
194 debug(DBG_WARN, "radtcpget: packet smaller than minimum radius size");
197 debug(DBG_DBG, "radtcpget: got %d bytes", len);
201 int clientradputtcp(struct server *server, unsigned char *rad) {
204 struct clsrvconf *conf = server->conf;
206 if (!server->connectionok)
209 if ((cnt = write(server->sock, rad, len)) <= 0) {
210 debug(DBG_ERR, "clientradputtcp: write error");
213 debug(DBG_DBG, "clientradputtcp: Sent %d bytes, Radius packet of length %d to TCP peer %s", cnt, len, conf->name);
217 void *tcpclientrd(void *arg) {
218 struct server *server = (struct server *)arg;
220 struct timeval lastconnecttry;
223 /* yes, lastconnecttry is really necessary */
224 lastconnecttry = server->lastconnecttry;
225 buf = radtcpget(server->sock, 0);
227 tcpconnect(server, &lastconnecttry, 0, "tcpclientrd");
233 server->clientrdgone = 1;
237 void *tcpserverwr(void *arg) {
239 struct client *client = (struct client *)arg;
240 struct gqueue *replyq;
241 struct request *reply;
243 debug(DBG_DBG, "tcpserverwr: starting for %s", addr2string(client->addr));
244 replyq = client->replyq;
246 pthread_mutex_lock(&replyq->mutex);
247 while (!list_first(replyq->entries)) {
248 if (client->sock >= 0) {
249 debug(DBG_DBG, "tcpserverwr: waiting for signal");
250 pthread_cond_wait(&replyq->cond, &replyq->mutex);
251 debug(DBG_DBG, "tcpserverwr: got signal");
253 if (client->sock < 0) {
254 /* s might have changed while waiting */
255 pthread_mutex_unlock(&replyq->mutex);
256 debug(DBG_DBG, "tcpserverwr: exiting as requested");
260 reply = (struct request *)list_shift(replyq->entries);
261 pthread_mutex_unlock(&replyq->mutex);
262 cnt = write(client->sock, reply->replybuf, RADLEN(reply->replybuf));
264 debug(DBG_DBG, "tcpserverwr: sent %d bytes, Radius packet of length %d to %s",
265 cnt, RADLEN(reply->replybuf), addr2string(client->addr));
267 debug(DBG_ERR, "tcpserverwr: write error for %s", addr2string(client->addr));
272 void tcpserverrd(struct client *client) {
275 pthread_t tcpserverwrth;
277 debug(DBG_DBG, "tcpserverrd: starting for %s", addr2string(client->addr));
279 if (pthread_create(&tcpserverwrth, &pthread_attr, tcpserverwr, (void *)client)) {
280 debug(DBG_ERR, "tcpserverrd: pthread_create failed");
285 buf = radtcpget(client->sock, 0);
287 debug(DBG_ERR, "tcpserverrd: connection from %s lost", addr2string(client->addr));
290 debug(DBG_DBG, "tcpserverrd: got Radius message from %s", addr2string(client->addr));
299 debug(DBG_ERR, "tcpserverrd: message authentication/validation failed, closing connection from %s", addr2string(client->addr));
304 /* stop writer by setting s to -1 and give signal in case waiting for data */
306 pthread_mutex_lock(&client->replyq->mutex);
307 pthread_cond_signal(&client->replyq->cond);
308 pthread_mutex_unlock(&client->replyq->mutex);
309 debug(DBG_DBG, "tcpserverrd: waiting for writer to end");
310 pthread_join(tcpserverwrth, NULL);
311 debug(DBG_DBG, "tcpserverrd: reader for %s exiting", addr2string(client->addr));
313 void *tcpservernew(void *arg) {
315 struct sockaddr_storage from;
316 socklen_t fromlen = sizeof(from);
317 struct clsrvconf *conf;
318 struct client *client;
321 if (getpeername(s, (struct sockaddr *)&from, &fromlen)) {
322 debug(DBG_DBG, "tcpservernew: getpeername failed, exiting");
325 debug(DBG_WARN, "tcpservernew: incoming TCP connection from %s", addr2string((struct sockaddr *)&from));
327 conf = find_clconf(handle, (struct sockaddr *)&from, NULL);
329 client = addclient(conf, 1);
332 client->addr = addr_copy((struct sockaddr *)&from);
334 removeclient(client);
336 debug(DBG_WARN, "tcpservernew: failed to create new client instance");
338 debug(DBG_WARN, "tcpservernew: ignoring request, no matching TCP client");
341 shutdown(s, SHUT_RDWR);
346 void *tcplistener(void *arg) {
347 pthread_t tcpserverth;
348 int s, *sp = (int *)arg;
349 struct sockaddr_storage from;
350 socklen_t fromlen = sizeof(from);
355 s = accept(*sp, (struct sockaddr *)&from, &fromlen);
357 debug(DBG_WARN, "accept failed");
360 if (pthread_create(&tcpserverth, &pthread_attr, tcpservernew, (void *)&s)) {
361 debug(DBG_ERR, "tcplistener: pthread_create failed");
362 shutdown(s, SHUT_RDWR);
366 pthread_detach(tcpserverth);
372 const struct protodefs *tcpinit(uint8_t h) {
377 /* Local Variables: */
378 /* c-file-style: "stroustrup" */