ReactOS  0.4.15-dev-1197-g8081ba9
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 
8 using std::string;
9 using std::cin;
10 using std::cout;
11 using std::cerr;
12 using std::endl;
13 
14 void 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 
34 void 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 
40 int byte_till_end( char *Packet, int PktLen ) {
41  int byte;
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 
61 extern "C"
73  ULONG EaLength);
74 
75 int main( int argc, char **argv ) {
76  string word;
77  HANDLE LanFile;
79  UNICODE_STRING LanDevice;
81  HANDLE Event;
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,
117  FILE_OPEN_IF,
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 }
signed char * PCHAR
Definition: retypes.h:7
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:35
static int argc
Definition: ServiceArgs.c:12
#define ETYPE_IPv6
Definition: lantest.cpp:58
UINT32 strtoul(const char *String, char **Terminator, UINT32 Base)
Definition: utclib.c:696
#define FILE_OPEN_IF
Definition: from_kernel.h:56
_In_ NDIS_HANDLE _In_ PNDIS_PACKET Packet
Definition: ndis.h:1548
#define ETYPE_ARP
Definition: lantest.cpp:59
#define OBJ_CASE_INSENSITIVE
Definition: winternl.h:228
#define IOCTL_IF_ADAPTER_INFO
Definition: net_lan.h:17
struct _FILE_FULL_EA_INFORMATION * PFILE_FULL_EA_INFORMATION
LAN_PACKET_HEADER_T Fixed
Definition: net_lan.h:29
char CHAR
Definition: xmlstorage.h:175
_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:835
LONG NTSTATUS
Definition: precomp.h:26
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:100
#define FILE_SHARE_WRITE
Definition: nt_native.h:681
#define argv
Definition: mplay32.c:18
_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:4137
int byte_till_end(char *Packet, int PktLen)
Definition: lantest.cpp:40
#define FILE_SHARE_READ
Definition: compat.h:136
#define LAN_EA_INFO_SIZE(NumTypes)
Definition: net_lan.h:67
struct _LAN_ADDRESS * PLAN_ADDRESS
HANDLE FileHandle
Definition: stats.c:38
#define cout
Definition: iostream.cpp:38
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
NTSTATUS(* NTAPI)(IN PFILE_FULL_EA_INFORMATION EaBuffer, IN ULONG EaLength, OUT PULONG ErrorOffset)
Definition: IoEaTest.cpp:117
basic_ostream< _CharT, _Traits > &_STLP_CALL endl(basic_ostream< _CharT, _Traits > &__os)
Definition: _ostream.h:357
_Check_return_opt_ _CRTIMP int __cdecl fprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format,...)
#define EVENT_ALL_ACCESS
Definition: isotest.c:82
#define GENERIC_WRITE
Definition: nt_native.h:90
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:3757
#define cin
Definition: iostream.cpp:37
_In_ PVOID _In_ ULONG Event
Definition: iotypes.h:450
#define IOCTL_IF_ENUM_ADAPTERS
Definition: net_lan.h:13
smooth NULL
Definition: ftsmooth.c:416
#define IOCTL_IF_BUFFERED_MODE
Definition: net_lan.h:15
_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:1230
IN PVCB IN PDIRENT OUT PULONG EaLength
Definition: fatprocs.h:875
return Iosb
Definition: create.c:4402
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define STATUS_PENDING
Definition: ntstatus.h:82
_STLP_DECLSPEC _Stl_aligned_buffer< ostream > cerr
Definition: iostream.cpp:102
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 _In_ ULONG _In_ ULONG _In_ ULONG CreateOptions
Definition: fltkernel.h:1230
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950
NTSTATUS NTAPI NtClose(IN HANDLE Handle)
Definition: obhandle.c:3399
int main(int argc, char **argv)
Definition: lantest.cpp:75
#define FILE_ATTRIBUTE_NORMAL
Definition: compat.h:137
Status
Definition: gdiplustypes.h:24
static const WCHAR L[]
Definition: oid.c:1250
PVOID *typedef PHANDLE
Definition: ntsecpkg.h:454
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)
GLenum GLsizei len
Definition: glext.h:6722
#define GENERIC_READ
Definition: compat.h:135
#define SYNCHRONIZE
Definition: nt_native.h:61
char string[160]
Definition: util.h:11
IN PFCB IN PFILE_OBJECT FileObject IN ULONG AllocationSize
Definition: fatprocs.h:319
struct _LAN_PACKET_HEADER * PLAN_PACKET_HEADER
_Must_inspect_result_ _In_ USHORT _In_ PHIDP_PREPARSED_DATA _Out_writes_to_ LengthAttributes PHIDP_EXTENDED_ATTRIBUTES Attributes
Definition: hidpi.h:348
#define ETYPE_IPv4
Definition: lantest.cpp:57
static ULONG Timeout
Definition: ping.c:61
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
unsigned short USHORT
Definition: pedump.c:61
#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 DesiredAccess
Definition: create.c:4137
static OUT PIO_STATUS_BLOCK IoStatusBlock
Definition: pipe.c:75
unsigned int UINT
Definition: ndis.h:50
const WCHAR * word
Definition: lex.c:36
#define FILE_SYNCHRONOUS_IO_NONALERT
Definition: from_kernel.h:31
#define calloc
Definition: rosglue.h:14
unsigned int ULONG
Definition: retypes.h:1
FILE * stderr
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
void display_buffer(char *Packet, int ReadLen)
Definition: lantest.cpp:34
#define GENERIC_EXECUTE
Definition: nt_native.h:91
return STATUS_SUCCESS
Definition: btrfs.c:3014
CHAR Address[1]
Definition: net_lan.h:30
_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 _In_ ULONG CreateDisposition
Definition: create.c:4137
ULONG ACCESS_MASK
Definition: nt_native.h:40
#define LAN_FILL_EA_INFO(Ea, NumTypes, Types)
Definition: net_lan.h:69
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)
#define printf
Definition: config.h:203
off
Definition: i386-dis.c:3909
void display_row(char *data, int off, int len)
Definition: lantest.cpp:14
NTSYSAPI NTSTATUS NTAPI NtWaitForSingleObject(IN HANDLE hObject, IN BOOLEAN bAlertable, IN PLARGE_INTEGER Timeout)