Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Choose client ports in a well-known range #134

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 11 additions & 13 deletions lib/ZOpenPort.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ ZOpenPort(u_short *port)
struct sockaddr_in bindin;
unsigned int len;
int val = 1;
int ret;

(void) ZClosePort();

Expand All @@ -38,17 +39,20 @@ ZOpenPort(u_short *port)
if (setsockopt(__Zephyr_fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof val) < 0)
return errno;
} else {
bindin.sin_port = 0;
bindin.sin_port = htons(60000);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that Mosh uses almost the same port range, picking the first available port in 60001–60999.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mosh's range was prominent in shaping my initial choice here (though I find it amusing that I was off by one for the start port!). Probably I was thinking that a single hole in the firewall would cover both, but some amount of discussion is appropriate before a choice is made.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m a little worried it might break Mosh for users who only open one firewall port for it instead of a range. Only a little, since those users are probably smart enough to figure out what happened. Maybe that risk can be avoided by starting at 60500 or something.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We had a bit of discussion about this involving Keith from the mosh side and a few other folks. The conclusion (if there could be said to be one) was:

To the extent it matters, though, I guess it's probably better for
every random UDP app that needs a range of port numbers (instead of a
single port that can be multiplexed because each peer will use a
different IP address or port) to 'standardize' on 60000-61000 than for
us all to go separately. So, go for it if you want.

}

bindin.sin_addr.s_addr = INADDR_ANY;

if (bind(__Zephyr_fd, (struct sockaddr *)&bindin, sizeof(bindin)) < 0) {
if (errno == EADDRINUSE && port && *port)
return ZERR_PORTINUSE;
else
return errno;
}
do {
ret = bind(__Zephyr_fd, (struct sockaddr *)&bindin, sizeof(bindin));
if (ret < 0 && !(port && *port))
bindin.sin_port++;
} while (ret < 0 && errno == EADDRINUSE && !(port && *port));
if (ret < 0 && errno == EADDRINUSE)
return ZERR_PORTINUSE;
else if (ret < 0)
return errno;

if (port && *port) {
/* turn SO_REUSEADDR back off so no one else can steal it */
Expand All @@ -57,12 +61,6 @@ ZOpenPort(u_short *port)
return errno;
}

if (!bindin.sin_port) {
len = sizeof(bindin);
if (getsockname(__Zephyr_fd, (struct sockaddr *)&bindin, &len))
return errno;
}

__Zephyr_port = bindin.sin_port;
__Zephyr_open = 1;

Expand Down