/src/avahi/avahi-common/address.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 | | #include <stdio.h> |
31 | | |
32 | | #include "address.h" |
33 | | #include "malloc.h" |
34 | | |
35 | 0 | static size_t address_get_size(const AvahiAddress *a) { |
36 | 0 | assert(a); |
37 | | |
38 | 0 | if (a->proto == AVAHI_PROTO_INET) |
39 | 0 | return 4; |
40 | 0 | else if (a->proto == AVAHI_PROTO_INET6) |
41 | 0 | return 16; |
42 | | |
43 | 0 | return 0; |
44 | 0 | } |
45 | | |
46 | 0 | int avahi_address_cmp(const AvahiAddress *a, const AvahiAddress *b) { |
47 | 0 | assert(a); |
48 | 0 | assert(b); |
49 | | |
50 | 0 | if (a->proto != b->proto) |
51 | 0 | return -1; |
52 | | |
53 | 0 | return memcmp(a->data.data, b->data.data, address_get_size(a)); |
54 | 0 | } |
55 | | |
56 | 0 | char *avahi_address_snprint(char *s, size_t length, const AvahiAddress *a) { |
57 | 0 | assert(s); |
58 | 0 | assert(length); |
59 | 0 | assert(a); |
60 | | |
61 | 0 | if (!(inet_ntop(avahi_proto_to_af(a->proto), a->data.data, s, length))) |
62 | 0 | return NULL; |
63 | | |
64 | 0 | return s; |
65 | 0 | } |
66 | | |
67 | 0 | char* avahi_reverse_lookup_name(const AvahiAddress *a, char *ret_s, size_t length) { |
68 | 0 | assert(ret_s); |
69 | 0 | assert(length > 0); |
70 | 0 | assert(a); |
71 | | |
72 | 0 | if (a->proto == AVAHI_PROTO_INET) { |
73 | 0 | uint32_t n = ntohl(a->data.ipv4.address); |
74 | 0 | snprintf( |
75 | 0 | ret_s, length, |
76 | 0 | "%u.%u.%u.%u.in-addr.arpa", |
77 | 0 | n & 0xFF, (n >> 8) & 0xFF, (n >> 16) & 0xFF, n >> 24); |
78 | 0 | } else { |
79 | 0 | assert(a->proto == AVAHI_PROTO_INET6); |
80 | | |
81 | 0 | snprintf( |
82 | 0 | ret_s, length, |
83 | 0 | "%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.ip6.arpa", |
84 | 0 | a->data.ipv6.address[15] & 0xF, a->data.ipv6.address[15] >> 4, |
85 | 0 | a->data.ipv6.address[14] & 0xF, a->data.ipv6.address[14] >> 4, |
86 | 0 | a->data.ipv6.address[13] & 0xF, a->data.ipv6.address[13] >> 4, |
87 | 0 | a->data.ipv6.address[12] & 0xF, a->data.ipv6.address[12] >> 4, |
88 | 0 | a->data.ipv6.address[11] & 0xF, a->data.ipv6.address[11] >> 4, |
89 | 0 | a->data.ipv6.address[10] & 0xF, a->data.ipv6.address[10] >> 4, |
90 | 0 | a->data.ipv6.address[ 9] & 0xF, a->data.ipv6.address[ 9] >> 4, |
91 | 0 | a->data.ipv6.address[ 8] & 0xF, a->data.ipv6.address[ 8] >> 4, |
92 | 0 | a->data.ipv6.address[ 7] & 0xF, a->data.ipv6.address[ 7] >> 4, |
93 | 0 | a->data.ipv6.address[ 6] & 0xF, a->data.ipv6.address[ 6] >> 4, |
94 | 0 | a->data.ipv6.address[ 5] & 0xF, a->data.ipv6.address[ 5] >> 4, |
95 | 0 | a->data.ipv6.address[ 4] & 0xF, a->data.ipv6.address[ 4] >> 4, |
96 | 0 | a->data.ipv6.address[ 3] & 0xF, a->data.ipv6.address[ 3] >> 4, |
97 | 0 | a->data.ipv6.address[ 2] & 0xF, a->data.ipv6.address[ 2] >> 4, |
98 | 0 | a->data.ipv6.address[ 1] & 0xF, a->data.ipv6.address[ 1] >> 4, |
99 | 0 | a->data.ipv6.address[ 0] & 0xF, a->data.ipv6.address[ 0] >> 4); |
100 | 0 | } |
101 | | |
102 | 0 | return ret_s; |
103 | 0 | } |
104 | | |
105 | 0 | AvahiAddress *avahi_address_parse(const char *s, AvahiProtocol proto, AvahiAddress *ret_addr) { |
106 | 0 | assert(ret_addr); |
107 | 0 | assert(s); |
108 | | |
109 | 0 | if (proto == AVAHI_PROTO_UNSPEC) { |
110 | 0 | if (inet_pton(AF_INET, s, ret_addr->data.data) <= 0) { |
111 | 0 | if (inet_pton(AF_INET6, s, ret_addr->data.data) <= 0) |
112 | 0 | return NULL; |
113 | 0 | else |
114 | 0 | ret_addr->proto = AVAHI_PROTO_INET6; |
115 | 0 | } else |
116 | 0 | ret_addr->proto = AVAHI_PROTO_INET; |
117 | 0 | } else { |
118 | 0 | if (inet_pton(avahi_proto_to_af(proto), s, ret_addr->data.data) <= 0) |
119 | 0 | return NULL; |
120 | | |
121 | 0 | ret_addr->proto = proto; |
122 | 0 | } |
123 | | |
124 | 0 | return ret_addr; |
125 | 0 | } |
126 | | |
127 | 0 | int avahi_proto_to_af(AvahiProtocol proto) { |
128 | 0 | if (proto == AVAHI_PROTO_INET) |
129 | 0 | return AF_INET; |
130 | 0 | if (proto == AVAHI_PROTO_INET6) |
131 | 0 | return AF_INET6; |
132 | | |
133 | 0 | assert(proto == AVAHI_PROTO_UNSPEC); |
134 | 0 | return AF_UNSPEC; |
135 | 0 | } |
136 | | |
137 | 0 | AvahiProtocol avahi_af_to_proto(int af) { |
138 | 0 | if (af == AF_INET) |
139 | 0 | return AVAHI_PROTO_INET; |
140 | 0 | if (af == AF_INET6) |
141 | 0 | return AVAHI_PROTO_INET6; |
142 | | |
143 | 0 | assert(af == AF_UNSPEC); |
144 | 0 | return AVAHI_PROTO_UNSPEC; |
145 | 0 | } |
146 | | |
147 | 0 | const char* avahi_proto_to_string(AvahiProtocol proto) { |
148 | 0 | if (proto == AVAHI_PROTO_INET) |
149 | 0 | return "IPv4"; |
150 | 0 | if (proto == AVAHI_PROTO_INET6) |
151 | 0 | return "IPv6"; |
152 | | |
153 | 0 | assert(proto == AVAHI_PROTO_UNSPEC); |
154 | 0 | return "UNSPEC"; |
155 | 0 | } |