MCPcopy Create free account
hub / github.com/RT-Thread/rt-thread / lwip_netdev_ping

Function lwip_netdev_ping

components/net/lwip/port/ethernetif.c:213–295  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

211extern err_t lwip_ping_send(int s, ip_addr_t *addr, int size);
212
213int lwip_netdev_ping(struct netdev *netif, const char *host, size_t data_len,
214 uint32_t timeout, struct netdev_ping_resp *ping_resp, rt_bool_t isbind)
215{
216 int s, ttl, recv_len, result = 0;
217 int elapsed_time;
218 rt_tick_t recv_start_tick;
219#if LWIP_VERSION_MAJOR == 1U /* v1.x */
220 int recv_timeout = timeout;
221#else /* >= v2.x */
222 struct timeval recv_timeout = { timeout / 1000UL, timeout % 1000UL * 1000 };
223#endif
224 ip_addr_t target_addr;
225 struct addrinfo hint, *res = RT_NULL;
226 struct sockaddr_in *h = RT_NULL;
227 struct in_addr ina;
228 struct sockaddr_in local;
229
230 RT_ASSERT(netif);
231 RT_ASSERT(host);
232 RT_ASSERT(ping_resp);
233
234 rt_memset(&hint, 0x00, sizeof(hint));
235 /* convert URL to IP */
236 if (lwip_getaddrinfo(host, RT_NULL, &hint, &res) != 0)
237 {
238 return -RT_ERROR;
239 }
240 SMEMCPY(&h, &res->ai_addr, sizeof(struct sockaddr_in *));
241 SMEMCPY(&ina, &h->sin_addr, sizeof(ina));
242 lwip_freeaddrinfo(res);
243 if (inet_aton(inet_ntoa(ina), &target_addr) == 0)
244 {
245 return -RT_ERROR;
246 }
247 SMEMCPY(&(ping_resp->ip_addr), &target_addr, sizeof(ip_addr_t));
248
249 /* new a socket */
250 if ((s = lwip_socket(AF_INET, SOCK_RAW, IP_PROTO_ICMP)) < 0)
251 {
252 return -RT_ERROR;
253 }
254
255 local.sin_len = sizeof(local);
256 local.sin_family = AF_INET;
257 local.sin_port = 0;
258#ifndef NETDEV_USING_IPV6
259 local.sin_addr.s_addr = (netif->ip_addr.addr);
260#else
261 local.sin_addr.s_addr = (netif->ip_addr.u_addr.ip4.addr);
262#endif
263 if (isbind) {
264 lwip_bind(s, (struct sockaddr *)&local, sizeof(struct sockaddr_in));
265 }
266
267 lwip_setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &recv_timeout, sizeof(recv_timeout));
268
269 if (lwip_ping_send(s, &target_addr, data_len) == ERR_OK)
270 {

Callers

nothing calls this directly

Calls 10

rt_memsetFunction · 0.85
rt_tick_getFunction · 0.85
lwip_getaddrinfoFunction · 0.50
lwip_freeaddrinfoFunction · 0.50
lwip_socketFunction · 0.50
lwip_bindFunction · 0.50
lwip_setsockoptFunction · 0.50
lwip_ping_sendFunction · 0.50
lwip_ping_recvFunction · 0.50
lwip_closeFunction · 0.50

Tested by

no test coverage detected