Skip to content

Commit d485ef0

Browse files
luca-fancellucarlescufi
authored andcommitted
net: zperf: Reduce the scope of input address variables
Reduce the scope of in4_addr_my and in6_addr_my pointer variables that are currently global, but they are used only inside tcp_receiver_thread. Take the occasion to fix a typo in one error message. Signed-off-by: Luca Fancellu <[email protected]>
1 parent e051c2a commit d485ef0

File tree

1 file changed

+22
-27
lines changed

1 file changed

+22
-27
lines changed

subsys/net/lib/zperf/zperf_tcp_receiver.c

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ LOG_MODULE_DECLARE(net_zperf, CONFIG_NET_ZPERF_LOG_LEVEL);
2323
#define NET_LOG_ENABLED 1
2424
#include "net_private.h"
2525

26-
static struct sockaddr_in6 *in6_addr_my;
27-
static struct sockaddr_in *in4_addr_my;
28-
2926
#if defined(CONFIG_NET_TC_THREAD_COOPERATIVE)
3027
#define TCP_RECEIVER_THREAD_PRIORITY K_PRIO_COOP(8)
3128
#else
@@ -114,9 +111,7 @@ static void tcp_server_session(void)
114111
}
115112

116113
if (IS_ENABLED(CONFIG_NET_IPV4)) {
117-
const struct in_addr *in4_addr = NULL;
118-
119-
in4_addr_my = zperf_get_sin();
114+
struct sockaddr_in *in4_addr = zperf_get_sin();
120115

121116
fds[SOCK_ID_IPV4_LISTEN].fd = zsock_socket(AF_INET, SOCK_STREAM,
122117
IPPROTO_TCP);
@@ -128,34 +123,35 @@ static void tcp_server_session(void)
128123
if (MY_IP4ADDR && strlen(MY_IP4ADDR)) {
129124
/* Use Setting IP */
130125
ret = zperf_get_ipv4_addr(MY_IP4ADDR,
131-
&in4_addr_my->sin_addr);
126+
&in4_addr->sin_addr);
132127
if (ret < 0) {
133128
NET_WARN("Unable to set IPv4");
134129
goto use_existing_ipv4;
135130
}
136131
} else {
137132
use_existing_ipv4:
138133
/* Use existing IP */
139-
in4_addr = zperf_get_default_if_in4_addr();
140-
if (!in4_addr) {
141-
NET_ERR("Unable to get IPv4 by default");
134+
const struct in_addr *addr =
135+
zperf_get_default_if_in4_addr();
136+
if (!addr) {
137+
NET_ERR("Unable to get IPv4 by default\n");
142138
goto error;
143139
}
144-
memcpy(&in4_addr_my->sin_addr, in4_addr,
140+
memcpy(&in4_addr->sin_addr, addr,
145141
sizeof(struct in_addr));
146142
}
147143

148-
in4_addr_my->sin_port = htons(tcp_server_port);
144+
in4_addr->sin_port = htons(tcp_server_port);
149145

150146
NET_INFO("Binding to %s",
151-
net_sprint_ipv4_addr(&in4_addr_my->sin_addr));
147+
net_sprint_ipv4_addr(&in4_addr->sin_addr));
152148

153149
ret = zsock_bind(fds[SOCK_ID_IPV4_LISTEN].fd,
154-
(struct sockaddr *)in4_addr_my,
150+
(struct sockaddr *)in4_addr,
155151
sizeof(struct sockaddr_in));
156152
if (ret < 0) {
157153
NET_ERR("Cannot bind IPv4 TCP port %d (%d)",
158-
ntohs(in4_addr_my->sin_port), errno);
154+
ntohs(in4_addr->sin_port), errno);
159155
goto error;
160156
}
161157

@@ -169,9 +165,7 @@ static void tcp_server_session(void)
169165
}
170166

171167
if (IS_ENABLED(CONFIG_NET_IPV6)) {
172-
const struct in6_addr *in6_addr = NULL;
173-
174-
in6_addr_my = zperf_get_sin6();
168+
struct sockaddr_in6 *in6_addr = zperf_get_sin6();
175169

176170
fds[SOCK_ID_IPV6_LISTEN].fd = zsock_socket(AF_INET6, SOCK_STREAM,
177171
IPPROTO_TCP);
@@ -184,34 +178,35 @@ static void tcp_server_session(void)
184178
/* Use Setting IP */
185179
ret = zperf_get_ipv6_addr(MY_IP6ADDR,
186180
MY_PREFIX_LEN_STR,
187-
&in6_addr_my->sin6_addr);
181+
&in6_addr->sin6_addr);
188182
if (ret < 0) {
189183
NET_WARN("Unable to set IPv6");
190184
goto use_existing_ipv6;
191185
}
192186
} else {
193187
use_existing_ipv6:
194188
/* Use existing IP */
195-
in6_addr = zperf_get_default_if_in6_addr();
196-
if (!in6_addr) {
197-
NET_ERR("Unable to get IPv4 by default");
189+
const struct in6_addr *addr =
190+
zperf_get_default_if_in6_addr();
191+
if (!addr) {
192+
NET_ERR("Unable to get IPv6 by default\n");
198193
goto error;
199194
}
200-
memcpy(&in6_addr_my->sin6_addr, in6_addr,
195+
memcpy(&in6_addr->sin6_addr, addr,
201196
sizeof(struct in6_addr));
202197
}
203198

204-
in6_addr_my->sin6_port = htons(tcp_server_port);
199+
in6_addr->sin6_port = htons(tcp_server_port);
205200

206201
NET_INFO("Binding to %s",
207-
net_sprint_ipv6_addr(&in6_addr_my->sin6_addr));
202+
net_sprint_ipv6_addr(&in6_addr->sin6_addr));
208203

209204
ret = zsock_bind(fds[SOCK_ID_IPV6_LISTEN].fd,
210-
(struct sockaddr *)in6_addr_my,
205+
(struct sockaddr *)in6_addr,
211206
sizeof(struct sockaddr_in6));
212207
if (ret < 0) {
213208
NET_ERR("Cannot bind IPv6 TCP port %d (%d)",
214-
ntohs(in6_addr_my->sin6_port), errno);
209+
ntohs(in6_addr->sin6_port), errno);
215210
goto error;
216211
}
217212

0 commit comments

Comments
 (0)