fork(1) 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. int tcp_scan(struct addrinfo *addr);
  20. uint16_t checksum(void *data, int length);
  21.  
  22. int main(int argc, char *argv[])
  23. {
  24. struct addrinfo hints;
  25. struct addrinfo *result;
  26. int ret_getaddr;
  27.  
  28. if (argc < 2) {
  29. fprintf(stderr, "Usage: %s host\n", argv[0]);
  30. exit(EXIT_FAILURE);
  31. }
  32.  
  33. hints.ai_family = AF_INET;
  34. hints.ai_socktype = SOCK_STREAM;
  35. hints.ai_protocol = IPPROTO_TCP;
  36. hints.ai_flags = 0;
  37.  
  38. ret_getaddr = getaddrinfo(argv[1], NULL, &hints, &result);
  39. if (ret_getaddr != 0) {
  40. fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ret_getaddr));
  41. exit(EXIT_FAILURE);
  42. }
  43.  
  44. for (struct addrinfo *rp = result; rp != NULL; rp = rp->ai_next) {
  45. tcp_scan(rp);
  46. }
  47.  
  48. exit(EXIT_SUCCESS);
  49. }
  50.  
  51. uint16_t checksum(void *data, int length) {
  52. uint16_t *p = (uint16_t *) data;
  53. uint32_t sum;
  54.  
  55. for (sum = 0; length > 1; length -= 2) {
  56. sum += *p++;
  57. }
  58. if (length == 1) {
  59. sum += *(uint8_t *) p;
  60. }
  61.  
  62. sum = (sum >> 16) + (sum & 0xFFFF);
  63. sum += (sum >> 16);
  64.  
  65. return ~sum;
  66. }
  67.  
  68. int tcp_scan(struct addrinfo *addr) {
  69. struct packet pckt;
  70. int pcktlen = sizeof(struct packet);
  71. int tcp_socket;
  72. ssize_t ret_send, ret_recv;
  73.  
  74. memset(&pckt, 0, sizeof(struct packet));
  75. pckt.hdr.th_sport = htons(rand() % (UINT16_MAX - 1024) + 1024);
  76. pckt.hdr.th_dport = htons(80);
  77. pckt.hdr.th_seq = rand() % UINT32_MAX;
  78. pckt.hdr.syn = 1;
  79. pckt.hdr.th_off = sizeof(struct tcphdr) / 4;
  80. pckt.hdr.th_win = PACKETSIZE;
  81.  
  82. strcpy(pckt.buffer, "Hello, Example Domain!");
  83.  
  84. pckt.hdr.th_sum = checksum(&pckt, pcktlen);
  85.  
  86. tcp_socket = socket(AF_INET, SOCK_RAW, IPPROTO_TCP);
  87. if (tcp_socket == -1) {
  88. perror("tcp_socket");
  89. return 1;
  90. }
  91.  
  92. ret_send = sendto(tcp_socket, &pckt, pcktlen, 0, addr->ai_addr, addr->ai_addrlen);
  93. if (ret_send == -1) {
  94. perror("sendto");
  95. return 1;
  96. }
  97.  
  98. close(tcp_socket);
  99.  
  100. return 0;
  101. }
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:19:21: warning: 'struct addrinfo' declared inside parameter list
 int tcp_scan(struct addrinfo *addr);
                     ^
prog.c:19:21: warning: its scope is only this definition or declaration, which is probably not what you want
prog.c: In function 'main':
prog.c:24:18: error: storage size of 'hints' isn't known
  struct addrinfo hints;
                  ^
prog.c:38:16: warning: implicit declaration of function 'getaddrinfo' [-Wimplicit-function-declaration]
  ret_getaddr = getaddrinfo(argv[1], NULL, &hints, &result);
                ^
prog.c:40:40: warning: implicit declaration of function 'gai_strerror' [-Wimplicit-function-declaration]
   fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ret_getaddr));
                                        ^
prog.c:40: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:44:56: error: dereferencing pointer to incomplete type 'struct addrinfo'
  for (struct addrinfo *rp = result; rp != NULL; rp = rp->ai_next) {
                                                        ^
prog.c:45:12: warning: passing argument 1 of 'tcp_scan' from incompatible pointer type [-Wincompatible-pointer-types]
   tcp_scan(rp);
            ^
prog.c:19:5: note: expected 'struct addrinfo *' but argument is of type 'struct addrinfo *'
 int tcp_scan(struct addrinfo *addr);
     ^
prog.c:24:18: warning: unused variable 'hints' [-Wunused-variable]
  struct addrinfo hints;
                  ^
prog.c: At top level:
prog.c:68:21: warning: 'struct addrinfo' declared inside parameter list
 int tcp_scan(struct addrinfo *addr) {
                     ^
prog.c:68:5: error: conflicting types for 'tcp_scan'
 int tcp_scan(struct addrinfo *addr) {
     ^
prog.c:19:5: note: previous declaration of 'tcp_scan' was here
 int tcp_scan(struct addrinfo *addr);
     ^
prog.c: In function 'tcp_scan':
prog.c:79:27: error: invalid application of 'sizeof' to incomplete type 'struct tcphdr'
  pckt.hdr.th_off = sizeof(struct tcphdr) / 4;
                           ^
prog.c:92: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:72:20: warning: unused variable 'ret_recv' [-Wunused-variable]
  ssize_t ret_send, ret_recv;
                    ^
stdout
Standard output is empty