]> git.meshlink.io Git - catta/blob - avahi-core/address.c
34f85812584b88e2bdc700a1eda0ca2299fb0f88
[catta] / avahi-core / address.c
1 /* $Id$ */
2
3 /***
4   This file is part of avahi.
5  
6   avahi is free software; you can redistribute it and/or modify it
7   under the terms of the GNU Lesser General Public License as
8   published by the Free Software Foundation; either version 2.1 of the
9   License, or (at your option) any later version.
10  
11   avahi is distributed in the hope that it will be useful, but WITHOUT
12   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14   Public License for more details.
15  
16   You should have received a copy of the GNU Lesser General Public
17   License along with avahi; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <arpa/inet.h>
29 #include <string.h>
30
31 #include "address.h"
32
33 guint avahi_address_get_size(const AvahiAddress *a) {
34     g_assert(a);
35
36     if (a->family == AF_INET)
37         return 4;
38     else if (a->family == AF_INET6)
39         return 16;
40
41     return 0;
42 }
43
44 gint avahi_address_cmp(const AvahiAddress *a, const AvahiAddress *b) {
45     g_assert(a);
46     g_assert(b);
47     
48     if (a->family != b->family)
49         return -1;
50
51     return memcmp(a->data.data, b->data.data, avahi_address_get_size(a));
52 }
53
54 gchar *avahi_address_snprint(char *s, guint length, const AvahiAddress *a) {
55     g_assert(s);
56     g_assert(length);
57     g_assert(a);
58     return (gchar*) inet_ntop(a->family, a->data.data, s, length);
59 }
60
61 gchar* avahi_reverse_lookup_name_ipv4(const AvahiIPv4Address *a) {
62     guint32 n = ntohl(a->address);
63     g_assert(a);
64
65     return g_strdup_printf("%u.%u.%u.%u.in-addr.arpa", n & 0xFF, (n >> 8) & 0xFF, (n >> 16) & 0xFF, n >> 24);
66 }
67
68 static gchar *reverse_lookup_name_ipv6(const AvahiIPv6Address *a, const gchar *suffix) {
69     
70     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",
71                            a->address[15] & 0xF,
72                            a->address[15] >> 4,
73                            a->address[14] & 0xF,
74                            a->address[14] >> 4,
75                            a->address[13] & 0xF,
76                            a->address[13] >> 4,
77                            a->address[12] & 0xF,
78                            a->address[12] >> 4,
79                            a->address[11] & 0xF,
80                            a->address[11] >> 4,
81                            a->address[10] & 0xF,
82                            a->address[10] >> 4,
83                            a->address[9] & 0xF,
84                            a->address[9] >> 4,
85                            a->address[8] & 0xF,
86                            a->address[8] >> 4,
87                            a->address[7] & 0xF,
88                            a->address[7] >> 4,
89                            a->address[6] & 0xF,
90                            a->address[6] >> 4,
91                            a->address[5] & 0xF,
92                            a->address[5] >> 4,
93                            a->address[4] & 0xF,
94                            a->address[4] >> 4,
95                            a->address[3] & 0xF,
96                            a->address[3] >> 4,
97                            a->address[2] & 0xF,
98                            a->address[2] >> 4,
99                            a->address[1] & 0xF,
100                            a->address[1] >> 4,
101                            a->address[0] & 0xF,
102                            a->address[0] >> 4,
103                            suffix);
104 }
105
106 gchar *avahi_reverse_lookup_name_ipv6_arpa(const AvahiIPv6Address *a) {
107     return reverse_lookup_name_ipv6(a, "ip6.arpa");
108 }
109
110 gchar *avahi_reverse_lookup_name_ipv6_int(const AvahiIPv6Address *a) {
111     return reverse_lookup_name_ipv6(a, "ip6.int");
112 }
113
114 AvahiAddress *avahi_address_parse(const char *s, guchar family, AvahiAddress *ret_addr) {
115     g_assert(ret_addr);
116     g_assert(s);
117
118     if (inet_pton(family, s, ret_addr->data.data) < 0)
119         return NULL;
120
121     ret_addr->family = family;
122     
123     return ret_addr;
124 }
125
126 AvahiAddress *avahi_address_from_sockaddr(const struct sockaddr* sa, AvahiAddress *ret_addr) {
127     g_assert(sa);
128     g_assert(ret_addr);
129
130     g_assert(sa->sa_family == AF_INET || sa->sa_family == AF_INET6);
131
132     ret_addr->family = sa->sa_family;
133
134     if (sa->sa_family == AF_INET)
135         memcpy(&ret_addr->data.ipv4, &((struct sockaddr_in*) sa)->sin_addr, sizeof(ret_addr->data.ipv4));
136     else
137         memcpy(&ret_addr->data.ipv6, &((struct sockaddr_in6*) sa)->sin6_addr, sizeof(ret_addr->data.ipv6));
138
139     return ret_addr;
140 }
141
142 guint16 avahi_port_from_sockaddr(const struct sockaddr* sa) {
143     g_assert(sa);
144
145     g_assert(sa->sa_family == AF_INET || sa->sa_family == AF_INET6);
146
147     if (sa->sa_family == AF_INET)
148         return ntohs(((struct sockaddr_in*) sa)->sin_port);
149     else
150         return ntohs(((struct sockaddr_in6*) sa)->sin6_port);
151 }