Advertisement
LA77

Untitled

May 23rd, 2025
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.57 KB | None | 0 0
  1. #include <arpa/inet.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <sys/socket.h>
  6. #include <unistd.h>
  7. #include <stdbool.h>
  8. #include <netinet/ip_icmp.h>
  9.  
  10. // ma transform
  11.  
  12. unsigned short checksum (void *b, int len) {
  13.     unsigned short * buf = b;
  14.     unsigned int sum = 0;
  15.     unsigned short result;
  16.    
  17.     for(sum = 0; len > 1; len -= 2)
  18.         sum += * buf ++;
  19.  
  20.     if(len == 1)
  21.         sum += *(unsigned char *) buf ;
  22.  
  23.     sum = (sum >> 16) + (sum & 0xFFFF);
  24.     sum += (sum >> 16);
  25.     result = ~sum;
  26.  
  27.     return result;
  28. }
  29.  
  30.  
  31. int main()
  32. {
  33.     // creating  a raw socket
  34.     int sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
  35.     if (sockfd < 0) {
  36.         perror("Failed to create socket");
  37.         exit(EXIT_FAILURE);
  38.     }
  39.  
  40.     char ip[16];
  41.     scanf("%s", ip);
  42.  
  43.     struct sockaddr_in dest_addr = {0};
  44.     dest_addr.sin_family = AF_INET;
  45.     if (inet_pton(AF_INET, ip, &(dest_addr.sin_addr)) <= 0) {
  46.         perror("Invalid IP");
  47.         exit(EXIT_FAILURE);
  48.     }
  49.  
  50.     for(int ttl = 1;;ttl++)
  51.     {
  52.         // Set the ttl on the socket
  53.         setsockopt(sockfd, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl));
  54.  
  55.         /// Create the ICMP echo request
  56.         struct icmphdr icmp_packet = {0};
  57.         icmp_packet.checksum = 0;
  58.         icmp_packet.type = ICMP_ECHO;
  59.         icmp_packet.code = 0;
  60.         icmp_packet.un.echo.id = htons(getpid());
  61.         icmp_packet.un.echo.sequence = htons(ttl);  
  62.         icmp_packet.checksum = checksum(&icmp_packet, sizeof(icmp_packet));
  63.  
  64.         /// send the packet
  65.         sendto(sockfd, &icmp_packet, sizeof(icmp_packet), 0, (struct sockaddr*)&dest_addr, sizeof(dest_addr));
  66.  
  67.         /// receiving the response
  68.         char packet[1500];
  69.         struct sockaddr_in sourceAddress;
  70.         socklen_t sourceLength = sizeof(sourceAddress);
  71.  
  72.         ssize_t recv_len = recvfrom(sockfd, packet, sizeof(packet), 0, (struct sockaddr*)&sourceAddress, &sourceLength);
  73.  
  74.         if (recv_len < 0) {
  75.             // timeout or no response
  76.             continue;
  77.         }
  78.  
  79.         // ughh??
  80.         struct iphdr *ip_header = (struct iphdr *) packet;
  81.         struct icmphdr *icmp_response = (struct icmphdr *)(packet + ip_header->ihl * 4);
  82.  
  83.         // this is black magic
  84.          char src_ip[INET_ADDRSTRLEN];
  85.         inet_ntop(AF_INET, &(ip_header->saddr), src_ip, INET_ADDRSTRLEN);
  86.         printf("%s\n", src_ip);
  87.  
  88.         // stopping if we find the echo reply
  89.         if (icmp_response->type == ICMP_ECHOREPLY) {
  90.             break;
  91.         }
  92.     }
  93.  
  94.     close(sockfd);
  95.     return 0;
  96. }
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement