Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2010-05

TAP-Win32

10 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Jorge Aldo G. de F. Junior

Anyone knows how to use it from pascal ?

I am even willing to pay for a component that simplifies TAP-win32 use.

Jorge Aldo G. de F. Junior
to Marc Weustink

TAP-Win32 is the win32 equivalent of linux/unix tun tap devices.

i need to:
- discover installed taps
- set device options (Dont need ip and etc, just the basic ioctls)
- read and write to device (after set up it becomes just a simple file)

i tried this :

Uses Windows, Classes;
Const
	TAP_Device = '\\\\.\\Global\\{44F7688F-77FA-43DC-8D8F-9CBA23E01BB0}.tap';
Var
	Handle : Integer;
	Buffer : Pointer;
	Stream : THandleStream;
	Count  : Integer;
Begin
	Handle := CreateFile(PChar(TAP_Device), GENERIC_READ Or
GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM Or
FILE_FLAG_OVERLAPPED, 0);
	WriteLn(Handle);
	Stream := THandleStream.Create(Handle);
	Buffer := GetMem(2048);
	Repeat
		Count := Stream.Read(Buffer^, 2048);
		If Count > 0 Then
			WriteLn('Got ', Count, ' bytes !');
		Stream.Write(Buffer^, Count);
	Until False;
	Stream.Free;
	FreeMem(Buffer, 2048);
	CloseHandle(Handle);
End.

(translated from a C# example)

but the handle returned is always -1

(The guid is correct as returned by openvpn)

2010/5/13 Marc Weustink :

Show quoted text 14 lines
Felipe Monteiro de Carvalho wrote: > > What does TAP stand for? see http://en.wikipedia.org/wiki/TUN/TAP for some pointers. Marc _______________________________________________ fpc-pascal maillist  -   http://lists.freepascal.org/mailman/listinfo/fpc-pascal
José Mejuto
to Jorge Aldo G. de F. Junior

Hello FPC-Pascal,

Thursday, May 13, 2010, 10:42:37 PM, you wrote:

JAGdFJ>         Handle := CreateFile(PChar(TAP_Device), GENERIC_READ Or
JAGdFJ> GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM Or
JAGdFJ> FILE_FLAG_OVERLAPPED, 0);

Try using:

Handle:=CreateFileA(PChar(TAP_Device), GENERIC_READ or GENERIC_WRITE,
                     0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);

Overlapped attribute is an error for sure as non overlapped structure is being passed. In fact I think that no FILE_xxxx is > (translated from a C# example)

Maybe the example is wrong ;)

Best regards, José
Jorge Aldo G. de F. Junior
to José Mejuto

handle still returns -1

2010/5/13 José Mejuto :

Show quoted text 28 lines
Hello FPC-Pascal, Thursday, May 13, 2010, 10:42:37 PM, you wrote: JAGdFJ>         Handle := CreateFile(PChar(TAP_Device), GENERIC_READ Or JAGdFJ> GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM Or JAGdFJ> FILE_FLAG_OVERLAPPED, 0); Try using: Handle:=CreateFileA(PChar(TAP_Device), GENERIC_READ or GENERIC_WRITE,                     0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); Overlapped attribute is an error for sure as non overlapped structure is being passed. In fact I think that no FILE_xxxx is needed at all. JAGdFJ> (translated from a C# example) Maybe the example is wrong ;) -- Best regards,  José _______________________________________________ fpc-pascal maillist  -   http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Jorge Aldo G. de F. Junior
to Jorge Aldo G. de F. Junior

This is the C# example i found :

http://www.varsanofiev.com/inside/TunTest.cs

2010/5/13 Jorge Aldo G. de F. Junior :

Show quoted text 32 lines
handle still returns -1 2010/5/13 José Mejuto : > Hello FPC-Pascal, > > Thursday, May 13, 2010, 10:42:37 PM, you wrote: > > JAGdFJ>         Handle := CreateFile(PChar(TAP_Device), GENERIC_READ Or > JAGdFJ> GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM Or > JAGdFJ> FILE_FLAG_OVERLAPPED, 0); > > Try using: > > Handle:=CreateFileA(PChar(TAP_Device), GENERIC_READ or GENERIC_WRITE, >                     0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); > > Overlapped attribute is an error for sure as non overlapped structure > is being passed. In fact I think that no FILE_xxxx is needed at all. > > JAGdFJ> (translated from a C# example) > > Maybe the example is wrong ;) > > -- > Best regards, >  José > > _______________________________________________ > fpc-pascal maillist  -   > http://lists.freepascal.org/mailman/listinfo/fpc-pascal >
Jorge Aldo G. de F. Junior
to José Mejuto

Uses
Windows,
Classes,

	BlckSock;
Const
	TAP_Device = '\\.\Global\{44F7688F-77FA-43DC-8D8F-9CBA23E01BB0}.tap'#00;
	TAP_Buffer = 8192;
Var
	Handle : Integer;
	Buffer : Pointer;
	Stream : THandleStream;
	Count  : Integer;
Begin
	Handle := CreateFileA(PChar(TAP_Device), GENERIC_READ or
GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM, 0);
	Stream := THandleStream.Create(Handle);
	Buffer := GetMem(TAP_Buffer);
	Repeat
		Count := Stream.Read(Buffer^, TAP_Buffer);
		If Count > 0 Then
			Stream.Write(Buffer^, Count);
	Until False;
	Stream.Free;
	FreeMem(Buffer, TAP_Buffer);
	CloseHandle(Handle);
End.

=========

i am able to receive packets setting up a tap device with a fixed ip and doing ping to the gateway address...

now i need to decipher the ioctl calls...

2010/5/13 José Mejuto :

Show quoted text 25 lines
Hello FPC-Pascal, Friday, May 14, 2010, 12:14:31 AM, you wrote: JAGdFJ> This is the C# example i found : JAGdFJ> http://www.varsanofiev.com/inside/TunTest.cs Also this line is wrong IMHO: TAP_Device = '\\\\.\\Global\\{44F7688F-77FA-43DC-8D8F-9CBA23E01BB0}.tap'; it should be: TAP_Device = '\\.\Global\{44F7688F-77FA-43DC-8D8F-9CBA23E01BB0}.tap'; Pascal does not need the slash escape sequence. -- Best regards,  José _______________________________________________ fpc-pascal maillist  -   http://lists.freepascal.org/mailman/listinfo/fpc-pascal
José Mejuto
to Jorge Aldo G. de F. Junior

Hello FPC-Pascal,

Friday, May 14, 2010, 1:34:08 PM, you wrote:

JAGdFJ> i am able to receive packets setting up a tap device with a fixed ip
JAGdFJ> and doing ping to the gateway address...
JAGdFJ> now i need to decipher the ioctl calls...

Sorry, I can not help more, I had never used TAP and even I do not know what it is (except a brief definition in wikipedia).

Best regards, José