Nsock currently ignores source port binding errors, leaving the consuming code blind to the fact that the connected socket is not using the chosen port. As you can see from the code of mksock_bind_addr() in nsock/src/nsock_connect.c, the error is logged but not propagated:
nsock_log_info("Binding to %s (IOD #%li)", get_localaddr_string(iod), iod->id);
rc = bind(iod->sd, (struct sockaddr *)&iod->local, (int) iod->locallen);
if (rc == -1) {
int err = socket_errno();
nsock_log_error("Bind to %s failed (IOD #%li): %s (%d)",
get_localaddr_string(iod), iod->id,
socket_strerror(err), err);
}
return 0;
This is apparently by design, judging by the following comment in nsock_make_socket():
if (iod->locallen)
mksock_bind_addr(ms, iod);
if (iod->ipoptslen && family == AF_INET)
mksock_set_ipopts(ms, iod);
if (ms->device)
mksock_bind_device(ms, iod);
if (ms->broadcast && type != SOCK_STREAM)
mksock_set_broadcast(ms, iod);
/* mksock_* functions can raise warnings/errors
* but we don't let them stop us for now. */
This is causing issues. As an example, the following NSE code in rpc.lua is trying to avoid port collisions but the error checking is not effective because the error is not raised in the first place:
-- Try to bind to a reserved port
for i = 1, 10, 1 do
local resvport = math.random(1, 1024)
socket = new_socket()
status, err = socket:bind(nil, resvport)
if status then
status, err = socket:connect(host, port)
if status or err == "TIMEOUT" then break end
socket:close()
end
end
I am proposing to revisit the decision for suppressing these errors so that the consuming code is not randomly failing.
Nsock currently ignores source port binding errors, leaving the consuming code blind to the fact that the connected socket is not using the chosen port. As you can see from the code of
mksock_bind_addr()innsock/src/nsock_connect.c, the error is logged but not propagated:This is apparently by design, judging by the following comment in
nsock_make_socket():This is causing issues. As an example, the following NSE code in
rpc.luais trying to avoid port collisions but the error checking is not effective because the error is not raised in the first place:I am proposing to revisit the decision for suppressing these errors so that the consuming code is not randomly failing.