e16ab26874c91512fe9b4231b013c40cdafb3f9e
[reactos.git] / reactos / drivers / network / afd / afd / bind.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: drivers/net/afd/afd/bind.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 NTSTATUS WarmSocketForBind( PAFD_FCB FCB, ULONG ShareType ) {
14 NTSTATUS Status;
15
16 AFD_DbgPrint(MID_TRACE,("Called (AF %u)\n",
17 FCB->LocalAddress->Address[0].AddressType));
18
19 if( !FCB->TdiDeviceName.Length || !FCB->TdiDeviceName.Buffer ) {
20 AFD_DbgPrint(MIN_TRACE,("Null Device\n"));
21 return STATUS_NO_SUCH_DEVICE;
22 }
23 if( !FCB->LocalAddress ) {
24 AFD_DbgPrint(MIN_TRACE,("No local address\n"));
25 return STATUS_INVALID_PARAMETER;
26 }
27
28 Status = TdiOpenAddressFile(&FCB->TdiDeviceName,
29 FCB->LocalAddress,
30 ShareType,
31 &FCB->AddressFile.Handle,
32 &FCB->AddressFile.Object );
33 if (!NT_SUCCESS(Status))
34 return Status;
35
36 if (FCB->Flags & AFD_ENDPOINT_CONNECTIONLESS)
37 {
38 if (!FCB->Recv.Size)
39 {
40 Status = TdiQueryMaxDatagramLength(FCB->AddressFile.Object,
41 &FCB->Recv.Size);
42 }
43
44 if (NT_SUCCESS(Status) && !FCB->Recv.Window)
45 {
46 FCB->Recv.Window = ExAllocatePool(PagedPool, FCB->Recv.Size);
47 if (!FCB->Recv.Window)
48 Status = STATUS_NO_MEMORY;
49 }
50
51 if (NT_SUCCESS(Status))
52 {
53 Status = TdiReceiveDatagram(&FCB->ReceiveIrp.InFlightRequest,
54 FCB->AddressFile.Object,
55 0,
56 FCB->Recv.Window,
57 FCB->Recv.Size,
58 FCB->AddressFrom,
59 &FCB->ReceiveIrp.Iosb,
60 PacketSocketRecvComplete,
61 FCB);
62
63 /* We don't want to wait for this read to complete. */
64 if( Status == STATUS_PENDING ) Status = STATUS_SUCCESS;
65 }
66 }
67
68 AFD_DbgPrint(MID_TRACE,("Returning %x\n", Status));
69
70 return Status;
71 }
72
73 NTSTATUS NTAPI
74 AfdBindSocket(PDEVICE_OBJECT DeviceObject, PIRP Irp,
75 PIO_STACK_LOCATION IrpSp) {
76 NTSTATUS Status = STATUS_SUCCESS;
77 PFILE_OBJECT FileObject = IrpSp->FileObject;
78 PAFD_FCB FCB = FileObject->FsContext;
79 PAFD_BIND_DATA BindReq;
80
81 UNREFERENCED_PARAMETER(DeviceObject);
82
83 AFD_DbgPrint(MID_TRACE,("Called\n"));
84
85 if( !SocketAcquireStateLock( FCB ) ) return LostSocket( Irp );
86 if( !(BindReq = LockRequest( Irp, IrpSp, FALSE, NULL )) )
87 return UnlockAndMaybeComplete( FCB, STATUS_NO_MEMORY,
88 Irp, 0 );
89
90 if( FCB->LocalAddress ) ExFreePool( FCB->LocalAddress );
91 FCB->LocalAddress = TaCopyTransportAddress( &BindReq->Address );
92
93 if( FCB->LocalAddress )
94 Status = TdiBuildConnectionInfo( &FCB->AddressFrom,
95 FCB->LocalAddress );
96
97 if( NT_SUCCESS(Status) )
98 Status = WarmSocketForBind( FCB, BindReq->ShareType );
99 AFD_DbgPrint(MID_TRACE,("FCB->Flags %x\n", FCB->Flags));
100
101 if (NT_SUCCESS(Status))
102 FCB->State = SOCKET_STATE_BOUND;
103
104 /* MSAFD relies on us returning the address file handle in the IOSB */
105 return UnlockAndMaybeComplete( FCB, Status, Irp,
106 (ULONG_PTR)FCB->AddressFile.Handle );
107 }