cleaning up code
[libradsec.git] / udp.c
diff --git a/udp.c b/udp.c
index 05e7a6b..b0cb464 100644 (file)
--- a/udp.c
+++ b/udp.c
 #include "list.h"
 #include "util.h"
 #include "radsecproxy.h"
-#include "tls.h"
+
+static void setprotoopts(struct commonprotoopts *opts);
+static char **getlistenerargs();
+void *udpserverrd(void *arg);
+int clientradputudp(struct server *server, unsigned char *rad);
+void addclientudp(struct client *client);
+void addserverextraudp(struct clsrvconf *conf);
+void udpsetsrcres();
+void initextraudp();
+
+static const struct protodefs protodefs = {
+    "udp",
+    NULL, /* secretdefault */
+    SOCK_DGRAM, /* socktype */
+    "1812", /* portdefault */
+    REQUEST_RETRY_COUNT, /* retrycountdefault */
+    10, /* retrycountmax */
+    REQUEST_RETRY_INTERVAL, /* retryintervaldefault */
+    60, /* retryintervalmax */
+    DUPLICATE_INTERVAL, /* duplicateintervaldefault */
+    setprotoopts, /* setprotoopts */
+    getlistenerargs, /* getlistenerargs */
+    udpserverrd, /* listener */
+    NULL, /* connecter */
+    NULL, /* clientconnreader */
+    clientradputudp, /* clientradput */
+    addclientudp, /* addclient */
+    addserverextraudp, /* addserverextra */
+    udpsetsrcres, /* setsrcres */
+    initextraudp /* initextra */
+};
 
 static int client4_sock = -1;
 static int client6_sock = -1;
 static struct queue *server_replyq = NULL;
 
+static struct addrinfo *srcres = NULL;
+static uint8_t handle;
+static struct commonprotoopts *protoopts = NULL;
+
+const struct protodefs *udpinit(uint8_t h) {
+    handle = h;
+    return &protodefs;
+}
+
+static void setprotoopts(struct commonprotoopts *opts) {
+    protoopts = opts;
+}
+
+static char **getlistenerargs() {
+    return protoopts ? protoopts->listenargs : NULL;
+}
+
+void udpsetsrcres() {
+    if (!srcres)
+       srcres = resolve_hostport_addrinfo(handle, protoopts ? protoopts->sourcearg : NULL);
+}
+
+void removeudpclientfromreplyq(struct client *c) {
+    struct list_node *n;
+    struct request *r;
+    
+    /* lock the common queue and remove replies for this client */
+    pthread_mutex_lock(&c->replyq->mutex);
+    for (n = list_first(c->replyq->entries); n; n = list_next(n)) {
+       r = (struct request *)n->data;
+       if (r->from == c)
+           r->from = NULL;
+    }
+    pthread_mutex_unlock(&c->replyq->mutex);
+}      
+
 /* exactly one of client and server must be non-NULL */
 /* return who we received from in *client or *server */
 /* return from in sa if not NULL */
@@ -48,6 +114,7 @@ unsigned char *radudpget(int s, struct client **client, struct server **server,
     struct list_node *node;
     fd_set readfds;
     struct client *c = NULL;
+    struct timeval now;
     
     for (;;) {
        if (rad) {
@@ -70,8 +137,8 @@ unsigned char *radudpget(int s, struct client **client, struct server **server,
        }
        
        p = client
-           ? find_clconf(RAD_UDP, (struct sockaddr *)&from, NULL)
-           : find_srvconf(RAD_UDP, (struct sockaddr *)&from, NULL);
+           ? find_clconf(handle, (struct sockaddr *)&from, NULL)
+           : find_srvconf(handle, (struct sockaddr *)&from, NULL);
        if (!p) {
            debug(DBG_WARN, "radudpget: got packet from wrong or unknown UDP peer %s, ignoring", addr2string((struct sockaddr *)&from));
            recv(s, buf, 4, 0);
@@ -103,13 +170,28 @@ unsigned char *radudpget(int s, struct client **client, struct server **server,
            debug(DBG_DBG, "radudpget: packet was padded with %d bytes", cnt - len);
 
        if (client) {
+           *client = NULL;
            pthread_mutex_lock(p->lock);
-           for (node = list_first(p->clients); node; node = list_next(node)) {
+           for (node = list_first(p->clients); node;) {
                c = (struct client *)node->data;
-               if (s == c->sock && addr_equal((struct sockaddr *)&from, c->addr))
-                   break;
+               node = list_next(node);
+               if (s != c->sock)
+                   continue;
+               gettimeofday(&now, NULL);
+               if (!*client && addr_equal((struct sockaddr *)&from, c->addr)) {
+                   c->expiry = now.tv_sec + 60;
+                   *client = c;
+               }
+               if (c->expiry >= now.tv_sec)
+                   continue;
+               
+               debug(DBG_DBG, "radudpget: removing expired client (%s)", addr2string(c->addr));
+               removeudpclientfromreplyq(c);
+               c->replyq = NULL; /* stop removeclient() from removing common udp replyq */
+               removelockedclient(c);
+               break;
            }
-           if (!node) {
+           if (!*client) {
                fromcopy = addr_copy((struct sockaddr *)&from);
                if (!fromcopy) {
                    pthread_mutex_unlock(p->lock);
@@ -123,8 +205,10 @@ unsigned char *radudpget(int s, struct client **client, struct server **server,
                }
                c->sock = s;
                c->addr = fromcopy;
+               gettimeofday(&now, NULL);
+               c->expiry = now.tv_sec + 60;
+               *client = c;
            }
-           *client = c;
            pthread_mutex_unlock(p->lock);
        } else if (server)
            *server = p->servers;
@@ -137,23 +221,11 @@ unsigned char *radudpget(int s, struct client **client, struct server **server,
 
 int clientradputudp(struct server *server, unsigned char *rad) {
     size_t len;
-    struct sockaddr_storage sa;
-    struct sockaddr *sap;
     struct clsrvconf *conf = server->conf;
-    uint16_t port;
     
     len = RADLEN(rad);
-    port = port_get(conf->addrinfo->ai_addr);
-    
-    if (*rad == RAD_Accounting_Request) {
-       sap = (struct sockaddr *)&sa;
-       memcpy(sap, conf->addrinfo->ai_addr, conf->addrinfo->ai_addrlen);
-       port_set(sap, ++port);
-    } else
-       sap = conf->addrinfo->ai_addr;
-
-    if (sendto(server->sock, rad, len, 0, sap, conf->addrinfo->ai_addrlen) >= 0) {
-       debug(DBG_DBG, "clienradputudp: sent UDP of length %d to %s port %d", len, conf->host, port);
+    if (sendto(server->sock, rad, len, 0, conf->addrinfo->ai_addr, conf->addrinfo->ai_addrlen) >= 0) {
+       debug(DBG_DBG, "clienradputudp: sent UDP of length %d to %s port %d", len, conf->host, port_get(conf->addrinfo->ai_addr));
        return 1;
     }
 
@@ -202,12 +274,16 @@ void *udpserverwr(void *arg) {
            pthread_cond_wait(&replyq->cond, &replyq->mutex);
            debug(DBG_DBG, "udp server writer, got signal");
        }
+       /* do this with lock, udpserverrd may set from = NULL if from expires */
+       if (reply->from)
+           memcpy(&to, reply->from->addr, SOCKADDRP_SIZE(reply->from->addr));
        pthread_mutex_unlock(&replyq->mutex);
-
-       memcpy(&to, reply->from->addr, SOCKADDRP_SIZE(reply->from->addr));
-       port_set((struct sockaddr *)&to, reply->udpport);
-       if (sendto(reply->udpsock, reply->replybuf, RADLEN(reply->replybuf), 0, (struct sockaddr *)&to, SOCKADDR_SIZE(to)) < 0)
-           debug(DBG_WARN, "udpserverwr: send failed");
+       if (reply->from) {
+           port_set((struct sockaddr *)&to, reply->udpport);
+           if (sendto(reply->udpsock, reply->replybuf, RADLEN(reply->replybuf), 0, (struct sockaddr *)&to, SOCKADDR_SIZE(to)) < 0)
+               debug(DBG_WARN, "udpserverwr: send failed");
+       }
+       debug(DBG_DBG, "udpserverwr: refcount %d", reply->refcount);
        freerq(reply);
     }
 }
@@ -220,7 +296,7 @@ void addserverextraudp(struct clsrvconf *conf) {
     switch (conf->addrinfo->ai_family) {
     case AF_INET:
        if (client4_sock < 0) {
-           client4_sock = bindtoaddr(getsrcprotores(RAD_UDP), AF_INET, 0, 1);
+           client4_sock = bindtoaddr(srcres, AF_INET, 0, 1);
            if (client4_sock < 0)
                debugx(1, DBG_ERR, "addserver: failed to create client socket for server %s", conf->host);
        }
@@ -228,7 +304,7 @@ void addserverextraudp(struct clsrvconf *conf) {
        break;
     case AF_INET6:
        if (client6_sock < 0) {
-           client6_sock = bindtoaddr(getsrcprotores(RAD_UDP), AF_INET6, 0, 1);
+           client6_sock = bindtoaddr(srcres, AF_INET6, 0, 1);
            if (client6_sock < 0)
                debugx(1, DBG_ERR, "addserver: failed to create client socket for server %s", conf->host);
        }
@@ -241,6 +317,11 @@ void addserverextraudp(struct clsrvconf *conf) {
 
 void initextraudp() {
     pthread_t cl4th, cl6th, srvth;
+
+    if (srcres) {
+       freeaddrinfo(srcres);
+       srcres = NULL;
+    }
     
     if (client4_sock >= 0)
        if (pthread_create(&cl4th, NULL, udpclientrd, (void *)&client4_sock))
@@ -249,7 +330,7 @@ void initextraudp() {
        if (pthread_create(&cl6th, NULL, udpclientrd, (void *)&client6_sock))
            debugx(1, DBG_ERR, "pthread_create failed");
 
-    if (find_clconf_type(RAD_UDP, NULL)) {
+    if (find_clconf_type(handle, NULL)) {
        server_replyq = newqueue();
        if (pthread_create(&srvth, NULL, udpserverwr, (void *)server_replyq))
            debugx(1, DBG_ERR, "pthread_create failed");