icmp_echo.rst 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. ICMP 回显
  2. ===========
  3. :link_to_translation:`en:[English]`
  4. 概述
  5. ----
  6. 网际控制报文协议 (ICMP) 通常用于诊断或控制目的,或响应 IP 操作中的错误。常用的网络工具 ``ping`` 的应答,即 ``Echo Reply``,就是基于类型字段值为 0 的 ICMP 数据包实现的。
  7. 在 ping 会话中,首先由源主机发出请求包 (ICMP echo request),然后等待应答包 (ICMP echo reply),等待具有超时限制。通过这一过程,还能测量出信息的往返用时。收到有效的应答包后,源主机会生成 IP 链路层的统计数据(如失包率、运行时间等)。
  8. IoT 设备通常需要检查远程服务器是否可用。如果服务器离线,设备应向用户发出警告。通过创建 ping 会话,定期发送或解析 ICMP echo 数据包,就能实现这一功能。
  9. 为简化这一过程方便用户操作,ESP-IDF 提供了一些好用的 API。
  10. 创建 ping 会话
  11. ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  12. 要创建 ping 会话,首先需填写 ``esp_ping_config_t``,指定目标芯片 IP 地址、间隔时间等配置。此外,还可以通过 ``esp_ping_callbacks_t`` 注册回调函数。
  13. 创建 ping 会话并注册回调函数示例:
  14. .. code-block:: c
  15. static void test_on_ping_success(esp_ping_handle_t hdl, void *args)
  16. {
  17. // optionally, get callback arguments
  18. // const char* str = (const char*) args;
  19. // printf("%s\r\n", str); // "foo"
  20. uint8_t ttl;
  21. uint16_t seqno;
  22. uint32_t elapsed_time, recv_len;
  23. ip_addr_t target_addr;
  24. esp_ping_get_profile(hdl, ESP_PING_PROF_SEQNO, &seqno, sizeof(seqno));
  25. esp_ping_get_profile(hdl, ESP_PING_PROF_TTL, &ttl, sizeof(ttl));
  26. esp_ping_get_profile(hdl, ESP_PING_PROF_IPADDR, &target_addr, sizeof(target_addr));
  27. esp_ping_get_profile(hdl, ESP_PING_PROF_SIZE, &recv_len, sizeof(recv_len));
  28. esp_ping_get_profile(hdl, ESP_PING_PROF_TIMEGAP, &elapsed_time, sizeof(elapsed_time));
  29. printf("%d bytes from %s icmp_seq=%d ttl=%d time=%d ms\n",
  30. recv_len, inet_ntoa(target_addr.u_addr.ip4), seqno, ttl, elapsed_time);
  31. }
  32. static void test_on_ping_timeout(esp_ping_handle_t hdl, void *args)
  33. {
  34. uint16_t seqno;
  35. ip_addr_t target_addr;
  36. esp_ping_get_profile(hdl, ESP_PING_PROF_SEQNO, &seqno, sizeof(seqno));
  37. esp_ping_get_profile(hdl, ESP_PING_PROF_IPADDR, &target_addr, sizeof(target_addr));
  38. printf("From %s icmp_seq=%d timeout\n", inet_ntoa(target_addr.u_addr.ip4), seqno);
  39. }
  40. static void test_on_ping_end(esp_ping_handle_t hdl, void *args)
  41. {
  42. uint32_t transmitted;
  43. uint32_t received;
  44. uint32_t total_time_ms;
  45. esp_ping_get_profile(hdl, ESP_PING_PROF_REQUEST, &transmitted, sizeof(transmitted));
  46. esp_ping_get_profile(hdl, ESP_PING_PROF_REPLY, &received, sizeof(received));
  47. esp_ping_get_profile(hdl, ESP_PING_PROF_DURATION, &total_time_ms, sizeof(total_time_ms));
  48. printf("%d packets transmitted, %d received, time %dms\n", transmitted, received, total_time_ms);
  49. }
  50. void initialize_ping()
  51. {
  52. /* convert URL to IP address */
  53. ip_addr_t target_addr;
  54. struct addrinfo hint;
  55. struct addrinfo *res = NULL;
  56. memset(&hint, 0, sizeof(hint));
  57. memset(&target_addr, 0, sizeof(target_addr));
  58. getaddrinfo("www.espressif.com", NULL, &hint, &res);
  59. struct in_addr addr4 = ((struct sockaddr_in *) (res->ai_addr))->sin_addr;
  60. inet_addr_to_ip4addr(ip_2_ip4(&target_addr), &addr4);
  61. freeaddrinfo(res);
  62. esp_ping_config_t ping_config = ESP_PING_DEFAULT_CONFIG();
  63. ping_config.target_addr = target_addr; // target IP address
  64. ping_config.count = ESP_PING_COUNT_INFINITE; // ping in infinite mode, esp_ping_stop can stop it
  65. /* set callback functions */
  66. esp_ping_callbacks_t cbs;
  67. cbs.on_ping_success = test_on_ping_success;
  68. cbs.on_ping_timeout = test_on_ping_timeout;
  69. cbs.on_ping_end = test_on_ping_end;
  70. cbs.cb_args = "foo"; // arguments that will feed to all callback functions, can be NULL
  71. cbs.cb_args = eth_event_group;
  72. esp_ping_handle_t ping;
  73. esp_ping_new_session(&ping_config, &cbs, &ping);
  74. }
  75. 启动和停止 ping 会话
  76. ^^^^^^^^^^^^^^^^^^^^^^^^^^
  77. 使用 ``esp_ping_new_session`` 返回的句柄可以启动或停止 ping 会话。注意,ping 会话在创建后不会自动启动。如果 ping 会话停止后重启,ICMP 数据包的序列号会归零重新计数。
  78. 删除 ping 会话
  79. ^^^^^^^^^^^^^^^^^^^
  80. 如果不再使用 ping 会话,可用 ``esp_ping_delete_session`` 将其删除。在删除 ping 会话时,确保该会话已处于停止状态(即已调用了 ``esp_ping_stop`` ,或该会话已完成所有步骤)。
  81. 获取运行时间数据
  82. ^^^^^^^^^^^^^^^^^^
  83. 在回调函数中调用 ``esp_ping_get_profile``,可获取 ping 会话的不同运行时间数据,如上文代码示例所示。
  84. 应用示例
  85. ----------
  86. ICMP echo 示例: :example:`protocols/icmp_echo`
  87. API 参考
  88. --------------
  89. .. include-build-file:: inc/ping_sock.inc