int icmp_pollteardown(FAR struct socket *psock, FAR struct pollfd *fds) { FAR struct icmp_conn_s *conn; FAR struct icmp_poll_s *info; DEBUGASSERT(psock != NULL && psock->s_conn != NULL && fds != NULL && fds->priv != NULL); conn = psock->s_conn; /* Recover the socket descriptor poll state info from the poll structure */ info = (FAR struct icmp_poll_s *)fds->priv; DEBUGASSERT(info != NULL && info->fds != NULL && info->cb != NULL); if (info != NULL) { /* Release the callback */ net_lock(); icmp_callback_free(conn->dev, info->cb); net_unlock(); /* Release the poll/select data slot */ info->fds->priv = NULL; /* Then free the poll info container */ kmm_free(info); } return OK; }
int icmp_ping(in_addr_t addr, uint16_t id, uint16_t seqno, uint16_t datalen, int dsecs) { struct icmp_ping_s state; net_lock_t save; #ifdef CONFIG_NET_ARP_SEND int ret; /* Make sure that the IP address mapping is in the ARP table */ ret = arp_send(addr); if (ret < 0) { ndbg("ERROR: Not reachable\n"); return -ENETUNREACH; } #endif /* Initialize the state structure */ sem_init(&state.png_sem, 0, 0); state.png_ticks = DSEC2TICK(dsecs); /* System ticks to wait */ state.png_result = -ENOMEM; /* Assume allocation failure */ state.png_addr = addr; /* Address of the peer to be ping'ed */ state.png_id = id; /* The ID to use in the ECHO request */ state.png_seqno = seqno; /* The seqno to use in the ECHO request */ state.png_datlen = datalen; /* The length of data to send in the ECHO request */ state.png_sent = false; /* ECHO request not yet sent */ save = net_lock(); state.png_time = clock_systimer(); /* Set up the callback */ state.png_cb = icmp_callback_alloc(); if (state.png_cb) { state.png_cb->flags = (ICMP_POLL | ICMP_ECHOREPLY); state.png_cb->priv = (void*)&state; state.png_cb->event = ping_interrupt; state.png_result = -EINTR; /* Assume sem-wait interrupted by signal */ /* Notify the device driver of the availability of TX data */ #ifdef CONFIG_NETDEV_MULTINIC netdev_ipv4_txnotify(g_ipv4_allzeroaddr, state.png_addr); #else netdev_ipv4_txnotify(state.png_addr); #endif /* Wait for either the full round trip transfer to complete or * for timeout to occur. (1) net_lockedwait will also terminate if a * signal is received, (2) interrupts may be disabled! They will * be re-enabled while the task sleeps and automatically * re-enabled when the task restarts. */ nllvdbg("Start time: 0x%08x seqno: %d\n", state.png_time, seqno); net_lockedwait(&state.png_sem); icmp_callback_free(state.png_cb); } net_unlock(save); /* Return the negated error number in the event of a failure, or the * sequence number of the ECHO reply on success. */ if (!state.png_result) { nllvdbg("Return seqno=%d\n", state.png_seqno); return (int)state.png_seqno; } else { nlldbg("Return error=%d\n", -state.png_result); return state.png_result; } }