Skip to content

Commit cca24cb

Browse files
committed
feat: SYN flag only
1 parent 774015e commit cca24cb

File tree

5 files changed

+33
-11
lines changed

5 files changed

+33
-11
lines changed

src/client.c

+6-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ int main(int argc, char* argv[]) {
145145
vector_init(&open_connections_vector);
146146

147147
if (argc < 5) {
148-
printf("Usage: kcpraw_client SERVER_IP SERVER_PORT LISTEN_IP LISTEN_PORT [--mode MODE] [--key KEY] [--noseq] [--nobpf]\n");
148+
printf("Usage: kcpraw_client SERVER_IP SERVER_PORT LISTEN_IP LISTEN_PORT [--mode MODE] [--key KEY] [--noseq] [--syn-only] [--nobpf]\n");
149149
exit(1);
150150
}
151151

@@ -178,12 +178,17 @@ int main(int argc, char* argv[]) {
178178
packetinfo.on_packet_recv = on_packet_recv;
179179
packetinfo.is_server = 0;
180180
packetinfo.disable_seq_update = 0;
181+
packetinfo.syn_only = 0;
181182

182183
for (int i=0; i<argc; i++) {
183184
if (!strcmp(argv[i], "--noseq")) {
184185
LOG("Disable TCP sequense counter.");
185186
packetinfo.disable_seq_update = 1;
186187
}
188+
if (!strcmp(argv[i], "--syn-only")) {
189+
LOG("Use SYN-only mode");
190+
packetinfo.syn_only = 1;
191+
}
187192
}
188193

189194
strcpy(bind_ip, argv[3]);

src/common.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,9 @@ void reinit_fake_tcp() {
468468
update_src_addr();
469469

470470
init_bpf();
471-
send_packet(&packetinfo, "", 0, FIRST_SYN);
471+
472+
if (!packetinfo.syn_only)
473+
send_packet(&packetinfo, "", 0, FIRST_SYN);
472474
}
473475
#endif
474476

src/server.c

+6-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ int main(int argc, char* argv[]) {
6969
vector_init(&open_connections_vector);
7070

7171
if (argc < 5) {
72-
printf("Usage: kcpraw_server TCP_CONNECT_TO_IP TCP_CONNECT_TO_PORT SERVER_IP SERVER_PORT [--mode MODE] [--key KEY] [--noseq] [--nobpf]\n");
72+
printf("Usage: kcpraw_server TCP_CONNECT_TO_IP TCP_CONNECT_TO_PORT SERVER_IP SERVER_PORT [--mode MODE] [--key KEY] [--noseq] [--syn-only] [--nobpf]\n");
7373
exit(1);
7474
}
7575

@@ -105,12 +105,17 @@ int main(int argc, char* argv[]) {
105105
packetinfo.on_packet_recv = on_packet_recv;
106106
packetinfo.is_server = 1;
107107
packetinfo.disable_seq_update = 0;
108+
packetinfo.syn_only = 0;
108109

109110
for (int i=0; i<argc; i++) {
110111
if (!strcmp(argv[i], "--noseq")) {
111112
LOG("Disable TCP sequense counter.");
112113
packetinfo.disable_seq_update = 1;
113114
}
115+
if (!strcmp(argv[i], "--syn-only")) {
116+
LOG("Use SYN-only mode");
117+
packetinfo.syn_only = 1;
118+
}
114119
}
115120

116121
loop = ev_default_loop(0);

src/trans_packet.c

+17-8
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ void check_packet_recv(struct packet_info* packetinfo) {
164164
}
165165
strcpy(packetinfo->source_ip, inet_ntoa(to_addr));
166166

167-
if (tcph->syn == 1 && tcph->ack == 0 && tcph->psh == 0) {
167+
if ((!packetinfo->syn_only) && tcph->syn == 1 && tcph->ack == 0 && tcph->psh == 0) {
168168
// Server replies SYN + ACK
169169
(packetinfo->state).seq = 1;
170170
(packetinfo->state).ack = 1;
@@ -176,7 +176,7 @@ void check_packet_recv(struct packet_info* packetinfo) {
176176
#endif
177177

178178
#ifndef SERVER
179-
if (tcph->syn == 1 && tcph->ack == 1 && tcph->psh == 0) {
179+
if ((!packetinfo->syn_only) && tcph->syn == 1 && tcph->ack == 1 && tcph->psh == 0) {
180180
//Client replies first ACK
181181
(packetinfo->state).seq = 1;
182182
(packetinfo->state).ack = 1;
@@ -311,12 +311,21 @@ int send_packet(struct packet_info* packetinfo, char* source_payload, int source
311311
tcph->seq = htonl((packetinfo->state).seq);
312312
tcph->ack_seq = htonl((packetinfo->state).ack);
313313
tcph->doff = 5; //tcp header size
314-
tcph->fin=0;
315-
tcph->syn=0;
316-
tcph->rst=0;
317-
tcph->psh=1;
318-
tcph->ack=1;
319-
tcph->urg=0;
314+
if (packetinfo->syn_only) {
315+
tcph->fin=0;
316+
tcph->syn=1;
317+
tcph->rst=0;
318+
tcph->psh=0;
319+
tcph->ack=0;
320+
tcph->urg=0;
321+
} else {
322+
tcph->fin=0;
323+
tcph->syn=0;
324+
tcph->rst=0;
325+
tcph->psh=1;
326+
tcph->ack=1;
327+
tcph->urg=0;
328+
}
320329
tcph->window = htons(129600);
321330
tcph->check = 0; //leave checksum 0 now, filled later by pseudo header
322331
tcph->urg_ptr = 0;

src/trans_packet.h

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ struct packet_info {
2020
int is_server;
2121
struct trans_packet_state state;
2222
int disable_seq_update;
23+
int syn_only;
2324
};
2425

2526
int packet_send_sd;

0 commit comments

Comments
 (0)