aa830064f3ba95112044e78a233b459c2a6b2eed
[gapil.git] / sources / mygetaddr.c
1 /* mygetaddr.c
2  * 
3  * Copyright (C) 2004 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  * File mygetaddr.c: An example host command
22  *
23  * Author: S. Piccardi Nov. 2004
24  *
25  * $Id$
26  *
27  *****************************************************************************/
28 #include <netdb.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <stdlib.h>        /* C standard library */
32 #include <stdio.h>         /* I/O standard library */
33 #include <unistd.h>
34 #include <arpa/inet.h>
35 #include <sys/socket.h>
36 #include <netinet/in.h>
37
38 #include "Gapil.h"
39 /*
40  * Program myhost
41  *
42  * Use gethostbyname and print results
43  */
44 /* Help printing routine */
45 void usage(void);
46
47 int main(int argc, char *argv[]) 
48 {
49 /* 
50  * Variables definition
51  */
52     int i;
53     struct addrinfo hint;
54     struct addrinfo *res, *ptr;
55     int ret, port;
56     struct sockaddr_in *addr;
57     struct sockaddr_in6 *addr6;
58     char buffer[INET6_ADDRSTRLEN];
59     char *string;
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, "h")) != -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 '?':                                    /* unrecognized options */
76             printf("Unrecognized options -%c\n",optopt);
77             usage();
78         default:                                       /* should not reached */
79             usage();
80         }
81     }
82     /* ***********************************************************
83      * 
84      *           Options processing completed
85      *
86      *                Main code beginning
87      * 
88      * ***********************************************************/
89     if ((argc - optind) != 2) {
90         printf("Wrong number of arguments %d\n", argc - optind);
91         usage();
92     }
93     
94     ret = getaddrinfo(argv[1], argv[2], NULL, &res);  /* main call */
95     if (ret != 0) {                                   /* on error exit */
96         printf("Resolution error %s\n", gai_strerror(ret));
97         exit(1);
98     }
99     ptr = res;                                        /* store pointer */
100     printf("Canonical name %s\n", ptr->ai_canonname); /* print cname */
101     do {
102         if (ptr->ai_family == PF_INET) {
103             printf("IPv4 address: \n");
104             addr = (struct sockaddr_in *) ptr->ai_addr;
105             port = ntohs(addr->sin_port);
106             string = inet_ntop(addr->sin_family, &addr->sin_addr, 
107                                buffer, sizeof(buffer));
108         } else if (ptr->ai_family == PF_INET6) {
109             printf("IPv6 address: \n");
110             addr6 = (struct sockaddr_in *) ptr->ai_addr;
111             port = ntohs(addr6->sin6_port);
112             string = inet_ntop(addr6->sin6_family, &addr6->sin6_addr, 
113                                buffer, sizeof(buffer));
114         } else {
115             printf("Address family error\n");
116             exit(1);
117         }       
118         printf("\tIndirizzo %s\n", string);
119         printf("\tProtocollo %i\n", ptr->ai_protocol);
120         printf("\tPorta %i\n", port);
121         
122
123         ptr = ptr->ai_next;
124     } while (ptr != NULL);
125     exit(0);
126 }
127 /*
128  * routine to print usage info and exit
129  */
130 void usage(void) {
131     printf("Program mygethost: do an hostname resolution \n");
132     printf("Usage:\n");
133     printf("  mygethost [-h] hostname service\n");
134     printf("  -h   print this help\n");
135     exit(1);
136 }