fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <string.h>
  5. #include <limits.h>
  6. #include <unistd.h>
  7. #include <sys/socket.h>
  8. #include <netdb.h>
  9. #include <netinet/tcp.h>
  10. #include <arpa/inet.h>
  11.  
  12. #define PACKETSIZE 96
  13.  
  14. struct packet {
  15. struct tcphdr hdr;
  16. char buffer[PACKETSIZE];
  17. };
  18.  
  19. struct pseudo_header {
  20. in_addr_t s_addr;
  21. in_addr_t d_addr;
  22. uint8_t zeroes;
  23. uint8_t protocol;
  24. uint16_t length;
  25. };
  26.  
  27. int tcp_scan(struct addrinfo *addr);
  28. uint16_t checksum(void *data, int length);
  29.  
  30. int main(int argc, char *argv[])
  31. {
  32. struct addrinfo hints;
  33. struct addrinfo *result;
  34. int ret_getaddr;
  35.  
  36. if (argc < 2) {
  37. fprintf(stderr, "Usage: %s host\n", argv[0]);
  38. exit(EXIT_FAILURE);
  39. }
  40.  
  41. hints.ai_family = AF_INET;
  42. hints.ai_socktype = SOCK_STREAM;
  43. hints.ai_protocol = IPPROTO_TCP;
  44. hints.ai_flags = 0;
  45.  
  46. ret_getaddr = getaddrinfo(argv[1], NULL, &hints, &result);
  47. if (ret_getaddr != 0) {
  48. fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ret_getaddr));
  49. exit(EXIT_FAILURE);
  50. }
  51.  
  52. for (struct addrinfo *rp = result; rp != NULL; rp = rp->ai_next) {
  53. tcp_scan(rp);
  54. }
  55.  
  56. exit(EXIT_SUCCESS);
  57. }
  58.  
  59. uint16_t checksum(void *data, int length) {
  60. uint16_t *p = (uint16_t *) data;
  61. uint32_t sum;
  62.  
  63. for (sum = 0; length > 1; length -= 2) {
  64. sum += *p++;
  65. }
  66. if (length == 1) {
  67. sum += *(uint8_t *) p;
  68. }
  69.  
  70. sum = (sum >> 16) + (sum & 0xFFFF);
  71. sum += (sum >> 16);
  72.  
  73. return ~sum;
  74. }
  75.  
  76. int tcp_scan(struct addrinfo *addr) {
  77. struct packet pckt;
  78. int pcktlen = sizeof(struct packet);
  79. int tcp_socket;
  80. ssize_t ret_send, ret_recv;
  81. struct pseudo_header pseudo;
  82.  
  83. memset(&pckt, 0, sizeof(struct packet));
  84. pckt.hdr.th_sport = htons(rand() % (UINT16_MAX - 1024) + 1024);
  85. pckt.hdr.th_dport = htons(80);
  86. pckt.hdr.th_seq = rand() % UINT32_MAX;
  87. pckt.hdr.syn = 1;
  88. pckt.hdr.th_off = sizeof(struct tcphdr) / 4;
  89. pckt.hdr.th_win = PACKETSIZE;
  90.  
  91. strcpy(pckt.buffer, "Hello, Example Domain!");
  92.  
  93. pseudo.s_addr = inet_addr("192.168.1.2");
  94. pseudo.d_addr = inet_addr("93.184.216.34");
  95. pseudo.protocol = IPPROTO_TCP;
  96. pseudo.zeroes = 0;
  97. pseudo.length = htons( pcktlen);
  98.  
  99. pckt.hdr.th_sum = checksum(&pckt, pcktlen) + checksum(&pseudo, sizeof(pseudo));
  100.  
  101. tcp_socket = socket(AF_INET, SOCK_RAW, IPPROTO_TCP);
  102. if (tcp_socket == -1) {
  103. perror("tcp_socket");
  104. return 1;
  105. }
  106.  
  107. ret_send = sendto(tcp_socket, &pckt, pcktlen, 0, addr->ai_addr, addr->ai_addrlen);
  108. if (ret_send == -1) {
  109. perror("sendto");
  110. return 1;
  111. }
  112.  
  113. close(tcp_socket);
  114.  
  115. return 0;
  116. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c:15:16: error: field 'hdr' has incomplete type
  struct tcphdr hdr;
                ^
prog.c:27:21: warning: 'struct addrinfo' declared inside parameter list
 int tcp_scan(struct addrinfo *addr);
                     ^
prog.c:27:21: warning: its scope is only this definition or declaration, which is probably not what you want
prog.c: In function 'main':
prog.c:32:18: error: storage size of 'hints' isn't known
  struct addrinfo hints;
                  ^
prog.c:46:16: warning: implicit declaration of function 'getaddrinfo' [-Wimplicit-function-declaration]
  ret_getaddr = getaddrinfo(argv[1], NULL, &hints, &result);
                ^
prog.c:48:40: warning: implicit declaration of function 'gai_strerror' [-Wimplicit-function-declaration]
   fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ret_getaddr));
                                        ^
prog.c:48:19: warning: format '%s' expects argument of type 'char *', but argument 3 has type 'int' [-Wformat=]
   fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ret_getaddr));
                   ^
prog.c:52:56: error: dereferencing pointer to incomplete type 'struct addrinfo'
  for (struct addrinfo *rp = result; rp != NULL; rp = rp->ai_next) {
                                                        ^
prog.c:53:12: warning: passing argument 1 of 'tcp_scan' from incompatible pointer type [-Wincompatible-pointer-types]
   tcp_scan(rp);
            ^
prog.c:27:5: note: expected 'struct addrinfo *' but argument is of type 'struct addrinfo *'
 int tcp_scan(struct addrinfo *addr);
     ^
prog.c:32:18: warning: unused variable 'hints' [-Wunused-variable]
  struct addrinfo hints;
                  ^
prog.c: At top level:
prog.c:76:21: warning: 'struct addrinfo' declared inside parameter list
 int tcp_scan(struct addrinfo *addr) {
                     ^
prog.c:76:5: error: conflicting types for 'tcp_scan'
 int tcp_scan(struct addrinfo *addr) {
     ^
prog.c:27:5: note: previous declaration of 'tcp_scan' was here
 int tcp_scan(struct addrinfo *addr);
     ^
prog.c: In function 'tcp_scan':
prog.c:88:27: error: invalid application of 'sizeof' to incomplete type 'struct tcphdr'
  pckt.hdr.th_off = sizeof(struct tcphdr) / 4;
                           ^
prog.c:107:55: error: dereferencing pointer to incomplete type 'struct addrinfo'
  ret_send = sendto(tcp_socket, &pckt, pcktlen, 0, addr->ai_addr, addr->ai_addrlen);
                                                       ^
prog.c:80:20: warning: unused variable 'ret_recv' [-Wunused-variable]
  ssize_t ret_send, ret_recv;
                    ^
stdout
Standard output is empty