示例#1
0
int
ping_xmit (PING *p)
{
  int i, buflen;

  if (_ping_setbuf (p))
    return -1;

  buflen = _ping_packetsize (p);

  /* Mark sequence number as sent */
  _PING_CLR (p, p->ping_num_xmit % p->ping_cktab_size);

  /* Encode ICMP header */
  switch (p->ping_type)
    {
    case ICMP_ECHO:
      icmp_echo_encode (p->ping_buffer, buflen, p->ping_ident,
		       p->ping_num_xmit);
      break;
    case ICMP_TIMESTAMP:
      icmp_timestamp_encode (p->ping_buffer, buflen, p->ping_ident,
			    p->ping_num_xmit);
      break;
    case ICMP_ADDRESS:
      icmp_address_encode (p->ping_buffer, buflen, p->ping_ident,
			  p->ping_num_xmit);
      break;
    default:
      icmp_generic_encode (p->ping_buffer, buflen, p->ping_type, p->ping_ident,
			  p->ping_num_xmit);
      break;
    }

  i = sendto (p->ping_fd, (char *)p->ping_buffer, buflen, 0,
	     (struct sockaddr*) &p->ping_dest,
	     sizeof (struct sockaddr_in));
  if (i < 0)
    {
      perror ("ping: sendto");
    }
  else
    {
      p->ping_num_xmit++;
      if (i != buflen)
	printf ("ping: wrote %s %d chars, ret=%d\n",
	       p->ping_hostname, buflen, i);
    }


  return 0;
}
示例#2
0
int
icmp_address_encode (u_char * buffer, size_t bufsize, int ident, int seqno)
{
  icmphdr_t *icmp;

  if (bufsize < 12)
    return -1;

  icmp = (icmphdr_t *) buffer;
  icmp->icmp_mask = 0;
  icmp_generic_encode (buffer, bufsize, ICMP_ADDRESS, ident, seqno);
  return 0;
}
示例#3
0
int
icmp_timestamp_encode (unsigned char * buffer, size_t bufsize, int ident, int seqno)
{
    icmphdr_t *icmp;
    struct timeval tv;
    unsigned long v;

    if (bufsize < ICMP_TSLEN)
        return -1;

    gettimeofday (&tv, NULL);
    v = htonl ((tv.tv_sec % 86400) * 1000 + tv.tv_usec / 1000);

    icmp = (icmphdr_t *) buffer;
    icmp->icmp_otime = v;
    icmp->icmp_rtime = v;
    icmp->icmp_ttime = v;
    icmp_generic_encode (buffer, bufsize, ICMP_TIMESTAMP, ident, seqno);
    return 0;
}
示例#4
0
int
icmp_echo_encode (unsigned char * buffer, size_t bufsize, int ident, int seqno)
{
  return icmp_generic_encode (buffer, bufsize, ICMP_ECHO, ident, seqno);
}