Fixes:
[reactos.git] / reactos / drivers / net / afd / afd / bind.c
1 /* $Id: bind.c,v 1.4 2004/09/05 04:26:29 arty Exp $
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 = STATUS_UNSUCCESSFUL;
18
19 AFD_DbgPrint(MID_TRACE,("Called (AF %d)\n",
20 FCB->LocalAddress->Address[0].AddressType));
21
22 if( FCB->LocalAddress ) {
23 Status = TdiOpenAddressFile
24 ( &FCB->TdiDeviceName,
25 FCB->LocalAddress,
26 &FCB->AddressFile.Handle,
27 &FCB->AddressFile.Object );
28 }
29
30 if( !NT_SUCCESS(Status) ) {
31 TdiCloseDevice( &FCB->AddressFile.Handle,
32 FCB->AddressFile.Object );
33 RtlZeroMemory( &FCB->AddressFile, sizeof( FCB->AddressFile ) );
34 }
35
36 AFD_DbgPrint(MID_TRACE,("Returning %x\n", Status));
37
38 return Status;
39 }
40
41 NTSTATUS STDCALL
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, FALSE );
52 if( !(BindReq = LockRequest( Irp, IrpSp )) )
53 return UnlockAndMaybeComplete( FCB, STATUS_NO_MEMORY,
54 Irp, 0, NULL, FALSE );
55
56 FCB->LocalAddress = TaCopyTransportAddress( &BindReq->Address );
57
58 if( FCB->LocalAddress )
59 Status = WarmSocketForBind( FCB );
60 else Status = STATUS_NO_MEMORY;
61
62 if( NT_SUCCESS(Status) )
63 FCB->State = SOCKET_STATE_BOUND;
64 else return UnlockAndMaybeComplete( FCB, Status, Irp, 0, NULL, FALSE );
65
66 AFD_DbgPrint(MID_TRACE,("FCB->Flags %x\n", FCB->Flags));
67
68 if( FCB->Flags & SGID_CONNECTIONLESS ) {
69 /* This will be the from address for subsequent recvfrom calls */
70 TdiBuildConnectionInfo( &FCB->AddressFrom,
71 &FCB->LocalAddress->Address[0] );
72 /* Allocate our backup buffer */
73 FCB->Recv.Window = ExAllocatePool( NonPagedPool, FCB->Recv.Size );
74 FCB->PollState |= AFD_EVENT_SEND;
75 /* A datagram socket is always sendable */
76
77 AFD_DbgPrint(MID_TRACE,("Calling TdiReceiveDatagram\n"));
78
79 Status = TdiReceiveDatagram
80 ( &FCB->ReceiveIrp.InFlightRequest,
81 FCB->AddressFile.Object,
82 0,
83 FCB->Recv.Window,
84 FCB->Recv.Size,
85 FCB->AddressFrom,
86 &FCB->ReceiveIrp.Iosb,
87 PacketSocketRecvComplete,
88 FCB );
89
90 /* We don't want to wait for this read to complete. */
91 if( Status == STATUS_PENDING ) Status = STATUS_SUCCESS;
92 }
93
94 return UnlockAndMaybeComplete( FCB, Status, Irp, 0, NULL, TRUE );
95 }
96