Statistics
| Branch: | Revision:

grapes / src / Tests / inet_test.c @ 1bb51f8f

History | View | Annotate | Download (3.07 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;
22
        const char *res1;
23
        struct sockaddr_storage s;
24
        struct sockaddr_storage c;
25
        char *buf = malloc(INET_ADDRSTRLEN);
26
        char *ipv4 = malloc(INET_ADDRSTRLEN);
27
        strcpy (ipv4, "10.23.23.10");
28
        printf("\tTranslating IPv4 string %s...",ipv4);
29
        res = inet_pton(AF_INET, ipv4, &((struct sockaddr_in *)&s)->sin_addr);
30
        if(!res)
31
        {
32
                perror ("inet");
33
                return res;
34
        }
35
        printf("to network %d\n", ((struct sockaddr_in *)&s)->sin_addr.s_addr);
36
        printf("\tTranslating IPv4 address %d...", ((struct sockaddr_in *)&s)->sin_addr.s_addr);
37
        res1 = inet_ntop(AF_INET, &((struct sockaddr_in *)&s)->sin_addr, buf, INET_ADDRSTRLEN);
38
        if(!res1)
39
        {
40
                perror ("inet");
41
                return res;
42
        }
43
        printf("to string %s\n", buf);
44
        res = inet_pton(AF_INET, buf, &((struct sockaddr_in *)&c)->sin_addr);
45
        return memcmp((const void *)&s,(const void *)&c,sizeof(c));
46
}
47

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

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