ping.c




As was mentioned, gnome-admin/gxsnmp/app/ping.c uses
"struct icmp", which Linux appears not to have available.
I read some Linux header files and, proceeding by names
and analogy (no understanding), I fixed it.  Either my
fix doesn't work, or there are some other things wrong,
because I can't get anything sensible from the SNMP
utility.  Anyhow, for what it's worth, here is the revised
code for Linux.  It compiles, anyway.

...

int 
sendping (u_long address)
{
  struct in_addr       if_addr;
  register struct icmphdr *icp;
  int                  cc;
  char                 outpack[sizeof(struct icmphdr)];

  memset ((char *)&whereto, '\0', sizeof(struct sockaddr) );
  to->sin_family      = AF_INET;
  to->sin_addr.s_addr = address;
  if_addr.s_addr      = address;

  if (!ident)
    ident = getpid () & 0xFFFF;

  icp = (struct icmphdr *)outpack;
  icp->type  = ICMP_ECHO;
  icp->code  = 0;
  icp->checksum = 0;
  (icp->un).echo.sequence   = htons((unsigned short)(ntransmitted++));
  (icp->un).echo.id    = ident;
  CLR (ntohs((icp->un).echo.sequence) % mx_dup_ck);
  cc = datalen + 8;                          /* skips ICMP portion */
  icp->checksum = in_cksum ((u_short *)icp, cc);
  sendto (Pingsocket, (char *)outpack, cc, 0, &whereto, 
	  sizeof (struct sockaddr));
  return 1;
}

struct sockaddr_in *
readping ()
{
  struct ip              *ip;
  register struct icmphdr   *icp;
  int                    hlen;
  int                    i, cc;
  struct sockaddr_in     from;
  struct sockaddr_in     *ret;

  if (!packet &&(packet = (u_char *)g_malloc(packlen)) == NULL )
    {
      g_print ("ping: g_malloc failed for %d bytes\n", packlen);
      perror ("");
      exit (1);
    }
  errno = 0;
  i     = sizeof(from);
  if ((cc = recvfrom (Pingsocket, (char *)packet, packlen, 0, &from, &i))
< 0)
    return NULL;
  /*
    Lets check the ip header
  */
  ip   = (struct ip *)packet;
  hlen = ip->ip_hl << 2;
  icp  = (struct icmphdr *)(packet + hlen);
  if (icp->type == ICMP_ECHOREPLY && (icp->un).echo.id == ident)
    {
      ret = (struct sockaddr_in *)g_malloc (sizeof (struct sockaddr_in));
      memcpy ((char *)ret, &from, sizeof (struct sockaddr_in));
      return ret;
    }
  else
    return NULL;
}



Greg Lee, lee@hawaii.edu, root@moore5651.lll.hawaii.edu



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]