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 <netinet/in.h>
27 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include <arpa/inet.h>
36 size_t avahi_address_get_size(const AvahiAddress *a) {
39 if (a->proto == AVAHI_PROTO_INET)
41 else if (a->proto == AVAHI_PROTO_INET6)
47 int avahi_address_cmp(const AvahiAddress *a, const AvahiAddress *b) {
51 if (a->proto != b->proto)
54 return memcmp(a->data.data, b->data.data, avahi_address_get_size(a));
57 char *avahi_address_snprint(char *s, size_t length, const AvahiAddress *a) {
62 if (!(inet_ntop(avahi_proto_to_af(a->proto), a->data.data, s, length)))
68 char* avahi_reverse_lookup_name_ipv4(const AvahiIPv4Address *a) {
69 uint32_t n = ntohl(a->address);
72 return avahi_strdup_printf("%u.%u.%u.%u.in-addr.arpa", n & 0xFF, (n >> 8) & 0xFF, (n >> 16) & 0xFF, n >> 24);
75 char *avahi_reverse_lookup_name_ipv6(const AvahiIPv6Address *a) {
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",
112 AvahiAddress *avahi_address_parse(const char *s, AvahiProtocol proto, AvahiAddress *ret_addr) {
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)
121 ret_addr->proto = AVAHI_PROTO_INET6;
123 ret_addr->proto = AVAHI_PROTO_INET;
125 if (inet_pton(avahi_proto_to_af(proto), s, ret_addr->data.data) <= 0)
128 ret_addr->proto = proto;
134 AvahiAddress *avahi_address_from_sockaddr(const struct sockaddr* sa, AvahiAddress *ret_addr) {
138 assert(sa->sa_family == AF_INET || sa->sa_family == AF_INET6);
140 ret_addr->proto = avahi_af_to_proto(sa->sa_family);
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));
145 memcpy(&ret_addr->data.ipv6, &((const struct sockaddr_in6*) sa)->sin6_addr, sizeof(ret_addr->data.ipv6));
150 uint16_t avahi_port_from_sockaddr(const struct sockaddr* sa) {
153 assert(sa->sa_family == AF_INET || sa->sa_family == AF_INET6);
155 if (sa->sa_family == AF_INET)
156 return ntohs(((const struct sockaddr_in*) sa)->sin_port);
158 return ntohs(((const struct sockaddr_in6*) sa)->sin6_port);
161 int avahi_address_is_ipv4_in_ipv6(const AvahiAddress *a) {
163 static const uint8_t ipv4_in_ipv6[] = {
164 0x00, 0x00, 0x00, 0x00,
165 0x00, 0x00, 0x00, 0x00,
166 0xFF, 0xFF, 0xFF, 0xFF
171 if (a->proto != AVAHI_PROTO_INET6)
174 return memcmp(a->data.ipv6.address, ipv4_in_ipv6, sizeof(ipv4_in_ipv6)) == 0;
177 int avahi_proto_to_af(AvahiProtocol proto) {
178 if (proto == AVAHI_PROTO_INET)
180 if (proto == AVAHI_PROTO_INET6)
183 assert(proto == AVAHI_PROTO_UNSPEC);
187 AvahiProtocol avahi_af_to_proto(int af) {
189 return AVAHI_PROTO_INET;
191 return AVAHI_PROTO_INET6;
193 assert(af == AF_UNSPEC);
194 return AVAHI_PROTO_UNSPEC;
197 const char* avahi_proto_to_string(AvahiProtocol proto) {
198 if (proto == AVAHI_PROTO_INET)
200 if (proto == AVAHI_PROTO_INET6)
203 assert(proto == AVAHI_PROTO_UNSPEC);