Merge commit 'e476442cf0e84c65565ace545f5b73602b5f0ffc'
authorHarald Welte <laforge@gnumonks.org>
Sun, 22 May 2011 10:26:42 +0000 (12:26 +0200)
committerHarald Welte <laforge@gnumonks.org>
Sun, 22 May 2011 10:26:42 +0000 (12:26 +0200)
1  2 
src/shared/libosmocore/configure.in
src/shared/libosmocore/include/osmocom/core/socket.h
src/shared/libosmocore/src/gsmtap_util.c
src/shared/libosmocore/src/socket.c

index ead18f0,0000000..2e22bb2
mode 100644,000000..100644
--- /dev/null
@@@ -1,123 -1,0 +1,123 @@@
- AC_CHECK_HEADERS(execinfo.h sys/select.h syslog.h ctype.h)
 +AC_INIT([libosmocore],
 +      m4_esyscmd([./git-version-gen .tarball-version]),
 +      [openbsc-devel@lists.openbsc.org])
 +
 +AM_INIT_AUTOMAKE([dist-bzip2])
 +
 +dnl kernel style compile messages
 +m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([yes])])
 +
 +dnl checks for programs
 +AC_PROG_MAKE_SET
 +AC_PROG_CC
 +AC_PROG_INSTALL
 +LT_INIT
 +AC_PROG_LIBTOOL
 +
 +AC_CONFIG_MACRO_DIR([m4])
 +
 +dnl checks for header files
 +AC_HEADER_STDC
++AC_CHECK_HEADERS(execinfo.h sys/select.h sys/socket.h syslog.h ctype.h)
 +
 +# The following test is taken from WebKit's webkit.m4
 +saved_CFLAGS="$CFLAGS"
 +CFLAGS="$CFLAGS -fvisibility=hidden "
 +AC_MSG_CHECKING([if ${CC} supports -fvisibility=hidden])
 +AC_COMPILE_IFELSE([char foo;],
 +      [ AC_MSG_RESULT([yes])
 +        SYMBOL_VISIBILITY="-fvisibility=hidden"],
 +        AC_MSG_RESULT([no]))
 +CFLAGS="$saved_CFLAGS"
 +AC_SUBST(SYMBOL_VISIBILITY)
 +
 +dnl Generate the output
 +AM_CONFIG_HEADER(config.h)
 +
 +AC_ARG_ENABLE(talloc,
 +      [AS_HELP_STRING(
 +              [--disable-talloc],
 +              [Disable building talloc memory allocator]
 +      )],
 +      [enable_talloc=0], [enable_talloc=1])
 +AM_CONDITIONAL(ENABLE_TALLOC, test "x$enable_talloc" = "x1")
 +
 +AC_ARG_ENABLE(plugin,
 +      [AS_HELP_STRING(
 +              [--disable-plugin],
 +              [Disable support for dlopen plugins],
 +      )],
 +      [enable_plugin=0], [enable_plugin=1])
 +AM_CONDITIONAL(ENABLE_PLUGIN, test "x$enable_plugin" = "x1")
 +
 +AC_ARG_ENABLE(tests,
 +      [AS_HELP_STRING(
 +              [--disable-tests],
 +              [Disable building test programs]
 +      )],
 +      [enable_tests=0], [enable_tests=1])
 +AM_CONDITIONAL(ENABLE_TESTS, test "x$enable_tests" = "x1")
 +
 +AC_ARG_ENABLE(vty,
 +      [AS_HELP_STRING(
 +              [--disable-vty],
 +              [Disable building VTY telnet interface]
 +      )],
 +      [enable_vty=0], [enable_vty=1])
 +AM_CONDITIONAL(ENABLE_VTY, test "x$enable_vty" = "x1")
 +
 +AC_ARG_ENABLE(panic_infloop,
 +      [AS_HELP_STRING(
 +              [--enable-panic-infloop],
 +              [Trigger infinite loop on panic rather than fprintf/abort]
 +      )],
 +      [panic_infloop=1], [panic_infloop=0])
 +if test "x$panic_infloop" = "x1"
 +then
 +      AC_DEFINE([PANIC_INFLOOP],[1],[Use infinite loop on panic rather than fprintf/abort])
 +fi
 +
 +AC_ARG_ENABLE(bsc_fd_check,
 +      [AS_HELP_STRING(
 +              [--enable-bsc-fd-check],
 +              [Instrument bsc_register_fd to check that the fd is registered]
 +      )],
 +      [fd_check=1], [fd_check=0])
 +if test "x$fd_check" = "x1"
 +then
 +      AC_DEFINE([BSC_FD_CHECK],[1],[Instrument the bsc_register_fd])
 +fi
 +
 +AC_ARG_ENABLE(msgfile,
 +      [AS_HELP_STRING(
 +              [--disable-msgfile],
 +              [Disable support for the msgfile],
 +      )],
 +      [enable_msgfile=0], [enable_msgfile=1])
 +AM_CONDITIONAL(ENABLE_MSGFILE, test "x$enable_msgfile" = "x1")
 +
 +
 +AC_OUTPUT(
 +      libosmocore.pc
 +      libosmocodec.pc
 +      libosmovty.pc
 +      libosmogsm.pc
 +      include/osmocom/Makefile
 +      include/osmocom/vty/Makefile
 +      include/osmocom/codec/Makefile
 +      include/osmocom/crypt/Makefile
 +      include/osmocom/gsm/Makefile
 +      include/osmocom/gsm/protocol/Makefile
 +      include/osmocom/core/Makefile
 +      include/Makefile
 +      src/Makefile
 +      src/vty/Makefile
 +      src/codec/Makefile
 +      src/gsm/Makefile
 +      tests/Makefile
 +      tests/timer/Makefile
 +      tests/sms/Makefile
 +      tests/msgfile/Makefile
 +      tests/ussd/Makefile
 +      tests/smscb/Makefile
 +      Makefile)
index 3ede524,0000000..a3baa9d
mode 100644,000000..100644
--- /dev/null
@@@ -1,16 -1,0 +1,17 @@@
- #include <sys/socket.h>
 +#ifndef _OSMOCORE_SOCKET_H
 +#define _OSMOCORE_SOCKET_H
 +
 +#include <stdint.h>
- int osmo_sockaddr_is_local(struct sockaddr *addr, socklen_t addrlen);
++
++struct sockaddr;
 +
 +int osmo_sock_init(uint16_t family, uint16_t type, uint8_t proto,
 +                 const char *host, uint16_t port, int connect0_bind1);
 +
 +int osmo_sock_init_sa(struct sockaddr *ss, uint16_t type,
 +                    uint8_t proto, int connect0_bind1);
 +
 +/* determine if the given address is a local address */
++int osmo_sockaddr_is_local(struct sockaddr *addr, unsigned int addrlen);
 +
 +#endif /* _OSMOCORE_SOCKET_H */
index 1542635,0000000..5c68b6a
mode 100644,000000..100644
--- /dev/null
@@@ -1,273 -1,0 +1,275 @@@
- #include <arpa/inet.h>
- #include <sys/socket.h>
 +/* GSMTAP support code in libmsomcore */
 +/*
 + * (C) 2010-2011 by Harald Welte <laforge@gnumonks.org>
 + *
 + * All Rights Reserved
 + *
 + * 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"
 +
 +#include <osmocom/core/gsmtap_util.h>
 +#include <osmocom/core/logging.h>
 +#include <osmocom/core/gsmtap.h>
 +#include <osmocom/core/msgb.h>
 +#include <osmocom/core/talloc.h>
 +#include <osmocom/core/select.h>
 +#include <osmocom/core/socket.h>
 +#include <osmocom/gsm/protocol/gsm_04_08.h>
 +#include <osmocom/gsm/rsl.h>
 +
- #include <netinet/in.h>
 +#include <sys/types.h>
- #ifdef HAVE_SYS_SELECT_H
++
++#include <arpa/inet.h>
 +
 +#include <stdio.h>
 +#include <unistd.h>
 +#include <stdint.h>
 +#include <string.h>
 +#include <errno.h>
 +
 +uint8_t chantype_rsl2gsmtap(uint8_t rsl_chantype, uint8_t link_id)
 +{
 +      uint8_t ret = GSMTAP_CHANNEL_UNKNOWN;
 +
 +      switch (rsl_chantype) {
 +      case RSL_CHAN_Bm_ACCHs:
 +              ret = GSMTAP_CHANNEL_TCH_F;
 +              break;
 +      case RSL_CHAN_Lm_ACCHs:
 +              ret = GSMTAP_CHANNEL_TCH_H;
 +              break;
 +      case RSL_CHAN_SDCCH4_ACCH:
 +              ret = GSMTAP_CHANNEL_SDCCH4;
 +              break;
 +      case RSL_CHAN_SDCCH8_ACCH:
 +              ret = GSMTAP_CHANNEL_SDCCH8;
 +              break;
 +      case RSL_CHAN_BCCH:
 +              ret = GSMTAP_CHANNEL_BCCH;
 +              break;
 +      case RSL_CHAN_RACH:
 +              ret = GSMTAP_CHANNEL_RACH;
 +              break;
 +      case RSL_CHAN_PCH_AGCH:
 +              /* it could also be AGCH... */
 +              ret = GSMTAP_CHANNEL_PCH;
 +              break;
 +      }
 +
 +      if (link_id & 0x40)
 +              ret |= GSMTAP_CHANNEL_ACCH;
 +
 +      return ret;
 +}
 +
 +/* receive a message from L1/L2 and put it in GSMTAP */
 +struct msgb *gsmtap_makemsg(uint16_t arfcn, uint8_t ts, uint8_t chan_type,
 +                          uint8_t ss, uint32_t fn, int8_t signal_dbm,
 +                          uint8_t snr, const uint8_t *data, unsigned int len)
 +{
 +      struct msgb *msg;
 +      struct gsmtap_hdr *gh;
 +      uint8_t *dst;
 +
 +      msg = msgb_alloc(sizeof(*gh) + len, "gsmtap_tx");
 +      if (!msg)
 +              return NULL;
 +
 +      gh = (struct gsmtap_hdr *) msgb_put(msg, sizeof(*gh));
 +
 +      gh->version = GSMTAP_VERSION;
 +      gh->hdr_len = sizeof(*gh)/4;
 +      gh->type = GSMTAP_TYPE_UM;
 +      gh->timeslot = ts;
 +      gh->sub_slot = ss;
 +      gh->arfcn = htons(arfcn);
 +      gh->snr_db = snr;
 +      gh->signal_dbm = signal_dbm;
 +      gh->frame_number = htonl(fn);
 +      gh->sub_type = chan_type;
 +      gh->antenna_nr = 0;
 +
 +      dst = msgb_put(msg, len);
 +      memcpy(dst, data, len);
 +
 +      return msg;
 +}
 +
++#ifdef HAVE_SYS_SOCKET_H
++
++#include <sys/socket.h>
++#include <netinet/in.h>
++
 +/* Open a GSMTAP source (sending) socket, conncet it to host/port and
 + * return resulting fd */
 +int gsmtap_source_init_fd(const char *host, uint16_t port)
 +{
 +      if (port == 0)
 +              port = GSMTAP_UDP_PORT;
 +      if (host == NULL)
 +              host = "localhost";
 +
 +      return osmo_sock_init(AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, host, port, 0);
 +}
 +
 +int gsmtap_source_add_sink_fd(int gsmtap_fd)
 +{
 +      struct sockaddr_storage ss;
 +      socklen_t ss_len = sizeof(ss);
 +      int rc;
 +
 +      rc = getpeername(gsmtap_fd, (struct sockaddr *)&ss, &ss_len);
 +      if (rc < 0)
 +              return rc;
 +
 +      if (osmo_sockaddr_is_local((struct sockaddr *)&ss, ss_len) == 1) {
 +              rc = osmo_sock_init_sa((struct sockaddr *)&ss, SOCK_DGRAM, IPPROTO_UDP, 1);
 +              if (rc >= 0)
 +                      return rc;
 +      }
 +
 +      return -ENODEV;
 +}
 +
- #endif /* HAVE_SYS_SELECT_H */
 +int gsmtap_sendmsg(struct gsmtap_inst *gti, struct msgb *msg)
 +{
 +      if (gti->ofd_wq_mode)
 +              return osmo_wqueue_enqueue(&gti->wq, msg);
 +      else {
 +              /* try immediate send and return error if any */
 +              int rc;
 +
 +              rc = write(gsmtap_inst_fd(gti), msg->data, msg->len);
 +              if (rc <= 0) {
 +                      return rc;
 +              } else if (rc >= msg->len) {
 +                      msgb_free(msg);
 +                      return 0;
 +              } else {
 +                      /* short write */
 +                      return -EIO;
 +              }
 +      }
 +}
 +
 +/* receive a message from L1/L2 and put it in GSMTAP */
 +int gsmtap_send(struct gsmtap_inst *gti, uint16_t arfcn, uint8_t ts,
 +              uint8_t chan_type, uint8_t ss, uint32_t fn,
 +              int8_t signal_dbm, uint8_t snr, const uint8_t *data,
 +              unsigned int len)
 +{
 +      struct msgb *msg;
 +
 +      msg = gsmtap_makemsg(arfcn, ts, chan_type, ss, fn, signal_dbm,
 +                           snr, data, len);
 +      if (!msg)
 +              return -ENOMEM;
 +
 +      return gsmtap_sendmsg(gti, msg);
 +}
 +
 +/* Callback from select layer if we can write to the socket */
 +static int gsmtap_wq_w_cb(struct osmo_fd *ofd, struct msgb *msg)
 +{
 +      int rc;
 +
 +      rc = write(ofd->fd, msg->data, msg->len);
 +      if (rc < 0) {
 +              perror("writing msgb to gsmtap fd");
 +              msgb_free(msg);
 +              return rc;
 +      }
 +      if (rc != msg->len) {
 +              perror("short write to gsmtap fd");
 +              msgb_free(msg);
 +              return -EIO;
 +      }
 +
 +      msgb_free(msg);
 +      return 0;
 +}
 +
 +/* Callback from select layer if we can read from the sink socket */
 +static int gsmtap_sink_fd_cb(struct osmo_fd *fd, unsigned int flags)
 +{
 +      int rc;
 +      uint8_t buf[4096];
 +
 +      if (!(flags & BSC_FD_READ))
 +              return 0;
 +
 +      rc = read(fd->fd, buf, sizeof(buf));
 +      if (rc < 0) {
 +              perror("reading from gsmtap sink fd");
 +              return rc;
 +      }
 +      /* simply discard any data arriving on the socket */
 +
 +      return 0;
 +}
 +
 +/* Add a local sink to an existing GSMTAP source instance */
 +int gsmtap_source_add_sink(struct gsmtap_inst *gti)
 +{
 +      int fd;
 +
 +      fd = gsmtap_source_add_sink_fd(gsmtap_inst_fd(gti));
 +      if (fd < 0)
 +              return fd;
 +
 +      if (gti->ofd_wq_mode) {
 +              struct osmo_fd *sink_ofd;
 +
 +              sink_ofd = &gti->sink_ofd;
 +              sink_ofd->fd = fd;
 +              sink_ofd->when = BSC_FD_READ;
 +              sink_ofd->cb = gsmtap_sink_fd_cb;
 +
 +              osmo_fd_register(sink_ofd);
 +      }
 +
 +      return fd;
 +}
 +
 +/* like gsmtap_init2() but integrated with libosmocore select.c */
 +struct gsmtap_inst *gsmtap_source_init(const char *host, uint16_t port,
 +                                      int ofd_wq_mode)
 +{
 +      struct gsmtap_inst *gti;
 +      int fd;
 +
 +      fd = gsmtap_source_init_fd(host, port);
 +      if (fd < 0)
 +              return NULL;
 +
 +      gti = talloc_zero(NULL, struct gsmtap_inst);
 +      gti->ofd_wq_mode = ofd_wq_mode;
 +      gti->wq.bfd.fd = fd;
 +      gti->sink_ofd.fd = -1;
 +
 +      if (ofd_wq_mode) {
 +              osmo_wqueue_init(&gti->wq, 64);
 +              gti->wq.write_cb = &gsmtap_wq_w_cb;
 +
 +              osmo_fd_register(&gti->wq.bfd);
 +      }
 +
 +      return gti;
 +}
 +
++#endif /* HAVE_SYS_SOCKET_H */
index bd4914f,0000000..e053c24
mode 100644,000000..100644
--- /dev/null
@@@ -1,143 -1,0 +1,147 @@@
-                         const struct sockaddr *b, socklen_t len)
 +#include "../config.h"
 +
++#ifdef HAVE_SYS_SOCKET_H
++
 +#include <osmocom/core/logging.h>
 +#include <osmocom/core/select.h>
 +#include <osmocom/core/socket.h>
 +
 +#include <arpa/inet.h>
 +#include <sys/socket.h>
 +#include <sys/types.h>
 +#include <netinet/in.h>
 +
 +#include <stdio.h>
 +#include <unistd.h>
 +#include <stdint.h>
 +#include <string.h>
 +#include <errno.h>
 +#include <netdb.h>
 +#include <ifaddrs.h>
 +
 +int osmo_sock_init(uint16_t family, uint16_t type, uint8_t proto,
 +                 const char *host, uint16_t port, int connect0_bind1)
 +{
 +      struct addrinfo hints, *result, *rp;
 +      int sfd, rc;
 +      char portbuf[16];
 +
 +      sprintf(portbuf, "%u", port);
 +      memset(&hints, 0, sizeof(struct addrinfo));
 +      hints.ai_family = family;
 +      hints.ai_socktype = type;
 +      hints.ai_flags = 0;
 +      hints.ai_protocol = proto;
 +
 +      rc = getaddrinfo(host, portbuf, &hints, &result);
 +      if (rc != 0) {
 +              perror("getaddrinfo returned NULL");
 +              return -EINVAL;
 +      }
 +
 +      for (rp = result; rp != NULL; rp = rp->ai_next) {
 +              sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
 +              if (sfd == -1)
 +                      continue;
 +              if (connect0_bind1 == 0) {
 +                      if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1)
 +                              break;
 +              } else {
 +                      if (bind(sfd, rp->ai_addr, rp->ai_addrlen) != -1)
 +                              break;
 +              }
 +              close(sfd);
 +      }
 +      freeaddrinfo(result);
 +
 +      if (rp == NULL) {
 +              perror("unable to connect/bind socket");
 +              return -ENODEV;
 +      }
 +      return sfd;
 +}
 +
 +int osmo_sock_init_sa(struct sockaddr *ss, uint16_t type,
 +                    uint8_t proto, int connect0_bind1)
 +{
 +      char host[NI_MAXHOST];
 +      uint16_t port;
 +      struct sockaddr_in *sin;
 +      struct sockaddr_in6 *sin6;
 +      int s, sa_len;
 +
 +      /* determine port and host from ss */
 +      switch (ss->sa_family) {
 +      case AF_INET:
 +              sin = (struct sockaddr_in *) ss;
 +              sa_len = sizeof(struct sockaddr_in);
 +              port = ntohs(sin->sin_port);
 +              break;
 +      case AF_INET6:
 +              sin6 = (struct sockaddr_in6 *) ss;
 +              sa_len = sizeof(struct sockaddr_in6);
 +              port = ntohs(sin6->sin6_port);
 +              break;
 +      default:
 +              return -EINVAL;
 +      }
 +      fprintf(stderr, "==> PORT = %u\n", port);
 +
 +      s = getnameinfo(ss, sa_len, host, NI_MAXHOST,
 +                      NULL, 0, NI_NUMERICHOST);
 +      if (s != 0) {
 +              perror("getnameinfo failed");
 +              return s;
 +      }
 +
 +      return osmo_sock_init(ss->sa_family, type, proto, host,
 +                            port, connect0_bind1);
 +}
 +
 +static int sockaddr_equal(const struct sockaddr *a,
++                        const struct sockaddr *b, unsigned int len)
 +{
 +      struct sockaddr_in *sin_a, *sin_b;
 +      struct sockaddr_in6 *sin6_a, *sin6_b;
 +
 +      if (a->sa_family != b->sa_family)
 +              return 0;
 +
 +      switch (a->sa_family) {
 +      case AF_INET:
 +              sin_a = (struct sockaddr_in *)a;
 +              sin_b = (struct sockaddr_in *)b;
 +              if (!memcmp(&sin_a->sin_addr, &sin_b->sin_addr,
 +                          sizeof(struct in_addr)))
 +                      return 1;
 +              break;
 +      case AF_INET6:
 +              sin6_a = (struct sockaddr_in6 *)a;
 +              sin6_b = (struct sockaddr_in6 *)b;
 +              if (!memcmp(&sin6_a->sin6_addr, &sin6_b->sin6_addr,
 +                          sizeof(struct in6_addr)))
 +                      return 1;
 +              break;
 +      }
 +      return 0;
 +}
 +
 +/* determine if the given address is a local address */
 +int osmo_sockaddr_is_local(struct sockaddr *addr, socklen_t addrlen)
 +{
 +      struct ifaddrs *ifaddr, *ifa;
 +
 +      if (getifaddrs(&ifaddr) == -1) {
 +              perror("getifaddrs");
 +              return -EIO;
 +      }
 +
 +      for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
 +              if (sockaddr_equal(ifa->ifa_addr, addr, addrlen))
 +                      return 1;
 +      }
 +
 +      return 0;
 +}
++
++#endif /* HAVE_SYS_SOCKET_H */