tcp_helper.c 8.8 KB

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