Reactos
1/*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: drivers/net/tdihelpers/tdiconn.c
5 * PURPOSE: Ancillary functions driver
6 * PROGRAMMER: Art Yerkes (ayerkes@speakeasy.net)
7 * UPDATE HISTORY:
8 * 20040708 Created
9 * 20251021 Moved to tdihelpers since it is used by 2 drivers (AFD, NETIO) now
10 */
11
12#include <afd.h>
13
14#ifdef UNIMPLEMENTED
15#undef UNIMPLEMENTED
16#endif
17
18/* If you want to see the DPRINT() output in your debugger,
19 * remove the following line (or comment it out) */
20#define NDEBUG
21#include <reactos/debug.h>
22
23UINT TdiAddressSizeFromType( UINT AddressType ) {
24 switch( AddressType ) {
25 case TDI_ADDRESS_TYPE_IP:
26 return TDI_ADDRESS_LENGTH_IP;
27 case TDI_ADDRESS_TYPE_APPLETALK:
28 return TDI_ADDRESS_LENGTH_APPLETALK;
29 case TDI_ADDRESS_TYPE_NETBIOS:
30 return TDI_ADDRESS_LENGTH_NETBIOS;
31 /* case TDI_ADDRESS_TYPE_NS: */
32 case TDI_ADDRESS_TYPE_IPX:
33 return TDI_ADDRESS_LENGTH_IPX;
34 case TDI_ADDRESS_TYPE_VNS:
35 return TDI_ADDRESS_LENGTH_VNS;
36 default:
37 DbgPrint("TdiAddressSizeFromType - invalid type: %x\n", AddressType);
38 return 0;
39 }
40}
41
42UINT TaLengthOfAddress( PTA_ADDRESS Addr )
43{
44 UINT AddrLen = Addr->AddressLength;
45
46 if (!AddrLen)
47 return 0;
48
49 AddrLen += 2 * sizeof( USHORT );
50
51 DPRINT("AddrLen %x\n", AddrLen);
52
53 return AddrLen;
54}
55
56UINT TaLengthOfTransportAddress( PTRANSPORT_ADDRESS Addr )
57{
58 UINT AddrLen = TaLengthOfAddress(&Addr->Address[0]);
59
60 if (!AddrLen)
61 return 0;
62
63 AddrLen += sizeof(ULONG);
64
65 DPRINT("AddrLen %x\n", AddrLen);
66
67 return AddrLen;
68}
69
70UINT TaLengthOfTransportAddressByType(UINT AddressType)
71{
72 UINT AddrLen = TdiAddressSizeFromType(AddressType);
73
74 if (!AddrLen)
75 return 0;
76
77 AddrLen += sizeof(ULONG) + 2 * sizeof(USHORT);
78
79 DPRINT("AddrLen %x\n", AddrLen);
80
81 return AddrLen;
82}
83
84VOID TaCopyTransportAddressInPlace( PTRANSPORT_ADDRESS Target,
85 PTRANSPORT_ADDRESS Source ) {
86 UINT AddrLen = TaLengthOfTransportAddress( Source );
87 RtlCopyMemory( Target, Source, AddrLen );
88}
89
90PTRANSPORT_ADDRESS TaCopyTransportAddress( PTRANSPORT_ADDRESS OtherAddress ) {
91 UINT AddrLen;
92 PTRANSPORT_ADDRESS A;
93
94 AddrLen = TaLengthOfTransportAddress( OtherAddress );
95 if (!AddrLen)
96 return NULL;
97
98 A = ExAllocatePoolWithTag(NonPagedPool,
99 AddrLen,
100 TAG_AFD_TRANSPORT_ADDRESS);
101
102 if( A )
103 TaCopyTransportAddressInPlace( A, OtherAddress );
104
105 return A;
106}
107
108NTSTATUS TdiBuildNullTransportAddressInPlace(PTRANSPORT_ADDRESS A, UINT AddressType)
109{
110 A->TAAddressCount = 1;
111
112 A->Address[0].AddressLength = TdiAddressSizeFromType(AddressType);
113 if (!A->Address[0].AddressLength)
114 return STATUS_INVALID_PARAMETER;
115
116 A->Address[0].AddressType = AddressType;
117
118 RtlZeroMemory(A->Address[0].Address, A->Address[0].AddressLength);
119
120 return STATUS_SUCCESS;
121}
122
123PTRANSPORT_ADDRESS TaBuildNullTransportAddress(UINT AddressType)
124{
125 UINT AddrLen;
126 PTRANSPORT_ADDRESS A;
127
128 AddrLen = TaLengthOfTransportAddressByType(AddressType);
129 if (!AddrLen)
130 return NULL;
131
132 A = ExAllocatePoolWithTag(NonPagedPool, AddrLen, TAG_AFD_TRANSPORT_ADDRESS);
133
134 if (A)
135 {
136 if (TdiBuildNullTransportAddressInPlace(A, AddressType) != STATUS_SUCCESS)
137 {
138 ExFreePoolWithTag(A, TAG_AFD_TRANSPORT_ADDRESS);
139 return NULL;
140 }
141 }
142
143 return A;
144}
145
146NTSTATUS TdiBuildNullConnectionInfoInPlace
147( PTDI_CONNECTION_INFORMATION ConnInfo,
148 ULONG Type )
149/*!
150 * @brief Builds a NULL TDI connection information structure
151 *
152 * @param ConnectionInfo = Address of buffer to place connection information
153 * @param Type = TDI style address type (TDI_ADDRESS_TYPE_XXX).
154 *
155 * @return Status of operation
156 */
157{
158 ULONG TdiAddressSize;
159 PTRANSPORT_ADDRESS TransportAddress;
160
161 TdiAddressSize = TaLengthOfTransportAddressByType(Type);
162 if (!TdiAddressSize)
163 {
164 DPRINT("Invalid parameter\n");
165 return STATUS_INVALID_PARAMETER;
166 }
167
168 RtlZeroMemory(ConnInfo,
169 sizeof(TDI_CONNECTION_INFORMATION) +
170 TdiAddressSize);
171
172 ConnInfo->OptionsLength = sizeof(ULONG);
173 ConnInfo->RemoteAddressLength = TdiAddressSize;
174 ConnInfo->RemoteAddress = TransportAddress =
175 (PTRANSPORT_ADDRESS)&ConnInfo[1];
176
177 return TdiBuildNullTransportAddressInPlace(TransportAddress, Type);
178}
179
180NTSTATUS TdiBuildNullConnectionInfo
181( PTDI_CONNECTION_INFORMATION *ConnectionInfo,
182 ULONG Type )
183/*!
184 * @brief Builds a NULL TDI connection information structure
185 *
186 * @param ConnectionInfo = Address of buffer pointer to allocate connection
187 * information in
188 * @param Type = TDI style address type (TDI_ADDRESS_TYPE_XXX).
189 *
190 * @return Status of operation
191 */
192{
193 PTDI_CONNECTION_INFORMATION ConnInfo;
194 ULONG TdiAddressSize;
195 NTSTATUS Status;
196
197 TdiAddressSize = TaLengthOfTransportAddressByType(Type);
198 if (!TdiAddressSize) {
199 DPRINT("Invalid parameter\n");
200 *ConnectionInfo = NULL;
201 return STATUS_INVALID_PARAMETER;
202 }
203
204 ConnInfo = (PTDI_CONNECTION_INFORMATION)
205 ExAllocatePoolWithTag(NonPagedPool,
206 sizeof(TDI_CONNECTION_INFORMATION) + TdiAddressSize,
207 TAG_AFD_TDI_CONNECTION_INFORMATION);
208 if (!ConnInfo) {
209 *ConnectionInfo = NULL;
210 return STATUS_INSUFFICIENT_RESOURCES;
211 }
212
213 Status = TdiBuildNullConnectionInfoInPlace( ConnInfo, Type );
214
215 if (!NT_SUCCESS(Status))
216 {
217 ExFreePoolWithTag(ConnInfo, TAG_AFD_TDI_CONNECTION_INFORMATION);
218 ConnInfo = NULL;
219 }
220
221 *ConnectionInfo = ConnInfo;
222
223 return Status;
224}
225
226
227NTSTATUS
228TdiBuildConnectionInfoInPlace
229( PTDI_CONNECTION_INFORMATION ConnectionInfo,
230 PTRANSPORT_ADDRESS Address ) {
231 NTSTATUS Status = STATUS_SUCCESS;
232
233 _SEH2_TRY {
234 RtlCopyMemory( ConnectionInfo->RemoteAddress,
235 Address,
236 ConnectionInfo->RemoteAddressLength );
237 } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) {
238 Status = _SEH2_GetExceptionCode();
239 } _SEH2_END;
240
241 return Status;
242}
243
244
245NTSTATUS
246TdiBuildConnectionInfo
247( PTDI_CONNECTION_INFORMATION *ConnectionInfo,
248 PTRANSPORT_ADDRESS Address ) {
249 NTSTATUS Status = TdiBuildNullConnectionInfo
250 ( ConnectionInfo, Address->Address[0].AddressType );
251
252 if( NT_SUCCESS(Status) )
253 TdiBuildConnectionInfoInPlace( *ConnectionInfo, Address );
254
255 return Status;
256}