dns_query.c

00001 /*
00002  * $Id$
00003  *
00004  * tests for ../resolver.c
00005  *
00006  * Compile with:
00007  *  gcc -o dns_query2 dns_query.c ../resolve.o ../dprint.o ../mem/ *.o -lresolv
00008  *  (and first compile ser with qm_malloc)
00009  * 
00010  *
00011  * Copyright (C) 2001-2003 FhG Fokus
00012  *
00013  * This file is part of ser, a free SIP server.
00014  *
00015  * ser is free software; you can redistribute it and/or modify
00016  * it under the terms of the GNU General Public License as published by
00017  * the Free Software Foundation; either version 2 of the License, or
00018  * (at your option) any later version
00019  *
00020  * For a license to use the ser software under conditions
00021  * other than those described here, or to purchase support for this
00022  * software, please contact iptel.org by e-mail at the following addresses:
00023  *    info@iptel.org
00024  *
00025  * ser is distributed in the hope that it will be useful,
00026  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00027  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00028  * GNU General Public License for more details.
00029  *
00030  * You should have received a copy of the GNU General Public License 
00031  * along with this program; if not, write to the Free Software 
00032  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00033  */
00034 /*
00035  * tester for ser resolver  (andrei) */
00036 
00037 
00038 #include <ctype.h>
00039 #include <stdio.h>
00040 #include <stdlib.h>
00041 #include <errno.h>
00042 #include <string.h>
00043 #include <unistd.h>
00044 
00045 #include "../resolve.h"
00046 #include "../mem/q_malloc.h"
00047 
00048 /* symbols needed by dprint */
00049 int log_stderr=1;
00050 int debug=0;
00051 int pids[1];
00052 int process_no=0;
00053 long shm_mem_size=0;
00054 char mem_pool[1024*1024];
00055 struct qm_block* mem_block;
00056 int log_facility=0;
00057 int memlog=0;
00058 int memdbg=0;
00059 int ser_error=0;
00060 struct process_table* pt=0;
00061 
00062 
00063 static char* id="$Id$";
00064 static char* version="dns_query 0.1";
00065 static char* help_msg="\
00066 Usage: dns_query  [-t type] [-hV] -n host\n\
00067 Options:\n\
00068     -n host       host name\n\
00069     -t type       query type (default A)\n\
00070     -V            version number\n\
00071     -h            this help message\n\
00072 ";
00073 
00074 
00075 int main(int argc, char** argv)
00076 {
00077         char c;
00078         char* name;
00079         char* type_str;
00080         int type;
00081         int r;
00082         struct rdata* head;
00083         struct rdata* l;
00084         struct srv_rdata* srv;
00085         struct naptr_rdata* naptr;
00086         struct a_rdata* ip;
00087 
00088         name=type_str=0;
00089         
00090         opterr=0;
00091         while ((c=getopt(argc, argv, "n:t:hV"))!=-1){
00092                 switch(c){
00093                         case 'n':
00094                                 name=optarg;
00095                                 break;
00096                         case 't':
00097                                 type_str=optarg;
00098                                 break;
00099                         case 'V':
00100                                 printf("version: %s\n", version);
00101                                 printf("%s\n", id);
00102                                 exit(0);
00103                                 break;
00104                         case 'h':
00105                                 printf("version: %s\n", version);
00106                                 printf("%s", help_msg);
00107                                 exit(0);
00108                                 break;
00109                         case '?':
00110                                 if (isprint(optopt))
00111                                         fprintf(stderr, "Unknown option `-%c´\n", optopt);
00112                                 else
00113                                         fprintf(stderr, "Unknown character `\\x%x´\n", optopt);
00114                                 goto error;
00115                         case ':':
00116                                 fprintf(stderr, "Option `-%c´ requires an argument.\n",
00117                                                 optopt);
00118                                 goto error;
00119                                 break;
00120                         default:
00121                                 abort();
00122                 }
00123         }
00124         
00125         if (name==0){
00126                 fprintf(stderr, "Missing domain name (-n name)\n");
00127                 goto error;
00128         }
00129         type=T_A;
00130         if (type_str){
00131                 if (strcasecmp(type_str, "A")==0) type=T_A;
00132                 else if (strcasecmp(type_str, "NS")==0) type=T_NS;
00133                 else if (strcasecmp(type_str, "MD")==0) type=T_MD;
00134                 else if (strcasecmp(type_str, "MF")==0) type=T_MF;
00135                 else if (strcasecmp(type_str, "CNAME")==0) type=T_CNAME;
00136                 else if (strcasecmp(type_str, "SOA")==0) type=T_SOA;
00137                 else if (strcasecmp(type_str, "PTR")==0) type=T_PTR;
00138                 else if (strcasecmp(type_str, "HINFO")==0) type=T_HINFO;
00139                 else if (strcasecmp(type_str, "MINFO")==0) type=T_MINFO;
00140                 else if (strcasecmp(type_str, "MX")==0) type=T_MX;
00141                 else if (strcasecmp(type_str, "TXT")==0) type=T_TXT;
00142                 else if (strcasecmp(type_str, "AAAA")==0) type=T_AAAA;
00143                 else if (strcasecmp(type_str, "SRV")==0) type=T_SRV;
00144                 else if (strcasecmp(type_str, "NAPTR")==0) type=T_NAPTR;
00145                 else if (strcasecmp(type_str, "AXFR")==0) type=T_AXFR;
00146                 else{
00147                         fprintf(stderr, "Unknown query type %s\n", type_str);
00148                         goto error;
00149                 }
00150         }
00151         /* init mallocs*/
00152         mem_block=qm_malloc_init(mem_pool, 1024*1024);
00153         printf("calling get_record...\n");
00154         head=get_record(name, type);
00155         if (head==0) printf("no answer\n");
00156         else{
00157                 printf("records:\n");
00158                 for(l=head; l; l=l->next){
00159                         switch(l->type){
00160                                 case T_SRV:
00161                                         srv=(struct srv_rdata*)l->rdata;
00162                                         printf("SRV    type= %d class=%d  ttl=%d\n",
00163                                                         l->type, l->class, l->ttl);
00164                                         printf("       prio= %d weight=%d port=%d\n",
00165                                                                 srv->priority, srv->weight, srv->port);
00166                                         printf("       name_len= %d (%d), name= [%.*s]\n",
00167                                                                         srv->name_len, strlen(srv->name),
00168                                                                         srv->name_len, srv->name);
00169                                         break;
00170                                 case T_CNAME:
00171                                         printf("CNAME  type= %d class=%d  ttl=%d\n",
00172                                                         l->type, l->class, l->ttl);
00173                                         printf("       name=[%s]\n", 
00174                                                                 ((struct cname_rdata*)l->rdata)->name);
00175                                         break;
00176                                 case T_A:
00177                                         ip=(struct a_rdata*)l->rdata;
00178                                         printf("A      type= %d class=%d  ttl=%d\n",
00179                                                                 l->type, l->class, l->ttl);
00180                                         printf("       ip= %d.%d.%d.%d\n",
00181                                                                 ip->ip[0], ip->ip[1], ip->ip[2], ip->ip[3]);
00182                                         break;
00183                                 case T_AAAA:
00184                                         printf("AAAA   type= %d class=%d  ttl=%d\n",
00185                                                         l->type, l->class, l->ttl);
00186                                         printf("        ip6= ");
00187                                         for(r=0;r<16;r++) 
00188                                                 printf("%x ", ((struct aaaa_rdata*)l->rdata)->ip6[r]);
00189                                         printf("\n");
00190                                         break;
00191                                 case T_NAPTR:
00192                                         naptr=(struct naptr_rdata*)l->rdata;
00193                                         printf("NAPTR  type= %d class=%d  ttl=%d\n",
00194                                                         l->type, l->class, l->ttl);
00195                                         printf("       order= %d pref=%d\n",
00196                                                                 naptr->order, naptr->pref);
00197                                         printf("       flags_len= %d,     flags= [%.*s]\n",
00198                                                                         naptr->flags_len, 
00199                                                                         naptr->flags_len, naptr->flags);
00200                                         printf("       services_len= %d,  services= [%.*s]\n",
00201                                                                         naptr->services_len, 
00202                                                                         naptr->services_len, naptr->services);
00203                                         printf("       regexp_len= %d,    regexp= [%.*s]\n",
00204                                                                         naptr->regexp_len, 
00205                                                                         naptr->regexp_len, naptr->regexp);
00206                                         printf("       repl_len= %d,      repl= [%s]\n",
00207                                                                         naptr->repl_len, naptr->repl);
00208                                         break;
00209 
00210                                 default:
00211                                         printf("UNKN    type= %d class=%d  ttl=%d\n",
00212                                                                 l->type, l->class, l->ttl);
00213                                         printf("       rdata=%p\n", l->rdata);
00214                         }
00215                 }
00216         }
00217         printf("done\n");
00218         exit(0);
00219 error:
00220         exit(-1);
00221 }