/src/avahi/avahi-core/addr-util.c
Line | Count | Source (jump to first uncovered line) |
1 | | /*** |
2 | | This file is part of avahi. |
3 | | |
4 | | avahi is free software; you can redistribute it and/or modify it |
5 | | under the terms of the GNU Lesser General Public License as |
6 | | published by the Free Software Foundation; either version 2.1 of the |
7 | | License, or (at your option) any later version. |
8 | | |
9 | | avahi is distributed in the hope that it will be useful, but WITHOUT |
10 | | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
11 | | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General |
12 | | Public License for more details. |
13 | | |
14 | | You should have received a copy of the GNU Lesser General Public |
15 | | License along with avahi; if not, write to the Free Software |
16 | | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
17 | | USA. |
18 | | ***/ |
19 | | |
20 | | #ifdef HAVE_CONFIG_H |
21 | | #include <config.h> |
22 | | #endif |
23 | | |
24 | | #include <sys/types.h> |
25 | | #include <netinet/in.h> |
26 | | #include <sys/socket.h> |
27 | | #include <arpa/inet.h> |
28 | | #include <string.h> |
29 | | #include <assert.h> |
30 | | |
31 | | #include "addr-util.h" |
32 | | |
33 | 0 | AvahiAddress *avahi_address_from_sockaddr(const struct sockaddr* sa, AvahiAddress *ret_addr) { |
34 | 0 | assert(sa); |
35 | 0 | assert(ret_addr); |
36 | | |
37 | 0 | assert(sa->sa_family == AF_INET || sa->sa_family == AF_INET6); |
38 | | |
39 | 0 | ret_addr->proto = avahi_af_to_proto(sa->sa_family); |
40 | |
|
41 | 0 | if (sa->sa_family == AF_INET) |
42 | 0 | memcpy(&ret_addr->data.ipv4, &((const struct sockaddr_in*) sa)->sin_addr, sizeof(ret_addr->data.ipv4)); |
43 | 0 | else |
44 | 0 | memcpy(&ret_addr->data.ipv6, &((const struct sockaddr_in6*) sa)->sin6_addr, sizeof(ret_addr->data.ipv6)); |
45 | |
|
46 | 0 | return ret_addr; |
47 | 0 | } |
48 | | |
49 | 0 | uint16_t avahi_port_from_sockaddr(const struct sockaddr* sa) { |
50 | 0 | assert(sa); |
51 | | |
52 | 0 | assert(sa->sa_family == AF_INET || sa->sa_family == AF_INET6); |
53 | | |
54 | 0 | if (sa->sa_family == AF_INET) |
55 | 0 | return ntohs(((const struct sockaddr_in*) sa)->sin_port); |
56 | 0 | else |
57 | 0 | return ntohs(((const struct sockaddr_in6*) sa)->sin6_port); |
58 | 0 | } |
59 | | |
60 | 0 | int avahi_address_is_ipv4_in_ipv6(const AvahiAddress *a) { |
61 | |
|
62 | 0 | static const uint8_t ipv4_in_ipv6[] = { |
63 | 0 | 0x00, 0x00, 0x00, 0x00, |
64 | 0 | 0x00, 0x00, 0x00, 0x00, |
65 | 0 | 0xFF, 0xFF, 0xFF, 0xFF |
66 | 0 | }; |
67 | |
|
68 | 0 | assert(a); |
69 | | |
70 | 0 | if (a->proto != AVAHI_PROTO_INET6) |
71 | 0 | return 0; |
72 | | |
73 | 0 | return memcmp(a->data.ipv6.address, ipv4_in_ipv6, sizeof(ipv4_in_ipv6)) == 0; |
74 | 0 | } |
75 | | |
76 | 0 | #define IPV4LL_NETWORK 0xA9FE0000L |
77 | 0 | #define IPV4LL_NETMASK 0xFFFF0000L |
78 | 0 | #define IPV6LL_NETWORK 0xFE80 |
79 | 0 | #define IPV6LL_NETMASK 0xFFC0 |
80 | | |
81 | 0 | int avahi_address_is_link_local(const AvahiAddress *a) { |
82 | 0 | assert(a); |
83 | | |
84 | 0 | if (a->proto == AVAHI_PROTO_INET) { |
85 | 0 | uint32_t n = ntohl(a->data.ipv4.address); |
86 | 0 | return (n & IPV4LL_NETMASK) == IPV4LL_NETWORK; |
87 | 0 | } |
88 | 0 | else if (a->proto == AVAHI_PROTO_INET6) { |
89 | 0 | unsigned n = (a->data.ipv6.address[0] << 8) | (a->data.ipv6.address[1] << 0); |
90 | 0 | return (n & IPV6LL_NETMASK) == IPV6LL_NETWORK; |
91 | 0 | } |
92 | | |
93 | 0 | return 0; |
94 | 0 | } |