Repository navigation
Expand file tree
/
Copy pathbuffer_server.c
More file actions
76 lines (68 loc) · 1.79 KB
/
Copy pathbuffer_server.c
File metadata and controls
76 lines (68 loc) · 1.79 KB
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
/* Copyright (c) 2019-2022 Homa Developers
* SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0+
*/
/* This is a test program used together with buffer_client.c to learn about
* how TCP handles buffer exhaustion. This program opens accepts connections
* on a given port, but it never reads any incoming data, so buffers will
* pile up in the kernel.
*
* Usage:
* buffer_server port
*/
#include <errno.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include "homa.h"
#include "test_utils.h"
int main(int argc, char** argv) {
int fd, port;
int optval = 1;
union sockaddr_in_union bindAddress;
if (argc < 2) {
printf("Usage: %s port\n", argv[0]);
exit(1);
}
port = strtol(argv[1], NULL, 10);
if (port == 0) {
printf("Bad port number %s; must be integer\n",
argv[1]);
exit(1);
}
fd = socket(PF_INET, SOCK_STREAM, 0);
if (fd < 0) {
printf("Couldn't create socket: %s\n", strerror(errno));
exit(1);
}
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval,
sizeof(optval)) != 0) {
printf("Couldn't set SO_REUSEADDR: %s\n", strerror(errno));
exit(1);
}
bindAddress.in4.sin_family = AF_INET;
bindAddress.in4.sin_port = htons(port);
bindAddress.in4.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(fd, &bindAddress.sa, sizeof(bindAddress.in4))
!= 0) {
printf("Couldn't bind to port %d\n: %s\n", port, strerror(errno));
exit(1);
}
if (listen(fd, 1000000) != 0) {
printf("Listen failed on socket: %s\n", strerror(errno));
exit(1);
}
while (1) {
int peerFd;
peerFd = accept(fd, NULL, NULL);
if (peerFd < 0) {
printf("Accept failed: %s\n", strerror(errno));
} else {
printf("Accepted connection on fd %d\n", peerFd);
}
}
exit(0);
}