Un po' di materiale su {{{splice}}} e inizio della ripulitura degli
[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  ****************************************************************/
30 /* 
31  * Include needed headers
32  */
33 #define _GNU_SOURCE
34 #include <errno.h>       /* error definitions and routines */ 
35 #include <stdlib.h>      /* C standard library */
36 #include <unistd.h>      /* unix standard library */
37 #include <stdio.h>       /* standard I/O library */
38 #include <string.h>      /* C strings library */
39 #include <sys/types.h>   /* primitive system data types */
40 #include <sys/socket.h>  /* socket constants, types and functions */
41 #include <netdb.h>
42
43 /* Help printing routine */
44 void usage(void);
45
46 int main(int argc, char *argv[])
47 {
48 /* 
49  * Variables definition  
50  */
51     int i;
52     int use = 0;
53     struct hostent *host;
54     struct in_addr addr;
55     struct addrinfo hint;
56     struct addrinfo *address;
57     struct sockaddr_in *socket;
58     /*
59      * Input section: decode command line parameters 
60      * Use getopt function
61      */
62     opterr = 0;  /* don't want writing to stderr */
63     while ( (i = getopt(argc, argv, "han")) != -1) {
64         switch (i) {
65         /* 
66          * Handling options 
67          */ 
68         case 'h':   /* help option */
69             printf("Wrong -h option use\n");
70             usage();
71             return -1;
72             break;
73         case 'a':   /* help option */
74             use = 1;
75             break;
76         case 'n':   /* help option */
77             use = 0;
78             break;
79         case '?':   /* unrecognized options */
80             printf("Unrecognized options -%c\n",optopt);
81             usage();
82         default:    /* should not reached */
83             usage();
84         }
85     }
86     /* ***********************************************************
87      * 
88      *           Options processing completed
89      *
90      *                Main code beginning
91      * 
92      * ***********************************************************/
93     if ((argc-optind)!= 1)  {
94         printf("From %d arguments, removed %d options\n", argc, optind);
95         usage();
96     }
97     if (use == 0) {
98         host = gethostbyname(argv[optind]);
99         printf("gethostbyname %s\n", argv[optind]);
100         printf("Official host name %s\n", host->h_name);
101         printf("Address Type %d\n", host->h_addrtype);
102         printf("Address Lenght %d\n", host->h_length);
103         addr.s_addr = *( (unsigned long *)host->h_addr);
104         printf("Address  %s\n", inet_ntoa(addr));
105     } else {
106 /*      host = getipnodebyname(argv[optind], AF_INET, 0, &i); */
107 /*      if (!host) printf("Error\n"); */
108 /*      printf("getipnodebyname %s, error %d\n", argv[optind], i); */
109 /*      printf("Official host name %s\n", host->h_name); */
110 /*      printf("Address Type %d\n", host->h_addrtype); */
111 /*      printf("Address Lenght %d\n", host->h_length); */
112 /*      addr.s_addr = *( (unsigned long *)host->h_addr); */
113 /*      printf("Address  %s\n", inet_ntoa(addr)); */
114         hint.ai_flags = 0;
115         hint.ai_family = PF_INET;
116         hint.ai_socktype = 0;
117         hint.ai_protocol = 0;
118         hint.ai_addrlen = 0;
119         hint.ai_addr = NULL;
120         hint.ai_canonname = NULL;
121         hint.ai_next = NULL;
122         if (i = getaddrinfo(argv[optind], "telnet", &hint, &address)) {
123             printf("getaddrinfo %s = %s \n", argv[optind], gai_strerror(i));
124         } else {
125             printf("Address flag %d\n", address->ai_flags);
126             printf("Address family %d\n", address->ai_family);
127             printf("Address socket type %d\n", address->ai_socktype);
128             printf("Address protocol %d\n", address->ai_protocol);
129             printf("Address lenght %d\n", address->ai_addrlen);
130             printf("Canonical name %s\n", address->ai_canonname);
131             socket = (struct sockaddr_in *) address->ai_addr;
132             printf("Address %s\n", inet_ntoa(socket->sin_addr));
133             printf("Port %d\n", htons(socket->sin_port));
134             printf("Family %d\n", socket->sin_family);
135             
136         }
137     }
138     return 0;
139 }
140 /*
141  * routine to print usage info and exit
142  */
143 void usage(void) {
144     printf("Program gehost: test host name functions  \n");
145     printf("Usage:\n");
146     printf("  gethost [-h] address \n");
147     printf("  -h           print this help\n");
148     printf("  -a           use getaddrinfo\n");
149     printf("  -n           use gethostbyname\n");
150     
151     exit(1);
152 }
153