Implemented non-blocking recv and recvfrom.
[reactos.git] / reactos / drivers / net / tcpip / include / titypes.h
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS TCP/IP protocol driver
4 * FILE: include/titypes.h
5 * PURPOSE: TCP/IP protocol driver types
6 */
7 #ifndef __TITYPES_H
8 #define __TITYPES_H
9
10
11 #ifdef DBG
12
13 #define DEFINE_TAG ULONG Tag;
14 #define INIT_TAG(_Object, _Tag) \
15 ((_Object)->Tag = (_Tag))
16
17 #define DEBUG_REFCHECK(Object) { \
18 if ((Object)->RefCount <= 0) { \
19 TI_DbgPrint(MIN_TRACE, ("Object at (0x%X) has invalid reference count (%d).\n", \
20 (Object), (Object)->RefCount)); \
21 } \
22 }
23
24 /*
25 * VOID ReferenceObject(
26 * PVOID Object)
27 */
28 #define ReferenceObject(Object) \
29 { \
30 CHAR c1, c2, c3, c4; \
31 \
32 c1 = ((Object)->Tag >> 24) & 0xFF; \
33 c2 = ((Object)->Tag >> 16) & 0xFF; \
34 c3 = ((Object)->Tag >> 8) & 0xFF; \
35 c4 = ((Object)->Tag & 0xFF); \
36 \
37 DEBUG_REFCHECK(Object); \
38 TI_DbgPrint(DEBUG_REFCOUNT, ("Referencing object of type (%c%c%c%c) at (0x%X). RefCount (%d).\n", \
39 c4, c3, c2, c1, (Object), (Object)->RefCount)); \
40 \
41 InterlockedIncrement(&((Object)->RefCount)); \
42 }
43
44 /*
45 * VOID DereferenceObject(
46 * PVOID Object)
47 */
48 #define DereferenceObject(Object) \
49 { \
50 CHAR c1, c2, c3, c4; \
51 \
52 c1 = ((Object)->Tag >> 24) & 0xFF; \
53 c2 = ((Object)->Tag >> 16) & 0xFF; \
54 c3 = ((Object)->Tag >> 8) & 0xFF; \
55 c4 = ((Object)->Tag & 0xFF); \
56 \
57 DEBUG_REFCHECK(Object); \
58 TI_DbgPrint(DEBUG_REFCOUNT, ("Dereferencing object of type (%c%c%c%c) at (0x%X). RefCount (%d).\n", \
59 c4, c3, c2, c1, (Object), (Object)->RefCount)); \
60 \
61 if (InterlockedDecrement(&((Object)->RefCount)) == 0) \
62 (((Object)->Free)(Object)); \
63 }
64
65 #else /* DBG */
66
67 #define DEFINE_TAG
68 #define INIT_TAG(Object, Tag)
69
70 /*
71 * VOID ReferenceObject(
72 * PVOID Object)
73 */
74 #define ReferenceObject(Object) \
75 { \
76 InterlockedIncrement(&((Object)->RefCount)); \
77 }
78
79 /*
80 * VOID DereferenceObject(
81 * PVOID Object)
82 */
83 #define DereferenceObject(Object) \
84 { \
85 if (InterlockedDecrement(&((Object)->RefCount)) == 0) \
86 (((Object)->Free)(Object)); \
87 }
88
89 #endif /* DBG */
90
91
92 #include <ip.h>
93
94 struct _ADDRESS_FILE;
95
96 /***************************************************
97 * Connection-less communication support structures *
98 ***************************************************/
99
100 typedef NTSTATUS (*DATAGRAM_SEND_ROUTINE)(
101 struct _ADDRESS_FILE *AddrFile,
102 PTDI_CONNECTION_INFORMATION ConnInfo,
103 PCHAR Buffer,
104 ULONG DataSize,
105 PULONG DataUsed);
106
107 /* Datagram completion handler prototype */
108 typedef VOID (*DATAGRAM_COMPLETION_ROUTINE)(
109 PVOID Context,
110 NDIS_STATUS Status,
111 ULONG Count);
112
113 typedef DATAGRAM_COMPLETION_ROUTINE PDATAGRAM_COMPLETION_ROUTINE;
114
115 typedef struct _DATAGRAM_RECEIVE_REQUEST {
116 LIST_ENTRY ListEntry; /* Entry on list */
117 IP_ADDRESS RemoteAddress; /* Remote address we receive from (NULL means any) */
118 USHORT RemotePort; /* Remote port we receive from (0 means any) */
119 PTDI_CONNECTION_INFORMATION ReturnInfo;/* Return information */
120 PCHAR Buffer; /* Pointer to receive buffer */
121 ULONG BufferSize; /* Size of Buffer */
122 DATAGRAM_COMPLETION_ROUTINE Complete; /* Completion routine */
123 PVOID Context; /* Pointer to context information */
124 DATAGRAM_COMPLETION_ROUTINE UserComplete; /* Completion routine */
125 PVOID UserContext; /* Pointer to context information */
126 } DATAGRAM_RECEIVE_REQUEST, *PDATAGRAM_RECEIVE_REQUEST;
127
128 /* Datagram build routine prototype */
129 typedef NTSTATUS (*DATAGRAM_BUILD_ROUTINE)(
130 PVOID Context,
131 PIP_ADDRESS LocalAddress,
132 USHORT LocalPort,
133 PIP_PACKET *IPPacket);
134
135 typedef struct _DATAGRAM_SEND_REQUEST {
136 LIST_ENTRY ListEntry;
137 PNDIS_PACKET PacketToSend;
138 DATAGRAM_COMPLETION_ROUTINE Complete; /* Completion routine */
139 PVOID Context; /* Pointer to context information */
140 IP_PACKET Packet;
141 UINT BufferSize;
142 IP_ADDRESS RemoteAddress;
143 USHORT RemotePort;
144 ULONG Flags; /* Protocol specific flags */
145 } DATAGRAM_SEND_REQUEST, *PDATAGRAM_SEND_REQUEST;
146
147 #if 0
148 #define InitializeDatagramSendRequest( \
149 _SendRequest, \
150 _RemoteAddress, \
151 _RemotePort, \
152 _Buffer, \
153 _BufferSize, \
154 _Complete, \
155 _Context, \
156 _Build, \
157 _Flags) { \
158 (_SendRequest)->RemoteAddress = (_RemoteAddress); \
159 (_SendRequest)->RemotePort = (_RemotePort); \
160 (_SendRequest)->Buffer = (_Buffer); \
161 (_SendRequest)->BufferSize = (_BufferSize); \
162 (_SendRequest)->Complete = (_Complete); \
163 (_SendRequest)->Context = (_Context); \
164 (_SendRequest)->Build = (_Build); \
165 (_SendRequest)->Flags = (_Flags); \
166 }
167 #endif /* These things bug me... They hide the member names. */
168
169 /* Transport address file context structure. The FileObject->FsContext2
170 field holds a pointer to this structure */
171 typedef struct _ADDRESS_FILE {
172 DEFINE_TAG
173 LIST_ENTRY ListEntry; /* Entry on list */
174 KSPIN_LOCK Lock; /* Spin lock to manipulate this structure */
175 OBJECT_FREE_ROUTINE Free; /* Routine to use to free resources for the object */
176 USHORT Flags; /* Flags for address file (see below) */
177 PADDRESS_ENTRY ADE; /* Associated address entry */
178 USHORT Protocol; /* Protocol number */
179 USHORT Port; /* Network port (network byte order) */
180 WORK_QUEUE_ITEM WorkItem; /* Work queue item handle */
181 DATAGRAM_COMPLETION_ROUTINE Complete; /* Completion routine for delete request */
182 PVOID Context; /* Delete request context */
183 DATAGRAM_SEND_ROUTINE Send; /* Routine to send a datagram */
184 LIST_ENTRY ReceiveQueue; /* List of outstanding receive requests */
185 LIST_ENTRY TransmitQueue; /* List of outstanding transmit requests */
186 struct _CONNECTION_ENDPOINT *Connection;
187 /* Associated connection or NULL if no
188 associated connection exist */
189 IP_ADDRESS AddrCache; /* One entry address cache (destination
190 address of last packet transmitted) */
191
192 /* The following members are used to control event notification */
193
194 /* Connection indication handler */
195 PTDI_IND_CONNECT ConnectHandler;
196 PVOID ConnectHandlerContext;
197 BOOL RegisteredConnectHandler;
198 /* Disconnect indication handler */
199 PTDI_IND_DISCONNECT DisconnectHandler;
200 PVOID DisconnectHandlerContext;
201 BOOL RegisteredDisconnectHandler;
202 /* Error indication handler */
203 PTDI_IND_ERROR ErrorHandler;
204 PVOID ErrorHandlerContext;
205 PVOID ErrorHandlerOwner;
206 BOOL RegisteredErrorHandler;
207 /* Receive indication handler */
208 PTDI_IND_RECEIVE ReceiveHandler;
209 PVOID ReceiveHandlerContext;
210 BOOL RegisteredReceiveHandler;
211 /* Receive datagram indication handler */
212 PTDI_IND_RECEIVE_DATAGRAM ReceiveDatagramHandler;
213 PVOID ReceiveDatagramHandlerContext;
214 BOOL RegisteredReceiveDatagramHandler;
215 /* Expedited receive indication handler */
216 PTDI_IND_RECEIVE_EXPEDITED ExpeditedReceiveHandler;
217 PVOID ExpeditedReceiveHandlerContext;
218 BOOL RegisteredExpeditedReceiveHandler;
219 /* Chained receive indication handler */
220 PTDI_IND_CHAINED_RECEIVE ChainedReceiveHandler;
221 PVOID ChainedReceiveHandlerContext;
222 BOOL RegisteredChainedReceiveHandler;
223 /* Chained receive datagram indication handler */
224 PTDI_IND_CHAINED_RECEIVE_DATAGRAM ChainedReceiveDatagramHandler;
225 PVOID ChainedReceiveDatagramHandlerContext;
226 BOOL RegisteredChainedReceiveDatagramHandler;
227 /* Chained expedited receive indication handler */
228 PTDI_IND_CHAINED_RECEIVE_EXPEDITED ChainedReceiveExpeditedHandler;
229 PVOID ChainedReceiveExpeditedHandlerContext;
230 BOOL RegisteredChainedReceiveExpeditedHandler;
231 } ADDRESS_FILE, *PADDRESS_FILE;
232
233 /* Address File Flag constants */
234 #define AFF_VALID 0x0001 /* Address file object is valid for use */
235 #define AFF_BUSY 0x0002 /* Address file object is exclusive to someone */
236 #define AFF_DELETE 0x0004 /* Address file object is sheduled to be deleted */
237 #define AFF_SEND 0x0008 /* A send request is pending */
238 #define AFF_RECEIVE 0x0010 /* A receive request is pending */
239 #define AFF_PENDING 0x001C /* A request is pending */
240
241 /* Macros for manipulating address file object flags */
242
243 #define AF_IS_VALID(ADF) ((ADF)->Flags & AFF_VALID)
244 #define AF_SET_VALID(ADF) ((ADF)->Flags |= AFF_VALID)
245 #define AF_CLR_VALID(ADF) ((ADF)->Flags &= ~AFF_VALID)
246
247 #define AF_IS_BUSY(ADF) ((ADF)->Flags & AFF_BUSY)
248 #define AF_SET_BUSY(ADF) ((ADF)->Flags |= AFF_BUSY)
249 #define AF_CLR_BUSY(ADF) ((ADF)->Flags &= ~AFF_BUSY)
250
251 #define AF_IS_PENDING(ADF, X) (ADF->Flags & X)
252 #define AF_SET_PENDING(ADF, X) (ADF->Flags |= X)
253 #define AF_CLR_PENDING(ADF, X) (ADF->Flags &= ~X)
254
255
256 /* Structure used to search through Address Files */
257 typedef struct _AF_SEARCH {
258 PLIST_ENTRY Next; /* Next address file to check */
259 PIP_ADDRESS Address; /* Pointer to address to be found */
260 USHORT Port; /* Network port */
261 USHORT Protocol; /* Protocol number */
262 } AF_SEARCH, *PAF_SEARCH;
263
264 /*******************************************************
265 * Connection-oriented communication support structures *
266 *******************************************************/
267
268 typedef struct _TCP_RECEIVE_REQUEST {
269 LIST_ENTRY ListEntry; /* Entry on list */
270 PNDIS_BUFFER Buffer; /* Pointer to receive buffer */
271 ULONG BufferSize; /* Size of Buffer */
272 DATAGRAM_COMPLETION_ROUTINE Complete; /* Completion routine */
273 PVOID Context; /* Pointer to context information */
274 } TCP_RECEIVE_REQUEST, *PTCP_RECEIVE_REQUEST;
275
276 /* Connection states */
277 typedef enum {
278 ctListen = 0, /* Waiting for incoming connection requests */
279 ctSynSent, /* Waiting for matching connection request */
280 ctSynReceived, /* Waiting for connection request acknowledgment */
281 ctEstablished, /* Connection is open for data transfer */
282 ctFinWait1, /* Waiting for termination request or ack. for same */
283 ctFinWait2, /* Waiting for termination request from remote TCP */
284 ctCloseWait, /* Waiting for termination request from local user */
285 ctClosing, /* Waiting for termination ack. from remote TCP */
286 ctLastAck, /* Waiting for termination request ack. from remote TCP */
287 ctTimeWait, /* Waiting for enough time to pass to be sure the remote TCP
288 received the ack. of its connection termination request */
289 ctClosed /* Represents a closed connection */
290 } CONNECTION_STATE, *PCONNECTION_STATE;
291
292
293 /* Structure for an TCP segment */
294 typedef struct _TCP_SEGMENT {
295 LIST_ENTRY ListEntry;
296 PIP_PACKET IPPacket; /* Pointer to IP packet */
297 PVOID SegmentData; /* Pointer to segment data */
298 ULONG SequenceNumber; /* Sequence number of first byte in segment */
299 ULONG Length; /* Number of bytes in segment */
300 ULONG BytesDelivered; /* Number of bytes already delivered to the client */
301 } TCP_SEGMENT, *PTCP_SEGMENT;
302
303 typedef struct _TDI_BUCKET {
304 LIST_ENTRY Entry;
305 TDI_REQUEST Request;
306 } TDI_BUCKET, *PTDI_BUCKET;
307
308 /* Transport connection context structure A.K.A. Transmission Control Block
309 (TCB) in TCP terminology. The FileObject->FsContext2 field holds a pointer
310 to this structure */
311 typedef struct _CONNECTION_ENDPOINT {
312 LIST_ENTRY ListEntry; /* Entry on list */
313 KSPIN_LOCK Lock; /* Spin lock to protect this structure */
314 PVOID ClientContext; /* Pointer to client context information */
315 PADDRESS_FILE AddressFile; /* Associated address file object (NULL if none) */
316 PVOID SocketContext; /* Context for lower layer */
317
318 UINT State; /* Socket state W.R.T. oskit */
319
320 /* Requests */
321 LIST_ENTRY ConnectRequest; /* Queued connect rqueusts */
322 LIST_ENTRY ListenRequest; /* Queued listen requests */
323 LIST_ENTRY ReceiveRequest; /* Queued receive requests */
324
325 /* Queues */
326 LIST_ENTRY ReceivedSegments;/* Segments that are received */
327 } CONNECTION_ENDPOINT, *PCONNECTION_ENDPOINT;
328
329
330
331 /*************************
332 * TDI support structures *
333 *************************/
334
335 /* Transport control channel context structure. The FileObject->FsContext2
336 field holds a pointer to this structure */
337 typedef struct _CONTROL_CHANNEL {
338 LIST_ENTRY ListEntry; /* Entry on list */
339 KSPIN_LOCK Lock; /* Spin lock to protect this structure */
340 } CONTROL_CHANNEL, *PCONTROL_CHANNEL;
341
342 /* Transport (TCP/UDP) endpoint context structure. The FileObject->FsContext
343 field holds a pointer to this structure */
344 typedef struct _TRANSPORT_CONTEXT {
345 union {
346 HANDLE AddressHandle;
347 CONNECTION_CONTEXT ConnectionContext;
348 HANDLE ControlChannel;
349 } Handle;
350 BOOL CancelIrps;
351 KEVENT CleanupEvent;
352 } TRANSPORT_CONTEXT, *PTRANSPORT_CONTEXT;
353
354 typedef struct _TI_QUERY_CONTEXT {
355 PIRP Irp;
356 PMDL InputMdl;
357 PMDL OutputMdl;
358 TCP_REQUEST_QUERY_INFORMATION_EX QueryInfo;
359 } TI_QUERY_CONTEXT, *PTI_QUERY_CONTEXT;
360
361 #endif /* __TITYPES_H */
362
363 /* EOF */