Skip to content

Commit

Permalink
Merge pull request mirage#511 from hannesm/add-tcp-src
Browse files Browse the repository at this point in the history
add TCP.S.src
  • Loading branch information
hannesm authored Dec 20, 2023
2 parents 18393c0 + 2301eb2 commit 4f40f81
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/core/tcp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module type S = sig
and type write_error := write_error

val dst: flow -> ipaddr * int
val src: flow -> ipaddr * int
val write_nodelay: flow -> Cstruct.t -> (unit, write_error) result Lwt.t
val writev_nodelay: flow -> Cstruct.t list -> (unit, write_error) result Lwt.t
val create_connection: ?keepalive:Keepalive.t -> t -> ipaddr * int -> (flow, error) result Lwt.t
Expand Down
4 changes: 4 additions & 0 deletions src/core/tcp.mli
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ module type S = sig
(** Get the destination IP address and destination port that a
flow is currently connected to. *)

val src : flow -> ipaddr * int
(** Get the source IP address and source port that a flow is currently
connected to. *)

val write_nodelay: flow -> Cstruct.t -> (unit, write_error) result Lwt.t
(** [write_nodelay flow buffer] writes the contents of [buffer]
to the flow. The thread blocks until all data has been successfully
Expand Down
12 changes: 12 additions & 0 deletions src/stack-unix/tcpv4v6_socket.ml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,18 @@ let dst fd =
in
ip, port

let src fd =
match Lwt_unix.getsockname fd with
| Unix.ADDR_UNIX _ ->
raise (Failure "unexpected: got a unix instead of tcp sock")
| Unix.ADDR_INET (ia,port) ->
let ip = Ipaddr_unix.of_inet_addr ia in
let ip = match Ipaddr.to_v4 ip with
| None -> ip
| Some v4 -> Ipaddr.V4 v4
in
ip, port

let create_connection ?keepalive t (dst,dst_port) =
match
match dst, t.interface with
Expand Down
2 changes: 2 additions & 0 deletions src/tcp/flow.ml
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,8 @@ struct

let dst pcb = WIRE.dst pcb.id, WIRE.dst_port pcb.id

let src pcb = WIRE.src pcb.id, WIRE.src_port pcb.id

let getid t dst dst_port =
(* TODO: make this more robust and recognise when all ports are gone *)
let islistener _t _port =
Expand Down

0 comments on commit 4f40f81

Please sign in to comment.