gethostbyname.c

00001 /*
00002  * $Id$
00003  *
00004  *
00005  * Copyright (C) 2001-2003 FhG Fokus
00006  *
00007  * This file is part of ser, a free SIP server.
00008  *
00009  * ser is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version
00013  *
00014  * For a license to use the ser software under conditions
00015  * other than those described here, or to purchase support for this
00016  * software, please contact iptel.org by e-mail at the following addresses:
00017  *    info@iptel.org
00018  *
00019  * ser is distributed in the hope that it will be useful,
00020  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00022  * GNU General Public License for more details.
00023  *
00024  * You should have received a copy of the GNU General Public License 
00025  * along with this program; if not, write to the Free Software 
00026  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00027  */
00028 
00029 
00030 #include <stdio.h>
00031 #include <errno.h>
00032 #include <unistd.h>
00033 #include <netdb.h>
00034 
00035 
00036 
00037 
00038 static char* id="$Id$";
00039 static char* version="gethostbyname 0.1";
00040 static char* help_msg="\
00041 Usage: gethostbyname   [-hV] -n host\n\
00042 Options:\n\
00043     -n host       host name\n\
00044     -V            version number\n\
00045     -h            this help message\n\
00046 ";
00047 
00048 
00049 int main(int argc, char** argv)
00050 {
00051         char c;
00052         char* name;
00053         struct hostent* he;
00054         unsigned char** h;
00055 
00056         name=0;
00057         
00058         opterr=0;
00059         while ((c=getopt(argc, argv, "n:hV"))!=-1){
00060                 switch(c){
00061                         case 'n':
00062                                 name=optarg;
00063                                 break;
00064                         case 'V':
00065                                 printf("version: %s\n", version);
00066                                 printf("%s\n", id);
00067                                 exit(0);
00068                                 break;
00069                         case 'h':
00070                                 printf("version: %s\n", version);
00071                                 printf("%s", help_msg);
00072                                 exit(0);
00073                                 break;
00074                         case '?':
00075                                 if (isprint(optopt))
00076                                         fprintf(stderr, "Unknown option `-%c´\n", optopt);
00077                                 else
00078                                         fprintf(stderr, "Unknown character `\\x%x´\n", optopt);
00079                                 goto error;
00080                         case ':':
00081                                 fprintf(stderr, "Option `-%c´ requires an argument.\n",
00082                                                 optopt);
00083                                 goto error;
00084                                 break;
00085                         default:
00086                                 abort();
00087                 }
00088         }
00089         
00090         if (name==0){
00091                 fprintf(stderr, "Missing domain name (-n name)\n");
00092                 goto error;
00093         }
00094         
00095         he=gethostbyname(name);
00096         if (he==0) printf("no answer\n");
00097         else{
00098                 printf("h_name=%s\n", he->h_name);
00099                 for(h=he->h_aliases;*h;h++)
00100                         printf("   alias=%s\n", *h);
00101                 for(h=he->h_addr_list;*h;h++)
00102                         printf("   ip=%d.%d.%d.%d\n", (*h)[0],(*h)[1],(*h)[2],(*h)[3] );
00103         }
00104         printf("done\n");
00105         exit(0);
00106 error:
00107         exit(-1);
00108 }