Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static void self_thread(void)
- {
- int send_status = -1;
- int recv_status = -1;
- int poll_status = 0;
- int ssl_status = 0;
- int recv_size = 8192;
- char recv_data[recv_size];
- struct pollfd pfds[args->concurrency];
- struct PollSSL *pssls = malloc(sizeof(struct PollSSL) * args->concurrency + 1);
- State states[args->concurrency];
- #ifdef RFORK
- uint32_t timestamp = monotonic_time();
- uint32_t last_measure = timestamp;
- #endif
- while (1)
- {
- #ifdef RTHREADING
- poll_status = poll(pfds, args->concurrency, 0);
- if (poll_status == -1)
- {
- error("poll", 1);
- }
- #elif defined RFORK
- poll_status = poll(pfds, args->concurrency, (last_measure + 1000 - timestamp));
- timestamp = monotonic_time();
- if (last_measure < timestamp - 1000)
- {
- last_measure = timestamp;
- if ( rs != 0)
- {
- 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);
- }
- rs = 0;
- rv = 0;
- }
- #endif
- for (size_t i = 0; i < args->concurrency; i++)
- {
- struct pollfd *pfd = &pfds[i];
- int *sockfd = &pfd->fd;
- State *state = &states[i];
- struct PollSSL *pssl = &pssls[i];
- SSL_CTX **ctx = &pssl->ctx;
- SSL **ssl = &pssl->ssl;
- switch (state->mode)
- {
- case STATE_CREATE:
- {
- if ( (*sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
- {
- #ifdef DEBUG
- fprintf(stdout, "[%d] ", i + 1);
- error("socket", 0);
- #endif
- break;
- }
- active_sockets++;
- state->mode = STATE_SETFLAGS;
- break;
- }
- case STATE_SETFLAGS:
- {
- int flags = fcntl(*sockfd, F_GETFL, 0);
- flags = flags | O_NONBLOCK;
- if ( (fcntl(*sockfd, F_SETFL, flags)) == -1)
- {
- #ifdef DEBUG
- fprintf(stdout, "[%d] ", i + 1);
- error("fcntl", 1);
- #endif
- state->mode = STATE_CREATE;
- pfd->events = 0;
- active_sockets--;
- close(*sockfd);
- break;
- }
- state->mode = STATE_CONNECT;
- break;
- }
- case STATE_CONNECT:
- {
- if ( (connect(*sockfd, (struct sockaddr *)&server, sizeof(server))) == -1 )
- {
- if (errno != EINPROGRESS)
- {
- state->mode = STATE_CREATE;
- pfd->events = 0;
- close_connections(*sockfd, *ssl, *ctx);
- continue;
- }
- #ifdef DEBUG
- fprintf(stdout, "[%d] ", i + 1);
- error("connect", 0);
- #endif
- }
- if ( !(strcmp(args->site.scheme, "https")) )
- {
- state->mode = STATE_CREATESSL;
- }
- else
- {
- state->requests = 0;
- pfd->events = POLLOUT;
- state->mode = STATE_SEND;
- }
- break;
- }
- case STATE_CREATESSL:
- {
- *ctx = InitCTX();
- *ssl = SSL_new(*ctx); /* create new SSL connection state */
- // SSL_set_verify(*ssl, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, NULL);
- SSL_set_tlsext_host_name(*ssl, args->site.domain);
- SSL_set_fd(*ssl, *sockfd); /* attach the socket descriptor */
- // SSL_set_rfd(*ssl, *sockfd);
- // SSL_set_wfd(*ssl, *sockfd);
- state->mode = STATE_CONNECTSSL;
- break;
- }
- case STATE_CONNECTSSL:
- {
- ssl_status = SSL_connect(*ssl); // Crashing if concurrency > 28
- if ( (ssl_status = SSL_connect(*ssl)) == -1 )
- {
- #ifdef DEBUG
- ERR_print_errors_fp(stderr);
- #endif
- // fd_set fds;
- // FD_ZERO(&fds);
- // FD_SET(*sockfd, &fds);
- switch (SSL_get_error(*ssl, ssl_status))
- {
- case SSL_ERROR_WANT_READ:
- {
- #ifdef DEBUG
- printf("SSL_ERROR_WANT_READ\n");
- #endif
- // close_connections(777, *ssl, *ctx);
- // state->mode = STATE_CREATESSL;
- // select(*sockfd + 1, &fds, NULL, NULL, NULL);
- *sockfd++;
- break;
- }
- case SSL_ERROR_WANT_WRITE:
- {
- #ifdef DEBUG
- printf("SSL_ERROR_WANT_WRITE\n");
- #endif
- // close_connections(777, *ssl, *ctx);
- // state->mode = STATE_CREATESSL;
- // select(*sockfd + 1, NULL, &fds, NULL, NULL);
- *sockfd++;
- break;
- }
- default:
- {
- state->mode = STATE_CREATE;
- pfd->events = 0;
- close_connections(*sockfd, *ssl, *ctx);
- }
- }
- }
- else
- {
- // ShowCerts(*ssl); // get any certs
- state->requests = 0;
- pfd->events = POLLOUT;
- state->mode = STATE_SEND;
- }
- break;
- }
- case STATE_SEND:
- {
- if (pfd->revents & POLLOUT)
- {
- char *packet = form_packet();
- if ( !(strcmp(args->site.scheme, "https")) )
- {
- send_status = SSL_write(*ssl, packet, strlen(packet));
- }
- else
- {
- send_status = send(*sockfd, packet, strlen(packet), MSG_NOSIGNAL);
- }
- if ( send_status > 0)
- {
- rs++;
- state->requests++;
- pfd->events = POLLIN;
- state->mode = STATE_RECEIVE;
- }
- else
- {
- #ifdef DEBUG
- fprintf(stdout, "[%d] ", i + 1);
- error("send", 0);
- #endif
- state->mode = STATE_CREATE;
- pfd->events = 0;
- close_connections(*sockfd, *ssl, *ctx);
- }
- free(packet);
- }
- break;
- }
- case STATE_RECEIVE:
- {
- if (pfd->revents & POLLIN)
- {
- while( 1 )
- {
- if ( !(strcmp(args->site.scheme, "https")) )
- {
- recv_status = SSL_read(*ssl, recv_data, recv_size);
- }
- else
- {
- recv_status = recv(*sockfd, recv_data, recv_size, MSG_NOSIGNAL);
- }
- if ( recv_status > 0 )
- {
- rv++;
- recv_data[recv_status] = '\0';
- // fprintf(stdout, "%s", recv_data);
- // memset(recv_data, 0, sizeof(recv_data));
- if ( recv_status < recv_size )
- {
- break;
- }
- }
- else
- {
- #ifdef DEBUG
- fprintf(stdout, "[%d] ", i + 1);
- error("recv", 0);
- #endif
- break;
- }
- }
- if ( !(args->mlp) || state->requests < args->mlp)
- {
- pfd->events = POLLOUT;
- state->mode = STATE_SEND;
- }
- else
- {
- state->mode = STATE_CREATE;
- pfd->events = 0;
- close_connections(*sockfd, *ssl, *ctx);
- }
- }
- break;
- }
- default:
- {
- state->mode = STATE_CREATE;
- pfd->events = 0;
- }
- }
- }
- }
- free(pssls);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement