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