-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp-mss.c
223 lines (187 loc) · 6.65 KB
/
tcp-mss.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include <ctype.h>
#include <getopt.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>
#include <sys/param.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <unistd.h>
int handle_opts(int argc, char* argv[], bool* is_server, char** client_addr) {
char* argv0 = argv[0];
bool clientflag = false;
int index;
int c;
opterr = 0;
while ((c = getopt(argc, argv, "hsc:")) != -1) {
switch (c) {
case 'h':
printf("Usage: %s [-h] [-s | -c <IP>]\n", argv0);
exit(0);
case 's':
if (clientflag != false) {
fputs("ti cho ebanulsa", stderr);
return 2;
}
*is_server = true;
break;
case 'c':
if (*is_server != false) {
fputs("ti cho ebanulsa", stderr);
return 2;
}
clientflag = true;
strcpy(*client_addr, optarg);
break;
case '?':
if (optopt == 'c') fprintf(stderr, "Option `-%c` requires an argument.\n", optopt);
else if (isprint(optopt)) fprintf(stderr, "Unknown option `-%c`.\n", optopt);
else fprintf(stderr, "Unknown option character `\\x%x`.\n", optopt);
return 1;
default:
abort();
}
}
if (!*is_server && !clientflag) {
fprintf(stderr, "Usage: %s [-h] [-s | -c <IP>]\n", argv0);
return 1;
}
return 0;
}
int get_address_family(const char* addr) {
char buf[16];
if (inet_pton(AF_INET, addr, buf)) return AF_INET;
if (inet_pton(AF_INET6, addr, buf)) return AF_INET6;
return -1;
}
int server_mode() {
int v4_sock_fd = socket(AF_INET, SOCK_STREAM, 0);
if (v4_sock_fd == -1) {
fputs("Could not create AF_INET socket.", stderr);
return 3;
}
int v6_sock_fd = socket(AF_INET6, SOCK_STREAM, 0);
if (v6_sock_fd == -1) {
fputs("Could not create AF_INET6 socket.", stderr);
return 3;
}
struct sockaddr_in v4_server;
v4_server.sin_family = AF_INET;
v4_server.sin_addr.s_addr = INADDR_ANY;
v4_server.sin_port = 3123;
if (bind(v4_sock_fd, (struct sockaddr*)&v4_server, sizeof(v4_server)) < 0) {
fputs("Could not bind IPv4 socket.", stderr);
return 4;
}
listen(v4_sock_fd, 1);
puts("Started listening on 0.0.0.0:3123");
struct sockaddr_in6 v6_server;
v6_server.sin6_family = AF_INET6;
v6_server.sin6_addr = in6addr_any;
v6_server.sin6_port = htons(3123);
v6_server.sin6_flowinfo = 0;
if (bind(v6_sock_fd, (struct sockaddr*)&v6_server, sizeof(v6_server)) < 0) {
fputs("Could not bind IPv6 socket.", stderr);
return 4;
}
listen(v6_sock_fd, 1);
puts("Started listening on [::]:3123");
fd_set fds;
while (true) {
FD_ZERO(&fds);
FD_SET(v4_sock_fd, &fds);
FD_SET(v6_sock_fd, &fds);
int code = select(MAX(v4_sock_fd, v6_sock_fd) + 1, &fds, NULL, NULL, NULL);
if (code < 1) {
fprintf(stderr, "Something wrong happened... `select` returned %d", code);
return 5;
}
if (FD_ISSET(v4_sock_fd, &fds)) {
struct sockaddr_in client_name;
socklen_t c = sizeof(client_name);
int client_fd = accept(v4_sock_fd, (struct sockaddr*)&client_name, &c);
char client_addr[INET_ADDRSTRLEN];
const char* _ = inet_ntop(AF_INET, &client_name.sin_addr, client_addr, sizeof(client_addr));
// inet_ntoa(client_name.sin_addr);
printf("Incoming connectiong from %s:%d...", client_addr, client_name.sin_port);
int mss;
socklen_t len = sizeof(mss);
getsockopt(client_fd, IPPROTO_TCP, TCP_MAXSEG, &mss, &len);
printf("TCP MSS is %d.\n", mss);
close(client_fd);
continue;
}
if (FD_ISSET(v6_sock_fd, &fds)) {
struct sockaddr_in6 client_name;
socklen_t c = sizeof(client_name);
int client_fd = accept(v6_sock_fd, (struct sockaddr*)&client_name, &c);
char client_addr[INET6_ADDRSTRLEN];
const char* _ = inet_ntop(AF_INET6, &client_name.sin6_addr, client_addr, sizeof(client_addr));
printf("Incoming connectiong from [%s]:%d...", client_addr, client_name.sin6_port);
int mss;
socklen_t len = sizeof(mss);
getsockopt(client_fd, IPPROTO_TCP, TCP_MAXSEG, &mss, &len);
printf("TCP MSS is %d.\n", mss);
close(client_fd);
continue;
}
}
return 0;
}
int client_mode(const char* client_addr) {
int af_type = get_address_family(client_addr);
if (af_type < 0) {
fputs("Given IP address is not a valid IPv4 nor IPv6 address.", stderr);
return 3;
}
int sock = socket(af_type, SOCK_STREAM, 0);
if (sock < 0) {
fputs("Could not create socket.", stderr);
return 4;
}
struct sockaddr *server = NULL;
socklen_t server_len;
if (af_type == AF_INET) {
size_t size = sizeof(struct sockaddr_in);
struct sockaddr_in* server_v4 = (struct sockaddr_in*)malloc(size);
inet_pton(AF_INET, client_addr, &server_v4->sin_addr);
server_v4->sin_family = AF_INET;
server_v4->sin_port = 3123;
server = (struct sockaddr*)server_v4;
server_len = size;
} else if (af_type == AF_INET6) {
size_t size = sizeof(struct sockaddr_in6);
struct sockaddr_in6* server_v6 = (struct sockaddr_in6*)malloc(size);
inet_pton(AF_INET6, client_addr, &server_v6->sin6_addr);
server_v6->sin6_family = AF_INET6;
server_v6->sin6_port = htons(3123);
server = (struct sockaddr*)server_v6;
server_len = size;
}
if (connect(sock, server, server_len) < 0) {
fputs("Could not connect to server.", stderr);
return 5;
}
printf("Connected to %s:3123...", client_addr);
//char buf[1];
//read(sock, buf, 1);
int mss;
socklen_t len = sizeof(mss);
getsockopt(sock, IPPROTO_TCP, TCP_MAXSEG, &mss, &len);
printf("TCP MSS is %d.\n", mss);
close(sock);
return 0;
}
int main(int argc, char* argv[]) {
char *client_addr = (char*)malloc(40 * sizeof(char));
bool is_server = false;
int handle_args = handle_opts(argc, argv, &is_server, &client_addr);
if (handle_args != 0) return handle_args;
int code = 0;
if (is_server) code = server_mode();
else code = client_mode(client_addr);
return code;
}