• Jueves 14 de Noviembre de 2024, 23:01

Autor Tema:  Crear Un Ping Con Turbo C  (Leído 1430 veces)

hedar

  • Nuevo Miembro
  • *
  • Mensajes: 13
    • Ver Perfil
Crear Un Ping Con Turbo C
« en: Lunes 27 de Febrero de 2006, 11:43 »
0
Lo que necesito compañeros es que me digan porfavor como hago para hacer el codigo fuente de un ping lo unico que se es que utiliza el protocolo icmp. Lo importante del programa es que me envie un paquete y que a la maquina que se lo envie me responda el ping, se los agradeceria, lo necesito muy urgente.

NRM

  • Miembro MUY activo
  • ***
  • Mensajes: 279
  • Nacionalidad: ar
    • Ver Perfil
    • http://www.narrowmind.com.ar
Re: Crear Un Ping Con Turbo C
« Respuesta #1 en: Martes 28 de Febrero de 2006, 01:43 »
0
Código: Text
  1. /* myping.c
  2.  *
  3.  * Copyright (c) 2000 Sean Walton and Macmillan Publishers.  Use may be in
  4.  * whole or in part in accordance to the General Public License (GPL).
  5.  *
  6.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  7.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  8.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  9.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  10.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  11.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  12.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  13.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  14.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  15.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  16.  * SUCH DAMAGE.
  17. */
  18.  
  19. /*****************************************************************************/
  20. /*** myping.c                                                              ***/
  21. /***                                                                       ***/
  22. /*** Use the ICMP protocol to request "echo" from destination.             ***/
  23. /*****************************************************************************/
  24.  
  25. #include <fcntl.h>
  26. #include <errno.h>
  27. #include <sys/socket.h>
  28. #include <resolv.h>
  29. #include <netdb.h>
  30. #include <netinet/in.h>
  31. #include <netinet/ip_icmp.h>
  32.  
  33. #define PACKETSIZE  64
  34. struct packet
  35. {
  36.   struct icmphdr hdr;
  37.   char msg[PACKETSIZE-sizeof(struct icmphdr)];
  38. };
  39.  
  40. int pid=-1;
  41. struct protoent *proto=NULL;
  42.  
  43. /*--------------------------------------------------------------------*/
  44. /*--- checksum - standard 1s complement checksum                   ---*/
  45. /*--------------------------------------------------------------------*/
  46. unsigned short checksum(void *b, int len)
  47. {  unsigned short *buf = b;
  48.   unsigned int sum=0;
  49.   unsigned short result;
  50.  
  51.   for ( sum = 0; len > 1; len -= 2 )
  52.     sum += *buf++;
  53.   if ( len == 1 )
  54.     sum += *(unsigned char*)buf;
  55.   sum = (sum >> 16) + (sum & 0xFFFF);
  56.   sum += (sum >> 16);
  57.   result = ~sum;
  58.   return result;
  59. }
  60.  
  61. /*--------------------------------------------------------------------*/
  62. /*--- display - present echo info                                  ---*/
  63. /*--------------------------------------------------------------------*/
  64. void display(void *buf, int bytes)
  65. {  int i;
  66.   struct iphdr *ip = buf;
  67.   struct icmphdr *icmp = buf+ip->ihl*4;
  68.  
  69.   printf("----------------\n");
  70.   for ( i = 0; i < bytes; i++ )
  71.   {
  72.     if ( !(i & 15) ) printf("\n%04X:  ", i);
  73.     printf("%04X ", ((unsigned char*)buf)[i]);
  74.   }
  75.   printf("\n");
  76.   printf("IPv%d: hdr-size=%d pkt-size=%d protocol=%d TTL=%d src=%s ",
  77.     ip->version, ip->ihl*4, ntohs(ip->tot_len), ip->protocol,
  78.     ip->ttl, inet_ntoa(ip->saddr));
  79.   printf("dst=%s\n", inet_ntoa(ip->daddr));
  80.   if ( icmp->un.echo.id == pid )
  81.   {
  82.     printf("ICMP: type[%d/%d] checksum[%d] id[%d] seq[%d]\n",
  83.       icmp->type, icmp->code, ntohs(icmp->checksum),
  84.       icmp->un.echo.id, icmp->un.echo.sequence);
  85.   }
  86. }
  87.  
  88. /*--------------------------------------------------------------------*/
  89. /*--- listener - separate process to listen for and collect messages--*/
  90. /*--------------------------------------------------------------------*/
  91. void listener(void)
  92. {  int sd;
  93.   struct sockaddr_in addr;
  94.   unsigned char buf[1024];
  95.  
  96.   sd = socket(PF_INET, SOCK_RAW, proto->p_proto);
  97.   if ( sd < 0 )
  98.   {
  99.     perror("socket");
  100.     exit(0);
  101.   }
  102.   for (;;)
  103.   {  int bytes, len=sizeof(addr);
  104.  
  105.     bzero(buf, sizeof(buf));
  106.     bytes = recvfrom(sd, buf, sizeof(buf), 0, (struct sockaddr*)&addr, &len);
  107.     if ( bytes > 0 )
  108.       display(buf, bytes);
  109.     else
  110.       perror("recvfrom");
  111.   }
  112.   exit(0);
  113. }
  114.  
  115. /*--------------------------------------------------------------------*/
  116. /*--- ping - Create message and send it.                           ---*/
  117. /*--------------------------------------------------------------------*/
  118. void ping(struct sockaddr_in *addr)
  119. {  const int val=255;
  120.   int i, sd, cnt=1;
  121.   struct packet pckt;
  122.   struct sockaddr_in r_addr;
  123.  
  124.   sd = socket(PF_INET, SOCK_RAW, proto->p_proto);
  125.   if ( sd < 0 )
  126.   {
  127.     perror("socket");
  128.     return;
  129.   }
  130.   if ( setsockopt(sd, SOL_IP, IP_TTL, &val, sizeof(val)) != 0)
  131.     perror("Set TTL option");
  132.   if ( fcntl(sd, F_SETFL, O_NONBLOCK) != 0 )
  133.     perror("Request nonblocking I/O");
  134.   for (;;)
  135.   {  int len=sizeof(r_addr);
  136.  
  137.     printf("Msg #%d\n", cnt);
  138.     if ( recvfrom(sd, &pckt, sizeof(pckt), 0, (struct sockaddr*)&r_addr, &len) > 0 )
  139.       printf("***Got message!***\n");
  140.     bzero(&pckt, sizeof(pckt));
  141.     pckt.hdr.type = ICMP_ECHO;
  142.     pckt.hdr.un.echo.id = pid;
  143.     for ( i = 0; i < sizeof(pckt.msg)-1; i++ )
  144.       pckt.msg[i] = i+'0';
  145.     pckt.msg[i] = 0;
  146.     pckt.hdr.un.echo.sequence = cnt++;
  147.     pckt.hdr.checksum = checksum(&pckt, sizeof(pckt));
  148.     if ( sendto(sd, &pckt, sizeof(pckt), 0, (struct sockaddr*)addr, sizeof(*addr)) <= 0 )
  149.       perror("sendto");
  150.     sleep(1);
  151.   }
  152. }
  153.  
  154. /*--------------------------------------------------------------------*/
  155. /*--- main - look up host and start ping processes.                ---*/
  156. /*--------------------------------------------------------------------*/
  157. int main(int count, char *strings[])
  158. {  struct hostent *hname;
  159.   struct sockaddr_in addr;
  160.  
  161.   if ( count != 2 )
  162.   {
  163.     printf("usage: %s <addr>\n", strings[0]);
  164.     exit(0);
  165.   }
  166.   if ( count > 1 )
  167.   {
  168.     pid = getpid();
  169.     proto = getprotobyname("ICMP");
  170.     hname = gethostbyname(strings[1]);
  171.     bzero(&addr, sizeof(addr));
  172.     addr.sin_family = hname->h_addrtype;
  173.     addr.sin_port = 0;
  174.     addr.sin_addr.s_addr = *(long*)hname->h_addr;
  175.     if ( fork() == 0 )
  176.       listener();
  177.     else
  178.       ping(&addr);
  179.     wait(0);
  180.   }
  181.   else
  182.     printf("usage: myping <hostname>\n");
  183.   return 0;
  184. }
  185.  

Si queres entender el tema un poco mas, aca tenes un tutorial.

su -

  • Moderador
  • ******
  • Mensajes: 2349
    • Ver Perfil
Re: Crear Un Ping Con Turbo C
« Respuesta #2 en: Miércoles 1 de Marzo de 2006, 22:38 »
0
Whow, que codigo, en Perl es mas facil, algo como esto:

Código: Text
  1. #!/usr/bin/perl
  2. use strict;
  3. use Getopt::Std;
  4. use Socket;
  5. use Net::Ping;
  6. my %opt;
  7. getopts('nI:',\%opt);
  8. die "Usage: $0 host [timeout]\n" unless @ARGV;
  9. my $host = shift;
  10. my $timeout = (@ARGV) ? shift : 20;
  11. my $a = gethostbyname($host);
  12. if (defined $a)
  13.  {
  14.   if ($opt{'n'})
  15.    {
  16.     my $name = inet_ntoa($a);
  17.     $host = $name if (defined $name);
  18.    }
  19.   else
  20.    {
  21.     my $name = gethostbyaddr($a,PF_INET);
  22.     $host = $name if (defined $name);
  23.    }
  24.   my $handle = Net::Ping->new($> ? 'udp' : 'icmp', $timeout);
  25.  
  26.   if ($handle->ping($host))
  27.    {
  28.     warn "$host is alive\n";
  29.    }
  30.   else
  31.    {
  32.     die "No answer from $host";
  33.    }
  34.  }
  35. else
  36.  {
  37.   die "Unknown host $host\n";
  38.  }
  39.  
  40. __END__
  41.  
  42. =head1 NAME
  43.  
  44. ping - probe for network hosts
  45.  
  46. =head1 SYNOPSIS
  47.  
  48.   ping [-n] hostname [ timeout ]
  49.  
  50. =head1 DESCRIPTION
  51.  
  52. C<ping> looks up I<hostname> and then attempts to contact it via the network.
  53. If the effective userid permits an ICMP (Internet Control Message Protocol)
  54. ECHO_REQUEST packet is sent, otherwise and attempt is made to connect to
  55. the echo port using UDP protocol.
  56.  
  57. A I<timeout> may be specified in seconds. The default is 20 seconds.
  58.  
  59. If C<-n> option is specified then the address of I<hostname> is reported
  60. as numbers.
  61.  
  62. =head1 AUTHOR
  63.  
  64. Nick Ing-Simmons <nick@ni-s.u-net.com>
  65.  
  66. =cut
  67.  

 :)
*******PELIGRO LEE ESTO!!*******

There is no place like 127.0.0.1

Conecto luego existo, no conecto luego insisto.

hedar

  • Nuevo Miembro
  • *
  • Mensajes: 13
    • Ver Perfil
Re: Crear Un Ping Con Turbo C
« Respuesta #3 en: Lunes 13 de Marzo de 2006, 19:31 »
0
Muchas gracias amigo corre a la perfeccion, Pero me gustaria saber que es lo que hace esta linea <icmp=(struct icmphdr *)pqt;> gracias.