1 /* Copyright (c) 2006-2010, UNINETT AS
2 * Copyright (c) 2010-2012, NORDUnet A/S */
3 /* See LICENSE for licensing information. */
6 #include <sys/socket.h>
7 #include <netinet/in.h>
16 #include <sys/types.h>
17 #include <sys/select.h>
20 #include <arpa/inet.h>
23 #include "radsecproxy.h"
29 static void setprotoopts(struct commonprotoopts *opts);
30 static char **getlistenerargs();
31 void *tcplistener(void *arg);
32 int tcpconnect(struct server *server, struct timeval *when, int timeout, char * text);
33 void *tcpclientrd(void *arg);
34 int clientradputtcp(struct server *server, unsigned char *rad);
37 static const struct protodefs protodefs = {
39 NULL, /* secretdefault */
40 SOCK_STREAM, /* socktype */
41 "1812", /* portdefault */
42 0, /* retrycountdefault */
43 0, /* retrycountmax */
44 REQUEST_RETRY_INTERVAL * REQUEST_RETRY_COUNT, /* retryintervaldefault */
45 60, /* retryintervalmax */
46 DUPLICATE_INTERVAL, /* duplicateintervaldefault */
47 setprotoopts, /* setprotoopts */
48 getlistenerargs, /* getlistenerargs */
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;
61 static struct commonprotoopts *protoopts = NULL;
62 const struct protodefs *tcpinit(uint8_t h) {
67 static void setprotoopts(struct commonprotoopts *opts) {
71 static char **getlistenerargs() {
72 return protoopts ? protoopts->listenargs : NULL;
78 resolvepassiveaddrinfo(protoopts ? protoopts->sourcearg : NULL,
79 AF_UNSPEC, NULL, protodefs.socktype);
82 int tcpconnect(struct server *server, struct timeval *when, int timeout, char *text) {
86 debug(DBG_DBG, "tcpconnect: called from %s", text);
87 pthread_mutex_lock(&server->lock);
88 if (when && memcmp(&server->lastconnecttry, when, sizeof(struct timeval))) {
89 /* already reconnected, nothing to do */
90 debug(DBG_DBG, "tcpconnect(%s): seems already reconnected", text);
91 pthread_mutex_unlock(&server->lock);
96 gettimeofday(&now, NULL);
97 elapsed = now.tv_sec - server->lastconnecttry.tv_sec;
98 if (timeout && server->lastconnecttry.tv_sec && elapsed > timeout) {
99 debug(DBG_DBG, "tcpconnect: timeout");
100 if (server->sock >= 0)
102 pthread_mutex_unlock(&server->lock);
105 if (server->connectionok) {
106 server->connectionok = 0;
108 } else if (elapsed < 1)
110 else if (elapsed < 60) {
111 debug(DBG_INFO, "tcpconnect: sleeping %lds", elapsed);
113 } else if (elapsed < 100000) {
114 debug(DBG_INFO, "tcpconnect: sleeping %ds", 60);
117 server->lastconnecttry.tv_sec = now.tv_sec; /* no sleep at startup */
119 if (server->sock >= 0)
121 if ((server->sock = connecttcphostlist(server->conf->hostports, srcres)) >= 0)
124 server->connectionok = 1;
125 gettimeofday(&server->lastconnecttry, NULL);
126 pthread_mutex_unlock(&server->lock);
130 /* timeout in seconds, 0 means no timeout (blocking), returns when num bytes have been read, or timeout */
131 /* returns 0 on timeout, -1 on error and num if ok */
132 int tcpreadtimeout(int s, unsigned char *buf, int num, int timeout) {
134 fd_set readfds, writefds;
135 struct timeval timer;
139 /* make socket non-blocking? */
140 for (len = 0; len < num; len += cnt) {
145 timer.tv_sec = timeout;
148 ndesc = select(s + 1, &readfds, &writefds, NULL, timeout ? &timer : NULL);
152 cnt = read(s, buf + len, num - len);
159 /* timeout in seconds, 0 means no timeout (blocking) */
160 unsigned char *radtcpget(int s, int timeout) {
162 unsigned char buf[4], *rad;
165 cnt = tcpreadtimeout(s, buf, 4, timeout);
167 debug(DBG_DBG, cnt ? "radtcpget: connection lost" : "radtcpget: timeout");
174 debug(DBG_ERR, "radtcpget: malloc failed");
179 cnt = tcpreadtimeout(s, rad + 4, len - 4, timeout);
181 debug(DBG_DBG, cnt ? "radtcpget: connection lost" : "radtcpget: timeout");
190 debug(DBG_WARN, "radtcpget: packet smaller than minimum radius size");
193 debug(DBG_DBG, "radtcpget: got %d bytes", len);
197 int clientradputtcp(struct server *server, unsigned char *rad) {
200 struct clsrvconf *conf = server->conf;
202 if (!server->connectionok)
205 if ((cnt = write(server->sock, rad, len)) <= 0) {
206 debug(DBG_ERR, "clientradputtcp: write error");
209 debug(DBG_DBG, "clientradputtcp: Sent %d bytes, Radius packet of length %d to TCP peer %s", cnt, len, conf->name);
213 void *tcpclientrd(void *arg) {
214 struct server *server = (struct server *)arg;
216 struct timeval lastconnecttry;
219 /* yes, lastconnecttry is really necessary */
220 lastconnecttry = server->lastconnecttry;
221 buf = radtcpget(server->sock, 0);
223 tcpconnect(server, &lastconnecttry, 0, "tcpclientrd");
229 server->clientrdgone = 1;
233 void *tcpserverwr(void *arg) {
235 struct client *client = (struct client *)arg;
236 struct gqueue *replyq;
237 struct request *reply;
239 debug(DBG_DBG, "tcpserverwr: starting for %s", addr2string(client->addr));
240 replyq = client->replyq;
242 pthread_mutex_lock(&replyq->mutex);
243 while (!list_first(replyq->entries)) {
244 if (client->sock >= 0) {
245 debug(DBG_DBG, "tcpserverwr: waiting for signal");
246 pthread_cond_wait(&replyq->cond, &replyq->mutex);
247 debug(DBG_DBG, "tcpserverwr: got signal");
249 if (client->sock < 0) {
250 /* s might have changed while waiting */
251 pthread_mutex_unlock(&replyq->mutex);
252 debug(DBG_DBG, "tcpserverwr: exiting as requested");
256 reply = (struct request *)list_shift(replyq->entries);
257 pthread_mutex_unlock(&replyq->mutex);
258 cnt = write(client->sock, reply->replybuf, RADLEN(reply->replybuf));
260 debug(DBG_DBG, "tcpserverwr: sent %d bytes, Radius packet of length %d to %s",
261 cnt, RADLEN(reply->replybuf), addr2string(client->addr));
263 debug(DBG_ERR, "tcpserverwr: write error for %s", addr2string(client->addr));
268 void tcpserverrd(struct client *client) {
271 pthread_t tcpserverwrth;
273 debug(DBG_DBG, "tcpserverrd: starting for %s", addr2string(client->addr));
275 if (pthread_create(&tcpserverwrth, NULL, tcpserverwr, (void *)client)) {
276 debug(DBG_ERR, "tcpserverrd: pthread_create failed");
281 buf = radtcpget(client->sock, 0);
283 debug(DBG_ERR, "tcpserverrd: connection from %s lost", addr2string(client->addr));
286 debug(DBG_DBG, "tcpserverrd: got Radius message from %s", addr2string(client->addr));
295 debug(DBG_ERR, "tcpserverrd: message authentication/validation failed, closing connection from %s", addr2string(client->addr));
300 /* stop writer by setting s to -1 and give signal in case waiting for data */
302 pthread_mutex_lock(&client->replyq->mutex);
303 pthread_cond_signal(&client->replyq->cond);
304 pthread_mutex_unlock(&client->replyq->mutex);
305 debug(DBG_DBG, "tcpserverrd: waiting for writer to end");
306 pthread_join(tcpserverwrth, NULL);
307 debug(DBG_DBG, "tcpserverrd: reader for %s exiting", addr2string(client->addr));
309 void *tcpservernew(void *arg) {
311 struct sockaddr_storage from;
312 socklen_t fromlen = sizeof(from);
313 struct clsrvconf *conf;
314 struct client *client;
317 if (getpeername(s, (struct sockaddr *)&from, &fromlen)) {
318 debug(DBG_DBG, "tcpservernew: getpeername failed, exiting");
321 debug(DBG_WARN, "tcpservernew: incoming TCP connection from %s", addr2string((struct sockaddr *)&from));
323 conf = find_clconf(handle, (struct sockaddr *)&from, NULL);
325 client = addclient(conf, 1);
328 client->addr = addr_copy((struct sockaddr *)&from);
330 removeclient(client);
332 debug(DBG_WARN, "tcpservernew: failed to create new client instance");
334 debug(DBG_WARN, "tcpservernew: ignoring request, no matching TCP client");
337 shutdown(s, SHUT_RDWR);
342 void *tcplistener(void *arg) {
343 pthread_t tcpserverth;
344 int s, *sp = (int *)arg;
345 struct sockaddr_storage from;
346 socklen_t fromlen = sizeof(from);
351 s = accept(*sp, (struct sockaddr *)&from, &fromlen);
353 debug(DBG_WARN, "accept failed");
356 if (pthread_create(&tcpserverth, NULL, tcpservernew, (void *)&s)) {
357 debug(DBG_ERR, "tcplistener: pthread_create failed");
358 shutdown(s, SHUT_RDWR);
362 pthread_detach(tcpserverth);
368 const struct protodefs *tcpinit(uint8_t h) {
373 /* Local Variables: */
374 /* c-file-style: "stroustrup" */