-
Notifications
You must be signed in to change notification settings - Fork 11
/
testtcp.c
164 lines (135 loc) · 2.89 KB
/
testtcp.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/tcp.h>
#include <netdb.h>
#include <netinet/in.h>
#include <ldns/ldns.h>
void usage()
{
fprintf(stderr, "usage: testtcp <host> <port>\n");
exit(EXIT_FAILURE);
}
int test(struct sockaddr_in *addr, void *out, size_t outlen, void *in, size_t *inlen)
{
int reuse = 1;
int done = 0;
uint16_t msglen = htons(outlen);
#if 0
int delay = 1;
struct iovec vec[2] = {
{ &msglen, sizeof (msglen) },
{ out, outlen }
};
#endif
int s = socket(PF_INET, SOCK_STREAM, 0);
if (s < 0) {
perror("socket");
return -1;
}
/* allow socket re-use */
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse))) {
perror("setsockopt");
}
if (connect(s, (struct sockaddr *)addr, sizeof(*addr)) < 0) {
perror("connect");
return -1;
}
#if 0
/* write packet length and output buffer */
if (writev(s, vec, 2) < 0) {
perror("writev");
return -1;
}
/* flush socket */
if (setsockopt(s, IPPROTO_TCP, TCP_NODELAY, &delay, sizeof(delay))) {
perror("setsockopt");
}
#else
if (write(s, &msglen, sizeof(msglen)) < 0) {
perror("write");
return -1;
}
done = 0;
while (done < outlen) {
int r = write(s, out + done, outlen - done);
if (r < 0) {
perror("write");
return -1;
}
done += r;
}
#endif
/* read returned packet length */
if (read(s, &msglen, sizeof(msglen)) < 0) {
perror("read");
return -1;
}
/* read the rest of the packet */
*inlen = ntohs(msglen);
done = 0;
while (done < *inlen) {
int r = read(s, in + done, *inlen - done);
if (r < 0) {
perror("read");
return -1;
}
done += r;
}
shutdown(s, SHUT_RDWR);
close(s);
return 0;
}
void loop(struct sockaddr_in *addr)
{
int i = 1;
size_t outlen, inlen;
uint8_t *outbuf;
uint8_t inbuf[LDNS_MAX_PACKETLEN];
struct timeval tv1, tv2;
ldns_pkt *outpkt;
gettimeofday(&tv1, NULL);
ldns_pkt_query_new_frm_str(&outpkt, "www.google.co.uk",
LDNS_RR_TYPE_A, LDNS_RR_CLASS_IN, 0);
while (1) {
ldns_pkt_set_id(outpkt, rand());
ldns_pkt2wire(&outbuf, outpkt, &outlen);
if (test(addr, outbuf, outlen, inbuf, &inlen) < 0) {
return ;
}
i++;
free(outbuf);
gettimeofday(&tv2, NULL);
if (tv2.tv_sec - tv1.tv_sec == 5 &&
tv2.tv_usec > tv1.tv_usec) {
break;
}
}
ldns_pkt_free(outpkt);
fprintf(stdout, "%5d packets handled\n", i++);
fprintf(stdout, "rate = %d\n", i / 5);
}
int main(int argc, char *argv[])
{
char *host;
int port;
struct hostent *hostent;
struct sockaddr_in addr;
if (argc != 3) {
usage();
}
host = argv[1];
port = atoi(argv[2]);
if ((hostent = gethostbyname(host)) == NULL) {
herror("gethostbyname");
return EXIT_FAILURE;
}
memset(&addr, 0, sizeof(addr));
memcpy(&addr.sin_addr, hostent->h_addr, sizeof(addr.sin_addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
loop(&addr);
return EXIT_SUCCESS;
}