fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <sys/socket.h>
  6. #include <arpa/inet.h>
  7. #include <netinet/ip.h>
  8. #include <netinet/ip_icmp.h>
  9.  
  10. int main(int argc, char *argv)
  11. {
  12. char dst_addr[] = "93.184.216.34";
  13. short dst_port = 80;
  14. int icmp_socket;
  15. struct sockaddr_in addr;
  16. struct iphdr ip_header;
  17. struct icmphdr icmp_header;
  18. int len = sizeof(struct iphdr) + sizeof(struct icmphdr) + 64;
  19. char data[len];
  20. char recv_data[len];
  21. socklen_t recv_len;
  22.  
  23. addr.sin_family = AF_INET;
  24. addr.sin_port = htons(dst_port);
  25. addr.sin_addr.s_addr = inet_addr(dst_addr);
  26.  
  27. icmp_socket = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
  28. if (icmp_socket == -1) {
  29. perror("socket");
  30. exit(EXIT_FAILURE);
  31. }
  32.  
  33. memset(&ip_header, 0, sizeof(struct iphdr));
  34. ip_header.version = IPVERSION;
  35. ip_header.tos = IPTOS_ECN_NOT_ECT;
  36. ip_header.id = 0;
  37. ip_header.frag_off = 1;
  38. ip_header.ttl = 32;
  39. ip_header.protocol = IPPROTO_ICMP;
  40. ip_header.saddr = 0;
  41. ip_header.daddr = addr.sin_addr.s_addr;
  42.  
  43. memset(&icmp_header, 0, sizeof(struct icmphdr));
  44. icmp_header.type = ICMP_ECHO;
  45. icmp_header.checksum = 0x00;
  46.  
  47. memcpy(data, &ip_header, sizeof(struct iphdr));
  48. memcpy(data + sizeof(struct iphdr), &icmp_header, sizeof(struct icmphdr));
  49. memcpy(data + len - 64, (char *) "Test...Test...Test...", 64);
  50.  
  51. printf("Sending...");
  52. sendto(icmp_socket, data, len, 0, (struct sockaddr *) &addr, sizeof(struct sockaddr));
  53. printf("Done!\n");
  54. printf("Receiving...");
  55. recvfrom(icmp_socket, recv_data, len, 0, (struct sockaddr *) &addr, &recv_len);
  56. printf("Done!\n");
  57.  
  58.  
  59.  
  60. exit(EXIT_SUCCESS);
  61. }
  62.  
Runtime error #stdin #stdout #stderr 0s 2156KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
socket: Operation not permitted