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; } }
int icmp_pollsetup(FAR struct socket *psock, FAR struct pollfd *fds) { FAR struct icmp_conn_s *conn = psock->s_conn; FAR struct icmp_poll_s *info; FAR struct devif_callback_s *cb; int ret; DEBUGASSERT(conn != NULL && fds != NULL); /* Allocate a container to hold the poll information */ info = (FAR struct icmp_poll_s *)kmm_malloc(sizeof(struct icmp_poll_s)); if (!info) { return -ENOMEM; } /* Some of the following must be atomic */ net_lock(); /* Get the device that will provide the provide the NETDEV_DOWN event. * NOTE: in the event that the local socket is bound to INADDR_ANY, the * dev value will be zero and there will be no NETDEV_DOWN notifications. */ if (conn->dev == NULL) { conn->dev = netdev_default(); } /* Allocate a ICMP callback structure */ cb = icmp_callback_alloc(conn->dev); if (cb == NULL) { ret = -EBUSY; goto errout_with_lock; } /* Initialize the poll info container */ info->psock = psock; info->fds = fds; info->cb = cb; /* Initialize the callback structure. Save the reference to the info * structure as callback private data so that it will be available during * callback processing. */ cb->flags = 0; cb->priv = (FAR void *)info; cb->event = icmp_poll_eventhandler; if ((info->fds->events & POLLOUT) != 0) { cb->flags |= ICMP_POLL; } if ((info->fds->events & POLLIN) != 0) { cb->flags |= ICMP_NEWDATA; } if ((info->fds->events & (POLLHUP | POLLERR)) != 0) { cb->flags |= NETDEV_DOWN; } /* Save the reference in the poll info structure as fds private as well * for use during poll teardown as well. */ fds->priv = (FAR void *)info; /* Check for read data availability now */ if (!IOB_QEMPTY(&conn->readahead)) { /* Normal data may be read without blocking. */ fds->revents |= (POLLRDNORM & fds->events); } /* Check if any requested events are already in effect */ if (fds->revents != 0) { /* Yes.. then signal the poll logic */ nxsem_post(fds->sem); } net_unlock(); return OK; errout_with_lock: kmm_free(info); net_unlock(); return ret; }