static void write_message_to_mailbox (client_t *self) { mlm_msg_t *msg = mlm_msg_new ( self->address, mlm_proto_address (self->message), mlm_proto_subject (self->message), mlm_proto_tracker (self->message), mlm_proto_timeout (self->message), mlm_proto_get_content (self->message)); // Try to dispatch to client immediately, if it's connected client_t *target = (client_t *) zhashx_lookup ( self->server->clients, mlm_proto_address (self->message)); if (target) { assert (!target->msg); target->msg = msg; engine_send_event (target, mailbox_message_event); } else // Else store in the eponymous mailbox zsock_send (self->server->mailbox, "ssp", "STORE", mlm_proto_address (self->message), msg); }
static bool s_authenticate_plain (self_t *self, zap_request_t *request) { if (self->passwords) { zhashx_refresh (self->passwords); char *password = (char *) zhashx_lookup (self->passwords, request->username); if (password && streq (password, request->password)) { if (self->verbose) zsys_info ("zauth: - allowed (PLAIN) username=%s password=%s", request->username, request->password); return true; } else { if (self->verbose) zsys_info ("zauth: - denied (PLAIN) username=%s password=%s", request->username, request->password); return false; } } else { if (self->verbose) zsys_info ("zauth: - denied (PLAIN) no password file defined"); return false; } }
zcert_t * zcertstore_lookup (zcertstore_t *self, const char *public_key) { if (self->loader) self->loader (self); return (zcert_t *) zhashx_lookup (self->certs, public_key); }
static service_t * s_service_require (client_t *self, const char *name) { service_t *service = (service_t *) zhashx_lookup (self->server->services, name); if (!service) service = s_service_new (name, self->server); if (service) zhashx_insert (self->server->services, name, service); return service; }
static stream_t * s_stream_require (client_t *self, const char *name) { stream_t *stream = (stream_t *) zhashx_lookup (self->server->streams, name); if (!stream) stream = s_stream_new (self, name); if (stream) zhashx_insert (self->server->streams, name, stream); return stream; }
static void write_message_to_xrap_client (client_t *self) { zuuid_t *client_id = xrap_traffic_sender (self->message); assert (client_id); client_t *client = (client_t *) zhashx_lookup (self->server->clients, zuuid_str (client_id)); if (client) { // Save message for broker client->msg = zmsg_dup (xrap_traffic_content (self->message)); client->callee = NULL; engine_send_event (client, xrap_message_event); } }
zcert_t * zcertstore_lookup (zcertstore_t *self, const char *public_key) { // If directory has changed, reload all certificates if (self->location) { zdir_t *dir = zdir_new (self->location, NULL); if (dir && (self->modified != zdir_modified (dir) || self->count != zdir_count (dir) || self->cursize != zdir_cursize (dir))) s_load_certs_from_disk (self); zdir_destroy (&dir); } return (zcert_t *) zhashx_lookup (self->certs, public_key); }
static void register_new_client (client_t *self) { self->address = strdup (mlm_proto_address (self->message)); // We ignore anonymous clients, which have empty addresses if (*self->address) { // If there's an existing client with this address, expire it // The alternative would be to reject new clients with the same address client_t *existing = (client_t *) zhashx_lookup ( self->server->clients, self->address); if (existing) engine_send_event (existing, expired_event); // In any case, we now own this address zhashx_update (self->server->clients, self->address, self); } if (*self->address) zsys_info ("client %u address='%s' - registering", self->unique_id, self->address); mlm_proto_set_status_code (self->message, MLM_PROTO_SUCCESS); }
static void server_accept (server_t *self, const char *key, const char *value) { tuple_t *tuple = (tuple_t *) zhashx_lookup (self->tuples, key); if (tuple && streq (tuple->value, value)) return; // Duplicate tuple, do nothing // Create new tuple tuple = (tuple_t *) zmalloc (sizeof (tuple_t)); assert (tuple); tuple->container = self->tuples; tuple->key = strdup (key); tuple->value = strdup (value); // Store new tuple zhashx_update (tuple->container, key, tuple); zhashx_freefn (tuple->container, key, tuple_free); // Deliver to calling application zstr_sendx (self->pipe, "DELIVER", key, value, NULL); // Hold in server context so we can broadcast to all clients self->cur_tuple = tuple; engine_broadcast_event (self, NULL, forward_event); // Copy new tuple announcement to all remotes zgossip_msg_t *gossip = zgossip_msg_new (); zgossip_msg_set_id (gossip, ZGOSSIP_MSG_PUBLISH); zsock_t *remote = (zsock_t *) zlistx_first (self->remotes); while (remote) { zgossip_msg_set_key (gossip, tuple->key); zgossip_msg_set_value (gossip, tuple->value); zgossip_msg_send (gossip, remote); remote = (zsock_t *) zlistx_next (self->remotes); } zgossip_msg_destroy (&gossip); }
// TODO: allow regular expressions in addresses static int s_self_authenticate (self_t *self) { zap_request_t *request = s_zap_request_new (self->handler, self->verbose); if (request) { // Is address explicitly whitelisted or blacklisted? bool allowed = false; bool denied = false; if (zhashx_size (self->whitelist)) { if (zhashx_lookup (self->whitelist, request->address)) { allowed = true; if (self->verbose) zsys_info ("zauth: - passed (whitelist) address=%s", request->address); } else { denied = true; if (self->verbose) zsys_info ("zauth: - denied (not in whitelist) address=%s", request->address); } } else if (zhashx_size (self->blacklist)) { if (zhashx_lookup (self->blacklist, request->address)) { denied = true; if (self->verbose) zsys_info ("zauth: - denied (blacklist) address=%s", request->address); } else { allowed = true; if (self->verbose) zsys_info ("zauth: - passed (not in blacklist) address=%s", request->address); } } // Mechanism-specific checks if (!denied) { if (streq (request->mechanism, "NULL") && !allowed) { // For NULL, we allow if the address wasn't blacklisted if (self->verbose) zsys_info ("zauth: - allowed (NULL)"); allowed = true; } else if (streq (request->mechanism, "PLAIN")) // For PLAIN, even a whitelisted address must authenticate allowed = s_authenticate_plain (self, request); else if (streq (request->mechanism, "CURVE")) // For CURVE, even a whitelisted address must authenticate allowed = s_authenticate_curve (self, request); else if (streq (request->mechanism, "GSSAPI")) // For GSSAPI, even a whitelisted address must authenticate allowed = s_authenticate_gssapi (self, request); } if (allowed) s_zap_request_reply (request, "200", "OK"); else s_zap_request_reply (request, "400", "No access"); s_zap_request_destroy (&request); } else s_zap_request_reply (request, "500", "Internal error"); return 0; }
JNIEXPORT jlong JNICALL Java_org_zeromq_czmq_Zhashx__1_1lookup (JNIEnv *env, jclass c, jlong self, jlong key) { jlong lookup_ = (jlong) (intptr_t) zhashx_lookup ((zhashx_t *) (intptr_t) self, (const void *) (intptr_t) key); return lookup_; }
zgtask_task_t * zgtask_tree_get_task (zgtask_tree_t *self) { assert (self); return (zgtask_task_t *) zhashx_lookup (self->data, "task"); }
zgtask_packet_t * zgtask_tree_get_packet (zgtask_tree_t *self) { assert (self); return (zgtask_packet_t *) zhashx_lookup (self->data, "packet"); }
void zhashx_test (int verbose) { printf (" * zhashx: "); // @selftest zhashx_t *hash = zhashx_new (); assert (hash); assert (zhashx_size (hash) == 0); assert (zhashx_first (hash) == NULL); assert (zhashx_cursor (hash) == NULL); // Insert some items int rc; rc = zhashx_insert (hash, "DEADBEEF", "dead beef"); char *item = (char *) zhashx_first (hash); assert (streq ((char *) zhashx_cursor (hash), "DEADBEEF")); assert (streq (item, "dead beef")); assert (rc == 0); rc = zhashx_insert (hash, "ABADCAFE", "a bad cafe"); assert (rc == 0); rc = zhashx_insert (hash, "C0DEDBAD", "coded bad"); assert (rc == 0); rc = zhashx_insert (hash, "DEADF00D", "dead food"); assert (rc == 0); assert (zhashx_size (hash) == 4); // Look for existing items item = (char *) zhashx_lookup (hash, "DEADBEEF"); assert (streq (item, "dead beef")); item = (char *) zhashx_lookup (hash, "ABADCAFE"); assert (streq (item, "a bad cafe")); item = (char *) zhashx_lookup (hash, "C0DEDBAD"); assert (streq (item, "coded bad")); item = (char *) zhashx_lookup (hash, "DEADF00D"); assert (streq (item, "dead food")); // Look for non-existent items item = (char *) zhashx_lookup (hash, "foo"); assert (item == NULL); // Try to insert duplicate items rc = zhashx_insert (hash, "DEADBEEF", "foo"); assert (rc == -1); item = (char *) zhashx_lookup (hash, "DEADBEEF"); assert (streq (item, "dead beef")); // Some rename tests // Valid rename, key is now LIVEBEEF rc = zhashx_rename (hash, "DEADBEEF", "LIVEBEEF"); assert (rc == 0); item = (char *) zhashx_lookup (hash, "LIVEBEEF"); assert (streq (item, "dead beef")); // Trying to rename an unknown item to a non-existent key rc = zhashx_rename (hash, "WHATBEEF", "NONESUCH"); assert (rc == -1); // Trying to rename an unknown item to an existing key rc = zhashx_rename (hash, "WHATBEEF", "LIVEBEEF"); assert (rc == -1); item = (char *) zhashx_lookup (hash, "LIVEBEEF"); assert (streq (item, "dead beef")); // Trying to rename an existing item to another existing item rc = zhashx_rename (hash, "LIVEBEEF", "ABADCAFE"); assert (rc == -1); item = (char *) zhashx_lookup (hash, "LIVEBEEF"); assert (streq (item, "dead beef")); item = (char *) zhashx_lookup (hash, "ABADCAFE"); assert (streq (item, "a bad cafe")); // Test keys method zlistx_t *keys = zhashx_keys (hash); assert (zlistx_size (keys) == 4); zlistx_destroy (&keys); zlistx_t *values = zhashx_values(hash); assert (zlistx_size (values) == 4); zlistx_destroy (&values); // Test dup method zhashx_t *copy = zhashx_dup (hash); assert (zhashx_size (copy) == 4); item = (char *) zhashx_lookup (copy, "LIVEBEEF"); assert (item); assert (streq (item, "dead beef")); zhashx_destroy (©); // Test pack/unpack methods zframe_t *frame = zhashx_pack (hash); copy = zhashx_unpack (frame); zframe_destroy (&frame); assert (zhashx_size (copy) == 4); item = (char *) zhashx_lookup (copy, "LIVEBEEF"); assert (item); assert (streq (item, "dead beef")); zhashx_destroy (©); // Test save and load zhashx_comment (hash, "This is a test file"); zhashx_comment (hash, "Created by %s", "czmq_selftest"); zhashx_save (hash, ".cache"); copy = zhashx_new (); assert (copy); zhashx_load (copy, ".cache"); item = (char *) zhashx_lookup (copy, "LIVEBEEF"); assert (item); assert (streq (item, "dead beef")); zhashx_destroy (©); zsys_file_delete (".cache"); // Delete a item zhashx_delete (hash, "LIVEBEEF"); item = (char *) zhashx_lookup (hash, "LIVEBEEF"); assert (item == NULL); assert (zhashx_size (hash) == 3); // Check that the queue is robust against random usage struct { char name [100]; bool exists; } testset [200]; memset (testset, 0, sizeof (testset)); int testmax = 200, testnbr, iteration; srandom ((unsigned) time (NULL)); for (iteration = 0; iteration < 25000; iteration++) { testnbr = randof (testmax); if (testset [testnbr].exists) { item = (char *) zhashx_lookup (hash, testset [testnbr].name); assert (item); zhashx_delete (hash, testset [testnbr].name); testset [testnbr].exists = false; } else { sprintf (testset [testnbr].name, "%x-%x", rand (), rand ()); if (zhashx_insert (hash, testset [testnbr].name, "") == 0) testset [testnbr].exists = true; } } // Test 10K lookups for (iteration = 0; iteration < 10000; iteration++) item = (char *) zhashx_lookup (hash, "DEADBEEFABADCAFE"); // Destructor should be safe to call twice zhashx_destroy (&hash); zhashx_destroy (&hash); assert (hash == NULL); // Test autofree; automatically copies and frees string values hash = zhashx_new (); assert (hash); zhashx_autofree (hash); char value [255]; strcpy (value, "This is a string"); rc = zhashx_insert (hash, "key1", value); assert (rc == 0); strcpy (value, "Ring a ding ding"); rc = zhashx_insert (hash, "key2", value); assert (rc == 0); assert (streq ((char *) zhashx_lookup (hash, "key1"), "This is a string")); assert (streq ((char *) zhashx_lookup (hash, "key2"), "Ring a ding ding")); zhashx_destroy (&hash); // @end printf ("OK\n"); }
void ztrie_test (bool verbose) { printf (" * ztrie: "); // @selftest // Create a new trie for matching strings that can be tokenized by a slash // (e.g. URLs minus the protocol, address and port). ztrie_t *self = ztrie_new ('/'); assert (self); int ret = 0; // Let's start by inserting a couple of routes into the trie. // This one is for the route '/foo/bar' the slash at the beginning of the // route is important because everything before the first delimiter will be // discarded. A slash at the end of a route is optional though. The data // associated with this node is passed without destroy function which means // it must be destroyed by the caller. int foo_bar_data = 10; ret = ztrie_insert_route (self, "/foo/bar", &foo_bar_data, NULL); assert (ret == 0); // Now suppose we like to match all routes with two tokens that start with // '/foo/' but aren't '/foo/bar'. This is possible by using regular // expressions which are enclosed in an opening and closing curly bracket. // Tokens that contain regular expressions are always match after string // based tokens. // Note: There is no order in which regular expressions are sorted thus // if you enter multiple expressions for a route you will have to make // sure they don't have overlapping results. For example '/foo/{[^/]+}' // and '/foo/{\d+} having could turn out badly. int foo_other_data = 100; ret = ztrie_insert_route (self, "/foo/{[^/]+}", &foo_other_data, NULL); assert (ret == 0); // Regular expression are only matched against tokens of the same level. // This allows us to append to are route with a regular expression as if // it were a string. ret = ztrie_insert_route (self, "/foo/{[^/]+}/gulp", NULL, NULL); assert (ret == 0); // Routes are identified by their endpoint, which is the last token of the route. // It is possible to insert routes for a node that already exists but isn't an // endpoint yet. The delimiter at the end of a route is optional and has no effect. ret = ztrie_insert_route (self, "/foo/", NULL, NULL); assert (ret == 0); // If you try to insert a route which already exists the method will return -1. ret = ztrie_insert_route (self, "/foo", NULL, NULL); assert (ret == -1); // It is not allowed to insert routes with empty tokens. ret = ztrie_insert_route (self, "//foo", NULL, NULL); assert (ret == -1); // Everything before the first delimiter is ignored so 'foo/bar/baz' is equivalent // to '/bar/baz'. ret = ztrie_insert_route (self, "foo/bar/baz", NULL, NULL); assert (ret == 0); ret = ztrie_insert_route (self, "/bar/baz", NULL, NULL); assert (ret == -1); // Of course you are allowed to remove routes, in case there is data associated with a // route and a destroy data function has been supplied that data will be destroyed. ret = ztrie_remove_route (self, "/foo"); assert (ret == 0); // Removing a non existent route will as well return -1. ret = ztrie_remove_route (self, "/foo"); assert (ret == -1); // Removing a route with a regular expression must exactly match the entered one. ret = ztrie_remove_route (self, "/foo/{[^/]+}"); assert (ret == 0); // Next we like to match a path by regular expressions and also extract matched // parts of a route. This can be done by naming the regular expression. The name of a // regular expression is entered at the beginning of the curly brackets and separated // by a colon from the regular expression. The first one in this examples is named // 'name' and names the expression '[^/]'. If there is no capturing group defined in // the expression the whole matched string will be associated with this parameter. In // case you don't like the get the whole matched string use a capturing group, like // it has been done for the 'id' parameter. This is nice but you can even match as // many parameter for a token as you like. Therefore simply put the parameter names // separated by colons in front of the regular expression and make sure to add a // capturing group for each parameter. The first parameter will be associated with // the first capturing and so on. char *data = (char *) malloc (80); sprintf (data, "%s", "Hello World!"); ret = ztrie_insert_route (self, "/baz/{name:[^/]+}/{id:--(\\d+)}/{street:nr:(\\a+)(\\d+)}", data, NULL); assert (ret == 0); // There is a lot you can do with regular expression but matching routes // of arbitrary length wont work. Therefore we make use of the asterisk // operator. Just place it at the end of your route, e.g. '/config/bar/*'. ret = ztrie_insert_route (self, "/config/bar/*", NULL, NULL); assert (ret == 0); // Appending to an asterisk as you would to with a regular expression // isn't valid. ret = ztrie_insert_route (self, "/config/bar/*/bar", NULL, NULL); assert (ret == -1); // The asterisk operator will only work as a leaf in the tree. If you // enter an asterisk in the middle of your route it will simply be // interpreted as a string. ret = ztrie_insert_route (self, "/test/*/bar", NULL, NULL); assert (ret == 0); // If a parent has an asterisk as child it is not allowed to have // other siblings. ret = ztrie_insert_route (self, "/config/bar/foo/glup", NULL, NULL); assert (ret != 0); // Test matches bool hasMatch = false; // The route '/bar/foo' will fail to match as this route has never been inserted. hasMatch = ztrie_matches (self, "/bar/foo"); assert (!hasMatch); // The route '/foo/bar' will match and we can obtain the data associated with it. hasMatch = ztrie_matches (self, "/foo/bar"); assert (hasMatch); int foo_bar_hit_data = *((int *) ztrie_hit_data (self)); assert (foo_bar_data == foo_bar_hit_data); // This route is part of another but is no endpoint itself thus the matches will fail. hasMatch = ztrie_matches (self, "/baz/blub"); assert (!hasMatch); // This route will match our named regular expressions route. Thus we can extract data // from the route by their names. hasMatch = ztrie_matches (self, "/baz/blub/--11/abc23"); assert (hasMatch); char *match_data = (char *) ztrie_hit_data (self); assert (streq ("Hello World!", match_data)); zhashx_t *parameters = ztrie_hit_parameters (self); assert (zhashx_size (parameters) == 4); assert (streq ("blub", (char *) zhashx_lookup (parameters, "name"))); assert (streq ("11", (char *) zhashx_lookup (parameters, "id"))); assert (streq ("abc", (char *) zhashx_lookup (parameters, "street"))); assert (streq ("23", (char *) zhashx_lookup (parameters, "nr"))); zhashx_destroy (¶meters); // This will match our asterisk route '/config/bar/*'. As the result we // can obtain the asterisk matched part of the route. hasMatch = ztrie_matches (self, "/config/bar/foo/bar"); assert (hasMatch); assert (streq (ztrie_hit_asterisk_match (self), "foo/bar")); zstr_free (&data); ztrie_destroy (&self); // @end printf ("OK\n"); }
/// // Return the item at the specified key, or null void * QZhashx::lookup (const void *key) { void * rv = zhashx_lookup (self, key); return rv; }