dns_query.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * $Id$
  3. *
  4. * tests for ../resolver.c
  5. *
  6. * Compile with:
  7. * gcc -o dns_query2 dns_query.c ../resolve.o ../dprint.o ../mem/ *.o -lresolv
  8. * (and first compile ser with qm_malloc)
  9. *
  10. *
  11. * Copyright (C) 2001-2003 Fhg Fokus
  12. *
  13. * This file is part of ser, a free SIP server.
  14. *
  15. * ser is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or
  18. * (at your option) any later version
  19. *
  20. * For a license to use the ser software under conditions
  21. * other than those described here, or to purchase support for this
  22. * software, please contact iptel.org by e-mail at the following addresses:
  23. * [email protected]
  24. *
  25. * ser is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. * GNU General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU General Public License
  31. * along with this program; if not, write to the Free Software
  32. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  33. */
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <errno.h>
  37. #include <string.h>
  38. #include <unistd.h>
  39. #include "../resolve.h"
  40. #include "../mem/q_malloc.h"
  41. /* symbols needed by dprint */
  42. int log_stderr=1;
  43. int debug=0;
  44. int pids[1];
  45. int process_no=0;
  46. int shm_mem_size=0;
  47. char mem_pool[1024*1024];
  48. struct qm_block* mem_block;
  49. static char* id="$Id$";
  50. static char* version="dns_query 0.1";
  51. static char* help_msg="\
  52. Usage: dns_query [-t type] [-hV] -n host\n\
  53. Options:\n\
  54. -n host host name\n\
  55. -t type query type (default A)\n\
  56. -V version number\n\
  57. -h this help message\n\
  58. ";
  59. int main(int argc, char** argv)
  60. {
  61. char c;
  62. char* name;
  63. char* type_str;
  64. int type;
  65. int r;
  66. struct rdata* head;
  67. struct rdata* l;
  68. struct srv_rdata* srv;
  69. struct a_rdata* ip;
  70. name=type_str=0;
  71. opterr=0;
  72. while ((c=getopt(argc, argv, "n:t:hV"))!=-1){
  73. switch(c){
  74. case 'n':
  75. name=optarg;
  76. break;
  77. case 't':
  78. type_str=optarg;
  79. break;
  80. case 'V':
  81. printf("version: %s\n", version);
  82. printf("%s\n", id);
  83. exit(0);
  84. break;
  85. case 'h':
  86. printf("version: %s\n", version);
  87. printf("%s", help_msg);
  88. exit(0);
  89. break;
  90. case '?':
  91. if (isprint(optopt))
  92. fprintf(stderr, "Unknown option `-%c´\n", optopt);
  93. else
  94. fprintf(stderr, "Unknown character `\\x%x´\n", optopt);
  95. goto error;
  96. case ':':
  97. fprintf(stderr, "Option `-%c´ requires an argument.\n",
  98. optopt);
  99. goto error;
  100. break;
  101. default:
  102. abort();
  103. }
  104. }
  105. if (name==0){
  106. fprintf(stderr, "Missing domain name (-n name)\n");
  107. goto error;
  108. }
  109. type=T_A;
  110. if (type_str){
  111. if (strcasecmp(type_str, "A")==0) type=T_A;
  112. else if (strcasecmp(type_str, "NS")==0) type=T_NS;
  113. else if (strcasecmp(type_str, "MD")==0) type=T_MD;
  114. else if (strcasecmp(type_str, "MF")==0) type=T_MF;
  115. else if (strcasecmp(type_str, "CNAME")==0) type=T_CNAME;
  116. else if (strcasecmp(type_str, "SOA")==0) type=T_SOA;
  117. else if (strcasecmp(type_str, "PTR")==0) type=T_PTR;
  118. else if (strcasecmp(type_str, "HINFO")==0) type=T_HINFO;
  119. else if (strcasecmp(type_str, "MINFO")==0) type=T_MINFO;
  120. else if (strcasecmp(type_str, "MX")==0) type=T_MX;
  121. else if (strcasecmp(type_str, "TXT")==0) type=T_TXT;
  122. else if (strcasecmp(type_str, "AAAA")==0) type=T_AAAA;
  123. else if (strcasecmp(type_str, "SRV")==0) type=T_SRV;
  124. else if (strcasecmp(type_str, "NAPTR")==0) type=T_NAPTR;
  125. else if (strcasecmp(type_str, "AXFR")==0) type=T_AXFR;
  126. else{
  127. fprintf(stderr, "Unknown query type %s\n", type_str);
  128. goto error;
  129. }
  130. }
  131. /* init mallocs*/
  132. mem_block=qm_malloc_init(mem_pool, 1024*1024);
  133. printf("calling get_record...\n");
  134. head=get_record(name, type);
  135. if (head==0) printf("no answer\n");
  136. else{
  137. printf("records:\n");
  138. for(l=head; l; l=l->next){
  139. switch(l->type){
  140. case T_SRV:
  141. srv=(struct srv_rdata*)l->rdata;
  142. printf("SRV type= %d class=%d ttl=%d\n",
  143. l->type, l->class, l->ttl);
  144. printf(" prio= %d weight=%d port=%d\n",
  145. srv->priority, srv->weight, srv->port);
  146. printf(" name= [%s]\n", srv->name);
  147. break;
  148. case T_CNAME:
  149. printf("CNAME type= %d class=%d ttl=%d\n",
  150. l->type, l->class, l->ttl);
  151. printf(" name=[%s]\n",
  152. ((struct cname_rdata*)l->rdata)->name);
  153. break;
  154. case T_A:
  155. ip=(struct a_rdata*)l->rdata;
  156. printf("A type= %d class=%d ttl=%d\n",
  157. l->type, l->class, l->ttl);
  158. printf(" ip= %d.%d.%d.%d\n",
  159. ip->ip[0], ip->ip[1], ip->ip[2], ip->ip[3]);
  160. break;
  161. case T_AAAA:
  162. printf("AAAA type= %d class=%d ttl=%d\n",
  163. l->type, l->class, l->ttl);
  164. printf(" ip6= ");
  165. for(r=0;r<16;r++)
  166. printf("%x ", ((struct aaaa_rdata*)l->rdata)->ip6[r]);
  167. printf("\n");
  168. break;
  169. default:
  170. printf("UNKN type= %d class=%d ttl=%d\n",
  171. l->type, l->class, l->ttl);
  172. printf(" rdata=%p\n", l->rdata);
  173. }
  174. }
  175. }
  176. printf("done\n");
  177. exit(0);
  178. error:
  179. exit(-1);
  180. }