GPLization banner introduced to *.[hc] files
[sip-router] / test / gethostbyname.c
1 /*
2  * $Id$
3  *
4  *
5  * Copyright (C) 2001-2003 Fhg Fokus
6  *
7  * This file is part of ser, a free SIP server.
8  *
9  * ser is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version
13  *
14  * For a license to use the ser software under conditions
15  * other than those described here, or to purchase support for this
16  * software, please contact iptel.org by e-mail at the following addresses:
17  *    info@iptel.org
18  *
19  * ser 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27  */
28
29
30 #include <stdio.h>
31 #include <errno.h>
32 #include <unistd.h>
33 #include <netdb.h>
34
35
36
37
38 static char* id="$Id$";
39 static char* version="gethostbyname 0.1";
40 static char* help_msg="\
41 Usage: gethostbyname   [-hV] -n host\n\
42 Options:\n\
43     -n host       host name\n\
44     -V            version number\n\
45     -h            this help message\n\
46 ";
47
48
49 int main(int argc, char** argv)
50 {
51         char c;
52         char* name;
53         struct hostent* he;
54         unsigned char** h;
55
56         name=0;
57         
58         opterr=0;
59         while ((c=getopt(argc, argv, "n:hV"))!=-1){
60                 switch(c){
61                         case 'n':
62                                 name=optarg;
63                                 break;
64                         case 'V':
65                                 printf("version: %s\n", version);
66                                 printf("%s\n", id);
67                                 exit(0);
68                                 break;
69                         case 'h':
70                                 printf("version: %s\n", version);
71                                 printf("%s", help_msg);
72                                 exit(0);
73                                 break;
74                         case '?':
75                                 if (isprint(optopt))
76                                         fprintf(stderr, "Unknown option `-%c´\n", optopt);
77                                 else
78                                         fprintf(stderr, "Unknown character `\\x%x´\n", optopt);
79                                 goto error;
80                         case ':':
81                                 fprintf(stderr, "Option `-%c´ requires an argument.\n",
82                                                 optopt);
83                                 goto error;
84                                 break;
85                         default:
86                                 abort();
87                 }
88         }
89         
90         if (name==0){
91                 fprintf(stderr, "Missing domain name (-n name)\n");
92                 goto error;
93         }
94         
95         he=gethostbyname(name);
96         if (he==0) printf("no answer\n");
97         else{
98                 printf("h_name=%s\n", he->h_name);
99                 for(h=he->h_aliases;*h;h++)
100                         printf("   alias=%s\n", *h);
101                 for(h=he->h_addr_list;*h;h++)
102                         printf("   ip=%d.%d.%d.%d\n", (*h)[0],(*h)[1],(*h)[2],(*h)[3] );
103         }
104         printf("done\n");
105         exit(0);
106 error:
107         exit(-1);
108 }