Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.69 KB | None | 0 0
  1. static void self_thread(void)
  2. {
  3.     int send_status = -1;
  4.     int recv_status = -1;
  5.     int poll_status = 0;
  6.     int ssl_status = 0;
  7.  
  8.     int recv_size = 8192;
  9.     char recv_data[recv_size];
  10.  
  11.     struct pollfd pfds[args->concurrency];
  12.  
  13.     struct PollSSL *pssls = malloc(sizeof(struct PollSSL) * args->concurrency + 1);
  14.  
  15.     State states[args->concurrency];
  16.  
  17.     #ifdef RFORK
  18.         uint32_t timestamp = monotonic_time();
  19.         uint32_t last_measure = timestamp;
  20.     #endif
  21.  
  22.     while (1)
  23.     {  
  24.         #ifdef RTHREADING
  25.             poll_status = poll(pfds, args->concurrency, 0);
  26.             if (poll_status == -1)
  27.             {
  28.                 error("poll", 1);
  29.             }
  30.         #elif defined RFORK
  31.             poll_status = poll(pfds, args->concurrency, (last_measure + 1000 - timestamp));
  32.             timestamp = monotonic_time();
  33.             if (last_measure < timestamp - 1000)
  34.             {
  35.                 last_measure = timestamp;
  36.                 if ( rs != 0)
  37.                 {
  38.                     fprintf(stdout, ANSI_COLOR_RED"Fork id:"ANSI_COLOR_GREEN" %d"ANSI_COLOR_GREEN" \t%d"ANSI_COLOR_RED" send/s"ANSI_COLOR_GREEN" \t %d "ANSI_COLOR_RED"recv/s \t Active sockets:"ANSI_COLOR_GREEN" %d"ANSI_COLOR_RED" \t Max sockets:"ANSI_COLOR_GREEN" %d"ANSI_COLOR_RESET"\n", fork_id, rs, rv, active_sockets, args->threads);
  39.                 }
  40.                 rs = 0;
  41.                 rv = 0;
  42.             }  
  43.         #endif
  44.  
  45.         for (size_t i = 0; i < args->concurrency; i++)
  46.         {
  47.             struct pollfd *pfd = &pfds[i];
  48.             int *sockfd = &pfd->fd;
  49.             State *state = &states[i];
  50.  
  51.             struct PollSSL *pssl = &pssls[i];
  52.             SSL_CTX **ctx = &pssl->ctx;
  53.             SSL **ssl = &pssl->ssl;
  54.  
  55.             switch (state->mode)
  56.             {
  57.  
  58.                 case STATE_CREATE:
  59.                 {
  60.                     if ( (*sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  61.                     {
  62.                         #ifdef DEBUG
  63.                             fprintf(stdout, "[%d] ", i + 1);
  64.                             error("socket", 0);
  65.                         #endif
  66.                         break;
  67.                     }
  68.                     active_sockets++;
  69.                     state->mode = STATE_SETFLAGS;
  70.                     break;
  71.                 }
  72.  
  73.                 case STATE_SETFLAGS:
  74.                 {
  75.                     int flags = fcntl(*sockfd, F_GETFL, 0);
  76.                     flags = flags | O_NONBLOCK;
  77.                     if ( (fcntl(*sockfd, F_SETFL, flags)) == -1)
  78.                     {
  79.                         #ifdef DEBUG
  80.                             fprintf(stdout, "[%d] ", i + 1);
  81.                             error("fcntl", 1);
  82.                         #endif
  83.  
  84.                         state->mode = STATE_CREATE;
  85.                         pfd->events = 0;
  86.                         active_sockets--;
  87.                         close(*sockfd);
  88.                         break;
  89.                     }
  90.  
  91.                     state->mode = STATE_CONNECT;
  92.                     break;
  93.                 }
  94.  
  95.                 case STATE_CONNECT:
  96.                 {
  97.  
  98.                     if ( (connect(*sockfd, (struct sockaddr *)&server, sizeof(server))) == -1 )
  99.                     {
  100.                         if (errno != EINPROGRESS)
  101.                         {
  102.                             state->mode = STATE_CREATE;
  103.                             pfd->events = 0;
  104.                             close_connections(*sockfd, *ssl, *ctx);
  105.                             continue;
  106.                         }
  107.                         #ifdef DEBUG
  108.                             fprintf(stdout, "[%d] ", i + 1);
  109.                             error("connect", 0);
  110.                         #endif
  111.                     }
  112.  
  113.                     if ( !(strcmp(args->site.scheme, "https")) )
  114.                     {
  115.                         state->mode = STATE_CREATESSL;
  116.                     }
  117.                     else
  118.                     {
  119.                         state->requests = 0;
  120.                         pfd->events = POLLOUT;
  121.                         state->mode = STATE_SEND;
  122.                     }
  123.                     break;
  124.                 }
  125.  
  126.                 case STATE_CREATESSL:
  127.                 {
  128.                     *ctx = InitCTX();
  129.                     *ssl = SSL_new(*ctx);   /* create new SSL connection state */
  130.                     // SSL_set_verify(*ssl, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, NULL);
  131.                     SSL_set_tlsext_host_name(*ssl, args->site.domain);
  132.                     SSL_set_fd(*ssl, *sockfd);    /* attach the socket descriptor */
  133.                     // SSL_set_rfd(*ssl, *sockfd);
  134.                     // SSL_set_wfd(*ssl, *sockfd);
  135.  
  136.                     state->mode = STATE_CONNECTSSL;
  137.                     break;
  138.                 }
  139.  
  140.                 case STATE_CONNECTSSL:
  141.                 {
  142.                     ssl_status = SSL_connect(*ssl); // Crashing if concurrency > 28
  143.                    
  144.                     if ( (ssl_status = SSL_connect(*ssl)) == -1 )
  145.                     {
  146.                         #ifdef DEBUG
  147.                             ERR_print_errors_fp(stderr);
  148.                         #endif
  149.                         // fd_set fds;
  150.                         // FD_ZERO(&fds);
  151.                         // FD_SET(*sockfd, &fds);
  152.                         switch (SSL_get_error(*ssl, ssl_status))
  153.                         {
  154.                             case SSL_ERROR_WANT_READ:
  155.                             {
  156.                                 #ifdef DEBUG
  157.                                     printf("SSL_ERROR_WANT_READ\n");
  158.                                 #endif
  159.                                 // close_connections(777, *ssl, *ctx);
  160.                                 // state->mode = STATE_CREATESSL;
  161.                                 // select(*sockfd + 1, &fds, NULL, NULL, NULL);
  162.                                 *sockfd++;
  163.                                
  164.                                 break;
  165.                             }
  166.  
  167.                             case SSL_ERROR_WANT_WRITE:
  168.                             {
  169.                                 #ifdef DEBUG
  170.                                     printf("SSL_ERROR_WANT_WRITE\n");
  171.                                 #endif
  172.                                 // close_connections(777, *ssl, *ctx);
  173.                                 // state->mode = STATE_CREATESSL;
  174.                                 // select(*sockfd + 1, NULL, &fds, NULL, NULL);
  175.                                 *sockfd++;
  176.                                 break;
  177.                             }
  178.  
  179.                             default:
  180.                             {
  181.                                 state->mode = STATE_CREATE;
  182.                                 pfd->events = 0;
  183.                                 close_connections(*sockfd, *ssl, *ctx);
  184.                             }
  185.                         }
  186.                     }
  187.                     else
  188.                     {
  189.                         // ShowCerts(*ssl);        // get any certs
  190.  
  191.                         state->requests = 0;
  192.                         pfd->events = POLLOUT;
  193.                         state->mode = STATE_SEND;
  194.                     }
  195.                     break;
  196.                 }
  197.  
  198.                 case STATE_SEND:
  199.                 {
  200.                     if (pfd->revents & POLLOUT)
  201.                     {  
  202.                         char *packet = form_packet();
  203.                         if ( !(strcmp(args->site.scheme, "https")) )
  204.                         {
  205.                             send_status = SSL_write(*ssl, packet, strlen(packet));
  206.                         }
  207.                         else
  208.                         {
  209.                             send_status = send(*sockfd, packet, strlen(packet), MSG_NOSIGNAL);
  210.                         }
  211.  
  212.                         if ( send_status > 0)
  213.                         {
  214.                             rs++;
  215.                             state->requests++;
  216.                             pfd->events = POLLIN;
  217.                             state->mode = STATE_RECEIVE;
  218.                         }
  219.                         else
  220.                         {
  221.                             #ifdef DEBUG
  222.                                 fprintf(stdout, "[%d] ", i + 1);
  223.                                 error("send", 0);
  224.                             #endif
  225.  
  226.                             state->mode = STATE_CREATE;
  227.                             pfd->events = 0;
  228.                             close_connections(*sockfd, *ssl, *ctx);
  229.                         }
  230.                         free(packet);
  231.                     }
  232.                     break;
  233.                 }
  234.  
  235.                 case STATE_RECEIVE:
  236.                 {
  237.                     if (pfd->revents & POLLIN)
  238.                     {
  239.  
  240.                         while( 1 )
  241.                         {
  242.                             if ( !(strcmp(args->site.scheme, "https")) )
  243.                             {
  244.                                 recv_status = SSL_read(*ssl, recv_data, recv_size);
  245.                             }
  246.                             else
  247.                             {
  248.                                 recv_status = recv(*sockfd, recv_data, recv_size, MSG_NOSIGNAL);
  249.                             }
  250.                            
  251.                             if ( recv_status > 0 )
  252.                             {
  253.                                 rv++;
  254.                                 recv_data[recv_status] = '\0';
  255.                                 // fprintf(stdout, "%s", recv_data);
  256.                                 // memset(recv_data, 0, sizeof(recv_data));
  257.                                
  258.                                 if ( recv_status < recv_size )
  259.                                 {
  260.                                     break;
  261.                                 }
  262.                             }
  263.                             else
  264.                             {
  265.                                 #ifdef DEBUG
  266.                                     fprintf(stdout, "[%d] ", i + 1);
  267.                                     error("recv", 0);
  268.                                 #endif
  269.                                 break;
  270.                             }
  271.                         }
  272.  
  273.                         if ( !(args->mlp) || state->requests < args->mlp)
  274.                         {
  275.                             pfd->events = POLLOUT;
  276.                             state->mode = STATE_SEND;
  277.                         }
  278.                         else
  279.                         {
  280.                             state->mode = STATE_CREATE;
  281.                             pfd->events = 0;
  282.                             close_connections(*sockfd, *ssl, *ctx);
  283.                         }
  284.                     }
  285.                     break;
  286.                 }
  287.  
  288.                 default:
  289.                 {
  290.                     state->mode = STATE_CREATE;
  291.                     pfd->events = 0;
  292.                 }
  293.             }
  294.         }
  295.     }
  296.     free(pssls);
  297. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement