Statistics
| Branch: | Revision:

grapes / src / Tests / inet_test.c @ 02eac2ca

History | View | Annotate | Download (2.97 KB)

1
#include <errno.h>
2
#include <stdio.h>
3
#include <string.h>
4
#include <stdlib.h>
5
#include <unistd.h>
6

    
7
#ifdef _WIN32
8
#define _WIN32_WINNT 0x0501
9
#include <winsock2.h>
10
#include <ws2tcpip.h>
11
#else
12
#include <arpa/inet.h>
13
#include <netdb.h>
14
#include <netinet/in.h>
15
#include <sys/socket.h>
16
#include <fcntl.h>
17
#endif
18

    
19
int checkIPv4Translation()
20
{
21
        int res = 0;
22
        struct sockaddr_storage s;
23
        struct sockaddr_storage c;
24
        char *buf = malloc(INET_ADDRSTRLEN);
25
        char *ipv4 = malloc(INET_ADDRSTRLEN);
26
        strcpy (ipv4, "10.23.23.10");
27
        printf("\tTranslating IPv4 string %s...",ipv4);
28
        res = inet_pton(AF_INET, ipv4, &((struct sockaddr_in *)&s)->sin_addr);
29
        if(!res)
30
        {
31
                perror ("inet");
32
                return res;
33
        }
34
        printf("to network %d\n", ((struct sockaddr_in *)&s)->sin_addr);
35
        printf("\tTranslating IPv4 address %d...",((struct sockaddr_in *)&s)->sin_addr);
36
        res = inet_ntop(AF_INET, &((struct sockaddr_in *)&s)->sin_addr, buf, INET_ADDRSTRLEN);
37
        if(!res)
38
        {
39
                perror ("inet");
40
                return res;
41
        }
42
        printf("to string %s\n", buf);
43
        res = inet_pton(AF_INET, buf, &((struct sockaddr_in *)&c)->sin_addr);
44
        return memcmp((const void *)&s,(const void *)&c,sizeof(c));
45
}
46

    
47
int checkIPv6Translation()
48
{
49
        int res = 0, i;
50
        struct sockaddr_storage s;
51
        struct sockaddr_storage c;
52
        char *buf = malloc(INET6_ADDRSTRLEN);
53
        char *ipv6 = malloc(INET6_ADDRSTRLEN);
54
        ipv6 = "fe80:0000:0000:0000:0223:aeff:fe54:ea0c";
55
        printf("\tIPv6 Address string %s ", ipv6);
56
        res = inet_pton(AF_INET6, ipv6, &((struct sockaddr_in6 *)&s)->sin6_addr);
57
        if(!res)
58
        {
59
                perror ("inet");
60
                return res;
61
        }
62
        printf("to network ");
63
        for (i = 0; i < 16; i+=2)
64
        #ifdef _WIN32
65
           printf("%02x%02x:", (unsigned char) (((struct sockaddr_in6 *)&s)->sin6_addr.s6_addr[i]),(unsigned char) (((struct sockaddr_in6 *)&s)->sin6_addr.s6_addr[i+1]));
66
        #else
67
            printf("%02x%02x:", (uint8_t) (((struct sockaddr_in6 *)&s)->sin6_addr.__in6_u.__u6_addr8[i]),(uint8_t) (((struct sockaddr_in6 *)&s)->sin6_addr.__in6_u.__u6_addr8[i+1]));
68
        #endif
69
            printf("\n");
70
        printf("\tIPv6 Address ");
71
        for (i = 0; i < 16; i+=2)
72
                #ifdef _WIN32
73
                   printf("%02x%02x:", (unsigned char) (((struct sockaddr_in6 *)&s)->sin6_addr.s6_addr[i]),(unsigned char) (((struct sockaddr_in6 *)&s)->sin6_addr.s6_addr[i+1]));
74
                #else
75
                    printf("%02x%02x:", (uint8_t) (((struct sockaddr_in6 *)&s)->sin6_addr.__in6_u.__u6_addr8[i]),(uint8_t) (((struct sockaddr_in6 *)&s)->sin6_addr.__in6_u.__u6_addr8[i+1]));
76
                #endif
77
        res = inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&s)->sin6_addr, buf, INET6_ADDRSTRLEN);
78
        if(!res)
79
        {
80
                perror ("inet");
81
                return res;
82
        }
83
        printf(" to string %s\n",buf);
84
        res = inet_pton(AF_INET6, buf, &((struct sockaddr_in6 *)&c)->sin6_addr);
85
        return memcmp((const void *)&s,(const void *)&c,sizeof(c));
86
}
87

    
88
int main(int argc, char *argv[])
89
{
90
        int res = 0;
91
        if(!checkIPv4Translation())
92
        {
93
                fprintf(stderr, "IPv4 Translation failed");
94
                return 1;
95
        }
96
        printf("IPv4 Translation success\n");
97
        if(!checkIPv6Translation())
98
        {
99
                fprintf(stderr, "IPv6 Translation failed");
100
                return 1;
101
        }
102
        printf("IPv6 Translation success\n");
103
        return res;
104
}