Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dhcp option parsing fixes #48

Merged
merged 9 commits into from Mar 27, 2014
16 changes: 14 additions & 2 deletions dhcp/dhcp_clientv4.ml
Expand Up @@ -109,13 +109,26 @@ let input t ~src ~dst ~src_port buf =
let siaddr = Ipaddr.V4.of_int32 (get_dhcp_siaddr buf) in
let giaddr = Ipaddr.V4.of_int32 (get_dhcp_giaddr buf) in
let xid = get_dhcp_xid buf in
let of_byte x =
Printf.sprintf "%02x" (Char.code x) in
let chaddr_to_string x =
let chaddr_size = (String.length x) in
let dst_buffer = (String.make (chaddr_size * 2) '\000') in
for i = 0 to (chaddr_size - 1) do
let thischar = of_byte x.[i] in
String.set dst_buffer (i*2) (String.get thischar 0);
String.set dst_buffer ((i*2)+1) (String.get thischar 1)
done;
dst_buffer
in
let chaddr = (chaddr_to_string) (copy_dhcp_chaddr buf) in
let options = Cstruct.(copy buf sizeof_dhcp (len buf - sizeof_dhcp)) in
let packet = Dhcpv4_option.Packet.of_bytes options in
(* For debugging, print out the DHCP response *)
Console.log_s t.c (sprintf "DHCP: input ciaddr %s yiaddr %s siaddr %s giaddr %s chaddr %s sname %s file %s\n"
(Ipaddr.V4.to_string ciaddr) (Ipaddr.V4.to_string yiaddr)
(Ipaddr.V4.to_string siaddr) (Ipaddr.V4.to_string giaddr)
(copy_dhcp_chaddr buf) (copy_dhcp_sname buf) (copy_dhcp_file buf))
(chaddr) (copy_dhcp_sname buf) (copy_dhcp_file buf))
>>= fun () ->
(* See what state our Netif is in and if this packet is useful *)
let open Dhcpv4_option.Packet in
Expand Down Expand Up @@ -204,7 +217,6 @@ let input t ~src ~dst ~src_port buf =
|Shutting_down ->
Console.log_s t.c "DHCP thread: done"
|_ ->
(* TODO: This should be looking at the lease time *)
Time.sleep 3600.0
>>= fun () ->
dhcp_thread t
Expand Down
26 changes: 19 additions & 7 deletions dhcp/dhcpv4_option.ml
Expand Up @@ -305,7 +305,9 @@ module Unmarshal = struct
let getint () = (* Get one integer *)
Char.code (getc ()) in
let slice len = (* Get a substring *)
let r = String.sub buf !pos len in
if (!pos + len) > (String.length buf) || !pos > (String.length buf)
then raise (Error (sprintf "Requested too much string at %d %d (%d)" !pos len (String.length buf) ));
let r = String.sub buf !pos len in
pos := !pos + len;
r in
let check c = (* Check that a char is the provided value *)
Expand All @@ -314,6 +316,14 @@ module Unmarshal = struct
let get_addr fn = (* Get one address *)
check '\004';
fn (slice 4) in
let get_number len = (* Get a number from len bytes *)
let bytestring = slice len in
let r = ref 0 in
for i = 0 to (len - 1) do
let bitshift = ((len - (i + 1)) * 8) in
r := ((Char.code bytestring.[i]) lsl bitshift) + !r;
done;
!r in
let get_addrs fn = (* Repeat fn n times and return the list *)
let len = getint () / 4 in
let res = ref [] in
Expand Down Expand Up @@ -366,14 +376,16 @@ module Unmarshal = struct
done;
cont (`Parameter_request (List.rev !params))
|`Max_size ->
let l1 = getint () lsl 8 in
cont (`Max_size (getint () + l1))
|`Interface_mtu ->
let l1 = getint () lsl 8 in
cont (`Interface_mtu (getint () + l1))
let len = getint () in
cont (`Max_size (get_number len))
|`Interface_mtu ->
(* according to some printf/tcpdump testing, this is being set but not
* respected by the unikernel *)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not clear what we should do with the MTU; I've opened up mirage/mirage#238

let len = getint () in
cont (`Interface_mtu (get_number len))
|`Client_id ->
let len = getint () in
let _ = getint () in
let _ = getint () in (* disregard type information *)
cont (`Client_id (slice len))
|`End -> acc
|`Unknown c -> cont (`Unknown (c, (slice (getint ()))))
Expand Down