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 int tcpconnect(struct server *server, struct timeval *when, int timeout, char *text) {
38 debug(DBG_DBG, "tcpconnect: called from %s", text);
39 pthread_mutex_lock(&server->lock);
40 if (when && memcmp(&server->lastconnecttry, when, sizeof(struct timeval))) {
41 /* already reconnected, nothing to do */
42 debug(DBG_DBG, "tcpconnect(%s): seems already reconnected", text);
43 pthread_mutex_unlock(&server->lock);
48 gettimeofday(&now, NULL);
49 elapsed = now.tv_sec - server->lastconnecttry.tv_sec;
50 if (timeout && server->lastconnecttry.tv_sec && elapsed > timeout) {
51 debug(DBG_DBG, "tcpconnect: timeout");
52 if (server->sock >= 0)
54 pthread_mutex_unlock(&server->lock);
57 if (server->connectionok) {
58 server->connectionok = 0;
60 } else if (elapsed < 1)
62 else if (elapsed < 60) {
63 debug(DBG_INFO, "tcpconnect: sleeping %lds", elapsed);
65 } else if (elapsed < 100000) {
66 debug(DBG_INFO, "tcpconnect: sleeping %ds", 60);
69 server->lastconnecttry.tv_sec = now.tv_sec; /* no sleep at startup */
70 debug(DBG_WARN, "tcpconnect: trying to open TCP connection to %s port %s", server->conf->host, server->conf->port);
71 if (server->sock >= 0)
73 if ((server->sock = connecttcp(server->conf->addrinfo, getsrcprotores(RAD_TCP))) >= 0)
75 debug(DBG_ERR, "tcpconnect: connecttcp failed");
77 debug(DBG_WARN, "tcpconnect: TCP connection to %s port %s up", server->conf->host, server->conf->port);
78 server->connectionok = 1;
79 gettimeofday(&server->lastconnecttry, NULL);
80 pthread_mutex_unlock(&server->lock);
84 /* timeout in seconds, 0 means no timeout (blocking), returns when num bytes have been read, or timeout */
85 /* returns 0 on timeout, -1 on error and num if ok */
86 int tcpreadtimeout(int s, unsigned char *buf, int num, int timeout) {
88 fd_set readfds, writefds;
93 /* make socket non-blocking? */
94 for (len = 0; len < num; len += cnt) {
99 timer.tv_sec = timeout;
102 ndesc = select(s + 1, &readfds, &writefds, NULL, timeout ? &timer : NULL);
106 cnt = read(s, buf + len, num - len);
113 /* timeout in seconds, 0 means no timeout (blocking) */
114 unsigned char *radtcpget(int s, int timeout) {
116 unsigned char buf[4], *rad;
119 cnt = tcpreadtimeout(s, buf, 4, timeout);
121 debug(DBG_DBG, cnt ? "radtcpget: connection lost" : "radtcpget: timeout");
128 debug(DBG_ERR, "radtcpget: malloc failed");
133 cnt = tcpreadtimeout(s, rad + 4, len - 4, timeout);
135 debug(DBG_DBG, cnt ? "radtcpget: connection lost" : "radtcpget: timeout");
144 debug(DBG_WARN, "radtcpget: packet smaller than minimum radius size");
147 debug(DBG_DBG, "radtcpget: got %d bytes", len);
151 int clientradputtcp(struct server *server, unsigned char *rad) {
154 struct clsrvconf *conf = server->conf;
156 if (!server->connectionok)
159 if ((cnt = write(server->sock, rad, len)) <= 0) {
160 debug(DBG_ERR, "clientradputtcp: write error");
163 debug(DBG_DBG, "clientradputtcp: Sent %d bytes, Radius packet of length %d to TCP peer %s", cnt, len, conf->host);
167 void *tcpclientrd(void *arg) {
168 struct server *server = (struct server *)arg;
170 struct timeval lastconnecttry;
173 /* yes, lastconnecttry is really necessary */
174 lastconnecttry = server->lastconnecttry;
175 buf = radtcpget(server->sock, 0);
177 tcpconnect(server, &lastconnecttry, 0, "tcpclientrd");
183 server->clientrdgone = 1;
187 void *tcpserverwr(void *arg) {
189 struct client *client = (struct client *)arg;
190 struct queue *replyq;
191 struct request *reply;
193 debug(DBG_DBG, "tcpserverwr: starting for %s", addr2string(client->addr));
194 replyq = client->replyq;
196 pthread_mutex_lock(&replyq->mutex);
197 while (!list_first(replyq->entries)) {
198 if (client->sock >= 0) {
199 debug(DBG_DBG, "tcpserverwr: waiting for signal");
200 pthread_cond_wait(&replyq->cond, &replyq->mutex);
201 debug(DBG_DBG, "tcpserverwr: got signal");
203 if (client->sock < 0) {
204 /* s might have changed while waiting */
205 pthread_mutex_unlock(&replyq->mutex);
206 debug(DBG_DBG, "tcpserverwr: exiting as requested");
210 reply = (struct request *)list_shift(replyq->entries);
211 pthread_mutex_unlock(&replyq->mutex);
212 cnt = write(client->sock, reply->replybuf, RADLEN(reply->replybuf));
214 debug(DBG_DBG, "tcpserverwr: sent %d bytes, Radius packet of length %d to %s",
215 cnt, RADLEN(reply->replybuf), addr2string(client->addr));
217 debug(DBG_ERR, "tcpserverwr: write error for %s", addr2string(client->addr));
222 void tcpserverrd(struct client *client) {
225 pthread_t tcpserverwrth;
227 debug(DBG_DBG, "tcpserverrd: starting for %s", addr2string(client->addr));
229 if (pthread_create(&tcpserverwrth, NULL, tcpserverwr, (void *)client)) {
230 debug(DBG_ERR, "tcpserverrd: pthread_create failed");
235 buf = radtcpget(client->sock, 0);
237 debug(DBG_ERR, "tcpserverrd: connection from %s lost", addr2string(client->addr));
240 debug(DBG_DBG, "tcpserverrd: got Radius message from %s", addr2string(client->addr));
249 debug(DBG_ERR, "tcpserverrd: message authentication/validation failed, closing connection from %s", addr2string(client->addr));
254 /* stop writer by setting s to -1 and give signal in case waiting for data */
256 pthread_mutex_lock(&client->replyq->mutex);
257 pthread_cond_signal(&client->replyq->cond);
258 pthread_mutex_unlock(&client->replyq->mutex);
259 debug(DBG_DBG, "tcpserverrd: waiting for writer to end");
260 pthread_join(tcpserverwrth, NULL);
261 debug(DBG_DBG, "tcpserverrd: reader for %s exiting", addr2string(client->addr));
263 void *tcpservernew(void *arg) {
265 struct sockaddr_storage from;
266 socklen_t fromlen = sizeof(from);
267 struct clsrvconf *conf;
268 struct client *client;
271 if (getpeername(s, (struct sockaddr *)&from, &fromlen)) {
272 debug(DBG_DBG, "tcpservernew: getpeername failed, exiting");
275 debug(DBG_WARN, "tcpservernew: incoming TCP connection from %s", addr2string((struct sockaddr *)&from));
277 conf = find_clconf(RAD_TCP, (struct sockaddr *)&from, NULL);
279 client = addclient(conf, 1);
282 client->addr = addr_copy((struct sockaddr *)&from);
284 removeclient(client);
286 debug(DBG_WARN, "tcpservernew: failed to create new client instance");
288 debug(DBG_WARN, "tcpservernew: ignoring request, no matching TCP client");
291 shutdown(s, SHUT_RDWR);
296 void *tcplistener(void *arg) {
297 pthread_t tcpserverth;
298 int s, *sp = (int *)arg;
299 struct sockaddr_storage from;
300 socklen_t fromlen = sizeof(from);
305 s = accept(*sp, (struct sockaddr *)&from, &fromlen);
307 debug(DBG_WARN, "accept failed");
310 if (pthread_create(&tcpserverth, NULL, tcpservernew, (void *)&s)) {
311 debug(DBG_ERR, "tcplistener: pthread_create failed");
312 shutdown(s, SHUT_RDWR);
316 pthread_detach(tcpserverth);