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

Implements ability to use custom client port #1328

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
19 changes: 18 additions & 1 deletion src/network/network.cc
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,21 @@ Connection::Connection( const char* key_str, const char* ip, const char* port )

socks.push_back( Socket( remote_addr.sa.sa_family ) );

/* Set user specified client source port. */
const char* mosh_client_port;
if ( ( mosh_client_port = getenv( "MOSH_CLIENT_PORT" ) ) != NULL ) {
struct sockaddr_in srcaddr;
memset( &srcaddr, 0, sizeof( srcaddr ) );
srcaddr.sin_family = AF_INET;
srcaddr.sin_addr.s_addr = htonl( INADDR_ANY );
srcaddr.sin_port = htons( std::stoi( mosh_client_port ) );

if ( bind( sock(), (struct sockaddr*)&srcaddr, sizeof( srcaddr ) ) < 0 ) {
perror( "Cannot bind port" );
throw NetworkException( "error" );
}
}

set_MTU( remote_addr.sa.sa_family );
}

Expand Down Expand Up @@ -393,7 +408,9 @@ void Connection::send( const std::string& s )
fprintf( stderr, "Server now detached from client.\n" );
}
} else { /* client */
if ( ( now - last_port_choice > PORT_HOP_INTERVAL ) && ( now - last_roundtrip_success > PORT_HOP_INTERVAL ) ) {

// only hop port when MOSH CLIENT's port is not fixed by user request
if ( !getenv( "MOSH_CLIENT_PORT" ) && ( now - last_port_choice > PORT_HOP_INTERVAL ) && ( now - last_roundtrip_success > PORT_HOP_INTERVAL ) ) {
hop_port();
}
}
Expand Down