[PATCH] libosmo-abis[master]: add basic unixsocket support

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

lynxis lazus gerrit-no-reply at lists.osmocom.org
Thu Nov 3 04:11:34 UTC 2016


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

add basic unixsocket support

Allow to connect to a unix socket communicating with LAPD.

Change-Id: Ia5723b09a5c68a0505829dc732def981e60a907a
---
M src/Makefile.am
M src/e1_input.c
M src/e1_input_vty.c
A src/input/unixsocket.c
4 files changed, 220 insertions(+), 3 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/libosmo-abis refs/changes/98/1198/1

diff --git a/src/Makefile.am b/src/Makefile.am
index b24f2cf..760c1f5 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -24,7 +24,8 @@
 			 input/lapd.c \
 			 input/lapd_pcap.c \
 			 input/misdn.c \
-			 input/rs232.c
+			 input/rs232.c \
+			 input/unixsocket.c
 
 libosmotrau_la_CFLAGS = $(AM_CFLAGS) $(ORTP_CFLAGS)
 libosmotrau_la_LDFLAGS = $(AM_LDFLAGS) -version-info $(TRAU_LIBVERSION)
diff --git a/src/e1_input.c b/src/e1_input.c
index 970bdb9..7222cee 100644
--- a/src/e1_input.c
+++ b/src/e1_input.c
@@ -815,6 +815,7 @@
 void e1inp_dahdi_init(void);
 void e1inp_ipaccess_init(void);
 void e1inp_rs232_init(void);
+void e1inp_unixsocket_init(void);
 
 void e1inp_init(void)
 {
@@ -829,4 +830,5 @@
 #endif
 	e1inp_ipaccess_init();
 	e1inp_rs232_init();
+	e1inp_unixsocket_init();
 }
diff --git a/src/e1_input_vty.c b/src/e1_input_vty.c
index 5320bb3..47bc5ba 100644
--- a/src/e1_input_vty.c
+++ b/src/e1_input_vty.c
@@ -38,12 +38,13 @@
 
 /* CONFIG */
 
-#define E1_DRIVER_NAMES		"(misdn|misdn_lapd|dahdi|ipa)"
+#define E1_DRIVER_NAMES		"(misdn|misdn_lapd|dahdi|ipa|unixsocket)"
 #define E1_DRIVER_HELP		"mISDN supported E1 Card (kernel LAPD)\n" \
 				"mISDN supported E1 Card (userspace LAPD)\n" \
 				"DAHDI supported E1/T1/J1 Card\n" \
 				"IPA TCP/IP input\n" \
-				"HSL TCP/IP input"
+				"HSL TCP/IP input\n" \
+				"Unix socket input\n"
 
 #define E1_LINE_HELP		"Configure E1/T1/J1 Line\n" "Line Number\n"
 
diff --git a/src/input/unixsocket.c b/src/input/unixsocket.c
new file mode 100644
index 0000000..bcc86ed
--- /dev/null
+++ b/src/input/unixsocket.c
@@ -0,0 +1,213 @@
+/* OpenBSC Abis receive lapd over a unix socket */
+
+/*
+ *
+ * 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 <errno.h>
+#include <stdio.h>
+#include <unistd.h>
+
+#include <osmocom/core/talloc.h>
+#include <osmocom/core/logging.h>
+#include <osmocom/core/socket.h>
+
+#include <osmocom/abis/e1_input.h>
+#include <osmocom/abis/lapd.h>
+#include <osmocom/abis/e1_input.h>
+
+#include "internal.h"
+
+void *tall_unixsocket_ctx;
+#define UNIXSOCKET_ALLOC_SIZE 1600
+
+struct unixsocket_line {
+	struct osmo_fd fd;
+};
+
+static int ts_want_write(struct e1inp_ts *e1i_ts);
+
+static int unixsocket_exception_cb(struct osmo_fd *bfd)
+{
+	struct e1inp_line *line = bfd->data;
+	LOGP(DLINP, LOGL_ERROR, "unixsocket: closing socket. Exception cb called.\n");
+
+	close(bfd->fd);
+
+	return 0;
+}
+
+static int unixsocket_read_cb(struct osmo_fd *bfd)
+{
+	struct e1inp_line *line = bfd->data;
+	struct msgb *msg = msgb_alloc(UNIXSOCKET_ALLOC_SIZE, "UNIXSOCKET TS");
+	int ret;
+
+	if (!msg)
+		return -ENOMEM;
+
+	ret = read(bfd->fd, msg->data, UNIXSOCKET_ALLOC_SIZE - 16);
+	if (ret == 0) {
+		unixsocket_exception_cb(bfd);
+		return ret;
+	} else if (ret < 0) {
+		perror("read ");
+		return ret;
+	}
+	msgb_put(msg, ret);
+
+	return e1inp_rx_ts_lapd(&line->ts[0], msg);
+}
+
+static void timeout_ts1_write(void *data)
+{
+	struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
+
+	/* trigger write of ts1, due to tx delay timer */
+	ts_want_write(e1i_ts);
+}
+
+static int unixsocket_write_cb(struct osmo_fd *bfd)
+{
+	struct e1inp_line *line = bfd->data;
+	struct e1inp_ts *e1i_ts = &line->ts[0];
+	struct msgb *msg;
+	struct e1inp_sign_link *sign_link;
+
+	bfd->when &= ~BSC_FD_WRITE;
+
+	/* get the next msg for this timeslot */
+	msg = e1inp_tx_ts(e1i_ts, &sign_link);
+	if (!msg) {
+		/* no message after tx delay timer */
+		LOGP(DLINP, LOGL_INFO, "unixsocket: no message available");
+		return 0;
+	}
+
+	/* set tx delay timer for next event */
+	e1i_ts->sign.tx_timer.cb = timeout_ts1_write;
+	e1i_ts->sign.tx_timer.data = e1i_ts;
+
+	osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, e1i_ts->sign.delay);
+
+	LOGP(DLINP, LOGL_INFO, "unixsocket: sending: %s", msgb_hexdump(msg));
+	lapd_transmit(e1i_ts->lapd, sign_link->tei,
+			sign_link->sapi, msg);
+
+	return 0;
+}
+
+static int unixsocket_cb(struct osmo_fd *bfd, unsigned int what)
+{
+	int ret = 0;
+
+	if (what & BSC_FD_READ)
+		ret = unixsocket_read_cb(bfd);
+	if (what & BSC_FD_WRITE)
+		ret = unixsocket_write_cb(bfd);
+
+	return ret;
+}
+
+static int ts_want_write(struct e1inp_ts *e1i_ts)
+{
+	struct unixsocket_line *line = e1i_ts->line->driver_data;
+
+	line->fd.when |= BSC_FD_WRITE;
+
+	return 0;
+}
+
+/*!
+ * \brief unixsocket_write_msg lapd callback for data to unixsocket
+ * \param msg
+ * \param cbdata
+ */
+static void unixsocket_write_msg(struct msgb *msg, void *cbdata)
+{
+	struct osmo_fd *bfd = cbdata;
+	int ret;
+
+	ret = write(bfd->fd, msg->data, msg->len);
+	msgb_free(msg);
+	if (ret == -1)
+		unixsocket_exception_cb(bfd);
+	else if (ret < 0)
+		LOGP(DLMI, LOGL_NOTICE, "%s write failed %d\n", __func__, ret);
+}
+
+static int unixsocket_line_update(struct e1inp_line *line)
+{
+	struct unixsocket_line *config;
+	const char *sock_path = "/tmp/rsl_oml";
+	int ret = 0;
+	int i;
+
+	LOGP(DLINP, LOGL_NOTICE, "unixsocket: line update\n");
+
+	if (!line->driver_data)
+		line->driver_data = talloc_zero(line, struct unixsocket_line);
+
+	if (!line->driver_data) {
+		LOGP(DLINP, LOGL_ERROR, "unixsocket: OOM in line update\n");
+		return -ENOMEM;
+	}
+
+	config = line->driver_data;
+
+	config->fd.data = line;
+	config->fd.when = BSC_FD_READ;
+	config->fd.cb = unixsocket_cb;
+	ret = osmo_sock_unix_init(SOCK_SEQPACKET, 0, sock_path, OSMO_SOCK_F_CONNECT);
+
+	if (ret < 0) {
+		talloc_free(config);
+		return ret;
+	}
+
+	config->fd.fd = ret;
+	if (osmo_fd_register(&config->fd) < 0) {
+		close(config->fd.fd);
+		return -EIO;
+	}
+
+	for (i = 0; i < ARRAY_SIZE(line->ts); i++) {
+		struct e1inp_ts *e1i_ts = &line->ts[i];
+
+		if (!e1i_ts->lapd)
+			e1i_ts->lapd = lapd_instance_alloc(1,
+				unixsocket_write_msg, &config->fd, e1inp_dlsap_up,
+				e1i_ts, &lapd_profile_abis);
+	}
+
+	return ret;
+}
+
+struct e1inp_driver unixsocket_driver = {
+	.name = "unixsocket",
+	.want_write = ts_want_write,
+	.line_update = unixsocket_line_update,
+	.default_delay = 0,
+};
+
+void e1inp_unixsocket_init(void)
+{
+	tall_unixsocket_ctx = talloc_named_const(libosmo_abis_ctx, 1, "unixsocket");
+	e1inp_driver_register(&unixsocket_driver);
+}

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ia5723b09a5c68a0505829dc732def981e60a907a
Gerrit-PatchSet: 1
Gerrit-Project: libosmo-abis
Gerrit-Branch: master
Gerrit-Owner: lynxis lazus <lynxis at fe80.eu>



More information about the gerrit-log mailing list