/******************************************************************* * Test DNS SRV lookups * copyright Gerald Carter 2006 * * For some bizarre reason the ns_initparse(), et. al. routines * are not available in the shared version of libresolv.so. * * To compile, run * gcc -o dnstest -static query-srv.c -lresolv * *******************************************************************/ /* standard system headers */ #include #include #include #include #include /* resolver headers */ #include #include #include #include #include int main ( int argc, char *argv[] ) { unsigned char buffer[NS_PACKETSZ]; int resp_len; ns_msg h; ns_rr rr; int rrnum; if ( argc != 2 ) { fprintf( stderr, "Usage: %s \n", argv[0]); exit( 1 ); } /* send the request */ if ( (resp_len = res_query(argv[1], ns_c_in, ns_t_aaaa, buffer, sizeof(buffer))) < 0 ) { fprintf(stderr, "Query for %s failed!\n", argv[1]); exit( 1 ); } /* now do the parsing */ if ( ns_initparse( buffer, resp_len, &h ) ) { fprintf( stderr, "Failed to parse response buffer!\n"); exit( 2 ); } printf("%d records returned in the answer section.\n", ns_msg_count(h, ns_s_an)); for ( rrnum=0; rrnum < ns_msg_count(h, ns_s_an); rrnum++ ) { if ( ns_parserr( &h, ns_s_an, rrnum, &rr ) ) { fprintf( stderr, "ns_parserr: %s\n", strerror(errno) ); exit (2); } if ( ns_rr_type(rr) == ns_t_aaaa ) { char astr[1024]; int ret; inet_ntop(AF_INET6, ns_rr_rdata(rr), astr, sizeof(astr)); printf( "%s has address %s, ns_rr_type: %d\n", ns_rr_name(rr), astr, ns_rr_type(rr)); } if (ns_rr_type(rr) == ns_t_cname ) { char name[1024]; int ret; struct in_addr ip; const unsigned char *p; // ret = dn_expand( ns_msg_base(h), ns_msg_end(h), ns_rr_rdata(rr)+6, name, sizeof(name)); ret = ns_name_uncompress(ns_msg_base(h), ns_msg_end(h), ns_rr_rdata(rr), name, 1024); printf("cname %s, ns_rr_type: %d\n", name, ns_rr_type(rr)); } } for ( rrnum=0; rrnum < ns_msg_count(h, ns_s_ar); rrnum++ ) { if ( ns_parserr( &h, ns_s_ar, rrnum, &rr ) ) { fprintf( stderr, "ns_parserr: %s\n", strerror(errno) ); continue; } if ( ns_rr_type(rr) == ns_t_aaaa ) { char astr[1024]; int ret; inet_ntop(AF_INET6, ns_rr_rdata(rr), astr, sizeof(astr)); printf( "%s has address %s\n", ns_rr_name(rr), astr); } } return 0; }