moved more code to tlscommon, fixed a couple of minor lint warnings
[radsecproxy.git] / tcp.c
diff --git a/tcp.c b/tcp.c
index 0d1da1c..c0a266c 100644 (file)
--- a/tcp.c
+++ b/tcp.c
 #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 "tcp.h"
 
+#ifdef RADPROT_TCP
+#include "debug.h"
+#include "util.h"
+static void setprotoopts(struct commonprotoopts *opts);
+static char **getlistenerargs();
+void *tcplistener(void *arg);
+int tcpconnect(struct server *server, struct timeval *when, int timeout, char * text);
+void *tcpclientrd(void *arg);
+int clientradputtcp(struct server *server, unsigned char *rad);
+void tcpsetsrcres();
+
+static const struct protodefs protodefs = {
+    "tcp",
+    NULL, /* secretdefault */
+    SOCK_STREAM, /* socktype */
+    "1812", /* portdefault */
+    0, /* retrycountdefault */
+    0, /* retrycountmax */
+    REQUEST_RETRY_INTERVAL * REQUEST_RETRY_COUNT, /* retryintervaldefault */
+    60, /* retryintervalmax */
+    DUPLICATE_INTERVAL, /* duplicateintervaldefault */
+    setprotoopts, /* setprotoopts */
+    getlistenerargs, /* getlistenerargs */
+    tcplistener, /* listener */
+    tcpconnect, /* connecter */
+    tcpclientrd, /* clientconnreader */
+    clientradputtcp, /* clientradput */
+    NULL, /* addclient */
+    NULL, /* addserverextra */
+    tcpsetsrcres, /* setsrcres */
+    NULL /* initextra */
+};
+
+static struct addrinfo *srcres = NULL;
+static uint8_t handle;
+static struct commonprotoopts *protoopts = NULL;
+const struct protodefs *tcpinit(uint8_t h) {
+    handle = h;
+    return &protodefs;
+}
+
+static void setprotoopts(struct commonprotoopts *opts) {
+    protoopts = opts;
+}
+
+static char **getlistenerargs() {
+    return protoopts ? protoopts->listenargs : NULL;
+}
+
+void tcpsetsrcres() {
+    if (!srcres)
+       srcres = resolve_hostport_addrinfo(handle, protoopts ? protoopts->sourcearg : NULL);
+}
+    
 int tcpconnect(struct server *server, struct timeval *when, int timeout, char *text) {
     struct timeval now;
     time_t elapsed;
@@ -70,11 +120,12 @@ int tcpconnect(struct server *server, struct timeval *when, int timeout, char *t
        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, getsrcprotores(RAD_TCP))) >= 0)
+       if ((server->sock = connecttcp(server->conf->addrinfo, 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);
     return 1;
@@ -150,18 +201,15 @@ unsigned char *radtcpget(int s, int timeout) {
 int clientradputtcp(struct server *server, unsigned char *rad) {
     int cnt;
     size_t len;
-    struct timeval lastconnecttry;
     struct clsrvconf *conf = server->conf;
-    
+
+    if (!server->connectionok)
+       return 0;
     len = RADLEN(rad);
-    lastconnecttry = server->lastconnecttry;
-    while ((cnt = write(server->sock, rad, len)) <= 0) {
+    if ((cnt = write(server->sock, rad, len)) <= 0) {
        debug(DBG_ERR, "clientradputtcp: write error");
-       tcpconnect(server, &lastconnecttry, 0, "clientradputtcp");
-       lastconnecttry = server->lastconnecttry;
+       return 0;
     }
-
-    server->connectionok = 1;
     debug(DBG_DBG, "clientradputtcp: Sent %d bytes, Radius packet of length %d to TCP peer %s", cnt, len, conf->host);
     return 1;
 }
@@ -260,13 +308,12 @@ void tcpserverrd(struct client *client) {
     pthread_mutex_unlock(&client->replyq->mutex);
     debug(DBG_DBG, "tcpserverrd: waiting for writer to end");
     pthread_join(tcpserverwrth, NULL);
-    removeclientrqs(client);
     debug(DBG_DBG, "tcpserverrd: reader for %s exiting", addr2string(client->addr));
 }
 void *tcpservernew(void *arg) {
     int s;
     struct sockaddr_storage from;
-    size_t fromlen = sizeof(from);
+    socklen_t fromlen = sizeof(from);
     struct clsrvconf *conf;
     struct client *client;
 
@@ -277,7 +324,7 @@ void *tcpservernew(void *arg) {
     }
     debug(DBG_WARN, "tcpservernew: incoming TCP connection from %s", addr2string((struct sockaddr *)&from));
 
-    conf = find_clconf(RAD_TCP, (struct sockaddr *)&from, NULL);
+    conf = find_clconf(handle, (struct sockaddr *)&from, NULL);
     if (conf) {
        client = addclient(conf, 1);
        if (client) {
@@ -300,7 +347,7 @@ void *tcplistener(void *arg) {
     pthread_t tcpserverth;
     int s, *sp = (int *)arg;
     struct sockaddr_storage from;
-    size_t fromlen = sizeof(from);
+    socklen_t fromlen = sizeof(from);
 
     listen(*sp, 0);
 
@@ -321,3 +368,8 @@ void *tcplistener(void *arg) {
     free(sp);
     return NULL;
 }
+#else
+const struct protodefs *tcpinit(uint8_t h) {
+    return NULL;
+}
+#endif