comparison dcclib/su2str.c @ 0:c7f6b056b673

First import of vendor version
author Peter Gervai <grin@grin.hu>
date Tue, 10 Mar 2009 13:49:58 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:c7f6b056b673
1 /* Distributed Checksum Clearinghouse
2 *
3 * Copyright (c) 2008 by Rhyolite Software, LLC
4 *
5 * This agreement is not applicable to any entity which sells anti-spam
6 * solutions to others or provides an anti-spam solution as part of a
7 * security solution sold to other entities, or to a private network
8 * which employs the DCC or uses data provided by operation of the DCC
9 * but does not provide corresponding data to other users.
10 *
11 * Permission to use, copy, modify, and distribute this software without
12 * changes for any purpose with or without fee is hereby granted, provided
13 * that the above copyright notice and this permission notice appear in all
14 * copies and any distributed versions or copies are either unchanged
15 * or not called anything similar to "DCC" or "Distributed Checksum
16 * Clearinghouse".
17 *
18 * Parties not eligible to receive a license under this agreement can
19 * obtain a commercial license to use DCC by contacting Rhyolite Software
20 * at sales@rhyolite.com.
21 *
22 * A commercial license would be for Distributed Checksum and Reputation
23 * Clearinghouse software. That software includes additional features. This
24 * free license for Distributed ChecksumClearinghouse Software does not in any
25 * way grant permision to use Distributed Checksum and Reputation Clearinghouse
26 * software
27 *
28 * THE SOFTWARE IS PROVIDED "AS IS" AND RHYOLITE SOFTWARE, LLC DISCLAIMS ALL
29 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
30 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL RHYOLITE SOFTWARE, LLC
31 * BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
32 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
33 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
34 * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
35 * SOFTWARE.
36 *
37 * Rhyolite Software DCC 1.3.103-1.42 $Revision$
38 */
39
40 #include "dcc_defs.h"
41 #ifndef DCC_WIN32
42 #include <arpa/inet.h>
43 #endif
44
45 #if !defined(HAVE_INET_NTOP) || defined(NO_IPV6)
46 #define DCC_INET_NTOP dcc_inet_ntop
47 extern const char *DCC_INET_NTOP(int, const void *, char *, size_t);
48 #else
49 #define DCC_INET_NTOP inet_ntop
50 #endif
51
52
53
54 /* strip IPv6 prefix from ::ffff:10.2.3.4 */
55 const char *
56 dcc_trim_ffff(const char *str)
57 {
58 return strncmp("::ffff:", str, 7) ? str : (str+7);
59 }
60
61
62
63 const char *
64 dcc_ipv4tostr(char *buf, int buf_len, const struct in_addr *addr4)
65 {
66 if (!DCC_INET_NTOP(AF_INET, addr4, buf, buf_len))
67 STRLCPY(buf, "???", buf_len);
68 return buf;
69 }
70
71
72
73 const char *
74 dcc_ipv6tostr(char *buf, int buf_len, const struct in6_addr *addr6)
75 {
76 if (!DCC_INET_NTOP(AF_INET6, addr6, buf, buf_len))
77 STRLCPY(buf, "???", buf_len);
78 return buf;
79 }
80
81
82
83 const char *
84 dcc_ipv6tostr2(char *buf, int buf_len, const struct in6_addr *addr6)
85 {
86 struct in_addr addr4;
87
88 if (dcc_ipv6toipv4(&addr4, addr6))
89 return dcc_ipv4tostr(buf, buf_len, &addr4);
90
91 if (!DCC_INET_NTOP(AF_INET6, addr6, buf, buf_len))
92 STRLCPY(buf, "???", buf_len);
93 return buf;
94 }
95
96
97
98 const char *
99 dcc_ip2str(char *buf, int buf_len, const DCC_IP *ip)
100 {
101 if (ip->family == AF_INET)
102 return dcc_ipv4tostr(buf, buf_len, &ip->u.v4);
103 else
104 return dcc_ipv6tostr(buf, buf_len, &ip->u.v6);
105 }
106
107
108
109 /* convert to a string including the port number.
110 * try to make a short IPv4 string */
111 const char *
112 dcc_su2str(char *buf, int buf_len, const DCC_SOCKU *su)
113 {
114 int i;
115
116 if (su->sa.sa_family == AF_INET) {
117 dcc_ipv4tostr(buf, buf_len, &su->ipv4.sin_addr);
118 } else {
119 dcc_ipv6tostr2(buf, buf_len, &su->ipv6.sin6_addr);
120 }
121 i = strlen(buf);
122 snprintf(buf+i, buf_len-i, ",%d", ntohs(*DCC_SU_PORTP(su)));
123 return buf;
124 }
125
126
127
128 /* convert to a string without the port number.
129 * try to make a short IPv4 string */
130 const char *
131 dcc_su2str2(char *buf, int buf_len, const DCC_SOCKU *su)
132 {
133 if (su->sa.sa_family == AF_INET)
134 return dcc_ipv4tostr(buf, buf_len, &su->ipv4.sin_addr);
135
136 return dcc_ipv6tostr2(buf, buf_len, &su->ipv6.sin6_addr);
137 }
138
139
140
141 /* convert to a string without a boring port number
142 * try to make a short IPv4 string */
143 const char *
144 dcc_su2str3(char *buf, int buf_len, const DCC_SOCKU *su, u_short port)
145 {
146 if (*DCC_SU_PORTP(su) != port) {
147 return dcc_su2str(buf, buf_len, su);
148 } else {
149 return dcc_su2str2(buf, buf_len, su);
150 }
151 }
152
153
154
155 /* Convert IP address to a string, but not into a single buffer
156 * This is not thread safe but good enough for error messages */
157 const char *
158 dcc_su2str_err(const DCC_SOCKU *su)
159 {
160 static int bufno;
161 static struct {
162 char str[DCC_SU2STR_SIZE];
163 } bufs[4];
164 char *s;
165
166 s = bufs[bufno].str;
167 bufno = (bufno+1) % DIM(bufs);
168 return dcc_su2str(s, sizeof(bufs[0].str), su);
169 }
170
171
172
173 const char *
174 dcc_host_portname(char *buf, int buf_len,
175 const char *hostname, const char *portname)
176 {
177 if (!portname || *portname == '\0' || !strcmp(portname, "-")) {
178 STRLCPY(buf, hostname, buf_len);
179 } else {
180 snprintf(buf, buf_len, "%s,%s", hostname, portname);
181 }
182 return buf;
183 }
184
185
186
187 /* Convert IP address to a name */
188 const char *
189 dcc_su2name(char *name, int name_len, const DCC_SOCKU *su)
190 {
191 #undef EXPANDED
192 #if defined(USE_GETIPNODEBYNAME) && !defined(EXPANDED) && !defined(NO_IPV6)
193 #define EXPANDED
194 struct hostent *hp;
195 int error;
196
197 dcc_host_lock();
198 if (su->sa.sa_family == AF_INET)
199 hp = getipnodebyaddr(&su->ipv4.sin_addr,
200 sizeof(su->ipv4.sin_addr),
201 su->sa.sa_family, &error);
202 else
203 hp = getipnodebyaddr(&su->ipv6.sin6_addr,
204 sizeof(su->ipv6.sin6_addr),
205 su->sa.sa_family, &error);
206 if (!hp) {
207 if (name_len > 0)
208 *name = '\0';
209 } else {
210 if (name_len > 0)
211 STRLCPY(name, hp->h_name, name_len);
212 freehostent(hp);
213 }
214 dcc_host_unlock();
215 #endif
216 #if defined(USE_GETADDRINFO) && !defined(EXPANDED) && !defined(NO_IPV6)
217 #define EXPANDED
218 int error;
219
220 dcc_host_lock();
221 error = getnameinfo(&su->sa, DCC_SU_LEN(su),
222 name, name_len, 0, 0, NI_NAMEREQD);
223 dcc_host_unlock();
224 if (error && name_len > 0)
225 *name = '\0';
226 #endif
227 #ifndef EXPANDED
228 struct hostent *hp;
229 DCC_SOCKU su4;
230
231 dcc_host_lock();
232 if (dcc_ipv6sutoipv4(&su4, su)) {
233 hp = gethostbyaddr((char *)&su4.ipv4.sin_addr,
234 sizeof(su4.ipv4.sin_addr),
235 AF_INET);
236 } else {
237 hp = 0;
238 }
239 if (name_len > 0)
240 STRLCPY(name, hp ? hp->h_name : "", name_len);
241 dcc_host_unlock();
242 #endif
243 return name;
244 #undef EXPANDED
245 }