[PATCH] osmo-bsc[master]: abisip-find: add getopts and option to prevent endless loop

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/.

dexter gerrit-no-reply at lists.osmocom.org
Sun Dec 10 20:54:45 UTC 2017


Hello Jenkins Builder,

I'd like you to reexamine a change.  Please visit

    https://gerrit.osmocom.org/5239

to look at the new patch set (#2).

abisip-find: add getopts and option to prevent endless loop

The utility abisip-find runs into an endless loop and detects the
BTS over and over again. This makes it hard to use the program
from scripts (autodetection and auto-configuration of a single
bts in a lab setup)

Add proper getops options, usage and help

Add option to exit after the first response of from a BTS
(one shot)

Change-Id: I819b9616282dd6efd36c9557c7d6a50b6b00cdc2
---
M src/ipaccess/abisip-find.c
1 file changed, 73 insertions(+), 7 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/osmo-bsc refs/changes/39/5239/2

diff --git a/src/ipaccess/abisip-find.c b/src/ipaccess/abisip-find.c
index c459161..b9d352e 100644
--- a/src/ipaccess/abisip-find.c
+++ b/src/ipaccess/abisip-find.c
@@ -25,12 +25,22 @@
 #include <netinet/in.h>
 #include <arpa/inet.h>
 
+#include <libgen.h>
+#include <getopt.h>
+#include <errno.h>
 
 #include <osmocom/core/select.h>
 #include <osmocom/core/timer.h>
 #include <osmocom/gsm/protocol/ipaccess.h>
 #include <osmocom/gsm/ipa.h>
 #include <osmocom/bsc/gsm_data.h>
+
+char program_name[255];
+
+struct bfd_data {
+	bool oneshot;
+	bool exit;
+};
 
 static int udp_sock(const char *ifname)
 {
@@ -140,18 +150,25 @@
 
 	/* 2 bytes length, 1 byte protocol */
 	if (buf[2] != IPAC_PROTO_IPACCESS)
-		return 0;
+		return -EINVAL;
 
 	if (buf[4] != IPAC_MSGT_ID_RESP)
-		return 0;
+		return -EINVAL;
 
 	return parse_response(buf+6, len-6);
 }
 
 static int bfd_cb(struct osmo_fd *bfd, unsigned int flags)
 {
-	if (flags & BSC_FD_READ)
-		return read_response(bfd->fd);
+	struct bfd_data *bfd_data = bfd->data;
+	int rc;
+
+	if (flags & BSC_FD_READ) {
+		rc = read_response(bfd->fd);
+		if (bfd_data->oneshot && rc == 0)
+			bfd_data->exit = true;
+		return rc;
+	}
 	if (flags & BSC_FD_WRITE) {
 		bfd->when &= ~BSC_FD_WRITE;
 		return bcast_find(bfd->fd);
@@ -170,25 +187,72 @@
 	osmo_timer_schedule(&timer, 5, 0);
 }
 
+static void print_usage()
+{
+	printf("Usage: %s\n", program_name);
+}
+
+static void print_help()
+{
+	printf("  Some useful help...\n");
+	printf("  -h --help this text\n");
+	printf("  -i --ifname. Specify outgoing interface\n");
+	printf("  -1 --one-shot. no endless loop, only one turn \n");
+}
+
 int main(int argc, char **argv)
 {
 	struct osmo_fd bfd;
 	char *ifname = NULL;
 	int rc;
+	struct bfd_data bfd_data;
+
+	strncpy(program_name, basename(argv[0]), sizeof(program_name));
+	memset(&bfd_data, 0, sizeof(bfd_data));
 
 	printf("abisip-find (C) 2009 by Harald Welte\n");
 	printf("This is FREE SOFTWARE with ABSOLUTELY NO WARRANTY\n\n");
 
-	if (argc < 2) {
+	while (1) {
+		int option_index = 0, c;
+		static struct option long_options[] = {
+			{"help", 0, 0, 'h'},
+			{"ifname", 1, 0, 'i'},
+			{"one-shot", 0, 0, '1'},
+			{0, 0, 0, 0}
+		};
+
+		c = getopt_long(argc, argv, "hi:1",
+				long_options, &option_index);
+		if (c == -1)
+			break;
+
+		switch (c) {
+		case 'h':
+			print_usage();
+			print_help();
+			exit(0);
+		case 'i':
+			ifname = optarg;
+			break;
+		case '1':
+			bfd_data.oneshot = true;
+			break;
+		default:
+			/* ignore */
+			break;
+		}
+	}
+
+	if (ifname == NULL) {
 		fprintf(stdout, "you might need to specify the outgoing\n"
 			" network interface, e.g. ``%s eth0''\n", argv[0]);
-	} else {
-		ifname = argv[1];
 	}
 
 	bfd.cb = bfd_cb;
 	bfd.when = BSC_FD_READ | BSC_FD_WRITE;
 	bfd.fd = udp_sock(ifname);
+	bfd.data = &bfd_data;
 	if (bfd.fd < 0) {
 		perror("Cannot create local socket for broadcast udp");
 		exit(1);
@@ -209,6 +273,8 @@
 		rc = osmo_select_main(0);
 		if (rc < 0)
 			exit(3);
+		if (bfd_data.exit)
+			break;
 	}
 
 	exit(0);

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

Gerrit-MessageType: newpatchset
Gerrit-Change-Id: I819b9616282dd6efd36c9557c7d6a50b6b00cdc2
Gerrit-PatchSet: 2
Gerrit-Project: osmo-bsc
Gerrit-Branch: master
Gerrit-Owner: dexter <pmaier at sysmocom.de>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: Vadim Yanitskiy <axilirator at gmail.com>



More information about the gerrit-log mailing list