@@ -614,6 +614,7 @@ static bool packet_receive2(rem_port*, UCHAR*, SSHORT, SSHORT*);
614614static bool packet_send (rem_port*, const SCHAR *, SSHORT );
615615static rem_port* receive (rem_port*, PACKET *);
616616static rem_port* select_accept (rem_port*);
617+ static bool is_listener (const rem_port*);
617618
618619static void select_port (rem_port*, Select*, RemPortPtr&);
619620static 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
12121268static 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+
24582523static 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 ;
0 commit comments