1 /* Copyright (c) 2006-2009, Stig Venaas, UNINETT AS.
2 * Copyright (c) 2010, UNINETT AS, NORDUnet A/S.
3 * Copyright (c) 2010-2012, NORDUnet A/S. */
4 /* See LICENSE for licensing information. */
7 #include <sys/socket.h>
8 #include <netinet/in.h>
17 #include <sys/types.h>
18 #include <sys/select.h>
21 #include <arpa/inet.h>
24 #include "radsecproxy.h"
30 static void setprotoopts(struct commonprotoopts *opts);
31 static char **getlistenerargs();
32 void *tcplistener(void *arg);
33 int tcpconnect(struct server *server, struct timeval *when, int timeout, char * text);
34 void *tcpclientrd(void *arg);
35 int clientradputtcp(struct server *server, unsigned char *rad);
38 static const struct protodefs protodefs = {
40 NULL, /* secretdefault */
41 SOCK_STREAM, /* socktype */
42 "1812", /* portdefault */
43 0, /* retrycountdefault */
44 0, /* retrycountmax */
45 REQUEST_RETRY_INTERVAL * REQUEST_RETRY_COUNT, /* retryintervaldefault */
46 60, /* retryintervalmax */
47 DUPLICATE_INTERVAL, /* duplicateintervaldefault */
48 setprotoopts, /* setprotoopts */
49 getlistenerargs, /* getlistenerargs */
50 tcplistener, /* listener */
51 tcpconnect, /* connecter */
52 tcpclientrd, /* clientconnreader */
53 clientradputtcp, /* clientradput */
55 NULL, /* addserverextra */
56 tcpsetsrcres, /* setsrcres */
60 static struct addrinfo *srcres = NULL;
61 static uint8_t handle;
62 static struct commonprotoopts *protoopts = NULL;
63 const struct protodefs *tcpinit(uint8_t h) {
68 static void setprotoopts(struct commonprotoopts *opts) {
72 static char **getlistenerargs() {
73 return protoopts ? protoopts->listenargs : NULL;
79 resolvepassiveaddrinfo(protoopts ? protoopts->sourcearg : NULL,
80 AF_UNSPEC, NULL, protodefs.socktype);
83 int tcpconnect(struct server *server, struct timeval *when, int timeout, char *text) {
87 debug(DBG_DBG, "tcpconnect: called from %s", text);
88 pthread_mutex_lock(&server->lock);
89 if (when && memcmp(&server->lastconnecttry, when, sizeof(struct timeval))) {
90 /* already reconnected, nothing to do */
91 debug(DBG_DBG, "tcpconnect(%s): seems already reconnected", text);
92 pthread_mutex_unlock(&server->lock);
97 gettimeofday(&now, NULL);
98 elapsed = now.tv_sec - server->lastconnecttry.tv_sec;
99 if (timeout && server->lastconnecttry.tv_sec && elapsed > timeout) {
100 debug(DBG_DBG, "tcpconnect: timeout");
101 if (server->sock >= 0)
103 pthread_mutex_unlock(&server->lock);
106 if (server->connectionok) {
107 server->connectionok = 0;
109 } else if (elapsed < 1)
111 else if (elapsed < 60) {
112 debug(DBG_INFO, "tcpconnect: sleeping %lds", elapsed);
114 } else if (elapsed < 100000) {
115 debug(DBG_INFO, "tcpconnect: sleeping %ds", 60);
118 server->lastconnecttry.tv_sec = now.tv_sec; /* no sleep at startup */
120 if (server->sock >= 0)
122 if ((server->sock = connecttcphostlist(server->conf->hostports, srcres)) >= 0)
125 server->connectionok = 1;
126 gettimeofday(&server->lastconnecttry, NULL);
127 pthread_mutex_unlock(&server->lock);
131 /* timeout in seconds, 0 means no timeout (blocking), returns when num bytes have been read, or timeout */
132 /* returns 0 on timeout, -1 on error and num if ok */
133 int tcpreadtimeout(int s, unsigned char *buf, int num, int timeout) {
135 fd_set readfds, writefds;
136 struct timeval timer;
140 /* make socket non-blocking? */
141 for (len = 0; len < num; len += cnt) {
146 timer.tv_sec = timeout;
149 ndesc = select(s + 1, &readfds, &writefds, NULL, timeout ? &timer : NULL);
153 cnt = read(s, buf + len, num - len);
160 /* timeout in seconds, 0 means no timeout (blocking) */
161 unsigned char *radtcpget(int s, int timeout) {
163 unsigned char buf[4], *rad;
166 cnt = tcpreadtimeout(s, buf, 4, timeout);
168 debug(DBG_DBG, cnt ? "radtcpget: connection lost" : "radtcpget: timeout");
175 debug(DBG_ERR, "radtcpget: malloc failed");
180 cnt = tcpreadtimeout(s, rad + 4, len - 4, timeout);
182 debug(DBG_DBG, cnt ? "radtcpget: connection lost" : "radtcpget: timeout");
191 debug(DBG_WARN, "radtcpget: packet smaller than minimum radius size");
194 debug(DBG_DBG, "radtcpget: got %d bytes", len);
198 int clientradputtcp(struct server *server, unsigned char *rad) {
201 struct clsrvconf *conf = server->conf;
203 if (!server->connectionok)
206 if ((cnt = write(server->sock, rad, len)) <= 0) {
207 debug(DBG_ERR, "clientradputtcp: write error");
210 debug(DBG_DBG, "clientradputtcp: Sent %d bytes, Radius packet of length %d to TCP peer %s", cnt, len, conf->name);
214 void *tcpclientrd(void *arg) {
215 struct server *server = (struct server *)arg;
217 struct timeval lastconnecttry;
220 /* yes, lastconnecttry is really necessary */
221 lastconnecttry = server->lastconnecttry;
222 buf = radtcpget(server->sock, 0);
224 tcpconnect(server, &lastconnecttry, 0, "tcpclientrd");
230 server->clientrdgone = 1;
234 void *tcpserverwr(void *arg) {
236 struct client *client = (struct client *)arg;
237 struct gqueue *replyq;
238 struct request *reply;
240 debug(DBG_DBG, "tcpserverwr: starting for %s", addr2string(client->addr));
241 replyq = client->replyq;
243 pthread_mutex_lock(&replyq->mutex);
244 while (!list_first(replyq->entries)) {
245 if (client->sock >= 0) {
246 debug(DBG_DBG, "tcpserverwr: waiting for signal");
247 pthread_cond_wait(&replyq->cond, &replyq->mutex);
248 debug(DBG_DBG, "tcpserverwr: got signal");
250 if (client->sock < 0) {
251 /* s might have changed while waiting */
252 pthread_mutex_unlock(&replyq->mutex);
253 debug(DBG_DBG, "tcpserverwr: exiting as requested");
257 reply = (struct request *)list_shift(replyq->entries);
258 pthread_mutex_unlock(&replyq->mutex);
259 cnt = write(client->sock, reply->replybuf, RADLEN(reply->replybuf));
261 debug(DBG_DBG, "tcpserverwr: sent %d bytes, Radius packet of length %d to %s",
262 cnt, RADLEN(reply->replybuf), addr2string(client->addr));
264 debug(DBG_ERR, "tcpserverwr: write error for %s", addr2string(client->addr));
269 void tcpserverrd(struct client *client) {
272 pthread_t tcpserverwrth;
274 debug(DBG_DBG, "tcpserverrd: starting for %s", addr2string(client->addr));
276 if (pthread_create(&tcpserverwrth, NULL, tcpserverwr, (void *)client)) {
277 debug(DBG_ERR, "tcpserverrd: pthread_create failed");
282 buf = radtcpget(client->sock, 0);
284 debug(DBG_ERR, "tcpserverrd: connection from %s lost", addr2string(client->addr));
287 debug(DBG_DBG, "tcpserverrd: got Radius message from %s", addr2string(client->addr));
296 debug(DBG_ERR, "tcpserverrd: message authentication/validation failed, closing connection from %s", addr2string(client->addr));
301 /* stop writer by setting s to -1 and give signal in case waiting for data */
303 pthread_mutex_lock(&client->replyq->mutex);
304 pthread_cond_signal(&client->replyq->cond);
305 pthread_mutex_unlock(&client->replyq->mutex);
306 debug(DBG_DBG, "tcpserverrd: waiting for writer to end");
307 pthread_join(tcpserverwrth, NULL);
308 debug(DBG_DBG, "tcpserverrd: reader for %s exiting", addr2string(client->addr));
310 void *tcpservernew(void *arg) {
312 struct sockaddr_storage from;
313 socklen_t fromlen = sizeof(from);
314 struct clsrvconf *conf;
315 struct client *client;
318 if (getpeername(s, (struct sockaddr *)&from, &fromlen)) {
319 debug(DBG_DBG, "tcpservernew: getpeername failed, exiting");
322 debug(DBG_WARN, "tcpservernew: incoming TCP connection from %s", addr2string((struct sockaddr *)&from));
324 conf = find_clconf(handle, (struct sockaddr *)&from, NULL);
326 client = addclient(conf, 1);
329 client->addr = addr_copy((struct sockaddr *)&from);
331 removeclient(client);
333 debug(DBG_WARN, "tcpservernew: failed to create new client instance");
335 debug(DBG_WARN, "tcpservernew: ignoring request, no matching TCP client");
338 shutdown(s, SHUT_RDWR);
343 void *tcplistener(void *arg) {
344 pthread_t tcpserverth;
345 int s, *sp = (int *)arg;
346 struct sockaddr_storage from;
347 socklen_t fromlen = sizeof(from);
352 s = accept(*sp, (struct sockaddr *)&from, &fromlen);
354 debug(DBG_WARN, "accept failed");
357 if (pthread_create(&tcpserverth, NULL, tcpservernew, (void *)&s)) {
358 debug(DBG_ERR, "tcplistener: pthread_create failed");
359 shutdown(s, SHUT_RDWR);
363 pthread_detach(tcpserverth);
369 const struct protodefs *tcpinit(uint8_t h) {
374 /* Local Variables: */
375 /* c-file-style: "stroustrup" */