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>
33 guint avahi_address_get_size(const AvahiAddress *a) {
36 if (a->family == AVAHI_PROTO_INET)
38 else if (a->family == AVAHI_PROTO_INET6)
44 gint avahi_address_cmp(const AvahiAddress *a, const AvahiAddress *b) {
48 if (a->family != b->family)
51 return memcmp(a->data.data, b->data.data, avahi_address_get_size(a));
54 gchar *avahi_address_snprint(char *s, guint length, const AvahiAddress *a) {
58 return (gchar*) inet_ntop(a->family, a->data.data, s, length);
61 gchar* avahi_reverse_lookup_name_ipv4(const AvahiIPv4Address *a) {
62 guint32 n = ntohl(a->address);
65 return g_strdup_printf("%u.%u.%u.%u.in-addr.arpa", n & 0xFF, (n >> 8) & 0xFF, (n >> 16) & 0xFF, n >> 24);
68 static gchar *reverse_lookup_name_ipv6(const AvahiIPv6Address *a, const gchar *suffix) {
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",
106 gchar *avahi_reverse_lookup_name_ipv6_arpa(const AvahiIPv6Address *a) {
107 return reverse_lookup_name_ipv6(a, "ip6.arpa");
110 gchar *avahi_reverse_lookup_name_ipv6_int(const AvahiIPv6Address *a) {
111 return reverse_lookup_name_ipv6(a, "ip6.int");
114 AvahiAddress *avahi_address_parse(const gchar *s, AvahiProtocol family, AvahiAddress *ret_addr) {
118 if (family == AVAHI_PROTO_UNSPEC) {
119 if (inet_pton(AF_INET, s, ret_addr->data.data) <= 0) {
120 if (inet_pton(AF_INET6, s, ret_addr->data.data) <= 0)
123 ret_addr->family = AVAHI_PROTO_INET6;
125 ret_addr->family = AVAHI_PROTO_INET;
127 if (inet_pton(family, s, ret_addr->data.data) <= 0)
130 ret_addr->family = family;
136 AvahiAddress *avahi_address_from_sockaddr(const struct sockaddr* sa, AvahiAddress *ret_addr) {
140 g_assert(sa->sa_family == AF_INET || sa->sa_family == AF_INET6);
142 ret_addr->family = sa->sa_family;
144 if (sa->sa_family == AF_INET)
145 memcpy(&ret_addr->data.ipv4, &((struct sockaddr_in*) sa)->sin_addr, sizeof(ret_addr->data.ipv4));
147 memcpy(&ret_addr->data.ipv6, &((struct sockaddr_in6*) sa)->sin6_addr, sizeof(ret_addr->data.ipv6));
152 guint16 avahi_port_from_sockaddr(const struct sockaddr* sa) {
155 g_assert(sa->sa_family == AF_INET || sa->sa_family == AF_INET6);
157 if (sa->sa_family == AF_INET)
158 return ntohs(((struct sockaddr_in*) sa)->sin_port);
160 return ntohs(((struct sockaddr_in6*) sa)->sin6_port);
163 gboolean avahi_address_is_ipv4_in_ipv6(const AvahiAddress *a) {
164 static const guint8 ipv4_in_ipv6[] = {
165 0x00, 0x00, 0x00, 0x00,
166 0x00, 0x00, 0x00, 0x00,
167 0xFF, 0xFF, 0xFF, 0xFF };
171 if (a->family != AVAHI_PROTO_INET6)
174 return memcmp(a->data.ipv6.address, ipv4_in_ipv6, sizeof(ipv4_in_ipv6)) == 0;