1 #include "tcp_helper.h" 2 3 #include "lwip/priv/tcp_priv.h" 4 #include "lwip/stats.h" 5 #include "lwip/pbuf.h" 6 #include "lwip/inet_chksum.h" 7 #include "lwip/ip_addr.h" 8 9 #if !LWIP_STATS || !TCP_STATS || !MEMP_STATS 10 #error "This tests needs TCP- and MEMP-statistics enabled" 11 #endif 12 13 /** Remove all pcbs on the given list. */ 14 static void 15 tcp_remove(struct tcp_pcb* pcb_list) 16 { 17 struct tcp_pcb *pcb = pcb_list; 18 struct tcp_pcb *pcb2; 19 20 while(pcb != NULL) { 21 pcb2 = pcb; 22 pcb = pcb->next; 23 tcp_abort(pcb2); 24 } 25 } 26 27 /** Remove all pcbs on listen-, active- and time-wait-list (bound- isn't exported). */ 28 void 29 tcp_remove_all(void) 30 { 31 tcp_remove(tcp_listen_pcbs.pcbs); 32 tcp_remove(tcp_active_pcbs); 33 tcp_remove(tcp_tw_pcbs); 34 fail_unless(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0); 35 fail_unless(MEMP_STATS_GET(used, MEMP_TCP_PCB_LISTEN) == 0); 36 fail_unless(MEMP_STATS_GET(used, MEMP_TCP_SEG) == 0); 37 fail_unless(MEMP_STATS_GET(used, MEMP_PBUF_POOL) == 0); 38 } 39 40 /** Create a TCP segment usable for passing to tcp_input */ 41 static struct pbuf* 42 tcp_create_segment_wnd(ip_addr_t* src_ip, ip_addr_t* dst_ip, 43 u16_t src_port, u16_t dst_port, void* data, size_t data_len, 44 u32_t seqno, u32_t ackno, u8_t headerflags, u16_t wnd) 45 { 46 struct pbuf *p, *q; 47 struct ip_hdr* iphdr; 48 struct tcp_hdr* tcphdr; 49 u16_t pbuf_len = (u16_t)(sizeof(struct ip_hdr) + sizeof(struct tcp_hdr) + data_len); 50 LWIP_ASSERT("data_len too big", data_len <= 0xFFFF); 51 52 p = pbuf_alloc(PBUF_RAW, pbuf_len, PBUF_POOL); 53 EXPECT_RETNULL(p != NULL); 54 /* first pbuf must be big enough to hold the headers */ 55 EXPECT_RETNULL(p->len >= (sizeof(struct ip_hdr) + sizeof(struct tcp_hdr))); 56 if (data_len > 0) { 57 /* first pbuf must be big enough to hold at least 1 data byte, too */ 58 EXPECT_RETNULL(p->len > (sizeof(struct ip_hdr) + sizeof(struct tcp_hdr))); 59 } 60 61 for(q = p; q != NULL; q = q->next) { 62 memset(q->payload, 0, q->len); 63 } 64 65 iphdr = (struct ip_hdr*)p->payload; 66 /* fill IP header */ 67 iphdr->dest.addr = ip_2_ip4(dst_ip)->addr; 68 iphdr->src.addr = ip_2_ip4(src_ip)->addr; 69 IPH_VHL_SET(iphdr, 4, IP_HLEN / 4); 70 IPH_TOS_SET(iphdr, 0); 71 IPH_LEN_SET(iphdr, htons(p->tot_len)); 72 IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, IP_HLEN)); 73 74 /* let p point to TCP header */ 75 pbuf_header(p, -(s16_t)sizeof(struct ip_hdr)); 76 77 tcphdr = (struct tcp_hdr*)p->payload; 78 tcphdr->src = htons(src_port); 79 tcphdr->dest = htons(dst_port); 80 tcphdr->seqno = htonl(seqno); 81 tcphdr->ackno = htonl(ackno); 82 TCPH_HDRLEN_SET(tcphdr, sizeof(struct tcp_hdr)/4); 83 TCPH_FLAGS_SET(tcphdr, headerflags); 84 tcphdr->wnd = htons(wnd); 85 86 if (data_len > 0) { 87 /* let p point to TCP data */ 88 pbuf_header(p, -(s16_t)sizeof(struct tcp_hdr)); 89 /* copy data */ 90 pbuf_take(p, data, (u16_t)data_len); 91 /* let p point to TCP header again */ 92 pbuf_header(p, sizeof(struct tcp_hdr)); 93 } 94 95 /* calculate checksum */ 96 97 tcphdr->chksum = ip_chksum_pseudo(p, 98 IP_PROTO_TCP, p->tot_len, src_ip, dst_ip); 99 100 pbuf_header(p, sizeof(struct ip_hdr)); 101 102 return p; 103 } 104 105 /** Create a TCP segment usable for passing to tcp_input */ 106 struct pbuf* 107 tcp_create_segment(ip_addr_t* src_ip, ip_addr_t* dst_ip, 108 u16_t src_port, u16_t dst_port, void* data, size_t data_len, 109 u32_t seqno, u32_t ackno, u8_t headerflags) 110 { 111 return tcp_create_segment_wnd(src_ip, dst_ip, src_port, dst_port, data, 112 data_len, seqno, ackno, headerflags, TCP_WND); 113 } 114 115 /** Create a TCP segment usable for passing to tcp_input 116 * - IP-addresses, ports, seqno and ackno are taken from pcb 117 * - seqno and ackno can be altered with an offset 118 */ 119 struct pbuf* 120 tcp_create_rx_segment(struct tcp_pcb* pcb, void* data, size_t data_len, u32_t seqno_offset, 121 u32_t ackno_offset, u8_t headerflags) 122 { 123 return tcp_create_segment(&pcb->remote_ip, &pcb->local_ip, pcb->remote_port, pcb->local_port, 124 data, data_len, pcb->rcv_nxt + seqno_offset, pcb->lastack + ackno_offset, headerflags); 125 } 126 127 /** Create a TCP segment usable for passing to tcp_input 128 * - IP-addresses, ports, seqno and ackno are taken from pcb 129 * - seqno and ackno can be altered with an offset 130 * - TCP window can be adjusted 131 */ 132 struct pbuf* tcp_create_rx_segment_wnd(struct tcp_pcb* pcb, void* data, size_t data_len, 133 u32_t seqno_offset, u32_t ackno_offset, u8_t headerflags, u16_t wnd) 134 { 135 return tcp_create_segment_wnd(&pcb->remote_ip, &pcb->local_ip, pcb->remote_port, pcb->local_port, 136 data, data_len, pcb->rcv_nxt + seqno_offset, pcb->lastack + ackno_offset, headerflags, wnd); 137 } 138 139 /** Safely bring a tcp_pcb into the requested state */ 140 void 141 tcp_set_state(struct tcp_pcb* pcb, enum tcp_state state, ip_addr_t* local_ip, 142 ip_addr_t* remote_ip, u16_t local_port, u16_t remote_port) 143 { 144 u32_t iss; 145 146 /* @todo: are these all states? */ 147 /* @todo: remove from previous list */ 148 pcb->state = state; 149 150 iss = tcp_next_iss(pcb); 151 pcb->snd_wl2 = iss; 152 pcb->snd_nxt = iss; 153 pcb->lastack = iss; 154 pcb->snd_lbb = iss; 155 156 if (state == ESTABLISHED) { 157 TCP_REG(&tcp_active_pcbs, pcb); 158 ip_addr_copy(pcb->local_ip, *local_ip); 159 pcb->local_port = local_port; 160 ip_addr_copy(pcb->remote_ip, *remote_ip); 161 pcb->remote_port = remote_port; 162 } else if(state == LISTEN) { 163 TCP_REG(&tcp_listen_pcbs.pcbs, pcb); 164 ip_addr_copy(pcb->local_ip, *local_ip); 165 pcb->local_port = local_port; 166 } else if(state == TIME_WAIT) { 167 TCP_REG(&tcp_tw_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 { 173 fail(); 174 } 175 } 176 177 void 178 test_tcp_counters_err(void* arg, err_t err) 179 { 180 struct test_tcp_counters* counters = (struct test_tcp_counters*)arg; 181 EXPECT_RET(arg != NULL); 182 counters->err_calls++; 183 counters->last_err = err; 184 } 185 186 static void 187 test_tcp_counters_check_rxdata(struct test_tcp_counters* counters, struct pbuf* p) 188 { 189 struct pbuf* q; 190 u32_t i, received; 191 if(counters->expected_data == NULL) { 192 /* no data to compare */ 193 return; 194 } 195 EXPECT_RET(counters->recved_bytes + p->tot_len <= counters->expected_data_len); 196 received = counters->recved_bytes; 197 for(q = p; q != NULL; q = q->next) { 198 char *data = (char*)q->payload; 199 for(i = 0; i < q->len; i++) { 200 EXPECT_RET(data[i] == counters->expected_data[received]); 201 received++; 202 } 203 } 204 EXPECT(received == counters->recved_bytes + p->tot_len); 205 } 206 207 err_t 208 test_tcp_counters_recv(void* arg, struct tcp_pcb* pcb, struct pbuf* p, err_t err) 209 { 210 struct test_tcp_counters* counters = (struct test_tcp_counters*)arg; 211 EXPECT_RETX(arg != NULL, ERR_OK); 212 EXPECT_RETX(pcb != NULL, ERR_OK); 213 EXPECT_RETX(err == ERR_OK, ERR_OK); 214 215 if (p != NULL) { 216 if (counters->close_calls == 0) { 217 counters->recv_calls++; 218 test_tcp_counters_check_rxdata(counters, p); 219 counters->recved_bytes += p->tot_len; 220 } else { 221 counters->recv_calls_after_close++; 222 counters->recved_bytes_after_close += p->tot_len; 223 } 224 pbuf_free(p); 225 } else { 226 counters->close_calls++; 227 } 228 EXPECT(counters->recv_calls_after_close == 0 && counters->recved_bytes_after_close == 0); 229 return ERR_OK; 230 } 231 232 /** Allocate a pcb and set up the test_tcp_counters_* callbacks */ 233 struct tcp_pcb* 234 test_tcp_new_counters_pcb(struct test_tcp_counters* counters) 235 { 236 struct tcp_pcb* pcb = tcp_new(); 237 if (pcb != NULL) { 238 /* set up args and callbacks */ 239 tcp_arg(pcb, counters); 240 tcp_recv(pcb, test_tcp_counters_recv); 241 tcp_err(pcb, test_tcp_counters_err); 242 pcb->snd_wnd = TCP_WND; 243 pcb->snd_wnd_max = TCP_WND; 244 } 245 return pcb; 246 } 247 248 /** Calls tcp_input() after adjusting current_iphdr_dest */ 249 void test_tcp_input(struct pbuf *p, struct netif *inp) 250 { 251 struct ip_hdr *iphdr = (struct ip_hdr*)p->payload; 252 /* these lines are a hack, don't use them as an example :-) */ 253 ip_addr_copy_from_ip4(*ip_current_dest_addr(), iphdr->dest); 254 ip_addr_copy_from_ip4(*ip_current_src_addr(), iphdr->src); 255 ip_current_netif() = inp; 256 ip_data.current_ip4_header = iphdr; 257 258 /* since adding IPv6, p->payload must point to tcp header, not ip header */ 259 pbuf_header(p, -(s16_t)sizeof(struct ip_hdr)); 260 261 tcp_input(p, inp); 262 263 ip_addr_set_zero(ip_current_dest_addr()); 264 ip_addr_set_zero(ip_current_src_addr()); 265 ip_current_netif() = NULL; 266 ip_data.current_ip4_header = NULL; 267 } 268 269 static err_t test_tcp_netif_output(struct netif *netif, struct pbuf *p, 270 const ip4_addr_t *ipaddr) 271 { 272 struct test_tcp_txcounters *txcounters = (struct test_tcp_txcounters*)netif->state; 273 LWIP_UNUSED_ARG(ipaddr); 274 if (txcounters != NULL) 275 { 276 txcounters->num_tx_calls++; 277 txcounters->num_tx_bytes += p->tot_len; 278 if (txcounters->copy_tx_packets) { 279 struct pbuf *p_copy = pbuf_alloc(PBUF_LINK, p->tot_len, PBUF_RAM); 280 err_t err; 281 EXPECT(p_copy != NULL); 282 err = pbuf_copy(p_copy, p); 283 EXPECT(err == ERR_OK); 284 if (txcounters->tx_packets == NULL) { 285 txcounters->tx_packets = p_copy; 286 } else { 287 pbuf_cat(txcounters->tx_packets, p_copy); 288 } 289 } 290 } 291 return ERR_OK; 292 } 293 294 void test_tcp_init_netif(struct netif *netif, struct test_tcp_txcounters *txcounters, 295 ip_addr_t *ip_addr, ip_addr_t *netmask) 296 { 297 struct netif *n; 298 memset(netif, 0, sizeof(struct netif)); 299 if (txcounters != NULL) { 300 memset(txcounters, 0, sizeof(struct test_tcp_txcounters)); 301 netif->state = txcounters; 302 } 303 netif->output = test_tcp_netif_output; 304 netif->flags |= NETIF_FLAG_UP | NETIF_FLAG_LINK_UP; 305 ip_addr_copy_from_ip4(netif->netmask, *ip_2_ip4(netmask)); 306 ip_addr_copy_from_ip4(netif->ip_addr, *ip_2_ip4(ip_addr)); 307 for (n = netif_list; n != NULL; n = n->next) { 308 if (n == netif) { 309 return; 310 } 311 } 312 netif->next = NULL; 313 netif_list = netif; 314 } 315