Easy ping


How can I easily ping a board by software (without using shell) so I ping( IPV6_address) and I get -1 for error (not reachable) or 0 for success?


Hi Baptiste, have you had a look at the implementation of the ping shell command? Another way would be to (re-)implement ping using conn_ip (or sock if merged). In that case, if you want it reaaaally easy you don't even have to implement ICMPv6 ping but can do whatever you want ;-).

Cheers, Martine

Yes but it's not as easy as I want (shell command) Tell me the easiest possible function to know if an address is reachable or not :slight_smile:

I assume the device you are trying to reach has some purpose. If so send it a purposeful packet. For example, you are running a web server on the device in question, open a connection to port 80 with your telnet utility and talk to it.

Marc Sissom Krypton Solutions

Yes it would work, but I prefer a simple ping, two gnrc_networking running and one wants to ping the other one, that's all what I want. ICMPV6 is great because I don't need to deal with the request on the second board.

Then setup a task that runs ping.

Marc Sissom Krypton Solutions

Hi Baptiste, that depends on what you mean by reachable. Even for ICMPv6 ping (which is used with the shell ping command) you need some “server”, that handles the request and issues a reply. If that server is not present the node will not reply to pings, even if it is reachable in the sense that you can send IPv6 packets to it and it will handle them (to see what I mean comment out the USEMODULE += gnrc_icmpv6_echo line in gnrc_networking’s Makefile for the receiver, you will be able to still send UDP packets, but ping will always fail).

If there is a server running somewhere you can just use conn (note that timeouts are not possible with conn, if you need them use sock [1] instead [which is also a little easier to handle]):

#include “net/af.h” #include “net/conn/ip.h” /* you need to include gnrc_conn_ip to your app for that */ #include “net/icmpv6.h” #include “net/ipv6.h” #include “net/protnum.h” #include “byteorder.h” #include “xtimer.h”

/* … */

conn_ip_t c; ipv6_addr_t tmp = IPV6_ADDR_UNSPECIFIED; ipv6_addr_t dst = SOME_ADDR; icmpv6_echo_t echo_req, echo_rep; uint16_t seq = 0;

echo_req.type = ICMPV6_ECHO_REQ; echo_req.code = 0; echo_req.id = byteorder_htons(SOME_ID); conn_ip_create(&conn, &tmp, sizeof(tmp), AF_INET6, PROTNUM_ICMPV6 while (true) { int reply = 0; echo_req.seq = byteorder_htons(seq++);> echo_req.csum.u16 = 0; /* will be recalculated by stack / conn_ip_sendto(&echo_req, sizeof(echo_req), NULL, 0, dst, sizeof(dst), AF_INET6, PROTNUM_IPV6_NONXT); while (!reply) { if (conn_ip_recvfrom(&conn, &echo_rep, sizeof(echo_rep), &tmp, sizeof(tmp)) == sizeof(echo_rep)) { / could be any ICMPv6 packet so check type */ if (echo_rep.type == ICMPV6_ECHO_REP) { printf(“Received ping seq %u\n”, (unsigned)ntohs_byteorder(echo_rep.seq)); reply = 1; } } } xtimer_sleep(DELAY); }

Did not test the application, but I hope you get the idea.

Cheers, Martine

[1] https://github.com/RIOT-OS/RIOT/pull/5772/files#diff-0fb2e56aa53bfaac6b1189715662110cR89

Thanks Martine! I do need timeout, otherwise I won't know if the node is connected. Sock is in PR so I prefer not to use it for the moment

Hi Baptiste, dead simple, but maybe not what you want might also be just to call the shell handler of the shell command with the arguments as a string array.

Cheers, Martine

It's what I finally did :slight_smile: Thanks