]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/ntp/tests/libntp/sockaddrtest.h
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / contrib / ntp / tests / libntp / sockaddrtest.h
1 #ifndef TESTS_SOCKADDRTEST_H
2 #define TESTS_SOCKADDRTEST_H
3
4 #include "ntp.h"
5 #include "ntp_stdlib.h"
6
7 sockaddr_u CreateSockaddr4(const char* address, unsigned int port) {
8         sockaddr_u s;
9         s.sa4.sin_family = AF_INET;
10         s.sa4.sin_addr.s_addr = inet_addr(address);
11         SET_PORT(&s, port);
12
13         return s;
14 }
15
16 int IsEqual(const sockaddr_u expected, const sockaddr_u actual) {
17         struct in_addr in;
18         struct in6_addr in6;
19
20         if (expected.sa.sa_family != actual.sa.sa_family) {
21                 //<< "Expected sa_family: " << expected.sa.sa_family
22                 //<< " but got: " << actual.sa.sa_family;
23                 return FALSE;
24         }
25
26         if (actual.sa.sa_family == AF_INET) { // IPv4
27                 if (expected.sa4.sin_port == actual.sa4.sin_port &&
28                         memcmp(&expected.sa4.sin_addr, &actual.sa4.sin_addr,
29                                    sizeof( in )) == 0) {
30                         return TRUE;
31                 } else {
32                         //<< "IPv4 comparision failed, expected: "
33                         //<< expected.sa4.sin_addr.s_addr
34                         //<< "(" << socktoa(&expected) << ") but was: "
35                         //<< actual.sa4.sin_addr.s_addr "(" << socktoa(&actual) << ")";
36                         return FALSE;
37                 }
38         } else if (actual.sa.sa_family == AF_INET6) { //IPv6
39                 if (expected.sa6.sin6_port == actual.sa6.sin6_port &&
40                         memcmp(&expected.sa6.sin6_addr, &actual.sa6.sin6_addr,
41                                    sizeof(in6)) == 0) {
42                         return TRUE;
43                 } else {
44                         printf("IPv6 comparision failed");
45                         return FALSE;
46                 }
47         } else { // Unknown family
48                 printf("Unknown sa_family: ");// << actual.sa.sa_family;
49                 return FALSE;
50         }
51 }
52
53
54 #endif // TESTS_SOCKADDRTEST_H
55
56
57