Advertisement
Guest User

Untitled

a guest
Mar 19th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.46 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <unistd.h>
  3. #include <fcntl.h>
  4. #include <errno.h>
  5. #include <sys/wait.h>
  6. #include <sys/socket.h>
  7. #include <resolv.h>
  8. #include <netdb.h>
  9. #include <netinet/in.h>
  10. #include <netinet/ip_icmp.h>
  11. #include <arpa/inet.h>
  12. #include <string.h>
  13. #include <strings.h>
  14. #include <pthread.h>
  15.  
  16. #define PACKETSIZE  64
  17. struct packet{
  18.     struct icmphdr hdr;
  19.     char msg[PACKETSIZE-sizeof(struct icmphdr)];
  20. };
  21.  
  22.  
  23. struct protoent *proto=NULL;
  24. unsigned int usecs_before;
  25. unsigned int usecs_after;
  26.  
  27. unsigned short checksum(void *b, int len)
  28. {   unsigned short *buf = b;
  29.     unsigned int sum=0;
  30.     unsigned short result;
  31.  
  32.     for ( sum = 0; len > 1; len -= 2 )
  33.         sum += *buf++;
  34.     if ( len == 1 )
  35.         sum += *(unsigned char*)buf;
  36.     sum = (sum >> 16) + (sum & 0xFFFF);
  37.     sum += (sum >> 16);
  38.     result = ~sum;
  39.     return result;
  40. }
  41.  
  42. void display(void *buf, int bytes){
  43.     int i;
  44.     struct in_addr saddr;
  45.     struct in_addr daddr;
  46.     struct iphdr *ip = buf;
  47.     struct icmphdr *icmp = buf+ip->ihl*4;
  48.  
  49.  
  50.     bzero(&saddr, sizeof(struct in_addr));
  51.     bzero(&daddr, sizeof(struct in_addr));
  52.     saddr.s_addr = ip->saddr;
  53.     daddr.s_addr = ip->daddr;
  54.  
  55.     printf("----------------\n");
  56.     for ( i = 0; i < bytes; i++ ){
  57.         if ( !(i & 15) ) printf("\n%02X:  ", i);
  58.         printf("%02X ", ((unsigned char*)buf)[i]);
  59.     }
  60.     printf("\n");
  61.     printf("IPv%d: hdr-size=%d pkt-size=%d protocol=%d TTL=%d src=%s ",
  62.         ip->version, ip->ihl*4, ntohs(ip->tot_len), ip->protocol,
  63.         ip->ttl, inet_ntoa(saddr));
  64.     printf("dst=%s\n", inet_ntoa(daddr));
  65.     printf("ICMP: type[%d/%d] checksum[%d] id[%d] seq[%d]\n",
  66.         icmp->type, icmp->code, ntohs(icmp->checksum),
  67.         icmp->un.echo.id, icmp->un.echo.sequence);
  68. }
  69.  
  70. void ping_reply(void *buf, int bytes){
  71.     int i, sd, ttl, pckt_size;
  72.     struct packet pckt;
  73.     struct sockaddr_in r_addr;
  74.     struct sockaddr_in s_addr;
  75.     struct in_addr saddr;
  76.  
  77.     uint32_t *timestamp;
  78.  
  79.     pckt_size = bytes - sizeof(struct iphdr);
  80.     struct iphdr *ip = buf;
  81.     memcpy(&pckt, buf+ip->ihl*4, pckt_size);
  82.     bzero(&saddr, sizeof(struct in_addr));
  83.     saddr.s_addr = ip->saddr;
  84.     ttl = ip->ttl / 2;
  85.  
  86.     r_addr.sin_family = AF_INET;
  87.     r_addr.sin_port = 0;
  88.     r_addr.sin_addr = saddr;
  89.  
  90.     sd = socket(PF_INET, SOCK_RAW, proto->p_proto);
  91.     if ( sd < 0 ){
  92.         perror("socket");
  93.         return;
  94.     }
  95.     if ( setsockopt(sd, SOL_IP, IP_TTL, &ttl, sizeof(ttl)) != 0)
  96.         perror("Set TTL option");
  97.     if ( fcntl(sd, F_SETFL, O_NONBLOCK) != 0 )
  98.         perror("Request nonblocking I/O");
  99.  
  100.     pckt.hdr.type = ICMP_ECHOREPLY;
  101.     {
  102.         socklen_t len=sizeof(s_addr);
  103.         if (recvfrom(sd, &pckt, sizeof(pckt), 0, (struct sockaddr*)&s_addr, &len) > 0 )
  104.             printf("recieved\n");
  105.  
  106.         pckt.hdr.checksum = 0;
  107.         uint32_t *timestamp = (uint32_t *)pckt.msg;
  108.         printf("%i\n",*timestamp);
  109.         (*timestamp)++;
  110.         printf("%i\n",*timestamp);
  111.         pckt.hdr.checksum = checksum(&pckt, pckt_size);
  112.         if ( sendto(sd, &pckt, pckt_size, 0, (struct sockaddr*)&r_addr, sizeof(r_addr)) <= 0 )
  113.             perror("send");
  114.     }
  115.  
  116. }
  117.  
  118. void listener(void){
  119.     int sd;
  120.     struct sockaddr_in addr;
  121.     unsigned char buf[1024];
  122.  
  123.     sd = socket(PF_INET, SOCK_RAW, proto->p_proto);
  124.     if ( sd < 0 ){
  125.         perror("socket");
  126.         exit(0);
  127.     }
  128.     for (;;){
  129.         int bytes; socklen_t len=sizeof(addr);
  130.  
  131.         bzero(buf, sizeof(buf));
  132.         bytes = recvfrom(sd, buf, sizeof(buf), 0, (struct sockaddr*)&addr, &len);
  133.         if (bytes>0){
  134.             display(buf, bytes);
  135.             ping_reply(buf, bytes);
  136.         }
  137.         else
  138.             perror("recv");
  139.     }
  140.     exit(0);
  141. }
  142.  
  143. int main(int argc, char const *argv[]){
  144.     proto = getprotobyname("ICMP");
  145.     listener();
  146.     return 0;
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement