[PATCH 1/8] libcommon: soak up three static functions.

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/OpenBSC@lists.osmocom.org/.

Neels Hofmeyr nhofmeyr at sysmocom.de
Thu Oct 1 13:23:50 UTC 2015


Add new kitchen sink openbsc/utils.h and libcommon/utils.c to make three so far
static functions public (so I can use them in the upcoming OAP code).

A place to put them could have been the gprs_utils.h, but all general functions
in there have a gprs_ prefix, and todo markings to move them away. All other
libcommon headers are too specific, so I opened up this kitchen sink header.

Apply the change in Makefiles and C files.
---
 openbsc/include/openbsc/Makefile.am   |  2 +-
 openbsc/include/openbsc/utils.h       | 17 ++++++++++
 openbsc/src/gprs/gprs_gsup_messages.c | 30 +---------------
 openbsc/src/libcommon/Makefile.am     |  2 +-
 openbsc/src/libcommon/utils.c         | 64 +++++++++++++++++++++++++++++++++++
 openbsc/src/osmo-bsc_nat/bsc_nat.c    | 12 +------
 openbsc/tests/gprs/Makefile.am        |  3 +-
 7 files changed, 87 insertions(+), 43 deletions(-)
 create mode 100644 openbsc/include/openbsc/utils.h
 create mode 100644 openbsc/src/libcommon/utils.c

diff --git a/openbsc/include/openbsc/Makefile.am b/openbsc/include/openbsc/Makefile.am
index 254f43d..828f5bd 100644
--- a/openbsc/include/openbsc/Makefile.am
+++ b/openbsc/include/openbsc/Makefile.am
@@ -14,7 +14,7 @@ noinst_HEADERS = abis_nm.h abis_rsl.h db.h gsm_04_08.h gsm_data.h \
 		osmo_msc_data.h osmo_bsc_grace.h sms_queue.h abis_om2000.h \
 		bss.h gsm_data_shared.h ipaccess.h mncc_int.h \
 		arfcn_range_encode.h nat_rewrite_trie.h bsc_nat_callstats.h \
-		osmux.h mgcp_transcode.h gprs_utils.h \
+		osmux.h mgcp_transcode.h gprs_utils.h utils.h \
 		 gprs_gb_parse.h smpp.h meas_feed.h gprs_gsup_messages.h \
 		 gprs_gsup_client.h bsc_msg_filter.h
 
diff --git a/openbsc/include/openbsc/utils.h b/openbsc/include/openbsc/utils.h
new file mode 100644
index 0000000..739b7a3
--- /dev/null
+++ b/openbsc/include/openbsc/utils.h
@@ -0,0 +1,17 @@
+/* OpenBSC kitchen sink */
+
+#pragma once
+
+#include <stdint.h>
+#include <stdlib.h>
+
+/* Compare count bytes of exp to rel. Return 0 if they are identical, 1
+ * otherwise. Do not return a mismatch on the first mismatching byte,
+ * but always compare all bytes, regardless. The idea is that the amount of
+ * matching bytes cannot be inferred from the time the comparison took.*/
+int constant_time_cmp(const uint8_t *exp, const uint8_t *rel, const int count);
+
+uint64_t decode_big_endian(const uint8_t *data, size_t data_len);
+uint8_t *encode_big_endian(uint64_t value, size_t data_len);
+
+
diff --git a/openbsc/src/gprs/gprs_gsup_messages.c b/openbsc/src/gprs/gprs_gsup_messages.c
index bdcff5f..07485f7 100644
--- a/openbsc/src/gprs/gprs_gsup_messages.c
+++ b/openbsc/src/gprs/gprs_gsup_messages.c
@@ -26,41 +26,13 @@
 
 #include <openbsc/debug.h>
 #include <openbsc/gprs_utils.h>
+#include <openbsc/utils.h>
 
 #include <osmocom/gsm/tlv.h>
 #include <osmocom/core/msgb.h>
 
 #include <stdint.h>
 
-
-static uint64_t decode_big_endian(const uint8_t *data, size_t data_len)
-{
-	uint64_t value = 0;
-
-	while (data_len > 0) {
-		value = (value << 8) + *data;
-		data += 1;
-		data_len -= 1;
-	}
-
-	return value;
-}
-
-static uint8_t *encode_big_endian(uint64_t value, size_t data_len)
-{
-	static uint8_t buf[sizeof(uint64_t)];
-	int idx;
-
-	OSMO_ASSERT(data_len <= ARRAY_SIZE(buf));
-
-	for (idx = data_len - 1; idx >= 0; idx--) {
-		buf[idx] = (uint8_t)value;
-		value = value >> 8;
-	}
-
-	return buf;
-}
-
 static int decode_pdp_info(uint8_t *data, size_t data_len,
 			  struct gprs_gsup_pdp_info *pdp_info)
 {
diff --git a/openbsc/src/libcommon/Makefile.am b/openbsc/src/libcommon/Makefile.am
index 75f40ee..84c7544 100644
--- a/openbsc/src/libcommon/Makefile.am
+++ b/openbsc/src/libcommon/Makefile.am
@@ -6,4 +6,4 @@ noinst_LIBRARIES = libcommon.a
 
 libcommon_a_SOURCES = bsc_version.c common_vty.c debug.c gsm_data.c \
 		      gsm_data_shared.c socket.c talloc_ctx.c \
-		      gsm_subscriber_base.c
+		      gsm_subscriber_base.c utils.c
diff --git a/openbsc/src/libcommon/utils.c b/openbsc/src/libcommon/utils.c
new file mode 100644
index 0000000..615eba9
--- /dev/null
+++ b/openbsc/src/libcommon/utils.c
@@ -0,0 +1,64 @@
+/* OpenBSC kitchen sink */
+
+/* (C) 2015 by sysmocom s.m.f.c <info at sysmocom.de>
+ * All Rights Reserved
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation; either version 3 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 Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include <openbsc/utils.h>
+#include <osmocom/core/utils.h>
+
+/* Wishful thinking to generate a constant time compare */
+int constant_time_cmp(const uint8_t *exp, const uint8_t *rel, const int count)
+{
+	int x = 0, i;
+
+	for (i = 0; i < count; ++i)
+		x |= exp[i] ^ rel[i];
+
+	/* if x is zero, all data was identical */
+	return x? 1 : 0;
+}
+
+
+uint64_t decode_big_endian(const uint8_t *data, size_t data_len)
+{
+	uint64_t value = 0;
+
+	while (data_len > 0) {
+		value = (value << 8) + *data;
+		data += 1;
+		data_len -= 1;
+	}
+
+	return value;
+}
+
+uint8_t *encode_big_endian(uint64_t value, size_t data_len)
+{
+	static uint8_t buf[sizeof(uint64_t)];
+	int idx;
+
+	OSMO_ASSERT(data_len <= ARRAY_SIZE(buf));
+
+	for (idx = data_len - 1; idx >= 0; idx--) {
+		buf[idx] = (uint8_t)value;
+		value = value >> 8;
+	}
+
+	return buf;
+}
+
diff --git a/openbsc/src/osmo-bsc_nat/bsc_nat.c b/openbsc/src/osmo-bsc_nat/bsc_nat.c
index 56c6286..18343dd 100644
--- a/openbsc/src/osmo-bsc_nat/bsc_nat.c
+++ b/openbsc/src/osmo-bsc_nat/bsc_nat.c
@@ -47,6 +47,7 @@
 #include <openbsc/abis_nm.h>
 #include <openbsc/socket.h>
 #include <openbsc/vty.h>
+#include <openbsc/utils.h>
 
 #include <osmocom/ctrl/control_cmd.h>
 #include <osmocom/ctrl/control_if.h>
@@ -987,17 +988,6 @@ static void ipaccess_close_bsc(void *data)
 	bsc_close_connection(conn);
 }
 
-/* Wishful thinking to generate a constant time compare */
-static int constant_time_cmp(const uint8_t *exp, const uint8_t *rel, const int count)
-{
-	int x = 0, i;
-
-	for (i = 0; i < count; ++i)
-		x |= exp[i] ^ rel[i];
-
-	return x != 0;
-}
-
 static int verify_key(struct bsc_connection *conn, struct bsc_config *conf, const uint8_t *key, const int keylen)
 {
 	struct osmo_auth_vector vec;
diff --git a/openbsc/tests/gprs/Makefile.am b/openbsc/tests/gprs/Makefile.am
index 633c362..b57977b 100644
--- a/openbsc/tests/gprs/Makefile.am
+++ b/openbsc/tests/gprs/Makefile.am
@@ -6,6 +6,7 @@ EXTRA_DIST = gprs_test.ok
 noinst_PROGRAMS = gprs_test
 
 gprs_test_SOURCES = gprs_test.c $(top_srcdir)/src/gprs/gprs_utils.c \
-		$(top_srcdir)/src/gprs/gprs_gsup_messages.c
+		$(top_srcdir)/src/gprs/gprs_gsup_messages.c \
+		$(top_srcdir)/src/libcommon/utils.c
 
 gprs_test_LDADD = $(LIBOSMOCORE_LIBS) $(LIBOSMOGSM_LIBS)
-- 
2.1.4




More information about the OpenBSC mailing list