remove whitespace from end of lines
[reactos.git] / reactos / drivers / net / afd / afd / tdiconn.c
1 /* $Id$
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: drivers/net/afd/afd/tdiconn.c
5 * PURPOSE: Ancillary functions driver
6 * PROGRAMMER: Art Yerkes (ayerkes@speakeasy.net)
7 * UPDATE HISTORY:
8 * 20040708 Created
9 */
10 #include <afd.h>
11 #include "debug.h"
12 #include "tdiconn.h"
13
14 UINT TdiAddressSizeFromType( UINT AddressType ) {
15 switch( AddressType ) {
16 case AF_INET:
17 return sizeof(TA_IP_ADDRESS);
18 default:
19 AFD_DbgPrint(MID_TRACE,("TdiAddressSizeFromType - invalid type: %x\n", AddressType));
20 KeBugCheck( 0 );
21 }
22 return 0;
23 }
24
25 UINT TaLengthOfAddress( PTA_ADDRESS Addr ) {
26 UINT AddrLen = 2 * sizeof( USHORT ) + Addr->AddressLength;
27 AFD_DbgPrint(MID_TRACE,("AddrLen %x\n", AddrLen));
28 return AddrLen;
29 }
30
31 UINT TaLengthOfTransportAddress( PTRANSPORT_ADDRESS Addr ) {
32 UINT AddrLen = 2 * sizeof( ULONG ) + Addr->Address[0].AddressLength;
33 AFD_DbgPrint(MID_TRACE,("AddrLen %x\n", AddrLen));
34 return AddrLen;
35 }
36
37 VOID TaCopyAddressInPlace( PTA_ADDRESS Target,
38 PTA_ADDRESS Source ) {
39 UINT AddrLen = TaLengthOfAddress( Source );
40 RtlCopyMemory( Target, Source, AddrLen );
41 }
42
43 PTA_ADDRESS TaCopyAddress( PTA_ADDRESS Source ) {
44 UINT AddrLen = TaLengthOfAddress( Source );
45 PVOID Buffer = ExAllocatePool( NonPagedPool, AddrLen );
46 RtlCopyMemory( Buffer, Source, AddrLen );
47 return Buffer;
48 }
49
50 VOID TaCopyTransportAddressInPlace( PTRANSPORT_ADDRESS Target,
51 PTRANSPORT_ADDRESS Source ) {
52 UINT AddrLen = TaLengthOfTransportAddress( Source );
53 RtlCopyMemory( Target, Source, AddrLen );
54 }
55
56 PTRANSPORT_ADDRESS TaCopyTransportAddress( PTRANSPORT_ADDRESS OtherAddress ) {
57 UINT AddrLen;
58 PTRANSPORT_ADDRESS A;
59
60 ASSERT(OtherAddress->TAAddressCount == 1);
61 AddrLen = TaLengthOfTransportAddress( OtherAddress );
62 A = ExAllocatePool( NonPagedPool, AddrLen );
63
64 if( A )
65 TaCopyTransportAddressInPlace( A, OtherAddress );
66
67 return A;
68 }
69
70 NTSTATUS TdiBuildNullConnectionInfoInPlace
71 ( PTDI_CONNECTION_INFORMATION ConnInfo,
72 ULONG Type )
73 /*
74 * FUNCTION: Builds a NULL TDI connection information structure
75 * ARGUMENTS:
76 * ConnectionInfo = Address of buffer to place connection information
77 * Type = TDI style address type (TDI_ADDRESS_TYPE_XXX).
78 * RETURNS:
79 * Status of operation
80 */
81 {
82 ULONG TdiAddressSize;
83 PTRANSPORT_ADDRESS TransportAddress;
84
85 TdiAddressSize = TdiAddressSizeFromType(Type);
86
87 RtlZeroMemory(ConnInfo,
88 sizeof(TDI_CONNECTION_INFORMATION) +
89 TdiAddressSize);
90
91 ConnInfo->OptionsLength = sizeof(ULONG);
92 ConnInfo->RemoteAddressLength = TdiAddressSize;
93 ConnInfo->RemoteAddress = TransportAddress =
94 (PTRANSPORT_ADDRESS)&ConnInfo[1];
95 TransportAddress->TAAddressCount = 1;
96 TransportAddress->Address[0].AddressType = Type;
97
98 return STATUS_SUCCESS;
99 }
100
101 NTSTATUS TdiBuildNullConnectionInfo
102 ( PTDI_CONNECTION_INFORMATION *ConnectionInfo,
103 ULONG Type )
104 /*
105 * FUNCTION: Builds a NULL TDI connection information structure
106 * ARGUMENTS:
107 * ConnectionInfo = Address of buffer pointer to allocate connection
108 * information in
109 * Type = TDI style address type (TDI_ADDRESS_TYPE_XXX).
110 * RETURNS:
111 * Status of operation
112 */
113 {
114 PTDI_CONNECTION_INFORMATION ConnInfo;
115 ULONG TdiAddressSize;
116 NTSTATUS Status;
117
118 TdiAddressSize = TdiAddressSizeFromType(Type);
119
120 ConnInfo = (PTDI_CONNECTION_INFORMATION)
121 ExAllocatePool(NonPagedPool,
122 sizeof(TDI_CONNECTION_INFORMATION) +
123 TdiAddressSize);
124 if (!ConnInfo)
125 return STATUS_INSUFFICIENT_RESOURCES;
126
127 Status = TdiBuildNullConnectionInfoInPlace( ConnInfo, Type );
128
129 if (!NT_SUCCESS(Status))
130 ExFreePool( ConnInfo );
131 else
132 *ConnectionInfo = ConnInfo;
133
134 ConnInfo->RemoteAddress = (PTA_ADDRESS)&ConnInfo[1];
135 ConnInfo->RemoteAddressLength = TdiAddressSize;
136
137 return Status;
138 }
139
140
141 NTSTATUS
142 TdiBuildConnectionInfoInPlace
143 ( PTDI_CONNECTION_INFORMATION ConnectionInfo,
144 PTRANSPORT_ADDRESS Address ) {
145 NTSTATUS Status = STATUS_SUCCESS;
146
147 RtlCopyMemory( ConnectionInfo->RemoteAddress,
148 Address,
149 ConnectionInfo->RemoteAddressLength );
150
151 return Status;
152 }
153
154
155 NTSTATUS
156 TdiBuildConnectionInfo
157 ( PTDI_CONNECTION_INFORMATION *ConnectionInfo,
158 PTRANSPORT_ADDRESS Address ) {
159 NTSTATUS Status = TdiBuildNullConnectionInfo
160 ( ConnectionInfo, Address->Address[0].AddressType );
161
162 if( NT_SUCCESS(Status) )
163 TdiBuildConnectionInfoInPlace( *ConnectionInfo, Address );
164
165 return Status;
166 }
167
168 NTSTATUS
169 TdiBuildConnectionInfoPair
170 ( PTDI_CONNECTION_INFO_PAIR ConnectionInfo,
171 PTRANSPORT_ADDRESS From, PTRANSPORT_ADDRESS To )
172 /*
173 * FUNCTION: Fill a TDI_CONNECTION_INFO_PAIR struct will the two addresses
174 * given.
175 * ARGUMENTS:
176 * ConnectionInfo: The pair
177 * From: The from address
178 * To: The to address
179 * RETURNS:
180 * Status of the operation
181 */
182 {
183 PCHAR LayoutFrame;
184 UINT SizeOfEntry;
185 ULONG TdiAddressSize;
186
187 /* FIXME: Get from socket information */
188 TdiAddressSize = TdiAddressSizeFromType(From->Address[0].AddressType);
189 SizeOfEntry = TdiAddressSize + sizeof(TDI_CONNECTION_INFORMATION);
190
191 LayoutFrame = (PCHAR)ExAllocatePool(NonPagedPool, 2 * SizeOfEntry);
192
193 if (!LayoutFrame) {
194 AFD_DbgPrint(MIN_TRACE, ("Insufficient resources.\n"));
195 return STATUS_INSUFFICIENT_RESOURCES;
196 }
197
198 RtlZeroMemory( LayoutFrame, 2 * SizeOfEntry );
199
200 PTDI_CONNECTION_INFORMATION
201 FromTdiConn = (PTDI_CONNECTION_INFORMATION)LayoutFrame,
202 ToTdiConn = (PTDI_CONNECTION_INFORMATION)LayoutFrame + SizeOfEntry;
203
204 if (From != NULL) {
205 TdiBuildConnectionInfoInPlace( FromTdiConn, From );
206 } else {
207 TdiBuildNullConnectionInfoInPlace( FromTdiConn,
208 From->Address[0].AddressType );
209 }
210
211 TdiBuildConnectionInfoInPlace( ToTdiConn, To );
212
213 return STATUS_SUCCESS;
214 }
215
216 PTA_ADDRESS TdiGetRemoteAddress( PTDI_CONNECTION_INFORMATION TdiConn )
217 /*
218 * Convenience function that rounds out the abstraction of
219 * the TDI_CONNECTION_INFORMATION struct.
220 */
221 {
222 return TdiConn->RemoteAddress;
223 }
224