Sistemas Operativos > GNU/Linux
Crear Un Ping Con Turbo C
(1/1)
hedar:
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:
--- Código: Text ---/* myping.c * * Copyright (c) 2000 Sean Walton and Macmillan Publishers. Use may be in * whole or in part in accordance to the General Public License (GPL). * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE.*/ /*****************************************************************************//*** myping.c ***//*** ***//*** Use the ICMP protocol to request "echo" from destination. ***//*****************************************************************************/ #include <fcntl.h>#include <errno.h>#include <sys/socket.h>#include <resolv.h>#include <netdb.h>#include <netinet/in.h>#include <netinet/ip_icmp.h> #define PACKETSIZE 64struct packet{ struct icmphdr hdr; char msg[PACKETSIZE-sizeof(struct icmphdr)];}; int pid=-1;struct protoent *proto=NULL; /*--------------------------------------------------------------------*//*--- checksum - standard 1s complement checksum ---*//*--------------------------------------------------------------------*/unsigned short checksum(void *b, int len){ unsigned short *buf = b; unsigned int sum=0; unsigned short result; for ( sum = 0; len > 1; len -= 2 ) sum += *buf++; if ( len == 1 ) sum += *(unsigned char*)buf; sum = (sum >> 16) + (sum & 0xFFFF); sum += (sum >> 16); result = ~sum; return result;} /*--------------------------------------------------------------------*//*--- display - present echo info ---*//*--------------------------------------------------------------------*/void display(void *buf, int bytes){ int i; struct iphdr *ip = buf; struct icmphdr *icmp = buf+ip->ihl*4; printf("----------------\n"); for ( i = 0; i < bytes; i++ ) { if ( !(i & 15) ) printf("\n%04X: ", i); printf("%04X ", ((unsigned char*)buf)[i]); } printf("\n"); printf("IPv%d: hdr-size=%d pkt-size=%d protocol=%d TTL=%d src=%s ", ip->version, ip->ihl*4, ntohs(ip->tot_len), ip->protocol, ip->ttl, inet_ntoa(ip->saddr)); printf("dst=%s\n", inet_ntoa(ip->daddr)); if ( icmp->un.echo.id == pid ) { printf("ICMP: type[%d/%d] checksum[%d] id[%d] seq[%d]\n", icmp->type, icmp->code, ntohs(icmp->checksum), icmp->un.echo.id, icmp->un.echo.sequence); }} /*--------------------------------------------------------------------*//*--- listener - separate process to listen for and collect messages--*//*--------------------------------------------------------------------*/void listener(void){ int sd; struct sockaddr_in addr; unsigned char buf[1024]; sd = socket(PF_INET, SOCK_RAW, proto->p_proto); if ( sd < 0 ) { perror("socket"); exit(0); } for (;;) { int bytes, len=sizeof(addr); bzero(buf, sizeof(buf)); bytes = recvfrom(sd, buf, sizeof(buf), 0, (struct sockaddr*)&addr, &len); if ( bytes > 0 ) display(buf, bytes); else perror("recvfrom"); } exit(0);} /*--------------------------------------------------------------------*//*--- ping - Create message and send it. ---*//*--------------------------------------------------------------------*/void ping(struct sockaddr_in *addr){ const int val=255; int i, sd, cnt=1; struct packet pckt; struct sockaddr_in r_addr; sd = socket(PF_INET, SOCK_RAW, proto->p_proto); if ( sd < 0 ) { perror("socket"); return; } if ( setsockopt(sd, SOL_IP, IP_TTL, &val, sizeof(val)) != 0) perror("Set TTL option"); if ( fcntl(sd, F_SETFL, O_NONBLOCK) != 0 ) perror("Request nonblocking I/O"); for (;;) { int len=sizeof(r_addr); printf("Msg #%d\n", cnt); if ( recvfrom(sd, &pckt, sizeof(pckt), 0, (struct sockaddr*)&r_addr, &len) > 0 ) printf("***Got message!***\n"); bzero(&pckt, sizeof(pckt)); pckt.hdr.type = ICMP_ECHO; pckt.hdr.un.echo.id = pid; for ( i = 0; i < sizeof(pckt.msg)-1; i++ ) pckt.msg[i] = i+'0'; pckt.msg[i] = 0; pckt.hdr.un.echo.sequence = cnt++; pckt.hdr.checksum = checksum(&pckt, sizeof(pckt)); if ( sendto(sd, &pckt, sizeof(pckt), 0, (struct sockaddr*)addr, sizeof(*addr)) <= 0 ) perror("sendto"); sleep(1); }} /*--------------------------------------------------------------------*//*--- main - look up host and start ping processes. ---*//*--------------------------------------------------------------------*/int main(int count, char *strings[]){ struct hostent *hname; struct sockaddr_in addr; if ( count != 2 ) { printf("usage: %s <addr>\n", strings[0]); exit(0); } if ( count > 1 ) { pid = getpid(); proto = getprotobyname("ICMP"); hname = gethostbyname(strings[1]); bzero(&addr, sizeof(addr)); addr.sin_family = hname->h_addrtype; addr.sin_port = 0; addr.sin_addr.s_addr = *(long*)hname->h_addr; if ( fork() == 0 ) listener(); else ping(&addr); wait(0); } else printf("usage: myping <hostname>\n"); return 0;}
Si queres entender el tema un poco mas, aca tenes un tutorial.
su -:
Whow, que codigo, en Perl es mas facil, algo como esto:
--- Código: Text ---#!/usr/bin/perluse strict;use Getopt::Std;use Socket;use Net::Ping;my %opt;getopts('nI:',\%opt);die "Usage: $0 host [timeout]\n" unless @ARGV;my $host = shift;my $timeout = (@ARGV) ? shift : 20;my $a = gethostbyname($host);if (defined $a) { if ($opt{'n'}) { my $name = inet_ntoa($a); $host = $name if (defined $name); } else { my $name = gethostbyaddr($a,PF_INET); $host = $name if (defined $name); } my $handle = Net::Ping->new($> ? 'udp' : 'icmp', $timeout); if ($handle->ping($host)) { warn "$host is alive\n"; } else { die "No answer from $host"; } }else { die "Unknown host $host\n"; } __END__ =head1 NAME ping - probe for network hosts =head1 SYNOPSIS ping [-n] hostname [ timeout ] =head1 DESCRIPTION C<ping> looks up I<hostname> and then attempts to contact it via the network.If the effective userid permits an ICMP (Internet Control Message Protocol)ECHO_REQUEST packet is sent, otherwise and attempt is made to connect tothe echo port using UDP protocol. A I<timeout> may be specified in seconds. The default is 20 seconds. If C<-n> option is specified then the address of I<hostname> is reportedas numbers. =head1 AUTHOR Nick Ing-Simmons <nick@ni-s.u-net.com> =cut
:)
hedar:
Muchas gracias amigo corre a la perfeccion, Pero me gustaria saber que es lo que hace esta linea <icmp=(struct icmphdr *)pqt;> gracias.
Navegación
Ir a la versión completa