4 This file is part of avahi.
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.
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.
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
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <arpa/inet.h>
35 size_t avahi_address_get_size(const AvahiAddress *a) {
38 if (a->proto == AVAHI_PROTO_INET)
40 else if (a->proto == AVAHI_PROTO_INET6)
46 int avahi_address_cmp(const AvahiAddress *a, const AvahiAddress *b) {
50 if (a->proto != b->proto)
53 return memcmp(a->data.data, b->data.data, avahi_address_get_size(a));
56 char *avahi_address_snprint(char *s, size_t length, const AvahiAddress *a) {
61 return (char*) inet_ntop(avahi_proto_to_af(a->proto), a->data.data, s, length);
64 char* avahi_reverse_lookup_name_ipv4(const AvahiIPv4Address *a) {
65 uint32_t n = ntohl(a->address);
68 return avahi_strdup_printf("%u.%u.%u.%u.in-addr.arpa", n & 0xFF, (n >> 8) & 0xFF, (n >> 16) & 0xFF, n >> 24);
71 static char *reverse_lookup_name_ipv6(const AvahiIPv6Address *a, const char *suffix) {
73 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.%s",
109 char *avahi_reverse_lookup_name_ipv6_arpa(const AvahiIPv6Address *a) {
110 return reverse_lookup_name_ipv6(a, "ip6.arpa");
113 char *avahi_reverse_lookup_name_ipv6_int(const AvahiIPv6Address *a) {
114 return reverse_lookup_name_ipv6(a, "ip6.int");
117 AvahiAddress *avahi_address_parse(const char *s, AvahiProtocol proto, AvahiAddress *ret_addr) {
121 if (proto == AVAHI_PROTO_UNSPEC) {
122 if (inet_pton(AF_INET, s, ret_addr->data.data) <= 0) {
123 if (inet_pton(AF_INET6, s, ret_addr->data.data) <= 0)
126 ret_addr->proto = AVAHI_PROTO_INET6;
128 ret_addr->proto = AVAHI_PROTO_INET;
130 if (inet_pton(avahi_proto_to_af(proto), s, ret_addr->data.data) <= 0)
133 ret_addr->proto = proto;
139 AvahiAddress *avahi_address_from_sockaddr(const struct sockaddr* sa, AvahiAddress *ret_addr) {
143 assert(sa->sa_family == AF_INET || sa->sa_family == AF_INET6);
145 ret_addr->proto = avahi_af_to_proto(sa->sa_family);
147 if (sa->sa_family == AF_INET)
148 memcpy(&ret_addr->data.ipv4, &((const struct sockaddr_in*) sa)->sin_addr, sizeof(ret_addr->data.ipv4));
150 memcpy(&ret_addr->data.ipv6, &((const struct sockaddr_in6*) sa)->sin6_addr, sizeof(ret_addr->data.ipv6));
155 uint16_t avahi_port_from_sockaddr(const struct sockaddr* sa) {
158 assert(sa->sa_family == AF_INET || sa->sa_family == AF_INET6);
160 if (sa->sa_family == AF_INET)
161 return ntohs(((const struct sockaddr_in*) sa)->sin_port);
163 return ntohs(((const struct sockaddr_in6*) sa)->sin6_port);
166 int avahi_address_is_ipv4_in_ipv6(const AvahiAddress *a) {
168 static const uint8_t ipv4_in_ipv6[] = {
169 0x00, 0x00, 0x00, 0x00,
170 0x00, 0x00, 0x00, 0x00,
171 0xFF, 0xFF, 0xFF, 0xFF
176 if (a->proto != AVAHI_PROTO_INET6)
179 return memcmp(a->data.ipv6.address, ipv4_in_ipv6, sizeof(ipv4_in_ipv6)) == 0;
182 int avahi_proto_to_af(AvahiProtocol proto) {
183 if (proto == AVAHI_PROTO_INET)
185 if (proto == AVAHI_PROTO_INET6)
188 assert(proto == AVAHI_PROTO_UNSPEC);
192 AvahiProtocol avahi_af_to_proto(int af) {
194 return AVAHI_PROTO_INET;
196 return AVAHI_PROTO_INET6;
198 assert(af == AF_UNSPEC);
199 return AVAHI_PROTO_UNSPEC;
202 const char* avahi_proto_to_string(AvahiProtocol proto) {
203 if (proto == AVAHI_PROTO_INET)
205 if (proto == AVAHI_PROTO_INET6)
208 assert(proto == AVAHI_PROTO_UNSPEC);