ReactOS  0.4.14-dev-77-gd9e7c48
loopback.c
Go to the documentation of this file.
1 /*
2  * COPYRIGHT: See COPYING in the top level directory
3  * PROJECT: ReactOS TCP/IP protocol driver
4  * FILE: datalink/loopback.c
5  * PURPOSE: Loopback adapter
6  * PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net)
7  * REVISIONS:
8  * CSH 01/08-2000 Created
9  */
10 
11 #include "precomp.h"
12 
14 
16  PVOID Context)
17 {
18  PIP_PACKET IPPacket = Context;
19 
20  /* IPReceive() takes care of the NDIS packet */
21  IPReceive(Loopback, IPPacket);
22 
23  ExFreePool(IPPacket);
24 }
25 
27  PVOID Context,
28  PNDIS_PACKET NdisPacket,
29  UINT Offset,
30  PVOID LinkAddress,
31  USHORT Type)
32 /*
33  * FUNCTION: Transmits a packet
34  * ARGUMENTS:
35  * Context = Pointer to context information (NULL)
36  * NdisPacket = Pointer to NDIS packet to send
37  * Offset = Offset in packet where packet data starts
38  * LinkAddress = Pointer to link address
39  * Type = LAN protocol type (unused)
40  */
41 {
42  PCHAR PacketBuffer;
43  UINT PacketLength;
44  PNDIS_PACKET XmitPacket;
45  NDIS_STATUS NdisStatus;
46  PIP_PACKET IPPacket;
47 
48  ASSERT_KM_POINTER(NdisPacket);
49  ASSERT_KM_POINTER(PC(NdisPacket));
50  ASSERT_KM_POINTER(PC(NdisPacket)->DLComplete);
51 
52  TI_DbgPrint(MAX_TRACE, ("Called (NdisPacket = %x)\n", NdisPacket));
53 
54  GetDataPtr( NdisPacket, 0, &PacketBuffer, &PacketLength );
55 
56  NdisStatus = AllocatePacketWithBuffer
57  ( &XmitPacket, PacketBuffer, PacketLength );
58 
59  if( NT_SUCCESS(NdisStatus) ) {
60  IPPacket = ExAllocatePool(NonPagedPool, sizeof(IP_PACKET));
61  if (IPPacket)
62  {
63  IPInitializePacket(IPPacket, 0);
64 
65  IPPacket->NdisPacket = XmitPacket;
66 
67  GetDataPtr(IPPacket->NdisPacket,
68  0,
69  (PCHAR*)&IPPacket->Header,
70  &IPPacket->TotalSize);
71 
72  IPPacket->MappedHeader = TRUE;
73 
74  if (!ChewCreate(LoopPassiveWorker, IPPacket))
75  {
76  IPPacket->Free(IPPacket);
77  ExFreePool(IPPacket);
78  NdisStatus = NDIS_STATUS_RESOURCES;
79  }
80  }
81  else
82  NdisStatus = NDIS_STATUS_RESOURCES;
83  }
84 
85  (PC(NdisPacket)->DLComplete)
86  ( PC(NdisPacket)->Context, NdisPacket, NdisStatus );
87 }
88 
91  PLAN_ADAPTER *Adapter)
92 /*
93  * FUNCTION: Registers loopback adapter with the network layer
94  * ARGUMENTS:
95  * AdapterName = Unused
96  * Adapter = Unused
97  * RETURNS:
98  * Status of operation
99  */
100 {
101  LLIP_BIND_INFO BindInfo;
102 
103  TI_DbgPrint(MID_TRACE, ("Called.\n"));
104 
105  /* Bind the adapter to network (IP) layer */
106  BindInfo.Context = NULL;
107  BindInfo.HeaderSize = 0;
108  BindInfo.MinFrameSize = 0;
109  BindInfo.Address = NULL;
110  BindInfo.AddressLength = 0;
111  BindInfo.Transmit = LoopTransmit;
112 
113  Loopback = IPCreateInterface(&BindInfo);
114  if (!Loopback) return NDIS_STATUS_RESOURCES;
115 
116  Loopback->MTU = 16384;
117 
118  Loopback->Name.Buffer = L"Loopback";
120  wcslen(Loopback->Name.Buffer) * sizeof(WCHAR);
121 
125 
127 
129 
130  TI_DbgPrint(MAX_TRACE, ("Leaving.\n"));
131 
132  return NDIS_STATUS_SUCCESS;
133 }
134 
135 
137  PLAN_ADAPTER Adapter)
138 /*
139  * FUNCTION: Unregisters loopback adapter with the network layer
140  * ARGUMENTS:
141  * Adapter = Unused
142  * RETURNS:
143  * Status of operation
144  * NOTES:
145  * Does not care wether we have registered loopback adapter
146  */
147 {
148  TI_DbgPrint(MID_TRACE, ("Called.\n"));
149 
150  if (Loopback != NULL)
151  {
154  Loopback = NULL;
155  }
156 
157  TI_DbgPrint(MAX_TRACE, ("Leaving.\n"));
158 
159  return NDIS_STATUS_SUCCESS;
160 }
signed char * PCHAR
Definition: retypes.h:7
#define TI_DbgPrint(_t_, _x_)
Definition: debug.h:45
IP_ADDRESS Broadcast
Definition: ip.h:162
UINT AddressLength
Definition: ip.h:131
VOID IPDestroyInterface(PIP_INTERFACE IF)
Definition: ip.c:246
#define TRUE
Definition: types.h:120
#define MID_TRACE
Definition: debug.h:15
#define LOOPBACK_BCASTADDR_IPv4
Definition: ip.h:187
Type
Definition: Type.h:6
USHORT MaximumLength
Definition: env_spec_w32.h:370
VOID IPUnregisterInterface(PIP_INTERFACE IF)
Definition: ip.c:353
UINT MinFrameSize
Definition: ip.h:129
void GetDataPtr(PNDIS_PACKET Packet, UINT Offset, PCHAR *DataOut, PUINT Size)
Definition: routines.c:65
PVOID Context
Definition: ip.h:127
PIP_INTERFACE IPCreateInterface(PLLIP_BIND_INFO BindInfo)
Definition: ip.c:182
UNICODE_STRING Name
Definition: ip.h:163
OBJECT_FREE_ROUTINE Free
Definition: ip.h:78
PUCHAR Address
Definition: ip.h:130
LL_TRANSMIT_ROUTINE Transmit
Definition: ip.h:132
int NDIS_STATUS
Definition: ntddndis.h:471
_In_ ULONG _In_ ULONG Offset
Definition: ntddpcm.h:101
PIP_PACKET IPInitializePacket(PIP_PACKET IPPacket, ULONG Type)
Definition: ip.c:92
#define AllocatePacketWithBuffer(x, y, z)
Definition: memtrack.h:7
PVOID Header
Definition: ip.h:83
smooth NULL
Definition: ftsmooth.c:416
IP_ADDRESS Unicast
Definition: ip.h:159
BOOLEAN IPRegisterInterface(PIP_INTERFACE IF)
Definition: ip.c:293
#define PC(Packet)
Definition: ip.h:106
UINT MTU
Definition: ip.h:157
BOOLEAN ChewCreate(VOID(*Worker)(PVOID), PVOID WorkerContext)
Definition: workqueue.c:65
VOID LoopPassiveWorker(PVOID Context)
Definition: loopback.c:15
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
UINT HeaderSize
Definition: ip.h:128
#define AddrInitIPv4(IPAddress, RawAddress)
Definition: address.h:16
UINT TotalSize
Definition: ip.h:86
#define ASSERT_KM_POINTER(_x)
Definition: debug.h:74
VOID IPAddInterfaceRoute(PIP_INTERFACE IF)
Definition: ip.c:266
#define NDIS_STATUS_SUCCESS
Definition: ndis.h:346
static const WCHAR L[]
Definition: oid.c:1250
#define ExAllocatePool(type, size)
Definition: fbtusb.h:44
IP_ADDRESS Netmask
Definition: ip.h:161
#define LOOPBACK_ADDRMASK_IPv4
Definition: ip.h:188
#define MAX_TRACE
Definition: debug.h:16
unsigned short USHORT
Definition: pedump.c:61
VOID IPReceive(PIP_INTERFACE IF, PIP_PACKET IPPacket)
Definition: receive.c:638
unsigned int UINT
Definition: ndis.h:50
PNDIS_PACKET NdisPacket
Definition: ip.h:88
#define LOOPBACK_ADDRESS_IPv4
Definition: ip.h:186
BOOLEAN MappedHeader
Definition: ip.h:81
struct tagContext Context
Definition: acpixf.h:1024
#define NDIS_STATUS_RESOURCES
Definition: ndis.h:466
PIP_INTERFACE Loopback
Definition: loopback.c:13
Definition: ip.h:77
VOID LoopTransmit(PVOID Context, PNDIS_PACKET NdisPacket, UINT Offset, PVOID LinkAddress, USHORT Type)
Definition: loopback.c:26
NDIS_STATUS LoopRegisterAdapter(PNDIS_STRING AdapterName, PLAN_ADAPTER *Adapter)
Definition: loopback.c:89
_Must_inspect_result_ _Out_ PNDIS_STATUS _Out_ PNDIS_STATUS _Out_ PNDIS_HANDLE _Out_ PUINT _In_ UINT _In_ NDIS_HANDLE _In_ NDIS_HANDLE _In_ PNDIS_STRING AdapterName
Definition: ndis.h:6013
NDIS_STATUS LoopUnregisterAdapter(PLAN_ADAPTER Adapter)
Definition: loopback.c:136
size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
#define ExFreePool(addr)
Definition: env_spec_w32.h:352