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>
33 #include "addr-util.h"
35 AvahiAddress *avahi_address_from_sockaddr(const struct sockaddr* sa, AvahiAddress *ret_addr) {
39 assert(sa->sa_family == AF_INET || sa->sa_family == AF_INET6);
41 ret_addr->proto = avahi_af_to_proto(sa->sa_family);
43 if (sa->sa_family == AF_INET)
44 memcpy(&ret_addr->data.ipv4, &((const struct sockaddr_in*) sa)->sin_addr, sizeof(ret_addr->data.ipv4));
46 memcpy(&ret_addr->data.ipv6, &((const struct sockaddr_in6*) sa)->sin6_addr, sizeof(ret_addr->data.ipv6));
51 uint16_t avahi_port_from_sockaddr(const struct sockaddr* sa) {
54 assert(sa->sa_family == AF_INET || sa->sa_family == AF_INET6);
56 if (sa->sa_family == AF_INET)
57 return ntohs(((const struct sockaddr_in*) sa)->sin_port);
59 return ntohs(((const struct sockaddr_in6*) sa)->sin6_port);
62 int avahi_address_is_ipv4_in_ipv6(const AvahiAddress *a) {
64 static const uint8_t ipv4_in_ipv6[] = {
65 0x00, 0x00, 0x00, 0x00,
66 0x00, 0x00, 0x00, 0x00,
67 0xFF, 0xFF, 0xFF, 0xFF
72 if (a->proto != AVAHI_PROTO_INET6)
75 return memcmp(a->data.ipv6.address, ipv4_in_ipv6, sizeof(ipv4_in_ipv6)) == 0;
78 #define IPV4LL_NETWORK 0xA9FE0000L
79 #define IPV4LL_NETMASK 0xFFFF0000L
80 #define IPV6LL_NETWORK 0xFE80
81 #define IPV6LL_NETMASK 0xFFC0
83 int avahi_address_is_link_local(const AvahiAddress *a) {
86 if (a->proto == AVAHI_PROTO_INET) {
87 uint32_t n = ntohl(a->data.ipv4.address);
88 return (n & IPV4LL_NETMASK) == IPV4LL_NETWORK;
90 else if (a->proto == AVAHI_PROTO_INET6) {
91 unsigned n = (a->data.ipv6.address[0] << 8) | (a->data.ipv6.address[1] << 0);
92 return (n & IPV6LL_NETMASK) == IPV6LL_NETWORK;