Skip to content

Commit 4e18f29

Browse files
authored
Merge pull request #1580 from dertom95/master
Fixed examples to work with CZMQ API v3
2 parents f25fabf + 44b70ea commit 4e18f29

File tree

7 files changed

+220
-78
lines changed

7 files changed

+220
-78
lines changed

examples/security/grasslands.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
// The Grasslands Pattern
22
//
33
// The Classic ZeroMQ model, plain text with no protection at all.
4+
//
5+
// CZMQ APIv3
6+
//
7+
// More info: http://hintjens.com/blog:49#toc2
8+
//
49

510
#include <czmq.h>
611

712
int main (void)
813
{
9-
// Create context
10-
zctx_t *ctx = zctx_new ();
11-
1214
// Create and bind server socket
13-
void *server = zsocket_new (ctx, ZMQ_PUSH);
14-
zsocket_bind (server, "tcp://*:9000");
15+
zsock_t *server = zsock_new (ZMQ_PUSH);
16+
zsock_bind (server, "tcp://*:9000");
1517

1618
// Create and connect client socket
17-
void *client = zsocket_new (ctx, ZMQ_PULL);
18-
zsocket_connect (client, "tcp://127.0.0.1:9000");
19+
zsock_t *client = zsock_new (ZMQ_PULL);
20+
zsock_connect (client, "tcp://127.0.0.1:9000");
1921

2022
// Send a single message from server to client
2123
zstr_send (server, "Hello");
@@ -24,6 +26,7 @@ int main (void)
2426
free (message);
2527
puts ("Grasslands test OK");
2628

27-
zctx_destroy (&ctx);
29+
zsock_destroy (&client);
30+
zsock_destroy (&server);
2831
return 0;
2932
}

examples/security/hello.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
//
2+
// CZMQ APIv3
3+
//
4+
15
#include <czmq.h>
26

37
int main (void) {
4-
zctx_t *ctx = zctx_new ();
5-
void *publisher = zsocket_new (ctx, ZMQ_PUB);
6-
zsocket_set_curve_server (publisher, true);
8+
zsock_t *publisher = zsock_new (ZMQ_PUB);
9+
zsock_set_curve_server (publisher, true);
710
puts ("Hello, Curve!");
8-
zctx_destroy (&ctx);
11+
zsock_destroy(&publisher);
912
return 0;
1013
}

examples/security/ironhouse.c

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,27 @@
22
//
33
// Security doesn't get any stronger than this. An attacker is going to
44
// have to break into your systems to see data before/after encryption.
5+
//
6+
// CZMQ APIv3
7+
//
8+
//
9+
// For more info see: http://hintjens.com/blog:49#toc6
10+
//
511

612
#include <czmq.h>
713

814
int main (void)
915
{
10-
// Create context and start authentication engine
11-
zctx_t *ctx = zctx_new ();
12-
zauth_t *auth = zauth_new (ctx);
13-
zauth_set_verbose (auth, true);
14-
zauth_allow (auth, "127.0.0.1");
15-
16-
// Tell authenticator to use the certificate store in .curve
17-
zauth_configure_curve (auth, "*", ".curve");
18-
16+
// Create and start authentication engine
17+
zactor_t *auth = zactor_new (zauth,NULL);
18+
zstr_send(auth,"VERBOSE");
19+
zsock_wait(auth);
20+
zstr_sendx(auth,"ALLOW","127.0.0.1",NULL);
21+
zsock_wait(auth);
22+
23+
// Tell the authenticator to use the certificate store in .curve
24+
zstr_sendx (auth,"CURVE",".curve",NULL);
25+
1926
// We'll generate a new client certificate and save the public part
2027
// in the certificate store (in practice this would be done by hand
2128
// or some out-of-band process).
@@ -26,19 +33,19 @@ int main (void)
2633

2734
// Prepare the server certificate as we did in Stonehouse
2835
zcert_t *server_cert = zcert_new ();
29-
char *server_key = zcert_public_txt (server_cert);
36+
const char *server_key = zcert_public_txt (server_cert);
3037

3138
// Create and bind server socket
32-
void *server = zsocket_new (ctx, ZMQ_PUSH);
39+
zsock_t *server = zsock_new (ZMQ_PUSH);
3340
zcert_apply (server_cert, server);
34-
zsocket_set_curve_server (server, 1);
35-
zsocket_bind (server, "tcp://*:9000");
41+
zsock_set_curve_server (server, 1);
42+
zsock_bind (server, "tcp://*:9000");
3643

3744
// Create and connect client socket
38-
void *client = zsocket_new (ctx, ZMQ_PULL);
45+
zsock_t *client = zsock_new (ZMQ_PULL);
3946
zcert_apply (client_cert, client);
40-
zsocket_set_curve_serverkey (client, server_key);
41-
zsocket_connect (client, "tcp://127.0.0.1:9000");
47+
zsock_set_curve_serverkey (client, server_key);
48+
zsock_connect (client, "tcp://127.0.0.1:9000");
4249

4350
// Send a single message from server to client
4451
zstr_send (server, "Hello");
@@ -49,7 +56,9 @@ int main (void)
4956

5057
zcert_destroy (&client_cert);
5158
zcert_destroy (&server_cert);
52-
zauth_destroy (&auth);
53-
zctx_destroy (&ctx);
59+
zactor_destroy (&auth);
60+
zsock_destroy (&client);
61+
zsock_destroy (&server);
62+
5463
return 0;
5564
}

examples/security/ironhouse2_v2.c

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// The Ironhouse Pattern
2+
//
3+
// This is exactly the same example but broken into two threads
4+
// so you can better see what client and server do, separately.
5+
//
6+
// WARNING: CZMQ APIv2 will not compile with the current version of CZMQ
7+
//
8+
//
9+
// For more info see: http://hintjens.com/blog:49#toc7
10+
//
11+
12+
#include <czmq.h>
13+
14+
// The client task runs in its own context, and receives the
15+
// server public key as an argument.
16+
17+
static void *
18+
client_task (void *args)
19+
{
20+
// Load our persistent certificate from disk
21+
zcert_t *client_cert = zcert_load ("client_cert.txt");
22+
assert (client_cert);
23+
24+
// Create client socket and configure it to use full encryption
25+
zctx_t *ctx = zctx_new ();
26+
assert (ctx);
27+
void *client = zsocket_new (ctx, ZMQ_PULL);
28+
assert (client);
29+
zcert_apply (client_cert, client);
30+
zsocket_set_curve_serverkey (client, (char *) args);
31+
int rc = zsocket_connect (client, "tcp://127.0.0.1:9000");
32+
assert (rc == 0);
33+
34+
// Wait for our message, that signals the test was successful
35+
char *message = zstr_recv (client);
36+
assert (streq (message, "Hello"));
37+
free (message);
38+
puts ("Ironhouse test OK");
39+
40+
// Free all memory we used
41+
zcert_destroy (&client_cert);
42+
zctx_destroy (&ctx);
43+
return NULL;
44+
}
45+
46+
static void *
47+
server_task (void *args)
48+
{
49+
zctx_t *ctx = zctx_new ();
50+
assert (ctx);
51+
52+
// Start the authenticator and tell it do authenticate clients
53+
// via the certificates stored in the .curve directory.
54+
zauth_t *auth = zauth_new (ctx);
55+
assert (auth);
56+
zauth_set_verbose (auth, true);
57+
zauth_allow (auth, "127.0.0.1");
58+
zauth_configure_curve (auth, "*", ".curve");
59+
60+
// Create server socket and configure it to use full encryption
61+
void *server = zsocket_new (ctx, ZMQ_PUSH);
62+
assert (server);
63+
zcert_apply ((zcert_t *) args, server);
64+
zsocket_set_curve_server (server, 1);
65+
int rc = zsocket_bind (server, "tcp://*:9000");
66+
assert (rc != -1);
67+
68+
// Send our test message, just once
69+
zstr_send (server, "Hello");
70+
zclock_sleep (200);
71+
72+
// Free all memory we used
73+
zauth_destroy (&auth);
74+
zctx_destroy (&ctx);
75+
return NULL;
76+
}
77+
78+
int main (void)
79+
{
80+
// Create the certificate store directory and client certs
81+
zcert_t *client_cert = zcert_new ();
82+
int rc = zsys_dir_create (".curve");
83+
assert (rc == 0);
84+
zcert_set_meta (client_cert, "name", "Client test certificate");
85+
zcert_save_public (client_cert, ".curve/testcert.pub");
86+
rc = zcert_save (client_cert, "client_cert.txt");
87+
assert (rc == 0);
88+
zcert_destroy (&client_cert);
89+
90+
// Create the server certificate
91+
zcert_t *server_cert = zcert_new ();
92+
93+
// Now start the two detached threads; each of these has their
94+
// own ZeroMQ context.
95+
zthread_new (server_task, server_cert);
96+
zthread_new (client_task, zcert_public_txt (server_cert));
97+
98+
// As we're using detached threads this is the simplest way
99+
// to ensure they both end, before we exit the process. 200
100+
// milliseconds should be enough for anyone. In real code,
101+
// you would use messages to synchronize threads.
102+
zclock_sleep (200);
103+
104+
// Free the memory we used
105+
zcert_destroy (&server_cert);
106+
return 0;
107+
}

examples/security/stonehouse.c

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,45 @@
33
// Where we allow any clients to connect, but we promise clients
44
// that we are who we claim to be, and our conversations won't be
55
// tampered with or modified, or spied on.
6+
//
7+
// CZMQ APIv3
8+
//
9+
//
10+
// For more info see: http://hintjens.com/blog:49#toc5
11+
//
612

713
#include <czmq.h>
814

915
int main (void)
1016
{
11-
// Create context and start authentication engine
12-
zctx_t *ctx = zctx_new ();
13-
zauth_t *auth = zauth_new (ctx);
14-
zauth_set_verbose (auth, true);
15-
zauth_allow (auth, "127.0.0.1");
16-
17+
// Create and start authentication engine
18+
zactor_t *auth = zactor_new (zauth,NULL);
19+
zstr_send(auth,"VERBOSE");
20+
zsock_wait(auth);
21+
zstr_sendx(auth,"ALLOW","127.0.0.1",NULL);
22+
zsock_wait(auth);
23+
1724
// Tell the authenticator how to handle CURVE requests
18-
zauth_configure_curve (auth, "*", CURVE_ALLOW_ANY);
25+
zstr_sendx (auth,"CURVE",CURVE_ALLOW_ANY,NULL);
1926

2027
// We need two certificates, one for the client and one for
2128
// the server. The client must know the server's public key
2229
// to make a CURVE connection.
2330
zcert_t *client_cert = zcert_new ();
2431
zcert_t *server_cert = zcert_new ();
25-
char *server_key = zcert_public_txt (server_cert);
32+
const char *server_key = zcert_public_txt (server_cert);
2633

2734
// Create and bind server socket
28-
void *server = zsocket_new (ctx, ZMQ_PUSH);
35+
zsock_t *server = zsock_new (ZMQ_PUSH);
2936
zcert_apply (server_cert, server);
30-
zsocket_set_curve_server (server, 1);
31-
zsocket_bind (server, "tcp://*:9000");
37+
zsock_set_curve_server (server, 1);
38+
zsock_bind (server, "tcp://*:9000");
3239

3340
// Create and connect client socket
34-
void *client = zsocket_new (ctx, ZMQ_PULL);
41+
zsock_t *client = zsock_new (ZMQ_PULL);
3542
zcert_apply (client_cert, client);
36-
zsocket_set_curve_serverkey (client, server_key);
37-
zsocket_connect (client, "tcp://127.0.0.1:9000");
43+
zsock_set_curve_serverkey (client, server_key);
44+
zsock_connect (client, "tcp://127.0.0.1:9000");
3845

3946
// Send a single message from server to client
4047
zstr_send (server, "Hello");
@@ -45,7 +52,8 @@ int main (void)
4552

4653
zcert_destroy (&client_cert);
4754
zcert_destroy (&server_cert);
48-
zauth_destroy (&auth);
49-
zctx_destroy (&ctx);
55+
zactor_destroy (&auth);
56+
zsock_destroy (&client);
57+
zsock_destroy (&server);
5058
return 0;
5159
}

examples/security/strawhouse.c

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,37 @@
33
// We allow or deny clients according to their IP address. It may keep
44
// spammers and idiots away, but won't stop a real attacker for more
55
// than a heartbeat.
6-
6+
//
7+
// CZMQ APIv3
8+
//
9+
// For more info see: http://hintjens.com/blog:49#toc3
10+
//
711
#include <czmq.h>
812

913
int main (void)
1014
{
11-
// Create context
12-
zctx_t *ctx = zctx_new ();
13-
1415
// Start an authentication engine for this context. This engine
1516
// allows or denies incoming connections (talking to the libzmq
1617
// core over a protocol called ZAP).
17-
zauth_t *auth = zauth_new (ctx);
18+
zactor_t *auth = zactor_new(zauth,NULL);
1819

1920
// Get some indication of what the authenticator is deciding
20-
zauth_set_verbose (auth, true);
21+
zstr_send (auth,"VERBOSE");
22+
zsock_wait (auth);
2123

2224
// Whitelist our address; any other address will be rejected
23-
zauth_allow (auth, "127.0.0.1");
24-
25+
// Add as much address as argument as you like before the NULL-Argument
26+
// e.g. zstr_sendx (auth,"127.0.0.1","192.168.1.20",NULL);
27+
zstr_sendx (auth,"PLAIN","127.0.0.1",NULL);
28+
zsock_wait (auth);
29+
2530
// Create and bind server socket
26-
void *server = zsocket_new (ctx, ZMQ_PUSH);
27-
zsocket_set_zap_domain (server, "global");
28-
zsocket_bind (server, "tcp://*:9000");
31+
zsock_t *server = zsock_new (ZMQ_PUSH);
32+
zsock_bind (server, "tcp://*:9000");
2933

3034
// Create and connect client socket
31-
void *client = zsocket_new (ctx, ZMQ_PULL);
32-
zsocket_connect (client, "tcp://127.0.0.1:9000");
35+
zsock_t *client = zsock_new (ZMQ_PULL);
36+
zsock_connect (client, "tcp://127.0.0.1:9000");
3337

3438
// Send a single message from server to client
3539
zstr_send (server, "Hello");
@@ -38,7 +42,8 @@ int main (void)
3842
free (message);
3943
puts ("Strawhouse test OK");
4044

41-
zauth_destroy (&auth);
42-
zctx_destroy (&ctx);
45+
zactor_destroy (&auth);
46+
zsock_destroy (&client);
47+
zsock_destroy (&server);
4348
return 0;
4449
}

0 commit comments

Comments
 (0)