Aggiunto README e programmino per il test di gethostbyname e getaddrinfo
[gapil.git] / sources / gethost.c
1 /* gethost.c
2  * 
3  * Copyright (C) 2001 Simone Piccardi
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or (at
8  * your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 /****************************************************************
20  *
21  * Program gethost.c: 
22  * Program to test gethostbyname and getaddrinfo
23  *
24  * Author: Simone Piccardi
25  * Aug. 2002
26  *
27  * Usage: gethost -h give all info's
28  *
29  * $Id: gethost.c,v 1.1 2002/08/27 16:05:04 piccardi Exp $
30  *
31  ****************************************************************/
32 /* 
33  * Include needed headers
34  */
35 #define _GNU_SOURCE
36 #include <errno.h>       /* error definitions and routines */ 
37 #include <stdlib.h>      /* C standard library */
38 #include <unistd.h>      /* unix standard library */
39 #include <stdio.h>       /* standard I/O library */
40 #include <string.h>      /* string functions */
41 #include <sys/types.h>
42 #include <sys/socket.h>
43 #include <netdb.h>
44
45 /* Help printing routine */
46 void usage(void);
47
48 int main(int argc, char *argv[])
49 {
50 /* 
51  * Variables definition  
52  */
53     int i;
54     int use = 0;
55     struct hostent *host;
56     struct in_addr addr;
57     struct addrinfo hint;
58     struct addrinfo *address;
59     struct sockaddr_in *socket;
60     /*
61      * Input section: decode command line parameters 
62      * Use getopt function
63      */
64     opterr = 0;  /* don't want writing to stderr */
65     while ( (i = getopt(argc, argv, "han")) != -1) {
66         switch (i) {
67         /* 
68          * Handling options 
69          */ 
70         case 'h':   /* help option */
71             printf("Wrong -h option use\n");
72             usage();
73             return -1;
74             break;
75         case 'a':   /* help option */
76             use = 1;
77             break;
78         case 'n':   /* help option */
79             use = 0;
80             break;
81         case '?':   /* unrecognized options */
82             printf("Unrecognized options -%c\n",optopt);
83             usage();
84         default:    /* should not reached */
85             usage();
86         }
87     }
88     /* ***********************************************************
89      * 
90      *           Options processing completed
91      *
92      *                Main code beginning
93      * 
94      * ***********************************************************/
95     if ((argc-optind)!= 1)  {
96         printf("From %d arguments, removed %d options\n", argc, optind);
97         usage();
98     }
99     if (use == 0) {
100         host = gethostbyname(argv[optind]);
101         printf("gethostbyname %s\n", argv[optind]);
102         printf("Official host name %s\n", host->h_name);
103         printf("Address Type %d\n", host->h_addrtype);
104         printf("Address Lenght %d\n", host->h_length);
105         addr.s_addr = *( (unsigned long *)host->h_addr);
106         printf("Address  %s\n", inet_ntoa(addr));
107     } else {
108 /*      host = getipnodebyname(argv[optind], AF_INET, 0, &i); */
109 /*      if (!host) printf("Error\n"); */
110 /*      printf("getipnodebyname %s, error %d\n", argv[optind], i); */
111 /*      printf("Official host name %s\n", host->h_name); */
112 /*      printf("Address Type %d\n", host->h_addrtype); */
113 /*      printf("Address Lenght %d\n", host->h_length); */
114 /*      addr.s_addr = *( (unsigned long *)host->h_addr); */
115 /*      printf("Address  %s\n", inet_ntoa(addr)); */
116         hint.ai_flags = 0;
117         hint.ai_family = PF_INET;
118         hint.ai_socktype = 0;
119         hint.ai_protocol = 0;
120         hint.ai_addrlen = 0;
121         hint.ai_addr = NULL;
122         hint.ai_canonname = NULL;
123         hint.ai_next = NULL;
124         if (i = getaddrinfo(argv[optind], "telnet", &hint, &address)) {
125             printf("getaddrinfo %s = %s \n", argv[optind], gai_strerror(i));
126         } else {
127             printf("Address flag %d\n", address->ai_flags);
128             printf("Address family %d\n", address->ai_family);
129             printf("Address socket type %d\n", address->ai_socktype);
130             printf("Address protocol %d\n", address->ai_protocol);
131             printf("Address lenght %d\n", address->ai_addrlen);
132             printf("Canonical name %s\n", address->ai_canonname);
133             socket = (struct sockaddr_in *) address->ai_addr;
134             printf("Address %s\n", inet_ntoa(socket->sin_addr));
135             printf("Port %d\n", htons(socket->sin_port));
136             printf("Family %d\n", socket->sin_family);
137             
138         }
139     }
140     return 0;
141 }
142 /*
143  * routine to print usage info and exit
144  */
145 void usage(void) {
146     printf("Program gehost: test host name functions  \n");
147     printf("Usage:\n");
148     printf("  gethost [-h] address \n");
149     printf("  -h           print this help\n");
150     printf("  -a           use getaddrinfo\n");
151     printf("  -n           use gethostbyname\n");
152     
153     exit(1);
154 }
155