2 * Copyright (C) 2008 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 <openssl/ssl.h>
31 #include "radsecproxy.h"
34 static struct addrinfo *srcres = NULL;
36 void tcpsetsrcres(char *source) {
38 srcres = resolve_hostport_addrinfo(RAD_TCP, source);
41 int tcpconnect(struct server *server, struct timeval *when, int timeout, char *text) {
45 debug(DBG_DBG, "tcpconnect: called from %s", text);
46 pthread_mutex_lock(&server->lock);
47 if (when && memcmp(&server->lastconnecttry, when, sizeof(struct timeval))) {
48 /* already reconnected, nothing to do */
49 debug(DBG_DBG, "tcpconnect(%s): seems already reconnected", text);
50 pthread_mutex_unlock(&server->lock);
55 gettimeofday(&now, NULL);
56 elapsed = now.tv_sec - server->lastconnecttry.tv_sec;
57 if (timeout && server->lastconnecttry.tv_sec && elapsed > timeout) {
58 debug(DBG_DBG, "tcpconnect: timeout");
59 if (server->sock >= 0)
61 pthread_mutex_unlock(&server->lock);
64 if (server->connectionok) {
65 server->connectionok = 0;
67 } else if (elapsed < 1)
69 else if (elapsed < 60) {
70 debug(DBG_INFO, "tcpconnect: sleeping %lds", elapsed);
72 } else if (elapsed < 100000) {
73 debug(DBG_INFO, "tcpconnect: sleeping %ds", 60);
76 server->lastconnecttry.tv_sec = now.tv_sec; /* no sleep at startup */
77 debug(DBG_WARN, "tcpconnect: trying to open TCP connection to %s port %s", server->conf->host, server->conf->port);
78 if (server->sock >= 0)
80 if ((server->sock = connecttcp(server->conf->addrinfo, srcres)) >= 0)
82 debug(DBG_ERR, "tcpconnect: connecttcp failed");
84 debug(DBG_WARN, "tcpconnect: TCP connection to %s port %s up", server->conf->host, server->conf->port);
85 server->connectionok = 1;
86 gettimeofday(&server->lastconnecttry, NULL);
87 pthread_mutex_unlock(&server->lock);
91 /* timeout in seconds, 0 means no timeout (blocking), returns when num bytes have been read, or timeout */
92 /* returns 0 on timeout, -1 on error and num if ok */
93 int tcpreadtimeout(int s, unsigned char *buf, int num, int timeout) {
95 fd_set readfds, writefds;
100 /* make socket non-blocking? */
101 for (len = 0; len < num; len += cnt) {
106 timer.tv_sec = timeout;
109 ndesc = select(s + 1, &readfds, &writefds, NULL, timeout ? &timer : NULL);
113 cnt = read(s, buf + len, num - len);
120 /* timeout in seconds, 0 means no timeout (blocking) */
121 unsigned char *radtcpget(int s, int timeout) {
123 unsigned char buf[4], *rad;
126 cnt = tcpreadtimeout(s, buf, 4, timeout);
128 debug(DBG_DBG, cnt ? "radtcpget: connection lost" : "radtcpget: timeout");
135 debug(DBG_ERR, "radtcpget: malloc failed");
140 cnt = tcpreadtimeout(s, rad + 4, len - 4, timeout);
142 debug(DBG_DBG, cnt ? "radtcpget: connection lost" : "radtcpget: timeout");
151 debug(DBG_WARN, "radtcpget: packet smaller than minimum radius size");
154 debug(DBG_DBG, "radtcpget: got %d bytes", len);
158 int clientradputtcp(struct server *server, unsigned char *rad) {
161 struct clsrvconf *conf = server->conf;
163 if (!server->connectionok)
166 if ((cnt = write(server->sock, rad, len)) <= 0) {
167 debug(DBG_ERR, "clientradputtcp: write error");
170 debug(DBG_DBG, "clientradputtcp: Sent %d bytes, Radius packet of length %d to TCP peer %s", cnt, len, conf->host);
174 void *tcpclientrd(void *arg) {
175 struct server *server = (struct server *)arg;
177 struct timeval lastconnecttry;
180 /* yes, lastconnecttry is really necessary */
181 lastconnecttry = server->lastconnecttry;
182 buf = radtcpget(server->sock, 0);
184 tcpconnect(server, &lastconnecttry, 0, "tcpclientrd");
190 server->clientrdgone = 1;
194 void *tcpserverwr(void *arg) {
196 struct client *client = (struct client *)arg;
197 struct queue *replyq;
198 struct request *reply;
200 debug(DBG_DBG, "tcpserverwr: starting for %s", addr2string(client->addr));
201 replyq = client->replyq;
203 pthread_mutex_lock(&replyq->mutex);
204 while (!list_first(replyq->entries)) {
205 if (client->sock >= 0) {
206 debug(DBG_DBG, "tcpserverwr: waiting for signal");
207 pthread_cond_wait(&replyq->cond, &replyq->mutex);
208 debug(DBG_DBG, "tcpserverwr: got signal");
210 if (client->sock < 0) {
211 /* s might have changed while waiting */
212 pthread_mutex_unlock(&replyq->mutex);
213 debug(DBG_DBG, "tcpserverwr: exiting as requested");
217 reply = (struct request *)list_shift(replyq->entries);
218 pthread_mutex_unlock(&replyq->mutex);
219 cnt = write(client->sock, reply->replybuf, RADLEN(reply->replybuf));
221 debug(DBG_DBG, "tcpserverwr: sent %d bytes, Radius packet of length %d to %s",
222 cnt, RADLEN(reply->replybuf), addr2string(client->addr));
224 debug(DBG_ERR, "tcpserverwr: write error for %s", addr2string(client->addr));
229 void tcpserverrd(struct client *client) {
232 pthread_t tcpserverwrth;
234 debug(DBG_DBG, "tcpserverrd: starting for %s", addr2string(client->addr));
236 if (pthread_create(&tcpserverwrth, NULL, tcpserverwr, (void *)client)) {
237 debug(DBG_ERR, "tcpserverrd: pthread_create failed");
242 buf = radtcpget(client->sock, 0);
244 debug(DBG_ERR, "tcpserverrd: connection from %s lost", addr2string(client->addr));
247 debug(DBG_DBG, "tcpserverrd: got Radius message from %s", addr2string(client->addr));
256 debug(DBG_ERR, "tcpserverrd: message authentication/validation failed, closing connection from %s", addr2string(client->addr));
261 /* stop writer by setting s to -1 and give signal in case waiting for data */
263 pthread_mutex_lock(&client->replyq->mutex);
264 pthread_cond_signal(&client->replyq->cond);
265 pthread_mutex_unlock(&client->replyq->mutex);
266 debug(DBG_DBG, "tcpserverrd: waiting for writer to end");
267 pthread_join(tcpserverwrth, NULL);
268 debug(DBG_DBG, "tcpserverrd: reader for %s exiting", addr2string(client->addr));
270 void *tcpservernew(void *arg) {
272 struct sockaddr_storage from;
273 socklen_t fromlen = sizeof(from);
274 struct clsrvconf *conf;
275 struct client *client;
278 if (getpeername(s, (struct sockaddr *)&from, &fromlen)) {
279 debug(DBG_DBG, "tcpservernew: getpeername failed, exiting");
282 debug(DBG_WARN, "tcpservernew: incoming TCP connection from %s", addr2string((struct sockaddr *)&from));
284 conf = find_clconf(RAD_TCP, (struct sockaddr *)&from, NULL);
286 client = addclient(conf, 1);
289 client->addr = addr_copy((struct sockaddr *)&from);
291 removeclient(client);
293 debug(DBG_WARN, "tcpservernew: failed to create new client instance");
295 debug(DBG_WARN, "tcpservernew: ignoring request, no matching TCP client");
298 shutdown(s, SHUT_RDWR);
303 void *tcplistener(void *arg) {
304 pthread_t tcpserverth;
305 int s, *sp = (int *)arg;
306 struct sockaddr_storage from;
307 socklen_t fromlen = sizeof(from);
312 s = accept(*sp, (struct sockaddr *)&from, &fromlen);
314 debug(DBG_WARN, "accept failed");
317 if (pthread_create(&tcpserverth, NULL, tcpservernew, (void *)&s)) {
318 debug(DBG_ERR, "tcplistener: pthread_create failed");
319 shutdown(s, SHUT_RDWR);
323 pthread_detach(tcpserverth);