aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNeels Hofmeyr <nhofmeyr@sysmocom.de>2016-09-19 21:26:52 +0200
committerNeels Hofmeyr <nhofmeyr@sysmocom.de>2016-09-22 21:00:42 +0200
commit0b918d8285d06cb3a97f3f36b737c3eb5edc2174 (patch)
treefb40215f041a23b0f8bf2dc598eea9a7882b1ff7
parent8ce66fd19efd7edb7f25ce08b9543f87f701395a (diff)
move osmo_sockaddr_* to libosmocore sockaddr.cneels/osmo_sockaddr
-rw-r--r--openbsc/include/openbsc/gtphub.h98
-rw-r--r--openbsc/src/gprs/gtphub.c146
-rw-r--r--openbsc/src/gprs/gtphub_ares.c1
3 files changed, 4 insertions, 241 deletions
diff --git a/openbsc/include/openbsc/gtphub.h b/openbsc/include/openbsc/gtphub.h
index 9cb7605f8..08f66ca9a 100644
--- a/openbsc/include/openbsc/gtphub.h
+++ b/openbsc/include/openbsc/gtphub.h
@@ -27,106 +27,10 @@
#include <osmocom/core/select.h>
#include <osmocom/core/timer.h>
#include <osmocom/core/rate_ctr.h>
+#include <osmocom/core/sockaddr.h>
#include <openbsc/gprs_sgsn.h>
-
-/* support */
-
-/* TODO move to osmocom/core/socket.c ? */
-#include <netdb.h> /* for IPPROTO_* etc */
-struct osmo_sockaddr {
- struct sockaddr_storage a;
- socklen_t l;
-};
-
-/* TODO move to osmocom/core/socket.c ? */
-/*! \brief Initialize a sockaddr
- * \param[out] addr Valid osmo_sockaddr pointer to write result to
- * \param[in] family Address Family like AF_INET, AF_INET6, AF_UNSPEC
- * \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM
- * \param[in] proto Protocol like IPPROTO_TCP, IPPROTO_UDP
- * \param[in] host Remote host name or IP address in string form
- * \param[in] port Remote port number in host byte order
- * \returns 0 on success, otherwise an error code (from getaddrinfo()).
- *
- * Copy the first result from a getaddrinfo() call with the given parameters to
- * *addr and *addr_len. On error, do not change *addr and return nonzero.
- */
-int osmo_sockaddr_init(struct osmo_sockaddr *addr,
- uint16_t family, uint16_t type, uint8_t proto,
- const char *host, uint16_t port);
-
-/* Conveniently pass AF_UNSPEC, SOCK_DGRAM and IPPROTO_UDP to
- * osmo_sockaddr_init(). */
-static inline int osmo_sockaddr_init_udp(struct osmo_sockaddr *addr,
- const char *host, uint16_t port)
-{
- return osmo_sockaddr_init(addr, AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP,
- host, port);
-}
-
-/*! \brief convert sockaddr to human readable string.
- * \param[out] addr_str Valid pointer to a buffer of length addr_str_len.
- * \param[in] addr_str_len Size of buffer addr_str points at.
- * \param[out] port_str Valid pointer to a buffer of length port_str_len.
- * \param[in] port_str_len Size of buffer port_str points at.
- * \param[in] addr Binary representation as returned by osmo_sockaddr_init().
- * \param[in] flags flags as passed to getnameinfo().
- * \returns 0 on success, an error code on error.
- *
- * Return the IPv4 or IPv6 address string and the port (a.k.a. service) string
- * representations of the given struct osmo_sockaddr in two caller provided
- * char buffers. Flags of (NI_NUMERICHOST | NI_NUMERICSERV) return numeric
- * address and port. Either one of addr_str or port_str may be NULL, in which
- * case nothing is returned there.
- *
- * See also osmo_sockaddr_to_str() (less flexible, but much more convenient). */
-int osmo_sockaddr_to_strs(char *addr_str, size_t addr_str_len,
- char *port_str, size_t port_str_len,
- const struct osmo_sockaddr *addr,
- int flags);
-
-
-/*! \brief concatenate the parts returned by osmo_sockaddr_to_strs().
- * \param[in] addr Binary representation as returned by osmo_sockaddr_init().
- * \param[in] buf A buffer to use for string operations.
- * \param[in] buf_len Length of the buffer.
- * \returns Address string (in buffer).
- *
- * Compose a string of the numeric IP-address and port represented by *addr of
- * the form "<ip-addr> port <port>". The returned string is valid until the
- * next invocation of this function.
- */
-const char *osmo_sockaddr_to_strb(const struct osmo_sockaddr *addr,
- char *buf, size_t buf_len);
-
-/*! \brief conveniently return osmo_sockaddr_to_strb() in a static buffer.
- * \param[in] addr Binary representation as returned by osmo_sockaddr_init().
- * \returns Address string in static buffer.
- *
- * See osmo_sockaddr_to_strb().
- *
- * Note: only one osmo_sockaddr_to_str() call will work per print/log
- * statement. For two or more, use osmo_sockaddr_to_strb() with a separate
- * buffer each.
- */
-const char *osmo_sockaddr_to_str(const struct osmo_sockaddr *addr);
-
-/*! \brief compare two osmo_sockaddr.
- * \param[in] a The first address to compare.
- * \param[in] b The other address to compare.
- * \returns 0 if equal, otherwise -1 or 1.
- */
-int osmo_sockaddr_cmp(const struct osmo_sockaddr *a,
- const struct osmo_sockaddr *b);
-
-/*! \brief Overwrite *dst with *src.
- * Like memcpy(), but copy only the valid bytes. */
-void osmo_sockaddr_copy(struct osmo_sockaddr *dst,
- const struct osmo_sockaddr *src);
-
-
/* general */
enum gtphub_plane_idx {
diff --git a/openbsc/src/gprs/gtphub.c b/openbsc/src/gprs/gtphub.c
index 58300ea13..b04469edb 100644
--- a/openbsc/src/gprs/gtphub.c
+++ b/openbsc/src/gprs/gtphub.c
@@ -25,9 +25,10 @@
#include <time.h>
#include <limits.h>
#include <unistd.h>
-#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
+#include <sys/socket.h>
+#include <netdb.h>
#include <gtp.h>
#include <gtpie.h>
@@ -2786,146 +2787,3 @@ static int gtphub_resolve_ggsn(struct gtphub *hub,
}
-/* TODO move to osmocom/core/socket.c ? */
-/* use this in osmo_sock_init() to remove dup. */
-/* Internal: call getaddrinfo for osmo_sockaddr_init(). The caller is required
- to call freeaddrinfo(*result), iff zero is returned. */
-static int _osmo_getaddrinfo(struct addrinfo **result,
- uint16_t family, uint16_t type, uint8_t proto,
- const char *host, uint16_t port)
-{
- struct addrinfo hints;
- char portbuf[16];
-
- sprintf(portbuf, "%u", port);
- memset(&hints, '\0', sizeof(struct addrinfo));
- hints.ai_family = family;
- if (type == SOCK_RAW) {
- /* Workaround for glibc, that returns EAI_SERVICE (-8) if
- * SOCK_RAW and IPPROTO_GRE is used.
- */
- hints.ai_socktype = SOCK_DGRAM;
- hints.ai_protocol = IPPROTO_UDP;
- } else {
- hints.ai_socktype = type;
- hints.ai_protocol = proto;
- }
-
- return getaddrinfo(host, portbuf, &hints, result);
-}
-
-/* TODO move to osmocom/core/socket.c ? */
-int osmo_sockaddr_init(struct osmo_sockaddr *addr,
- uint16_t family, uint16_t type, uint8_t proto,
- const char *host, uint16_t port)
-{
- struct addrinfo *res;
- int rc;
- rc = _osmo_getaddrinfo(&res, family, type, proto, host, port);
-
- if (rc != 0) {
- LOG(LOGL_ERROR, "getaddrinfo returned error %d\n", (int)rc);
- return -EINVAL;
- }
-
- OSMO_ASSERT(res->ai_addrlen <= sizeof(addr->a));
- memcpy(&addr->a, res->ai_addr, res->ai_addrlen);
- addr->l = res->ai_addrlen;
- freeaddrinfo(res);
-
- return 0;
-}
-
-int osmo_sockaddr_to_strs(char *addr_str, size_t addr_str_len,
- char *port_str, size_t port_str_len,
- const struct osmo_sockaddr *addr,
- int flags)
-{
- int rc;
-
- if ((addr->l < 1) || (addr->l > sizeof(addr->a))) {
- LOGP(DGTPHUB, LOGL_ERROR, "Invalid address size: %d\n", addr->l);
- return -1;
- }
-
- if (addr->l > sizeof(addr->a)) {
- LOGP(DGTPHUB, LOGL_ERROR, "Invalid address: too long: %d\n",
- addr->l);
- return -1;
- }
-
- rc = getnameinfo((struct sockaddr*)&addr->a, addr->l,
- addr_str, addr_str_len,
- port_str, port_str_len,
- flags);
-
- if (rc)
- LOGP(DGTPHUB, LOGL_ERROR, "Invalid address: %s: %s\n",
- gai_strerror(rc), osmo_hexdump((uint8_t*)&addr->a,
- addr->l));
-
- return rc;
-}
-
-const char *osmo_sockaddr_to_strb(const struct osmo_sockaddr *addr,
- char *buf, size_t buf_len)
-{
- const int portbuf_len = 6;
- OSMO_ASSERT(buf_len > portbuf_len);
- char *portbuf = buf + buf_len - portbuf_len;
- buf_len -= portbuf_len;
- if (osmo_sockaddr_to_strs(buf, buf_len,
- portbuf, portbuf_len,
- addr,
- NI_NUMERICHOST | NI_NUMERICSERV))
- return NULL;
-
- char *pos = buf + strnlen(buf, buf_len-1);
- size_t len = buf_len - (pos - buf);
-
- snprintf(pos, len, " port %s", portbuf);
- buf[buf_len-1] = '\0';
-
- return buf;
-}
-
-const char *osmo_sockaddr_to_str(const struct osmo_sockaddr *addr)
-{
- static char buf[256];
- const char *result = osmo_sockaddr_to_strb(addr, buf, sizeof(buf));
- if (! result)
- return "(invalid)";
- return result;
-}
-
-int osmo_sockaddr_cmp(const struct osmo_sockaddr *a,
- const struct osmo_sockaddr *b)
-{
- if (a == b)
- return 0;
- if (!a)
- return -1;
- if (!b)
- return 1;
- if (a->l != b->l) {
- /* Lengths are not the same, but determine the order. Will
- * anyone ever sort a list by osmo_sockaddr though...? */
- int cmp = memcmp(&a->a, &b->a, (a->l < b->l)? a->l : b->l);
- if (cmp == 0) {
- if (a->l < b->l)
- return -1;
- else
- return 1;
- }
- return cmp;
- }
- return memcmp(&a->a, &b->a, a->l);
-}
-
-void osmo_sockaddr_copy(struct osmo_sockaddr *dst,
- const struct osmo_sockaddr *src)
-{
- OSMO_ASSERT(src->l <= sizeof(dst->a));
- memcpy(&dst->a, &src->a, src->l);
- dst->l = src->l;
-}
diff --git a/openbsc/src/gprs/gtphub_ares.c b/openbsc/src/gprs/gtphub_ares.c
index 667013b8b..0793ed421 100644
--- a/openbsc/src/gprs/gtphub_ares.c
+++ b/openbsc/src/gprs/gtphub_ares.c
@@ -27,6 +27,7 @@
#include <string.h>
#include <unistd.h>
+#include <netdb.h>
#include <openbsc/gtphub.h>
#include <openbsc/debug.h>