static int client_set_metadata (const char *key, void *item, void *arg) { client_t *self = (client_t *) arg; curve_codec_set_metadata (self->codec, (char *) key, (char *) item); return 0; }
static int s_agent_handle_control (agent_t *self) { // Get the whole message off the control socket in one go zmsg_t *request = zmsg_recv (self->control); char *command = zmsg_popstr (request); if (!command) return -1; // Interrupted if (streq (command, "SET")) { char *name = zmsg_popstr (request); char *value = zmsg_popstr (request); curve_codec_set_metadata (self->codec, name, value); free (name); free (value); } else if (streq (command, "CONNECT")) { assert (!self->endpoint); self->endpoint = zmsg_popstr (request); int rc = zsocket_connect (self->dealer, "%s", self->endpoint); assert (rc != -1); zframe_t *server_key = zmsg_pop (request); zframe_t *output = curve_codec_execute (self->codec, &server_key); zframe_send (&output, self->dealer, 0); self->state = connecting; } else if (streq (command, "DISCONNECT")) { if (self->endpoint) { int rc = zsocket_disconnect (self->dealer, "%s", self->endpoint); assert (rc != -1); free (self->endpoint); } } else if (streq (command, "VERBOSE")) { char *verbose = zmsg_popstr (request); curve_codec_set_verbose (self->codec, *verbose == '1'); free (verbose); } else if (streq (command, "TERMINATE")) { self->state = terminated; zstr_send (self->control, "OK"); } else { puts ("E: invalid command from API"); assert (false); } free (command); zmsg_destroy (&request); return 0; }
static void * server_task (void *args) { bool verbose = *((bool *) args); // Install the authenticator zctx_t *ctx = zctx_new (); zauth_t *auth = zauth_new (ctx); assert (auth); zauth_set_verbose (auth, verbose); zauth_configure_curve (auth, "*", TESTDIR); void *router = zsocket_new (ctx, ZMQ_ROUTER); int rc = zsocket_bind (router, "tcp://127.0.0.1:9005"); assert (rc != -1); zcert_t *server_cert = zcert_load (TESTDIR "/server.cert"); assert (server_cert); curve_codec_t *server = curve_codec_new_server (server_cert, ctx); assert (server); zcert_destroy (&server_cert); curve_codec_set_verbose (server, verbose); // Set some metadata properties curve_codec_set_metadata (server, "Server", "CURVEZMQ/curve_codec"); // Execute incoming frames until ready or exception // In practice we'd want a server instance per unique client while (!curve_codec_connected (server)) { zframe_t *sender = zframe_recv (router); zframe_t *input = zframe_recv (router); assert (input); zframe_t *output = curve_codec_execute (server, &input); assert (output); zframe_send (&sender, router, ZFRAME_MORE); zframe_send (&output, router, 0); } // Check client metadata char *client_name = (char *) zhash_lookup (curve_codec_metadata (server), "client"); assert (client_name); assert (streq (client_name, "CURVEZMQ/curve_client")); bool finished = false; while (!finished) { // Now act as echo service doing a full decode and encode zframe_t *sender = zframe_recv (router); zframe_t *encrypted = zframe_recv (router); assert (encrypted); zframe_t *cleartext = curve_codec_decode (server, &encrypted); assert (cleartext); if (memcmp (cleartext, "END", 3) == 0) finished = true; // Echo message back encrypted = curve_codec_encode (server, &cleartext); assert (encrypted); zframe_send (&sender, router, ZFRAME_MORE); zframe_send (&encrypted, router, 0); } curve_codec_destroy (&server); zauth_destroy (&auth); zctx_destroy (&ctx); return NULL; }