Sync with trunk r63637.
[reactos.git] / drivers / network / afd / afd / tdiconn.c
1 /*
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
11 #include <afd.h>
12
13 UINT TdiAddressSizeFromType( UINT AddressType ) {
14 switch( AddressType ) {
15 case TDI_ADDRESS_TYPE_IP:
16 return TDI_ADDRESS_LENGTH_IP;
17 case TDI_ADDRESS_TYPE_APPLETALK:
18 return TDI_ADDRESS_LENGTH_APPLETALK;
19 case TDI_ADDRESS_TYPE_NETBIOS:
20 return TDI_ADDRESS_LENGTH_NETBIOS;
21 /* case TDI_ADDRESS_TYPE_NS: */
22 case TDI_ADDRESS_TYPE_IPX:
23 return TDI_ADDRESS_LENGTH_IPX;
24 case TDI_ADDRESS_TYPE_VNS:
25 return TDI_ADDRESS_LENGTH_VNS;
26 default:
27 DbgPrint("TdiAddressSizeFromType - invalid type: %x\n", AddressType);
28 return 0;
29 }
30 }
31
32 UINT TaLengthOfAddress( PTA_ADDRESS Addr )
33 {
34 UINT AddrLen = Addr->AddressLength;
35
36 if (!AddrLen)
37 return 0;
38
39 AddrLen += 2 * sizeof( USHORT );
40
41 AFD_DbgPrint(MID_TRACE,("AddrLen %x\n", AddrLen));
42
43 return AddrLen;
44 }
45
46 UINT TaLengthOfTransportAddress( PTRANSPORT_ADDRESS Addr )
47 {
48 UINT AddrLen = TaLengthOfAddress(&Addr->Address[0]);
49
50 if (!AddrLen)
51 return 0;
52
53 AddrLen += sizeof(ULONG);
54
55 AFD_DbgPrint(MID_TRACE,("AddrLen %x\n", AddrLen));
56
57 return AddrLen;
58 }
59
60 UINT TaLengthOfTransportAddressByType(UINT AddressType)
61 {
62 UINT AddrLen = TdiAddressSizeFromType(AddressType);
63
64 if (!AddrLen)
65 return 0;
66
67 AddrLen += sizeof(ULONG) + 2 * sizeof(USHORT);
68
69 AFD_DbgPrint(MID_TRACE,("AddrLen %x\n", AddrLen));
70
71 return AddrLen;
72 }
73
74 VOID TaCopyAddressInPlace( PTA_ADDRESS Target,
75 PTA_ADDRESS Source ) {
76 UINT AddrLen = TaLengthOfAddress( Source );
77 RtlCopyMemory( Target, Source, AddrLen );
78 }
79
80 PTA_ADDRESS TaCopyAddress( PTA_ADDRESS Source ) {
81 UINT AddrLen = TaLengthOfAddress( Source );
82 PVOID Buffer;
83 if (!AddrLen)
84 return NULL;
85
86 Buffer = ExAllocatePool( NonPagedPool, AddrLen );
87
88 if (Buffer)
89 RtlCopyMemory( Buffer, Source, AddrLen );
90
91 return Buffer;
92 }
93
94 VOID TaCopyTransportAddressInPlace( PTRANSPORT_ADDRESS Target,
95 PTRANSPORT_ADDRESS Source ) {
96 UINT AddrLen = TaLengthOfTransportAddress( Source );
97 RtlCopyMemory( Target, Source, AddrLen );
98 }
99
100 PTRANSPORT_ADDRESS TaCopyTransportAddress( PTRANSPORT_ADDRESS OtherAddress ) {
101 UINT AddrLen;
102 PTRANSPORT_ADDRESS A;
103
104 AddrLen = TaLengthOfTransportAddress( OtherAddress );
105 if (!AddrLen)
106 return NULL;
107
108 A = ExAllocatePool( NonPagedPool, AddrLen );
109
110 if( A )
111 TaCopyTransportAddressInPlace( A, OtherAddress );
112
113 return A;
114 }
115
116 NTSTATUS TdiBuildNullTransportAddressInPlace(PTRANSPORT_ADDRESS A, UINT AddressType)
117 {
118 A->TAAddressCount = 1;
119
120 A->Address[0].AddressLength = TdiAddressSizeFromType(AddressType);
121 if (!A->Address[0].AddressLength)
122 return STATUS_INVALID_PARAMETER;
123
124 A->Address[0].AddressType = AddressType;
125
126 RtlZeroMemory(A->Address[0].Address, A->Address[0].AddressLength);
127
128 return STATUS_SUCCESS;
129 }
130
131 PTRANSPORT_ADDRESS TaBuildNullTransportAddress(UINT AddressType)
132 {
133 UINT AddrLen;
134 PTRANSPORT_ADDRESS A;
135
136 AddrLen = TaLengthOfTransportAddressByType(AddressType);
137 if (!AddrLen)
138 return NULL;
139
140 A = ExAllocatePool(NonPagedPool, AddrLen);
141
142 if (A)
143 {
144 if (TdiBuildNullTransportAddressInPlace(A, AddressType) != STATUS_SUCCESS)
145 {
146 ExFreePool(A);
147 return NULL;
148 }
149 }
150
151 return A;
152 }
153
154 NTSTATUS TdiBuildNullConnectionInfoInPlace
155 ( PTDI_CONNECTION_INFORMATION ConnInfo,
156 ULONG Type )
157 /*
158 * FUNCTION: Builds a NULL TDI connection information structure
159 * ARGUMENTS:
160 * ConnectionInfo = Address of buffer to place connection information
161 * Type = TDI style address type (TDI_ADDRESS_TYPE_XXX).
162 * RETURNS:
163 * Status of operation
164 */
165 {
166 ULONG TdiAddressSize;
167 PTRANSPORT_ADDRESS TransportAddress;
168
169 TdiAddressSize = TaLengthOfTransportAddressByType(Type);
170 if (!TdiAddressSize)
171 {
172 AFD_DbgPrint(MIN_TRACE,("Invalid parameter\n"));
173 return STATUS_INVALID_PARAMETER;
174 }
175
176 RtlZeroMemory(ConnInfo,
177 sizeof(TDI_CONNECTION_INFORMATION) +
178 TdiAddressSize);
179
180 ConnInfo->OptionsLength = sizeof(ULONG);
181 ConnInfo->RemoteAddressLength = TdiAddressSize;
182 ConnInfo->RemoteAddress = TransportAddress =
183 (PTRANSPORT_ADDRESS)&ConnInfo[1];
184
185 return TdiBuildNullTransportAddressInPlace(TransportAddress, Type);
186 }
187
188 NTSTATUS TdiBuildNullConnectionInfo
189 ( PTDI_CONNECTION_INFORMATION *ConnectionInfo,
190 ULONG Type )
191 /*
192 * FUNCTION: Builds a NULL TDI connection information structure
193 * ARGUMENTS:
194 * ConnectionInfo = Address of buffer pointer to allocate connection
195 * information in
196 * Type = TDI style address type (TDI_ADDRESS_TYPE_XXX).
197 * RETURNS:
198 * Status of operation
199 */
200 {
201 PTDI_CONNECTION_INFORMATION ConnInfo;
202 ULONG TdiAddressSize;
203 NTSTATUS Status;
204
205 TdiAddressSize = TaLengthOfTransportAddressByType(Type);
206 if (!TdiAddressSize) {
207 AFD_DbgPrint(MIN_TRACE,("Invalid parameter\n"));
208 *ConnectionInfo = NULL;
209 return STATUS_INVALID_PARAMETER;
210 }
211
212 ConnInfo = (PTDI_CONNECTION_INFORMATION)
213 ExAllocatePool(NonPagedPool,
214 sizeof(TDI_CONNECTION_INFORMATION) +
215 TdiAddressSize);
216 if (!ConnInfo) {
217 *ConnectionInfo = NULL;
218 return STATUS_INSUFFICIENT_RESOURCES;
219 }
220
221 Status = TdiBuildNullConnectionInfoInPlace( ConnInfo, Type );
222
223 if (!NT_SUCCESS(Status))
224 {
225 ExFreePool( ConnInfo );
226 ConnInfo = NULL;
227 }
228
229 *ConnectionInfo = ConnInfo;
230
231 return Status;
232 }
233
234
235 NTSTATUS
236 TdiBuildConnectionInfoInPlace
237 ( PTDI_CONNECTION_INFORMATION ConnectionInfo,
238 PTRANSPORT_ADDRESS Address ) {
239 NTSTATUS Status = STATUS_SUCCESS;
240
241 _SEH2_TRY {
242 RtlCopyMemory( ConnectionInfo->RemoteAddress,
243 Address,
244 ConnectionInfo->RemoteAddressLength );
245 } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) {
246 Status = _SEH2_GetExceptionCode();
247 } _SEH2_END;
248
249 return Status;
250 }
251
252
253 NTSTATUS
254 TdiBuildConnectionInfo
255 ( PTDI_CONNECTION_INFORMATION *ConnectionInfo,
256 PTRANSPORT_ADDRESS Address ) {
257 NTSTATUS Status = TdiBuildNullConnectionInfo
258 ( ConnectionInfo, Address->Address[0].AddressType );
259
260 if( NT_SUCCESS(Status) )
261 TdiBuildConnectionInfoInPlace( *ConnectionInfo, Address );
262
263 return Status;
264 }