Skip to content

Commit e4a8503

Browse files
committed
Keep TCP enabled by default, add configured Unix sockets as an additional endpoint, and support disabling TCP with RemoteServicePort = 0 or -P 0.
1 parent 892e02f commit e4a8503

7 files changed

Lines changed: 312 additions & 56 deletions

File tree

builds/install/misc/firebird.conf

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -769,9 +769,12 @@
769769
# The TCP Service name/Port number to be used for client database
770770
# connections.
771771
#
772-
# It is only necessary to change one of the entries, not both. The
773-
# order of precendence is the 'RemoteServiceName' (if an entry is
774-
# found in the 'services.' file), then the 'RemoteServicePort'.
772+
# If RemoteServicePort is not set, RemoteServiceName is used. If
773+
# RemoteServicePort is set to a non-zero value, it takes precedence
774+
# over RemoteServiceName. Set RemoteServicePort to 0 in the global
775+
# firebird.conf to disable the TCP listener (a Unix domain socket
776+
# listener may still be enabled).
777+
# The server command-line option -P 0 has the same effect.
775778
#
776779
# Per-connection configurable.
777780
#
@@ -784,7 +787,8 @@
784787
# ----------------------------
785788
# Unix domain socket path to be used for client database and service
786789
# connections on platforms with Unix domain socket support. If set,
787-
# the server listens on this socket instead of the TCP service name/port.
790+
# the server listens on this socket in addition to TCP. If TCP is
791+
# disabled with RemoteServicePort = 0, this is the only listener.
788792
# The name should not contain colons, except for a Windows drive letter.
789793
#
790794
# Event notification connections use a separate Unix domain socket in

doc/Firebird_conf.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ PrioritySwitchDelay integer default 100 (milliseconds)
141141
DeadThreadsCollection integer default 50
142142
PriorityBoost integer default 5
143143
RemoteServiceName string default gds_db
144-
RemoteServicePort integer default 3050 (TCP port number)
145-
RemoteServiceUnixSocket string default empty (Unix domain socket path)
144+
RemoteServicePort integer default 3050 (TCP port number; explicitly set to 0 to disable TCP listening)
145+
RemoteServiceUnixSocket string default empty (Unix domain socket path; additional listener, or sole listener when RemoteServicePort is 0)
146146
IpcName string default "FIREBIRD" (Windows only)
147147

148148
MaxUnflushedWrites integer

src/remote/inet.cpp

Lines changed: 80 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,7 @@ static bool packet_receive2(rem_port*, UCHAR*, SSHORT, SSHORT*);
614614
static bool packet_send(rem_port*, const SCHAR*, SSHORT);
615615
static rem_port* receive(rem_port*, PACKET *);
616616
static rem_port* select_accept(rem_port*);
617+
static bool is_listener(const rem_port*);
617618

618619
static void select_port(rem_port*, Select*, RemPortPtr&);
619620
static bool select_multi(rem_port*, UCHAR* buffer, SSHORT bufsize, SSHORT* length, RemPortPtr&);
@@ -999,7 +1000,8 @@ rem_port* INET_connect(const TEXT* name,
9991000
USHORT flag,
10001001
ClumpletReader* dpb,
10011002
RefPtr<const Config>* config,
1002-
int af)
1003+
int af,
1004+
bool disableTcp)
10031005
{
10041006
/**************************************
10051007
*
@@ -1036,20 +1038,33 @@ rem_port* INET_connect(const TEXT* name,
10361038
}
10371039
REMOTE_get_timeout_params(port, dpb);
10381040

1041+
const RefPtr<const Config> portConfig = port->getPortConfig();
1042+
const bool explicitTcpPort = !packet && name && name[0];
1043+
// RemoteServicePort defaults to zero internally; only an explicitly configured zero disables TCP.
1044+
const bool configDisablesTcp = !packet && !explicitTcpPort &&
1045+
portConfig->getIsSet(KEY_REMOTE_SERVICE_PORT) && portConfig->getRemoteServicePort() == 0;
1046+
const bool tcpDisabled = !packet && (disableTcp || configDisablesTcp);
1047+
10391048
string host;
10401049
string protocol;
10411050

1042-
if ((!name || !name[0]) && !packet)
1051+
#ifdef HAVE_AF_UNIX_SUPPORT
1052+
const char* const socketPath = !packet ? portConfig->getRemoteServiceUnixSocket() : nullptr;
1053+
#endif
1054+
1055+
if (!packet && tcpDisabled)
10431056
{
10441057
#ifdef HAVE_AF_UNIX_SUPPORT
1045-
const char* const socketPath = port->getPortConfig()->getRemoteServiceUnixSocket();
10461058
if (socketPath && socketPath[0])
10471059
return unix_connect(port, socketPath, packet, flag);
10481060
#endif
10491061

1050-
name = port->getPortConfig()->getRemoteBindAddress();
1062+
inet_error(true, port, "listen", isc_net_connect_listen_err, 0);
10511063
}
10521064

1065+
if ((!name || !name[0]) && !packet)
1066+
name = portConfig->getRemoteBindAddress();
1067+
10531068
#ifdef HAVE_AF_UNIX_SUPPORT
10541069
if (af == AF_UNIX)
10551070
return unix_connect(port, name, packet, flag);
@@ -1087,12 +1102,12 @@ rem_port* INET_connect(const TEXT* name,
10871102

10881103
if (protocol.isEmpty())
10891104
{
1090-
const unsigned short port2 = port->getPortConfig()->getRemoteServicePort();
1105+
const unsigned short port2 = portConfig->getRemoteServicePort();
10911106
if (port2) {
10921107
protocol.printf("%hu", port2);
10931108
}
10941109
else {
1095-
protocol = port->getPortConfig()->getRemoteServiceName();
1110+
protocol = portConfig->getRemoteServiceName();
10961111
}
10971112
}
10981113

@@ -1207,6 +1222,47 @@ rem_port* INET_connect(const TEXT* name,
12071222
}
12081223

12091224

1225+
bool INET_shouldListenUnix(const TEXT* name, bool disableTcp)
1226+
{
1227+
#ifdef HAVE_AF_UNIX_SUPPORT
1228+
const RefPtr<const Config> config = Config::getDefaultConfig();
1229+
const bool explicitTcpPort = name && name[0];
1230+
const bool configDisablesTcp = !explicitTcpPort &&
1231+
config->getIsSet(KEY_REMOTE_SERVICE_PORT) && config->getRemoteServicePort() == 0;
1232+
const char* const socketPath = config->getRemoteServiceUnixSocket();
1233+
1234+
return socketPath && socketPath[0] && !disableTcp && !configDisablesTcp;
1235+
#else
1236+
return false;
1237+
#endif
1238+
}
1239+
1240+
1241+
rem_port* INET_listenUnix(USHORT flag)
1242+
{
1243+
#ifdef HAVE_AF_UNIX_SUPPORT
1244+
const char* const socketPath = Config::getDefaultConfig()->getRemoteServiceUnixSocket();
1245+
if (socketPath && socketPath[0])
1246+
return unix_connect(alloc_port(nullptr), socketPath, nullptr, flag);
1247+
#endif
1248+
1249+
return nullptr;
1250+
}
1251+
1252+
1253+
void INET_addUnixListener(rem_port* mainPort, USHORT flag)
1254+
{
1255+
#ifdef HAVE_AF_UNIX_SUPPORT
1256+
if (!(flag & SRVR_multi_client) || (mainPort->port_flags & PORT_unix))
1257+
return;
1258+
1259+
const char* const socketPath = mainPort->getPortConfig()->getRemoteServiceUnixSocket();
1260+
if (socketPath && socketPath[0])
1261+
unix_connect(alloc_port(mainPort), socketPath, nullptr, flag);
1262+
#endif
1263+
}
1264+
1265+
12101266
#ifdef HAVE_AF_UNIX_SUPPORT
12111267

12121268
static rem_port* unix_connect(rem_port* port, const TEXT* socketPath, PACKET* packet, USHORT flag)
@@ -1263,7 +1319,7 @@ static rem_port* unix_listener_socket(rem_port* port, USHORT flag, const TEXT* s
12631319
port->port_flags |= PORT_unix_unlink;
12641320

12651321
if (listen(port->port_handle, SOMAXCONN) < 0)
1266-
inet_error(false, port, "listen", isc_net_connect_listen_err, INET_ERRNO);
1322+
inet_error(true, port, "listen", isc_net_connect_listen_err, INET_ERRNO);
12671323

12681324
inet_ports->registerPort(port);
12691325

@@ -1282,7 +1338,10 @@ static rem_port* unix_listener_socket(rem_port* port, USHORT flag, const TEXT* s
12821338
if (s == INVALID_SOCKET)
12831339
{
12841340
if (INET_shutting_down)
1341+
{
1342+
disconnect(port);
12851343
return NULL;
1344+
}
12861345
inet_error(true, port, "accept", isc_net_connect_err, inetErrNo);
12871346
}
12881347

@@ -2455,6 +2514,12 @@ static rem_port* receive( rem_port* main_port, PACKET * packet)
24552514
return main_port;
24562515
}
24572516

2517+
static bool is_listener(const rem_port* port)
2518+
{
2519+
return port && (port->port_server_flags & SRVR_multi_client) &&
2520+
!(port->port_flags & (PORT_server | PORT_async));
2521+
}
2522+
24582523
static bool select_multi(rem_port* main_port, UCHAR* buffer, SSHORT bufsize, SSHORT* length,
24592524
RemPortPtr& port)
24602525
{
@@ -2482,19 +2547,19 @@ static bool select_multi(rem_port* main_port, UCHAR* buffer, SSHORT bufsize, SSH
24822547
for (;;)
24832548
{
24842549
select_port(main_port, &INET_select, port);
2485-
if (port == main_port && (port->port_server_flags & SRVR_multi_client))
2550+
if (is_listener(port))
24862551
{
24872552
if (INET_shutting_down)
24882553
{
2489-
if (main_port->port_state == rem_port::PENDING)
2554+
if (port->port_state == rem_port::PENDING)
24902555
{
2491-
main_port->port_state = rem_port::BROKEN;
2556+
port->port_state = rem_port::BROKEN;
24922557

2493-
shutdown(main_port->port_handle, 2);
2494-
SOCLOSE(main_port->port_handle);
2558+
shutdown(port->port_handle, 2);
2559+
SOCLOSE(port->port_handle);
24952560
}
24962561
}
2497-
else if ((port = select_accept(main_port)))
2562+
else if ((port = select_accept(port)))
24982563
{
24992564
if (!REMOTE_inflate(port, packet_receive, buffer, bufsize, length))
25002565
{
@@ -2726,8 +2791,8 @@ static bool select_wait( rem_port* main_port, Select* selct)
27262791
}
27272792
}
27282793

2729-
// if process is shuting down - don't listen on main port
2730-
if (!INET_shutting_down || port != main_port)
2794+
// if process is shutting down - don't listen on server ports
2795+
if (!INET_shutting_down || !is_listener(port))
27312796
{
27322797
selct->set(port->port_handle);
27332798
found = true;

src/remote/inet_proto.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ rem_port* INET_analyze(ClntAuthBlock*, const Firebird::PathName&, const TEXT*,
3737
bool, Firebird::ClumpletReader&, Firebird::RefPtr<const Firebird::Config>*,
3838
const Firebird::PathName*, Firebird::ICryptKeyCallback*, int af = AF_UNSPEC);
3939
rem_port* INET_connect(const TEXT*, struct packet*, USHORT, Firebird::ClumpletReader*,
40-
Firebird::RefPtr<const Firebird::Config>*, int af = AF_UNSPEC);
40+
Firebird::RefPtr<const Firebird::Config>*, int af = AF_UNSPEC,
41+
bool disableTcp = false);
42+
bool INET_shouldListenUnix(const TEXT*, bool disableTcp);
43+
rem_port* INET_listenUnix(USHORT);
44+
void INET_addUnixListener(rem_port*, USHORT);
4145
rem_port* INET_reconnect(SOCKET, bool unixSocket);
4246
rem_port* INET_server(SOCKET);
4347
void setStopMainThread(FPTR_INT func);

0 commit comments

Comments
 (0)