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>
29 #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 = resolvepassiveaddrinfo(protoopts ? protoopts->sourcearg : NULL, NULL, protodefs.socktype);
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 */
122 if (server->sock >= 0)
124 if ((server->sock = connecttcphostlist(server->conf->hostports, srcres)) >= 0)
127 server->connectionok = 1;
128 gettimeofday(&server->lastconnecttry, NULL);
129 pthread_mutex_unlock(&server->lock);
133 /* timeout in seconds, 0 means no timeout (blocking), returns when num bytes have been read, or timeout */
134 /* returns 0 on timeout, -1 on error and num if ok */
135 int tcpreadtimeout(int s, unsigned char *buf, int num, int timeout) {
137 fd_set readfds, writefds;
138 struct timeval timer;
142 /* make socket non-blocking? */
143 for (len = 0; len < num; len += cnt) {
148 timer.tv_sec = timeout;
151 ndesc = select(s + 1, &readfds, &writefds, NULL, timeout ? &timer : NULL);
155 cnt = read(s, buf + len, num - len);
162 /* timeout in seconds, 0 means no timeout (blocking) */
163 unsigned char *radtcpget(int s, int timeout) {
165 unsigned char buf[4], *rad;
168 cnt = tcpreadtimeout(s, buf, 4, timeout);
170 debug(DBG_DBG, cnt ? "radtcpget: connection lost" : "radtcpget: timeout");
177 debug(DBG_ERR, "radtcpget: malloc failed");
182 cnt = tcpreadtimeout(s, rad + 4, len - 4, timeout);
184 debug(DBG_DBG, cnt ? "radtcpget: connection lost" : "radtcpget: timeout");
193 debug(DBG_WARN, "radtcpget: packet smaller than minimum radius size");
196 debug(DBG_DBG, "radtcpget: got %d bytes", len);
200 int clientradputtcp(struct server *server, unsigned char *rad) {
203 struct clsrvconf *conf = server->conf;
205 if (!server->connectionok)
208 if ((cnt = write(server->sock, rad, len)) <= 0) {
209 debug(DBG_ERR, "clientradputtcp: write error");
212 debug(DBG_DBG, "clientradputtcp: Sent %d bytes, Radius packet of length %d to TCP peer %s", cnt, len, conf->name);
216 void *tcpclientrd(void *arg) {
217 struct server *server = (struct server *)arg;
219 struct timeval lastconnecttry;
222 /* yes, lastconnecttry is really necessary */
223 lastconnecttry = server->lastconnecttry;
224 buf = radtcpget(server->sock, 0);
226 tcpconnect(server, &lastconnecttry, 0, "tcpclientrd");
232 server->clientrdgone = 1;
236 void *tcpserverwr(void *arg) {
238 struct client *client = (struct client *)arg;
239 struct gqueue *replyq;
240 struct request *reply;
242 debug(DBG_DBG, "tcpserverwr: starting for %s", addr2string(client->addr));
243 replyq = client->replyq;
245 pthread_mutex_lock(&replyq->mutex);
246 while (!list_first(replyq->entries)) {
247 if (client->sock >= 0) {
248 debug(DBG_DBG, "tcpserverwr: waiting for signal");
249 pthread_cond_wait(&replyq->cond, &replyq->mutex);
250 debug(DBG_DBG, "tcpserverwr: got signal");
252 if (client->sock < 0) {
253 /* s might have changed while waiting */
254 pthread_mutex_unlock(&replyq->mutex);
255 debug(DBG_DBG, "tcpserverwr: exiting as requested");
259 reply = (struct request *)list_shift(replyq->entries);
260 pthread_mutex_unlock(&replyq->mutex);
261 cnt = write(client->sock, reply->replybuf, RADLEN(reply->replybuf));
263 debug(DBG_DBG, "tcpserverwr: sent %d bytes, Radius packet of length %d to %s",
264 cnt, RADLEN(reply->replybuf), addr2string(client->addr));
266 debug(DBG_ERR, "tcpserverwr: write error for %s", addr2string(client->addr));
271 void tcpserverrd(struct client *client) {
274 pthread_t tcpserverwrth;
276 debug(DBG_DBG, "tcpserverrd: starting for %s", addr2string(client->addr));
278 if (pthread_create(&tcpserverwrth, NULL, tcpserverwr, (void *)client)) {
279 debug(DBG_ERR, "tcpserverrd: pthread_create failed");
284 buf = radtcpget(client->sock, 0);
286 debug(DBG_ERR, "tcpserverrd: connection from %s lost", addr2string(client->addr));
289 debug(DBG_DBG, "tcpserverrd: got Radius message from %s", addr2string(client->addr));
298 debug(DBG_ERR, "tcpserverrd: message authentication/validation failed, closing connection from %s", addr2string(client->addr));
303 /* stop writer by setting s to -1 and give signal in case waiting for data */
305 pthread_mutex_lock(&client->replyq->mutex);
306 pthread_cond_signal(&client->replyq->cond);
307 pthread_mutex_unlock(&client->replyq->mutex);
308 debug(DBG_DBG, "tcpserverrd: waiting for writer to end");
309 pthread_join(tcpserverwrth, NULL);
310 debug(DBG_DBG, "tcpserverrd: reader for %s exiting", addr2string(client->addr));
312 void *tcpservernew(void *arg) {
314 struct sockaddr_storage from;
315 socklen_t fromlen = sizeof(from);
316 struct clsrvconf *conf;
317 struct client *client;
320 if (getpeername(s, (struct sockaddr *)&from, &fromlen)) {
321 debug(DBG_DBG, "tcpservernew: getpeername failed, exiting");
324 debug(DBG_WARN, "tcpservernew: incoming TCP connection from %s", addr2string((struct sockaddr *)&from));
326 conf = find_clconf(handle, (struct sockaddr *)&from, NULL);
328 client = addclient(conf, 1);
331 client->addr = addr_copy((struct sockaddr *)&from);
333 removeclient(client);
335 debug(DBG_WARN, "tcpservernew: failed to create new client instance");
337 debug(DBG_WARN, "tcpservernew: ignoring request, no matching TCP client");
340 shutdown(s, SHUT_RDWR);
345 void *tcplistener(void *arg) {
346 pthread_t tcpserverth;
347 int s, *sp = (int *)arg;
348 struct sockaddr_storage from;
349 socklen_t fromlen = sizeof(from);
354 s = accept(*sp, (struct sockaddr *)&from, &fromlen);
356 debug(DBG_WARN, "accept failed");
359 if (pthread_create(&tcpserverth, NULL, tcpservernew, (void *)&s)) {
360 debug(DBG_ERR, "tcplistener: pthread_create failed");
361 shutdown(s, SHUT_RDWR);
365 pthread_detach(tcpserverth);
371 const struct protodefs *tcpinit(uint8_t h) {