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