]> git.meshlink.io Git - catta/blob - avahi-common/address.c
in order to reduce our code/API size, drop support for reverse IPv6 name lookups...
[catta] / avahi-common / 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 <netinet/in.h>
27 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include <arpa/inet.h>
30 #include <string.h>
31 #include <assert.h>
32
33 #include "address.h"
34 #include "malloc.h"
35
36 size_t avahi_address_get_size(const AvahiAddress *a) {
37     assert(a);
38
39     if (a->proto == AVAHI_PROTO_INET)
40         return 4;
41     else if (a->proto == AVAHI_PROTO_INET6)
42         return 16;
43
44     return 0;
45 }
46
47 int avahi_address_cmp(const AvahiAddress *a, const AvahiAddress *b) {
48     assert(a);
49     assert(b);
50     
51     if (a->proto != b->proto)
52         return -1;
53
54     return memcmp(a->data.data, b->data.data, avahi_address_get_size(a));
55 }
56
57 char *avahi_address_snprint(char *s, size_t length, const AvahiAddress *a) {
58     assert(s);
59     assert(length);
60     assert(a);
61     
62     if (!(inet_ntop(avahi_proto_to_af(a->proto), a->data.data, s, length)))
63         return NULL;
64     
65     return s;
66 }
67
68 char* avahi_reverse_lookup_name_ipv4(const AvahiIPv4Address *a) {
69     uint32_t n = ntohl(a->address);
70     assert(a);
71
72     return avahi_strdup_printf("%u.%u.%u.%u.in-addr.arpa", n & 0xFF, (n >> 8) & 0xFF, (n >> 16) & 0xFF, n >> 24);
73 }
74
75 char *avahi_reverse_lookup_name_ipv6(const AvahiIPv6Address *a) {
76     
77     return avahi_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.ip6.arpa",
78                            a->address[15] & 0xF,
79                            a->address[15] >> 4,
80                            a->address[14] & 0xF,
81                            a->address[14] >> 4,
82                            a->address[13] & 0xF,
83                            a->address[13] >> 4,
84                            a->address[12] & 0xF,
85                            a->address[12] >> 4,
86                            a->address[11] & 0xF,
87                            a->address[11] >> 4,
88                            a->address[10] & 0xF,
89                            a->address[10] >> 4,
90                            a->address[9] & 0xF,
91                            a->address[9] >> 4,
92                            a->address[8] & 0xF,
93                            a->address[8] >> 4,
94                            a->address[7] & 0xF,
95                            a->address[7] >> 4,
96                            a->address[6] & 0xF,
97                            a->address[6] >> 4,
98                            a->address[5] & 0xF,
99                            a->address[5] >> 4,
100                            a->address[4] & 0xF,
101                            a->address[4] >> 4,
102                            a->address[3] & 0xF,
103                            a->address[3] >> 4,
104                            a->address[2] & 0xF,
105                            a->address[2] >> 4,
106                            a->address[1] & 0xF,
107                            a->address[1] >> 4,
108                            a->address[0] & 0xF,
109                            a->address[0] >> 4);
110 }
111
112 AvahiAddress *avahi_address_parse(const char *s, AvahiProtocol proto, AvahiAddress *ret_addr) {
113     assert(ret_addr);
114     assert(s);
115
116     if (proto == AVAHI_PROTO_UNSPEC) {
117         if (inet_pton(AF_INET, s, ret_addr->data.data) <= 0) {
118             if (inet_pton(AF_INET6, s, ret_addr->data.data) <= 0)
119                 return NULL;
120             else
121                 ret_addr->proto = AVAHI_PROTO_INET6;
122         } else
123             ret_addr->proto = AVAHI_PROTO_INET;
124     } else {
125         if (inet_pton(avahi_proto_to_af(proto), s, ret_addr->data.data) <= 0)
126             return NULL;
127         
128         ret_addr->proto = proto;
129     }
130     
131     return ret_addr;
132 }
133
134 AvahiAddress *avahi_address_from_sockaddr(const struct sockaddr* sa, AvahiAddress *ret_addr) {
135     assert(sa);
136     assert(ret_addr);
137
138     assert(sa->sa_family == AF_INET || sa->sa_family == AF_INET6);
139
140     ret_addr->proto = avahi_af_to_proto(sa->sa_family);
141
142     if (sa->sa_family == AF_INET)
143         memcpy(&ret_addr->data.ipv4, &((const struct sockaddr_in*) sa)->sin_addr, sizeof(ret_addr->data.ipv4));
144     else
145         memcpy(&ret_addr->data.ipv6, &((const struct sockaddr_in6*) sa)->sin6_addr, sizeof(ret_addr->data.ipv6));
146
147     return ret_addr;
148 }
149
150 uint16_t avahi_port_from_sockaddr(const struct sockaddr* sa) {
151     assert(sa);
152
153     assert(sa->sa_family == AF_INET || sa->sa_family == AF_INET6);
154
155     if (sa->sa_family == AF_INET)
156         return ntohs(((const struct sockaddr_in*) sa)->sin_port);
157     else
158         return ntohs(((const struct sockaddr_in6*) sa)->sin6_port);
159 }
160
161 int avahi_address_is_ipv4_in_ipv6(const AvahiAddress *a) {
162
163     static const uint8_t ipv4_in_ipv6[] = {
164         0x00, 0x00, 0x00, 0x00,
165         0x00, 0x00, 0x00, 0x00,
166         0xFF, 0xFF, 0xFF, 0xFF
167     };
168     
169     assert(a);
170
171     if (a->proto != AVAHI_PROTO_INET6)
172         return 0;
173
174     return memcmp(a->data.ipv6.address, ipv4_in_ipv6, sizeof(ipv4_in_ipv6)) == 0;
175 }
176
177 int avahi_proto_to_af(AvahiProtocol proto) {
178     if (proto == AVAHI_PROTO_INET)
179         return AF_INET;
180     if (proto == AVAHI_PROTO_INET6)
181         return AF_INET6;
182
183     assert(proto == AVAHI_PROTO_UNSPEC);
184     return AF_UNSPEC;
185 }
186
187 AvahiProtocol avahi_af_to_proto(int af) {
188     if (af == AF_INET)
189         return AVAHI_PROTO_INET;
190     if (af == AF_INET6)
191         return AVAHI_PROTO_INET6;
192
193     assert(af == AF_UNSPEC);
194     return AVAHI_PROTO_UNSPEC;
195 }
196
197 const char* avahi_proto_to_string(AvahiProtocol proto) {
198     if (proto == AVAHI_PROTO_INET)
199         return "IPv4";
200     if (proto == AVAHI_PROTO_INET6)
201         return "IPv6";
202
203     assert(proto == AVAHI_PROTO_UNSPEC);
204     return "UNSPEC";
205 }