Skip to content

Commit

Permalink
Merge pull request #214 from yomimono/standardize_record_fields
Browse files Browse the repository at this point in the history
standardize source/destination IP, source/destination port arguments
  • Loading branch information
yomimono authored Jun 29, 2016
2 parents 29a5e06 + 1ff5d6c commit 5d0ed4d
Show file tree
Hide file tree
Showing 19 changed files with 60 additions and 60 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ script: bash -ex .travis-ci.sh
sudo: required
env:
global:
- PINS="mirage mirage-types mirage-types-lwt"
- PINS="mirage mirage-types mirage-types-lwt dns:git://github.com/yomimono/ocaml-dns.git#standardize_record_fields"
matrix:
- OCAML_VERSION=4.02 PACKAGE=tcpip MIRAGE_MODE=unix
- UPDATE_GCC_BINUTILS=1 OCAML_VERSION=4.02 PACKAGE=tcpip MIRAGE_MODE=xen
2 changes: 1 addition & 1 deletion lib/dhcp/dhcp_clientv4.ml
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ module Make
Cstruct.blit_from_string options 0 buf sizeof_dhcp options_len;
let buf = Cstruct.set_len buf (sizeof_dhcp + options_len) in
Log.info (fun f -> f "Sending DHCP broadcast (length %d)" total_len);
Udp.write ~dest_ip:Ipaddr.V4.broadcast ~source_port:68 ~dest_port:67 t.udp buf
Udp.write ~dst:Ipaddr.V4.broadcast ~src_port:68 ~dst_port:67 t.udp buf

(* Receive a DHCP UDP packet *)
let input t ~src:_ ~dst:_ ~src_port:_ buf =
Expand Down
2 changes: 1 addition & 1 deletion lib/ipv4/ipv4.ml
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ module Make(Ethif: V1_LWT.ETHIF) (Arpv4 : V1_LWT.ARP) = struct
Ipv4_wire.set_ipv4_csum packet 0;
Tcpip_checksum.ones_complement_list (packet :: bufs)

let get_source t ~dst:_ =
let src t ~dst:_ =
t.ip

type uipaddr = Ipaddr.t
Expand Down
4 changes: 2 additions & 2 deletions lib/ipv6/ipv6.ml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ module Make (E : V1_LWT.ETHIF) (T : V1_LWT.TIME) (C : V1.CLOCK) = struct

let checksum = Ndpv6.checksum

let get_source t ~dst = Ndpv6.select_source t.ctx dst
let src t ~dst = Ndpv6.select_source t.ctx dst

let set_ip t ip =
let now = C.time () in
Expand Down Expand Up @@ -111,7 +111,7 @@ module Make (E : V1_LWT.ETHIF) (T : V1_LWT.TIME) (C : V1.CLOCK) = struct

let pseudoheader t ~dst ~proto len =
let ph = Cstruct.create (16 + 16 + 8) in
let src = get_source t ~dst in
let src = src t ~dst in
Ndpv6.ipaddr_to_cstruct_raw src ph 0;
Ndpv6.ipaddr_to_cstruct_raw dst ph 16;
Cstruct.BE.set_uint32 ph 32 (Int32.of_int len);
Expand Down
4 changes: 2 additions & 2 deletions lib/tcp/flow.ml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ module Make(IP:V1_LWT.IP)(TM:V1_LWT.TIME)(C:V1.CLOCK)(R:V1.RANDOM) = struct
| Result.Ok () -> Lwt.return_unit

let id = Pcb.ip
let get_dest = Pcb.get_dest
let dst = Pcb.dst
let close t = Pcb.close t
let input = Pcb.input

Expand All @@ -88,7 +88,7 @@ module Make(IP:V1_LWT.IP)(TM:V1_LWT.TIME)(C:V1.CLOCK)(R:V1.RANDOM) = struct
let disconnect _ = Lwt.return_unit

let create_connection tcp (daddr, dport) =
Pcb.connect tcp ~dest_ip:daddr ~dest_port:dport >>= function
Pcb.connect tcp ~dst:daddr ~dst_port:dport >>= function
| `Timeout -> err_timeout daddr dport
| `Rst -> err_refused daddr dport
| `Ok (fl, _) -> ok fl
Expand Down
18 changes: 9 additions & 9 deletions lib/tcp/pcb.ml
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ struct

let process_syn t id ~listeners ~tx_wnd ~ack_number ~sequence ~options ~syn ~fin =
Logs.(log_with_stats Debug "process-syn" t);
match listeners @@ WIRE.local_port_of_id id with
match listeners @@ WIRE.src_port_of_id id with
| Some pushf ->
let tx_isn = Sequence.of_int ((Random.int 65535) + 0x1AFE0000) in
(* TODO: make this configurable per listener *)
Expand Down Expand Up @@ -481,7 +481,7 @@ struct
| Result.Error s -> Log.debug (fun f -> f "parsing TCP header failed: %s" s);
Lwt.return_unit
| Result.Ok (pkt, payload) ->
let id = WIRE.wire ~local_port:pkt.dest_port ~dest_port:pkt.source_port ~dest_ip:src ~local_ip:dst in
let id = WIRE.wire ~src_port:pkt.dst_port ~dst_port:pkt.src_port ~dst:src ~src:dst in
(* Lookup connection from the active PCB hash *)
with_hashtbl t.channels id
(* PCB exists, so continue the connection state machine in tcp_input *)
Expand Down Expand Up @@ -538,9 +538,9 @@ struct
(* Close - no more will be written *)
let close pcb = Tx.close pcb

let get_dest pcb = WIRE.dest_of_id pcb.id
let dst pcb = WIRE.dst_of_id pcb.id

let getid t dest_ip dest_port =
let getid t dst dst_port =
(* TODO: make this more robust and recognise when all ports are gone *)
let islistener _t _port =
(* TODO keep a list of active listen ports *)
Expand All @@ -550,13 +550,13 @@ struct
Hashtbl.mem t.connects id ||
Hashtbl.mem t.listens id
in
let inuse t id = islistener t (WIRE.local_port_of_id id) || idinuse t id in
let inuse t id = islistener t (WIRE.src_port_of_id id) || idinuse t id in
let rec bumpport t =
(match t.localport with
| 65535 -> t.localport <- 10000
| _ -> t.localport <- t.localport + 1);
let id = WIRE.wire ~local_ip:(Ip.get_source t.ip dest_ip)
~local_port:t.localport ~dest_ip ~dest_port in
let id = WIRE.wire ~src:(Ip.src t.ip dst)
~src_port:t.localport ~dst ~dst_port in
if inuse t id then bumpport t else id
in
bumpport t
Expand All @@ -582,8 +582,8 @@ struct
)
else Lwt.return_unit

let connect t ~dest_ip ~dest_port =
let id = getid t dest_ip dest_port in
let connect t ~dst ~dst_port =
let id = getid t dst dst_port in
let tx_isn = Sequence.of_int ((Random.int 65535) + 0x1BCD0000) in
(* TODO: This is hardcoded for now - make it configurable *)
let rx_wnd_scaleoffer = wscale_default in
Expand Down
4 changes: 2 additions & 2 deletions lib/tcp/pcb.mli
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ module Make(Ip:V1_LWT.IP)(Time:V1_LWT.TIME)(Clock:V1.CLOCK)(Random:V1.RANDOM) :
val input: t -> listeners:(int -> (pcb -> unit Lwt.t) option)
-> src:Ip.ipaddr -> dst:Ip.ipaddr -> Cstruct.t -> unit Lwt.t

val connect: t -> dest_ip:Ip.ipaddr -> dest_port:int -> connection_result Lwt.t
val connect: t -> dst:Ip.ipaddr -> dst_port:int -> connection_result Lwt.t

val close: pcb -> unit Lwt.t

val get_dest: pcb -> (Ip.ipaddr * int)
val dst: pcb -> (Ip.ipaddr * int)

(* Blocking read for a segment *)
val read: pcb -> Cstruct.t option Lwt.t
Expand Down
14 changes: 7 additions & 7 deletions lib/tcp/tcp_packet.ml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ type t = {
options : Options.t list;
sequence : Sequence.t;
ack_number : Sequence.t;
source_port : Cstruct.uint16;
dest_port : Cstruct.uint16;
src_port : Cstruct.uint16;
dst_port : Cstruct.uint16;
}

let equal p q = (p = q)
Expand Down Expand Up @@ -58,11 +58,11 @@ module Unmarshal = struct
let syn = get_syn pkt in
let fin = get_fin pkt in
let window = get_tcp_window pkt in
let source_port = get_tcp_src_port pkt in
let dest_port = get_tcp_dst_port pkt in
let src_port = get_tcp_src_port pkt in
let dst_port = get_tcp_dst_port pkt in
let data = Cstruct.shift pkt data_offset in
Result.Ok ({ urg; ack; psh; rst; syn; fin; window; options;
sequence; ack_number; source_port; dest_port }, data)
sequence; ack_number; src_port; dst_port }, data)
end
module Marshal = struct
open Rresult
Expand All @@ -72,8 +72,8 @@ module Marshal = struct

let unsafe_fill ~pseudoheader ~payload t buf options_len =
let data_off = (sizeof_tcp / 4) + (options_len / 4) in
set_tcp_src_port buf t.source_port;
set_tcp_dst_port buf t.dest_port;
set_tcp_src_port buf t.src_port;
set_tcp_dst_port buf t.dst_port;
set_tcp_sequence buf (Sequence.to_int32 t.sequence);
set_tcp_ack_number buf (Sequence.to_int32 t.ack_number);
set_data_offset buf data_off;
Expand Down
4 changes: 2 additions & 2 deletions lib/tcp/tcp_packet.mli
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ type t = {
options : Options.t list;
sequence : Sequence.t;
ack_number : Sequence.t;
source_port : Cstruct.uint16;
dest_port : Cstruct.uint16;
src_port : Cstruct.uint16;
dst_port : Cstruct.uint16;
}

val pp : Format.formatter -> t -> unit
Expand Down
26 changes: 13 additions & 13 deletions lib/tcp/wire.ml
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,25 @@ let set_options buf ts =

module Make (Ip:V1_LWT.IP) = struct
type id = {
dest_port: int; (* Remote TCP port *)
dest_ip: Ip.ipaddr; (* Remote IP address *)
local_port: int; (* Local TCP port *)
local_ip: Ip.ipaddr; (* Local IP address *)
dst_port: int; (* Remote TCP port *)
dst: Ip.ipaddr; (* Remote IP address *)
src_port: int; (* Local TCP port *)
src: Ip.ipaddr; (* Local IP address *)
}

let wire ~local_ip ~local_port ~dest_ip ~dest_port =
{ dest_port ; dest_ip; local_port ; local_ip }
let wire ~src ~src_port ~dst ~dst_port =
{ dst_port ; dst ; src_port ; src }

let local_port_of_id id = id.local_port
let src_port_of_id id = id.src_port

let dest_of_id id = (id.dest_ip, id.dest_port)
let dst_of_id id = (id.dst, id.dst_port)

let pp_id fmt id =
let uip = Ip.to_uipaddr in
Format.fprintf fmt "remote %a,%d to local %a, %d"
Ipaddr.pp_hum (uip id.dest_ip) id.dest_port Ipaddr.pp_hum (uip id.local_ip) id.local_port
Ipaddr.pp_hum (uip id.dst) id.dst_port Ipaddr.pp_hum (uip id.src) id.src_port

let xmit ~ip ~id ?(rst=false) ?(syn=false) ?(fin=false) ?(psh=false)
let xmit ~ip ~(id:id) ?(rst=false) ?(syn=false) ?(fin=false) ?(psh=false)
~rx_ack ~seq ~window ~options payload =
let (ack, ack_number) = match rx_ack with
| None -> (false, Sequence.zero)
Expand All @@ -54,13 +54,13 @@ module Make (Ip:V1_LWT.IP) = struct
sequence = seq; ack_number; window;
urg = false; ack; psh; rst; syn; fin;
options;
source_port = id.local_port; dest_port = id.dest_port;
src_port = id.src_port; dst_port = id.dst_port;
}) in
(* Make a TCP/IP header frame *)
let frame, header_len = Ip.allocate_frame ip ~dst:id.dest_ip ~proto:`TCP in
let frame, header_len = Ip.allocate_frame ip ~dst:id.dst ~proto:`TCP in
(* Shift this out by the combined ethernet + IP header sizes *)
let tcp_buf = Cstruct.shift frame header_len in
let pseudoheader = Ip.pseudoheader ip ~dst:id.dest_ip ~proto:`TCP (Cstruct.len payload) in
let pseudoheader = Ip.pseudoheader ip ~dst:id.dst ~proto:`TCP (Cstruct.len payload) in
match Tcp_packet.Marshal.into_cstruct header tcp_buf ~pseudoheader ~payload with
| Result.Error s ->
Log.info (fun fmt -> fmt "Error transmitting TCP packet: %s" s);
Expand Down
6 changes: 3 additions & 3 deletions lib/tcp/wire.mli
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
module Make(Ip:V1_LWT.IP) : sig
type id

val local_port_of_id : id -> int
val src_port_of_id : id -> int

val dest_of_id : id -> (Ip.ipaddr * int)
val dst_of_id : id -> (Ip.ipaddr * int)

val wire : local_ip:Ip.ipaddr -> local_port:int -> dest_ip:Ip.ipaddr -> dest_port:int -> id
val wire : src:Ip.ipaddr -> src_port:int -> dst:Ip.ipaddr -> dst_port:int -> id

val pp_id : Format.formatter -> id -> unit

Expand Down
16 changes: 8 additions & 8 deletions lib/udp/udp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -52,25 +52,25 @@ module Make(Ip: V1_LWT.IP) = struct
| Some fn ->
fn ~src ~dst ~src_port payload

let writev ?source_port ~dest_ip ~dest_port t bufs =
begin match source_port with
let writev ?src_port ~dst ~dst_port t bufs =
begin match src_port with
| None -> Lwt.fail (Failure "TODO; random source port")
| Some p -> Lwt.return p
end >>= fun source_port ->
let frame, header_len = Ip.allocate_frame t.ip ~dst:dest_ip ~proto:`UDP in
end >>= fun src_port ->
let frame, header_len = Ip.allocate_frame t.ip ~dst:dst ~proto:`UDP in
let frame = Cstruct.set_len frame (header_len + Udp_wire.sizeof_udp) in
let udp_buf = Cstruct.shift frame header_len in
let ph = Ip.pseudoheader t.ip ~dst:dest_ip ~proto:`UDP (Cstruct.lenv bufs) in
let udp_header = Udp_packet.({ src_port = source_port; dst_port = dest_port; }) in
let ph = Ip.pseudoheader t.ip ~dst ~proto:`UDP (Cstruct.lenv bufs) in
let udp_header = Udp_packet.({ src_port = src_port; dst_port = dst_port; }) in
match Udp_packet.Marshal.into_cstruct udp_header udp_buf ~pseudoheader:ph
~payload:(Cstruct.concat bufs) with
| Ok () ->
Ip.writev t.ip frame bufs
| Error s -> Log.debug (fun f -> f "Discarding transmitted UDP message: error writing: %s" s);
Lwt.return_unit

let write ?source_port ~dest_ip ~dest_port t buf =
writev ?source_port ~dest_ip ~dest_port t [buf]
let write ?src_port ~dst ~dst_port t buf =
writev ?src_port ~dst ~dst_port t [buf]

let connect ip =
let ips = List.map Ip.to_uipaddr @@ Ip.get_ip ip in
Expand Down
2 changes: 1 addition & 1 deletion lib_test/test_connect.ml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ module Test_connect (B : Vnetif_backends.Backend) = struct
fail "client tried to write, got %s" err

let accept c flow expected =
let ip, port = V.Stackv4.TCPV4.get_dest flow in
let ip, port = V.Stackv4.TCPV4.dst flow in
log_s c "Accepted connection from %s:%d" (Ipaddr.V4.to_string ip) port
>>= fun () ->
V.Stackv4.TCPV4.read flow >>= function
Expand Down
2 changes: 1 addition & 1 deletion lib_test/test_rfc5961.ml
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ let run backend fsm sut () =
let reply_id_from ~src ~dst data =
let sport = Tcp_wire.get_tcp_src_port data in
let dport = Tcp_wire.get_tcp_dst_port data in
WIRE.wire ~dest_port:sport ~dest_ip:src ~local_port:dport ~local_ip:dst
WIRE.wire ~dst_port:sport ~dst:src ~src_port:dport ~src:dst

let ack_for data =
match Tcp_unmarshal.of_cstruct data with
Expand Down
2 changes: 1 addition & 1 deletion unix/ipv4_socket.ml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ let get_ip_netmasks _ = [Ipaddr.V4.of_string_exn "255.255.255.0"]
let get_ip_gateways _ = raise (Failure "Not implemented")
let set_ip_netmask _ _ = fail (Failure "Not implemented")
let set_ip_gateways _ _ = fail (Failure "Not implemented")
let get_source _ ~dst:_ = raise (Failure "Not implemented")
let src _ ~dst:_ = raise (Failure "Not implemented")
let checksum _ _ = raise (Failure "Not implemented")
let pseudoheader _ ~dst:_ ~proto:_ _ = raise (Failure "Not implemented")
2 changes: 1 addition & 1 deletion unix/ipv6_socket.ml
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ let get_ip_gateways _ = raise (Failure "Not implemented")
let set_ip_gateways _ _ = fail (Failure "Not implemented")

let checksum _ _ = raise (Failure "Not implemented")
let get_source _ ~dst:_ = raise (Failure "Not implemented")
let src _ ~dst:_ = raise (Failure "Not implemented")
2 changes: 1 addition & 1 deletion unix/tcpv4_socket.ml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ let id {interface} =
| None -> None
| Some i -> Some (Ipaddr_unix.V4.of_inet_addr_exn i)

let get_dest fd =
let dst fd =
match Lwt_unix.getpeername fd with
| Unix.ADDR_UNIX _ ->
raise (Failure "unexpected: got a unix instead of tcp sock")
Expand Down
2 changes: 1 addition & 1 deletion unix/tcpv6_socket.ml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ let id {interface} =
| None -> None
| Some i -> Some (Ipaddr_unix.V6.of_inet_addr_exn i)

let get_dest fd =
let dst fd =
match Lwt_unix.getpeername fd with
| Unix.ADDR_UNIX _ ->
raise (Failure "unexpected: got a unix instead of tcp sock")
Expand Down
6 changes: 3 additions & 3 deletions unix/udpv4_socket.ml
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ let id { interface; _ } =
let t, _ = Lwt.task () in
t

let write ?source_port ~dest_ip ~dest_port t buf =
let write ?src_port ~dst ~dst_port t buf =
let open Lwt_unix in
let fd =
match source_port with
match src_port with
| None -> get_udpv4_listening_fd t 0
| Some port -> get_udpv4_listening_fd t port
in
Lwt_cstruct.sendto fd buf [] (ADDR_INET ((Ipaddr_unix.V4.to_inet_addr dest_ip), dest_port))
Lwt_cstruct.sendto fd buf [] (ADDR_INET ((Ipaddr_unix.V4.to_inet_addr dst), dst_port))
>>= fun _ ->
return_unit

0 comments on commit 5d0ed4d

Please sign in to comment.