An OCaml library for constructing Wake-on-LAN magic packets
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

parameritise

+45 -18
+1 -1
bin/dune
··· 2 2 (name owol) 3 3 (public_name owol) 4 4 (modules owol) 5 - (libraries wol)) 5 + (libraries wol cmdliner))
+25 -3
bin/owol.ml
··· 1 - let () = 1 + 2 + let send_wol mac port broadcast = 2 3 Eio_posix.run @@ fun env -> 3 - let mac = "AA:BB:CC:DD:EE:FF" in 4 - Wol.send ~net:env#net mac 4 + Wol.send ~net:env#net ~port ~broadcast mac 5 + 6 + let () = 7 + let open Cmdliner in 8 + let mac_address = 9 + let doc = "The MAC address to send the magic packet to." in 10 + Arg.(required & pos 0 (some string) None & info [] ~docv:"MAC_ADDRESS" ~doc) 11 + in 12 + let port = 13 + let doc = "Port to send the packet too (default 9)." in 14 + Arg.(value & opt int 9 & info ["p"; "port"] ~docv:"PORT" ~doc) 15 + in 16 + let broadcast = 17 + let doc = "Address to send the packet too (default broadcast to 255.255.255.255)." in 18 + Arg.(value & opt string "255.255.255.255" & info ["a"; "address"] ~docv:"ADDRESS" ~doc) 19 + in 20 + let cmd = 21 + let term = Term.(const send_wol $ mac_address $ port $ broadcast) in 22 + let doc = "Send a Wake-on-LAN magic packet with a specified MAC address." in 23 + let info = Cmd.info "wol" ~doc in 24 + Cmd.v info term 25 + in 26 + exit (Cmd.eval cmd)
+19 -14
lib/wol.ml
··· 3 3 List.map (fun x -> int_of_string ("0x" ^ x)) (Str.split (Str.regexp ":") mac_str) 4 4 |> Array.of_list 5 5 6 - let construct_magic_packet mac = 7 - let buf = Cstruct.create 102 in 8 - for i = 0 to 5 do 9 - Cstruct.set_uint8 buf i 0xFF 10 - done; 11 - for i = 0 to 15 do 12 - for j = 0 to 5 do 13 - Cstruct.set_uint8 buf (6 + i * 6 + j) mac.(j) 14 - done; 15 - done; 6 + let mac_to_bytes mac = 7 + String.split_on_char ':' mac 8 + |> List.map (fun x -> int_of_string ("0x" ^ x)) 9 + 10 + let repeat_list n list = 11 + List.init n (fun _ -> list) 12 + |> List.concat 13 + 14 + let construct_magic_packet mac_str = 15 + let prefix = [0xFF; 0xFF; 0xFF; 0xFF; 0xFF; 0xFF] in 16 + let mac_bytes = mac_to_bytes mac_str in 17 + let repeated_mac = repeat_list 16 mac_bytes in 18 + let packet_data = prefix @ repeated_mac in 19 + let buf = Cstruct.create (List.length packet_data) in 20 + List.iteri (fun i byte -> Cstruct.set_uint8 buf i byte) packet_data; 16 21 buf 17 22 18 - let send ~net mac_str = 23 + let send ~net ?(port=9) ?(broadcast="255.255.255.255") mac_str = 19 24 Eio.Switch.run @@ fun sw -> 20 - let addr = Ipaddr.V4.of_string_exn "255.255.255.255" |> Ipaddr.V4.to_octets |> Eio.Net.Ipaddr.of_raw in 25 + let addr = Ipaddr.V4.of_string_exn broadcast |> Ipaddr.V4.to_octets |> Eio.Net.Ipaddr.of_raw in 21 26 let sock = 22 27 let proto = 23 28 Eio.Net.Ipaddr.fold ··· 27 32 in 28 33 Eio.Net.datagram_socket ~sw net proto 29 34 in 30 - let packet = construct_magic_packet (mac_of_string mac_str) in 31 - Eio.Net.send sock ~dst:(`Udp (addr, 9)) [ packet ] 35 + let packet = construct_magic_packet mac_str in 36 + Eio.Net.send sock ~dst:(`Udp (addr, port)) [ packet ]