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 <netinet/in.h>
28 #include <sys/socket.h>
29 #include <arpa/inet.h>
37 static size_t address_get_size(const AvahiAddress *a) {
40 if (a->proto == AVAHI_PROTO_INET)
42 else if (a->proto == AVAHI_PROTO_INET6)
48 int avahi_address_cmp(const AvahiAddress *a, const AvahiAddress *b) {
52 if (a->proto != b->proto)
55 return memcmp(a->data.data, b->data.data, address_get_size(a));
58 char *avahi_address_snprint(char *s, size_t length, const AvahiAddress *a) {
63 if (!(inet_ntop(avahi_proto_to_af(a->proto), a->data.data, s, length)))
69 char* avahi_reverse_lookup_name(const AvahiAddress *a, char *ret_s, size_t length) {
74 if (a->proto == AVAHI_PROTO_INET) {
75 uint32_t n = ntohl(a->data.ipv4.address);
78 "%u.%u.%u.%u.in-addr.arpa",
79 n & 0xFF, (n >> 8) & 0xFF, (n >> 16) & 0xFF, n >> 24);
81 assert(a->proto == AVAHI_PROTO_INET6);
85 "%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",
86 a->data.ipv6.address[15] & 0xF, a->data.ipv6.address[15] >> 4,
87 a->data.ipv6.address[14] & 0xF, a->data.ipv6.address[14] >> 4,
88 a->data.ipv6.address[13] & 0xF, a->data.ipv6.address[13] >> 4,
89 a->data.ipv6.address[12] & 0xF, a->data.ipv6.address[12] >> 4,
90 a->data.ipv6.address[11] & 0xF, a->data.ipv6.address[11] >> 4,
91 a->data.ipv6.address[10] & 0xF, a->data.ipv6.address[10] >> 4,
92 a->data.ipv6.address[ 9] & 0xF, a->data.ipv6.address[ 9] >> 4,
93 a->data.ipv6.address[ 8] & 0xF, a->data.ipv6.address[ 8] >> 4,
94 a->data.ipv6.address[ 7] & 0xF, a->data.ipv6.address[ 7] >> 4,
95 a->data.ipv6.address[ 6] & 0xF, a->data.ipv6.address[ 6] >> 4,
96 a->data.ipv6.address[ 5] & 0xF, a->data.ipv6.address[ 5] >> 4,
97 a->data.ipv6.address[ 4] & 0xF, a->data.ipv6.address[ 4] >> 4,
98 a->data.ipv6.address[ 3] & 0xF, a->data.ipv6.address[ 3] >> 4,
99 a->data.ipv6.address[ 2] & 0xF, a->data.ipv6.address[ 2] >> 4,
100 a->data.ipv6.address[ 1] & 0xF, a->data.ipv6.address[ 1] >> 4,
101 a->data.ipv6.address[ 0] & 0xF, a->data.ipv6.address[ 0] >> 4);
107 AvahiAddress *avahi_address_parse(const char *s, AvahiProtocol proto, AvahiAddress *ret_addr) {
111 if (proto == AVAHI_PROTO_UNSPEC) {
112 if (inet_pton(AF_INET, s, ret_addr->data.data) <= 0) {
113 if (inet_pton(AF_INET6, s, ret_addr->data.data) <= 0)
116 ret_addr->proto = AVAHI_PROTO_INET6;
118 ret_addr->proto = AVAHI_PROTO_INET;
120 if (inet_pton(avahi_proto_to_af(proto), s, ret_addr->data.data) <= 0)
123 ret_addr->proto = proto;
129 int avahi_proto_to_af(AvahiProtocol proto) {
130 if (proto == AVAHI_PROTO_INET)
132 if (proto == AVAHI_PROTO_INET6)
135 assert(proto == AVAHI_PROTO_UNSPEC);
139 AvahiProtocol avahi_af_to_proto(int af) {
141 return AVAHI_PROTO_INET;
143 return AVAHI_PROTO_INET6;
145 assert(af == AF_UNSPEC);
146 return AVAHI_PROTO_UNSPEC;
149 const char* avahi_proto_to_string(AvahiProtocol proto) {
150 if (proto == AVAHI_PROTO_INET)
152 if (proto == AVAHI_PROTO_INET6)
155 assert(proto == AVAHI_PROTO_UNSPEC);