cleaning up code
[radsecproxy.git] / udp.c
diff --git a/udp.c b/udp.c
index 3079a68..6286392 100644 (file)
--- a/udp.c
+++ b/udp.c
@@ -35,10 +35,31 @@ static int client4_sock = -1;
 static int client6_sock = -1;
 static struct queue *server_replyq = NULL;
 
+static struct addrinfo *srcres = NULL;
+
+void udpsetsrcres(char *source) {
+    if (!srcres)
+       srcres = resolve_hostport_addrinfo(RAD_UDP, source);
+}
+
+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 */
-unsigned char *radudpget(int s, struct client **client, struct server **server, struct sockaddr_storage *sa) {
+unsigned char *radudpget(int s, struct client **client, struct server **server, uint16_t *port) {
     int cnt, len;
     unsigned char buf[4], *rad = NULL;
     struct sockaddr_storage from;
@@ -47,6 +68,8 @@ unsigned char *radudpget(int s, struct client **client, struct server **server,
     struct clsrvconf *p;
     struct list_node *node;
     fd_set readfds;
+    struct client *c = NULL;
+    struct timeval now;
     
     for (;;) {
        if (rad) {
@@ -72,7 +95,7 @@ unsigned char *radudpget(int s, struct client **client, struct server **server,
            ? find_clconf(RAD_UDP, (struct sockaddr *)&from, NULL)
            : find_srvconf(RAD_UDP, (struct sockaddr *)&from, NULL);
        if (!p) {
-           debug(DBG_WARN, "radudpget: got packet from wrong or unknown UDP peer %s, ignoring", addr2string((struct sockaddr *)&from, fromlen));
+           debug(DBG_WARN, "radudpget: got packet from wrong or unknown UDP peer %s, ignoring", addr2string((struct sockaddr *)&from));
            recv(s, buf, 4, 0);
            continue;
        }
@@ -92,7 +115,7 @@ unsigned char *radudpget(int s, struct client **client, struct server **server,
        }
        
        cnt = recv(s, rad, len, MSG_TRUNC);
-       debug(DBG_DBG, "radudpget: got %d bytes from %s", cnt, addr2string((struct sockaddr *)&from, fromlen));
+       debug(DBG_DBG, "radudpget: got %d bytes from %s", cnt, addr2string((struct sockaddr *)&from));
 
        if (cnt < len) {
            debug(DBG_WARN, "radudpget: packet smaller than length field in radius header");
@@ -102,68 +125,62 @@ 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))
-               if (addr_equal((struct sockaddr *)&from, ((struct client *)node->data)->addr))
-                   break;
-           if (node) {
-               *client = (struct client *)node->data;
-               pthread_mutex_unlock(p->lock);
+           for (node = list_first(p->clients); node;) {
+               c = (struct client *)node->data;
+               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;
            }
-           fromcopy = addr_copy((struct sockaddr *)&from);
-           if (!fromcopy) {
-               pthread_mutex_unlock(p->lock);
-               continue;
-           }
-           *client = addclient(p, 0);
            if (!*client) {
-               free(fromcopy);
-               pthread_mutex_unlock(p->lock);
-               continue;
+               fromcopy = addr_copy((struct sockaddr *)&from);
+               if (!fromcopy) {
+                   pthread_mutex_unlock(p->lock);
+                   continue;
+               }
+               c = addclient(p, 0);
+               if (!c) {
+                   free(fromcopy);
+                   pthread_mutex_unlock(p->lock);
+                   continue;
+               }
+               c->sock = s;
+               c->addr = fromcopy;
+               gettimeofday(&now, NULL);
+               c->expiry = now.tv_sec + 60;
+               *client = c;
            }
-           (*client)->addr = fromcopy;
            pthread_mutex_unlock(p->lock);
        } else if (server)
            *server = p->servers;
        break;
     }
-    if (sa)
-       *sa = from;
+    if (port)
+       *port = port_get((struct sockaddr *)&from);
     return rad;
 }
 
 int clientradputudp(struct server *server, unsigned char *rad) {
     size_t len;
-    struct sockaddr_storage sa;
-    struct sockaddr *sap;
     struct clsrvconf *conf = server->conf;
-    in_port_t *port = NULL;
     
     len = RADLEN(rad);
-    
-    if (*rad == RAD_Accounting_Request) {
-       sap = (struct sockaddr *)&sa;
-       memcpy(sap, conf->addrinfo->ai_addr, conf->addrinfo->ai_addrlen);
-    } else
-       sap = conf->addrinfo->ai_addr;
-    
-    switch (sap->sa_family) {
-    case AF_INET:
-       port = &((struct sockaddr_in *)sap)->sin_port;
-       break;
-    case AF_INET6:
-       port = &((struct sockaddr_in6 *)sap)->sin6_port;
-       break;
-    default:
-       return 0;
-    }
-
-    if (*rad == RAD_Accounting_Request)
-       *port = htons(ntohs(*port) + 1);
-    
-    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, ntohs(*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;
     }
 
@@ -193,32 +210,36 @@ void *udpserverrd(void *arg) {
            sleep(5); /* malloc failed */
            continue;
        }
-       rq->buf = radudpget(*sp, &rq->from, NULL, &rq->fromsa);
-       rq->fromudpsock = *sp;
+       rq->buf = radudpget(*sp, &rq->from, NULL, &rq->udpport);
+       rq->udpsock = *sp;
        radsrv(rq);
-       freerq(rq);
     }
     free(sp);
 }
 
 void *udpserverwr(void *arg) {
     struct queue *replyq = (struct queue *)arg;
-    struct reply *reply;
+    struct request *reply;
+    struct sockaddr_storage to;
     
     for (;;) {
        pthread_mutex_lock(&replyq->mutex);
-       while (!(reply = (struct reply *)list_shift(replyq->entries))) {
+       while (!(reply = (struct request *)list_shift(replyq->entries))) {
            debug(DBG_DBG, "udp server writer, waiting for signal");
            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);
-
-       if (sendto(reply->toudpsock, reply->buf, RADLEN(reply->buf), 0,
-                  (struct sockaddr *)&reply->tosa, SOCKADDR_SIZE(reply->tosa)) < 0)
-           debug(DBG_WARN, "sendudp: send failed");
-       free(reply->buf);
-       free(reply);
+       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);
     }
 }
 
@@ -230,7 +251,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);
        }
@@ -238,7 +259,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);
        }
@@ -251,6 +272,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))