aboutsummaryrefslogtreecommitdiffstats
path: root/include
diff options
context:
space:
mode:
authorNeels Hofmeyr <neels@hofmeyr.de>2019-02-25 02:45:06 +0100
committerNeels Hofmeyr <nhofmeyr@sysmocom.de>2019-04-08 13:47:17 +0000
commit0c7826e9bd8026b239d320dba49416fd603f17fd (patch)
tree2d7553c002f684e8f2b76a4682093e29378271ac /include
parente8461e486ebac6361b0869962aa86c5b8d46e59e (diff)
add osmo_sockaddr_str API
For handling RTP IP addresses and ports, osmo-mgw, osmo-bsc and osmo-msc so far have their own separate shims and code duplication around inet_ntoa(), htons(), sockaddr conversions etc. Unify and standardize with this common API. In the MGW endpoint FSM that was introduced in osmo-bsc and which I would like to re-use for osmo-msc (upcoming patch moving that to osmo-mgw), it has turned out that using char* IP address and uint16_t port number types are a convenient common denominator for logging, MGCP message composition and GSM48. Ongoing osmo-msc work also uses this for MNCC. This is of course potentially useful for any other IP+port combinations besides RTP stream handling. Needless to say that most current implementations will probably stay with their current own conversion code for a long time; for current osmo-{bsc,msc,mgw} work (MGW endpoint FSM) though, I would like to move to this API here. Change-Id: Id617265337f09dfb6ddfe111ef5e578cd3dc9f63
Diffstat (limited to 'include')
-rw-r--r--include/Makefile.am1
-rw-r--r--include/osmocom/core/sockaddr_str.h87
2 files changed, 88 insertions, 0 deletions
diff --git a/include/Makefile.am b/include/Makefile.am
index 17f7d1ce..6ed7fe67 100644
--- a/include/Makefile.am
+++ b/include/Makefile.am
@@ -52,6 +52,7 @@ nobase_include_HEADERS = \
osmocom/core/timer_compat.h \
osmocom/core/utils.h \
osmocom/core/write_queue.h \
+ osmocom/core/sockaddr_str.h \
osmocom/crypt/auth.h \
osmocom/crypt/gprs_cipher.h \
osmocom/ctrl/control_cmd.h \
diff --git a/include/osmocom/core/sockaddr_str.h b/include/osmocom/core/sockaddr_str.h
new file mode 100644
index 00000000..253b755f
--- /dev/null
+++ b/include/osmocom/core/sockaddr_str.h
@@ -0,0 +1,87 @@
+/*! \file sockaddr_str.h
+ * Common API 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.
+ *
+ */
+
+#pragma once
+
+#include <stdint.h>
+#include <stdbool.h>
+#include <arpa/inet.h>
+
+struct in_addr;
+struct in6_addr;
+struct sockaddr_storage;
+struct sockaddr_in;
+struct sockaddr_in6;
+
+/*! \defgroup sockaddr_str IP address/port utilities.
+ * @{
+ * \file sockaddr_str.h
+ */
+
+int osmo_ip_str_type(const char *ip);
+
+struct osmo_sockaddr_str {
+ /*! AF_INET for IPv4 address, or AF_INET6 for IPv6 address. */
+ int af;
+ /*! NUL terminated string of the IPv4 or IPv6 address. */
+ char ip[INET6_ADDRSTRLEN];
+ /*! Port number */
+ uint16_t port;
+};
+
+/*! Format string to print an osmo_sockaddr_str.
+ *
+ * For example:
+ *
+ * struct osmo_sockaddr_str *my_sockaddr_str = ...;
+ * printf("got " OSMO_SOCKADDR_STR_FMT "\n", OSMO_SOCKADDR_STR_FMT_ARGS(my_sockaddr_str));
+ */
+#define OSMO_SOCKADDR_STR_FMT "%s:%u"
+#define OSMO_SOCKADDR_STR_FMT_ARGS(R) ((R)->ip ? : ""), (R)->port
+
+bool osmo_sockaddr_str_is_set(const struct osmo_sockaddr_str *sockaddr_str);
+
+int osmo_sockaddr_str_from_str(struct osmo_sockaddr_str *sockaddr_str, const char *ip, uint16_t port);
+
+int osmo_sockaddr_str_from_in_addr(struct osmo_sockaddr_str *sockaddr_str, const struct in_addr *addr, uint16_t port);
+int osmo_sockaddr_str_from_in6_addr(struct osmo_sockaddr_str *sockaddr_str, const struct in6_addr *addr, uint16_t port);
+int osmo_sockaddr_str_from_32(struct osmo_sockaddr_str *sockaddr_str, uint32_t ip, uint16_t port);
+int osmo_sockaddr_str_from_32n(struct osmo_sockaddr_str *sockaddr_str, uint32_t ip, uint16_t port);
+int osmo_sockaddr_str_from_sockaddr_in(struct osmo_sockaddr_str *sockaddr_str, const struct sockaddr_in *src);
+int osmo_sockaddr_str_from_sockaddr_in6(struct osmo_sockaddr_str *sockaddr_str, const struct sockaddr_in6 *src);
+int osmo_sockaddr_str_from_sockaddr(struct osmo_sockaddr_str *sockaddr_str, const struct sockaddr_storage *src);
+
+int osmo_sockaddr_str_to_in_addr(const struct osmo_sockaddr_str *sockaddr_str, struct in_addr *dst);
+int osmo_sockaddr_str_to_in6_addr(const struct osmo_sockaddr_str *sockaddr_str, struct in6_addr *dst);
+int osmo_sockaddr_str_to_32(const struct osmo_sockaddr_str *sockaddr_str, uint32_t *ip);
+int osmo_sockaddr_str_to_32n(const struct osmo_sockaddr_str *sockaddr_str, uint32_t *ip);
+int osmo_sockaddr_str_to_sockaddr_in(const struct osmo_sockaddr_str *sockaddr_str, struct sockaddr_in *dst);
+int osmo_sockaddr_str_to_sockaddr_in6(const struct osmo_sockaddr_str *sockaddr_str, struct sockaddr_in6 *dst);
+int osmo_sockaddr_str_to_sockaddr(const struct osmo_sockaddr_str *sockaddr_str, struct sockaddr_storage *dst);
+
+/*! @} */