]> git.meshlink.io Git - catta/blob - address.c
9099ad9b00e8d36fd65f534031dfe7cdeffb9ad6
[catta] / address.c
1 #include <sys/types.h>
2 #include <sys/socket.h>
3 #include <arpa/inet.h>
4 #include <string.h>
5
6 #include "address.h"
7
8 guint flx_address_get_size(const flxAddress *a) {
9     g_assert(a);
10
11     if (a->family == AF_INET)
12         return 4;
13     else if (a->family == AF_INET6)
14         return 16;
15
16     return 0;
17 }
18
19 gint flx_address_cmp(const flxAddress *a, const flxAddress *b) {
20     g_assert(a);
21     g_assert(b);
22     
23     if (a->family != b->family)
24         return -1;
25
26     return memcmp(a, b, flx_address_get_size(a));
27 }
28
29 gchar *flx_address_snprint(char *s, guint length, const flxAddress *a) {
30     g_assert(s);
31     g_assert(length);
32     g_assert(a);
33     return (gchar*) inet_ntop(a->family, a->data, s, length);
34 }
35
36 gchar* flx_reverse_lookup_name_ipv4(const flxIPv4Address *a) {
37     guint32 n = ntohl(a->address);
38     g_assert(a);
39
40     return g_strdup_printf("%u.%u.%u.%u.in-addr.arpa", n & 0xFF, (n >> 8) & 0xFF, (n >> 16) & 0xFF, n >> 24);
41 }
42
43 static gchar *reverse_lookup_name_ipv6(const flxIPv6Address *a, const gchar *suffix) {
44     
45     return g_strdup_printf("%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%s",
46                            a->address[15] & 0xF,
47                            a->address[15] >> 4,
48                            a->address[14] & 0xF,
49                            a->address[14] >> 4,
50                            a->address[13] & 0xF,
51                            a->address[13] >> 4,
52                            a->address[12] & 0xF,
53                            a->address[12] >> 4,
54                            a->address[11] & 0xF,
55                            a->address[11] >> 4,
56                            a->address[10] & 0xF,
57                            a->address[10] >> 4,
58                            a->address[9] & 0xF,
59                            a->address[9] >> 4,
60                            a->address[8] & 0xF,
61                            a->address[8] >> 4,
62                            a->address[7] & 0xF,
63                            a->address[7] >> 4,
64                            a->address[6] & 0xF,
65                            a->address[6] >> 4,
66                            a->address[5] & 0xF,
67                            a->address[5] >> 4,
68                            a->address[4] & 0xF,
69                            a->address[4] >> 4,
70                            a->address[3] & 0xF,
71                            a->address[3] >> 4,
72                            a->address[2] & 0xF,
73                            a->address[2] >> 4,
74                            a->address[1] & 0xF,
75                            a->address[1] >> 4,
76                            a->address[0] & 0xF,
77                            a->address[0] >> 4,
78                            suffix);
79 }
80
81 gchar *flx_reverse_lookup_name_ipv6_arpa(const flxIPv6Address *a) {
82     return reverse_lookup_name_ipv6(a, "ip6.arpa");
83 }
84
85 gchar *flx_reverse_lookup_name_ipv6_int(const flxIPv6Address *a) {
86     return reverse_lookup_name_ipv6(a, "ip6.int");
87 }
88
89 flxAddress *flx_address_parse(const char *s, int family, flxAddress *ret_addr) {
90     g_assert(ret_addr);
91     g_assert(s);
92
93     if (inet_pton(family, s, ret_addr->data) < 0)
94         return NULL;
95
96     ret_addr->family = family;
97     
98     return ret_addr;
99 }