Skip to content

Commit 2301eb2

Browse files
committedDec 16, 2023
add TCP.S.src
1 parent 18393c0 commit 2301eb2

File tree

4 files changed

+19
-0
lines changed

4 files changed

+19
-0
lines changed
 

‎src/core/tcp.ml

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ module type S = sig
3030
and type write_error := write_error
3131

3232
val dst: flow -> ipaddr * int
33+
val src: flow -> ipaddr * int
3334
val write_nodelay: flow -> Cstruct.t -> (unit, write_error) result Lwt.t
3435
val writev_nodelay: flow -> Cstruct.t list -> (unit, write_error) result Lwt.t
3536
val create_connection: ?keepalive:Keepalive.t -> t -> ipaddr * int -> (flow, error) result Lwt.t

‎src/core/tcp.mli

+4
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ module type S = sig
5353
(** Get the destination IP address and destination port that a
5454
flow is currently connected to. *)
5555

56+
val src : flow -> ipaddr * int
57+
(** Get the source IP address and source port that a flow is currently
58+
connected to. *)
59+
5660
val write_nodelay: flow -> Cstruct.t -> (unit, write_error) result Lwt.t
5761
(** [write_nodelay flow buffer] writes the contents of [buffer]
5862
to the flow. The thread blocks until all data has been successfully

‎src/stack-unix/tcpv4v6_socket.ml

+12
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,18 @@ let dst fd =
7878
in
7979
ip, port
8080

81+
let src fd =
82+
match Lwt_unix.getsockname fd with
83+
| Unix.ADDR_UNIX _ ->
84+
raise (Failure "unexpected: got a unix instead of tcp sock")
85+
| Unix.ADDR_INET (ia,port) ->
86+
let ip = Ipaddr_unix.of_inet_addr ia in
87+
let ip = match Ipaddr.to_v4 ip with
88+
| None -> ip
89+
| Some v4 -> Ipaddr.V4 v4
90+
in
91+
ip, port
92+
8193
let create_connection ?keepalive t (dst,dst_port) =
8294
match
8395
match dst, t.interface with

‎src/tcp/flow.ml

+2
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,8 @@ struct
637637

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

640+
let src pcb = WIRE.src pcb.id, WIRE.src_port pcb.id
641+
640642
let getid t dst dst_port =
641643
(* TODO: make this more robust and recognise when all ports are gone *)
642644
let islistener _t _port =

0 commit comments

Comments
 (0)
Please sign in to comment.