radsecproxy-1.6.5.
[radsecproxy.git] / tcp.c
diff --git a/tcp.c b/tcp.c
index cddd554..6de39fc 100644 (file)
--- a/tcp.c
+++ b/tcp.c
@@ -1,12 +1,11 @@
 /*
- * Copyright (C) 2008 Stig Venaas <venaas@uninett.no>
+ * Copyright (C) 2008-2009 Stig Venaas <venaas@uninett.no>
  *
  * Permission to use, copy, modify, and distribute this software for any
  * purpose with or without fee is hereby granted, provided that the above
  * copyright notice and this permission notice appear in all copies.
  */
 
-#ifdef RADPROT_TCP
 #include <signal.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
 #include <regex.h>
 #include <pthread.h>
-#include <openssl/ssl.h>
-#include "debug.h"
-#include "list.h"
-#include "util.h"
 #include "radsecproxy.h"
+#include "hostport.h"
 
+#ifdef RADPROT_TCP
+#include "debug.h"
+#include "util.h"
 static void setprotoopts(struct commonprotoopts *opts);
 static char **getlistenerargs();
 void *tcplistener(void *arg);
@@ -79,13 +78,15 @@ static char **getlistenerargs() {
 
 void tcpsetsrcres() {
     if (!srcres)
-       srcres = resolve_hostport_addrinfo(handle, protoopts ? protoopts->sourcearg : NULL);
+       srcres =
+            resolvepassiveaddrinfo(protoopts ? protoopts->sourcearg : NULL,
+                                   AF_UNSPEC, NULL, protodefs.socktype);
 }
-    
+
 int tcpconnect(struct server *server, struct timeval *when, int timeout, char *text) {
     struct timeval now;
     time_t elapsed;
-    
+
     debug(DBG_DBG, "tcpconnect: called from %s", text);
     pthread_mutex_lock(&server->lock);
     if (when && memcmp(&server->lastconnecttry, when, sizeof(struct timeval))) {
@@ -118,14 +119,12 @@ int tcpconnect(struct server *server, struct timeval *when, int timeout, char *t
            sleep(60);
        } else
            server->lastconnecttry.tv_sec = now.tv_sec;  /* no sleep at startup */
-       debug(DBG_WARN, "tcpconnect: trying to open TCP connection to %s port %s", server->conf->host, server->conf->port);
+
        if (server->sock >= 0)
            close(server->sock);
-       if ((server->sock = connecttcp(server->conf->addrinfo, srcres)) >= 0)
+       if ((server->sock = connecttcphostlist(server->conf->hostports, srcres)) >= 0)
            break;
-       debug(DBG_ERR, "tcpconnect: connecttcp failed");
     }
-    debug(DBG_WARN, "tcpconnect: TCP connection to %s port %s up", server->conf->host, server->conf->port);
     server->connectionok = 1;
     gettimeofday(&server->lastconnecttry, NULL);
     pthread_mutex_unlock(&server->lock);
@@ -138,7 +137,7 @@ int tcpreadtimeout(int s, unsigned char *buf, int num, int timeout) {
     int ndesc, cnt, len;
     fd_set readfds, writefds;
     struct timeval timer;
-    
+
     if (s < 0)
        return -1;
     /* make socket non-blocking? */
@@ -180,21 +179,21 @@ unsigned char *radtcpget(int s, int timeout) {
            continue;
        }
        memcpy(rad, buf, 4);
-       
+
        cnt = tcpreadtimeout(s, rad + 4, len - 4, timeout);
        if (cnt < 1) {
            debug(DBG_DBG, cnt ? "radtcpget: connection lost" : "radtcpget: timeout");
            free(rad);
            return NULL;
        }
-       
+
        if (len >= 20)
            break;
-       
+
        free(rad);
        debug(DBG_WARN, "radtcpget: packet smaller than minimum radius size");
     }
-    
+
     debug(DBG_DBG, "radtcpget: got %d bytes", len);
     return rad;
 }
@@ -211,7 +210,7 @@ int clientradputtcp(struct server *server, unsigned char *rad) {
        debug(DBG_ERR, "clientradputtcp: write error");
        return 0;
     }
-    debug(DBG_DBG, "clientradputtcp: Sent %d bytes, Radius packet of length %d to TCP peer %s", cnt, len, conf->host);
+    debug(DBG_DBG, "clientradputtcp: Sent %d bytes, Radius packet of length %d to TCP peer %s", cnt, len, conf->name);
     return 1;
 }
 
@@ -219,7 +218,7 @@ void *tcpclientrd(void *arg) {
     struct server *server = (struct server *)arg;
     unsigned char *buf;
     struct timeval lastconnecttry;
-    
+
     for (;;) {
        /* yes, lastconnecttry is really necessary */
        lastconnecttry = server->lastconnecttry;
@@ -238,15 +237,15 @@ void *tcpclientrd(void *arg) {
 void *tcpserverwr(void *arg) {
     int cnt;
     struct client *client = (struct client *)arg;
-    struct queue *replyq;
+    struct gqueue *replyq;
     struct request *reply;
-    
+
     debug(DBG_DBG, "tcpserverwr: starting for %s", addr2string(client->addr));
     replyq = client->replyq;
     for (;;) {
        pthread_mutex_lock(&replyq->mutex);
        while (!list_first(replyq->entries)) {
-           if (client->sock >= 0) {        
+           if (client->sock >= 0) {
                debug(DBG_DBG, "tcpserverwr: waiting for signal");
                pthread_cond_wait(&replyq->cond, &replyq->mutex);
                debug(DBG_DBG, "tcpserverwr: got signal");
@@ -274,10 +273,10 @@ void tcpserverrd(struct client *client) {
     struct request *rq;
     uint8_t *buf;
     pthread_t tcpserverwrth;
-    
+
     debug(DBG_DBG, "tcpserverrd: starting for %s", addr2string(client->addr));
-    
-    if (pthread_create(&tcpserverwrth, NULL, tcpserverwr, (void *)client)) {
+
+    if (pthread_create(&tcpserverwrth, &pthread_attr, tcpserverwr, (void *)client)) {
        debug(DBG_ERR, "tcpserverrd: pthread_create failed");
        return;
     }
@@ -338,7 +337,7 @@ void *tcpservernew(void *arg) {
     } else
        debug(DBG_WARN, "tcpservernew: ignoring request, no matching TCP client");
 
- exit:
+exit:
     shutdown(s, SHUT_RDWR);
     close(s);
     pthread_exit(NULL);
@@ -358,7 +357,7 @@ void *tcplistener(void *arg) {
            debug(DBG_WARN, "accept failed");
            continue;
        }
-       if (pthread_create(&tcpserverth, NULL, tcpservernew, (void *)&s)) {
+       if (pthread_create(&tcpserverth, &pthread_attr, tcpservernew, (void *)&s)) {
            debug(DBG_ERR, "tcplistener: pthread_create failed");
            shutdown(s, SHUT_RDWR);
            close(s);
@@ -374,3 +373,7 @@ const struct protodefs *tcpinit(uint8_t h) {
     return NULL;
 }
 #endif
+
+/* Local Variables: */
+/* c-file-style: "stroustrup" */
+/* End: */