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