:pserver:cvsanon@mok.lvcm.com:/CVS/ReactOS reactos
[reactos.git] / 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
95 /***************************************************
96 * Connection-less communication support structures *
97 ***************************************************/
98
99 typedef NTSTATUS (*DATAGRAM_SEND_ROUTINE)(
100     PTDI_REQUEST Request,
101     PTDI_CONNECTION_INFORMATION ConnInfo,
102     PNDIS_BUFFER Buffer,
103     ULONG DataSize);
104
105 /* Datagram completion handler prototype */
106 typedef VOID (*DATAGRAM_COMPLETION_ROUTINE)(
107     PVOID Context,
108     NDIS_STATUS Status,
109     ULONG Count);
110
111 typedef struct _DATAGRAM_RECEIVE_REQUEST {
112     LIST_ENTRY ListEntry;                   /* Entry on list */
113     PIP_ADDRESS RemoteAddress;              /* Remote address we receive from (NULL means any) */
114     USHORT RemotePort;                      /* Remote port we receive from (0 means any) */
115     PTDI_CONNECTION_INFORMATION ReturnInfo; /* Return information */
116     PNDIS_BUFFER Buffer;                    /* Pointer to receive buffer */
117     ULONG BufferSize;                       /* Size of Buffer */
118     DATAGRAM_COMPLETION_ROUTINE Complete;   /* Completion routine */
119     PVOID Context;                          /* Pointer to context information */
120 } DATAGRAM_RECEIVE_REQUEST, *PDATAGRAM_RECEIVE_REQUEST;
121
122 /* Datagram build routine prototype */
123 typedef NTSTATUS (*DATAGRAM_BUILD_ROUTINE)(
124     PVOID Context,
125     PIP_ADDRESS LocalAddress,
126     USHORT LocalPort,
127     PIP_PACKET *IPPacket);
128
129 typedef struct _DATAGRAM_SEND_REQUEST {
130     LIST_ENTRY ListEntry;                 /* Entry on list */
131     PIP_ADDRESS RemoteAddress;            /* Pointer to remote IP address */
132     USHORT RemotePort;                    /* Remote port number */
133     PNDIS_BUFFER Buffer;                  /* Pointer to NDIS buffer to send */
134     DWORD BufferSize;                     /* Size of Buffer */
135     DATAGRAM_COMPLETION_ROUTINE Complete; /* Completion routine */
136     PVOID Context;                        /* Pointer to context information */
137     DATAGRAM_BUILD_ROUTINE Build;         /* Datagram build routine */
138     ULONG Flags;                          /* Protocol specific flags */
139 } DATAGRAM_SEND_REQUEST, *PDATAGRAM_SEND_REQUEST;
140
141 #define InitializeDatagramSendRequest( \
142   _SendRequest, \
143   _RemoteAddress, \
144   _RemotePort, \
145   _Buffer, \
146   _BufferSize, \
147   _Complete, \
148   _Context, \
149   _Build, \
150   _Flags) { \
151     (_SendRequest)->RemoteAddress = (_RemoteAddress); \
152     (_SendRequest)->RemotePort = (_RemotePort); \
153     (_SendRequest)->Buffer = (_Buffer); \
154     (_SendRequest)->BufferSize = (_BufferSize); \
155     (_SendRequest)->Complete = (_Complete); \
156     (_SendRequest)->Context = (_Context); \
157     (_SendRequest)->Build = (_Build); \
158     (_SendRequest)->Flags = (_Flags); \
159   }
160
161 /* Transport address file context structure. The FileObject->FsContext2
162    field holds a pointer to this structure */
163 typedef struct _ADDRESS_FILE {
164     DEFINE_TAG
165     LIST_ENTRY ListEntry;                 /* Entry on list */
166     KSPIN_LOCK Lock;                      /* Spin lock to manipulate this structure */
167     ULONG RefCount;                       /* Number of references to this object */
168     OBJECT_FREE_ROUTINE Free;             /* Routine to use to free resources for the object */
169     USHORT Flags;                         /* Flags for address file (see below) */
170     PADDRESS_ENTRY ADE;                   /* Associated address entry */
171     USHORT Protocol;                      /* Protocol number */
172     USHORT Port;                          /* Network port (network byte order) */
173     WORK_QUEUE_ITEM WorkItem;             /* Work queue item handle */
174     DATAGRAM_COMPLETION_ROUTINE Complete; /* Completion routine for delete request */
175     PVOID Context;                        /* Delete request context */
176     DATAGRAM_SEND_ROUTINE Send;           /* Routine to send a datagram */
177     LIST_ENTRY ReceiveQueue;              /* List of outstanding receive requests */
178     LIST_ENTRY TransmitQueue;             /* List of outstanding transmit requests */
179     LIST_ENTRY Connections;               /* List of associated connections */
180     PIP_ADDRESS AddrCache;                /* One entry address cache (destination
181                                              address of last packet transmitted) */
182
183     /* The following members are used to control event notification */
184
185     /* Connection indication handler */
186     PTDI_IND_CONNECT ConnectHandler;
187     PVOID ConnectHandlerContext;
188     BOOL RegisteredConnectHandler;
189     /* Disconnect indication handler */
190     PTDI_IND_DISCONNECT DisconnectHandler;
191     PVOID DisconnectHandlerContext;
192     BOOL RegisteredDisconnectHandler;
193     /* Error indication handler */
194     PTDI_IND_ERROR ErrorHandler;
195     PVOID ErrorHandlerContext;
196     PVOID ErrorHandlerOwner;
197     BOOL RegisteredErrorHandler;
198     /* Receive indication handler */
199     PTDI_IND_RECEIVE ReceiveHandler;
200     PVOID ReceiveHandlerContext;
201     BOOL RegisteredReceiveHandler;
202     /* Receive datagram indication handler */
203     PTDI_IND_RECEIVE_DATAGRAM ReceiveDatagramHandler;
204     PVOID ReceiveDatagramHandlerContext;
205     BOOL RegisteredReceiveDatagramHandler;
206     /* Expedited receive indication handler */
207     PTDI_IND_RECEIVE_EXPEDITED ExpeditedReceiveHandler;
208     PVOID ExpeditedReceiveHandlerContext;
209     BOOL RegisteredExpeditedReceiveHandler;
210     /* Chained receive indication handler */
211     PTDI_IND_CHAINED_RECEIVE ChainedReceiveHandler;
212     PVOID ChainedReceiveHandlerContext;
213     BOOL RegisteredChainedReceiveHandler;
214     /* Chained receive datagram indication handler */
215     PTDI_IND_CHAINED_RECEIVE_DATAGRAM ChainedReceiveDatagramHandler;
216     PVOID ChainedReceiveDatagramHandlerContext;
217     BOOL RegisteredChainedReceiveDatagramHandler;
218     /* Chained expedited receive indication handler */
219     PTDI_IND_CHAINED_RECEIVE_EXPEDITED ChainedReceiveExpeditedHandler;
220     PVOID ChainedReceiveExpeditedHandlerContext;
221     BOOL RegisteredChainedReceiveExpeditedHandler;
222 } ADDRESS_FILE, *PADDRESS_FILE;
223
224 /* Address File Flag constants */
225 #define AFF_VALID    0x0001 /* Address file object is valid for use */
226 #define AFF_BUSY     0x0002 /* Address file object is exclusive to someone */
227 #define AFF_DELETE   0x0004 /* Address file object is sheduled to be deleted */
228 #define AFF_SEND     0x0008 /* A send request is pending */
229 #define AFF_RECEIVE  0x0010 /* A receive request is pending */
230 #define AFF_PENDING  0x001C /* A request is pending */
231
232 /* Macros for manipulating address file object flags */
233
234 #define AF_IS_VALID(ADF)  ((ADF)->Flags & AFF_VALID)
235 #define AF_SET_VALID(ADF) ((ADF)->Flags |= AFF_VALID)
236 #define AF_CLR_VALID(ADF) ((ADF)->Flags &= ~AFF_VALID)
237
238 #define AF_IS_BUSY(ADF)  ((ADF)->Flags & AFF_BUSY)
239 #define AF_SET_BUSY(ADF) ((ADF)->Flags |= AFF_BUSY)
240 #define AF_CLR_BUSY(ADF) ((ADF)->Flags &= ~AFF_BUSY)
241
242 #define AF_IS_PENDING(ADF, X)  (ADF->Flags & X)
243 #define AF_SET_PENDING(ADF, X) (ADF->Flags |= X)
244 #define AF_CLR_PENDING(ADF, X) (ADF->Flags &= ~X)
245
246
247 /* Structure used to search through Address Files */
248 typedef struct _AF_SEARCH {
249     PLIST_ENTRY Next;       /* Next address file to check */
250     PIP_ADDRESS Address;    /* Pointer to address to be found */
251     USHORT Port;            /* Network port */
252     USHORT Protocol;        /* Protocol number */
253 } AF_SEARCH, *PAF_SEARCH;
254
255
256
257 /*******************************************************
258 * Connection-oriented communication support structures *
259 *******************************************************/
260
261 typedef struct _TCP_SEND_REQUEST {
262   LIST_ENTRY ListEntry;                 /* Entry on list */
263   DATAGRAM_COMPLETION_ROUTINE Complete; /* Completion routine */
264   PVOID Context;                        /* Pointer to context information */
265   PVOID ProtocolContext;                /* Protocol specific context */
266   ULONG Flags;                          /* Protocol specific flags */
267 } TCP_SEND_REQUEST, *PTCP_SEND_REQUEST;
268
269 #define InitializeTCPSendRequest( \
270   _SendRequest, \
271   _Complete, \
272   _Context, \
273   _ProtocolContext) { \
274     (_SendRequest)->Complete = (_Complete); \
275     (_SendRequest)->Context = (_Context); \
276     (_SendRequest)->ProtocolContext = (_ProtocolContext); \
277   }
278
279
280 /* Connection states */
281 typedef enum {
282   ctListen = 0,   /* Waiting for incoming connection requests */
283   ctSynSent,      /* Waiting for matching connection request */
284   ctSynReceived,  /* Waiting for connection request acknowledgment */
285   ctEstablished,  /* Connection is open for data transfer */
286   ctFinWait1,     /* Waiting for termination request or ack. for same */
287   ctFinWait2,     /* Waiting for termination request from remote TCP */
288   ctCloseWait,    /* Waiting for termination request from local user */
289   ctClosing,      /* Waiting for termination ack. from remote TCP */
290   ctLastAck,      /* Waiting for termination request ack. from remote TCP */
291   ctTimeWait,     /* Waiting for enough time to pass to be sure the remote TCP
292                      received the ack. of its connection termination request */
293   ctClosed        /* Represents a closed connection */
294 } CONNECTION_STATE, *PCONNECTION_STATE;
295
296
297 /* Transport connection context structure A.K.A. Transmission Control Block
298    (TCB) in TCP terminology. The FileObject->FsContext2 field holds a pointer
299    to this structure */
300 typedef struct _CONNECTION_ENDPOINT {
301   LIST_ENTRY ListEntry;       /* Entry on list */
302   LIST_ENTRY AddrFileEntry;   /* Entry on address file list */
303   KSPIN_LOCK Lock;            /* Spin lock to protect this structure */
304   ULONG RefCount;             /* Number of references to this object */
305   PVOID ClientContext;        /* Pointer to client context information */
306   PADDRESS_FILE AddressFile;  /* Associated address file object (NULL if none) */
307
308   CONNECTION_STATE State;     /* Connection state */
309
310   PIP_ADDRESS LocalAddress;   /* Pointer to local IP address */
311   USHORT LocalPort;           /* Local port number */
312
313   PIP_ADDRESS RemoteAddress;  /* Pointer to remote IP address */
314   USHORT RemotePort;          /* Remote port number */
315
316   /* Send sequence variables */
317   ULONG SendUnacknowledged;   /* Highest sequence number that is acknowledged */
318   ULONG SendNext;             /* Sequence number of last data block sent */
319   ULONG SendWindow;           /* Maximum allowed number of octets in a segment */
320   ULONG SendUrgentPointer;    /* Sequence number of start of urgent data */
321   ULONG SendWL1;              /* Sequence number used for last window update */
322   ULONG SendWL2;              /* Acknowledgment number used for last window update */
323   ULONG SendISS;              /* Initial send sequence number */
324
325   /* Receive sequence variables */
326   ULONG RecvNext;             /* Sequence number of last data block received */
327   ULONG RecvWindow;           /* Maximum allowed number of octets in a segment */
328   ULONG RecvUrgentPointer;    /* Sequence number of start of urgent data */
329   ULONG RecvIRS;              /* Initial receive sequence number */
330
331   /* Statistics for computing the retransmission timeout */
332   ULONG TimestampSend;        /* Timestamp when sending a segment */
333   ULONG TimestampAck;         /* Timestamp when receiving acknowledgment */
334 } CONNECTION_ENDPOINT, *PCONNECTION_ENDPOINT;
335
336
337
338 /*************************
339 * TDI support structures *
340 *************************/
341
342 /* Transport control channel context structure. The FileObject->FsContext2
343    field holds a pointer to this structure */
344 typedef struct _CONTROL_CHANNEL {
345     LIST_ENTRY ListEntry;       /* Entry on list */
346     KSPIN_LOCK Lock;            /* Spin lock to protect this structure */
347     ULONG RefCount;             /* Number of references to this object */
348 } CONTROL_CHANNEL, *PCONTROL_CHANNEL;
349
350 /* Transport (TCP/UDP) endpoint context structure. The FileObject->FsContext
351    field holds a pointer to this structure */
352 typedef struct _TRANSPORT_CONTEXT {
353     union {
354         HANDLE AddressHandle;
355         CONNECTION_CONTEXT ConnectionContext;
356         HANDLE ControlChannel;
357     } Handle;
358     ULONG RefCount;
359     BOOL CancelIrps;
360     KEVENT CleanupEvent;
361 } TRANSPORT_CONTEXT, *PTRANSPORT_CONTEXT;
362
363 typedef struct _TI_QUERY_CONTEXT {
364     PIRP Irp;
365     PMDL InputMdl;
366     PMDL OutputMdl;
367     TCP_REQUEST_QUERY_INFORMATION_EX QueryInfo;
368 } TI_QUERY_CONTEXT, *PTI_QUERY_CONTEXT;
369
370 #endif /* __TITYPES_H */
371
372 /* EOF */