-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtcp.c
More file actions
92 lines (75 loc) · 3.5 KB
/
tcp.c
File metadata and controls
92 lines (75 loc) · 3.5 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
* name: tcp.c
* this file is part of the "Net-Injector" project. It defines the write_tcp() function
* Copyright (C) 2002 by Jean Philippe GUILLEMIN <jp.guillemin@free.fr>
* license: This software is under GPL license
* date: 04 17 2003
* rev: 0.8
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Source Port | Destination Port |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Sequence Number |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Acknowledgment Number |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Data | |U|A|P|R|S|F| |
| Offset| Reserved |R|C|S|S|Y|I| Window |
| | |G|K|H|T|N|N| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Checksum | Urgent Pointer |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Options | Padding |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| data |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/
#include "neti.h"
/* function that create the TCP header and data field
the result is a pointer (*tcp_packet) to the begining of TCP header
*/
int write_tcp(char *tcp_packet, int size, u_int32_t ip_saddr, u_int32_t ip_daddr){
struct tcphdr *tcph;
struct pseudohdr *pseudoh;
unsigned char *tstamp;
size = TCPHDR_SIZE + myhdr->tcp_opt_size + myhdr->data_size;
pseudoh = (struct pseudohdr*) tcp_packet; /* address of pseudo-header */
tcph = (struct tcphdr*) (tcp_packet+PSEUDOHDR_SIZE); /* address of tcp-header */
tstamp = (unsigned char*) (tcp_packet+PSEUDOHDR_SIZE+TCPHDR_SIZE); /* address of timestamp */
data = (char*) (tcp_packet+PSEUDOHDR_SIZE+TCPHDR_SIZE+myhdr->tcp_opt_size); /* address of begining of payload */
memset(data, 0, myhdr->data_size);
memset(tcp_packet, 0, PSEUDOHDR_SIZE+size);
memcpy (data, payload, myhdr->data_size);
/*filling TCP header */
tcph->syn=myhdr->syn;
tcph->ack=myhdr->ack;
tcph->psh=myhdr->psh;
tcph->rst=myhdr->rst;
tcph->fin=myhdr->fin;
tcph->urg=myhdr->urg;
tcph->source = myhdr->inet_src_port;
tcph->dest = myhdr->inet_dst_port;
tcph->seq = random ();
tcph->ack_seq = random ();
/*tcph->th_x2 = 0; not used */
tcph->doff = 5 + myhdr->tcp_opt_size/4; /* size of TCP header in x32 bits */
tcph->window = htons (myhdr->window); /* max window */
tcph->check = 0; /* checksum will be set later */
tcph->urg_ptr = 0;
/*filling PSEUDO header */
memcpy(&pseudoh->saddr, &ip_saddr, 4);
memcpy(&pseudoh->daddr, &ip_daddr, 4);
pseudoh->protocol = 6; /* layer 4 protocole */
pseudoh->lenght = htons(size);
if (myhdr->tcp_opt_size) {
u_int32_t randts = rand() ^ (rand() << 16);
tstamp[0] = tstamp[1] = 1; /* NOOP */
tstamp[2] = 8;
tstamp[3] = 10; /* 10 bytes, kind+len+T1+T2 */
memcpy(tstamp+4, &randts, 4); /* random */
memset(tstamp+8, 0, 4); /* zero */
}
tcph->check = sum((u_int16_t *) tcp_packet, PSEUDOHDR_SIZE + size);
return EXIT_SUCCESS;
}