Spostato il vecchi myhost.c e creato nuovo programma mygetaddr per provare
[gapil.git] / sources / mygetaddr.c
diff --git a/sources/mygetaddr.c b/sources/mygetaddr.c
new file mode 100644 (file)
index 0000000..aa83006
--- /dev/null
@@ -0,0 +1,136 @@
+/* mygetaddr.c
+ * 
+ * Copyright (C) 2004 Simone Piccardi
+ * 
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+/*****************************************************************************
+ *
+ * File mygetaddr.c: An example host command
+ *
+ * Author: S. Piccardi Nov. 2004
+ *
+ * $Id$
+ *
+ *****************************************************************************/
+#include <netdb.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <stdlib.h>        /* C standard library */
+#include <stdio.h>         /* I/O standard library */
+#include <unistd.h>
+#include <arpa/inet.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+
+#include "Gapil.h"
+/*
+ * Program myhost
+ *
+ * Use gethostbyname and print results
+ */
+/* Help printing routine */
+void usage(void);
+
+int main(int argc, char *argv[]) 
+{
+/* 
+ * Variables definition
+ */
+    int i;
+    struct addrinfo hint;
+    struct addrinfo *res, *ptr;
+    int ret, port;
+    struct sockaddr_in *addr;
+    struct sockaddr_in6 *addr6;
+    char buffer[INET6_ADDRSTRLEN];
+    char *string;
+    /*
+     * Input section: decode command line parameters 
+     * Use getopt function
+     */
+    opterr = 0;         /* don't want writing to stderr */
+    while ( (i = getopt(argc, argv, "h")) != -1) {
+       switch (i) {
+       /* 
+        * Handling options 
+        */ 
+       case 'h':                                            /* help option */
+           printf("Wrong -h option use\n");
+           usage();
+           return -1;
+           break;
+       case '?':                                    /* unrecognized options */
+           printf("Unrecognized options -%c\n",optopt);
+           usage();
+       default:                                       /* should not reached */
+           usage();
+       }
+    }
+    /* ***********************************************************
+     * 
+     *          Options processing completed
+     *
+     *               Main code beginning
+     * 
+     * ***********************************************************/
+    if ((argc - optind) != 2) {
+       printf("Wrong number of arguments %d\n", argc - optind);
+        usage();
+    }
+    
+    ret = getaddrinfo(argv[1], argv[2], NULL, &res);  /* main call */
+    if (ret != 0) {                                   /* on error exit */
+       printf("Resolution error %s\n", gai_strerror(ret));
+       exit(1);
+    }
+    ptr = res;                                        /* store pointer */
+    printf("Canonical name %s\n", ptr->ai_canonname); /* print cname */
+    do {
+       if (ptr->ai_family == PF_INET) {
+           printf("IPv4 address: \n");
+           addr = (struct sockaddr_in *) ptr->ai_addr;
+           port = ntohs(addr->sin_port);
+           string = inet_ntop(addr->sin_family, &addr->sin_addr, 
+                              buffer, sizeof(buffer));
+       } else if (ptr->ai_family == PF_INET6) {
+           printf("IPv6 address: \n");
+           addr6 = (struct sockaddr_in *) ptr->ai_addr;
+           port = ntohs(addr6->sin6_port);
+           string = inet_ntop(addr6->sin6_family, &addr6->sin6_addr, 
+                              buffer, sizeof(buffer));
+       } else {
+           printf("Address family error\n");
+           exit(1);
+       }       
+       printf("\tIndirizzo %s\n", string);
+       printf("\tProtocollo %i\n", ptr->ai_protocol);
+       printf("\tPorta %i\n", port);
+       
+
+       ptr = ptr->ai_next;
+    } while (ptr != NULL);
+    exit(0);
+}
+/*
+ * routine to print usage info and exit
+ */
+void usage(void) {
+    printf("Program mygethost: do an hostname resolution \n");
+    printf("Usage:\n");
+    printf("  mygethost [-h] hostname service\n");
+    printf("  -h   print this help\n");
+    exit(1);
+}