diff options
author | Lennart Poettering <lennart@poettering.net> | 2007-02-16 12:49:17 +0000 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2007-02-16 12:49:17 +0000 |
commit | 798e297be094c9872d7dd4121e61d39698e09ebc (patch) | |
tree | bb8b5ea6690e8ae8384c1a39320aaf99f1f9951e /libasyncns/asyncns-test.c | |
parent | 26a4d2b3d1bbe6eeb5e95ce3a0900e31b89e42ce (diff) |
Merge res_query patch from Sjoerd Simons
git-svn-id: file:///home/lennart/svn/public/libasyncns/trunk@23 cc0fb855-19ed-0310-866e-8c1d96e4abae
Diffstat (limited to 'libasyncns/asyncns-test.c')
-rw-r--r-- | libasyncns/asyncns-test.c | 59 |
1 files changed, 57 insertions, 2 deletions
diff --git a/libasyncns/asyncns-test.c b/libasyncns/asyncns-test.c index a6e2d72..a59ee30 100644 --- a/libasyncns/asyncns-test.c +++ b/libasyncns/asyncns-test.c @@ -24,16 +24,21 @@ #include <sys/socket.h> #include <arpa/inet.h> #include <stdio.h> +#include <netinet/in.h> +#include <arpa/nameser.h> +#include <resolv.h> +#include <assert.h> #include "asyncns.h" int main(int argc, char *argv[]) { asyncns_t* asyncns = NULL; - asyncns_query_t *q1, *q2; + asyncns_query_t *q1, *q2, *q3; int r = 1, ret; struct addrinfo *ai, hints; struct sockaddr_in sa; char host[NI_MAXHOST] = "", serv[NI_MAXSERV] = ""; + unsigned char *srv; if (!(asyncns = asyncns_new(10))) { fprintf(stderr, "asyncns_new() failed\n"); @@ -55,8 +60,12 @@ int main(int argc, char *argv[]) { q2 = asyncns_getnameinfo(asyncns, (struct sockaddr*) &sa, sizeof(sa), 0, 1, 1); + q3 = asyncns_res_query(asyncns, "_xmpp-client._tcp.gmail.com", C_IN, T_SRV); + /* Wait until the two queries are completed */ - while (!asyncns_isdone(asyncns, q1) || !asyncns_isdone(asyncns, q2)) { + while (!asyncns_isdone(asyncns, q1) + || !asyncns_isdone(asyncns, q2) + || !asyncns_isdone(asyncns, q3)) { if (asyncns_wait(asyncns, 1) < 0) goto fail; } @@ -87,6 +96,52 @@ int main(int argc, char *argv[]) { fprintf(stderr, "error: %s %i\n", gai_strerror(ret), ret); else printf("%s -- %s\n", host, serv); + + /* Interpret the result of the SRV lookup */ + if ((ret = asyncns_res_done(asyncns, q3, &srv)) < 0) { + fprintf(stderr, "error: %s %i\n", strerror(ret), ret); + } else if (ret == 0) { + fprintf(stderr, "No reply for SRV lookup\n"); + } else { + int qdcount; + int ancount; + int len; + const unsigned char *pos = srv + sizeof(HEADER); + unsigned char *end = srv + ret; + HEADER *head = (HEADER *)srv; + + qdcount = ntohs(head->qdcount); + ancount = ntohs(head->ancount); + + char name[256]; + + printf("%d answers for srv lookup:\n", ancount); + + /* Ignore the questions */ + while (qdcount-- > 0 && (len = dn_expand(srv, end, pos, name, 255)) >= 0) { + assert(len >= 0); + pos += len + QFIXEDSZ; + } + + /* Parse the answers */ + while (ancount-- > 0 && (len = dn_expand(srv, end, pos, name, 255)) >= 0) { + /* Ignore the initial string */ + uint16_t pref, weight, port; + assert(len >= 0); + pos += len; + /* Ignore type, ttl, class and dlen */ + pos += 10; + + GETSHORT(pref, pos); + GETSHORT(weight, pos); + GETSHORT(port, pos); + len = dn_expand(srv, end, pos, name, 255); + printf("\tpreference: %2d weight: %2d port: %d host: %s\n", + pref, weight, port, name); + + pos += len; + } + } r = 0; |