This eliminates a slightly fiddly loop from two call sites.
* might be broken, but no reconnect will be attempted.
*/
* 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.
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.
proto= getprotobyname("udp"); if (!proto) { r= ENOPROTOOPT; goto x_free; }
ads->nudp = 0;
for (i = 0; i < ads->nservers; i++) {
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);
assert(ads->nudp < MAXUDP);
udp = &ads->udpsocket[ads->nudp];
udp->ai = find_afinfo(ads->servers[i].addr.sa.sa_family);
r= adns__setnonblock(ads,udp->fd);
if (r) { r= errno; goto x_closeudp; }
ads->nudp++;
r= adns__setnonblock(ads,udp->fd);
if (r) { r= errno; goto x_closeudp; }
ads->nudp++;
adns__tcp_tryconnect(qu->ads,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;
void adns__query_send(adns_query qu, struct timeval now) {
int serv, r, i;
adns_state ads;
struct udpsocket *udp;
adns_rr_addr *addr;
struct udpsocket *udp;
adns_rr_addr *addr;
ads= qu->ads;
serv= qu->udpnextserver;
addr= &ads->servers[serv];
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;
&addr->addr.sa,addr->len);
if (r<0 && errno == EMSGSIZE) {
qu->retries= 0;