inet_ntop.c
00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #define __USE_MISC 1
00004 #include <arpa/inet.h>
00005 #include <sys/socket.h>
00006 #include <netinet/in.h>
00007
00008 int main()
00009 {
00010 short int i;
00011 char tmp[INET6_ADDRSTRLEN];
00012 struct in_addr addr_ipv4;
00013 struct in6_addr addr_ipv6;
00014 char * addr4 = "192.168.0.14";
00015 char * addr_mixte = "::192.168.0.14";
00016 char * addr6 = "8000::ABC:DEF1:2345:6789";
00017
00018
00019 if ( inet_aton(addr4, &addr_ipv4) )
00020 printf("Adresse IPv4 récupérée par inet_aton: %02x\n", addr_ipv4.s_addr);
00021
00022
00023 if ( inet_pton(AF_INET, addr4, &addr_ipv4) > 0)
00024 printf("Adresse IPv4 récupérée par inet_pton: %02x\n", addr_ipv4.s_addr);
00025
00026
00027 if ( inet_pton(AF_INET6, addr_mixte, &addr_ipv6) > 0) {
00028 printf("Adresse IPv4 récupérée dans une structure IPv6 "
00029 "par inet_pton:\n ");
00030 for (i=0; i<16; i++) printf("%02x", addr_ipv6.s6_addr[i]);
00031
00032 if (inet_ntop(AF_INET6, &addr_ipv6, tmp, INET6_ADDRSTRLEN))
00033 printf("\nAffichage en notation standard: %s\n", tmp);
00034 }
00035
00036 if ( inet_pton(AF_INET6, addr6, &addr_ipv6) > 0) {
00037 printf("Adresse IPv6 récupérée par inet_pton:\n ");
00038 for (i=0; i<16; i++) printf("%02x", addr_ipv6.s6_addr[i]);
00039
00040 if (inet_ntop(AF_INET6, &addr_ipv6, tmp, INET6_ADDRSTRLEN))
00041 printf("\nAffichage en notation standard: %s\n", tmp);
00042 }
00043 return EXIT_SUCCESS;
00044 }
00045