Skip to content

Commit

Permalink
feat: SYN flag only
Browse files Browse the repository at this point in the history
  • Loading branch information
Chion82 committed Jun 16, 2018
1 parent 774015e commit cca24cb
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 11 deletions.
7 changes: 6 additions & 1 deletion src/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ int main(int argc, char* argv[]) {
vector_init(&open_connections_vector);

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

Expand Down Expand Up @@ -178,12 +178,17 @@ int main(int argc, char* argv[]) {
packetinfo.on_packet_recv = on_packet_recv;
packetinfo.is_server = 0;
packetinfo.disable_seq_update = 0;
packetinfo.syn_only = 0;

for (int i=0; i<argc; i++) {
if (!strcmp(argv[i], "--noseq")) {
LOG("Disable TCP sequense counter.");
packetinfo.disable_seq_update = 1;
}
if (!strcmp(argv[i], "--syn-only")) {
LOG("Use SYN-only mode");
packetinfo.syn_only = 1;
}
}

strcpy(bind_ip, argv[3]);
Expand Down
4 changes: 3 additions & 1 deletion src/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,9 @@ void reinit_fake_tcp() {
update_src_addr();

init_bpf();
send_packet(&packetinfo, "", 0, FIRST_SYN);

if (!packetinfo.syn_only)
send_packet(&packetinfo, "", 0, FIRST_SYN);
}
#endif

Expand Down
7 changes: 6 additions & 1 deletion src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ int main(int argc, char* argv[]) {
vector_init(&open_connections_vector);

if (argc < 5) {
printf("Usage: kcpraw_server TCP_CONNECT_TO_IP TCP_CONNECT_TO_PORT SERVER_IP SERVER_PORT [--mode MODE] [--key KEY] [--noseq] [--nobpf]\n");
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");
exit(1);
}

Expand Down Expand Up @@ -105,12 +105,17 @@ int main(int argc, char* argv[]) {
packetinfo.on_packet_recv = on_packet_recv;
packetinfo.is_server = 1;
packetinfo.disable_seq_update = 0;
packetinfo.syn_only = 0;

for (int i=0; i<argc; i++) {
if (!strcmp(argv[i], "--noseq")) {
LOG("Disable TCP sequense counter.");
packetinfo.disable_seq_update = 1;
}
if (!strcmp(argv[i], "--syn-only")) {
LOG("Use SYN-only mode");
packetinfo.syn_only = 1;
}
}

loop = ev_default_loop(0);
Expand Down
25 changes: 17 additions & 8 deletions src/trans_packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ void check_packet_recv(struct packet_info* packetinfo) {
}
strcpy(packetinfo->source_ip, inet_ntoa(to_addr));

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

#ifndef SERVER
if (tcph->syn == 1 && tcph->ack == 1 && tcph->psh == 0) {
if ((!packetinfo->syn_only) && tcph->syn == 1 && tcph->ack == 1 && tcph->psh == 0) {
//Client replies first ACK
(packetinfo->state).seq = 1;
(packetinfo->state).ack = 1;
Expand Down Expand Up @@ -311,12 +311,21 @@ int send_packet(struct packet_info* packetinfo, char* source_payload, int source
tcph->seq = htonl((packetinfo->state).seq);
tcph->ack_seq = htonl((packetinfo->state).ack);
tcph->doff = 5; //tcp header size
tcph->fin=0;
tcph->syn=0;
tcph->rst=0;
tcph->psh=1;
tcph->ack=1;
tcph->urg=0;
if (packetinfo->syn_only) {
tcph->fin=0;
tcph->syn=1;
tcph->rst=0;
tcph->psh=0;
tcph->ack=0;
tcph->urg=0;
} else {
tcph->fin=0;
tcph->syn=0;
tcph->rst=0;
tcph->psh=1;
tcph->ack=1;
tcph->urg=0;
}
tcph->window = htons(129600);
tcph->check = 0; //leave checksum 0 now, filled later by pseudo header
tcph->urg_ptr = 0;
Expand Down
1 change: 1 addition & 0 deletions src/trans_packet.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ struct packet_info {
int is_server;
struct trans_packet_state state;
int disable_seq_update;
int syn_only;
};

int packet_send_sd;
Expand Down

0 comments on commit cca24cb

Please sign in to comment.