forked from CAIDA/mper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscamper_tcp4.c
197 lines (168 loc) · 4.35 KB
/
scamper_tcp4.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
/*
* scamper_tcp4.c
*
* $Id: scamper_tcp4.c,v 1.45 2010/09/11 22:10:42 mjl Exp $
*
* Copyright (C) 2005-2010 The University of Waikato
* Author: Matthew Luckie
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 2.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#ifndef lint
static const char rcsid[] =
"$Id: scamper_tcp4.c,v 1.45 2010/09/11 22:10:42 mjl Exp $";
#endif
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "internal.h"
#include "scamper_addr.h"
#include "scamper_dl.h"
#include "scamper_probe.h"
#include "scamper_ip4.h"
#include "scamper_tcp4.h"
#include "scamper_debug.h"
#include "utils.h"
static void tcp_mss(uint8_t *buf, uint16_t mss)
{
buf[0] = 2;
buf[1] = 4;
bytes_htons(buf+2, mss);
return;
}
static void tcp_cksum(scamper_probe_t *probe, struct tcphdr *tcp, size_t len)
{
uint16_t *w;
int sum = 0;
/*
* the TCP checksum includes a checksum calculated over a psuedo header
* that includes the src and dst IP addresses, the protocol type, and
* the TCP length.
*/
w = probe->pr_ip_src->addr;
sum += *w++; sum += *w++;
w = probe->pr_ip_dst->addr;
sum += *w++; sum += *w++;
sum += htons(len);
sum += htons(IPPROTO_TCP);
/* compute the checksum over the body of the TCP message */
w = (uint16_t *)tcp;
while(len > 1)
{
sum += *w++;
len -= 2;
}
if(len != 0)
{
sum += ((uint8_t *)w)[0];
}
/* fold the checksum */
sum = (sum >> 16) + (sum & 0xffff);
sum += (sum >> 16);
if((tcp->th_sum = ~sum) == 0)
{
tcp->th_sum = 0xffff;
}
return;
}
static void tcp4_build(scamper_probe_t *probe, uint8_t *buf)
{
struct tcphdr *tcp = (struct tcphdr *)buf;
size_t tcphlen = 20;
tcp->th_sport = htons(probe->pr_tcp_sport);
tcp->th_dport = htons(probe->pr_tcp_dport);
tcp->th_seq = htonl(probe->pr_tcp_seq);
tcp->th_ack = htonl(probe->pr_tcp_ack);
tcp->th_flags = probe->pr_tcp_flags;
tcp->th_win = htons(probe->pr_tcp_win);
tcp->th_sum = 0;
tcp->th_urp = 0;
if(probe->pr_tcp_flags & TH_SYN)
{
if(probe->pr_tcp_mss != 0)
{
tcp_mss(buf+tcphlen, probe->pr_tcp_mss);
tcphlen += 4;
}
}
#ifndef _WIN32
tcp->th_off = tcphlen >> 2;
tcp->th_x2 = 0;
#else
tcp->th_offx2 = ((tcphlen >> 2) << 4);
#endif
/* if there is data to include in the payload, copy it in now */
if(probe->pr_len > 0)
{
memcpy(buf + tcphlen, probe->pr_data, probe->pr_len);
}
/* compute the checksum over the tcp portion of the probe */
tcp_cksum(probe, tcp, tcphlen + probe->pr_len);
return;
}
size_t scamper_tcp4_hlen(scamper_probe_t *probe)
{
size_t len = 20;
if(probe->pr_tcp_mss != 0)
len += 4;
return len;
}
int scamper_tcp4_build(scamper_probe_t *probe, uint8_t *buf, size_t *len)
{
size_t ip4hlen, req;
int rc = 0;
ip4hlen = *len;
scamper_ip4_build(probe, buf, &ip4hlen);
req = ip4hlen + scamper_tcp4_hlen(probe) + probe->pr_len;
if(req <= *len)
tcp4_build(probe, buf + ip4hlen);
else
rc = -1;
*len = req;
return rc;
}
void scamper_tcp4_close(int fd)
{
#ifndef _WIN32
close(fd);
#else
closesocket(fd);
#endif
return;
}
int scamper_tcp4_open(const void *addr, int sport)
{
struct sockaddr_in sin4;
char tmp[32];
int fd = -1;
if((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1)
{
printerror(errno, strerror, __func__, "could not open socket");
goto err;
}
sockaddr_compose((struct sockaddr *)&sin4, AF_INET, addr, sport);
if(bind(fd, (struct sockaddr *)&sin4, sizeof(sin4)) == -1)
{
if(addr == NULL || addr_tostr(AF_INET, addr, tmp, sizeof(tmp)) == NULL)
printerror(errno,strerror,__func__, "could not bind port %d", sport);
else
printerror(errno,strerror,__func__, "could not bind %s:%d", tmp, sport);
goto err;
}
return fd;
err:
if(fd != -1) scamper_tcp4_close(fd);
return -1;
}