All pastes #246732 Raw Edit

czokl

public cpp v1 · immutable
#246732 ·published 2006-11-12 16:37 UTC
rendered paste body
#include <iostream>#include <netinet/in.h>#include <sys/socket.h>#include <sys/types.h>#include <netdb.h>#include <arpa/inet.h>#include <stdlib.h>#include <netinet/ip.h>#include <netinet/ip_icmp.h>#include <unistd.h>#include <string.h>#define BUFSIZE 1024using namespace std;/* Funkce checksum je opsaná z p&#345;íslu&#261;ných RFC dokument&#367;. */unsigned short checksum(unsigned char *addr, int count){  register long sum = 0;    while( count > 1 )    {    /*  This is the inner loop */    sum += * ((unsigned short *) addr)++;    count -= 2;  }    /*  Add left-over byte, if any */  if( count > 0 )    sum += * (unsigned char *) addr;  /*  Fold 32-bit sum to 16 bits */  while (sum>>16)    sum = (sum & 0xffff) + (sum >> 16);  return ~sum;}int main(int argc, char *argv[]){  size_t size;  hostent *host;   icmphdr *icmp, *icmpRecv;  iphdr *ip;  int sock, lenght;  unsigned int ttl;  sockaddr_in sendSockAddr, receiveSockAddr;  char buffer[BUFSIZE];  fd_set mySet;  timeval tv;  char *addrString;  unsigned short int pid = getpid(), p;  if (argc != 2)  {    cerr << "Syntaxe:\n\t" << argv[0]	 << " " << "adresa" << endl;    return -1;  }  /* Zjistíme info o vzdáleném po&#269;íta&#269;i */  if ((host = gethostbyname(argv[1])) == NULL)  {    cerr << "©patná adresa" << endl;    return -1;  }  /* Vytvo&#345;ím soket */  if ((sock = socket(PF_INET, SOCK_RAW, IPPROTO_ICMP)) == -1)  {    cerr << "Nelze vytvo&#345;it soket" << endl;    return -1;  }  setsockopt(sock, SOL_IP, IP_TTL, (const char *)&ttl, sizeof(ttl));  /* Zaplníme polo&#318;ky ICMP hlavi&#269;ky */  icmp = (icmphdr *)malloc(sizeof(icmphdr));  icmp->type = ICMP_ECHO;  icmp->code = 0;  icmp->un.echo.id = pid;  /* P&#345;ipravíme sendto */  sendSockAddr.sin_family = AF_INET;  sendSockAddr.sin_port = 0;  memcpy(&(sendSockAddr.sin_addr), host->h_addr, host->h_length);  for (p = 1; p <= 5; p++)  {    icmp->checksum = 0;    icmp->un.echo.sequence = p;    icmp->checksum = checksum((unsigned char *)icmp, sizeof(icmphdr));    sendto(sock, (char *)icmp, sizeof(icmphdr), 0, (sockaddr *)&sendSockAddr, sizeof(sockaddr));    tv.tv_sec = 5;    tv.tv_usec = 0;    do    {      FD_ZERO(&mySet);      FD_SET(sock, &mySet);      if (select(sock + 1, &mySet, NULL, NULL, &tv) < 0)      {	cerr << "Selhal select" << endl;	break;      }      if (FD_ISSET(sock, &mySet))      {	size = sizeof(sockaddr_in);	if ((lenght = recvfrom(sock, buffer, BUFSIZE, 0, (sockaddr *)&receiveSockAddr, &size)) == -1)	{	  cerr << "Problem p&#345;i p&#345;ijímáni dat" << endl;	}	/* P&#345;i&#261;el blok dat IP hlavi&#269;ka + ICMP hlavi&#269;ka. */ 	ip = (iphdr *) buffer;	icmpRecv = (icmphdr *) (buffer + ip->ihl * 4);	if ((icmpRecv->un.echo.id == pid) && (icmpRecv->type == ICMP_ECHOREPLY) && (icmpRecv->un.echo.sequence == p))	{	  addrString = strdup(inet_ntoa(receiveSockAddr.sin_addr));	  host = gethostbyaddr(&receiveSockAddr.sin_addr, 4, AF_INET);	  cout << lenght << " byt&#367; z " << (host == NULL? "?" : host->h_name) << " (" << addrString << "): icmp_seq=" << icmpRecv->un.echo.sequence << " ttl=" << (int)ip->ttl << " &#269;as=nevím, nem&#283;&#345;il jsem:-)" << endl;	  free(addrString);	}      }      else      {	cout << "&#268;as vypr&#261;el" << endl;	break;      }    } while (!((icmpRecv->un.echo.id == pid) && (icmpRecv->type == ICMP_ECHOREPLY) && (icmpRecv->un.echo.sequence == p)));  }  close(sock);  free(icmp);  return 0;}