Tools/NetTest - TCP test passing with connection opened/used/closed
[tpg/acess2.git] / Tools / NetTest_Runner / tcp.c
1 /*
2  */
3 #include "common.h"
4 #include "tcp.h"
5 #include "ip.h"
6 #include "test.h"       // TEST_ASSERT
7 #include <string.h>
8
9 typedef struct {
10         uint16_t        SPort;
11         uint16_t        DPort;
12         uint32_t        Seq;
13         uint32_t        Ack;
14         uint8_t         DataOfs;
15         uint8_t         Flags;
16         uint16_t        Window;
17         uint16_t        Checksum;
18         uint16_t        UrgPtr;
19 } __attribute__((packed)) tTCPHeader;
20
21 // === CODE ===
22 uint16_t TCP_int_GetPseudoHeader(int AF, const void *SrcAddr, const void *DstAddr, uint8_t pctl, size_t Len)
23 {
24         if( AF == 4 ) {
25                 uint8_t phdr[12];
26                 memcpy(phdr+0, SrcAddr, 4);
27                 memcpy(phdr+4, DstAddr, 4);
28                 phdr[8] = 0;
29                 phdr[9] = pctl;
30                 *(uint16_t*)(phdr+10) = htons(Len);
31                 
32                 //test_trace_hexdump("TCP IPv4 PHdr", phdr, sizeof(phdr));
33                 
34                 return IP_Checksum(IP_CHECKSUM_START, 12, phdr);
35         }
36         else {
37                 TEST_WARN("TCP unknown AF %i", AF);
38                 return 0;
39         }
40 }
41
42 void TCP_Send(int IF, int AF, const void *IP, short sport, short dport,
43         uint32_t seq, uint32_t ack, uint8_t flags, uint16_t window,
44         size_t data_len, const void *data
45         )
46 {
47         tTCPHeader      hdr;
48         hdr.SPort = htons(sport);
49         hdr.DPort = htons(dport);
50         hdr.Seq = htonl(seq);
51         hdr.Ack = htonl(ack);
52         hdr.DataOfs = (sizeof(hdr)/4) << 4;
53         hdr.Flags = flags;
54         hdr.Window = htons(window);
55         hdr.Checksum = htons(0);
56         hdr.UrgPtr = htons(0);
57
58         uint16_t        checksum;
59         checksum = TCP_int_GetPseudoHeader(AF, BLOB(HOST_IP), IP, IPPROTO_TCP, sizeof(hdr)+data_len);
60         checksum = IP_Checksum(checksum, sizeof(hdr), &hdr);
61         checksum = IP_Checksum(checksum, data_len, data);
62         hdr.Checksum = htons( checksum );
63
64         size_t  buflens[] = {sizeof(hdr), data_len};
65         const void *bufs[] = {&hdr, data};
66         IP_Send(IF, AF, BLOB(HOST_IP), IP, IPPROTO_TCP, 2, buflens, bufs);
67 }
68
69 struct {
70         bool    Seq;
71         bool    Ack;
72         bool    SPort;
73 }       gTCP_Skips;
74
75 void TCP_SkipCheck_Seq(bool Skip) {
76         gTCP_Skips.Seq = Skip;
77 }
78
79 bool TCP_Pkt_Check(size_t len, const void *data, size_t *out_ofs, size_t *len_out,
80         int AF, const void *IP, short sport, short dport,
81         uint32_t seq, uint32_t ack, uint8_t flags)
82 {
83         size_t  ofs, rlen;
84         if( !IP_Pkt_Check(len, data, &ofs, &rlen, AF, IP, BLOB(HOST_IP), IPPROTO_TCP) )
85                 return false;
86         // TODO: IP has its own length field, use that?
87         
88         tTCPHeader      hdr;
89         TEST_ASSERT_REL(rlen, >=, sizeof(hdr)); 
90         memcpy(&hdr, (char*)data + ofs, sizeof(hdr));
91         
92         TEST_ASSERT_REL( hdr.DataOfs >> 4, >=, sizeof(hdr)/4 );
93         if( !gTCP_Skips.SPort ) TEST_ASSERT_REL( ntohs(hdr.SPort), ==, sport );
94         TEST_ASSERT_REL( ntohs(hdr.DPort), ==, dport );
95         if( !gTCP_Skips.Seq )   TEST_ASSERT_REL( ntohl(hdr.Seq), ==, seq );
96         if( !gTCP_Skips.Ack )   TEST_ASSERT_REL( ntohl(hdr.Ack), ==, ack );
97         TEST_ASSERT_REL( hdr.Flags, ==, flags);
98
99         uint16_t        real_cksum = htons(hdr.Checksum);
100         hdr.Checksum = 0;
101         uint16_t        calc_cksum;
102         calc_cksum = TCP_int_GetPseudoHeader(AF, IP, BLOB(HOST_IP), IPPROTO_TCP, rlen);
103         calc_cksum = IP_Checksum(calc_cksum, sizeof(hdr), &hdr);
104         calc_cksum = IP_Checksum(calc_cksum, rlen - sizeof(hdr), (char*)data+ofs+sizeof(hdr));
105         TEST_ASSERT_REL( real_cksum, ==, calc_cksum );
106
107         memset(&gTCP_Skips, 0, sizeof(gTCP_Skips));
108
109         *out_ofs = ofs + sizeof(hdr);
110         *len_out = rlen - sizeof(hdr);
111         return true;
112 }
113
114 uint32_t TCP_Pkt_GetSeq(size_t len, const void *data, int AF) {
115         size_t  ofs, rlen;
116         IP_Pkt_Check(len, data, &ofs, &rlen, AF, NULL, NULL, IPPROTO_TCP);
117         
118         tTCPHeader      hdr;
119         memcpy(&hdr, (char*)data + ofs, sizeof(hdr));
120         return ntohl(hdr.Seq);
121 }
122

UCC git Repository :: git.ucc.asn.au