Change in osmo-mgw[master]: embed strings into structs

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

laforge gerrit-no-reply at lists.osmocom.org
Mon Sep 13 19:05:53 UTC 2021


laforge has submitted this change. ( https://gerrit.osmocom.org/c/osmo-mgw/+/25426 )

Change subject: embed strings into structs
......................................................................

embed strings into structs

They are mostly not even as large as the talloc header used to
dynamically allocate them, and they are also not "shared" by anything.

Change-Id: I7b46d531c5d3b53984f2ce44538116973f6a074d
---
M include/osmocom/mgcp/mgcp.h
M src/libosmo-mgcp/mgcp_network.c
M src/libosmo-mgcp/mgcp_protocol.c
M src/libosmo-mgcp/mgcp_vty.c
M src/osmo-mgw/mgw_main.c
5 files changed, 26 insertions(+), 30 deletions(-)

Approvals:
  laforge: Looks good to me, approved
  dexter: Looks good to me, but someone else must approve
  Jenkins Builder: Verified



diff --git a/include/osmocom/mgcp/mgcp.h b/include/osmocom/mgcp/mgcp.h
index 228b0b3..f1e6460 100644
--- a/include/osmocom/mgcp/mgcp.h
+++ b/include/osmocom/mgcp/mgcp.h
@@ -95,8 +95,8 @@
 struct mgcp_port_range {
 	pthread_mutex_t lock;
 	/* addr or NULL to fall-back to default */
-	char *bind_addr_v4;
-	char *bind_addr_v6;
+	char bind_addr_v4[INET6_ADDRSTRLEN];
+	char bind_addr_v6[INET6_ADDRSTRLEN];
 
 	/* dynamically allocated */
 	int range_start;
@@ -129,9 +129,9 @@
 
 struct mgcp_config {
 	int source_port;
-	char *local_ip;
-	char *source_addr;
-	char *call_agent_addr;
+	char local_ip[INET6_ADDRSTRLEN];
+	char source_addr[INET6_ADDRSTRLEN];
+	char call_agent_addr[INET6_ADDRSTRLEN];
 
 	/* RTP processing */
 	mgcp_processing rtp_processing_cb;
@@ -151,8 +151,6 @@
 	mgcp_rqnt rqnt_cb;
 	void *data;
 
-	uint32_t last_call_id;
-
 	/* list holding the trunks */
 	struct llist_head trunks;
 
@@ -161,7 +159,7 @@
 	/* osmux translator: 0 means disabled, 1 means enabled */
 	int osmux;
 	/* addr to bind the server to */
-	char *osmux_addr;
+	char osmux_addr[INET6_ADDRSTRLEN];
 	/* The BSC-NAT may ask for enabling osmux on demand. This tells us if
 	 * the osmux socket is already initialized.
 	 */
diff --git a/src/libosmo-mgcp/mgcp_network.c b/src/libosmo-mgcp/mgcp_network.c
index 86b0d06..5249fef 100644
--- a/src/libosmo-mgcp/mgcp_network.c
+++ b/src/libosmo-mgcp/mgcp_network.c
@@ -135,10 +135,10 @@
 	} else {
 		/* Choose any of the bind addresses, preferring v6 over v4 */
 		bind_addr = endp->cfg->net_ports.bind_addr_v6;
-		if (!bind_addr)
+		if (!strlen(bind_addr))
 			bind_addr = endp->cfg->net_ports.bind_addr_v4;
 	}
-	if (bind_addr) {
+	if (strlen(bind_addr)) {
 		LOGPCONN(conn->conn, DRTP, LOGL_DEBUG,
 			 "using configured rtp bind ip as local bind ip %s\n",
 			 bind_addr);
diff --git a/src/libosmo-mgcp/mgcp_protocol.c b/src/libosmo-mgcp/mgcp_protocol.c
index 16b7dab..736b071 100644
--- a/src/libosmo-mgcp/mgcp_protocol.c
+++ b/src/libosmo-mgcp/mgcp_protocol.c
@@ -273,7 +273,7 @@
 	 * us for OSMUX connections. Perhaps adding a new internal API to get it
 	 * based on conn type.
 	 */
-	const char *addr = endp->cfg->local_ip ? : conn->end.local_addr;
+	const char *addr = strlen(endp->cfg->local_ip) ? endp->cfg->local_ip : conn->end.local_addr;
 	struct msgb *sdp;
 	int rc;
 	struct msgb *result;
@@ -1615,8 +1615,8 @@
 	cfg->net_ports.last_port = cfg->net_ports.range_start;
 
 	cfg->source_port = 2427;
-	cfg->source_addr = talloc_strdup(cfg, "0.0.0.0");
-	cfg->osmux_addr = talloc_strdup(cfg, "0.0.0.0");
+	osmo_strlcpy(cfg->source_addr, "0.0.0.0", sizeof(cfg->source_addr));
+	osmo_strlcpy(cfg->osmux_addr, "0.0.0.0", sizeof(cfg->osmux_addr));
 
 	cfg->rtp_processing_cb = &mgcp_rtp_processing_default;
 	cfg->setup_rtp_processing_cb = &mgcp_setup_rtp_processing_default;
diff --git a/src/libosmo-mgcp/mgcp_vty.c b/src/libosmo-mgcp/mgcp_vty.c
index 6bc09d0..738bfcc 100644
--- a/src/libosmo-mgcp/mgcp_vty.c
+++ b/src/libosmo-mgcp/mgcp_vty.c
@@ -65,17 +65,17 @@
 
 	vty_out(vty, "mgcp%s", VTY_NEWLINE);
 	vty_out(vty, " domain %s%s", g_cfg->domain, VTY_NEWLINE);
-	if (g_cfg->local_ip)
+	if (strlen(g_cfg->local_ip))
 		vty_out(vty, "  local ip %s%s", g_cfg->local_ip, VTY_NEWLINE);
 	vty_out(vty, " bind ip %s%s", g_cfg->source_addr, VTY_NEWLINE);
 	vty_out(vty, " bind port %u%s", g_cfg->source_port, VTY_NEWLINE);
 	vty_out(vty, " rtp port-range %u %u%s",
 		g_cfg->net_ports.range_start, g_cfg->net_ports.range_end,
 		VTY_NEWLINE);
-	if (g_cfg->net_ports.bind_addr_v4)
+	if (strlen(g_cfg->net_ports.bind_addr_v4))
 		vty_out(vty, " rtp bind-ip %s%s",
 			g_cfg->net_ports.bind_addr_v4, VTY_NEWLINE);
-	if (g_cfg->net_ports.bind_addr_v6)
+	if (strlen(g_cfg->net_ports.bind_addr_v6))
 		vty_out(vty, " rtp bind-ip-v6 %s%s",
 			g_cfg->net_ports.bind_addr_v6, VTY_NEWLINE);
 	if (g_cfg->net_ports.bind_addr_probe)
@@ -122,7 +122,7 @@
 		trunk->v.vty_number_endpoints, VTY_NEWLINE);
 	vty_out(vty, " %sallow-transcoding%s",
 		trunk->no_audio_transcoding ? "no " : "", VTY_NEWLINE);
-	if (g_cfg->call_agent_addr)
+	if (strlen(g_cfg->call_agent_addr))
 		vty_out(vty, " call-agent ip %s%s", g_cfg->call_agent_addr,
 			VTY_NEWLINE);
 	if (g_cfg->force_ptime > 0)
@@ -443,7 +443,7 @@
 	      "IPv4 Address to use in SDP record\n"
 	      "IPv6 Address to use in SDP record\n")
 {
-	osmo_talloc_replace_string(g_cfg, &g_cfg->local_ip, argv[0]);
+	osmo_strlcpy(g_cfg->local_ip, argv[0], sizeof(g_cfg->local_ip));
 	return CMD_SUCCESS;
 }
 
@@ -455,7 +455,7 @@
       "IPv4 Address to bind to\n"
       "IPv6 Address to bind to\n")
 {
-	osmo_talloc_replace_string(g_cfg, &g_cfg->source_addr, argv[0]);
+	osmo_strlcpy(g_cfg->source_addr, argv[0], sizeof(g_cfg->source_addr));
 	return CMD_SUCCESS;
 }
 
@@ -533,7 +533,7 @@
 	      RTP_STR "Bind endpoints facing the Network\n"
 	      "IPv4 Address to bind to\n")
 {
-	osmo_talloc_replace_string(g_cfg, &g_cfg->net_ports.bind_addr_v4, argv[0]);
+	osmo_strlcpy(g_cfg->net_ports.bind_addr_v4, argv[0], sizeof(g_cfg->net_ports.bind_addr_v4));
 	return CMD_SUCCESS;
 }
 ALIAS_DEPRECATED(cfg_mgcp_rtp_bind_ip,
@@ -548,8 +548,7 @@
 	      NO_STR RTP_STR "Bind endpoints facing the Network\n"
 	      "Address to bind to\n")
 {
-	talloc_free(g_cfg->net_ports.bind_addr_v4);
-	g_cfg->net_ports.bind_addr_v4 = NULL;
+	osmo_strlcpy(g_cfg->net_ports.bind_addr_v4, "", sizeof(g_cfg->net_ports.bind_addr_v4));
 	return CMD_SUCCESS;
 }
 ALIAS_DEPRECATED(cfg_mgcp_rtp_no_bind_ip,
@@ -565,7 +564,7 @@
 	      RTP_STR "Bind endpoints facing the Network\n"
 	      "IPv6 Address to bind to\n")
 {
-	osmo_talloc_replace_string(g_cfg, &g_cfg->net_ports.bind_addr_v6, argv[0]);
+	osmo_strlcpy(g_cfg->net_ports.bind_addr_v6, argv[0], sizeof(g_cfg->net_ports.bind_addr_v6));
 	return CMD_SUCCESS;
 }
 
@@ -576,8 +575,7 @@
 	      NO_STR RTP_STR "Bind endpoints facing the Network\n"
 	      "Address to bind to\n")
 {
-	talloc_free(g_cfg->net_ports.bind_addr_v6);
-	g_cfg->net_ports.bind_addr_v6 = NULL;
+	osmo_strlcpy(g_cfg->net_ports.bind_addr_v6, "", sizeof(g_cfg->net_ports.bind_addr_v6));
 	return CMD_SUCCESS;
 }
 
@@ -950,7 +948,7 @@
       "IPv4 Address of the call agent\n"
       "IPv6 Address of the call agent\n")
 {
-	osmo_talloc_replace_string(g_cfg, &g_cfg->call_agent_addr, argv[0]);
+	osmo_strlcpy(g_cfg->call_agent_addr, argv[0], sizeof(g_cfg->call_agent_addr));
 	return CMD_SUCCESS;
 }
 
@@ -1559,7 +1557,7 @@
       "IPv4 Address to bind to\n"
       "IPv6 Address to bind to\n")
 {
-	osmo_talloc_replace_string(g_cfg, &g_cfg->osmux_addr, argv[0]);
+	osmo_strlcpy(g_cfg->osmux_addr, argv[0], sizeof(g_cfg->osmux_addr));
 	return CMD_SUCCESS;
 }
 
@@ -1748,7 +1746,7 @@
 		return rc;
 	}
 
-	if (!g_cfg->source_addr) {
+	if (!strlen(g_cfg->source_addr)) {
 		fprintf(stderr, "You need to specify a bind address.\n");
 		return -1;
 	}
diff --git a/src/osmo-mgw/mgw_main.c b/src/osmo-mgw/mgw_main.c
index 52a1622..d12011c 100644
--- a/src/osmo-mgw/mgw_main.c
+++ b/src/osmo-mgw/mgw_main.c
@@ -369,12 +369,12 @@
 
 	/* we need to bind a socket */
 	flags = OSMO_SOCK_F_BIND;
-	if (cfg->call_agent_addr)
+	if (strlen(cfg->call_agent_addr))
 		flags |= OSMO_SOCK_F_CONNECT;
 
 	rc = osmo_sock_init2_ofd(&cfg->gw_fd.bfd, AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP,
 				cfg->source_addr, cfg->source_port,
-				cfg->call_agent_addr, cfg->call_agent_addr ? 2727 : 0, flags);
+				cfg->call_agent_addr, strlen(cfg->call_agent_addr) ? 2727 : 0, flags);
 	if (rc < 0) {
 		perror("Gateway failed to bind");
 		return -1;

-- 
To view, visit https://gerrit.osmocom.org/c/osmo-mgw/+/25426
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: osmo-mgw
Gerrit-Branch: master
Gerrit-Change-Id: I7b46d531c5d3b53984f2ce44538116973f6a074d
Gerrit-Change-Number: 25426
Gerrit-PatchSet: 12
Gerrit-Owner: Hoernchen <ewild at sysmocom.de>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: dexter <pmaier at sysmocom.de>
Gerrit-Reviewer: laforge <laforge at osmocom.org>
Gerrit-CC: pespin <pespin at sysmocom.de>
Gerrit-MessageType: merged
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20210913/af7156c9/attachment.htm>


More information about the gerrit-log mailing list