Page 1 of 1

Problems with Wake on LAN

Posted: Tue Feb 01, 2005 8:27 am
by Vater
Hello NG,

i try to write a programm that can send a wake on lan magic packet to a network broadcast adress. I have a working VB6 Project that sends the same data in the magic Packet that my Purebasic sample does, but it won't work. For UDP broadcasting I use the WinsockFunctions.pb found in the Codearchiv from purearea.net. I am really desperated and need some help ... :cry: I put the source for download here

http://www.visualsoft-net.de/iframes/do ... Packet.zip

It would be great if someone finds the time to have a look on it.
Thanks in advance for your help

Stefan

Posted: Tue Feb 01, 2005 5:01 pm
by ABBKlaus
Hi,

i posted this already in this forum :
ps : you need the xlib http://purebasic.myforums.net/viewtopic ... highlight= Autor=blueznl

Code: Select all

IncludeFile "WinSocket_lib.pb"

  x_init()                                                ; some general stuff
  ws_init()                                               ; load dll and start winsock
  ;

Structure mac
  mac1.b[6]                                               ; MAC Adress is 6 Bytes long
EndStructure

Structure magic
  magic1.b[102]                                           ; Magic Packet 6x$FF 16xMAC = 102 Bytes
EndStructure

; Magig Packet Example :
;
; FFFFFFFFFFFF000C6EB8CEEB000C6EB8CEEB000C6EB8CEEB000C6EB8CEEB000C6EB8CEEB
; 000C6EB8CEEB000C6EB8CEEB000C6EB8CEEB000C6EB8CEEB000C6EB8CEEB000C6EB8CEEB
; 000C6EB8CEEB000C6EB8CEEB000C6EB8CEEB000C6EB8CEEB000C6EB8CEEB

Procedure.s storemac(macadress$,*mac_adress.mac)
  pointer1.b=0
  pointer2.b=0
  value.b=0
  tmac.l=0
  wert.l=0
  a$=""
  For i=1 To Len(macadress$)
    a$=Mid(macadress$,i,1)
    value=0
    If a$>="0" And a$=<"9"
      wert=Asc(a$)-48
      value=1
    EndIf
    If a$>="a" And a$=<"f"
      wert=Asc(a$)-87
      value=1
    EndIf
    If a$>="A" And a$=<"F"
      wert=Asc(a$)-55
      value=1
    EndIf
    If value = 1
      If pointer1=0
        tmac=tmac+wert*16
      Else
        tmac=tmac+wert
      EndIf
      pointer1+1
      If pointer1>1
        *mac_adress\mac1[pointer2]=tmac
        pointer1=0
        pointer2+1
        tmac=0
      EndIf
    EndIf
  Next
EndProcedure

macadress$="00:0C:6E:B8:CE:EB"
DefType.mac mac_adress
StoreMac(macadress$,@mac_adress)

For j=0 To 16
  For i=0 To 5
    If j=0
      magic_packet.magic\magic1[i+j*6] = 255
    Else
      magic_packet.magic\magic1[i+j*6] = mac_adress.mac\mac1[i]
    EndIf
  Next 
Next

client_ip.s = "255.255.255.255" ; Broadcast IP
client_bip.l = inet_addr_(client_ip)
client_port.l = 60000 ; Port (change if you want)
client_bport.l = htons_(client_port) ; convert to long
client_socket.l = Socket_(#AF_INET, #SOCK_DGRAM, #IPPROTO_UDP) ; create the socket
socket_keepalive.b = 1
client_socket1.l = Setsockopt_(client_socket, #SOL_SOCKET, #SO_BROADCAST, @socket_keepalive, 1) 
client_sockaddr.ws_sockaddr                   
client_sockaddr\ws_in_family = #AF_INET
client_sockaddr\ws_in_port = client_bport
client_sockaddr\ws_in_addr = client_bip
client_sockaddr_l.l = 16
retval = Bind_(client_socket,@client_sockaddr,16)
server_mode.l = 0
retval = ioctlsocket_(client_socket,#FIONBIO,@server_mode)
send.l = sendto_(client_socket, @magic_packet,SizeOf(magic_packet), 0,@client_sockaddr,16 )
error.l = WSAGetLastError_()
closesocket_(client_socket)

; ExecutableFormat=Windows
; EOF

Wake on Lan

Posted: Wed Feb 02, 2005 8:10 am
by Vater
Hello ABBKlaus,

I already found your code and tried it ... unfortunately it is not working for me. :cry:

Stefan

Posted: Wed Feb 02, 2005 7:23 pm
by ABBKlaus
i found a bug in your code :

Code: Select all

Structure magicPacket
  magicPacketData.b[102] ; should be byte not long
EndStructure
maybe this helps :?:

Posted: Thu Feb 03, 2005 3:07 pm
by ABBKlaus
Modified your code a bit :idea:
Now it works 8)

Code: Select all

IncludeFile "WinsockFunctions.pb"

Declare SendMagicPacket(strSubnet.s, strMAC.s)
Declare MACToByteArray(strMAC.s)
Declare.l hex2dec(h$)
Declare Open_frm_Main()

Structure magicPacket
  magicPacketData.b[102]
EndStructure

myMagic.magicPacket

Dim MACBytes.l(5)


;- ProgrammEntrypoint
Open_frm_Main()


;-Wake on LAN
Procedure SendMagicPacket(strSubnet.s, strMAC.s)
  
  Protected counter.l  
  Protected index.b
  
  OnErrorGoto(?errHandler)
  
  ;Convert the MAC Adress to Byte Array
  MACToByteArray(strMAC)
  
  For counter = 0 To 5
    myMagic.magicPacket\magicPacketData[counter] = 255
  Next
  
  For index = 0 To 15
    For counter = 0 To 5
      myMagic.magicPacket\magicPacketData[index*6+counter+6] = MACBytes(counter)
      Debug "MAC : "+Str(myMagic.magicPacket\magicPacketData[index*6+counter+6])
    Next
  Next
  
  For idx = 0 To 101
    ;Debug "myMAC : "+Str(idx)+"="+Str(myMagic.magicPacket\magicPacketData[idx])
    ;Debug myMagic.magicPacket\magicPacketData[idx]
  Next
  
  wsVer=InitWinsock()
  If wsVer=0
     MessageRequester("Error","Problem initializing Winsock",0):End
    Else
     Debug "Winsock "+Str(wsVer & 255)+"."+Str((wsVer/256) & 255)+" started"
  EndIf

  udpidx=InitUDP(7)
  If udpidx>0
    Debug "UDP bind on port 7 successful"
  Else
    Debug "Unable to bind port 7, address in use?"
    ProcedureReturn
  EndIf
  
  Debug "Sizeof="+Str(SizeOf(myMagic))
  
  ; Inserted code --------------------------------------------------------
  #SOL_SOCKET = $FFFF
  #SO_KEEPALIVE = $8            ; send keepalive messages
  socket_keepalive.b = 1
  client_socket1.l = setsockopt_(sockets(udpidx)\sck, #SOL_SOCKET, #SO_BROADCAST, @socket_keepalive, 1) 
  ;-----------------------------------------------------------------------
  
  SendUDP(Sockets(udpidx)\sck,@myMagic,SizeOf(myMagic),"255.255.255.255",7)
  Debug udpidx
  Debug Sockets(udpidx)\sck
  MessageRequester("Wake", "Wake on Lan Sent")
  terminatewinsock()
  
  ProcedureReturn
  
errHandler:
  
EndProcedure

Procedure MACToByteArray(strMAC.s)
  
  Protected counter.l
  Protected value.s
  Protected lngBinValue.l
  
  For counter = 0 To 5
    MACBytes(counter) = 0
  Next
  
  
  For counter = 1 To 11 Step 2
    value = Mid(strMAC, counter, 2)
    lngBinValue = hex2dec(value)
    idx=(counter - 1) / 2
    Debug "MACtoByteArray : "+Str(idx)+"="+Str(lngBinValue)
    MACBytes(idx) = lngBinValue
  Next
  
EndProcedure

Procedure.l hex2dec(h$)
  h$=UCase(h$)
  For r=1 To Len(h$)
    d<<4 : a$=Mid(h$,r,1)
    If Asc(a$)>60
      d+Asc(a$)-55
    Else
      d+Asc(a$)-48
    EndIf
  Next
  ProcedureReturn d
EndProcedure


;- Window Constants
;
Enumeration
  #frm_Main
EndEnumeration

;- Gadget Constants
;
Enumeration
  #txt_TargetSubnet
  #cmd_Send
  #lbl_TargetSubnet
  #lbl_TargetMAC
  #txt_TargetMac
EndEnumeration


Procedure Open_frm_Main()
  If OpenWindow(#frm_Main, 245, 73, 296, 110,  #PB_Window_SystemMenu | #PB_Window_TitleBar | #PB_Window_ScreenCentered , "Wake on Lan")
    If CreateGadgetList(WindowID())
      TextGadget(#lbl_TargetSubnet, 7, 10, 83, 20, "Broadcast")
      TextGadget(#lbl_TargetMAC, 7, 40, 83, 20, "Target-MAC") 
      StringGadget(#txt_TargetSubnet, 100, 10, 190, 20, "")
      StringGadget(#txt_TargetMac, 100, 40, 190, 20, "") 
      ButtonGadget(#cmd_Send, 190, 70, 100, 30, "Send") 
    EndIf
  EndIf
EndProcedure

;- Main Loop

lngQuit.l
lngWindowMessage.l

SetGadgetText(#txt_TargetSubnet, "255.255.255.255")
; Rolf 000B6A969431
; Klaus 000C6EB8CEEB
SetGadgetText(#txt_TargetMac, "000C6EB8CEEB")

Repeat
  
  lngWindowMessage = WaitWindowEvent()
  
  If lngWindowMessage = #PB_Event_CloseWindow
    lngQuit = 1
  EndIf
  
  If lngWindowMessage = #PB_Event_Gadget
    If EventGadgetID() = #cmd_Send
      strTargetSubnet$ = GetGadgetText(#txt_TargetSubnet)
      strTargetMAC$ = GetGadgetText(#txt_TargetMac)
      If Len(strTargetSubnet$) = 0 Or Len(strTargetMAC$) = 0
        MessageRequester("Error", "Incomplete Entries")
      Else
        SendMagicPacket(strTargetSubnet$, strTargetMAC$)
      EndIf 
    EndIf
  EndIf 
  
Until lngQuit = 1