@@ -46,6 +46,44 @@ static char client_secret [41];
4646static char server_public [41 ];
4747static char server_secret [41 ];
4848
49+ #ifdef ZMQ_BUILD_DRAFT_API
50+ // Read one event off the monitor socket; return value and address
51+ // by reference, if not null, and event number by value. Returns -1
52+ // in case of error.
53+
54+ static int
55+ get_monitor_event (void *monitor, int *value, char **address)
56+ {
57+ // First frame in message contains event number and value
58+ zmq_msg_t msg;
59+ zmq_msg_init (&msg);
60+ if (zmq_msg_recv (&msg, monitor, 0 ) == -1 )
61+ return -1 ; // Interruped, presumably
62+ assert (zmq_msg_more (&msg));
63+
64+ uint8_t *data = (uint8_t *) zmq_msg_data (&msg);
65+ uint16_t event = *(uint16_t *) (data);
66+ if (value)
67+ *value = *(uint32_t *) (data + 2 );
68+
69+ // Second frame in message contains event address
70+ zmq_msg_init (&msg);
71+ if (zmq_msg_recv (&msg, monitor, 0 ) == -1 )
72+ return -1 ; // Interruped, presumably
73+ assert (!zmq_msg_more (&msg));
74+
75+ if (address) {
76+ uint8_t *data = (uint8_t *) zmq_msg_data (&msg);
77+ size_t size = zmq_msg_size (&msg);
78+ *address = (char *) malloc (size + 1 );
79+ memcpy (*address, data, size);
80+ *address [size] = 0 ;
81+ }
82+ return event;
83+ }
84+ #endif
85+
86+
4987// --------------------------------------------------------------------------
5088// This methods receives and validates ZAP requestes (allowing or denying
5189// each client connection).
@@ -138,6 +176,21 @@ int main (void)
138176 rc = zmq_bind (server, " tcp://127.0.0.1:9998" );
139177 assert (rc == 0 );
140178
179+ #ifdef ZMQ_BUILD_DRAFT_API
180+ // Monitor handshake events on the server
181+ rc = zmq_socket_monitor (server, " inproc://monitor-server" ,
182+ ZMQ_EVENT_HANDSHAKE_SUCCEED | ZMQ_EVENT_HANDSHAKE_FAILED);
183+ assert (rc == 0 );
184+
185+ // Create socket for collecting monitor events
186+ void *server_mon = zmq_socket (ctx, ZMQ_PAIR);
187+ assert (server_mon);
188+
189+ // Connect it to the inproc endpoints so they'll get events
190+ rc = zmq_connect (server_mon, " inproc://monitor-server" );
191+ assert (rc == 0 );
192+ #endif
193+
141194 // Check CURVE security with valid credentials
142195 void *client = zmq_socket (ctx, ZMQ_DEALER);
143196 assert (client);
@@ -153,6 +206,11 @@ int main (void)
153206 rc = zmq_close (client);
154207 assert (rc == 0 );
155208
209+ #ifdef ZMQ_BUILD_DRAFT_API
210+ int event = get_monitor_event (server_mon, NULL , NULL );
211+ assert (event == ZMQ_EVENT_HANDSHAKE_SUCCEED);
212+ #endif
213+
156214 // Check CURVE security with a garbage server key
157215 // This will be caught by the curve_server class, not passed to ZAP
158216 char garbage_key [] = " 0000000000000000000000000000000000000000" ;
@@ -169,6 +227,11 @@ int main (void)
169227 expect_bounce_fail (server, client);
170228 close_zero_linger (client);
171229
230+ #ifdef ZMQ_BUILD_DRAFT_API
231+ event = get_monitor_event (server_mon, NULL , NULL );
232+ assert (event == ZMQ_EVENT_HANDSHAKE_FAILED);
233+ #endif
234+
172235 // Check CURVE security with a garbage client public key
173236 // This will be caught by the curve_server class, not passed to ZAP
174237 client = zmq_socket (ctx, ZMQ_DEALER);
@@ -184,6 +247,11 @@ int main (void)
184247 expect_bounce_fail (server, client);
185248 close_zero_linger (client);
186249
250+ #ifdef ZMQ_BUILD_DRAFT_API
251+ event = get_monitor_event (server_mon, NULL , NULL );
252+ assert (event == ZMQ_EVENT_HANDSHAKE_FAILED);
253+ #endif
254+
187255 // Check CURVE security with a garbage client secret key
188256 // This will be caught by the curve_server class, not passed to ZAP
189257 client = zmq_socket (ctx, ZMQ_DEALER);
@@ -199,6 +267,11 @@ int main (void)
199267 expect_bounce_fail (server, client);
200268 close_zero_linger (client);
201269
270+ #ifdef ZMQ_BUILD_DRAFT_API
271+ event = get_monitor_event (server_mon, NULL , NULL );
272+ assert (event == ZMQ_EVENT_HANDSHAKE_FAILED);
273+ #endif
274+
202275 // Check CURVE security with bogus client credentials
203276 // This must be caught by the ZAP handler
204277 char bogus_public [41 ];
@@ -218,6 +291,11 @@ int main (void)
218291 expect_bounce_fail (server, client);
219292 close_zero_linger (client);
220293
294+ #ifdef ZMQ_BUILD_DRAFT_API
295+ event = get_monitor_event (server_mon, NULL , NULL );
296+ assert (event == ZMQ_EVENT_HANDSHAKE_FAILED);
297+ #endif
298+
221299 // Check CURVE security with NULL client credentials
222300 // This must be caught by the curve_server class, not passed to ZAP
223301 client = zmq_socket (ctx, ZMQ_DEALER);
@@ -227,6 +305,11 @@ int main (void)
227305 expect_bounce_fail (server, client);
228306 close_zero_linger (client);
229307
308+ #ifdef ZMQ_BUILD_DRAFT_API
309+ event = get_monitor_event (server_mon, NULL , NULL );
310+ assert (event == ZMQ_EVENT_HANDSHAKE_FAILED);
311+ #endif
312+
230313 // Check CURVE security with PLAIN client credentials
231314 // This must be caught by the curve_server class, not passed to ZAP
232315 client = zmq_socket (ctx, ZMQ_DEALER);
@@ -282,6 +365,9 @@ int main (void)
282365 assert (rc == 0 );
283366
284367 // Shutdown
368+ #ifdef ZMQ_BUILD_DRAFT_API
369+ close_zero_linger (server_mon);
370+ #endif
285371 rc = zmq_close (server);
286372 assert (rc == 0 );
287373 rc = zmq_ctx_term (ctx);
0 commit comments