Difference between revisions of "La commande haah (host-address-address-host)"

From Livre IPv6

 
Line 16: Line 16:
 
Le programme réalisant la commande <tt>haah</tt> ne présente aucune difficulté. C'est une simple application des primitives précédemment décrites.
 
Le programme réalisant la commande <tt>haah</tt> ne présente aucune difficulté. C'est une simple application des primitives précédemment décrites.
 
   
 
   
+
{|
  #include <stdio.h>
+
|#|| #include <stdio.h>
#include <string.h>
+
|-
#include <errno.h>
+
|#||
#include <sys/types.h>
+
|-
#include <sys/socket.h>
+
|#|| #include <string.h>
#include <netinet/in.h>
+
|-
#include <netdb.h>
+
|#|| #include <errno.h>
#include <arpa/inet.h>
+
|-
 
+
|#|| #include <sys/types.h>
 
+
|-
  int main(int argc, char **argv)
+
|#|| #include <sys/socket.h>
{
+
|-
    int ret;
+
|#|| #include <netinet/in.h>
    struct addrinfo *res, *ptr;
+
|-
    struct addrinfo hints = {
+
|#|| #include <netdb.h>
      AI_CANONNAME,
+
|-
      PF_UNSPEC,
+
|#|| #include <arpa/inet.h>
      SOCK_STREAM,
+
|-
      0,
+
|#|| 
      0,
+
|-
      NULL,
+
|#||  
      NULL,
+
|-
      NULL
+
|#|| int main(int argc, char **argv)
    };
+
|-
 
+
|#|| {
    if (argc != 2) {
+
|-
      fprintf(stderr, "%s: usage: %s host | addr.\n", *argv, *argv);
+
|#||    int ret;
      exit(1);
+
|-
    }
+
|#||    struct addrinfo *res, *ptr;
    ret = getaddrinfo(argv[1], NULL, &hints, &res);
+
|-
    if (ret) {
+
|#||    struct addrinfo hints = {
      fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ret));
+
|-
      exit(1);
+
|#||      AI_CANONNAME,
    }
+
|-
    for (ptr = res; ptr; ptr = ptr->ai_next) {
+
|#||      PF_UNSPEC,
      if (ptr->ai_canonname)
+
|-
          fprintf(stdout,"Canonical name:\n%s\nAdresses:\n", ptr->ai_canonname);
+
|#||      SOCK_STREAM,
      switch (ptr->ai_family) {
+
|-
          case AF_INET:
+
|#||      0,
          {
+
|-
            char dst[INET_ADDRSTRLEN];
+
|#||      0,
            struct in_addr *src = &((struct sockaddr_in *) ptr->ai_addr)->sin_addr;
+
|-
     
+
|#||      NULL,
            if(!inet_ntop(AF_INET, (const void *) src, dst, sizeof(dst))) {
+
|-
                fprintf(stderr, "inet_ntop: %s\n", strerror(errno));
+
|#||      NULL,
                break;
+
|-
            }
+
|#||      NULL
            fprintf(stdout, "%s\n", dst);
+
|-
            break;
+
|#||    };
          }  
+
|-
          case AF_INET6:
+
|#|| 
          {
+
|-
            char dst[INET6_ADDRSTRLEN];
+
|#||    if (argc != 2) {
            struct in6_addr *src=&((struct sockaddr_in6 *)ptr->ai_addr)->sin6_addr;
+
|-
   
+
|#||      fprintf(stderr, "%s: usage: %s host | addr.\n", *argv, *argv);
            if (!inet_ntop(AF_INET6, (const void *) src, dst, sizeof(dst))) {
+
|-
                fprintf(stderr, "inet_ntop: %s\n", strerror(errno));
+
|#||      exit(1);
                break;
+
|-
            }
+
|#||    }
            fprintf(stdout, "%s\n", dst);
+
|-
            break;
+
|#||    ret = getaddrinfo(argv[1], NULL, &hints, &res);
          }
+
|-
          default:
+
|#||    if (ret) {
            fprintf(stderr, "getaddrinfo: %s\n", strerror(EAFNOSUPPORT));
+
|-
      }
+
|#||      fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ret));
    }  
+
|-
    freeaddrinfo(res);
+
|#||      exit(1);
    exit(0);
+
|-
}
+
|#||    }
 +
|-
 +
|#||    for (ptr = res; ptr; ptr = ptr->ai_next) {
 +
|-
 +
|#||      if (ptr->ai_canonname)
 +
|-
 +
|#||          fprintf(stdout,"Canonical name:\n%s\nAdresses:\n", ptr->ai_canonname);
 +
|-
 +
|#||      switch (ptr->ai_family) {
 +
|-
 +
|#||          case AF_INET:
 +
|-
 +
|#||          {
 +
|-
 +
|#||            char dst[INET_ADDRSTRLEN];
 +
|-
 +
|#||            struct in_addr *src = &((struct sockaddr_in *) ptr->ai_addr)->sin_addr;
 +
|-
 +
|#||     
 +
|-
 +
|#||            if(!inet_ntop(AF_INET, (const void *) src, dst, sizeof(dst))) {
 +
|-
 +
|#||                fprintf(stderr, "inet_ntop: %s\n", strerror(errno));
 +
|-
 +
|#||                break;
 +
|-
 +
|#||            }
 +
|-
 +
|#||            fprintf(stdout, "%s\n", dst);
 +
|-
 +
|#||            break;
 +
|-
 +
|#||          }  
 +
|-
 +
|#||          case AF_INET6:
 +
|-
 +
|#||          {
 +
|-
 +
|#||            char dst[INET6_ADDRSTRLEN];
 +
|-
 +
|#||            struct in6_addr *src=&((struct sockaddr_in6 *)ptr->ai_addr)->sin6_addr;
 +
|-
 +
|#||   
 +
|-
 +
|#||            if (!inet_ntop(AF_INET6, (const void *) src, dst, sizeof(dst))) {
 +
|-
 +
|#||                fprintf(stderr, "inet_ntop: %s\n", strerror(errno));
 +
|-
 +
|#||                break;
 +
|-
 +
|#||            }
 +
|-
 +
|#||            fprintf(stdout, "%s\n", dst);
 +
|-
 +
|#||            break;
 +
|-
 +
|#||          }
 +
|-
 +
|#||          default:
 +
|-
 +
|#||            fprintf(stderr, "getaddrinfo: %s\n", strerror(EAFNOSUPPORT));
 +
|-
 +
|#||      }
 +
|-
 +
|#||    }  
 +
|-
 +
|#||    freeaddrinfo(res);
 +
|-
 +
|#||    exit(0);
 +
|-
 +
|#|| }
 +
|}

Revision as of 20:15, 15 December 2005

L'exemple proposé n'est autre qu'une sorte de nslookup (très) simplifié. Si par exemple on lui donne en argument une adresse numérique (IPv4 ou IPv6), il imprime le nom complètement qualifié correspondant lorsque la requête DNS aboutit. L'extrait de session qui suit illustre l'utilisation de cette commande.

$ haah bernays
Canonical name:
bernays.ipv6.logique.jussieu.fr
Adresses:
2001:660:101:101:200:f8ff:fe31:17ec
3ffe:304:101:1:200:f8ff:fe31:17ec
$ haah 134.157.19.71
Canonical name:
bernays.logique.jussieu.fr
Adresses:
134.157.19.71
$

Le programme réalisant la commande haah ne présente aucune difficulté. C'est une simple application des primitives précédemment décrites.

# #include <stdio.h>
#
# #include <string.h>
# #include <errno.h>
# #include <sys/types.h>
# #include <sys/socket.h>
# #include <netinet/in.h>
# #include <netdb.h>
# #include <arpa/inet.h>
#
#
# int main(int argc, char **argv)
# {
# int ret;
# struct addrinfo *res, *ptr;
# struct addrinfo hints = {
# AI_CANONNAME,
# PF_UNSPEC,
# SOCK_STREAM,
# 0,
# 0,
# NULL,
# NULL,
# NULL
# };
#
# if (argc != 2) {
# addr.\n", *argv, *argv);
# exit(1);
# }
# ret = getaddrinfo(argv[1], NULL, &hints, &res);
# if (ret) {
# fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ret));
# exit(1);
# }
# for (ptr = res; ptr; ptr = ptr->ai_next) {
# if (ptr->ai_canonname)
# fprintf(stdout,"Canonical name:\n%s\nAdresses:\n", ptr->ai_canonname);
# switch (ptr->ai_family) {
# case AF_INET:
# {
# char dst[INET_ADDRSTRLEN];
# struct in_addr *src = &((struct sockaddr_in *) ptr->ai_addr)->sin_addr;
#
# if(!inet_ntop(AF_INET, (const void *) src, dst, sizeof(dst))) {
# fprintf(stderr, "inet_ntop: %s\n", strerror(errno));
# break;
# }
# fprintf(stdout, "%s\n", dst);
# break;
# }
# case AF_INET6:
# {
# char dst[INET6_ADDRSTRLEN];
# struct in6_addr *src=&((struct sockaddr_in6 *)ptr->ai_addr)->sin6_addr;
#
# if (!inet_ntop(AF_INET6, (const void *) src, dst, sizeof(dst))) {
# fprintf(stderr, "inet_ntop: %s\n", strerror(errno));
# break;
# }
# fprintf(stdout, "%s\n", dst);
# break;
# }
# default:
# fprintf(stderr, "getaddrinfo: %s\n", strerror(EAFNOSUPPORT));
# }
# }
# freeaddrinfo(res);
# exit(0);
# }
Personal tools