ReactOS 0.4.16-dev-835-gd769f56
tcp_helper.c
Go to the documentation of this file.
1#include "tcp_helper.h"
2
4#include "lwip/stats.h"
5#include "lwip/pbuf.h"
6#include "lwip/inet.h"
7#include "lwip/inet_chksum.h"
8#include "lwip/ip_addr.h"
9
10#if !LWIP_STATS || !TCP_STATS || !MEMP_STATS
11#error "This tests needs TCP- and MEMP-statistics enabled"
12#endif
13
14const ip_addr_t test_local_ip = IPADDR4_INIT_BYTES(192, 168, 1, 1);
15const ip_addr_t test_remote_ip = IPADDR4_INIT_BYTES(192, 168, 1, 2);
16const ip_addr_t test_netmask = IPADDR4_INIT_BYTES(255, 255, 255, 0);
17
19static void
20tcp_remove(struct tcp_pcb* pcb_list)
21{
22 struct tcp_pcb *pcb = pcb_list;
23 struct tcp_pcb *pcb2;
24
25 while(pcb != NULL) {
26 pcb2 = pcb;
27 pcb = pcb->next;
28 if (pcb2->state == LISTEN) {
29 tcp_close(pcb2);
30 } else {
31 tcp_abort(pcb2);
32 }
33 }
34}
35
37void
39{
40 tcp_remove(tcp_listen_pcbs.pcbs);
41 tcp_remove(tcp_bound_pcbs);
42 tcp_remove(tcp_active_pcbs);
43 tcp_remove(tcp_tw_pcbs);
44 fail_unless(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
45 fail_unless(MEMP_STATS_GET(used, MEMP_TCP_PCB_LISTEN) == 0);
46 fail_unless(MEMP_STATS_GET(used, MEMP_TCP_SEG) == 0);
47 fail_unless(MEMP_STATS_GET(used, MEMP_PBUF_POOL) == 0);
48}
49
51static struct pbuf*
53 u16_t src_port, u16_t dst_port, void* data, size_t data_len,
54 u32_t seqno, u32_t ackno, u8_t headerflags, u16_t wnd)
55{
56 struct pbuf *p, *q;
57 struct ip_hdr* iphdr;
58 struct tcp_hdr* tcphdr;
59 u16_t pbuf_len = (u16_t)(sizeof(struct ip_hdr) + sizeof(struct tcp_hdr) + data_len);
60 LWIP_ASSERT("data_len too big", data_len <= 0xFFFF);
61
62 p = pbuf_alloc(PBUF_RAW, pbuf_len, PBUF_POOL);
64 /* first pbuf must be big enough to hold the headers */
65 EXPECT_RETNULL(p->len >= (sizeof(struct ip_hdr) + sizeof(struct tcp_hdr)));
66 if (data_len > 0) {
67 /* first pbuf must be big enough to hold at least 1 data byte, too */
68 EXPECT_RETNULL(p->len > (sizeof(struct ip_hdr) + sizeof(struct tcp_hdr)));
69 }
70
71 for(q = p; q != NULL; q = q->next) {
72 memset(q->payload, 0, q->len);
73 }
74
75 iphdr = (struct ip_hdr*)p->payload;
76 /* fill IP header */
77 iphdr->dest.addr = ip_2_ip4(dst_ip)->addr;
78 iphdr->src.addr = ip_2_ip4(src_ip)->addr;
79 IPH_VHL_SET(iphdr, 4, IP_HLEN / 4);
80 IPH_TOS_SET(iphdr, 0);
81 IPH_LEN_SET(iphdr, htons(p->tot_len));
82 IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, IP_HLEN));
83
84 /* let p point to TCP header */
85 pbuf_header(p, -(s16_t)sizeof(struct ip_hdr));
86
87 tcphdr = (struct tcp_hdr*)p->payload;
88 tcphdr->src = htons(src_port);
89 tcphdr->dest = htons(dst_port);
90 tcphdr->seqno = htonl(seqno);
91 tcphdr->ackno = htonl(ackno);
92 TCPH_HDRLEN_SET(tcphdr, sizeof(struct tcp_hdr)/4);
93 TCPH_FLAGS_SET(tcphdr, headerflags);
94 tcphdr->wnd = htons(wnd);
95
96 if (data_len > 0) {
97 /* let p point to TCP data */
98 pbuf_header(p, -(s16_t)sizeof(struct tcp_hdr));
99 /* copy data */
100 pbuf_take(p, data, (u16_t)data_len);
101 /* let p point to TCP header again */
102 pbuf_header(p, sizeof(struct tcp_hdr));
103 }
104
105 /* calculate checksum */
106
107 tcphdr->chksum = ip_chksum_pseudo(p,
108 IP_PROTO_TCP, p->tot_len, src_ip, dst_ip);
109
110 pbuf_header(p, sizeof(struct ip_hdr));
111
112 return p;
113}
114
116struct pbuf*
118 u16_t src_port, u16_t dst_port, void* data, size_t data_len,
119 u32_t seqno, u32_t ackno, u8_t headerflags)
120{
121 return tcp_create_segment_wnd(src_ip, dst_ip, src_port, dst_port, data,
122 data_len, seqno, ackno, headerflags, TCP_WND);
123}
124
129struct pbuf*
130tcp_create_rx_segment(struct tcp_pcb* pcb, void* data, size_t data_len, u32_t seqno_offset,
131 u32_t ackno_offset, u8_t headerflags)
132{
133 return tcp_create_segment(&pcb->remote_ip, &pcb->local_ip, pcb->remote_port, pcb->local_port,
134 data, data_len, pcb->rcv_nxt + seqno_offset, pcb->lastack + ackno_offset, headerflags);
135}
136
142struct pbuf* tcp_create_rx_segment_wnd(struct tcp_pcb* pcb, void* data, size_t data_len,
143 u32_t seqno_offset, u32_t ackno_offset, u8_t headerflags, u16_t wnd)
144{
145 return tcp_create_segment_wnd(&pcb->remote_ip, &pcb->local_ip, pcb->remote_port, pcb->local_port,
146 data, data_len, pcb->rcv_nxt + seqno_offset, pcb->lastack + ackno_offset, headerflags, wnd);
147}
148
150void
151tcp_set_state(struct tcp_pcb* pcb, enum tcp_state state, const ip_addr_t* local_ip,
152 const ip_addr_t* remote_ip, u16_t local_port, u16_t remote_port)
153{
154 u32_t iss;
155
156 /* @todo: are these all states? */
157 /* @todo: remove from previous list */
158 pcb->state = state;
159
160 iss = tcp_next_iss(pcb);
161 pcb->snd_wl2 = iss;
162 pcb->snd_nxt = iss;
163 pcb->lastack = iss;
164 pcb->snd_lbb = iss;
165
166 if (state == ESTABLISHED) {
167 TCP_REG(&tcp_active_pcbs, pcb);
168 ip_addr_copy(pcb->local_ip, *local_ip);
169 pcb->local_port = local_port;
170 ip_addr_copy(pcb->remote_ip, *remote_ip);
171 pcb->remote_port = remote_port;
172 } else if(state == LISTEN) {
173 TCP_REG(&tcp_listen_pcbs.pcbs, pcb);
174 ip_addr_copy(pcb->local_ip, *local_ip);
175 pcb->local_port = local_port;
176 } else if(state == TIME_WAIT) {
177 TCP_REG(&tcp_tw_pcbs, pcb);
178 ip_addr_copy(pcb->local_ip, *local_ip);
179 pcb->local_port = local_port;
180 ip_addr_copy(pcb->remote_ip, *remote_ip);
181 pcb->remote_port = remote_port;
182 } else {
183 fail();
184 }
185}
186
187void
189{
191 EXPECT_RET(arg != NULL);
192 counters->err_calls++;
193 counters->last_err = err;
194}
195
196static void
198{
199 struct pbuf* q;
201 if(counters->expected_data == NULL) {
202 /* no data to compare */
203 return;
204 }
205 EXPECT_RET(counters->recved_bytes + p->tot_len <= counters->expected_data_len);
206 received = counters->recved_bytes;
207 for(q = p; q != NULL; q = q->next) {
208 char *data = (char*)q->payload;
209 for(i = 0; i < q->len; i++) {
210 EXPECT_RET(data[i] == counters->expected_data[received]);
211 received++;
212 }
213 }
214 EXPECT(received == counters->recved_bytes + p->tot_len);
215}
216
217err_t
218test_tcp_counters_recv(void* arg, struct tcp_pcb* pcb, struct pbuf* p, err_t err)
219{
222 EXPECT_RETX(pcb != NULL, ERR_OK);
224
225 if (p != NULL) {
226 if (counters->close_calls == 0) {
227 counters->recv_calls++;
229 counters->recved_bytes += p->tot_len;
230 } else {
231 counters->recv_calls_after_close++;
232 counters->recved_bytes_after_close += p->tot_len;
233 }
234 pbuf_free(p);
235 } else {
236 counters->close_calls++;
237 }
238 EXPECT(counters->recv_calls_after_close == 0 && counters->recved_bytes_after_close == 0);
239 return ERR_OK;
240}
241
243struct tcp_pcb*
245{
246 struct tcp_pcb* pcb = tcp_new();
247 if (pcb != NULL) {
248 /* set up args and callbacks */
249 tcp_arg(pcb, counters);
251 tcp_err(pcb, test_tcp_counters_err);
252 pcb->snd_wnd = TCP_WND;
253 pcb->snd_wnd_max = TCP_WND;
254 }
255 return pcb;
256}
257
259void test_tcp_input(struct pbuf *p, struct netif *inp)
260{
261 struct ip_hdr *iphdr = (struct ip_hdr*)p->payload;
262 /* these lines are a hack, don't use them as an example :-) */
263 ip_addr_copy_from_ip4(*ip_current_dest_addr(), iphdr->dest);
264 ip_addr_copy_from_ip4(*ip_current_src_addr(), iphdr->src);
265 ip_current_netif() = inp;
266 ip_data.current_ip4_header = iphdr;
267 ip_data.current_input_netif = inp;
268
269 /* since adding IPv6, p->payload must point to tcp header, not ip header */
270 pbuf_header(p, -(s16_t)sizeof(struct ip_hdr));
271
272 tcp_input(p, inp);
273
277 ip_data.current_ip4_header = NULL;
278}
279
280static err_t test_tcp_netif_output(struct netif *netif, struct pbuf *p,
281 const ip4_addr_t *ipaddr)
282{
283 struct test_tcp_txcounters *txcounters = (struct test_tcp_txcounters*)netif->state;
284 LWIP_UNUSED_ARG(ipaddr);
285 if (txcounters != NULL)
286 {
287 txcounters->num_tx_calls++;
288 txcounters->num_tx_bytes += p->tot_len;
289 if (txcounters->copy_tx_packets) {
290 struct pbuf *p_copy = pbuf_alloc(PBUF_LINK, p->tot_len, PBUF_RAM);
291 err_t err;
292 EXPECT(p_copy != NULL);
293 err = pbuf_copy(p_copy, p);
294 EXPECT(err == ERR_OK);
295 if (txcounters->tx_packets == NULL) {
296 txcounters->tx_packets = p_copy;
297 } else {
298 pbuf_cat(txcounters->tx_packets, p_copy);
299 }
300 }
301 }
302 return ERR_OK;
303}
304
305void test_tcp_init_netif(struct netif *netif, struct test_tcp_txcounters *txcounters,
306 const ip_addr_t *ip_addr, const ip_addr_t *netmask)
307{
308 struct netif *n;
309 memset(netif, 0, sizeof(struct netif));
310 if (txcounters != NULL) {
311 memset(txcounters, 0, sizeof(struct test_tcp_txcounters));
312 netif->state = txcounters;
313 }
316 ip_addr_copy_from_ip4(netif->netmask, *ip_2_ip4(netmask));
317 ip_addr_copy_from_ip4(netif->ip_addr, *ip_2_ip4(ip_addr));
318 for (n = netif_list; n != NULL; n = n->next) {
319 if (n == netif) {
320 return;
321 }
322 }
323 netif->next = NULL;
325}
static int used
Definition: adh-main.c:39
static int state
Definition: maze.c:121
STREAM tcp_recv(STREAM s, uint32 length)
Definition: tcp.c:344
#define NULL
Definition: types.h:112
r received
Definition: btrfs.c:3005
#define LWIP_ASSERT(message, assertion)
Definition: debug.h:116
#define ip_current_src_addr()
Definition: ip.h:223
#define ip_current_dest_addr()
Definition: ip.h:225
#define ip_current_netif()
Definition: ip.h:146
struct ip_globals ip_data
#define IP_PROTO_TCP
Definition: ip.h:50
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950
GLdouble GLdouble GLdouble GLdouble q
Definition: gl.h:2063
GLdouble n
Definition: glext.h:7729
GLint GLint GLsizei GLuint * counters
Definition: glext.h:11114
GLfloat GLfloat p
Definition: glext.h:8902
GLenum GLsizei len
Definition: glext.h:6722
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
uint32_t u32_t
Definition: arch.h:129
uint8_t u8_t
Definition: arch.h:125
#define LWIP_UNUSED_ARG(x)
Definition: arch.h:373
uint16_t u16_t
Definition: arch.h:127
int16_t s16_t
Definition: arch.h:128
s8_t err_t
Definition: err.h:96
@ ERR_OK
Definition: err.h:55
#define NETIF_FLAG_LINK_UP
Definition: netif.h:93
#define NETIF_FLAG_UP
Definition: netif.h:84
void pbuf_cat(struct pbuf *h, struct pbuf *t)
Definition: pbuf.c:855
struct pbuf * pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
Definition: pbuf.c:224
u8_t pbuf_free(struct pbuf *p)
Definition: pbuf.c:727
err_t pbuf_copy(struct pbuf *p_to, const struct pbuf *p_from)
Definition: pbuf.c:959
err_t pbuf_take(struct pbuf *buf, const void *dataptr, u16_t len)
Definition: pbuf.c:1227
@ PBUF_RAM
Definition: pbuf.h:152
@ PBUF_POOL
Definition: pbuf.h:167
@ PBUF_RAW
Definition: pbuf.h:111
@ PBUF_LINK
Definition: pbuf.h:102
#define TCP_WND
Definition: lwipopts.h:48
u16_t inet_chksum(const void *dataptr, u16_t len)
Definition: inet_chksum.c:555
u16_t ip_chksum_pseudo(struct pbuf *p, u8_t proto, u16_t proto_len, const ip_addr_t *src, const ip_addr_t *dest)
Definition: inet_chksum.c:379
#define ip_addr_copy(dest, src)
Definition: ip_addr.h:360
ip6_addr_t ip_addr_t
Definition: ip_addr.h:344
#define ip_addr_set_zero(ipaddr)
Definition: ip_addr.h:365
#define TCPH_HDRLEN_SET(phdr, len)
Definition: tcp.h:89
#define TCPH_FLAGS_SET(phdr, flags)
Definition: tcp.h:90
#define EXPECT_RETX(x, y)
Definition: lwip_check.h:13
#define EXPECT(x)
Definition: lwip_check.h:11
#define EXPECT_RETNULL(x)
Definition: lwip_check.h:14
#define EXPECT_RET(x)
Definition: lwip_check.h:12
#define htons(x)
Definition: module.h:215
#define htonl(x)
Definition: module.h:214
struct netif * netif_list
Definition: netif.c:113
u8_t pbuf_header(struct pbuf *p, s16_t header_size_increment)
Definition: pbuf.c:649
#define IPH_VHL_SET(hdr, v, hl)
Definition: ip4.h:117
#define IPH_LEN_SET(hdr, len)
Definition: ip4.h:119
#define IP_HLEN
Definition: ip4.h:64
#define IPH_CHKSUM_SET(hdr, chksum)
Definition: ip4.h:124
#define IPH_TOS_SET(hdr, tos)
Definition: ip4.h:118
#define err(...)
#define memset(x, y, z)
Definition: compat.h:39
#define MEMP_STATS_GET(x, i)
Definition: stats.h:409
struct define * next
Definition: compiler.c:65
Definition: ip4.h:73
Definition: netif.h:269
u8_t flags
Definition: netif.h:354
void * state
Definition: netif.h:332
struct netif * next
Definition: netif.h:272
Definition: pbuf.h:186
Definition: tcp.h:56
Definition: tcpdef.h:22
__u16 dest
Definition: tcpdef.h:24
struct pbuf * tx_packets
Definition: tcp_helper.h:26
void test_tcp_init_netif(struct netif *netif, struct test_tcp_txcounters *txcounters, const ip_addr_t *ip_addr, const ip_addr_t *netmask)
Definition: tcp_helper.c:305
static struct pbuf * tcp_create_segment_wnd(ip_addr_t *src_ip, ip_addr_t *dst_ip, u16_t src_port, u16_t dst_port, void *data, size_t data_len, u32_t seqno, u32_t ackno, u8_t headerflags, u16_t wnd)
Definition: tcp_helper.c:52
void test_tcp_input(struct pbuf *p, struct netif *inp)
Definition: tcp_helper.c:259
static err_t test_tcp_netif_output(struct netif *netif, struct pbuf *p, const ip4_addr_t *ipaddr)
Definition: tcp_helper.c:280
void test_tcp_counters_err(void *arg, err_t err)
Definition: tcp_helper.c:188
struct pbuf * tcp_create_rx_segment_wnd(struct tcp_pcb *pcb, void *data, size_t data_len, u32_t seqno_offset, u32_t ackno_offset, u8_t headerflags, u16_t wnd)
Definition: tcp_helper.c:142
struct pbuf * tcp_create_rx_segment(struct tcp_pcb *pcb, void *data, size_t data_len, u32_t seqno_offset, u32_t ackno_offset, u8_t headerflags)
Definition: tcp_helper.c:130
struct tcp_pcb * test_tcp_new_counters_pcb(struct test_tcp_counters *counters)
Definition: tcp_helper.c:244
err_t test_tcp_counters_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
Definition: tcp_helper.c:218
void tcp_set_state(struct tcp_pcb *pcb, enum tcp_state state, const ip_addr_t *local_ip, const ip_addr_t *remote_ip, u16_t local_port, u16_t remote_port)
Definition: tcp_helper.c:151
static void test_tcp_counters_check_rxdata(struct test_tcp_counters *counters, struct pbuf *p)
Definition: tcp_helper.c:197
const ip_addr_t test_netmask
Definition: tcp_helper.c:16
void tcp_remove_all(void)
Definition: tcp_helper.c:38
const ip_addr_t test_local_ip
Definition: tcp_helper.c:14
const ip_addr_t test_remote_ip
Definition: tcp_helper.c:15
struct pbuf * tcp_create_segment(ip_addr_t *src_ip, ip_addr_t *dst_ip, u16_t src_port, u16_t dst_port, void *data, size_t data_len, u32_t seqno, u32_t ackno, u8_t headerflags)
Definition: tcp_helper.c:117
static void tcp_remove(struct tcp_pcb *pcb_list)
Definition: tcp_helper.c:20
void tcp_close(struct sock *sk, long timeout)