Skip to content

Commit 939d46c

Browse files
committed
support bind and connect
1 parent e731c2c commit 939d46c

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

freebsd/netinet/in_pcb.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1126,7 +1126,10 @@ if (lport == 0)
11261126
ifp_sin.sin_len = sizeof(ifp_sin);
11271127
ifa = ifa_ifwithnet((struct sockaddr *)&ifp_sin, 0, RT_ALL_FIBS);
11281128
if (ifa == NULL) {
1129-
return (EADDRNOTAVAIL);
1129+
ifp_sin.sin_addr.s_addr = faddr.s_addr;
1130+
ifa = ifa_ifwithnet((struct sockaddr *)&ifp_sin, 0, RT_ALL_FIBS);
1131+
if ( ifa == NULL )
1132+
return (EADDRNOTAVAIL);
11301133
}
11311134
ifp = ifa->ifa_ifp;
11321135
while (lport == 0) {

freebsd/netinet/tcp_usrreq.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,14 +1321,21 @@ tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
13211321
error = EADDRINUSE;
13221322
goto out;
13231323
}
1324+
// inp->inp_lport != lport means in_pcbconnect_setup selected new port to inp->inp_lport.
1325+
// inp will inhash.
1326+
if (in_pcbinshash(inp) != 0) {
1327+
inp->inp_laddr.s_addr = INADDR_ANY;
1328+
inp->inp_lport = 0;
1329+
return (EAGAIN);
1330+
}
13241331
}
1325-
1326-
if (in_pcbinshash(inp) != 0) {
1327-
inp->inp_laddr.s_addr = INADDR_ANY;
1328-
inp->inp_lport = 0;
1329-
return (EAGAIN);
1332+
else
1333+
{
1334+
// app call bind() and connect(), lport is set when bind, and the inp is inhashed in bind() function.
1335+
// in_pcbconnect_setup() update inp->inp_faddr/inp->inp_fport, so inp should be rehashed.
1336+
in_pcbrehash(inp);
13301337
}
1331-
1338+
13321339
if (anonport) {
13331340
inp->inp_flags |= INP_ANONPORT;
13341341
}

0 commit comments

Comments
 (0)