3 * Copyright (C) 2004 Simone Piccardi
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.
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.
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.
19 /*****************************************************************************
21 * File mygetaddr.c: An example host command
23 * Author: S. Piccardi Nov. 2004
27 *****************************************************************************/
29 #include <sys/types.h>
31 #include <stdlib.h> /* C standard library */
32 #include <stdio.h> /* I/O standard library */
34 #include <arpa/inet.h>
35 #include <sys/socket.h>
36 #include <netinet/in.h>
42 * Use gethostbyname and print results
44 /* Help printing routine */
47 int main(int argc, char *argv[])
50 * Variables definition
54 struct addrinfo *res, *ptr;
56 struct sockaddr_in *addr;
57 struct sockaddr_in6 *addr6;
58 char buffer[INET6_ADDRSTRLEN];
61 * Input section: decode command line parameters
64 opterr = 0; /* don't want writing to stderr */
65 while ( (i = getopt(argc, argv, "h")) != -1) {
70 case 'h': /* help option */
71 printf("Wrong -h option use\n");
75 case '?': /* unrecognized options */
76 printf("Unrecognized options -%c\n",optopt);
78 default: /* should not reached */
82 /* ***********************************************************
84 * Options processing completed
88 * ***********************************************************/
89 if ((argc - optind) != 2) {
90 printf("Wrong number of arguments %d\n", argc - optind);
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));
99 ptr = res; /* store pointer */
100 printf("Canonical name %s\n", ptr->ai_canonname); /* print cname */
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));
115 printf("Address family error\n");
118 printf("\tIndirizzo %s\n", string);
119 printf("\tProtocollo %i\n", ptr->ai_protocol);
120 printf("\tPorta %i\n", port);
124 } while (ptr != NULL);
128 * routine to print usage info and exit
131 printf("Program mygethost: do an hostname resolution \n");
133 printf(" mygethost [-h] hostname service\n");
134 printf(" -h print this help\n");