xref: /haiku/headers/private/kernel/boot/net/TCP.h (revision 040b33980cb8a30a3676589984a24302937751ee)
1 /*
2  * Copyright 2010 Andreas Färber <andreas.faerber@web.de>
3  * All rights reserved. Distributed under the terms of the MIT License.
4  */
5 #ifndef BOOT_NET_TCP_H
6 #define BOOT_NET_TCP_H
7 
8 
9 #include <boot/net/IP.h>
10 
11 
12 class TCPPacket {
13 public:
14 	TCPPacket();
15 	~TCPPacket();
16 
17 	status_t SetTo(const void* data, size_t size, ip_addr_t sourceAddress,
18 		uint16 sourcePort, ip_addr_t destinationAddress,
19 		uint16 destinationPort, uint32 sequenceNumber,
20 		uint32 acknowledgmentNumber, uint8 flags);
21 
22 	ip_addr_t SourceAddress() const;
23 	ip_addr_t DestinationAddress() const;
24 	uint16 SourcePort() const;
25 	uint16 DestinationPort() const;
26 	uint32 SequenceNumber() const;
27 	uint32 AcknowledgmentNumber() const;
Data()28 	const void* Data() const { return fData; }
DataSize()29 	size_t DataSize() const { return fSize; }
Flags()30 	uint8 Flags() const { return fFlags; }
31 
32 	bool ProvidesSequenceNumber(uint32 sequenceNo) const;
33 
34 	TCPPacket* Next() const;
35 	void SetNext(TCPPacket* packet);
36 
37 private:
38 	ip_addr_t	fSourceAddress;
39 	ip_addr_t	fDestinationAddress;
40 	uint16		fSourcePort;
41 	uint16		fDestinationPort;
42 	uint32		fSequenceNumber;
43 	uint32		fAcknowledgmentNumber;
44 	void*		fData;
45 	size_t		fSize;
46 	uint8		fFlags;
47 	TCPPacket*	fNext;
48 };
49 
50 class TCPService;
51 
52 enum TCPSocketState {
53 	TCP_SOCKET_STATE_INITIAL,
54 	TCP_SOCKET_STATE_SYN_SENT,
55 	TCP_SOCKET_STATE_SYN_RECEIVED,
56 	TCP_SOCKET_STATE_OPEN,
57 	TCP_SOCKET_STATE_FIN_SENT,
58 	TCP_SOCKET_STATE_CLOSED
59 };
60 
61 class TCPSocket {
62 public:
63 	TCPSocket();
64 	~TCPSocket();
65 
Address()66 	ip_addr_t Address() const	{ return fAddress; }
Port()67 	uint16 Port() const			{ return fPort; }
68 	uint16 WindowSize() const;
69 
70 	status_t Connect(ip_addr_t address, uint16 port);
71 	status_t Close();
72 	status_t Read(void* buffer, size_t bufferSize, size_t* bytesRead, bigtime_t timeout = 0);
73 	status_t Write(const void* buffer, size_t bufferSize);
74 
75 	void Acknowledge(uint32 number);
76 	void ProcessPacket(TCPPacket* packet);
77 
78 private:
79 	TCPPacket* _PeekPacket();
80 	TCPPacket* _DequeuePacket();
81 	status_t _Send(TCPPacket* packet, bool enqueue = true);
82 	status_t _ResendQueue();
83 	void _EnqueueOutgoingPacket(TCPPacket* packet);
84 	inline void	_DumpQueue();
85 	status_t _WaitForState(TCPSocketState state, bigtime_t timeout = 0);
86 	status_t _Ack();
87 
88 	TCPService*	fTCPService;
89 	ip_addr_t	fAddress;
90 	uint16		fPort;
91 	ip_addr_t	fRemoteAddress;
92 	uint16		fRemotePort;
93 	uint32		fSequenceNumber;
94 	uint32		fAcknowledgeNumber;
95 	uint32		fNextSequence;
96 	TCPPacket*	fFirstPacket;
97 	TCPPacket*	fLastPacket;
98 	TCPPacket*	fFirstSentPacket;
99 	TCPPacket*	fLastSentPacket;
100 	TCPSocketState fState;
101 	TCPSocketState fRemoteState;
102 };
103 
104 class TCPService : public IPSubService {
105 public:
106 	TCPService(IPService* ipService);
107 	virtual ~TCPService();
108 
109 	status_t Init();
110 
111 	virtual uint8 IPProtocol() const;
112 
113 	virtual void HandleIPPacket(IPService* ipService, ip_addr_t sourceIP,
114 		ip_addr_t destinationIP, const void* data, size_t size);
115 
116 	status_t Send(uint16 sourcePort, ip_addr_t destinationAddress,
117 		uint16 destinationPort, uint32 sequenceNumber,
118 		uint32 acknowledgmentNumber, uint8 flags, uint16 windowSize,
119 		ChainBuffer* buffer);
120 
121 	void ProcessIncomingPackets();
122 
123 	status_t BindSocket(TCPSocket* socket);
124 	void UnbindSocket(TCPSocket* socket);
125 
126 private:
127 	uint16 _ChecksumBuffer(ChainBuffer* buffer, ip_addr_t source,
128 		ip_addr_t destination, uint16 length);
129 	uint16 _ChecksumData(const void* data, uint16 length, ip_addr_t source,
130 		ip_addr_t destination);
131 
132 	TCPSocket* _FindSocket(ip_addr_t address, uint16 port);
133 
134 	IPService*			fIPService;
135 	Vector<TCPSocket*>	fSockets;
136 };
137 
138 
139 #endif
140