1b5072f6e435582c7a7a10cf14ccf686b2598213
[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 Status = WarmSocketForBind( FCB );
61 else Status = STATUS_NO_MEMORY;
62
63 if( NT_SUCCESS(Status) )
64 FCB->State = SOCKET_STATE_BOUND;
65 else return UnlockAndMaybeComplete( FCB, Status, Irp, 0 );
66
67 AFD_DbgPrint(MID_TRACE,("FCB->Flags %x\n", FCB->Flags));
68
69 if( FCB->Flags & SGID_CONNECTIONLESS ) {
70 /* This will be the from address for subsequent recvfrom calls */
71 TdiBuildConnectionInfo( &FCB->AddressFrom,
72 FCB->LocalAddress );
73
74 if( !FCB->AddressFrom ) return UnlockAndMaybeComplete( FCB, STATUS_NO_MEMORY, Irp, 0 );
75
76 AFD_DbgPrint(MID_TRACE,("Calling TdiReceiveDatagram\n"));
77
78 Status = TdiReceiveDatagram
79 ( &FCB->ReceiveIrp.InFlightRequest,
80 FCB->AddressFile.Object,
81 0,
82 FCB->Recv.Window,
83 FCB->Recv.Size,
84 FCB->AddressFrom,
85 &FCB->ReceiveIrp.Iosb,
86 PacketSocketRecvComplete,
87 FCB );
88
89 /* We don't want to wait for this read to complete. */
90 if( Status == STATUS_PENDING ) Status = STATUS_SUCCESS;
91 }
92
93 return UnlockAndMaybeComplete( FCB, Status, Irp, 0 );
94 }
95