From 490ee526cca46c7ca8a0cb486197959fde3a880c Mon Sep 17 00:00:00 2001 From: 10077240 Date: Sat, 5 Jan 2019 11:51:57 +0800 Subject: [PATCH 1/2] support bind and connect --- freebsd/netinet/in_pcb.c | 5 ++++- freebsd/netinet/tcp_usrreq.c | 19 +++++++++++++------ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/freebsd/netinet/in_pcb.c b/freebsd/netinet/in_pcb.c index 37d65330a..22730aed6 100644 --- a/freebsd/netinet/in_pcb.c +++ b/freebsd/netinet/in_pcb.c @@ -1126,7 +1126,10 @@ if (lport == 0) ifp_sin.sin_len = sizeof(ifp_sin); ifa = ifa_ifwithnet((struct sockaddr *)&ifp_sin, 0, RT_ALL_FIBS); if (ifa == NULL) { - return (EADDRNOTAVAIL); + ifp_sin.sin_addr.s_addr = faddr.s_addr; + ifa = ifa_ifwithnet((struct sockaddr *)&ifp_sin, 0, RT_ALL_FIBS); + if ( ifa == NULL ) + return (EADDRNOTAVAIL); } ifp = ifa->ifa_ifp; while (lport == 0) { diff --git a/freebsd/netinet/tcp_usrreq.c b/freebsd/netinet/tcp_usrreq.c index b33efedc7..b932beecc 100644 --- a/freebsd/netinet/tcp_usrreq.c +++ b/freebsd/netinet/tcp_usrreq.c @@ -1321,14 +1321,21 @@ tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td) error = EADDRINUSE; goto out; } + // inp->inp_lport != lport means in_pcbconnect_setup selected new port to inp->inp_lport. + // inp will inhash. + if (in_pcbinshash(inp) != 0) { + inp->inp_laddr.s_addr = INADDR_ANY; + inp->inp_lport = 0; + return (EAGAIN); + } } - - if (in_pcbinshash(inp) != 0) { - inp->inp_laddr.s_addr = INADDR_ANY; - inp->inp_lport = 0; - return (EAGAIN); + else + { + // app call bind() and connect(), lport is set when bind, and the inp is inhashed in bind() function. + // in_pcbconnect_setup() update inp->inp_faddr/inp->inp_fport, so inp should be rehashed. + in_pcbrehash(inp); } - + if (anonport) { inp->inp_flags |= INP_ANONPORT; } From 2c6bc8e771e0f470e4a3f0d5e9b22e51802bac1b Mon Sep 17 00:00:00 2001 From: 10077240 Date: Mon, 14 Jan 2019 15:10:42 +0800 Subject: [PATCH 2/2] indent correctly --- freebsd/netinet/tcp_usrreq.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/freebsd/netinet/tcp_usrreq.c b/freebsd/netinet/tcp_usrreq.c index b932beecc..57c7bc18f 100644 --- a/freebsd/netinet/tcp_usrreq.c +++ b/freebsd/netinet/tcp_usrreq.c @@ -1321,19 +1321,20 @@ tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td) error = EADDRINUSE; goto out; } - // inp->inp_lport != lport means in_pcbconnect_setup selected new port to inp->inp_lport. - // inp will inhash. + + // inp->inp_lport != lport means in_pcbconnect_setup selected new port to inp->inp_lport. + // inp will inhash. if (in_pcbinshash(inp) != 0) { - inp->inp_laddr.s_addr = INADDR_ANY; - inp->inp_lport = 0; - return (EAGAIN); - } + inp->inp_laddr.s_addr = INADDR_ANY; + inp->inp_lport = 0; + return (EAGAIN); + } } else { - // app call bind() and connect(), lport is set when bind, and the inp is inhashed in bind() function. - // in_pcbconnect_setup() update inp->inp_faddr/inp->inp_fport, so inp should be rehashed. - in_pcbrehash(inp); + // app call bind() and connect(), lport is set when bind, and the inp is inhashed in bind() function. + // in_pcbconnect_setup() update inp->inp_faddr/inp->inp_fport, so inp should be rehashed. + in_pcbrehash(inp); } if (anonport) {