]> git.meshlink.io Git - catta/blob - avahi-core/addr-util.c
22a2714cf402cd53aa111a95037a0a56ce4194a2
[catta] / avahi-core / addr-util.c
1 /* $Id$ */
2
3 /***
4   This file is part of avahi.
5
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.
10
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.
15
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
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <sys/types.h>
27 #include <netinet/in.h>
28 #include <sys/socket.h>
29 #include <arpa/inet.h>
30 #include <string.h>
31 #include <assert.h>
32
33 #include "addr-util.h"
34
35 AvahiAddress *avahi_address_from_sockaddr(const struct sockaddr* sa, AvahiAddress *ret_addr) {
36     assert(sa);
37     assert(ret_addr);
38
39     assert(sa->sa_family == AF_INET || sa->sa_family == AF_INET6);
40
41     ret_addr->proto = avahi_af_to_proto(sa->sa_family);
42
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));
45     else
46         memcpy(&ret_addr->data.ipv6, &((const struct sockaddr_in6*) sa)->sin6_addr, sizeof(ret_addr->data.ipv6));
47
48     return ret_addr;
49 }
50
51 uint16_t avahi_port_from_sockaddr(const struct sockaddr* sa) {
52     assert(sa);
53
54     assert(sa->sa_family == AF_INET || sa->sa_family == AF_INET6);
55
56     if (sa->sa_family == AF_INET)
57         return ntohs(((const struct sockaddr_in*) sa)->sin_port);
58     else
59         return ntohs(((const struct sockaddr_in6*) sa)->sin6_port);
60 }
61
62 int avahi_address_is_ipv4_in_ipv6(const AvahiAddress *a) {
63
64     static const uint8_t ipv4_in_ipv6[] = {
65         0x00, 0x00, 0x00, 0x00,
66         0x00, 0x00, 0x00, 0x00,
67         0xFF, 0xFF, 0xFF, 0xFF
68     };
69
70     assert(a);
71
72     if (a->proto != AVAHI_PROTO_INET6)
73         return 0;
74
75     return memcmp(a->data.ipv6.address, ipv4_in_ipv6, sizeof(ipv4_in_ipv6)) == 0;
76 }
77
78 #define IPV4LL_NETWORK 0xA9FE0000L
79 #define IPV4LL_NETMASK 0xFFFF0000L
80 #define IPV6LL_NETWORK 0xFE80
81 #define IPV6LL_NETMASK 0xFFC0
82
83 int avahi_address_is_link_local(const AvahiAddress *a) {
84     assert(a);
85
86     if (a->proto == AVAHI_PROTO_INET) {
87         uint32_t n = ntohl(a->data.ipv4.address);
88         return (n & IPV4LL_NETMASK) == IPV4LL_NETWORK;
89     }
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;
93     }
94
95     return 0;
96 }