|
| 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 | +} |
0 commit comments