Page 1 of 1

help convert perl script to purebasic [solved]

Posted: Fri Nov 04, 2011 9:48 am
by szerda
I have a perl script, which read info from our NAS server, i try convert it to purebasic but not work, please help if somebody can.
Thank you.

Code: Select all

#!/usr/bin/perl -w

use Socket;

my $data='';

# NAS UDP port number
my $nas_port = 22081;

# NAS IP address/hostname
my $nas_host = '192.168.1.1';

my $nas_paddr = sockaddr_in($nas_port, inet_aton($nas_host));

# craft request packet
my $msg = chr(0x00).chr(0x00).chr(0x05).chr(0xd3).chr(0x00).chr(0x00). 
       chr(0x00).chr(0x01).chr(0x00).chr(0x00).chr(0x00).chr(0x00). 
       chr(0x80).chr(0xc9).chr(0x6c).chr(0x05).chr(0xff).chr(0xff). 
       chr(0xff).chr(0xff).chr(0x00).chr(0x00).chr(0x00).chr(0x1c). 
       chr(0x00).chr(0x00).chr(0x00).chr(0x00);

socket(SOCKET, PF_INET, SOCK_DGRAM, getprotobyname('udp'))   || die "socket: $!";

defined(send(SOCKET, $msg, 0, $nas_paddr))    || die "send $nas_host: $!";

$nas_paddr = recv(SOCKET, $data, 1458, 0)        || die "recv: $!";
print substr($data, 28)."\n";

exit 0;

Re: help convert perl script to purebasic

Posted: Fri Nov 04, 2011 12:22 pm
by szerda
here is my code that i try, but it is not work, no receive data and i don't known how can convert msg variable (Protocol request, must be 28 bytes long )

Code: Select all

;Protocol request, this must be 28 bytes long
msg.s="000007050000000100000000A4A1EFB8FFFFFFFF0000001C00000000"
len=Len(msg)
*memory=AllocateMemory(len/2)
For k=0 To (len/2)-1
  PokeB(*memory+k,Val("$"+PeekS(@msg+(k*2),2)))
Next

*Buffer = AllocateMemory(1000)

If InitNetwork()

  ConnectionID = OpenNetworkConnection("192.168.1.1", 22081, #PB_Network_UDP)
  
  If ConnectionID

    Result = SendNetworkData(ConnectionID,@memory,len/2)

    Result = ReceiveNetworkData(ConnectionID, *Buffer, 1000)
    
    MessageRequester("Info", "String: "+PeekS(*Buffer), 0)
    
  EndIf
  
  CloseNetworkConnection(ConnectionID)
  
EndIf

Re: help convert perl script to purebasic

Posted: Fri Nov 04, 2011 2:23 pm
by infratec
Hi,

maybe this gives better results

Code: Select all

#nas_port = 22081
#nas_host = "192.168.1.1"


If InitNetwork()
 
  *Buffer = AllocateMemory(1500)
  If *Buffer
    ConnectionID = OpenNetworkConnection(#nas_host, #nas_port, #PB_Network_UDP)
    If ConnectionID
      SendNetworkData(ConnectionID, ?MSG, 28) 
     
      Result = 0
      Starttime = ElapsedMilliseconds()
      While ElapsedMilliseconds() - Starttime < 3000
        If NetworkClientEvent(ConnectionID) = #PB_NetworkEvent_Data
          Result = ReceiveNetworkData(ConnectionID, *Buffer, MemorySize(*Buffer))
          Break
        EndIf
      Wend
      If Result > 28
        MessageRequester("Info", "String: "+ PeekS(*Buffer + 28))
      EndIf
      CloseNetworkConnection(ConnectionID)
    EndIf
    FreeMemory(*Buffer)
  EndIf
EndIf

End 0

DataSection
  MSG:
  Data.a $00, $00, $05, $d3, $00, $00
  Data.a $00, $01, $00, $00, $00, $00
  Data.a $80, $c9, $6c, $05, $ff, $ff
  Data.a $ff, $ff, $00 ,$00, $00, $1c
  Data.a $00, $00, $00, $00
EndDataSection
Not tested :wink:

Not fully true: Sending works, checked with wireshark.

Bernd

Re: help convert perl script to purebasic

Posted: Mon Nov 07, 2011 7:19 am
by szerda
Thank you your help infratec
i think it is work because the receive result size 442, so there are some data in *Buffer, but PeekS read nothing.
Why not, or how can i reed *Buffer data?

Re: help convert perl script to purebasic

Posted: Mon Nov 07, 2011 8:03 am
by infratec
Hi szerda,

you can replace

Code: Select all

MessageRequester("Info", "String: "+ PeekS(*Buffer, 28))
with

Code: Select all

For i = 0 To 27
  Byte.a = PeekA(*Buffer + i)
  Debug "Byte " + Str(i + 1) + " = " + Hex(Byte) + " -> " + Chr(Byte)
Next i
Than you can see definately the first 28 received bytes.

Maybe the 'string' contains a $00 at the beginning.
Than Peeks() stops, because the string is finished.

Bernd

Re: help convert perl script to purebasic

Posted: Mon Nov 07, 2011 11:12 am
by szerda
Thank you infratec again

this code working, *Buffer need shift 28 and i can read received string.

Code: Select all

MessageRequester("Info", PeekS(*Buffer+28))
Thanx
Szerda

Re: help convert perl script to purebasic [solved]

Posted: Mon Nov 07, 2011 1:09 pm
by infratec
Sorry,
my fault.

I missinterpreted the substr() command.

Bernd

P.S.: I changed my code above.

Re: help convert perl script to purebasic [solved]

Posted: Mon Nov 07, 2011 10:51 pm
by infratec
Hi,

I found an optimization:
It was not neccessary to copy the MSG Data to the *Buffer.
We can directly send the MSG.

I modified my listing above.

Bernd

Re: help convert perl script to purebasic [solved]

Posted: Tue Nov 08, 2011 11:23 am
by szerda
Nice thank you