dns_query.c 5.8 KB

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