ReactOS 0.4.15-dev-8241-g63935f8
lantest.cpp
Go to the documentation of this file.
1#include <windows.h>
2#include <net/lan.h>
3#include <iostream>
4#include <string>
5#include <ddk/ntddk.h>
6#include <rosrtl/string.h>
7
8using std::string;
9using std::cin;
10using std::cout;
11using std::cerr;
12using std::endl;
13
14void display_row( char *data, int off, int len ) {
15 int i;
16
17 printf( "%08x:", off );
18 for( i = off; i < len && i < off + 16; i++ ) {
19 printf( " %02x", data[i] & 0xff );
20 }
21
22 for( ; i < off + 16; i++ )
23 printf(" ");
24
25 printf( " -- " );
26
27 for( i = off; i < len && i < off + 16; i++ ) {
28 printf( "%c", (data[i] >= ' ' && data[i] <= '~') ? data[i] : '.' );
29 }
30
31 printf( "\n" );
32}
33
34void display_buffer( char *Packet, int ReadLen ) {
35 UINT PktLen;
36 for( PktLen = 0; PktLen < ReadLen; PktLen += 16 )
37 display_row( Packet, PktLen, ReadLen );
38}
39
40int byte_till_end( char *Packet, int PktLen ) {
41 int byte;
42 std::string word;
43
44 cin >> word;
45 while( word != "end" ) {
46 byte = strtoul( (string("0x") + word).c_str(), 0, 0 );
47 fprintf( stderr, "Byte[%d]: %x\n", PktLen, byte & 0xff );
48 Packet[PktLen++] = byte;
49 cin >> word;
50 }
51
52 return PktLen;
53}
54
55/* Ethernet types. We swap constants so we can compare values at runtime
56 without swapping them there */
57#define ETYPE_IPv4 WH2N(0x0800)
58#define ETYPE_IPv6 WH2N(0x86DD)
59#define ETYPE_ARP WH2N(0x0806)
60
61extern "C"
74
75int main( int argc, char **argv ) {
76 string word;
77 HANDLE LanFile;
79 UNICODE_STRING LanDevice;
84 DWORD On = 1, PktLen;
85 CHAR Packet[1600];
88 USHORT TypesToListen[] = { ETYPE_IPv4, ETYPE_IPv6, ETYPE_ARP };
89 UINT EaLength = LAN_EA_INFO_SIZE(sizeof(TypesToListen)/sizeof(USHORT));
90
93 NULL,
94 0,
95 0 );
96
97 RtlInitUnicodeString( &LanDevice, L"\\Device\\Lan" );
98
100 &LanDevice,
102 NULL,
103 NULL );
104
106 LAN_FILL_EA_INFO(EaBuffer,sizeof(TypesToListen)/sizeof(USHORT),
107 TypesToListen);
108
109 Status = ZwCreateFile( &LanFile,
112 &Attributes,
113 &Iosb,
114 NULL,
119 EaBuffer,
120 EaLength );
121
122 if( !NT_SUCCESS(Status) ) {
123 cerr << "Could not open lan device " << Status << "\n";
124 return 1;
125 }
126
127 Status = DeviceIoControl( LanFile,
129 &On,
130 sizeof(On),
131 0,
132 0,
133 &PktLen,
134 NULL );
135
136 if( !Status ) {
137 cerr << "Could not turn on buffered mode " << Status << "\n";
138 return 1;
139 }
140
141 while( cin >> word ) {
142 if( word == "end" ) {
143 NtClose( LanFile );
144 return 0;
145 } else if( word == "enum" ) {
146 Status = DeviceIoControl( LanFile,
148 NULL,
149 0,
150 Packet,
151 sizeof(Packet),
152 &PktLen,
153 NULL );
154
155 cout << "EnumAdapters: " << Status << "\n";
156 if( Status )
157 display_buffer( Packet, PktLen );
158 } else if( word == "query" ) {
159 cin >> PktLen;
160
161 Status = DeviceIoControl( LanFile,
163 &PktLen,
164 sizeof(PktLen),
165 Packet,
166 sizeof(Packet),
167 &PktLen,
168 NULL );
169
170 cout << "QueryAdapterInfo: " << Status << "\n";
171 if( Status )
172 display_buffer( Packet, PktLen );
173 } else if( word == "send" ) {
174 cin >> Hdr->Fixed.Adapter
175 >> Hdr->Fixed.AddressType
176 >> Hdr->Fixed.AddressLen
177 >> Hdr->Fixed.PacketType;
178 Hdr->Fixed.Mdl = NULL;
179 PktLen = byte_till_end( Packet, Hdr->Address - (PCHAR)Hdr );
180 Status = NtWriteFile( LanFile,
181 NULL,
182 NULL,
183 NULL,
184 &Iosb,
185 Packet,
186 PktLen,
187 NULL,
188 NULL );
189
190 cout << "NtWriteFile: " << Status << "\n";
191 } else if( word == "recv" ) {
192 ULONG ReadLen;
193 Status = NtReadFile( LanFile,
194 Event,
195 NULL,
196 NULL,
197 &Iosb,
198 Packet,
199 sizeof(Packet),
200 NULL,
201 NULL );
202 cout << "NtReadFile: " << Status << "\n";
203 if( Status == STATUS_PENDING ) {
204 LARGE_INTEGER Timeout = { 0 };
206 }
207
208 ReadLen = Iosb.Information;
209
210 if( Status == STATUS_SUCCESS ) {
211 cout << "Read " << ReadLen << " bytes\n";
212 display_buffer( Packet, ReadLen );
213 }
214 }
215 }
216
217 return 0;
218}
static int argc
Definition: ServiceArgs.c:12
UINT32 strtoul(const char *String, char **Terminator, UINT32 Base)
Definition: utclib.c:696
LONG NTSTATUS
Definition: precomp.h:26
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:36
BOOL WINAPI DeviceIoControl(IN HANDLE hDevice, IN DWORD dwIoControlCode, IN LPVOID lpInBuffer OPTIONAL, IN DWORD nInBufferSize OPTIONAL, OUT LPVOID lpOutBuffer OPTIONAL, IN DWORD nOutBufferSize OPTIONAL, OUT LPDWORD lpBytesReturned OPTIONAL, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: deviceio.c:136
#define NULL
Definition: types.h:112
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define GENERIC_READ
Definition: compat.h:135
#define FILE_ATTRIBUTE_NORMAL
Definition: compat.h:137
#define FILE_SHARE_READ
Definition: compat.h:136
#define byte(x, n)
Definition: tomcrypt.h:118
_In_ PIO_STACK_LOCATION _Inout_ PFILE_OBJECT _Inout_ PVCB _Outptr_result_maybenull_ PDCB _In_ PDCB _In_ PDIRENT _In_ ULONG _In_ ULONG _In_ PUNICODE_STRING _In_ PACCESS_MASK _In_ USHORT ShareAccess
Definition: create.c:4147
return Iosb
Definition: create.c:4402
int main()
Definition: test.c:6
IN PVCB IN PDIRENT OUT PULONG EaLength
Definition: fatprocs.h:878
IN PFCB IN PFILE_OBJECT FileObject IN ULONG AllocationSize
Definition: fatprocs.h:322
unsigned long DWORD
Definition: ntddk_ex.h:95
_Must_inspect_result_ _In_opt_ PFLT_INSTANCE _Out_ PHANDLE _In_ ACCESS_MASK _In_ POBJECT_ATTRIBUTES _Out_ PIO_STATUS_BLOCK _In_opt_ PLARGE_INTEGER _In_ ULONG FileAttributes
Definition: fltkernel.h:1236
_Must_inspect_result_ _In_opt_ PFLT_INSTANCE _Out_ PHANDLE FileHandle
Definition: fltkernel.h:1231
#define printf
Definition: freeldr.h:97
struct _FILE_FULL_EA_INFORMATION * PFILE_FULL_EA_INFORMATION
#define FILE_SYNCHRONOUS_IO_NONALERT
Definition: from_kernel.h:31
#define FILE_OPEN_IF
Definition: from_kernel.h:56
Status
Definition: gdiplustypes.h:25
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950
GLenum GLsizei len
Definition: glext.h:6722
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define stderr
Definition: stdio.h:100
_Check_return_opt_ _CRTIMP int __cdecl fprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format,...)
#define OBJ_CASE_INSENSITIVE
Definition: winternl.h:228
#define cout
Definition: iostream.cpp:38
#define cin
Definition: iostream.cpp:37
#define cerr
Definition: iostream.cpp:39
#define EVENT_ALL_ACCESS
Definition: isotest.c:82
const WCHAR * word
Definition: lex.c:36
#define ETYPE_IPv4
Definition: lantest.cpp:57
NTSTATUS NTAPI NtCreateFile(PHANDLE FileHandle, ACCESS_MASK DesiredAccess, POBJECT_ATTRIBUTES ObjectAttributes, PIO_STATUS_BLOCK IoStatusBlock, PLARGE_INTEGER AllocationSize, ULONG FileAttributes, ULONG ShareAccess, ULONG CreateDisposition, ULONG CreateOptions, PVOID EaBuffer, ULONG EaLength)
Definition: file.c:3758
void display_buffer(char *Packet, int ReadLen)
Definition: lantest.cpp:34
int byte_till_end(char *Packet, int PktLen)
Definition: lantest.cpp:40
#define ETYPE_ARP
Definition: lantest.cpp:59
#define ETYPE_IPv6
Definition: lantest.cpp:58
void display_row(char *data, int off, int len)
Definition: lantest.cpp:14
static OUT PIO_STATUS_BLOCK IoStatusBlock
Definition: pipe.c:75
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
#define argv
Definition: mplay32.c:18
_In_ NDIS_HANDLE _In_ PNDIS_PACKET Packet
Definition: ndis.h:1549
unsigned int UINT
Definition: ndis.h:50
#define IOCTL_IF_ADAPTER_INFO
Definition: net_lan.h:17
#define IOCTL_IF_ENUM_ADAPTERS
Definition: net_lan.h:13
struct _LAN_ADDRESS * PLAN_ADDRESS
#define LAN_FILL_EA_INFO(Ea, NumTypes, Types)
Definition: net_lan.h:69
#define IOCTL_IF_BUFFERED_MODE
Definition: net_lan.h:15
struct _LAN_PACKET_HEADER * PLAN_PACKET_HEADER
#define LAN_EA_INFO_SIZE(NumTypes)
Definition: net_lan.h:67
#define FILE_SHARE_WRITE
Definition: nt_native.h:681
#define SYNCHRONIZE
Definition: nt_native.h:61
ULONG ACCESS_MASK
Definition: nt_native.h:40
NTSYSAPI NTSTATUS NTAPI NtWriteFile(IN HANDLE hFile, IN HANDLE hEvent OPTIONAL, IN PIO_APC_ROUTINE IoApcRoutine OPTIONAL, IN PVOID IoApcContext OPTIONAL, OUT PIO_STATUS_BLOCK pIoStatusBlock, IN PVOID WriteBuffer, IN ULONG WriteBufferLength, IN PLARGE_INTEGER FileOffset OPTIONAL, IN PULONG LockOperationKey OPTIONAL)
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
NTSTATUS NTAPI NtClose(IN HANDLE Handle)
Definition: obhandle.c:3402
NTSYSAPI NTSTATUS NTAPI NtWaitForSingleObject(IN HANDLE hObject, IN BOOLEAN bAlertable, IN PLARGE_INTEGER Timeout)
#define GENERIC_WRITE
Definition: nt_native.h:90
#define GENERIC_EXECUTE
Definition: nt_native.h:91
NTSTATUS NTAPI NtCreateEvent(OUT PHANDLE EventHandle, IN ACCESS_MASK DesiredAccess, IN POBJECT_ATTRIBUTES ObjectAttributes OPTIONAL, IN EVENT_TYPE EventType, IN BOOLEAN InitialState)
Definition: event.c:96
PVOID *typedef PHANDLE
Definition: ntsecpkg.h:455
#define STATUS_PENDING
Definition: ntstatus.h:82
#define L(x)
Definition: ntvdm.h:50
unsigned short USHORT
Definition: pedump.c:61
static ULONG Timeout
Definition: ping.c:61
#define calloc
Definition: rosglue.h:14
#define STATUS_SUCCESS
Definition: shellext.h:65
NTSTATUS NTAPI NtReadFile(HANDLE FileHandle, HANDLE Event, PIO_APC_ROUTINE ApcRoutine, PVOID ApcContext, PIO_STATUS_BLOCK IoStatusBlock, PVOID Buffer, ULONG Length, PLARGE_INTEGER ByteOffset, PULONG Key)
CHAR Address[1]
Definition: net_lan.h:30
LAN_PACKET_HEADER_T Fixed
Definition: net_lan.h:29
#define NTAPI
Definition: typedefs.h:36
uint32_t ULONG
Definition: typedefs.h:59
char * PCHAR
Definition: typedefs.h:51
_Must_inspect_result_ _In_ WDFDMAENABLER _In_ _In_opt_ PWDF_OBJECT_ATTRIBUTES Attributes
_Must_inspect_result_ _In_ WDFDEVICE _In_ ULONG _In_ ACCESS_MASK DesiredAccess
Definition: wdfdevice.h:2658
_Must_inspect_result_ _In_opt_ WDFKEY _In_ PCUNICODE_STRING _In_ ACCESS_MASK _In_ ULONG _Out_opt_ PULONG CreateDisposition
Definition: wdfregistry.h:120
_Must_inspect_result_ _In_opt_ WDFKEY _In_ PCUNICODE_STRING _In_ ACCESS_MASK _In_ ULONG CreateOptions
Definition: wdfregistry.h:118
_In_ ACCESS_MASK _In_ POBJECT_ATTRIBUTES _Out_ PIO_STATUS_BLOCK _In_opt_ PLARGE_INTEGER _In_ ULONG _In_ ULONG _In_ ULONG _In_ ULONG _In_opt_ PVOID EaBuffer
Definition: iofuncs.h:845
char CHAR
Definition: xmlstorage.h:175