Example #1
0
int ping_cmd(int argc, char **argv)
{
    ipv6_addr_t dst;
    int payload_len, _num;

    if ((argc < 2) || (ipv6_addr_from_str(&dst, argv[1]) == NULL)) {
        usage(argv[0]);
        return 1;
    }
    if ((argc < 3) || ((_num = atoi(argv[2])) == 0)) {
        _num = 3;
    }
    if ((argc < 4) || ((payload_len = atoi(argv[3])) == 0)) {
        payload_len = 16;
    }
    atomic_store(&num, _num);
    atomic_store(&received, 0);
    seq = 0;
    if (recv_ntfy.callback == NULL) {
        uip_icmp6_echo_reply_callback_add(&recv_ntfy, handle_reply);
    }
    for (uint16_t i = 0; i < _num; i++) {
        _waiting = true;
        ping_send((uip_ipaddr_t *)&dst, payload_len);
        xtimer_usleep(1000000);
        if (_waiting) {
            puts("Timeout");
        }
    }

    return 0;
}
Example #2
0
void ping_start(void)
{
	printf("Using %s device\n", eth_get_name());
	net_set_timeout_handler(10000UL, ping_timeout_handler);

	ping_send();
}
Example #3
0
static void
ping_thread(void *arg)
{
  int s;
  int timeout = PING_RCV_TIMEO;
  ip_addr_t ping_target;

  LWIP_UNUSED_ARG(arg);

  if ((s = lwip_socket(AF_INET, SOCK_RAW, IP_PROTO_ICMP)) < 0) {
    return;
  }

  lwip_setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));

  while (1) {
    ping_target = PING_TARGET;

    if (ping_send(s, &ping_target) == ERR_OK) {
      LWIP_DEBUGF( PING_DEBUG, ("ping: send "));
      ip_addr_debug_print(PING_DEBUG, &ping_target);
      LWIP_DEBUGF( PING_DEBUG, ("\n"));

      ping_time = sys_now();
      ping_recv(s);
    } else {
      LWIP_DEBUGF( PING_DEBUG, ("ping: send "));
      ip_addr_debug_print(PING_DEBUG, &ping_target);
      LWIP_DEBUGF( PING_DEBUG, (" - error\n"));
    }
    sys_msleep(PING_DELAY);
  }
}
Example #4
0
void
ping_send_now()
{
  ip_addr_t ping_target = PING_TARGET;
  LWIP_ASSERT("ping_pcb != NULL", ping_pcb != NULL);
  ping_send(ping_pcb, &ping_target);
}
Example #5
0
void ping_start(void)
{
	printf("Using %s device\n", eth_get_name());
	NetSetTimeout(10000UL, ping_timeout);

	ping_send();
}
Example #6
0
void
ping_send_now()
{
  ip_addr_t ping_target;
  LWIP_ASSERT("ping_pcb != NULL", ping_pcb != NULL);
  ip_addr_copy_from_ip4(ping_target, PING_TARGET);
  ping_send(ping_pcb, &ping_target);
}
Example #7
0
static void ping_timeout(void *arg)
{
    struct raw_pcb *pcb = (struct raw_pcb*)arg;
    ip_addr_t ping_target = PING_TARGET;

    LWIP_ASSERT("ping_timeout: no pcb given!", pcb != NULL);

    ping_send(pcb, &ping_target);

    sys_timeout(PING_DELAY, ping_timeout, pcb);
}
Example #8
0
void AbstractRPM::pollPings()
{
#ifdef USE_PING
	pingobj_t *pingContext = ping_construct();

	std::map<Wt::WString, Wt::WString> ipToComputer;

	double timeout = 0.1;
	if (ping_setopt(pingContext, PING_OPT_TIMEOUT, (void *)&timeout))
		std::cerr << "ping_setopt failed: " << ping_get_error(pingContext) << std::endl;

	for (size_t i = 0; i < _computers.size(); i++) {
		if (_computers[i].ipAddress != Wt::WString()) {
			if (ping_host_add(pingContext, _computers[i].ipAddress.toUTF8().c_str()))
				std::cerr << "ping_host_add failed: " << ping_get_error(pingContext) << std::endl;
			ipToComputer[_computers[i].ipAddress] = _computers[i].name;
		}
	}

	while (1)
	{
		{
			boost::lock_guard<boost::mutex> lock(pollingThreadsExitLock);
			if (shouldExit)
				break;
		}

		if (ping_send(pingContext) < 0)
			std::cerr << "ping_send failed: " << ping_get_error(pingContext) << std::endl;

		pingobj_iter_t *iter;
		for (iter = ping_iterator_get(pingContext); iter != NULL; iter = ping_iterator_next(iter)) {
			double ping;
			size_t size = sizeof(double);
			if (ping_iterator_get_info(iter, PING_INFO_LATENCY, (void*)&ping, &size))
				std::cerr << "ping_iterator_get_info/latency failed" << std::endl;

			char hostname[101];
			size_t hostname_len = sizeof(hostname);
			if (ping_iterator_get_info(iter, PING_INFO_USERNAME, (void*)hostname, &hostname_len))
				std::cerr << "ping_iterator_get_info/hostname failed" << std::endl;

			setPingDelay(ipToComputer[hostname], ping);
		}

		sleep(1);
	}

	ping_destroy(pingContext);
#endif
}
Example #9
0
//Entry point of ping application.
void ping_Entry(void *arg)
{
  int s;
  int timeout = PING_RCV_TIMEO;
  __PING_PARAM* pParam = (__PING_PARAM*)arg;

  ping_pkt_seq = 0;  //Reset ping sequence number.
	ping_succ    = 0;

  if((s = lwip_socket(AF_INET, SOCK_RAW, IP_PROTO_ICMP)) < 0)
  {
		PrintLine("  ping : Create raw socket failed,quit.");
    return;
  }

  lwip_setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
  _hx_printf("\r\n  Ping %s with %d bytes packet:\r\n",inet_ntoa(pParam->targetAddr),pParam->size);
  while (1)
	{
		//ping_target = PING_TARGET; //ping gw
		//IP4_ADDR(&ping_target, 127,0,0,1); //ping loopback.
    if (ping_send(s, &pParam->targetAddr,pParam->size) == ERR_OK)
		{
			//printf(" ping_Entry : Send out packet,addr = %s,size = %d\r\n",inet_ntoa(pParam->targetAddr),pParam->size);
      ping_time = sys_now();
      ping_recv(s);
	    ping_pkt_seq ++;
    }
	  else
	  {
	    PrintLine("   ping : Send out packet failed.");
    }
    //sys_msleep(PING_DELAY);

	  //Try the specified times.
	  pParam->count --;
	  if(0 == pParam->count)
	  {
		  break;
	  }
  }
	//Show ping statistics.
	_hx_printf("\r\n");
	_hx_printf("  ping statistics: total send = %d,received = %d,%d loss.\r\n",
	  ping_pkt_seq,ping_succ,(ping_pkt_seq - ping_succ));
  //Close socket.
  lwip_close(s);
}
Example #10
0
static void
ping_thread(void *arg)
{
	int s;
	LWIP_UNUSED_ARG(arg);

	if ((s = lwip_socket(AF_INET, SOCK_RAW, IP_PROTO_ICMP)) < 0) {
		return;
	}

	while (1) {
		printf("sending ping\n");
		ping_send(s,&ping_addr);
		ping_recv(s,&ping_addr);
		sleep(1);
	}
}
Example #11
0
static void
ping_thread(void *arg)
{
	struct raw_pcb *raw;

	if (!(raw = raw_new(IP_PROTO_ICMP))) return;

	raw_recv(raw,ping_recv,NULL);

	while (1) {
		printf("ping send\n");
		ping_send(raw,&ping_addr);
		sleep(1);
	}
	/* Never reaches this */
	raw_remove(raw);
}
Example #12
0
static int transmit_fibril(void *arg)
{
	uint16_t seq_no = 0;

	while (true) {
		fibril_mutex_lock(&done_lock);
		if (done) {
			fibril_mutex_unlock(&done_lock);
			return 0;
		}
		fibril_mutex_unlock(&done_lock);

		(void) ping_send(++seq_no);
		async_usleep(PING_DELAY);
	}

	return 0;
}
Example #13
0
static void
ping_thread(void *arg)
{
  int s;
  struct ip_addr dest_addr;

  if ((s = lwip_socket(AF_INET, SOCK_RAW, IP_PROTO_ICMP)) < 0) {
    return;
  }

  IP4_ADDR(&dest_addr,192,168,2,1);

  while (1) {
    printf("sending ping\n");
    ping_send(s,&dest_addr);
    ping_recv(s,&dest_addr);
    sleep(1);
  }
}
static void
ping_host_thread(void *arg)
{
  int s,i=0;
  int timeout = PING_RCV_TIMEO;
  ip_addr_t ping_target;
  char *host = (char *)arg;
  //LWIP_UNUSED_ARG(arg);

  if ((s = lwip_socket(AF_INET, SOCK_RAW, IP_PROTO_ICMP)) < 0) {
    return;
  }

  lwip_setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));

  while (1) {
    //ping_target = PING_TARGET;
	if (i>4) break;
	i++;  
	(ip4_addr_set_u32(&ping_target, ipaddr_addr(host)));

    if (ping_send(s, &ping_target) == ERR_OK) {
      //LWIP_DEBUGF( PING_DEBUG, ("ping: send "));
      //ip_addr_debug_print(PING_DEBUG, &ping_target);
      //LWIP_DEBUGF( PING_DEBUG, ("\n"));
			rt_kprintf("ping: send ");
			ip_addr_debug_print1(&ping_target);
			rt_kprintf("\n");
		
      ping_time = sys_now();
      ping_recv(s);
    } else {
      //LWIP_DEBUGF( PING_DEBUG, ("ping: send "));
      //ip_addr_debug_print(PING_DEBUG, &ping_target);
      //LWIP_DEBUGF( PING_DEBUG, (" - error\n"));
			rt_kprintf("ping: send ");
			ip_addr_debug_print1(&ping_target);
			rt_kprintf(" - error\n");
    }
    sys_msleep(PING_DELAY);
  }
}
Example #15
0
static bool ICACHE_FLASH_ATTR
ping_raw_init(struct ping_msg *pingmsg)
{
	if (pingmsg == NULL)
		return false;

	ip_addr_t ping_target;
	pingmsg->ping_pcb = raw_new(IP_PROTO_ICMP);
	LWIP_ASSERT("ping_pcb != NULL", pingmsg->ping_pcb != NULL);

	raw_recv(pingmsg->ping_pcb, ping_recv, pingmsg);
	raw_bind(pingmsg->ping_pcb, IP_ADDR_ANY);

	ping_target.addr = pingmsg->ping_opt->ip;
	pingmsg ->ping_sent = system_get_time();
	ping_send(pingmsg->ping_pcb, &ping_target);

	sys_timeout(PING_TIMEOUT_MS, ping_timeout, pingmsg);
	sys_timeout(pingmsg->coarse_time, ping_coarse_tmr, pingmsg);
	return true;
}
Example #16
0
static void ICACHE_FLASH_ATTR
ping_coarse_tmr(void *arg)
{
//	struct ping_msg *pingmsg = (struct ping_msg*)arg;
	struct ping_option *ping_opt= NULL;
	struct ping_resp pingresp;
	ip_addr_t ping_target;

	LWIP_ASSERT("ping_timeout: no pcb given!", pingmsg != NULL);
	ping_target.addr = pingmsg->ping_opt->ip;
	ping_opt = pingmsg->ping_opt;
	if (--pingmsg->sent_count != 0){
		pingmsg ->ping_sent = system_get_time();
		ping_send(pingmsg->ping_pcb, &ping_target);

		sys_timeout(PING_TIMEOUT_MS, ping_timeout, pingmsg);
		sys_timeout(pingmsg->coarse_time, ping_coarse_tmr, pingmsg);
	} else {
		uint32 delay = system_relative_time(pingmsg->ping_start);
		delay /= PING_COARSE;
		//ping_seq_num = 0;
		if (ping_opt->sent_function == NULL){
			os_printf("ping %d, timeout %d, total payload %d bytes, %d ms\n",
					pingmsg->max_count, pingmsg->timeout_count, PING_DATA_SIZE*(pingmsg->max_count - pingmsg->timeout_count),delay);
		} else {
			os_bzero(&pingresp, sizeof(struct ping_resp));
			pingresp.total_count = pingmsg->max_count;
			pingresp.timeout_count = pingmsg->timeout_count;
			pingresp.total_bytes = PING_DATA_SIZE*(pingmsg->max_count - pingmsg->timeout_count);
			pingresp.total_time = delay;
			pingresp.ping_err = 0;
		}
		sys_untimeout(ping_coarse_tmr, pingmsg);
		raw_remove(pingmsg->ping_pcb);
//		os_free(pingmsg);
		if (ping_opt->sent_function != NULL)
			ping_opt->sent_function(ping_opt,(uint8*)&pingresp);
	}
}
static void
ping_host(char * host)
{
	int s,i=0;
	int timeout = PING_RCV_TIMEO;
	ip_addr_t ping_target;


	if ((s = lwip_socket(AF_INET, SOCK_RAW, IP_PROTO_ICMP)) < 0) {
		return;
	}

	lwip_setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));

	while (1) {
		if (i>4) break;
		i++;  
		//ping_target = netif_default->gw;
		//IP4_ADDR(&ping_target,210,82,5,1);
		(ip4_addr_set_u32(&ping_target, ipaddr_addr(host))); 

		if (ping_send(s, &ping_target) == ERR_OK) {
			//LWIP_DEBUGF( PING_DEBUG, ("ping: send "));
			rt_kprintf("ping: send ");
			ip_addr_debug_print1(&ping_target);
			rt_kprintf("\n");

			ping_time = sys_now();
			ping_recv(s);
		} else {
			//LWIP_DEBUGF( PING_DEBUG, ("ping: send "));
			rt_kprintf("ping: send ");
			ip_addr_debug_print1(&ping_target);
			rt_kprintf(" - error\n");
			//LWIP_DEBUGF( PING_DEBUG, (" - error\n"));
		}
		sys_msleep(PING_DELAY);
	}
}
Example #18
0
static void *ping_thread (void *arg) /* {{{ */
{
  static pingobj_t *pingobj = NULL;

  struct timeval  tv_begin;
  struct timeval  tv_end;
  struct timespec ts_wait;
  struct timespec ts_int;

  hostlist_t *hl;
  int count;

  c_complain_t complaint = C_COMPLAIN_INIT_STATIC;

  pthread_mutex_lock (&ping_lock);

  pingobj = ping_construct ();
  if (pingobj == NULL)
  {
    ERROR ("ping plugin: ping_construct failed.");
    ping_thread_error = 1;
    pthread_mutex_unlock (&ping_lock);
    return ((void *) -1);
  }

  if (ping_source != NULL)
    if (ping_setopt (pingobj, PING_OPT_SOURCE, (void *) ping_source) != 0)
      ERROR ("ping plugin: Failed to set source address: %s",
          ping_get_error (pingobj));

#ifdef HAVE_OPING_1_3
  if (ping_device != NULL)
    if (ping_setopt (pingobj, PING_OPT_DEVICE, (void *) ping_device) != 0)
      ERROR ("ping plugin: Failed to set device: %s",
          ping_get_error (pingobj));
#endif

  ping_setopt (pingobj, PING_OPT_TIMEOUT, (void *) &ping_timeout);
  ping_setopt (pingobj, PING_OPT_TTL, (void *) &ping_ttl);

  if (ping_data != NULL)
    ping_setopt (pingobj, PING_OPT_DATA, (void *) ping_data);

  /* Add all the hosts to the ping object. */
  count = 0;
  for (hl = hostlist_head; hl != NULL; hl = hl->next)
  {
    int tmp_status;
    tmp_status = ping_host_add (pingobj, hl->host);
    if (tmp_status != 0)
      WARNING ("ping plugin: ping_host_add (%s) failed: %s",
          hl->host, ping_get_error (pingobj));
    else
      count++;
  }

  if (count == 0)
  {
    ERROR ("ping plugin: No host could be added to ping object. Giving up.");
    ping_thread_error = 1;
    pthread_mutex_unlock (&ping_lock);
    return ((void *) -1);
  }

  /* Set up `ts_int' */
  {
    double temp_sec;
    double temp_nsec;

    temp_nsec = modf (ping_interval, &temp_sec);
    ts_int.tv_sec  = (time_t) temp_sec;
    ts_int.tv_nsec = (long) (temp_nsec * 1000000000L);
  }

  while (ping_thread_loop > 0)
  {
    int status;
    _Bool send_successful = 0;

    if (gettimeofday (&tv_begin, NULL) < 0)
    {
      char errbuf[1024];
      ERROR ("ping plugin: gettimeofday failed: %s",
          sstrerror (errno, errbuf, sizeof (errbuf)));
      ping_thread_error = 1;
      break;
    }

    pthread_mutex_unlock (&ping_lock);

    status = ping_send (pingobj);
    if (status < 0)
    {
      c_complain (LOG_ERR, &complaint, "ping plugin: ping_send failed: %s",
          ping_get_error (pingobj));
    }
    else
    {
      c_release (LOG_NOTICE, &complaint, "ping plugin: ping_send succeeded.");
      send_successful = 1;
    }

    pthread_mutex_lock (&ping_lock);

    if (ping_thread_loop <= 0)
      break;

    if (send_successful)
      (void) ping_dispatch_all (pingobj);

    if (gettimeofday (&tv_end, NULL) < 0)
    {
      char errbuf[1024];
      ERROR ("ping plugin: gettimeofday failed: %s",
          sstrerror (errno, errbuf, sizeof (errbuf)));
      ping_thread_error = 1;
      break;
    }

    /* Calculate the absolute time until which to wait and store it in
     * `ts_wait'. */
    time_calc (&ts_wait, &ts_int, &tv_begin, &tv_end);

    pthread_cond_timedwait (&ping_cond, &ping_lock, &ts_wait);
    if (ping_thread_loop <= 0)
      break;
  } /* while (ping_thread_loop > 0) */

  pthread_mutex_unlock (&ping_lock);
  ping_destroy (pingobj);

  return ((void *) 0);
} /* }}} void *ping_thread */
Example #19
0
File: ping.c Project: suborb/zsock
void Ping()
{
    char	buffer[80];
    char	buffer2[20];
    u16_t	i;
    u16_t	sequence;
    u32_t	ident;
    u32_t	timeout;
    ipaddr_t  addr;
    
    printf("Enter hostname/IP to ping\n");

    fgets_cons(buffer,79);
    if ( (addr=resolve_i(buffer) ) == 0L ) {
	printf("\nUnknown host\n");
	return;
    }

    

    memset(pingstat,0,sizeof(pingstat));
    
    ident = sequence = 0;
    received = pingav = pingmin = pingmax = 0;
    
    if ( icmp_register(ping_handler) == NULL ) {
	printf("\nCan't register handler..\n");
	return;
    }
    
    printf("\nPING %s (%s): 56 data bytes\n",buffer,inet_ntoa_i(addr,buffer2));


    timeout = set_timeout(50);
    for (;;) {
	if (  chk_timeout(timeout) ) {
	    timeout = set_timeout(100);
	    if ( ping_send(addr,&ident) ) 
		ident++;
	    else
		break;
	} 
#ifdef __Z88__
	if (getk() == 27 ) break;
#else
	if ( ident == 4 )
	    break;
#endif
	BUSYINT();
   }


    icmp_deregister();
    /* Just to test the resolver.... */
    if (reverse_addr_lookup_i(addr,buffer) == 0 )
	inet_ntoa_i(addr,buffer);
    printf("\n--- %s ping statistics ---\n",buffer);
    printf("%lu packets transmitted, %d packets received, %d%% packet loss\n",ident,received, 100-((received*100)/ident)  );
    printf("round-trip time min/avg/max= %d/%d/%d ms\n",pingmin,(pingav*10)/received,pingmax);
    return;
}
Example #20
0
int main (int argc, char **argv) {
    /* Local Vars */
    int         rv;
    pingobj_t   *oping;
    pingobj_iter_t *iter;

    uint32_t dropped=0, num=0;
    int ttl=0;
    double rt, rta=0;
    char haddr[40];
    size_t data_len, buf_len;

    /* Set signal handling and alarm */
    if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR)
        critical("Setup SIGALRM trap failed!");

    /* Process check arguments */
    if (process_arguments(argc, argv) != OK)
        unknown("Parsing arguments failed!");

    /* Start plugin timeout */
    alarm(mp_timeout);

    /* Init liboping */
    oping = ping_construct();
    if (oping == NULL)
        unknown("Can't initialize liboping");


    rv = ping_setopt(oping, PING_OPT_AF, &ipv);
    if (rv != 0)
        unknown("liboping setup Error: %s", ping_get_error(oping));
    rv = ping_setopt(oping, PING_OPT_DATA,
                     "monitoringplug/check_oping based on liboping.           ");
    if (rv != 0)
        unknown("liboping setup Error: %s", ping_get_error(oping));
    if (interface) {
        rv = ping_setopt(oping, PING_OPT_DEVICE, (char *)interface);
        if (rv != 0)
            unknown("liboping setup Error: %s", ping_get_error(oping));
    }
    if (ttl) {
        rv = ping_setopt(oping, PING_OPT_TTL, &ttl);
        if (rv != 0)
            unknown("liboping setup Error: %s", ping_get_error(oping));
    }

    rv = ping_host_add(oping, hostname);
    if (rv != 0)
        unknown("liboping setup Error: %s", ping_get_error(oping));


    for (; packets > 0; packets--) {
        rv = ping_send(oping);
        if (rv < 0)
            critical("Send Error: %s", ping_get_error(oping));

        for (iter = ping_iterator_get(oping); iter != NULL; iter = ping_iterator_next(iter)) {

            buf_len = sizeof(dropped);
            rv =  ping_iterator_get_info(iter, PING_INFO_DROPPED,
                                         &dropped, &buf_len);
            if (rv != 0)
                unknown("liboping ping_iterator_get_info failed!");

            buf_len = sizeof(rt);
            rv =  ping_iterator_get_info(iter, PING_INFO_LATENCY,
                                         &rt, &buf_len);
            if (rv != 0)
                unknown("liboping ping_iterator_get_info failed!");
            rta += rt;

            buf_len = sizeof(num);
            rv =  ping_iterator_get_info(iter, PING_INFO_SEQUENCE,
                                         &num, &buf_len);
            if (rv != 0)
                unknown("liboping ping_iterator_get_info failed!");

            data_len = 0;
            rv = ping_iterator_get_info(iter, PING_INFO_DATA,
                                        NULL, &data_len);
            if (rv != 0)
                unknown("liboping ping_iterator_get_info failed!");

            buf_len = sizeof(haddr);
            rv =  ping_iterator_get_info(iter, PING_INFO_ADDRESS,
                                         haddr, &buf_len);
            if (rv != 0)
                unknown("liboping ping_iterator_get_info failed!");

            buf_len = sizeof(ttl);
            rv = ping_iterator_get_info(iter, PING_INFO_RECV_TTL,
                                        &ttl, &buf_len);
            if (rv != 0)
                unknown("liboping ping_iterator_get_info failed!");

            if (mp_verbose > 0 && ttl > 0)
                printf("%zu bytes from %s (%s): icmp_seq=%u ttl=%i time=%.2f ms\n",
                       data_len, hostname,  haddr, num, ttl, rt);
        }

        if (quick && num > 0 &&
                get_status((float)(rta/num), rta_thresholds) == STATE_OK &&
                get_status((dropped*100)/num, lost_thresholds) == STATE_OK) {
            break;
        } else if (packets > 1) {
            if (interval.tv_sec || interval.tv_nsec)
                nanosleep(&interval, NULL);
            else
                sleep(1);
        }

    }

    if (num == 0)
        critical("PING_INFO_SEQUENCE is 0");

    mp_perfdata_float("rta", (float)(rta/num), "s", rta_thresholds);
    mp_perfdata_int("pl", (int)(dropped*100)/num, "%", lost_thresholds);


    int result1, result2;

    result1 = get_status((float)(rta/num), rta_thresholds);
    result2 = get_status((dropped*100)/num, lost_thresholds);
    result1 = result1 > result2 ? result1 : result2;

    free_threshold(rta_thresholds);
    free_threshold(lost_thresholds);

    switch(result1) {
    case STATE_OK:
        ok("Packet loss = %d%, RTA = %.2f ms",
           (int)(dropped*100)/num, (float)(rta/num));
        break;
    case STATE_WARNING:
        warning("Packet loss = %d%, RTA = %.2f ms",
                (int)(dropped*100)/num, (float)(rta/num));
        break;
    case STATE_CRITICAL:
        critical("Packet loss = %d%, RTA = %.2f ms",
                 (int)(dropped*100)/num, (float)(rta/num));
        break;
    }

    critical("You should never reach this point.");
}
Example #21
0
cmd_state_t cmd_ping(int argc, char* argv[], void* ctx)
{
        static enum {
                INIT,
                PING,
                WAIT_REPLY
        } state = INIT;

        struct ping_info_t *ping_info = &INFO;
        static struct raw_pcb *pcb;

        switch (state) {
        case INIT:
                if (init_ping_info(argc, argv, ping_info) != 0) {
                        printk("Usage: ping [-c count] [-i interval] "  \
                               "[-s packetsize]\n            " \
                               "[-w deadline] [-q] destination\n");
                        return CMD_DONE;
                }

                if (!(pcb = raw_new(IP_PROTO_ICMP))) {
                        printk("could not allocate pcb\n");
                        state = INIT;
                        return CMD_DONE;
                }
                raw_recv(pcb, ping_recv, ping_info);
                raw_bind(pcb, IP_ADDR_ANY);

                printk("PING %s %d(%d) bytes of data\n",
                       ip2str(ping_info->destination),
                       ping_info->data_size,
                       ping_info->size);
                state = PING;
                /* fall through */

        case PING:
                if (!netif_is_up(netif_default)) {
                        printk("netif is down\n");
                        raw_remove(pcb);
                        state = INIT;
                        return CMD_DONE;
                }

                if (ping_info->count && ping_info->num_tx == ping_info->count) {
                        ping_finalize(ping_info);
                        raw_remove(pcb);
                        state = INIT;
                        return CMD_DONE;
                }


                if (timer_get_ms() < ping_info->last_rx_tm + ping_info->interval) {
                        return CMD_INPROGRESS;
                }
                ping_send(pcb, ping_info);

                state = WAIT_REPLY;
                return CMD_INPROGRESS;

        case WAIT_REPLY:
                if (ping_info->flags & PING_REPLY) {
                        ping_info->flags &= (~PING_REPLY);
                        state = PING;
                        return CMD_INPROGRESS;
                }

                if (timer_get_ms() >
                    ping_info->last_tx_tm + ping_info->timeout) {
                        if (!ping_info->quiet)
                                printk("timeout from %s\n",
                                       ip2str(ping_info->destination));
                        state = PING;
                        return CMD_INPROGRESS;
                }

                if (ping_info->deadline &&
                    timer_get_ms() >
                    ping_info->first_tx_tm + ping_info->deadline * 1000) {
                        ping_finalize(ping_info);
                        raw_remove(pcb);
                        state = INIT;
                        return CMD_DONE;
                }

                return CMD_INPROGRESS;
        }

        /* unreachable */
        assert(0);
	return CMD_DONE;
}
Example #22
0
static int do_ping(int argc, char *argv[])
{
	int ret;
	uint64_t ping_start;
	unsigned retries = 0;

	if (argc < 2)
		return COMMAND_ERROR_USAGE;

	net_ping_ip = resolv(argv[1]);
	if (!net_ping_ip) {
		printf("unknown host %s\n", argv[1]);
		return 1;
	}

	ping_con = net_icmp_new(net_ping_ip, ping_handler, NULL);
	if (IS_ERR(ping_con)) {
		ret = PTR_ERR(ping_con);
		goto out;
	}

	ping_start = get_time_ns();
	ret = ping_send();
	if (ret)
		goto out_unreg;

	ping_state = PING_STATE_INIT;
	ping_sequence_number = 0;

	while (ping_state == PING_STATE_INIT) {
		if (ctrlc()) {
			ret = -EINTR;
			break;
		}

		net_poll();

		if (is_timeout(ping_start, SECOND)) {
			/* No answer, send another packet */
			ping_start = get_time_ns();
			ret = ping_send();
			if (ret)
				goto out_unreg;
			retries++;
		}

		if (retries > PKT_NUM_RETRIES) {
			ret = -ETIMEDOUT;
			goto out_unreg;
		}
	}

	if (!ret)
		printf("host %s is alive\n", argv[1]);

out_unreg:
	net_unregister(ping_con);
out:
	if (ret)
		printf("ping failed: %s\n", strerror(-ret));
	return ping_state == PING_STATE_SUCCESS ? 0 : 1;
}
Example #23
0
int main(int argc, char *argv[])
{
	int rc;
	int argi;

	rc = inetping_init(&ev_ops);
	if (rc != EOK) {
		printf(NAME ": Failed connecting to internet ping service "
		    "(%d).\n", rc);
		return 1;
	}

	argi = 1;
	if (argi < argc && str_cmp(argv[argi], "-r") == 0) {
		ping_repeat = true;
		++argi;
	} else {
		ping_repeat = false;
	}

	if (argc - argi != 1) {
		print_syntax();
		return 1;
	}

	/* Parse destination address */
	rc = addr_parse(argv[argi], &dest_addr);
	if (rc != EOK) {
		printf(NAME ": Invalid address format.\n");
		print_syntax();
		return 1;
	}

	/* Determine source address */
	rc = inetping_get_srcaddr(&dest_addr, &src_addr);
	if (rc != EOK) {
		printf(NAME ": Failed determining source address.\n");
		return 1;
	}

	fid_t fid;

	if (ping_repeat) {
		fid = fibril_create(transmit_fibril, NULL);
		if (fid == 0) {
			printf(NAME ": Failed creating transmit fibril.\n");
			return 1;
		}

		fibril_add_ready(fid);

		fid = fibril_create(input_fibril, NULL);
		if (fid == 0) {
			printf(NAME ": Failed creating input fibril.\n");
			return 1;
		}

		fibril_add_ready(fid);
	} else {
		ping_send(1);
	}

	fibril_mutex_lock(&done_lock);
	rc = EOK;
	while (!done && rc != ETIMEOUT) {
		rc = fibril_condvar_wait_timeout(&done_cv, &done_lock,
			ping_repeat ? 0 : PING_TIMEOUT);
	}
	fibril_mutex_unlock(&done_lock);

	if (rc == ETIMEOUT) {
		printf(NAME ": Echo request timed out.\n");
		return 1;
	}

	return 0;
}