#include #include #include #include "config.h" #include "connect.h" #include "net.h" #include "request.h" #include "response.h" #include "status.h" int send_request(struct config *conf, struct request *req) { char *req_string = request_to_string(req); int rc = SSL_write(conf->s.conn->ssl, req_string, strlen(req_string)); if (rc < 0) { return SSL_SEND_ERROR; } free(req_string); return 0; } int read_response(struct config *conf, struct response *res) { struct timespec start_time; clock_gettime(CLOCK_MONOTONIC, &start_time); int buf_len = 4096; char *buf = malloc(buf_len); int bytes_read = 0; do { bytes_read = SSL_read(conf->s.conn->ssl, buf, buf_len); if (bytes_read == buf_len) { buf_len *= 2; char *temp_buf = realloc(buf, buf_len); if (temp_buf == NULL) return ALLOC_ERROR; buf = temp_buf; } } while (bytes_read > 0); int rc = parse_response(res, buf); if (rc < 0) { return RESPONSE_PARSE_ERROR; } free(buf); struct timespec end_time; clock_gettime(CLOCK_MONOTONIC, &end_time); float time_diff = (end_time.tv_sec - start_time.tv_sec) + 1e-9 * (end_time.tv_nsec - start_time.tv_nsec); int msg_len = snprintf(NULL, 0, "Received after %0.3f seconds", time_diff) + 1; char *msg = malloc(msg_len); snprintf(msg, msg_len, "Received after %.3f seconds", time_diff); update_status(conf, msg); free(msg); return 0; }