[PATCH] libosmocore[master]: utils: add osmo-gsmtap-sink, simplistic UDP receiver

Neels Hofmeyr gerrit-no-reply at lists.osmocom.org
Fri Feb 16 03:34:47 UTC 2018


Review at  https://gerrit.osmocom.org/6516

utils: add osmo-gsmtap-sink, simplistic UDP receiver

Yes, I know, but this is even simpler than a netcat.

Change-Id: I85499af06bc5a0d757f6071779075e57bb435930
---
M utils/Makefile.am
A utils/osmo-gsmtap-sink.c
2 files changed, 121 insertions(+), 1 deletion(-)


  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/16/6516/1

diff --git a/utils/Makefile.am b/utils/Makefile.am
index d4999bd..6b80b3e 100644
--- a/utils/Makefile.am
+++ b/utils/Makefile.am
@@ -5,12 +5,14 @@
 
 EXTRA_DIST = conv_gen.py conv_codes_gsm.py
 
-bin_PROGRAMS = osmo-arfcn osmo-auc-gen
+bin_PROGRAMS = osmo-arfcn osmo-auc-gen osmo-gsmtap-sink
 
 osmo_arfcn_SOURCES = osmo-arfcn.c
 
 osmo_auc_gen_SOURCES = osmo-auc-gen.c
 
+osmo_gsmtap_sink_SOURCES = osmo-gsmtap-sink.c
+
 if ENABLE_PCSC
 noinst_PROGRAMS = osmo-sim-test
 osmo_sim_test_SOURCES = osmo-sim-test.c
diff --git a/utils/osmo-gsmtap-sink.c b/utils/osmo-gsmtap-sink.c
new file mode 100644
index 0000000..3f4bf51
--- /dev/null
+++ b/utils/osmo-gsmtap-sink.c
@@ -0,0 +1,118 @@
+/*! \file osmo-gsmtap-sink.c
+ * Receive GSMTAP without causing ICMP reject messages */
+/*
+ * (C) 2018 by sysmocom - s.f.m.c. GmbH <info at sysmocom.de>
+ *
+ * All Rights Reserved
+ *
+ * Author: Neels Hofmeyr <nhofmeyr at sysmocom.de>
+ *
+ * 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 <stdlib.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <getopt.h>
+#include <netinet/in.h>
+#include <stdbool.h>
+
+#include <osmocom/core/socket.h>
+#include <osmocom/core/gsmtap.h>
+#include <osmocom/core/select.h>
+
+static void help(const char *progname)
+{
+	printf("Usage: %s [-hv] [<local-ip> [<local-port>]]\n"
+	       "\n"
+	       "Bind to UDP local-ip:local-port, read and discard everything received.\n"
+	       "Default is to listen on 'any' interface at the GSMTAP port %u\n"
+	       "(hence invoking this is a no-brainer compared to e.g. a netcat).\n"
+	       "\n"
+	       "Rationale: sending GSMTAP has the sole purpose of being picked up by a\n"
+	       "network trace like tcpdump. However, sending GSMTAP to an unbound port\n"
+	       "will trigger ICMP packets to indicate rejection, and this may even\n"
+	       "cause GSMTAP packets to go missing from the trace. Simply launch this\n"
+	       "program, a black hole to receive GSMTAP, to resolve these problems.\n"
+	       "\n"
+	       "  -h  show help\n"
+	       "  -v  verbose: print a dot for each received packet\n",
+	       progname, GSMTAP_UDP_PORT);
+}
+
+bool verbose = false;
+
+int receive_cb(struct osmo_fd *ofd, unsigned int what)
+{
+	static uint8_t buf[4096];
+
+	if (!(what & BSC_FD_READ))
+		return 0;
+
+	recv(ofd->fd, buf, sizeof(buf), 0);
+	if (verbose) {
+		printf(".");
+		fflush(stdout);
+	}
+	return 0;
+}
+
+int main(int argc, char **argv)
+{
+	int opt;
+	char *local_ip = NULL;
+	uint16_t local_port = GSMTAP_UDP_PORT;
+	struct osmo_fd ofd;
+
+	while ((opt = getopt(argc, argv, "hv")) != -1) {
+		switch (opt) {
+		case 'v':
+			verbose = true;
+			break;
+		case 'h':
+			help(argv[0]);
+			exit(0);
+			break;
+		default:
+			break;
+		}
+	}
+
+	if (argc - optind > 0) {
+		local_ip = argv[optind++];
+	}
+
+	if (argc - optind > 0) {
+		local_port = atoi(argv[optind++]);
+	}
+
+	if (argc - optind > 0) {
+		help(argv[0]);
+		exit(0);
+	}
+
+	ofd.cb = receive_cb;
+	osmo_sock_init_ofd(&ofd, AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP,
+			   local_ip, local_port, OSMO_SOCK_F_BIND);
+
+	while (1) {
+		int rc;
+		rc = osmo_select_main(0);
+		if (rc < 0)
+			exit(1);
+	}
+	exit(0);
+}

-- 
To view, visit https://gerrit.osmocom.org/6516
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I85499af06bc5a0d757f6071779075e57bb435930
Gerrit-PatchSet: 1
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Owner: Neels Hofmeyr <nhofmeyr at sysmocom.de>


More information about the gerrit-log mailing list