chiark / gitweb /
src/setup.c, src/transmit.c: New function finds udpsocket by AF.
authorMark Wooding <mdw@distorted.org.uk>
Sat, 24 May 2014 13:00:03 +0000 (14:00 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Sun, 25 May 2014 13:15:32 +0000 (14:15 +0100)
This eliminates a slightly fiddly loop from two call sites.

src/internal.h
src/setup.c
src/transmit.c

index 3e85a5872e050b0a9594cb9c2ae0519bfd148d03..bc5f542bb132f7b71ba9213959c561527f008a6e 100644 (file)
@@ -501,6 +501,15 @@ void adns__querysend_tcp(adns_query qu, struct timeval now);
  * might be broken, but no reconnect will be attempted.
  */
 
+struct udpsocket *adns__udpsocket_by_af(adns_state ads, int af);
+/* Find the UDP socket structure in ads which has the given address family.
+ * Return null if there isn't one.
+ *
+ * This is used during initialization, so ads is only partially filled in.
+ * The requirements are that nudp is set, and that udpsocket[i].af are
+ * defined for 0<=i<nudp.
+ */
+
 void adns__query_send(adns_query qu, struct timeval now);
 /* Query must be in state tosend/NONE; it will be moved to a new state,
  * and no further processing can be done on it for now.
index 267f3dad3df10b8cbe5e334ec8b808ce3a7ad37a..fdbc6d08e23425d276acbf974d00d4dcf12f88d0 100644 (file)
@@ -627,11 +627,8 @@ static int init_finish(adns_state ads) {
   proto= getprotobyname("udp"); if (!proto) { r= ENOPROTOOPT; goto x_free; }
   ads->nudp = 0;
   for (i = 0; i < ads->nservers; i++) {
-    for (j = 0; j < ads->nudp; j++) {
-      if (ads->udpsocket[j].ai->af == ads->servers[i].addr.sa.sa_family)
-       goto afmatch;
-    }
-
+    if (adns__udpsocket_by_af(ads, ads->servers[i].addr.sa.sa_family))
+      continue;
     assert(ads->nudp < MAXUDP);
     udp = &ads->udpsocket[ads->nudp];
     udp->ai = find_afinfo(ads->servers[i].addr.sa.sa_family);
@@ -641,8 +638,6 @@ static int init_finish(adns_state ads) {
     r= adns__setnonblock(ads,udp->fd);
     if (r) { r= errno; goto x_closeudp; }
     ads->nudp++;
-
-  afmatch:;
   }
   
   return 0;
index 212c6ec2ff40c50cf65c92a9116db1e54122ede3..63e222c23356784655a7b805ce02371b79554b8d 100644 (file)
@@ -222,10 +222,16 @@ static void query_usetcp(adns_query qu, struct timeval now) {
   adns__tcp_tryconnect(qu->ads,now);
 }
 
+struct udpsocket *adns__udpsocket_by_af(adns_state ads, int af) {
+  int i;
+  for (i=0; i<ads->nudp; i++)
+    if (ads->udpsocket[i].af == af) return &ads->udpsocket[i];
+  return 0;
+}
+
 void adns__query_send(adns_query qu, struct timeval now) {
   int serv, r, i;
   adns_state ads;
-  int fd = -1;
   struct udpsocket *udp;
   adns_rr_addr *addr;
 
@@ -243,13 +249,10 @@ void adns__query_send(adns_query qu, struct timeval now) {
   ads= qu->ads;
   serv= qu->udpnextserver;
   addr= &ads->servers[serv];
-  for (i = 0; i < ads->nudp; i++) {
-    udp = &ads->udpsocket[i];
-    if (udp->ai->af == addr->addr.sa.sa_family) { fd = udp->fd; break; }
-  }
-  assert(fd >= 0);
+  udp= adns__udpsocket_by_af(ads, addr->addr.sa.sa_family);
+  assert(udp);
   
-  r= sendto(fd,qu->query_dgram,qu->query_dglen,0,
+  r= sendto(udp->fd,qu->query_dgram,qu->query_dglen,0,
            &addr->addr.sa,addr->len);
   if (r<0 && errno == EMSGSIZE) {
     qu->retries= 0;