X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=src%2Fnetwork%2F__dns.c;h=0d4f47e0ccdc373c466ad04fcdb969fd370e7185;hb=88d84b7cc85841c575e54b635b125012c1d2e39d;hp=a5458b516a21d42e015a6095b4b01732221512dd;hpb=bf027ca5e0c7bc0b90e84dcbb1d7cbea6ed1e3ef;p=musl diff --git a/src/network/__dns.c b/src/network/__dns.c index a5458b51..0d4f47e0 100644 --- a/src/network/__dns.c +++ b/src/network/__dns.c @@ -19,6 +19,11 @@ #define PACKET_MAX 512 #define PTR_MAX (64 + sizeof ".in-addr.arpa") +static void cleanup(void *p) +{ + close((intptr_t)p); +} + int __dns_doqueries(unsigned char *dest, const char *name, int *rr, int rrcnt) { time_t t0 = time(0); @@ -44,6 +49,8 @@ int __dns_doqueries(unsigned char *dest, const char *name, int *rr, int rrcnt) int id; int cs; + pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs); + /* Construct query template - RR and ID will be filled later */ if (strlen(name)-1 >= 254U) return EAI_NONAME; q[2] = q[5] = 1; @@ -75,13 +82,15 @@ int __dns_doqueries(unsigned char *dest, const char *name, int *rr, int rrcnt) } if (f) __fclose_ca(f); if (!nns) { - ns[0].sin.sin_family = AF_INET; + ns[0].sin.sin_family = family = AF_INET; ns[0].sin.sin_port = htons(53); + ns[0].sin.sin_addr.s_addr = htonl(0x7f000001); nns=1; sl = sizeof sa.sin; } - pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs); + pthread_cleanup_push(cleanup, (void *)(intptr_t)fd); + pthread_setcancelstate(cs, 0); /* Get local address and open/bind a socket */ sa.sin.sin_family = family; @@ -143,8 +152,7 @@ int __dns_doqueries(unsigned char *dest, const char *name, int *rr, int rrcnt) if (got+failed == rrcnt) break; } out: - close(fd); - pthread_setcancelstate(cs, 0); + pthread_cleanup_pop(1); /* Return the number of results, or an error code if none */ if (got) return got;