blob: c38a05cb235bd9b2e500217a719278ffff68591c [file] [log] [blame]
Neels Hofmeyr0c7826e2019-02-25 02:45:06 +01001/*! \file sockaddr_str.c
2 * Common implementation to store an IP address and port.
3 */
4/*
5 * (C) 2019 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
6 *
7 * Author: neels@hofmeyr.de
8 *
9 * All Rights Reserved
10 *
11 * SPDX-License-Identifier: GPL-2.0+
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 *
27 */
28
29#include "config.h"
30
31#ifdef HAVE_NETINET_IN_H
32#include <string.h>
33#include <errno.h>
34#include <netinet/in.h>
35#include <arpa/inet.h>
36
37#include <osmocom/core/sockaddr_str.h>
38#include <osmocom/core/utils.h>
39#include <osmocom/core/byteswap.h>
40
41/*! \addtogroup sockaddr_str
42 *
43 * Common operations to store IP address as a char string along with a uint16_t port number.
44 *
45 * Convert IP address string to/from in_addr and in6_addr, with bounds checking and basic housekeeping.
46 *
47 * The initial purpose is to store and translate IP address info between GSM CC and MGCP protocols -- GSM mostly using
48 * 32-bit IPv4 addresses, and MGCP forwarding addresses as ASCII character strings.
49 *
50 * (At the time of writing, there are no immediate IPv6 users that come to mind, but it seemed appropriate to
51 * accommodate both address families from the start.)
52 *
53 * @{
54 * \file sockaddr_str.c
55 */
56
57/*! Return true if all elements of the osmo_sockaddr_str instance are set.
58 * \param[in] sockaddr_str The instance to examine.
59 * \return True iff ip is nonempty, port is not 0 and af is set to either AF_INET or AF_INET6.
60 */
61bool osmo_sockaddr_str_is_set(const struct osmo_sockaddr_str *sockaddr_str)
62{
Neels Hofmeyr04cb09c2019-04-03 18:15:36 +020063 return sockaddr_str
64 && *sockaddr_str->ip
Neels Hofmeyr0c7826e2019-02-25 02:45:06 +010065 && sockaddr_str->port
66 && (sockaddr_str->af == AF_INET || sockaddr_str->af == AF_INET6);
67}
68
Neels Hofmeyr2ceb7582019-10-05 05:12:33 +020069/*! Return true if IP and port are valid and nonzero.
70 * \param[in] sockaddr_str The instance to examine.
71 * \return True iff ip can be converted to a nonzero IP address, and port is not 0.
72 */
73bool osmo_sockaddr_str_is_nonzero(const struct osmo_sockaddr_str *sockaddr_str)
74{
75 uint32_t ipv4;
76 struct in6_addr ipv6_zero = {};
77 struct in6_addr ipv6;
78
79 if (!osmo_sockaddr_str_is_set(sockaddr_str))
80 return false;
81
82 switch (sockaddr_str->af) {
83 case AF_INET:
84 if (osmo_sockaddr_str_to_32(sockaddr_str, &ipv4))
85 return false;
86 return ipv4 != 0;
87
88 case AF_INET6:
89 if (osmo_sockaddr_str_to_in6_addr(sockaddr_str, &ipv6))
90 return false;
91 return memcmp(&ipv6, &ipv6_zero, sizeof(ipv6)) != 0;
92
93 default:
94 return false;
95 }
96}
97
Neels Hofmeyr002a51d2019-10-30 04:37:47 +010098/*! Compare two osmo_sockaddr_str instances by string comparison.
99 * Compare by strcmp() for the address and compare port numbers, ignore the AF_INET/AF_INET6 value.
100 * \param[in] a left side of comparison.
101 * \param[in] b right side of comparison.
102 * \return -1 if a < b, 0 if a == b, 1 if a > b.
103 */
104static int osmo_sockaddr_str_cmp_by_string(const struct osmo_sockaddr_str *a, const struct osmo_sockaddr_str *b)
105{
106 int cmp;
107 if (a == b)
108 return 0;
109 if (!a)
110 return -1;
111 if (!b)
112 return 1;
113 cmp = strncmp(a->ip, b->ip, sizeof(a->ip));
114 if (cmp)
115 return cmp;
116 return OSMO_CMP(a->port, b->port);
117}
118
119/*! Compare two osmo_sockaddr_str instances by resulting IP address.
120 * Compare IP versions (AF_INET vs AF_INET6), compare resulting IP address bytes and compare port numbers.
121 * If the IP address strings cannot be parsed successfully / if the 'af' is neither AF_INET nor AF_INET6, fall back to
122 * pure string comparison of the ip address.
123 * \param[in] a left side of comparison.
124 * \param[in] b right side of comparison.
125 * \return -1 if a < b, 0 if a == b, 1 if a > b.
126 */
127int osmo_sockaddr_str_cmp(const struct osmo_sockaddr_str *a, const struct osmo_sockaddr_str *b)
128{
129 int cmp;
130 uint32_t ipv4_a, ipv4_b;
131 struct in6_addr ipv6_a = {}, ipv6_b = {};
132
133 if (a == b)
134 return 0;
135 if (!a)
136 return -1;
137 if (!b)
138 return 1;
139 cmp = OSMO_CMP(a->af, b->af);
140 if (cmp)
141 return cmp;
142 switch (a->af) {
143 case AF_INET:
144 if (osmo_sockaddr_str_to_32(a, &ipv4_a)
145 || osmo_sockaddr_str_to_32(b, &ipv4_b))
146 goto fallback_to_strcmp;
147 cmp = OSMO_CMP(ipv4_a, ipv4_b);
148 break;
149
150 case AF_INET6:
151 if (osmo_sockaddr_str_to_in6_addr(a, &ipv6_a)
152 || osmo_sockaddr_str_to_in6_addr(b, &ipv6_b))
153 goto fallback_to_strcmp;
154 cmp = memcmp(&ipv6_a, &ipv6_b, sizeof(ipv6_a));
155 break;
156
157 default:
158 goto fallback_to_strcmp;
159 }
160 if (cmp)
161 return cmp;
162
163 cmp = OSMO_CMP(a->port, b->port);
164 if (cmp)
165 return cmp;
166 return 0;
167
168fallback_to_strcmp:
169 return osmo_sockaddr_str_cmp_by_string(a, b);
170}
171
Neels Hofmeyr0c7826e2019-02-25 02:45:06 +0100172/*! Distinguish between valid IPv4 and IPv6 strings.
173 * This does not verify whether the string is a valid IP address; it assumes that the input is a valid IP address, and
174 * on that premise returns whether it is an IPv4 or IPv6 string, by looking for '.' and ':' characters. It is safe to
175 * feed invalid address strings, but the return value is only guaranteed to be meaningful if the input was valid.
176 * \param[in] ip Valid IP address string.
177 * \return AF_INET or AF_INET6, or AF_UNSPEC if neither '.' nor ':' are found in the string.
178 */
179int osmo_ip_str_type(const char *ip)
180{
181 if (!ip)
182 return AF_UNSPEC;
183 /* Could also be IPv4-mapped IPv6 format with both colons and dots: x:x:x:x:x:x:d.d.d.d */
184 if (strchr(ip, ':'))
185 return AF_INET6;
186 if (strchr(ip, '.'))
187 return AF_INET;
188 return AF_UNSPEC;
189}
190
191/*! Safely copy the given ip string to sockaddr_str, classify to AF_INET or AF_INET6, and set the port.
192 * Data will be written to sockaddr_str even if an error is returned.
193 * \param[out] sockaddr_str The instance to copy to.
194 * \param[in] ip Valid IP address string.
195 * \param[in] port Port number.
196 * \return 0 on success, negative if copying the address string failed (e.g. too long), if the address family could
197 * not be detected (i.e. if osmo_ip_str_type() returned AF_UNSPEC), or if sockaddr_str is NULL.
198 */
199int osmo_sockaddr_str_from_str(struct osmo_sockaddr_str *sockaddr_str, const char *ip, uint16_t port)
200{
201 int rc;
202 if (!sockaddr_str)
203 return -ENOSPC;
204 if (!ip)
205 ip = "";
206 *sockaddr_str = (struct osmo_sockaddr_str){
207 .af = osmo_ip_str_type(ip),
208 .port = port,
209 };
210 rc = osmo_strlcpy(sockaddr_str->ip, ip, sizeof(sockaddr_str->ip));
211 if (rc <= 0)
212 return -EIO;
213 if (rc >= sizeof(sockaddr_str->ip))
214 return -ENOSPC;
215 if (sockaddr_str->af == AF_UNSPEC)
216 return -EINVAL;
217 return 0;
218}
219
220/*! Convert IPv4 address to osmo_sockaddr_str, and set port.
221 * \param[out] sockaddr_str The instance to copy to.
222 * \param[in] addr IPv4 address data.
223 * \param[in] port Port number.
224 * \return 0 on success, negative on error.
225 */
226int osmo_sockaddr_str_from_in_addr(struct osmo_sockaddr_str *sockaddr_str, const struct in_addr *addr, uint16_t port)
227{
228 if (!sockaddr_str)
229 return -ENOSPC;
230 *sockaddr_str = (struct osmo_sockaddr_str){
231 .af = AF_INET,
232 .port = port,
233 };
234 if (!inet_ntop(AF_INET, addr, sockaddr_str->ip, sizeof(sockaddr_str->ip)))
235 return -ENOSPC;
236 return 0;
237}
238
239/*! Convert IPv6 address to osmo_sockaddr_str, and set port.
240 * \param[out] sockaddr_str The instance to copy to.
241 * \param[in] addr IPv6 address data.
242 * \param[in] port Port number.
243 * \return 0 on success, negative on error.
244 */
245int osmo_sockaddr_str_from_in6_addr(struct osmo_sockaddr_str *sockaddr_str, const struct in6_addr *addr, uint16_t port)
246{
247 if (!sockaddr_str)
248 return -ENOSPC;
249 *sockaddr_str = (struct osmo_sockaddr_str){
250 .af = AF_INET6,
251 .port = port,
252 };
253 if (!inet_ntop(AF_INET6, addr, sockaddr_str->ip, sizeof(sockaddr_str->ip)))
254 return -ENOSPC;
255 return 0;
256}
257
Neels Hofmeyrdf22b002019-11-11 19:20:42 +0100258/*! Convert IPv4 address from 32bit network-byte-order to osmo_sockaddr_str, and set port.
Neels Hofmeyr0c7826e2019-02-25 02:45:06 +0100259 * \param[out] sockaddr_str The instance to copy to.
260 * \param[in] addr 32bit IPv4 address data.
261 * \param[in] port Port number.
262 * \return 0 on success, negative on error.
263 */
264int osmo_sockaddr_str_from_32(struct osmo_sockaddr_str *sockaddr_str, uint32_t ip, uint16_t port)
265{
266 struct in_addr addr;
267 if (!sockaddr_str)
268 return -ENOSPC;
269 addr.s_addr = ip;
270 return osmo_sockaddr_str_from_in_addr(sockaddr_str, &addr, port);
271}
272
Neels Hofmeyrdf22b002019-11-11 19:20:42 +0100273/*! Convert IPv4 address from 32bit host-byte-order to osmo_sockaddr_str, and set port.
274 * For legacy reasons, this function has a misleading 'n' in its name.
Neels Hofmeyr0c7826e2019-02-25 02:45:06 +0100275 * \param[out] sockaddr_str The instance to copy to.
276 * \param[in] addr 32bit IPv4 address data.
277 * \param[in] port Port number.
278 * \return 0 on success, negative on error.
279 */
Neels Hofmeyr951d32b2019-11-11 19:20:42 +0100280int osmo_sockaddr_str_from_32h(struct osmo_sockaddr_str *sockaddr_str, uint32_t ip, uint16_t port)
Neels Hofmeyr0c7826e2019-02-25 02:45:06 +0100281{
282 if (!sockaddr_str)
283 return -ENOSPC;
284 return osmo_sockaddr_str_from_32(sockaddr_str, osmo_ntohl(ip), port);
285}
286
Neels Hofmeyr951d32b2019-11-11 19:20:42 +0100287/*! DEPRECATED: the name suggests a conversion from network byte order, but actually converts from host byte order. Use
288 * osmo_sockaddr_str_from_32 for network byte order and osmo_sockaddr_str_from_32h for host byte order. */
289int osmo_sockaddr_str_from_32n(struct osmo_sockaddr_str *sockaddr_str, uint32_t ip, uint16_t port)
290{
291 return osmo_sockaddr_str_from_32h(sockaddr_str, ip, port);
292}
293
Neels Hofmeyr0c7826e2019-02-25 02:45:06 +0100294/*! Convert IPv4 address and port to osmo_sockaddr_str.
295 * \param[out] sockaddr_str The instance to copy to.
296 * \param[in] src IPv4 address and port data.
297 * \return 0 on success, negative on error.
298 */
299int osmo_sockaddr_str_from_sockaddr_in(struct osmo_sockaddr_str *sockaddr_str, const struct sockaddr_in *src)
300{
301 if (!sockaddr_str)
302 return -ENOSPC;
303 if (!src)
304 return -EINVAL;
305 if (src->sin_family != AF_INET)
306 return -EINVAL;
307 return osmo_sockaddr_str_from_in_addr(sockaddr_str, &src->sin_addr, osmo_ntohs(src->sin_port));
308}
309
310/*! Convert IPv6 address and port to osmo_sockaddr_str.
311 * \param[out] sockaddr_str The instance to copy to.
312 * \param[in] src IPv6 address and port data.
313 * \return 0 on success, negative on error.
314 */
315int osmo_sockaddr_str_from_sockaddr_in6(struct osmo_sockaddr_str *sockaddr_str, const struct sockaddr_in6 *src)
316{
317 if (!sockaddr_str)
318 return -ENOSPC;
319 if (!src)
320 return -EINVAL;
321 if (src->sin6_family != AF_INET6)
322 return -EINVAL;
323 return osmo_sockaddr_str_from_in6_addr(sockaddr_str, &src->sin6_addr, osmo_ntohs(src->sin6_port));
324}
325
326/*! Convert IPv4 or IPv6 address and port to osmo_sockaddr_str.
327 * \param[out] sockaddr_str The instance to copy to.
328 * \param[in] src IPv4 or IPv6 address and port data.
329 * \return 0 on success, negative if src does not indicate AF_INET nor AF_INET6 (or if the conversion fails, which
330 * should not be possible in practice).
331 */
332int osmo_sockaddr_str_from_sockaddr(struct osmo_sockaddr_str *sockaddr_str, const struct sockaddr_storage *src)
333{
334 const struct sockaddr_in *sin = (void*)src;
335 const struct sockaddr_in6 *sin6 = (void*)src;
336 if (!sockaddr_str)
337 return -ENOSPC;
338 if (!src)
339 return -EINVAL;
340 if (sin->sin_family == AF_INET)
341 return osmo_sockaddr_str_from_sockaddr_in(sockaddr_str, sin);
342 if (sin6->sin6_family == AF_INET6)
343 return osmo_sockaddr_str_from_sockaddr_in6(sockaddr_str, sin6);
344 return -EINVAL;
345}
346
347/*! Convert osmo_sockaddr_str address string to IPv4 address data.
348 * \param[in] sockaddr_str The instance to convert the IP of.
349 * \param[out] dst IPv4 address data to write to.
350 * \return 0 on success, negative on error (e.g. invalid IPv4 address string).
351 */
352int osmo_sockaddr_str_to_in_addr(const struct osmo_sockaddr_str *sockaddr_str, struct in_addr *dst)
353{
354 int rc;
355 if (!sockaddr_str)
356 return -EINVAL;
357 if (!dst)
358 return -ENOSPC;
359 if (sockaddr_str->af != AF_INET)
360 return -EAFNOSUPPORT;
361 rc = inet_pton(AF_INET, sockaddr_str->ip, dst);
362 if (rc != 1)
363 return -EINVAL;
364 return 0;
365}
366
367/*! Convert osmo_sockaddr_str address string to IPv6 address data.
368 * \param[in] sockaddr_str The instance to convert the IP of.
369 * \param[out] dst IPv6 address data to write to.
370 * \return 0 on success, negative on error (e.g. invalid IPv6 address string).
371 */
372int osmo_sockaddr_str_to_in6_addr(const struct osmo_sockaddr_str *sockaddr_str, struct in6_addr *dst)
373{
374 int rc;
375 if (!sockaddr_str)
376 return -EINVAL;
377 if (!dst)
378 return -ENOSPC;
379 if (sockaddr_str->af != AF_INET6)
380 return -EINVAL;
381 rc = inet_pton(AF_INET6, sockaddr_str->ip, dst);
382 if (rc != 1)
383 return -EINVAL;
384 return 0;
385}
386
Neels Hofmeyrdf22b002019-11-11 19:20:42 +0100387/*! Convert osmo_sockaddr_str address string to IPv4 address data in network-byte-order.
Neels Hofmeyr0c7826e2019-02-25 02:45:06 +0100388 * \param[in] sockaddr_str The instance to convert the IP of.
Neels Hofmeyrdf22b002019-11-11 19:20:42 +0100389 * \param[out] dst IPv4 address data in 32bit network-byte-order format to write to.
Neels Hofmeyr0c7826e2019-02-25 02:45:06 +0100390 * \return 0 on success, negative on error (e.g. invalid IPv4 address string).
391 */
392int osmo_sockaddr_str_to_32(const struct osmo_sockaddr_str *sockaddr_str, uint32_t *ip)
393{
394 int rc;
395 struct in_addr addr;
396 if (!sockaddr_str)
397 return -EINVAL;
398 if (!ip)
399 return -ENOSPC;
400 rc = osmo_sockaddr_str_to_in_addr(sockaddr_str, &addr);
401 if (rc)
402 return rc;
403 *ip = addr.s_addr;
404 return 0;
405}
406
Neels Hofmeyrdf22b002019-11-11 19:20:42 +0100407/*! Convert osmo_sockaddr_str address string to IPv4 address data in host-byte-order.
408 * For legacy reasons, this function has a misleading 'n' in its name.
Neels Hofmeyr0c7826e2019-02-25 02:45:06 +0100409 * \param[in] sockaddr_str The instance to convert the IP of.
Neels Hofmeyrdf22b002019-11-11 19:20:42 +0100410 * \param[out] dst IPv4 address data in 32bit host-byte-order format to write to.
Neels Hofmeyr0c7826e2019-02-25 02:45:06 +0100411 * \return 0 on success, negative on error (e.g. invalid IPv4 address string).
412 */
Neels Hofmeyr951d32b2019-11-11 19:20:42 +0100413int osmo_sockaddr_str_to_32h(const struct osmo_sockaddr_str *sockaddr_str, uint32_t *ip)
Neels Hofmeyr0c7826e2019-02-25 02:45:06 +0100414{
415 int rc;
416 uint32_t ip_h;
417 if (!sockaddr_str)
418 return -EINVAL;
419 if (!ip)
420 return -ENOSPC;
421 rc = osmo_sockaddr_str_to_32(sockaddr_str, &ip_h);
422 if (rc)
423 return rc;
424 *ip = osmo_htonl(ip_h);
425 return 0;
426}
427
Neels Hofmeyr951d32b2019-11-11 19:20:42 +0100428/*! DEPRECATED: the name suggests a conversion to network byte order, but actually converts to host byte order. Use
429 * osmo_sockaddr_str_to_32() for network byte order and osmo_sockaddr_str_to_32h() for host byte order. */
430int osmo_sockaddr_str_to_32n(const struct osmo_sockaddr_str *sockaddr_str, uint32_t *ip)
431{
432 return osmo_sockaddr_str_to_32h(sockaddr_str, ip);
433}
434
Neels Hofmeyr0c7826e2019-02-25 02:45:06 +0100435/*! Convert osmo_sockaddr_str address string and port to IPv4 address and port data.
436 * \param[in] sockaddr_str The instance to convert the IP and port of.
437 * \param[out] dst IPv4 address and port data to write to.
438 * \return 0 on success, negative on error (e.g. invalid IPv4 address string).
439 */
440int osmo_sockaddr_str_to_sockaddr_in(const struct osmo_sockaddr_str *sockaddr_str, struct sockaddr_in *dst)
441{
442 if (!sockaddr_str)
443 return -EINVAL;
444 if (!dst)
445 return -ENOSPC;
446 if (sockaddr_str->af != AF_INET)
447 return -EINVAL;
448 *dst = (struct sockaddr_in){
449 .sin_family = sockaddr_str->af,
450 .sin_port = osmo_htons(sockaddr_str->port),
451 };
452 return osmo_sockaddr_str_to_in_addr(sockaddr_str, &dst->sin_addr);
453}
454
455/*! Convert osmo_sockaddr_str address string and port to IPv6 address and port data.
456 * \param[in] sockaddr_str The instance to convert the IP and port of.
457 * \param[out] dst IPv6 address and port data to write to.
458 * \return 0 on success, negative on error (e.g. invalid IPv6 address string).
459 */
460int osmo_sockaddr_str_to_sockaddr_in6(const struct osmo_sockaddr_str *sockaddr_str, struct sockaddr_in6 *dst)
461{
462 if (!sockaddr_str)
463 return -EINVAL;
464 if (!dst)
465 return -ENOSPC;
466 if (sockaddr_str->af != AF_INET6)
467 return -EINVAL;
468 *dst = (struct sockaddr_in6){
469 .sin6_family = sockaddr_str->af,
470 .sin6_port = osmo_htons(sockaddr_str->port),
471 };
472 return osmo_sockaddr_str_to_in6_addr(sockaddr_str, &dst->sin6_addr);
473}
474
475/*! Convert osmo_sockaddr_str address string and port to IPv4 or IPv6 address and port data.
476 * Depending on sockaddr_str->af, dst will be handled as struct sockaddr_in or struct sockaddr_in6.
477 * \param[in] sockaddr_str The instance to convert the IP and port of.
478 * \param[out] dst IPv4/IPv6 address and port data to write to.
479 * \return 0 on success, negative on error (e.g. invalid IP address string for the family indicated by sockaddr_str->af).
480 */
481int osmo_sockaddr_str_to_sockaddr(const struct osmo_sockaddr_str *sockaddr_str, struct sockaddr_storage *dst)
482{
483 if (!sockaddr_str)
484 return -EINVAL;
485 if (!dst)
486 return -ENOSPC;
487 switch (sockaddr_str->af) {
488 case AF_INET:
489 return osmo_sockaddr_str_to_sockaddr_in(sockaddr_str, (void*)dst);
490 case AF_INET6:
491 return osmo_sockaddr_str_to_sockaddr_in6(sockaddr_str, (void*)dst);
492 default:
493 return -EINVAL;
494 }
495}
496
497/*! @} */
498#endif // HAVE_NETINET_IN_H