Merge remote-tracking branch 'origin/topic/awelzel/4474-cluster-websocket-ipv6'

* origin/topic/awelzel/4474-cluster-websocket-ipv6:
  IXWebsocket: Bump to version with memset() sock addr fix
  cluster/websocket: Deprecate $listen_host, introduce $listen_addr
  cluster/websocket-ixwebsocket: Determine proper address_family
This commit is contained in:
Arne Welzel 2025-05-30 11:43:57 +02:00
commit f16ebd34b3
44 changed files with 354 additions and 42 deletions

View file

@ -1,3 +1,11 @@
8.0.0-dev.267 | 2025-05-30 11:43:57 +0200
* IXWebsocket: Bump to version with memset() sock addr fix (Arne Welzel, Corelight)
* cluster/websocket: Deprecate $listen_host, introduce $listen_addr (Arne Welzel, Corelight)
* GH-4474: cluster/websocket-ixwebsocket: Determine proper address_family (Arne Welzel, Corelight)
8.0.0-dev.262 | 2025-05-28 08:29:33 -0700 8.0.0-dev.262 | 2025-05-28 08:29:33 -0700
* Remove obsolete --with-bind configure flag (Tim Wojtulewicz, Corelight) * Remove obsolete --with-bind configure flag (Tim Wojtulewicz, Corelight)

4
NEWS
View file

@ -69,6 +69,10 @@ Changed Functionality
found/picked-up by default. Use --with-krb5 for pointing at a custom librkb5 found/picked-up by default. Use --with-krb5 for pointing at a custom librkb5
installation. installation.
- The ``$listen_host`` configuration for ``Cluster::listen_websocket()``'s
``WebSocketServerOptions`` was deprecated. Use the new ``$listen_addr`` field
instead.
Removed Functionality Removed Functionality
--------------------- ---------------------

View file

@ -1 +1 @@
8.0.0-dev.262 8.0.0-dev.267

View file

@ -363,7 +363,9 @@ export {
## WebSocket server options to pass to :zeek:see:`Cluster::listen_websocket`. ## WebSocket server options to pass to :zeek:see:`Cluster::listen_websocket`.
type WebSocketServerOptions: record { type WebSocketServerOptions: record {
## The host address to listen on. ## The host address to listen on.
listen_host: string; listen_host: string &optional &deprecated="Remove in v8.1: Use $listen_addr instead.";
## The address to listen on, cannot be used together with ``listen_host``.
listen_addr: addr &optional;
## The port the WebSocket server is supposed to listen on. ## The port the WebSocket server is supposed to listen on.
listen_port: port; listen_port: port;
## The maximum event queue size for this server. ## The maximum event queue size for this server.

View file

@ -1,6 +1,7 @@
%%{ %%{
#include <string> #include <string>
#include "zeek/IPAddr.h"
#include "zeek/cluster/Backend.h" #include "zeek/cluster/Backend.h"
#include "zeek/cluster/BifSupport.h" #include "zeek/cluster/BifSupport.h"
#include "zeek/cluster/Manager.h" #include "zeek/cluster/Manager.h"
@ -192,8 +193,32 @@ function Cluster::__listen_websocket%(options: WebSocketServerOptions%): bool
tls_options_rec->GetFieldOrDefault<zeek::StringVal>("ciphers")->ToStdString(), tls_options_rec->GetFieldOrDefault<zeek::StringVal>("ciphers")->ToStdString(),
}; };
struct ServerOptions server_options { std::string listen_addr;
options_rec->GetField<zeek::StringVal>("listen_host")->ToStdString(),
// Backwards compat for listen_host if listen_addr isn't set.
if ( options_rec->HasField("listen_host") ) {
if ( options_rec->HasField("listen_addr") ) {
zeek::emit_builtin_error("cannot use both listen_addr and listen_host");
return zeek::val_mgr->False();
}
auto host = options_rec->GetField<zeek::StringVal>("listen_host")->ToStdString();
if ( ! zeek::IPAddr::IsValid(host.c_str()) ) {
zeek::emit_builtin_error("invalid listen_host");
return zeek::val_mgr->False();
}
listen_addr = std::move(host);
} else if ( options_rec->HasField("listen_addr") ) {
listen_addr = options_rec->GetField<zeek::AddrVal>("listen_addr")->AsAddr().AsString();
} else {
zeek::emit_builtin_error("missing listen_host field");
return zeek::val_mgr->False();
}
struct ServerOptions server_options{
listen_addr,
static_cast<uint16_t>(options_rec->GetField<zeek::PortVal>("listen_port")->Port()), static_cast<uint16_t>(options_rec->GetField<zeek::PortVal>("listen_port")->Port()),
}; };

View file

@ -3,10 +3,13 @@
// Implementation of a WebSocket server and clients using the IXWebSocket client library. // Implementation of a WebSocket server and clients using the IXWebSocket client library.
#include "zeek/cluster/websocket/WebSocket.h" #include "zeek/cluster/websocket/WebSocket.h"
#include <sys/socket.h>
#include <memory> #include <memory>
#include <stdexcept> #include <stdexcept>
#include "zeek/IPAddr.h"
#include "zeek/Reporter.h" #include "zeek/Reporter.h"
#include "zeek/net_util.h"
#include "ixwebsocket/IXConnectionState.h" #include "ixwebsocket/IXConnectionState.h"
#include "ixwebsocket/IXSocketTLSOptions.h" #include "ixwebsocket/IXSocketTLSOptions.h"
@ -73,11 +76,18 @@ private:
std::unique_ptr<WebSocketServer> StartServer(std::unique_ptr<WebSocketEventDispatcher> dispatcher, std::unique_ptr<WebSocketServer> StartServer(std::unique_ptr<WebSocketEventDispatcher> dispatcher,
const ServerOptions& options) { const ServerOptions& options) {
auto server = if ( ! zeek::IPAddr::IsValid(options.host.c_str()) ) {
std::make_unique<ix::WebSocketServer>(options.port, options.host, ix::SocketServer::kDefaultTcpBacklog, zeek::reporter->Error("WebSocket: Host is not a valid IP %s", options.host.c_str());
options.max_connections, return nullptr;
ix::WebSocketServer::kDefaultHandShakeTimeoutSecs, }
ix::SocketServer::kDefaultAddressFamily, options.ping_interval_seconds);
zeek::IPAddr host_addr(options.host);
int address_family = host_addr.GetFamily() == IPv4 ? AF_INET : AF_INET6;
auto server = std::make_unique<ix::WebSocketServer>(options.port, options.host,
ix::SocketServer::kDefaultTcpBacklog, options.max_connections,
ix::WebSocketServer::kDefaultHandShakeTimeoutSecs,
address_family, options.ping_interval_seconds);
if ( ! options.per_message_deflate ) if ( ! options.per_message_deflate )
server->disablePerMessageDeflate(); server->disablePerMessageDeflate();

@ -1 +1 @@
Subproject commit 2efe037c9cc96fd536774f17bdb5215161ee5087 Subproject commit a9351e1f11561c1ae846b3005ea4b0f29b2a8ccc

View file

@ -1,5 +1,5 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63. ### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
error in <...>/main.zeek, line 673: Already listening on 127.0.0.1:<port> (Cluster::__listen_websocket(ws_opts_x)) error in <...>/main.zeek, line 675: Already listening on 127.0.0.1:<port> (Cluster::__listen_websocket(ws_opts_x))
error in <...>/main.zeek, line 673: Already listening on 127.0.0.1:<port> (Cluster::__listen_websocket(ws_opts_wss_port)) error in <...>/main.zeek, line 675: Already listening on 127.0.0.1:<port> (Cluster::__listen_websocket(ws_opts_wss_port))
error in <...>/main.zeek, line 673: Already listening on 127.0.0.1:<port> (Cluster::__listen_websocket(ws_opts_qs)) error in <...>/main.zeek, line 675: Already listening on 127.0.0.1:<port> (Cluster::__listen_websocket(ws_opts_qs))
received termination signal received termination signal

View file

@ -0,0 +1,4 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
warning in ../manager.zeek, line 12: deprecated (Cluster::WebSocketServerOptions$listen_host): Remove in v8.1: Use $listen_addr instead. ((coerce [$listen_host=::1, $listen_port=to_port(getenv(WEBSOCKET_PORT))] to Cluster::WebSocketServerOptions))
warning in <no location>: deprecated (Cluster::WebSocketServerOptions$listen_host): Remove in v8.1: Use $listen_addr instead. (Cluster::WebSocketServerOptions($listen_host=::1, $listen_port=to_port(getenv(WEBSOCKET_PORT))))
received termination signal

View file

@ -1,3 +1,3 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63. ### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
error in <...>/main.zeek, line 673: Invalid tls_options: No key_file field (Cluster::__listen_websocket(Cluster::options.0)) error in <...>/main.zeek, line 675: Invalid tls_options: No key_file field (Cluster::__listen_websocket(Cluster::options.0))
error in <...>/main.zeek, line 673: Invalid tls_options: No cert_file field (Cluster::__listen_websocket(Cluster::options.3)) error in <...>/main.zeek, line 675: Invalid tls_options: No cert_file field (Cluster::__listen_websocket(Cluster::options.3))

View file

@ -0,0 +1 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.

View file

@ -0,0 +1,12 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
Connected
Sending ping 0
topic /test/pings event name pong args [{'@data-type': 'string', 'data': 'my-message'}, {'@data-type': 'count', 'data': 1}]
Sending ping 1
topic /test/pings event name pong args [{'@data-type': 'string', 'data': 'my-message'}, {'@data-type': 'count', 'data': 2}]
Sending ping 2
topic /test/pings event name pong args [{'@data-type': 'string', 'data': 'my-message'}, {'@data-type': 'count', 'data': 3}]
Sending ping 3
topic /test/pings event name pong args [{'@data-type': 'string', 'data': 'my-message'}, {'@data-type': 'count', 'data': 4}]
Sending ping 4
topic /test/pings event name pong args [{'@data-type': 'string', 'data': 'my-message'}, {'@data-type': 'count', 'data': 5}]

View file

@ -0,0 +1,3 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
warning in ../manager.zeek, line 12: deprecated (Cluster::WebSocketServerOptions$listen_host): Remove in v8.1: Use $listen_addr instead. ((coerce [$listen_host=::1, $listen_port=to_port(getenv(WEBSOCKET_PORT))] to Cluster::WebSocketServerOptions))
received termination signal

View file

@ -0,0 +1,8 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
Cluster::websocket_client_added, [/test/pings, /zeek/wstest/ws1/]
got ping: ping 0, 0
got ping: ping 1, 1
got ping: ping 2, 2
got ping: ping 3, 3
got ping: ping 4, 4
Cluster::websocket_client_lost

View file

@ -0,0 +1 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.

View file

@ -0,0 +1,12 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
Connected
Sending ping 0
topic /test/pings event name pong args [{'@data-type': 'string', 'data': 'my-message'}, {'@data-type': 'count', 'data': 1}]
Sending ping 1
topic /test/pings event name pong args [{'@data-type': 'string', 'data': 'my-message'}, {'@data-type': 'count', 'data': 2}]
Sending ping 2
topic /test/pings event name pong args [{'@data-type': 'string', 'data': 'my-message'}, {'@data-type': 'count', 'data': 3}]
Sending ping 3
topic /test/pings event name pong args [{'@data-type': 'string', 'data': 'my-message'}, {'@data-type': 'count', 'data': 4}]
Sending ping 4
topic /test/pings event name pong args [{'@data-type': 'string', 'data': 'my-message'}, {'@data-type': 'count', 'data': 5}]

View file

@ -0,0 +1,2 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
received termination signal

View file

@ -0,0 +1,8 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
Cluster::websocket_client_added, [/test/pings, /zeek/wstest/ws1/]
got ping: ping 0, 0
got ping: ping 1, 1
got ping: ping 2, 2
got ping: ping 3, 3
got ping: ping 4, 4
Cluster::websocket_client_lost

View file

@ -1,3 +1,3 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63. ### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
error in <...>/tls-usage-error.zeek, line 17: Invalid tls_options: No key_file field (Cluster::listen_websocket((coerce [$listen_host=127.0.0.1, $listen_port=1234/tcp, $tls_options=tls_options_no_key] to Cluster::WebSocketServerOptions))) error in <...>/tls-usage-error.zeek, line 17: Invalid tls_options: No key_file field (Cluster::listen_websocket((coerce [$listen_addr=127.0.0.1, $listen_port=1234/tcp, $tls_options=tls_options_no_key] to Cluster::WebSocketServerOptions)))
error in <...>/tls-usage-error.zeek, line 18: Invalid tls_options: No cert_file field (Cluster::listen_websocket((coerce [$listen_host=127.0.0.1, $listen_port=1234/tcp, $tls_options=tls_options_no_cert] to Cluster::WebSocketServerOptions))) error in <...>/tls-usage-error.zeek, line 18: Invalid tls_options: No cert_file field (Cluster::listen_websocket((coerce [$listen_addr=127.0.0.1, $listen_port=1234/tcp, $tls_options=tls_options_no_cert] to Cluster::WebSocketServerOptions)))

View file

@ -43,6 +43,7 @@ WS_PORT = (
# IPv4 non-secure WebSocker URL for version 1 # IPv4 non-secure WebSocker URL for version 1
WS4_URL_V1 = f"ws://127.0.0.1:{WS_PORT}/v1/messages/json" WS4_URL_V1 = f"ws://127.0.0.1:{WS_PORT}/v1/messages/json"
WS6_URL_V1 = f"ws://[::1]:{WS_PORT}/v1/messages/json"
DEFAULT_RECV_TIMEOUT = 0.1 DEFAULT_RECV_TIMEOUT = 0.1
OWN_TOPIC_PREFIX = "/zeek/wstest" OWN_TOPIC_PREFIX = "/zeek/wstest"

View file

@ -35,7 +35,7 @@ global pong: event(msg: string, c: count) &is_used;
event zeek_init() event zeek_init()
{ {
Cluster::subscribe("/zeek/event/my_topic"); Cluster::subscribe("/zeek/event/my_topic");
Cluster::listen_websocket([$listen_host="127.0.0.1", $listen_port=to_port(getenv("WEBSOCKET_PORT"))]); Cluster::listen_websocket([$listen_addr=127.0.0.1, $listen_port=to_port(getenv("WEBSOCKET_PORT"))]);
} }
event ping(msg: string, n: count) &is_used event ping(msg: string, n: count) &is_used

View file

@ -35,7 +35,7 @@ global pong: event(msg: string, c: count) &is_used;
event zeek_init() event zeek_init()
{ {
Cluster::subscribe("/zeek/event/my_topic"); Cluster::subscribe("/zeek/event/my_topic");
Cluster::listen_websocket([$listen_host="127.0.0.1", $listen_port=to_port(getenv("WEBSOCKET_PORT"))]); Cluster::listen_websocket([$listen_addr=127.0.0.1, $listen_port=to_port(getenv("WEBSOCKET_PORT"))]);
} }
global added = 0; global added = 0;

View file

@ -37,7 +37,7 @@ global pong: event(msg: string, c: count) &is_used;
event zeek_init() event zeek_init()
{ {
Cluster::subscribe("/zeek/event/my_topic"); Cluster::subscribe("/zeek/event/my_topic");
Cluster::listen_websocket([$listen_host="127.0.0.1", $listen_port=to_port(getenv("WEBSOCKET_PORT"))]); Cluster::listen_websocket([$listen_addr=127.0.0.1, $listen_port=to_port(getenv("WEBSOCKET_PORT"))]);
} }
event ping(msg: string, n: count) &is_used event ping(msg: string, n: count) &is_used

View file

@ -42,7 +42,7 @@ event zeek_init()
Cluster::subscribe("/test/pings/"); Cluster::subscribe("/test/pings/");
Cluster::listen_websocket([ Cluster::listen_websocket([
$listen_host="127.0.0.1", $listen_addr=127.0.0.1,
$listen_port=to_port(getenv("WEBSOCKET_PORT")), $listen_port=to_port(getenv("WEBSOCKET_PORT")),
]); ]);
} }

View file

@ -40,7 +40,7 @@ global lost = 0;
event zeek_init() event zeek_init()
{ {
Cluster::listen_websocket([ Cluster::listen_websocket([
$listen_host="127.0.0.1", $listen_addr=127.0.0.1,
$listen_port=to_port(getenv("WEBSOCKET_PORT")), $listen_port=to_port(getenv("WEBSOCKET_PORT")),
]); ]);
} }

View file

@ -76,7 +76,7 @@ event Cluster::node_up(name: string, id: string)
# Delay listening on WebSocket clients until worker-1 is around. # Delay listening on WebSocket clients until worker-1 is around.
if ( name == "worker-1" ) if ( name == "worker-1" )
Cluster::listen_websocket([ Cluster::listen_websocket([
$listen_host="127.0.0.1", $listen_addr=127.0.0.1,
$listen_port=to_port(getenv("WEBSOCKET_PORT")) $listen_port=to_port(getenv("WEBSOCKET_PORT"))
]); ]);
} }

View file

@ -32,7 +32,7 @@ redef Broker::disable_ssl = T;
event zeek_init() event zeek_init()
{ {
Cluster::subscribe("/test/pings"); Cluster::subscribe("/test/pings");
Cluster::listen_websocket([$listen_host="127.0.0.1", $listen_port=to_port(getenv("WEBSOCKET_PORT"))]); Cluster::listen_websocket([$listen_addr=127.0.0.1, $listen_port=to_port(getenv("WEBSOCKET_PORT"))]);
} }
global ping_count = 0; global ping_count = 0;

View file

@ -36,7 +36,7 @@ global ping: event(msg: string, c: count) &is_used;
event zeek_init() event zeek_init()
{ {
Cluster::subscribe("/test/pings"); Cluster::subscribe("/test/pings");
Cluster::listen_websocket([$listen_host="127.0.0.1", $listen_port=to_port(getenv("WEBSOCKET_PORT"))]); Cluster::listen_websocket([$listen_addr=127.0.0.1, $listen_port=to_port(getenv("WEBSOCKET_PORT"))]);
} }
event ping(msg: string, n: count) &is_used event ping(msg: string, n: count) &is_used

View file

@ -43,7 +43,7 @@ global pong: event(msg: string, c: count) &is_used;
event zeek_init() event zeek_init()
{ {
Cluster::subscribe("/test/manager"); Cluster::subscribe("/test/manager");
Cluster::listen_websocket([$listen_host="127.0.0.1", $listen_port=to_port(getenv("WEBSOCKET_PORT"))]); Cluster::listen_websocket([$listen_addr=127.0.0.1, $listen_port=to_port(getenv("WEBSOCKET_PORT"))]);
} }
event ping(msg: string, n: count) &is_used event ping(msg: string, n: count) &is_used

View file

@ -20,20 +20,20 @@ event zeek_init()
local ws_port = to_port(getenv("WEBSOCKET_PORT")); local ws_port = to_port(getenv("WEBSOCKET_PORT"));
local wss_port = to_port(getenv("WEBSOCKET_SECURE_PORT")); local wss_port = to_port(getenv("WEBSOCKET_SECURE_PORT"));
local ws_opts = Cluster::WebSocketServerOptions($listen_host="127.0.0.1", $listen_port=ws_port); local ws_opts = Cluster::WebSocketServerOptions($listen_addr=127.0.0.1, $listen_port=ws_port);
local ws_opts_x = copy(ws_opts); local ws_opts_x = copy(ws_opts);
ws_opts_x$tls_options = tls_options; ws_opts_x$tls_options = tls_options;
local ws_opts_wss_port = Cluster::WebSocketServerOptions($listen_host="127.0.0.1", $listen_port=wss_port); local ws_opts_wss_port = Cluster::WebSocketServerOptions($listen_addr=127.0.0.1, $listen_port=wss_port);
local ws_tls_opts = Cluster::WebSocketServerOptions( local ws_tls_opts = Cluster::WebSocketServerOptions(
$listen_host="127.0.0.1", $listen_addr=127.0.0.1,
$listen_port=wss_port, $listen_port=wss_port,
$tls_options=tls_options, $tls_options=tls_options,
); );
# Same as ws_tls_opts # Same as ws_tls_opts
local ws_tls_opts_copy = Cluster::WebSocketServerOptions( local ws_tls_opts_copy = Cluster::WebSocketServerOptions(
$listen_host="127.0.0.1", $listen_addr=127.0.0.1,
$listen_port=wss_port, $listen_port=wss_port,
$tls_options=tls_options_2, $tls_options=tls_options_2,
); );

View file

@ -0,0 +1,85 @@
# @TEST-DOC: Use listen_host to listen on an IPv6 address, otherwise same as one-ipv6.zeek
#
# @TEST-REQUIRES: have-zeromq
# @TEST-REQUIRES: python3 -c 'import websockets.sync'
# @TEST-REQUIRES: can-listen-tcp 6 ::1
#
# @TEST-GROUP: cluster-zeromq
#
# @TEST-PORT: XPUB_PORT
# @TEST-PORT: XSUB_PORT
# @TEST-PORT: LOG_PULL_PORT
# @TEST-PORT: WEBSOCKET_PORT
#
# @TEST-EXEC: cp $FILES/zeromq/cluster-layout-simple.zeek cluster-layout.zeek
# @TEST-EXEC: cp $FILES/zeromq/test-bootstrap.zeek zeromq-test-bootstrap.zeek
# @TEST-EXEC: cp $FILES/ws/wstest.py .
#
# @TEST-EXEC: zeek -b --parse-only manager.zeek
# @TEST-EXEC: python3 -m py_compile client.py
#
# @TEST-EXEC: btest-bg-run manager "ZEEKPATH=$ZEEKPATH:.. && CLUSTER_NODE=manager zeek -b ../manager.zeek >out"
# @TEST-EXEC: btest-bg-run client "python3 ../client.py >out"
#
# @TEST-EXEC: btest-bg-wait 30
# @TEST-EXEC: btest-diff ./manager/out
# @TEST-EXEC: btest-diff ./manager/.stderr
# @TEST-EXEC: btest-diff ./client/out
# @TEST-EXEC: btest-diff ./client/.stderr
# @TEST-START-FILE manager.zeek
@load ./zeromq-test-bootstrap
redef exit_only_after_terminate = T;
global ping_count = 0;
global ping: event(msg: string, c: count) &is_used;
global pong: event(msg: string, c: count) &is_used;
event zeek_init()
{
Cluster::subscribe("/test/pings/");
Cluster::listen_websocket([$listen_host="::1", $listen_port=to_port(getenv("WEBSOCKET_PORT"))]);
}
event ping(msg: string, n: count) &is_used
{
++ping_count;
print fmt("got ping: %s, %s", msg, n);
local e = Cluster::make_event(pong, "my-message", ping_count);
Cluster::publish("/test/pings", e);
}
event Cluster::websocket_client_added(info: Cluster::EndpointInfo, subscriptions: string_vec)
{
print "Cluster::websocket_client_added", subscriptions;
}
event Cluster::websocket_client_lost(info: Cluster::EndpointInfo, code: count, reason: string)
{
print "Cluster::websocket_client_lost";
terminate();
}
# @TEST-END-FILE
# @TEST-START-FILE client.py
# @TEST-START-FILE client.py
import wstest
def run(ws_url):
with wstest.connect("ws1", ws_url) as tc:
print("Connected")
tc.hello_v1(["/test/pings"])
for i in range(5):
print("Sending ping", i)
tc.send_json(wstest.build_event_v1("/test/pings/", "ping", [f"ping {i}", i]))
pong = tc.recv_json()
assert pong["@data-type"] == "vector"
ev = pong["data"][2]["data"]
print("topic", pong["topic"], "event name", ev[0]["data"], "args", ev[1]["data"])
if __name__ == "__main__":
wstest.main(run, wstest.WS6_URL_V1)
# @TEST-END-FILE

View file

@ -0,0 +1,85 @@
# @TEST-DOC: Make a WebSocket server listen on IPv6 ::1.
#
# @TEST-REQUIRES: have-zeromq
# @TEST-REQUIRES: python3 -c 'import websockets.sync'
# @TEST-REQUIRES: can-listen-tcp 6 ::1
#
# @TEST-GROUP: cluster-zeromq
#
# @TEST-PORT: XPUB_PORT
# @TEST-PORT: XSUB_PORT
# @TEST-PORT: LOG_PULL_PORT
# @TEST-PORT: WEBSOCKET_PORT
#
# @TEST-EXEC: cp $FILES/zeromq/cluster-layout-simple.zeek cluster-layout.zeek
# @TEST-EXEC: cp $FILES/zeromq/test-bootstrap.zeek zeromq-test-bootstrap.zeek
# @TEST-EXEC: cp $FILES/ws/wstest.py .
#
# @TEST-EXEC: zeek -b --parse-only manager.zeek
# @TEST-EXEC: python3 -m py_compile client.py
#
# @TEST-EXEC: btest-bg-run manager "ZEEKPATH=$ZEEKPATH:.. && CLUSTER_NODE=manager zeek -b ../manager.zeek >out"
# @TEST-EXEC: btest-bg-run client "python3 ../client.py >out"
#
# @TEST-EXEC: btest-bg-wait 30
# @TEST-EXEC: btest-diff ./manager/out
# @TEST-EXEC: btest-diff ./manager/.stderr
# @TEST-EXEC: btest-diff ./client/out
# @TEST-EXEC: btest-diff ./client/.stderr
# @TEST-START-FILE manager.zeek
@load ./zeromq-test-bootstrap
redef exit_only_after_terminate = T;
global ping_count = 0;
global ping: event(msg: string, c: count) &is_used;
global pong: event(msg: string, c: count) &is_used;
event zeek_init()
{
Cluster::subscribe("/test/pings/");
Cluster::listen_websocket([$listen_addr=[::1], $listen_port=to_port(getenv("WEBSOCKET_PORT"))]);
}
event ping(msg: string, n: count) &is_used
{
++ping_count;
print fmt("got ping: %s, %s", msg, n);
local e = Cluster::make_event(pong, "my-message", ping_count);
Cluster::publish("/test/pings", e);
}
event Cluster::websocket_client_added(info: Cluster::EndpointInfo, subscriptions: string_vec)
{
print "Cluster::websocket_client_added", subscriptions;
}
event Cluster::websocket_client_lost(info: Cluster::EndpointInfo, code: count, reason: string)
{
print "Cluster::websocket_client_lost";
terminate();
}
# @TEST-END-FILE
# @TEST-START-FILE client.py
# @TEST-START-FILE client.py
import wstest
def run(ws_url):
with wstest.connect("ws1", ws_url) as tc:
print("Connected")
tc.hello_v1(["/test/pings"])
for i in range(5):
print("Sending ping", i)
tc.send_json(wstest.build_event_v1("/test/pings/", "ping", [f"ping {i}", i]))
pong = tc.recv_json()
assert pong["@data-type"] == "vector"
ev = pong["data"][2]["data"]
print("topic", pong["topic"], "event name", ev[0]["data"], "args", ev[1]["data"])
if __name__ == "__main__":
wstest.main(run, wstest.WS6_URL_V1)
# @TEST-END-FILE

View file

@ -37,7 +37,7 @@ global pong: event(msg: string, c: count) &is_used;
event zeek_init() event zeek_init()
{ {
Cluster::subscribe("/zeek/event/my_topic"); Cluster::subscribe("/zeek/event/my_topic");
Cluster::listen_websocket([$listen_host="127.0.0.1", $listen_port=to_port(getenv("WEBSOCKET_PORT"))]); Cluster::listen_websocket([$listen_addr=127.0.0.1, $listen_port=to_port(getenv("WEBSOCKET_PORT"))]);
} }
event ping(msg: string, n: count) &is_used event ping(msg: string, n: count) &is_used

View file

@ -37,7 +37,7 @@ global pong: event(msg: string, c: count) &is_used;
event zeek_init() event zeek_init()
{ {
Cluster::subscribe("/zeek/event/my_topic"); Cluster::subscribe("/zeek/event/my_topic");
Cluster::listen_websocket([$listen_host="127.0.0.1", $listen_port=to_port(getenv("WEBSOCKET_PORT"))]); Cluster::listen_websocket([$listen_addr=127.0.0.1, $listen_port=to_port(getenv("WEBSOCKET_PORT"))]);
} }
event ping(msg: string, n: count) &is_used event ping(msg: string, n: count) &is_used

View file

@ -39,7 +39,7 @@ event Cluster::websocket_client_lost(info: Cluster::EndpointInfo, code: count, r
event zeek_init() event zeek_init()
{ {
Cluster::listen_websocket([$listen_host="127.0.0.1", $listen_port=to_port(getenv("WEBSOCKET_PORT")), $ping_interval=1sec]); Cluster::listen_websocket([$listen_addr=127.0.0.1, $listen_port=to_port(getenv("WEBSOCKET_PORT")), $ping_interval=1sec]);
Cluster::subscribe("/test/pings/"); Cluster::subscribe("/test/pings/");
} }
# @TEST-END-FILE # @TEST-END-FILE

View file

@ -53,7 +53,7 @@ event Cluster::websocket_client_lost(info: Cluster::EndpointInfo, code: count, r
event zeek_init() event zeek_init()
{ {
Cluster::listen_websocket([$listen_host="127.0.0.1", $listen_port=to_port(getenv("WEBSOCKET_PORT"))]); Cluster::listen_websocket([$listen_addr=127.0.0.1, $listen_port=to_port(getenv("WEBSOCKET_PORT"))]);
Cluster::subscribe("/test/pings/"); Cluster::subscribe("/test/pings/");
} }
# @TEST-END-FILE # @TEST-END-FILE

View file

@ -71,7 +71,7 @@ event Cluster::Backend::ZeroMQ::subscription(topic: string)
event zeek_init() event zeek_init()
{ {
Cluster::listen_websocket([$listen_host="127.0.0.1", $listen_port=to_port(getenv("WEBSOCKET_PORT"))]); Cluster::listen_websocket([$listen_addr=127.0.0.1, $listen_port=to_port(getenv("WEBSOCKET_PORT"))]);
Cluster::subscribe("/test/manager"); Cluster::subscribe("/test/manager");
} }
# @TEST-END-FILE # @TEST-END-FILE

View file

@ -14,6 +14,6 @@ event zeek_init()
$key_file="../localhost.key", $key_file="../localhost.key",
); );
assert ! Cluster::listen_websocket([$listen_host="127.0.0.1", $listen_port=1234/tcp, $tls_options=tls_options_no_key]); assert ! Cluster::listen_websocket([$listen_addr=127.0.0.1, $listen_port=1234/tcp, $tls_options=tls_options_no_key]);
assert ! Cluster::listen_websocket([$listen_host="127.0.0.1", $listen_port=1234/tcp, $tls_options=tls_options_no_cert]); assert ! Cluster::listen_websocket([$listen_addr=127.0.0.1, $listen_port=1234/tcp, $tls_options=tls_options_no_cert]);
} }

View file

@ -46,7 +46,7 @@ event zeek_init()
); );
local ws_server_options = Cluster::WebSocketServerOptions( local ws_server_options = Cluster::WebSocketServerOptions(
$listen_host="127.0.0.1", $listen_addr=127.0.0.1,
$listen_port=to_port(getenv("WEBSOCKET_PORT")), $listen_port=to_port(getenv("WEBSOCKET_PORT")),
$tls_options=tls_options, $tls_options=tls_options,
); );

View file

@ -103,7 +103,7 @@ event Cluster::Backend::ZeroMQ::subscription(topic: string)
event zeek_init() event zeek_init()
{ {
Cluster::listen_websocket([$listen_host="127.0.0.1", $listen_port=to_port(getenv("WEBSOCKET_PORT"))]); Cluster::listen_websocket([$listen_addr=127.0.0.1, $listen_port=to_port(getenv("WEBSOCKET_PORT"))]);
Cluster::subscribe("/test/manager"); Cluster::subscribe("/test/manager");
} }
# @TEST-END-FILE # @TEST-END-FILE

View file

@ -79,7 +79,7 @@ event Cluster::node_up(name: string, id: string)
# Delay listening on WebSocket clients until worker-1 is around. # Delay listening on WebSocket clients until worker-1 is around.
if ( name == "worker-1" ) if ( name == "worker-1" )
Cluster::listen_websocket([ Cluster::listen_websocket([
$listen_host="127.0.0.1", $listen_addr=127.0.0.1,
$listen_port=to_port(getenv("WEBSOCKET_PORT")) $listen_port=to_port(getenv("WEBSOCKET_PORT"))
]); ]);
} }

View file

@ -33,7 +33,7 @@ redef exit_only_after_terminate = T;
event zeek_init() event zeek_init()
{ {
Cluster::subscribe("/test/pings"); Cluster::subscribe("/test/pings");
Cluster::listen_websocket([$listen_host="127.0.0.1", $listen_port=to_port(getenv("WEBSOCKET_PORT"))]); Cluster::listen_websocket([$listen_addr=127.0.0.1, $listen_port=to_port(getenv("WEBSOCKET_PORT"))]);
} }
global ping_count = 0; global ping_count = 0;

View file

@ -38,7 +38,7 @@ global ping: event(msg: string, c: count) &is_used;
event zeek_init() event zeek_init()
{ {
Cluster::subscribe("/test/pings"); Cluster::subscribe("/test/pings");
Cluster::listen_websocket([$listen_host="127.0.0.1", $listen_port=to_port(getenv("WEBSOCKET_PORT"))]); Cluster::listen_websocket([$listen_addr=127.0.0.1, $listen_port=to_port(getenv("WEBSOCKET_PORT"))]);
} }
event ping(msg: string, n: count) &is_used event ping(msg: string, n: count) &is_used

41
testing/scripts/can-listen-tcp Executable file
View file

@ -0,0 +1,41 @@
#!/usr/bin/env python3
#
# Try binding on the given family with addr and a port of 0 so that an
# ephemeral port is allocated. This can be used to guard IPv6 tests.
import socket
import sys
def usage():
print(f"Usage: {sys.argv[0]} 4|6 <addr>", file=sys.stderr)
sys.exit(1)
if len(sys.argv) != 3:
usage()
family = 0
if sys.argv[1] == "4":
family = socket.AF_INET
elif sys.argv[1] == "6":
family = socket.AF_INET6
else:
usage()
addr = sys.argv[2]
port = 0
s = socket.socket(family)
try:
s.bind((addr, port))
except Exception as e:
print(f"cannot listen on {addr}:{port} ({e!r})")
sys.exit(1)
finally:
try:
s.close()
except:
pass