Skip to content

Commit

Permalink
fixes in lwt files with new Cstruct API
Browse files Browse the repository at this point in the history
  • Loading branch information
crotsos committed Dec 28, 2012
1 parent 1e1b2c3 commit 1f7921c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 16 deletions.
9 changes: 5 additions & 4 deletions lwt/dns_resolver.ml
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,12 @@ let outfd addr port =
fd

let txbuf fd dst buf =
Lwt_bytes.sendto fd buf 0 (Cstruct.len buf) [] dst
Lwt_bytes.sendto fd buf.Cstruct.buffer buf.Cstruct.off buf.Cstruct.len [] dst

let rxbuf fd len =
let buf = Lwt_bytes.create len in
lwt (len, sa) = Lwt_bytes.recvfrom fd buf 0 len [] in
let buf = Cstruct.create len in
lwt (len, sa) = Lwt_bytes.recvfrom fd buf.Cstruct.buffer buf.Cstruct.off
buf.Cstruct.len [] in
return (buf, sa)

let rec send_req ofd dst q = function
Expand All @@ -97,7 +98,7 @@ let rec rcv_query ofd q =

let send_pkt (server:string) (dns_port:int) pkt =
let ofd = outfd "0.0.0.0" 0 in
let buf = Lwt_bytes.create 4096 in
let buf = Cstruct.create 4096 in
let q = DP.marshal buf pkt in
try_lwt
let dst = sockaddr server dns_port in
Expand Down
10 changes: 6 additions & 4 deletions lwt/dns_server.ml
Original file line number Diff line number Diff line change
Expand Up @@ -70,26 +70,28 @@ let process_query fd buf len src dst dnsfn names =
additionals=answer.DQ.additional
})
in
Lwt_bytes.unsafe_fill buf 0 (Lwt_bytes.length buf) '\x00';
(* Lwt_bytes.unsafe_fill buf 0 (Lwt_bytes.length buf) '\x00'; *)
let bits =
contain_exc "marshal" (fun () -> DP.marshal buf response)
in
match bits with
| None -> return ()
| Some buf ->
(* TODO transmit queue, rather than ignoring result here *)
let _ = Lwt_bytes.(sendto fd buf 0 (length buf) [] dst) in
let _ = Lwt_bytes.(sendto fd buf.Cstruct.buffer
buf.Cstruct.off buf.Cstruct.len [] dst) in
return ()
end


let listen ~fd ~src ~(dnsfn:dnsfn) =
let cont = ref true in
let bufs = Lwt_pool.create 64 (fun () -> return (Lwt_bytes.create 1024)) in
let bufs = Lwt_pool.create 64 (fun () -> return (Cstruct.create 1024)) in
let _ =
while_lwt !cont do
Lwt_pool.use bufs (fun buf ->
lwt len, dst = Lwt_bytes.(recvfrom fd buf 0 (length buf) []) in
lwt len, dst = Lwt_bytes.(recvfrom fd buf.Cstruct.buffer buf.Cstruct.off
buf.Cstruct.len[]) in
let names = Hashtbl.create 64 in
return (Lwt.ignore_result (process_query fd buf len src dst dnsfn names) )
)
Expand Down
8 changes: 4 additions & 4 deletions lwt/dnssec_rsa.ml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ let new_rsa_key_from_param param =
let rsa_key_to_dnskey key =
let e = from_hex (rsa_get_e key) in
let n = from_hex (rsa_get_n key) in
let ret = Lwt_bytes.create 4096 in
let ret = Cstruct.create 4096 in
let len =
if (String.length e > 255) then
let _ = Cstruct.set_uint8 ret 0 0 in
Expand All @@ -92,14 +92,14 @@ let rsa_key_to_dnskey key =
1
in
let buf = Cstruct.shift ret len in
let _ = Cstruct.set_buffer e 0 buf 0 (String.length e) in
let _ = Cstruct.blit_from_string e 0 buf 0 (String.length e) in
let buf = Cstruct.shift buf (String.length e) in
let _ = Cstruct.set_buffer n 0 buf 0 (String.length n) in
let _ = Cstruct.blit_from_string n 0 buf 0 (String.length n) in
let len = len + (String.length e) + (String.length n) in
Cstruct.to_string (Cstruct.sub ret 0 len)

let dnskey_to_rsa_key data =
let buf = Lwt_bytes.of_string data in
let buf = Cstruct.of_bigarray (Lwt_bytes.of_string data) in
let ret = new_rsa_key () in
let (e, n) =
match (Cstruct.get_uint8 buf 0) with
Expand Down
14 changes: 10 additions & 4 deletions lwt/sec.ml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ let get_dnskey_tag rdata =
let names = Hashtbl.create 0 in
let buf = Lwt_bytes.create 1024 in
let _ = Lwt_bytes.fill buf 0 1024 (char_of_int 0) in
let buf = Cstruct.of_bigarray buf in
let (_, _, len) = marshal_rdata names 0 buf rdata in
let buf = Cstruct.sub buf 0 len in
let res = ref 0l in
Expand Down Expand Up @@ -200,7 +201,9 @@ let get_ds_rr owner digest rdata =
| DNSKEY(_, alg, key) ->
let names = Hashtbl.create 0 in
let buf = Lwt_bytes.create 1024 in
let _ = Lwt_bytes.fill buf 0 1024 (char_of_int 0) in
let _ = Lwt_bytes.fill (Lwt_bytes.create 1024) 0 1024 (char_of_int 0) in
let buf = Cstruct.of_bigarray buf in

let (_, name_len, _) = marshal_name names 0 buf owner in
let (_, _, len) = marshal_rdata names 0
(Cstruct.shift buf name_len) rdata in
Expand Down Expand Up @@ -247,7 +250,8 @@ let resolve_record st typ owner =

let marshal_rrsig_data ttl rrsig rrset =
let buf = Lwt_bytes.create 4096 in
let _ = Lwt_bytes.fill buf 0 4096 (char_of_int 0) in
let _ = Lwt_bytes.fill buf 0 4096 (char_of_int 0) in
let buf = Cstruct.of_bigarray buf in
(* Firstly marshal the rrsig field *)
let names = Hashtbl.create 0 in
let (_, names, rdbuf) = marshal_rdata names
Expand Down Expand Up @@ -415,7 +419,8 @@ let sign_packet
?(expiration=(Int32.of_float ((Unix.gettimeofday ()) +. 300.0))) (* 1 week duration *)
alg key tag owner pkt =
let data = Lwt_bytes.create 4096 in
let _ = Lwt_bytes.fill data 0 4096 (char_of_int 0) in
let _ = Lwt_bytes.fill data 0 4096 (char_of_int 0) in
let data = Cstruct.of_bigarray data in
let rdata = SIG(alg, expiration, inception, tag, owner, "") in
let names = Hashtbl.create 0 in
let (_, _, rdlen) =
Expand Down Expand Up @@ -461,7 +466,8 @@ let verify_packet st pkt =
answers=pkt.answers; authorities=pkt.authorities;
additionals;} in
let data = Lwt_bytes.create 4096 in
let _ = Lwt_bytes.fill data 0 4096 (char_of_int 0) in
let _ = Lwt_bytes.fill data 0 4096 (char_of_int 0) in
let data = Cstruct.of_bigarray data in
let rdata = SIG(alg, expiration, inception, tag, owner, "") in
let names = Hashtbl.create 0 in
let (_, _, rdlen) =
Expand Down

0 comments on commit 1f7921c

Please sign in to comment.