xref: /haiku/src/add-ons/kernel/network/protocols/tcp/tcp.h (revision bae87c9140b843a16dd71b5ed9cf467f44b57b98)
1 /*
2  * Copyright 2006, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Andrew Galante, haiku.galante@gmail.com
7  */
8 #ifndef TCP_H
9 #define TCP_H
10 
11 
12 #include <net_buffer.h>
13 #include <net_datalink.h>
14 #include <net_stack.h>
15 
16 #include <util/khash.h>
17 
18 #include <sys/socket.h>
19 
20 
21 typedef enum {
22 	// establishing a connection
23 	CLOSED,
24 	LISTEN,
25 	SYNCHRONIZE_SENT,
26 	SYNCHRONIZE_RECEIVED,
27 	ESTABLISHED,
28 
29 	// peer closes the connection
30 	FINISH_RECEIVED,
31 	WAIT_FOR_FINISH_ACKNOWLEDGE,
32 
33 	// we close the connection
34 	FINISH_SENT,
35 	FINISH_ACKNOWLEDGED,
36 	CLOSING,
37 	TIME_WAIT
38 } tcp_state;
39 
40 struct tcp_header {
41 	uint16 source_port;
42 	uint16 destination_port;
43 	uint32 sequence_num;
44 	uint32 acknowledge_num;
45 	struct {
46 #if B_HOST_IS_LENDIAN == 1
47 		uint8 reserved : 4;
48 		uint8 header_length : 4;
49 #else
50 		uint8 header_length : 4;
51 		uint8 reserved : 4;
52 #endif
53 	};
54 	uint8  flags;
55 	uint16 advertised_window;
56 	uint16 checksum;
57 	uint16 urgent_ptr;
58 };
59 
60 // TCP flag constants
61 #define TCP_FLAG_FINISH			0x01
62 #define TCP_FLAG_SYNCHRONIZE	0x02
63 #define TCP_FLAG_RESET			0x04
64 #define TCP_FLAG_PUSH			0x08
65 #define TCP_FLAG_ACKNOWLEDGE	0x10
66 #define TCP_FLAG_URGENT			0x20
67 #define TCP_FLAG_ECN			0x40 // Explicit Congestion Notification echo
68 #define TCP_FLAG_CWR			0x80 // Congestion Window Reduced
69 
70 struct tcp_connection_key {
71 	const sockaddr	*local;
72 	const sockaddr	*peer;
73 };
74 
75 
76 extern net_domain *gDomain;
77 extern net_address_module_info *gAddressModule;
78 extern net_buffer_module_info *gBufferModule;
79 extern net_datalink_module_info *gDatalinkModule;
80 extern net_stack_module_info *gStackModule;
81 extern hash_table *gConnectionHash;
82 extern benaphore gConnectionLock;
83 
84 
85 status_t add_tcp_header(net_buffer *buffer, uint16 flags, uint32 sequence,
86 	uint32 ack, uint16 advertisedWindow);
87 
88 #endif	// TCP_H
89