[riot-devel] Easy ping

Martine Lenders mail at martine-lenders.eu
Fri Sep 23 20:37:16 CEST 2016


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.riot-os.org/pipermail/devel/attachments/20160923/f6ce7914/attachment.html>


More information about the devel mailing list