blob: f523050cff71c38ba7ed51abf642fd7293609cab [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 Hofmeyr0c7826e2019-02-25 02:45:06 +010098/*! Distinguish between valid IPv4 and IPv6 strings.
99 * This does not verify whether the string is a valid IP address; it assumes that the input is a valid IP address, and
100 * on that premise returns whether it is an IPv4 or IPv6 string, by looking for '.' and ':' characters. It is safe to
101 * feed invalid address strings, but the return value is only guaranteed to be meaningful if the input was valid.
102 * \param[in] ip Valid IP address string.
103 * \return AF_INET or AF_INET6, or AF_UNSPEC if neither '.' nor ':' are found in the string.
104 */
105int osmo_ip_str_type(const char *ip)
106{
107 if (!ip)
108 return AF_UNSPEC;
109 /* Could also be IPv4-mapped IPv6 format with both colons and dots: x:x:x:x:x:x:d.d.d.d */
110 if (strchr(ip, ':'))
111 return AF_INET6;
112 if (strchr(ip, '.'))
113 return AF_INET;
114 return AF_UNSPEC;
115}
116
117/*! Safely copy the given ip string to sockaddr_str, classify to AF_INET or AF_INET6, and set the port.
118 * Data will be written to sockaddr_str even if an error is returned.
119 * \param[out] sockaddr_str The instance to copy to.
120 * \param[in] ip Valid IP address string.
121 * \param[in] port Port number.
122 * \return 0 on success, negative if copying the address string failed (e.g. too long), if the address family could
123 * not be detected (i.e. if osmo_ip_str_type() returned AF_UNSPEC), or if sockaddr_str is NULL.
124 */
125int osmo_sockaddr_str_from_str(struct osmo_sockaddr_str *sockaddr_str, const char *ip, uint16_t port)
126{
127 int rc;
128 if (!sockaddr_str)
129 return -ENOSPC;
130 if (!ip)
131 ip = "";
132 *sockaddr_str = (struct osmo_sockaddr_str){
133 .af = osmo_ip_str_type(ip),
134 .port = port,
135 };
136 rc = osmo_strlcpy(sockaddr_str->ip, ip, sizeof(sockaddr_str->ip));
137 if (rc <= 0)
138 return -EIO;
139 if (rc >= sizeof(sockaddr_str->ip))
140 return -ENOSPC;
141 if (sockaddr_str->af == AF_UNSPEC)
142 return -EINVAL;
143 return 0;
144}
145
146/*! Convert IPv4 address to osmo_sockaddr_str, and set port.
147 * \param[out] sockaddr_str The instance to copy to.
148 * \param[in] addr IPv4 address data.
149 * \param[in] port Port number.
150 * \return 0 on success, negative on error.
151 */
152int osmo_sockaddr_str_from_in_addr(struct osmo_sockaddr_str *sockaddr_str, const struct in_addr *addr, uint16_t port)
153{
154 if (!sockaddr_str)
155 return -ENOSPC;
156 *sockaddr_str = (struct osmo_sockaddr_str){
157 .af = AF_INET,
158 .port = port,
159 };
160 if (!inet_ntop(AF_INET, addr, sockaddr_str->ip, sizeof(sockaddr_str->ip)))
161 return -ENOSPC;
162 return 0;
163}
164
165/*! Convert IPv6 address to osmo_sockaddr_str, and set port.
166 * \param[out] sockaddr_str The instance to copy to.
167 * \param[in] addr IPv6 address data.
168 * \param[in] port Port number.
169 * \return 0 on success, negative on error.
170 */
171int osmo_sockaddr_str_from_in6_addr(struct osmo_sockaddr_str *sockaddr_str, const struct in6_addr *addr, uint16_t port)
172{
173 if (!sockaddr_str)
174 return -ENOSPC;
175 *sockaddr_str = (struct osmo_sockaddr_str){
176 .af = AF_INET6,
177 .port = port,
178 };
179 if (!inet_ntop(AF_INET6, addr, sockaddr_str->ip, sizeof(sockaddr_str->ip)))
180 return -ENOSPC;
181 return 0;
182}
183
184/*! Convert IPv4 address from 32bit host-byte-order to osmo_sockaddr_str, and set port.
185 * \param[out] sockaddr_str The instance to copy to.
186 * \param[in] addr 32bit IPv4 address data.
187 * \param[in] port Port number.
188 * \return 0 on success, negative on error.
189 */
190int osmo_sockaddr_str_from_32(struct osmo_sockaddr_str *sockaddr_str, uint32_t ip, uint16_t port)
191{
192 struct in_addr addr;
193 if (!sockaddr_str)
194 return -ENOSPC;
195 addr.s_addr = ip;
196 return osmo_sockaddr_str_from_in_addr(sockaddr_str, &addr, port);
197}
198
199/*! Convert IPv4 address from 32bit network-byte-order to osmo_sockaddr_str, and set port.
200 * \param[out] sockaddr_str The instance to copy to.
201 * \param[in] addr 32bit IPv4 address data.
202 * \param[in] port Port number.
203 * \return 0 on success, negative on error.
204 */
205int osmo_sockaddr_str_from_32n(struct osmo_sockaddr_str *sockaddr_str, uint32_t ip, uint16_t port)
206{
207 if (!sockaddr_str)
208 return -ENOSPC;
209 return osmo_sockaddr_str_from_32(sockaddr_str, osmo_ntohl(ip), port);
210}
211
212/*! Convert IPv4 address and port to osmo_sockaddr_str.
213 * \param[out] sockaddr_str The instance to copy to.
214 * \param[in] src IPv4 address and port data.
215 * \return 0 on success, negative on error.
216 */
217int osmo_sockaddr_str_from_sockaddr_in(struct osmo_sockaddr_str *sockaddr_str, const struct sockaddr_in *src)
218{
219 if (!sockaddr_str)
220 return -ENOSPC;
221 if (!src)
222 return -EINVAL;
223 if (src->sin_family != AF_INET)
224 return -EINVAL;
225 return osmo_sockaddr_str_from_in_addr(sockaddr_str, &src->sin_addr, osmo_ntohs(src->sin_port));
226}
227
228/*! Convert IPv6 address and port to osmo_sockaddr_str.
229 * \param[out] sockaddr_str The instance to copy to.
230 * \param[in] src IPv6 address and port data.
231 * \return 0 on success, negative on error.
232 */
233int osmo_sockaddr_str_from_sockaddr_in6(struct osmo_sockaddr_str *sockaddr_str, const struct sockaddr_in6 *src)
234{
235 if (!sockaddr_str)
236 return -ENOSPC;
237 if (!src)
238 return -EINVAL;
239 if (src->sin6_family != AF_INET6)
240 return -EINVAL;
241 return osmo_sockaddr_str_from_in6_addr(sockaddr_str, &src->sin6_addr, osmo_ntohs(src->sin6_port));
242}
243
244/*! Convert IPv4 or IPv6 address and port to osmo_sockaddr_str.
245 * \param[out] sockaddr_str The instance to copy to.
246 * \param[in] src IPv4 or IPv6 address and port data.
247 * \return 0 on success, negative if src does not indicate AF_INET nor AF_INET6 (or if the conversion fails, which
248 * should not be possible in practice).
249 */
250int osmo_sockaddr_str_from_sockaddr(struct osmo_sockaddr_str *sockaddr_str, const struct sockaddr_storage *src)
251{
252 const struct sockaddr_in *sin = (void*)src;
253 const struct sockaddr_in6 *sin6 = (void*)src;
254 if (!sockaddr_str)
255 return -ENOSPC;
256 if (!src)
257 return -EINVAL;
258 if (sin->sin_family == AF_INET)
259 return osmo_sockaddr_str_from_sockaddr_in(sockaddr_str, sin);
260 if (sin6->sin6_family == AF_INET6)
261 return osmo_sockaddr_str_from_sockaddr_in6(sockaddr_str, sin6);
262 return -EINVAL;
263}
264
265/*! Convert osmo_sockaddr_str address string to IPv4 address data.
266 * \param[in] sockaddr_str The instance to convert the IP of.
267 * \param[out] dst IPv4 address data to write to.
268 * \return 0 on success, negative on error (e.g. invalid IPv4 address string).
269 */
270int osmo_sockaddr_str_to_in_addr(const struct osmo_sockaddr_str *sockaddr_str, struct in_addr *dst)
271{
272 int rc;
273 if (!sockaddr_str)
274 return -EINVAL;
275 if (!dst)
276 return -ENOSPC;
277 if (sockaddr_str->af != AF_INET)
278 return -EAFNOSUPPORT;
279 rc = inet_pton(AF_INET, sockaddr_str->ip, dst);
280 if (rc != 1)
281 return -EINVAL;
282 return 0;
283}
284
285/*! Convert osmo_sockaddr_str address string to IPv6 address data.
286 * \param[in] sockaddr_str The instance to convert the IP of.
287 * \param[out] dst IPv6 address data to write to.
288 * \return 0 on success, negative on error (e.g. invalid IPv6 address string).
289 */
290int osmo_sockaddr_str_to_in6_addr(const struct osmo_sockaddr_str *sockaddr_str, struct in6_addr *dst)
291{
292 int rc;
293 if (!sockaddr_str)
294 return -EINVAL;
295 if (!dst)
296 return -ENOSPC;
297 if (sockaddr_str->af != AF_INET6)
298 return -EINVAL;
299 rc = inet_pton(AF_INET6, sockaddr_str->ip, dst);
300 if (rc != 1)
301 return -EINVAL;
302 return 0;
303}
304
305/*! Convert osmo_sockaddr_str address string to IPv4 address data in host-byte-order.
306 * \param[in] sockaddr_str The instance to convert the IP of.
307 * \param[out] dst IPv4 address data in 32bit host-byte-order format to write to.
308 * \return 0 on success, negative on error (e.g. invalid IPv4 address string).
309 */
310int osmo_sockaddr_str_to_32(const struct osmo_sockaddr_str *sockaddr_str, uint32_t *ip)
311{
312 int rc;
313 struct in_addr addr;
314 if (!sockaddr_str)
315 return -EINVAL;
316 if (!ip)
317 return -ENOSPC;
318 rc = osmo_sockaddr_str_to_in_addr(sockaddr_str, &addr);
319 if (rc)
320 return rc;
321 *ip = addr.s_addr;
322 return 0;
323}
324
325/*! Convert osmo_sockaddr_str address string to IPv4 address data in network-byte-order.
326 * \param[in] sockaddr_str The instance to convert the IP of.
327 * \param[out] dst IPv4 address data in 32bit network-byte-order format to write to.
328 * \return 0 on success, negative on error (e.g. invalid IPv4 address string).
329 */
330int osmo_sockaddr_str_to_32n(const struct osmo_sockaddr_str *sockaddr_str, uint32_t *ip)
331{
332 int rc;
333 uint32_t ip_h;
334 if (!sockaddr_str)
335 return -EINVAL;
336 if (!ip)
337 return -ENOSPC;
338 rc = osmo_sockaddr_str_to_32(sockaddr_str, &ip_h);
339 if (rc)
340 return rc;
341 *ip = osmo_htonl(ip_h);
342 return 0;
343}
344
345/*! Convert osmo_sockaddr_str address string and port to IPv4 address and port data.
346 * \param[in] sockaddr_str The instance to convert the IP and port of.
347 * \param[out] dst IPv4 address and port data to write to.
348 * \return 0 on success, negative on error (e.g. invalid IPv4 address string).
349 */
350int osmo_sockaddr_str_to_sockaddr_in(const struct osmo_sockaddr_str *sockaddr_str, struct sockaddr_in *dst)
351{
352 if (!sockaddr_str)
353 return -EINVAL;
354 if (!dst)
355 return -ENOSPC;
356 if (sockaddr_str->af != AF_INET)
357 return -EINVAL;
358 *dst = (struct sockaddr_in){
359 .sin_family = sockaddr_str->af,
360 .sin_port = osmo_htons(sockaddr_str->port),
361 };
362 return osmo_sockaddr_str_to_in_addr(sockaddr_str, &dst->sin_addr);
363}
364
365/*! Convert osmo_sockaddr_str address string and port to IPv6 address and port data.
366 * \param[in] sockaddr_str The instance to convert the IP and port of.
367 * \param[out] dst IPv6 address and port data to write to.
368 * \return 0 on success, negative on error (e.g. invalid IPv6 address string).
369 */
370int osmo_sockaddr_str_to_sockaddr_in6(const struct osmo_sockaddr_str *sockaddr_str, struct sockaddr_in6 *dst)
371{
372 if (!sockaddr_str)
373 return -EINVAL;
374 if (!dst)
375 return -ENOSPC;
376 if (sockaddr_str->af != AF_INET6)
377 return -EINVAL;
378 *dst = (struct sockaddr_in6){
379 .sin6_family = sockaddr_str->af,
380 .sin6_port = osmo_htons(sockaddr_str->port),
381 };
382 return osmo_sockaddr_str_to_in6_addr(sockaddr_str, &dst->sin6_addr);
383}
384
385/*! Convert osmo_sockaddr_str address string and port to IPv4 or IPv6 address and port data.
386 * Depending on sockaddr_str->af, dst will be handled as struct sockaddr_in or struct sockaddr_in6.
387 * \param[in] sockaddr_str The instance to convert the IP and port of.
388 * \param[out] dst IPv4/IPv6 address and port data to write to.
389 * \return 0 on success, negative on error (e.g. invalid IP address string for the family indicated by sockaddr_str->af).
390 */
391int osmo_sockaddr_str_to_sockaddr(const struct osmo_sockaddr_str *sockaddr_str, struct sockaddr_storage *dst)
392{
393 if (!sockaddr_str)
394 return -EINVAL;
395 if (!dst)
396 return -ENOSPC;
397 switch (sockaddr_str->af) {
398 case AF_INET:
399 return osmo_sockaddr_str_to_sockaddr_in(sockaddr_str, (void*)dst);
400 case AF_INET6:
401 return osmo_sockaddr_str_to_sockaddr_in6(sockaddr_str, (void*)dst);
402 default:
403 return -EINVAL;
404 }
405}
406
407/*! @} */
408#endif // HAVE_NETINET_IN_H