1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
#include <openssl/ssl.h>
#include <string.h>
#include <time.h>
#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;
}
|