-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathcommon.c
134 lines (109 loc) · 2.89 KB
/
common.c
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright (c) 2016, Yahoo Inc.
// Copyrights licensed under the New BSD License. See the
// accompanying LICENSE.txt file for terms.
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <common.h>
input_handle_t in_handle;
void init_stats(stats_t *st)
{
memset(st, 0, sizeof(stats_t));
gettimeofday(&st->start_time, NULL);
}
ssize_t ts_get_line_input(input_handle_t *handle, char *line, size_t len)
{
ssize_t read = 0;
line[0] = 0;
if ((handle->eof) || (handle->fp == NULL)) {
return -1;
}
if (strlen(handle->host)) {
strncpy(line, handle->host, len);
line[len - 1] = '\0';
handle->eof = true;
return len; // TODO len?
}
while ((read = getline(&line, &len, handle->fp)) != -1) {
// zero-terminate only new lines
if (line[read - 1] == '\n') {
line[read - 1] = 0;
}
// skip empty lines
if (read > 0)
break;
}
if ((read < 1) || (len == 0)) {
handle->eof = true;
fclose(handle->fp);
handle->fp = NULL;
return -1;
}
return read;
}
uint64_t ts_elapsed_time(struct timeval t1)
{
struct timeval t2;
gettimeofday(&t2, NULL);
return ((t2.tv_sec - t1.tv_sec) * 1000000) + (t2.tv_usec - t1.tv_usec);
}
void ts_get_ip(int fd, char *ipstr, size_t ipstr_len)
{
ipstr[0] = 0;
socklen_t len;
struct sockaddr_storage addr;
len = sizeof(addr);
getpeername(fd, (struct sockaddr *) &addr, &len);
if (addr.ss_family == AF_INET) {
struct sockaddr_in *s = (struct sockaddr_in *) &addr;
inet_ntop(AF_INET, &s->sin_addr, ipstr, ipstr_len);
} else { // AF_INET6
struct sockaddr_in6 *s = (struct sockaddr_in6 *) &addr;
inet_ntop(AF_INET6, &s->sin6_addr, ipstr, ipstr_len);
}
}
ts_address_family_t ts_address_family(const char *target)
{
if (target[0] == '[') {
return TS_IPV6;
}
size_t len = strlen(target);
if (len == strspn(target, "0123456789.:")) {
return TS_IPV4;
}
return TS_HOSTNAME;
}
void ts_parse_connect_target(const char *target, char *host, size_t hlen, uint16_t *port)
{
size_t len = strlen(target);
// addr should be atleast 4 chars
if (len < 5) {
return;
}
ts_address_family_t addr = ts_address_family(target);
switch (addr) {
case TS_HOSTNAME:
case TS_IPV4:
for (int i=len-1; i>=0; i--) {
if ((target[i] == ':') && (i+1 < len)) {
*port = strtol(target + i + 1, NULL, 10);
snprintf(host, hlen, "%.*s", i, target);
return;
}
}
snprintf(host, OPT_STRLEN, "%s", target);
break;
case TS_IPV6:
for (int i=len-1; i>=0; i--) {
if ((target[i] == ']') && (target[i+1] == ':') && (i+1 < len)) {
*port = strtol(target + i + 2, NULL, 10);
snprintf(host, hlen, "%.*s", i-1, target+1);
return;
}
}
snprintf(host, OPT_STRLEN, "%.*s", (int)len-2, target+1);
break;
default: break;
}
return;
}