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 == AF_INET)
38 else if (a->family == AF_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 char *s, guchar family, AvahiAddress *ret_addr) {
118 if (inet_pton(family, s, ret_addr->data.data) < 0)
121 ret_addr->family = family;
126 AvahiAddress *avahi_address_from_sockaddr(const struct sockaddr* sa, AvahiAddress *ret_addr) {
130 g_assert(sa->sa_family == AF_INET || sa->sa_family == AF_INET6);
132 ret_addr->family = sa->sa_family;
134 if (sa->sa_family == AF_INET)
135 memcpy(&ret_addr->data.ipv4, &((struct sockaddr_in*) sa)->sin_addr, sizeof(ret_addr->data.ipv4));
137 memcpy(&ret_addr->data.ipv6, &((struct sockaddr_in6*) sa)->sin6_addr, sizeof(ret_addr->data.ipv6));
142 guint16 avahi_port_from_sockaddr(const struct sockaddr* sa) {
145 g_assert(sa->sa_family == AF_INET || sa->sa_family == AF_INET6);
147 if (sa->sa_family == AF_INET)
148 return ntohs(((struct sockaddr_in*) sa)->sin_port);
150 return ntohs(((struct sockaddr_in6*) sa)->sin6_port);