Tools/NetTest - TCP stack testing, going well
[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                 return IP_Checksum(IP_CHECKSUM_START, 12, phdr);
32         }
33         else {
34                 TEST_WARN("TCP unknown AF %i", AF);
35                 return 0;
36         }
37 }
38
39 void TCP_Send(int IF, int AF, const void *IP, short sport, short dport,
40         uint32_t seq, uint32_t ack, uint8_t flags, uint16_t window,
41         size_t data_len, const void *data
42         )
43 {
44         tTCPHeader      hdr;
45         hdr.SPort = htons(sport);
46         hdr.DPort = htons(dport);
47         hdr.Seq = htonl(seq);
48         hdr.Ack = htonl(ack);
49         hdr.DataOfs = (sizeof(hdr)/4) << 4;
50         hdr.Flags = flags;
51         hdr.Window = htons(window);
52         hdr.Checksum = htons(0);
53         hdr.UrgPtr = htons(0);
54
55         uint16_t        checksum;
56         checksum = TCP_int_GetPseudoHeader(AF, BLOB(HOST_IP), IP, IPPROTO_TCP, sizeof(hdr)+data_len);
57         checksum = IP_Checksum(checksum, sizeof(hdr), &hdr);
58         checksum = IP_Checksum(checksum, data_len, data);
59         hdr.Checksum = htons( checksum );
60
61         size_t  buflens[] = {sizeof(hdr), data_len};
62         const void *bufs[] = {&hdr, data};
63         IP_Send(IF, AF, BLOB(HOST_IP), IP, IPPROTO_TCP, 2, buflens, bufs);
64 }
65
66 struct {
67         bool    Seq;
68         bool    Ack;
69         bool    SPort;
70 }       gTCP_Skips;
71
72 void TCP_SkipCheck_Seq(bool Skip) {
73         gTCP_Skips.Seq = Skip;
74 }
75
76 bool TCP_Pkt_Check(size_t len, const void *data, size_t *out_ofs,
77         int AF, const void *IP, short sport, short dport,
78         uint32_t seq, uint32_t ack, uint8_t flags)
79 {
80         size_t  ofs;
81         if( !IP_Pkt_Check(len, data, &ofs, AF, IP, BLOB(HOST_IP), IPPROTO_TCP) )
82                 return false;
83         // TODO: IP has its own length field, use that?
84         
85         tTCPHeader      hdr;
86         TEST_ASSERT_REL(len - ofs, >=, sizeof(hdr));    
87         memcpy(&hdr, (char*)data + ofs, sizeof(hdr));
88         
89         TEST_ASSERT_REL( hdr.DataOfs >> 4, >=, sizeof(hdr)/4 );
90         if( !gTCP_Skips.SPort ) TEST_ASSERT_REL( ntohs(hdr.SPort), ==, sport );
91         TEST_ASSERT_REL( ntohs(hdr.DPort), ==, dport );
92         if( !gTCP_Skips.Seq )   TEST_ASSERT_REL( ntohl(hdr.Seq), ==, seq );
93         if( !gTCP_Skips.Ack )   TEST_ASSERT_REL( ntohl(hdr.Ack), ==, ack );
94         TEST_ASSERT_REL( hdr.Flags, ==, flags);
95
96         uint16_t        real_cksum = htons(hdr.Checksum);
97         hdr.Checksum = 0;
98         uint16_t        calc_cksum;
99         calc_cksum = TCP_int_GetPseudoHeader(AF, IP, BLOB(HOST_IP), IPPROTO_TCP, len-ofs);
100         calc_cksum = IP_Checksum(calc_cksum, sizeof(hdr), &hdr);
101         calc_cksum = IP_Checksum(calc_cksum, len - ofs - sizeof(hdr), (char*)data+ofs+sizeof(hdr));
102         TEST_ASSERT_REL( real_cksum, ==, calc_cksum );
103
104         memset(&gTCP_Skips, 0, sizeof(gTCP_Skips));
105
106         *out_ofs = ofs + sizeof(hdr);
107         return true;
108 }
109
110 uint32_t TCP_Pkt_GetSeq(size_t len, const void *data, int AF) {
111         size_t  ofs;
112         IP_Pkt_Check(len, data, &ofs, AF, NULL, NULL, IPPROTO_TCP);
113         
114         tTCPHeader      hdr;
115         memcpy(&hdr, (char*)data + ofs, sizeof(hdr));
116         return ntohl(hdr.Seq);
117 }
118

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