Skip to content

Commit

Permalink
net: Convert various address loops to use ARRAY_FOR_EACH macro
Browse files Browse the repository at this point in the history
Convert various networking subsystem files to use ARRAY_FOR_EACH
macro to make the looping more robust.

Signed-off-by: Jukka Rissanen <[email protected]>
  • Loading branch information
jukkar committed Feb 5, 2024
1 parent 2a8899d commit c2f238c
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 24 deletions.
3 changes: 1 addition & 2 deletions subsys/net/l2/ethernet/arp.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,13 +220,12 @@ static inline struct in_addr *if_get_addr(struct net_if *iface,
struct in_addr *addr)
{
struct net_if_ipv4 *ipv4 = iface->config.ip.ipv4;
int i;

if (!ipv4) {
return NULL;
}

for (i = 0; i < NET_IF_MAX_IPV4_ADDR; i++) {
ARRAY_FOR_EACH(ipv4->unicast, i) {
if (ipv4->unicast[i].ipv4.is_used &&
ipv4->unicast[i].ipv4.address.family == AF_INET &&
ipv4->unicast[i].ipv4.addr_state == NET_ADDR_PREFERRED &&
Expand Down
3 changes: 1 addition & 2 deletions subsys/net/lib/config/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,12 @@ static void ipv4_addr_add_handler(struct net_mgmt_event_callback *cb,
#if CONFIG_NET_CONFIG_LOG_LEVEL >= LOG_LEVEL_INF
char hr_addr[NET_IPV4_ADDR_LEN];
#endif
int i;

if (mgmt_event != NET_EVENT_IPV4_ADDR_ADD) {
return;
}

for (i = 0; i < NET_IF_MAX_IPV4_ADDR; i++) {
ARRAY_FOR_EACH(iface->config.ip.ipv4->unicast, i) {
struct net_if_addr *if_addr =
&iface->config.ip.ipv4->unicast[i].ipv4;

Expand Down
29 changes: 13 additions & 16 deletions subsys/net/lib/shell/iface.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@ static void print_supported_ethernet_capabilities(
const struct shell *sh, struct net_if *iface)
{
enum ethernet_hw_caps caps = net_eth_get_hw_capabilities(iface);
int i;

for (i = 0; i < ARRAY_SIZE(eth_hw_caps); i++) {
ARRAY_FOR_EACH(eth_hw_caps, i) {
if (caps & eth_hw_caps[i].capability) {
PR("\t%s\n", eth_hw_caps[i].description);
}
Expand Down Expand Up @@ -150,7 +149,7 @@ static void iface_cb(struct net_if *iface, void *user_data)
#endif
const char *extra;
#if defined(CONFIG_NET_IP)
int i, count;
int count;
#endif

if (data->user_data && data->user_data != iface) {
Expand Down Expand Up @@ -257,7 +256,7 @@ static void iface_cb(struct net_if *iface, void *user_data)
if (!ret && params.priority_queues_num) {
count = params.priority_queues_num;
PR("Priority queues:\n");
for (i = 0; i < count; ++i) {
for (int i = 0; i < count; ++i) {
params.qav_param.queue_id = i;
params.qav_param.type =
ETHERNET_QAV_PARAM_TYPE_STATUS;
Expand Down Expand Up @@ -290,7 +289,7 @@ static void iface_cb(struct net_if *iface, void *user_data)
eth_ctx = net_if_l2_data(iface);

if (eth_ctx->vlan_enabled) {
for (i = 0; i < CONFIG_NET_VLAN_COUNT; i++) {
for (int i = 0; i < CONFIG_NET_VLAN_COUNT; i++) {
if (eth_ctx->vlan[i].iface != iface ||
eth_ctx->vlan[i].tag ==
NET_VLAN_TAG_UNSPEC) {
Expand All @@ -316,17 +315,16 @@ static void iface_cb(struct net_if *iface, void *user_data)

#if defined(CONFIG_NET_IPV6)
count = 0;
ipv6 = iface->config.ip.ipv6;

if (!net_if_flag_is_set(iface, NET_IF_IPV6)) {
if (!net_if_flag_is_set(iface, NET_IF_IPV6) || ipv6 == NULL) {
PR("%s not %s for this interface.\n", "IPv6", "enabled");
ipv6 = NULL;
goto skip_ipv6;
}

ipv6 = iface->config.ip.ipv6;

PR("IPv6 unicast addresses (max %d):\n", NET_IF_MAX_IPV6_ADDR);
for (i = 0; ipv6 && i < NET_IF_MAX_IPV6_ADDR; i++) {
ARRAY_FOR_EACH(ipv6->unicast, i) {
unicast = &ipv6->unicast[i];

if (!unicast->is_used) {
Expand All @@ -349,7 +347,7 @@ static void iface_cb(struct net_if *iface, void *user_data)
count = 0;

PR("IPv6 multicast addresses (max %d):\n", NET_IF_MAX_IPV6_MADDR);
for (i = 0; ipv6 && i < NET_IF_MAX_IPV6_MADDR; i++) {
ARRAY_FOR_EACH(ipv6->mcast, i) {
mcast = &ipv6->mcast[i];

if (!mcast->is_used) {
Expand All @@ -368,7 +366,7 @@ static void iface_cb(struct net_if *iface, void *user_data)
count = 0;

PR("IPv6 prefixes (max %d):\n", NET_IF_MAX_IPV6_PREFIX);
for (i = 0; ipv6 && i < NET_IF_MAX_IPV6_PREFIX; i++) {
ARRAY_FOR_EACH(ipv6->prefix, i) {
prefix = &ipv6->prefix[i];

if (!prefix->is_used) {
Expand Down Expand Up @@ -426,17 +424,16 @@ static void iface_cb(struct net_if *iface, void *user_data)
}

count = 0;
ipv4 = iface->config.ip.ipv4;

if (!net_if_flag_is_set(iface, NET_IF_IPV4)) {
if (!net_if_flag_is_set(iface, NET_IF_IPV4) || ipv4 == NULL) {
PR("%s not %s for this interface.\n", "IPv4", "enabled");
ipv4 = NULL;
goto skip_ipv4;
}

ipv4 = iface->config.ip.ipv4;

PR("IPv4 unicast addresses (max %d):\n", NET_IF_MAX_IPV4_ADDR);
for (i = 0; ipv4 && i < NET_IF_MAX_IPV4_ADDR; i++) {
ARRAY_FOR_EACH(ipv4->unicast, i) {
unicast = &ipv4->unicast[i].ipv4;

if (!unicast->is_used) {
Expand All @@ -461,7 +458,7 @@ static void iface_cb(struct net_if *iface, void *user_data)
count = 0;

PR("IPv4 multicast addresses (max %d):\n", NET_IF_MAX_IPV4_MADDR);
for (i = 0; ipv4 && i < NET_IF_MAX_IPV4_MADDR; i++) {
ARRAY_FOR_EACH(ipv4->mcast, i) {
mcast = &ipv4->mcast[i];

if (!mcast->is_used) {
Expand Down
3 changes: 1 addition & 2 deletions subsys/net/lib/shell/ipv4.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ static void ip_address_lifetime_cb(struct net_if *iface, void *user_data)
const struct shell *sh = data->sh;
struct net_if_ipv4 *ipv4 = iface->config.ip.ipv4;
const char *extra;
int i;

ARG_UNUSED(user_data);

Expand All @@ -35,7 +34,7 @@ static void ip_address_lifetime_cb(struct net_if *iface, void *user_data)

PR("Type \tState \tLifetime (sec)\tAddress\n");

for (i = 0; i < NET_IF_MAX_IPV4_ADDR; i++) {
ARRAY_FOR_EACH(ipv4->unicast, i) {
if (!ipv4->unicast[i].ipv4.is_used ||
ipv4->unicast[i].ipv4.address.family != AF_INET) {
continue;
Expand Down
3 changes: 1 addition & 2 deletions subsys/net/lib/shell/ipv6.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ static void address_lifetime_cb(struct net_if *iface, void *user_data)
const struct shell *sh = data->sh;
struct net_if_ipv6 *ipv6 = iface->config.ip.ipv6;
const char *extra;
int i;

ARG_UNUSED(user_data);

Expand All @@ -77,7 +76,7 @@ static void address_lifetime_cb(struct net_if *iface, void *user_data)

PR("Type \tState \tLifetime (sec)\tAddress\n");

for (i = 0; i < NET_IF_MAX_IPV6_ADDR; i++) {
ARRAY_FOR_EACH(ipv6->unicast, i) {
struct net_if_ipv6_prefix *prefix;
char remaining_str[sizeof("01234567890")];
uint64_t remaining;
Expand Down

0 comments on commit c2f238c

Please sign in to comment.