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