Ok, here is the source i have been working on. Found out that I didn't have to use raw sockets anyway, but now I know how to do it.

The program sends a UDP packet and tries to wake it up. The documentation for these magic packets can be found on AMD page.

Sorry that I didn't have time to comment it all for you, but if you have any questions, please send them to tfs@inbound.dk.

.386
.model flat,stdcall
option casemap:none

PrintError PROTO :DWORD

include \masm32\include\windows.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc
include \masm32\include\ws2_32.inc
includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\ws2_32.lib

.data?
_hInstance dd ?
_WSAData WSADATA <?>
_target sockaddr_in <?>
_SocketHandler dd ?

.data
ProgramName db "WOL (c) 2001 by Torben Flindt Sillesen",0
ErrStartup db "WinSock not installed or version mismatch",0
ErrSocket db "Can't create UDP Socket",0
ErrConnect db "Can't connect to target",0
ErrSend db "Can't send data.",0
ErrOn db "Error in IP_HDRINCL",0
source db "127.0.0.1",0
target db "127.0.0.1",0
port dd 32767
on dd 1
buffer ip_hdr <0>
udp udp_hdr <0>
realbuffer db 0FFh,0FFh,0FFh,0FFh,0FFh,0FFh ;INIT SEQ
db 16 DUP(000h, 050h, 0BAh, 0D5h, 0F5h, 011h) ;The MAC-addr

.code
start: invoke GetModuleHandle, 0
mov _hInstance, eax

;STARTUP WSA
invoke WSAStartup, 201h, addr _WSAData
cmp eax,0
je ok2
invoke PrintError, addr ErrStartup
jmp exit

ok2: ;CREATE SOCKET
invoke WSASocket, AF_INET, SOCK_RAW, IPPROTO_UDP, 0, 0, 0
cmp eax,INVALID_SOCKET
jne ok3
invoke PrintError, addr ErrSocket
jmp cleanup

ok3: ;FILL IN HEADERS FOR IP AND UDP
mov _SocketHandler,eax

mov _target.sin_family, AF_INET
invoke htons, port
mov _target.sin_port, ax
invoke inet_addr, addr target
mov _target.sin_addr, eax

;IP HEADER
mov buffer.ip_hlv, 45h
mov buffer.ip_tos, 0
invoke htons, 130
mov buffer.ip_len, ax
invoke htons, 1234h
mov buffer.ip_id, ax
mov buffer.ip_off, 40h
mov buffer.ip_ttl, 48
mov buffer.ip_p, IPPROTO_UDP
mov buffer.ip_cksum, 0
invoke inet_addr, addr source
mov buffer.ip_src, eax
mov eax, _target.sin_addr
mov buffer.ip_dest, eax

;UDP HEADER
mov ax, _target.sin_port
mov udp.udp_srcport, ax
mov udp.udp_dstport, ax
invoke htons, 110
mov udp.udp_len, ax
mov udp.udp_cksum, 0

;SET IP_HDRINCL (IP-HEADER INCLUDED)
invoke setsockopt, _SocketHandler, IPPROTO_IP, 2h, addr on, sizeof on
cmp eax, SOCKET_ERROR
jne ok4
invoke PrintError, addr ErrOn
jmp close

ok4: ;SEND DATA
invoke sendto, _SocketHandler, addr buffer, sizeof buffer+sizeof udp+102, 0, addr _target, sizeof _target
cmp eax,SOCKET_ERROR
jne close
invoke PrintError, addr ErrSend

close: invoke closesocket, _SocketHandler
cleanup:invoke WSACleanup
exit: invoke ExitProcess, 0

PrintError proc mes:DWORD
invoke MessageBox, NULL, mes, addr ProgramName, MB_OK
ret
PrintError endp
end start
Posted on 2001-07-27 05:02:22 by Unregistered
I almost forgot. The udp_hdr structure was not defined in my windows.inc, so I had to define i myself.

Also the IP stack does not fill in the checksum for the UDP protocol when using raw sockets. It doesn't matter in this program, since the NIC doesn't check it, but if you need to implement it, a routine can be found at the WinSock FAQ.

udp_hdr STRUCT
udp_srcport WORD ?
udp_dstport WORD ?
udp_len WORD ?
udp_cksum WORD ?
udp_hdr ENDS

Clerihew
Posted on 2001-07-27 05:17:31 by clerihew
Thanks :alright:
Posted on 2001-07-27 05:18:27 by eko
Packet sniffer I tested it on had it show up as an icmp packet heh :) but yah thanks for the code, is much appreciated.
Posted on 2005-01-20 21:18:58 by Webring