Bug 837 - netcat does not accept connections when listening for a specified remote host
Summary: netcat does not accept connections when listening for a specified remote host
Status: RESOLVED FIXED
Alias: None
Product: Busybox
Classification: Unclassified
Component: Networking (show other bugs)
Version: 1.15.x
Hardware: PC Linux
: P5 normal
Target Milestone: ---
Assignee: unassigned
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-01-01 21:07 UTC by mikeus
Modified: 2010-01-30 23:17 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:


Attachments
Fix (1.54 KB, patch)
2010-01-08 15:09 UTC, Denys Vlasenko
Details

Note You need to log in before you can comment on or make changes to this bug.
Description mikeus 2010-01-01 21:07:00 UTC
Nc does not accept connections when listening for a specified remote host.

The command:
nc -n -l -p <local port> <remote host>
always reports "nc: connect from wrong ip/port ... ignored"

Nc accepts a connection only when a remote source port is specified (and the connection comes from it):
nc -n -l -p <local port> <remote host> <remote source port>

This is because of 'memcmp' method of checking out the incoming connection address and port in 'dolisten()' function (line #343 in networking/nc_bloaty.c):
----------------------------------------
340   rr = accept(netfd, &remend.u.sa, &remend.len);
341   if (rr < 0)
342       bb_perror_msg_and_die("accept");
343   if (themaddr && memcmp(&remend.u.sa, &themaddr->u.sa, remend.len) != 0) {
344       /* nc 1.10 bails out instead, and its error message
345        * is not suppressed by o_verbose */
346       if (o_verbose) {
347           char *remaddr = xmalloc_sockaddr2dotted(&remend.u.sa);
348           bb_error_msg("connect from wrong ip/port %s ignored", remaddr);
349           free(remaddr);
350       }
351       close(rr);
352       goto again;
353   }
----------------------------------------

Here 'themaddr->u.sa' has port=0 if it is not specified on the command line while 'remend.u.sa' always has nonzero source port
Comment 1 Denys Vlasenko 2010-01-08 15:09:28 UTC
Created attachment 881 [details]
Fix

Please try attached patch
Comment 2 Denys Vlasenko 2010-01-30 23:17:53 UTC
Fixed in 1.16.0