12454b9e94dc445c7dc6c4a982e2dcfbe89a6da0
[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 TaCopyTransportAddressInPlace( PTRANSPORT_ADDRESS Target,
75 PTRANSPORT_ADDRESS Source ) {
76 UINT AddrLen = TaLengthOfTransportAddress( Source );
77 RtlCopyMemory( Target, Source, AddrLen );
78 }
79
80 PTRANSPORT_ADDRESS TaCopyTransportAddress( PTRANSPORT_ADDRESS OtherAddress ) {
81 UINT AddrLen;
82 PTRANSPORT_ADDRESS A;
83
84 AddrLen = TaLengthOfTransportAddress( OtherAddress );
85 if (!AddrLen)
86 return NULL;
87
88 A = ExAllocatePool( NonPagedPool, AddrLen );
89
90 if( A )
91 TaCopyTransportAddressInPlace( A, OtherAddress );
92
93 return A;
94 }
95
96 NTSTATUS TdiBuildNullTransportAddressInPlace(PTRANSPORT_ADDRESS A, UINT AddressType)
97 {
98 A->TAAddressCount = 1;
99
100 A->Address[0].AddressLength = TdiAddressSizeFromType(AddressType);
101 if (!A->Address[0].AddressLength)
102 return STATUS_INVALID_PARAMETER;
103
104 A->Address[0].AddressType = AddressType;
105
106 RtlZeroMemory(A->Address[0].Address, A->Address[0].AddressLength);
107
108 return STATUS_SUCCESS;
109 }
110
111 PTRANSPORT_ADDRESS TaBuildNullTransportAddress(UINT AddressType)
112 {
113 UINT AddrLen;
114 PTRANSPORT_ADDRESS A;
115
116 AddrLen = TaLengthOfTransportAddressByType(AddressType);
117 if (!AddrLen)
118 return NULL;
119
120 A = ExAllocatePool(NonPagedPool, AddrLen);
121
122 if (A)
123 {
124 if (TdiBuildNullTransportAddressInPlace(A, AddressType) != STATUS_SUCCESS)
125 {
126 ExFreePool(A);
127 return NULL;
128 }
129 }
130
131 return A;
132 }
133
134 NTSTATUS TdiBuildNullConnectionInfoInPlace
135 ( PTDI_CONNECTION_INFORMATION ConnInfo,
136 ULONG Type )
137 /*
138 * FUNCTION: Builds a NULL TDI connection information structure
139 * ARGUMENTS:
140 * ConnectionInfo = Address of buffer to place connection information
141 * Type = TDI style address type (TDI_ADDRESS_TYPE_XXX).
142 * RETURNS:
143 * Status of operation
144 */
145 {
146 ULONG TdiAddressSize;
147 PTRANSPORT_ADDRESS TransportAddress;
148
149 TdiAddressSize = TaLengthOfTransportAddressByType(Type);
150 if (!TdiAddressSize)
151 {
152 AFD_DbgPrint(MIN_TRACE,("Invalid parameter\n"));
153 return STATUS_INVALID_PARAMETER;
154 }
155
156 RtlZeroMemory(ConnInfo,
157 sizeof(TDI_CONNECTION_INFORMATION) +
158 TdiAddressSize);
159
160 ConnInfo->OptionsLength = sizeof(ULONG);
161 ConnInfo->RemoteAddressLength = TdiAddressSize;
162 ConnInfo->RemoteAddress = TransportAddress =
163 (PTRANSPORT_ADDRESS)&ConnInfo[1];
164
165 return TdiBuildNullTransportAddressInPlace(TransportAddress, Type);
166 }
167
168 NTSTATUS TdiBuildNullConnectionInfo
169 ( PTDI_CONNECTION_INFORMATION *ConnectionInfo,
170 ULONG Type )
171 /*
172 * FUNCTION: Builds a NULL TDI connection information structure
173 * ARGUMENTS:
174 * ConnectionInfo = Address of buffer pointer to allocate connection
175 * information in
176 * Type = TDI style address type (TDI_ADDRESS_TYPE_XXX).
177 * RETURNS:
178 * Status of operation
179 */
180 {
181 PTDI_CONNECTION_INFORMATION ConnInfo;
182 ULONG TdiAddressSize;
183 NTSTATUS Status;
184
185 TdiAddressSize = TaLengthOfTransportAddressByType(Type);
186 if (!TdiAddressSize) {
187 AFD_DbgPrint(MIN_TRACE,("Invalid parameter\n"));
188 *ConnectionInfo = NULL;
189 return STATUS_INVALID_PARAMETER;
190 }
191
192 ConnInfo = (PTDI_CONNECTION_INFORMATION)
193 ExAllocatePool(NonPagedPool,
194 sizeof(TDI_CONNECTION_INFORMATION) +
195 TdiAddressSize);
196 if (!ConnInfo) {
197 *ConnectionInfo = NULL;
198 return STATUS_INSUFFICIENT_RESOURCES;
199 }
200
201 Status = TdiBuildNullConnectionInfoInPlace( ConnInfo, Type );
202
203 if (!NT_SUCCESS(Status))
204 {
205 ExFreePool( ConnInfo );
206 ConnInfo = NULL;
207 }
208
209 *ConnectionInfo = ConnInfo;
210
211 return Status;
212 }
213
214
215 NTSTATUS
216 TdiBuildConnectionInfoInPlace
217 ( PTDI_CONNECTION_INFORMATION ConnectionInfo,
218 PTRANSPORT_ADDRESS Address ) {
219 NTSTATUS Status = STATUS_SUCCESS;
220
221 _SEH2_TRY {
222 RtlCopyMemory( ConnectionInfo->RemoteAddress,
223 Address,
224 ConnectionInfo->RemoteAddressLength );
225 } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) {
226 Status = _SEH2_GetExceptionCode();
227 } _SEH2_END;
228
229 return Status;
230 }
231
232
233 NTSTATUS
234 TdiBuildConnectionInfo
235 ( PTDI_CONNECTION_INFORMATION *ConnectionInfo,
236 PTRANSPORT_ADDRESS Address ) {
237 NTSTATUS Status = TdiBuildNullConnectionInfo
238 ( ConnectionInfo, Address->Address[0].AddressType );
239
240 if( NT_SUCCESS(Status) )
241 TdiBuildConnectionInfoInPlace( *ConnectionInfo, Address );
242
243 return Status;
244 }