- Implement IOCTL_AFD_GET_CONNECT_DATA, IOCTL_AFD_SET_CONNECT_DATA, IOCTL_AFD_SET_CO...
[reactos.git] / drivers / network / afd / afd / bind.c
1 /* $Id$
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 #include "tdi_proto.h"
13 #include "tdiconn.h"
14 #include "debug.h"
15
16 NTSTATUS WarmSocketForBind( PAFD_FCB FCB ) {
17 NTSTATUS Status;
18
19 AFD_DbgPrint(MID_TRACE,("Called (AF %d)\n",
20 FCB->LocalAddress->Address[0].AddressType));
21
22 if( !FCB->TdiDeviceName.Length || !FCB->TdiDeviceName.Buffer ) {
23 AFD_DbgPrint(MID_TRACE,("Null Device\n"));
24 return STATUS_NO_SUCH_DEVICE;
25 }
26 if( !FCB->LocalAddress ) {
27 AFD_DbgPrint(MID_TRACE,("No local address\n"));
28 return STATUS_INVALID_PARAMETER;
29 }
30
31 Status = TdiOpenAddressFile(&FCB->TdiDeviceName,
32 FCB->LocalAddress,
33 &FCB->AddressFile.Handle,
34 &FCB->AddressFile.Object );
35
36 AFD_DbgPrint(MID_TRACE,("Returning %x\n", Status));
37
38 return Status;
39 }
40
41 NTSTATUS NTAPI
42 AfdBindSocket(PDEVICE_OBJECT DeviceObject, PIRP Irp,
43 PIO_STACK_LOCATION IrpSp) {
44 NTSTATUS Status = STATUS_SUCCESS;
45 PFILE_OBJECT FileObject = IrpSp->FileObject;
46 PAFD_FCB FCB = FileObject->FsContext;
47 PAFD_BIND_DATA BindReq;
48
49 AFD_DbgPrint(MID_TRACE,("Called\n"));
50
51 if( !SocketAcquireStateLock( FCB ) ) return LostSocket( Irp );
52 if( !(BindReq = LockRequest( Irp, IrpSp )) )
53 return UnlockAndMaybeComplete( FCB, STATUS_NO_MEMORY,
54 Irp, 0 );
55
56 if( FCB->LocalAddress ) ExFreePool( FCB->LocalAddress );
57 FCB->LocalAddress = TaCopyTransportAddress( &BindReq->Address );
58
59 if( FCB->LocalAddress )
60 TdiBuildConnectionInfo( &FCB->AddressFrom,
61 FCB->LocalAddress );
62
63 if( FCB->AddressFrom )
64 Status = WarmSocketForBind( FCB );
65 else return UnlockAndMaybeComplete(FCB, STATUS_NO_MEMORY, Irp, 0);
66
67 AFD_DbgPrint(MID_TRACE,("FCB->Flags %x\n", FCB->Flags));
68
69 if( FCB->Flags & AFD_ENDPOINT_CONNECTIONLESS ) {
70 AFD_DbgPrint(MID_TRACE,("Calling TdiReceiveDatagram\n"));
71
72 FCB->Recv.Window = ExAllocatePool(PagedPool, FCB->Recv.Size);
73 if (!FCB->Recv.Window)
74 return UnlockAndMaybeComplete(FCB, STATUS_NO_MEMORY, Irp, 0);
75
76 Status = TdiReceiveDatagram
77 ( &FCB->ReceiveIrp.InFlightRequest,
78 FCB->AddressFile.Object,
79 0,
80 FCB->Recv.Window,
81 FCB->Recv.Size,
82 FCB->AddressFrom,
83 &FCB->ReceiveIrp.Iosb,
84 PacketSocketRecvComplete,
85 FCB );
86
87 /* We don't want to wait for this read to complete. */
88 if( Status == STATUS_PENDING ) Status = STATUS_SUCCESS;
89 }
90
91 if (NT_SUCCESS(Status))
92 FCB->State = SOCKET_STATE_BOUND;
93
94 return UnlockAndMaybeComplete( FCB, Status, Irp, 0 );
95 }
96