Skip to content

Ping command for Portenta C33 #424

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 3, 2025
Merged

Conversation

fabik111
Copy link
Contributor

Add the Ping command for the network interfaces (WiFi and Ethernet) of Portenta C33

@per1234 per1234 added type: enhancement Proposed improvement topic: code Related to content of the project itself labels Jan 10, 2025
ICMPH_TYPE_SET(iecho, ICMP_ECHO);
ICMPH_CODE_SET(iecho, 0);
iecho->chksum = 0;
iecho->id = 0xAFAF;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to avoid strange cases of stray icmp packets I would make this not fixed, either an incremental id(faster) or random(better), and keep it in the context recv_callback_data, for it to match correctly

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't the random sequence number requestCbkData.seqNum enough?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quoting rfc792, I see that there is not a preferred method. Using both we lower the chances of possible collisions. I don't see any reason to enforce this change.

For future implementations I would even consider matching against the source and destination IP.

Identifier

If code = 0, an identifier to aid in matching echos and replies,
may be zero.

Sequence Number

If code = 0, a sequence number to aid in matching echos and
replies, may be zero.


struct pbuf *p;
struct icmp_echo_hdr *iecho;
size_t ping_size = sizeof(struct icmp_echo_hdr) + 32;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add a comment explaining why we need the icmp packet needs to be at least 64 bytes in size

Comment on lines 30 to 40
if ((p->tot_len >= (PBUF_IP_HLEN + sizeof(struct icmp_echo_hdr))) &&
pbuf_remove_header(p, PBUF_IP_HLEN) == 0) {
iecho = (struct icmp_echo_hdr *)p->payload;

if ((iecho->id == 0xAFAF) && (iecho->seqno == lwip_htons(request->seqNum))) {
/* do some ping result processing */
request->endMillis = millis();
pbuf_free(p);
return 1; /* eat the packet */
}
/* not eaten, restore original packet */
pbuf_add_header(p, PBUF_IP_HLEN);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rewrite the logic of this function, to improve its readability by applying the return early pattern.

Suggested change
if ((p->tot_len >= (PBUF_IP_HLEN + sizeof(struct icmp_echo_hdr))) &&
pbuf_remove_header(p, PBUF_IP_HLEN) == 0) {
iecho = (struct icmp_echo_hdr *)p->payload;
if ((iecho->id == 0xAFAF) && (iecho->seqno == lwip_htons(request->seqNum))) {
/* do some ping result processing */
request->endMillis = millis();
pbuf_free(p);
return 1; /* eat the packet */
}
/* not eaten, restore original packet */
pbuf_add_header(p, PBUF_IP_HLEN);
}
if (p->tot_len < (PBUF_IP_HLEN + sizeof(struct icmp_echo_hdr)) ||
pbuf_remove_header(p, PBUF_IP_HLEN) != 0) {
return 0; /* do not consume the packet */
}
iecho = (struct icmp_echo_hdr *)p->payload;
if (iecho->id != request->id || iecho->seqno != lwip_htons(request->seqNum)) {
/* not consumed, restore original packet */
pbuf_add_header(p, PBUF_IP_HLEN);
return 0;
}
/* do some ping result processing */
request->endMillis = millis();
pbuf_free(p);
return 1; /* consume the packet */

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would also make sense to check the ip header for the protocol field to match the value 1 reserved for ICMP packets, instead of checking for the size of the pbuf.
Reference rfc790 and Iana IP Protocol Numbers

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactorized

@fabik111 fabik111 marked this pull request as ready for review February 27, 2025 15:46
@pennam pennam force-pushed the add-ping-command branch 2 times, most recently from d81171e to 55ea2d0 Compare March 3, 2025 09:40
  This ensure access to existing data if icmp callback is fired after ping timeout
@pennam pennam force-pushed the add-ping-command branch from 55ea2d0 to 8094f71 Compare March 3, 2025 09:55
@pennam pennam merged commit b83b15c into arduino:main Mar 3, 2025
6 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: code Related to content of the project itself type: enhancement Proposed improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants