Skip to content

Commit 55e0d62

Browse files
authored
Merge pull request #3774 from softins/ip4-ip6-separate-sockets
Make IPv4 and IPv6 use separate sockets
2 parents 41d4dc7 + fa3bc12 commit 55e0d62

6 files changed

Lines changed: 477 additions & 289 deletions

File tree

src/client.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ CClient::CClient ( const quint16 iPortNumber,
7272
bMuteOutStream ( false ),
7373
fMuteOutStreamGain ( 1.0f ),
7474
bIPv6Available ( false ),
75-
Socket ( &Channel, iPortNumber, iQosNumber, "", bNDisableIPv6, bIPv6Available ),
75+
Socket ( &Channel, iPortNumber, iQosNumber, "", "", bNDisableIPv6, bIPv6Available ),
7676
Sound ( AudioCallback, this, bNoAutoJackConnect, strNClientName ),
7777
iAudioInFader ( AUD_FADER_IN_MIDDLE ),
7878
bReverbOnLeftChan ( false ),

src/main.cpp

Lines changed: 55 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ int main ( int argc, char** argv )
133133
QString strServerListFileName = "";
134134
QString strServerInfo = "";
135135
QString strServerPublicIP = "";
136-
QString strServerBindIP = "";
136+
QString strServerBindIP4 = "";
137+
QString strServerBindIP6 = "";
137138
QString strServerListFilter = "";
138139
QString strWelcomeMessage = "";
139140
QString strClientName = "";
@@ -444,14 +445,29 @@ int main ( int argc, char** argv )
444445
if ( GetStringArgument ( argc,
445446
argv,
446447
i,
447-
"--serverbindip", // no short form
448-
"--serverbindip",
448+
"--serverbindip", // use short form for compatibility alias
449+
"--serverbindip4",
449450
strArgument ) )
450451
{
451-
strServerBindIP = strArgument;
452-
qInfo() << qUtf8Printable ( QString ( "- server bind IP: %1" ).arg ( strServerBindIP ) );
453-
CommandLineOptions << "--serverbindip";
454-
ServerOnlyOptions << "--serverbindip";
452+
strServerBindIP4 = strArgument;
453+
qInfo() << qUtf8Printable ( QString ( "- server bind IPv4: %1" ).arg ( strServerBindIP4 ) );
454+
CommandLineOptions << "--serverbindip4";
455+
ServerOnlyOptions << "--serverbindip4";
456+
continue;
457+
}
458+
459+
// Server Bind IPv6 --------------------------------------------------
460+
if ( GetStringArgument ( argc,
461+
argv,
462+
i,
463+
"--serverbindip6", // no short form
464+
"--serverbindip6",
465+
strArgument ) )
466+
{
467+
strServerBindIP6 = strArgument;
468+
qInfo() << qUtf8Printable ( QString ( "- server bind IPv6: %1" ).arg ( strServerBindIP6 ) );
469+
CommandLineOptions << "--serverbindip6";
470+
ServerOnlyOptions << "--serverbindip6";
455471
continue;
456472
}
457473

@@ -787,20 +803,17 @@ int main ( int argc, char** argv )
787803
}
788804
}
789805

790-
if ( strDirectoryAddress.isEmpty() )
806+
if ( !strServerPublicIP.isEmpty() )
791807
{
792-
if ( !strServerPublicIP.isEmpty() )
808+
if ( strDirectoryAddress.isEmpty() )
793809
{
794810
qWarning() << "Server Public IP will only take effect when registering a server with a directory.";
795811
strServerPublicIP = "";
796812
}
797-
}
798-
else
799-
{
800-
if ( !strServerPublicIP.isEmpty() )
813+
else
801814
{
802-
QHostAddress InetAddr;
803-
if ( !InetAddr.setAddress ( strServerPublicIP ) )
815+
QHostAddress InetAddr ( strServerPublicIP );
816+
if ( InetAddr.protocol() != QAbstractSocket::IPv4Protocol )
804817
{
805818
qWarning() << "Server Public IP is invalid. Only plain IP addresses are supported.";
806819
strServerPublicIP = "";
@@ -809,13 +822,29 @@ int main ( int argc, char** argv )
809822
}
810823
}
811824

812-
if ( !strServerBindIP.isEmpty() )
825+
if ( !strServerBindIP4.isEmpty() )
813826
{
814-
QHostAddress InetAddr;
815-
if ( !InetAddr.setAddress ( strServerBindIP ) )
827+
QHostAddress InetAddr ( strServerBindIP4 );
828+
if ( InetAddr.protocol() != QAbstractSocket::IPv4Protocol )
816829
{
817-
qWarning() << "Server Bind IP is invalid. Only plain IP addresses are supported.";
818-
strServerBindIP = "";
830+
qCritical() << "Server Bind IPv4 is invalid. Only plain IP addresses are supported.";
831+
exit ( 1 );
832+
}
833+
}
834+
835+
if ( !strServerBindIP6.isEmpty() )
836+
{
837+
if ( bDisableIPv6 )
838+
{
839+
qCritical() << "IPv6 is disabled; --serverbindip6 not allowed.";
840+
exit ( 1 );
841+
}
842+
843+
QHostAddress InetAddr ( strServerBindIP6 );
844+
if ( InetAddr.protocol() != QAbstractSocket::IPv6Protocol )
845+
{
846+
qCritical() << "Server Bind IPv6 is invalid. Only plain IPv6 addresses are supported.";
847+
exit ( 1 );
819848
}
820849
}
821850
#ifndef NO_JSON_RPC
@@ -832,8 +861,8 @@ int main ( int argc, char** argv )
832861
// we do it here as an upfront check. The downstream network calls will error
833862
// out on malformed addresses not caught here.
834863
{
835-
QHostAddress InetAddr;
836-
if ( !InetAddr.setAddress ( strJsonRpcBindIP ) )
864+
QHostAddress InetAddr ( strJsonRpcBindIP );
865+
if ( InetAddr.protocol() != QAbstractSocket::IPv4Protocol )
837866
{
838867
qCritical() << qUtf8Printable ( QString ( "The JSON-RPC address specified is not valid, exiting. " ) );
839868
exit ( 1 );
@@ -1006,7 +1035,8 @@ int main ( int argc, char** argv )
10061035
// actual server object
10071036
CServer Server ( iNumServerChannels,
10081037
strLoggingFileName,
1009-
strServerBindIP,
1038+
strServerBindIP4,
1039+
strServerBindIP6,
10101040
iPortNumber,
10111041
iQosNumber,
10121042
strDirectoryAddress,
@@ -1147,8 +1177,8 @@ QString UsageArguments ( char** argv )
11471177
" --norecord set server not to record by default when recording is configured\n"
11481178
" --noraw disable raw audio\n"
11491179
" -s, --server start Server\n"
1150-
" --serverbindip IPv4 address the Server will bind to (rather than all)\n"
1151-
" (only works if IPv6 is unavailable or disabled with --noipv6)\n"
1180+
" --serverbindip4 IPv4 address the Server will bind to (rather than all)\n"
1181+
" --serverbindip6 IPv6 address the Server will bind to (rather than all)\n"
11521182
" -T, --multithreading use multithreading to make better use of\n"
11531183
" multi-core CPUs and support more Clients\n"
11541184
" -u, --numchannels maximum number of channels\n"

src/server.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@
5050
// CServer implementation ******************************************************
5151
CServer::CServer ( const int iNewMaxNumChan,
5252
const QString& strLoggingFileName,
53-
const QString& strServerBindIP,
53+
const QString& strServerBindIP4,
54+
const QString& strServerBindIP6,
5455
const quint16 iPortNumber,
5556
const quint16 iQosNumber,
5657
const QString& strDirectoryAddress,
@@ -74,7 +75,7 @@ CServer::CServer ( const int iNewMaxNumChan,
7475
iCurNumChannels ( 0 ),
7576
bDisableRaw ( bNDisableRaw ),
7677
bIPv6Available ( false ),
77-
Socket ( this, iPortNumber, iQosNumber, strServerBindIP, bNDisableIPv6, bIPv6Available ),
78+
Socket ( this, iPortNumber, iQosNumber, strServerBindIP4, strServerBindIP6, bNDisableIPv6, bIPv6Available ),
7879
Logging(),
7980
iFrameCount ( 0 ),
8081
HighPrecisionTimer ( bNUseDoubleSystemFrameSize ),

src/server.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ class CServer : public QObject, public CServerSlots<MAX_NUM_CHANNELS>
109109
public:
110110
CServer ( const int iNewMaxNumChan,
111111
const QString& strLoggingFileName,
112-
const QString& strServerBindIP,
112+
const QString& strServerBindIP4,
113+
const QString& strServerBindIP6,
113114
const quint16 iPortNumber,
114115
const quint16 iQosNumber,
115116
const QString& strDirectoryAddress,

0 commit comments

Comments
 (0)