Advertisement
LA77

Untitled

May 23rd, 2025
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.98 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. unsigned short checksum (void *b, int len) {
  11.     unsigned short * buf = b;
  12.     unsigned int sum = 0;
  13.     unsigned short result;
  14.    
  15.     for(sum = 0; len > 1; len -= 2)
  16.         sum += * buf ++;
  17.  
  18.     if(len == 1)
  19.         sum += *(unsigned char *) buf ;
  20.  
  21.     sum = (sum >> 16) + (sum & 0xFFFF);
  22.     sum += (sum >> 16);
  23.     result = ~sum;
  24.  
  25.     return result;
  26. }
  27.  
  28. int main()
  29. {
  30.     char ip[16];
  31.     scanf("%s", ip);
  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.  
  41.     // Prepare the ICMP packet
  42.     struct icmphdr icmp_packet = {0};
  43.     memset(&icmp_packet, 0, sizeof(icmp_packet));
  44.  
  45.     // Structure your packet here (e.g., set type, code, checksum, identifier, sequence number)
  46.     icmp_packet.checksum = 0;
  47.     icmp_packet.type = ICMP_ECHO;
  48.     icmp_packet.code = 0;
  49.     icmp_packet.un.echo.id = getpid();
  50.     icmp_packet.un.echo.sequence = 1;
  51.     icmp_packet.checksum = checksum(&icmp_packet, sizeof(icmp_packet));
  52.  
  53.     // Fill in the destination address
  54.     struct sockaddr_in dest_addr = {0};
  55.     dest_addr.sin_family = AF_INET;
  56.    
  57.    
  58.     // Set dest_addr.sin_addr based on input IP
  59.     if (inet_pton(AF_INET, ip, &(dest_addr.sin_addr)) <= 0) {
  60.         perror("Invalid IP address format");
  61.         exit(EXIT_FAILURE);
  62.     }
  63.     // Send the ICMP packet
  64.     int result = sendto(sockfd, &icmp_packet, sizeof(struct icmphdr), 0, (struct sockaddr*)&dest_addr, sizeof(dest_addr));
  65.     if (result < 0) {
  66.         perror("Failed to send ICMP packet");
  67.     // Handle error
  68.     } else {
  69.         printf("ICMP Echo Request sent successfully.\n");
  70.     }
  71.  
  72.  
  73.     char packet[1500]; // A common MTU size is sufficient for buffer
  74.     struct sockaddr_in sourceAddress;
  75.     socklen_t sourceLength = sizeof(sourceAddress);
  76.  
  77.     // Receive the packet
  78.     ssize_t bytes_received = recvfrom(sockfd, packet, sizeof(packet), 0, (struct
  79.     sockaddr*)&sourceAddress, &sourceLength);
  80.  
  81.     if (bytes_received < 0) {
  82.         perror("Failed to receive packet");
  83.         // Handle error
  84.     }
  85.     struct iphdr *ip_header = (struct iphdr *)packet;
  86.     struct icmphdr *icmp_header = (struct icmphdr *)(packet + (ip_header->ihl * 4));
  87.     char src_ip[INET_ADDRSTRLEN], dst_ip[INET_ADDRSTRLEN];
  88.     inet_ntop(AF_INET, &(ip_header->saddr), src_ip, INET_ADDRSTRLEN);
  89.     inet_ntop(AF_INET, &(ip_header->daddr), dst_ip, INET_ADDRSTRLEN);
  90.  
  91.     printf("Destination Address: %s\n", dst_ip);
  92.     printf("Source Address: %s\n", src_ip);
  93.     printf("TTL: %d\n", ip_header->ttl);
  94.     printf("Protocol: %d\n", ip_header->protocol);
  95.     printf("Total Length: %d\n", ntohs(ip_header->tot_len));
  96.     printf("Version: %d\n", ip_header->version);
  97.  
  98.     close(sockfd);
  99.     return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement