Esempio n. 1
0
//  Apply configuration tree:
//   * apply server configuration
//   * print any echo items in top-level sections
//   * apply sections that match methods
static void
server_apply_config (server_t *self)
{
    //  Apply echo commands and class methods
    zconfig_t *section = zconfig_child (self->config);
    while (section) {
        zconfig_t *entry = zconfig_child (section);
        while (entry) {
            if (streq (zconfig_name (entry), "echo"))
                zclock_log (zconfig_value (entry));
            entry = zconfig_next (entry);
        }
        if (streq (zconfig_name (section), "bind")) {
            char *endpoint = zconfig_resolve (section, "endpoint", "?");
            self->port = zsocket_bind (self->router, endpoint);
        }
        else
        if (streq (zconfig_name (section), "publish")) {
            char *location = zconfig_resolve (section, "location", "?");
            char *alias = zconfig_resolve (section, "alias", "?");
            mount_t *mount = mount_new (location, alias);
            zlist_append (self->mounts, mount);          
        }
        else
        if (streq (zconfig_name (section), "set_anonymous")) {
            long enabled = atoi (zconfig_resolve (section, "enabled", ""));
            //  Enable anonymous access without a config file                   
            zconfig_put (self->config, "security/anonymous", enabled? "1" :"0");
        }
        section = zconfig_next (section);
    }
    server_config_self (self);
}
static
zsock_t* subscriber_sub_socket_new(subscriber_state_t *state)
{
    zsock_t *socket = zsock_new(ZMQ_SUB);
    assert(socket);
    zsock_set_rcvhwm(socket, state->rcv_hwm);

    // set subscription
    if (!state->subscriptions || zlist_size(state->subscriptions) == 0) {
        if (!state->subscriptions)
            state->subscriptions = zlist_new();
        zlist_append(state->subscriptions, zconfig_resolve(state->config, "/logjam/subscription", ""));
    }

    char *subscription = zlist_first(state->subscriptions);
    bool subscribed_to_all = false;
    while (subscription) {
        printf("[I] subscriber: subscribing to '%s'\n", subscription);
        if (streq(subscription, ""))
            subscribed_to_all = true;
        zsock_set_subscribe(socket, subscription);
        subscription = zlist_next(state->subscriptions);
    }
    if (!subscribed_to_all)
        zsock_set_subscribe(socket, "heartbeat");

    if (!state->devices || zlist_size(state->devices) == 0) {
        // convert config file to list of devices
        if (!state->devices)
            state->devices = zlist_new();
        zconfig_t *endpoints = zconfig_locate(state->config, "/logjam/endpoints");
        if (!endpoints) {
            zlist_append(state->devices, "tcp://localhost:9606");
        } else {
            zconfig_t *endpoint = zconfig_child(endpoints);
            while (endpoint) {
                char *spec = zconfig_value(endpoint);
                char *new_spec = augment_zmq_connection_spec(spec, 9606);
                zlist_append(state->devices, new_spec);
                endpoint = zconfig_next(endpoint);
            }
        }
    }

    char* device = zlist_first(state->devices);
    while (device) {
        printf("[I] subscriber: connecting SUB socket to logjam-device via %s\n", device);
        int rc = zsock_connect(socket, "%s", device);
        log_zmq_error(rc, __FILE__, __LINE__);
        assert(rc == 0);
        device = zlist_next(state->devices);
    }

    return socket;
}
Esempio n. 3
0
zcert_t *
zcert_load (char *format, ...)
{
#if (ZMQ_VERSION_MAJOR == 4)
    assert (format);
    va_list argptr;
    va_start (argptr, format);
    char *filename = zsys_vprintf (format, argptr);
    va_end (argptr);

    //  Try first to load secret certificate, which has both keys
    //  Then fallback to loading public certificate
    char filename_secret [256];
    snprintf (filename_secret, 256, "%s_secret", filename);
    zconfig_t *root = zconfig_load (filename_secret);
    if (!root)
        root = zconfig_load (filename);
        
    zcert_t *self = NULL;
    if (root) {
        char *public_text = zconfig_resolve (root, "/curve/public-key", NULL);
        char *secret_text = zconfig_resolve (root, "/curve/secret-key", NULL);
        if (public_text && strlen (public_text) == 40) {
            byte public_key [32] = { 0 };
            byte secret_key [32] = { 0 };
            zmq_z85_decode (public_key, public_text);
            if (secret_text && strlen (secret_text) == 40)
                zmq_z85_decode (secret_key, secret_text);

            //  Load metadata into certificate
            self = zcert_new_from (public_key, secret_key);
            zconfig_t *metadata = zconfig_locate (root, "/metadata");
            zconfig_t *item = metadata? zconfig_child (metadata): NULL;
            while (item) {
                zcert_set_meta (self, zconfig_name (item), zconfig_value (item));
                item = zconfig_next (item);
            }
        }
    }
    zconfig_destroy (&root);
    zstr_free (&filename);
    return self;
#else   
    return NULL;
#endif
}
Esempio n. 4
0
static
zlist_t* extract_devices_from_config(zconfig_t* config)
{
    zlist_t *devices = zlist_new();
    zconfig_t *bindings = zconfig_locate(config, "frontend/endpoints/bindings");
    assert(bindings);
    zconfig_t *binding = zconfig_child(bindings);
    while (binding) {
        char *spec = zconfig_value(binding);
        if (streq(spec, "")) {
            if (verbose)
                printf("[I] subscriber: ignoring empty SUB socket binding\n");
        } else {
            zlist_append(devices, spec);
        }
        binding = zconfig_next(binding);
    }
    return devices;
}
Esempio n. 5
0
///
//  Find our first child, if any
QmlZconfig *QmlZconfig::child () {
    QmlZconfig *retQ_ = new QmlZconfig ();
    retQ_->self = zconfig_child (self);
    return retQ_;
};
Esempio n. 6
0
static int
server_join_cluster (server_t *self)
{
    self->zyre = zyre_new (zconfig_resolve (self->config, "zyre/name", NULL));
    
    //  Get Zyre configuration properties
    char *value = zconfig_resolve (self->config, "zyre/discovery", "beacon");
    if (streq (value, "beacon")) {
        //  Set-up UDP beacon discovery
        zsys_info ("using UDP beacon discovery service");
        value = zconfig_resolve (self->config, "zyre/beacon/interface", "auto");
        if (strneq (value, "auto")) {
            zsys_info ("forcing cluster interface to %s", value);
            zyre_set_interface (self->zyre, value);
        }
        value = zconfig_resolve (self->config, "zyre/beacon/interval", NULL);
        if (value)
            zyre_set_interval (self->zyre, atoi (value));

        value = zconfig_resolve (self->config, "zyre/beacon/port", NULL);
        if (value) {
            zsys_info ("UDP beaconing on port %s", value);
            zyre_set_port (self->zyre, atoi (value));
        }
    }
    else
    if (streq (value, "gossip")) {
        //  Set-up TCP gossip discovery
        zsys_info ("using TCP gossip discovery service");
        zconfig_t *section = zconfig_locate (self->config, "zyre/gossip");
        if (!section) {
            zsys_warning ("please configure zyre/gossip section");
            return -1;
        }
        zconfig_t *entry = zconfig_child (section);
        while (entry) {
            char *name = zconfig_name (entry);
            char *value = zconfig_value (entry);
            if (streq (name, "endpoint")) {
                zsys_info ("Zyre node endpoint=%s", value);
                zyre_set_endpoint (self->zyre, "%s", value);
            }
            else
            if (streq (name, "bind")) {
                zsys_info ("gossip service bind to %s", value);
                zyre_gossip_bind (self->zyre, "%s", value);
            }
            else
            if (streq (name, "connect")) {
                zsys_info ("gossip service connect to %s", value);
                zyre_gossip_connect (self->zyre, "%s", value);
            }
            else
                zsys_warning ("unknown zyre/gossip entry '%s' ignored", name);
            entry = zconfig_next (entry);
        }
    }
    else {
        zsys_error ("bad zyre/discovery=%s (use beacon|gossip)", value);
        assert (0);
    }
    if (zyre_start (self->zyre)) {
        zsys_warning ("clustering not working");
        return -1;              //  Can't join cluster
    }
    zyre_join (self->zyre, "ZPIPES");

    //  Get Zyre public name for logging
    self->name = strdup (zyre_name (self->zyre));
    zsys_info ("joining cluster as %s", self->name);

    //  Set-up reader for Zyre events
    engine_handle_socket (self, zyre_socket (self->zyre), zyre_handler);
    return 0;
}
Esempio n. 7
0
JNIEXPORT jlong JNICALL
Java_zconfig__1_1child (JNIEnv *env, jclass c, jlong self)
{
    jlong child_ = (jlong) zconfig_child ((zconfig_t *) self);
    return child_;
}
Esempio n. 8
0
void
zconfig_test (bool verbose)
{
    printf (" * zconfig: ");

    //  @selftest

    const char *SELFTEST_DIR_RW = "src/selftest-rw";

    const char *testbasedir  = ".test_zconfig";
    const char *testfile = "test.cfg";
    char *basedirpath = NULL;   // subdir in a test, under SELFTEST_DIR_RW
    char *filepath = NULL;      // pathname to testfile in a test, in dirpath

    basedirpath = zsys_sprintf ("%s/%s", SELFTEST_DIR_RW, testbasedir);
    assert (basedirpath);
    filepath = zsys_sprintf ("%s/%s", basedirpath, testfile);
    assert (filepath);

    // Make sure old aborted tests do not hinder us
    zdir_t *dir = zdir_new (basedirpath, NULL);
    if (dir) {
        zdir_remove (dir, true);
        zdir_destroy (&dir);
    }
    zsys_file_delete (filepath);
    zsys_dir_delete  (basedirpath);

    //  Create temporary directory for test files
    zsys_dir_create (basedirpath);

    zconfig_t *root = zconfig_new ("root", NULL);
    assert (root);
    zconfig_t *section, *item;

    section = zconfig_new ("headers", root);
    assert (section);
    item = zconfig_new ("email", section);
    assert (item);
    zconfig_set_value (item, "*****@*****.**");
    item = zconfig_new ("name", section);
    assert (item);
    zconfig_set_value (item, "Justin Kayce");
    zconfig_putf (root, "/curve/secret-key", "%s", "Top Secret");
    zconfig_set_comment (root, "   CURVE certificate");
    zconfig_set_comment (root, "   -----------------");
    assert (zconfig_comments (root));
    zconfig_save (root, filepath);
    zconfig_destroy (&root);
    root = zconfig_load (filepath);
    if (verbose)
        zconfig_save (root, "-");
    assert (streq (zconfig_filename (root), filepath));

    char *email = zconfig_get (root, "/headers/email", NULL);
    assert (email);
    assert (streq (email, "*****@*****.**"));
    char *passwd = zconfig_get (root, "/curve/secret-key", NULL);
    assert (passwd);
    assert (streq (passwd, "Top Secret"));

    zconfig_savef (root, "%s/%s", basedirpath, testfile);
    assert (!zconfig_has_changed (root));
    int rc = zconfig_reload (&root);
    assert (rc == 0);
    assert (!zconfig_has_changed (root));
    zconfig_destroy (&root);

    //  Test chunk load/save
    root = zconfig_new ("root", NULL);
    assert (root);
    section = zconfig_new ("section", root);
    assert (section);
    item = zconfig_new ("value", section);
    assert (item);
    zconfig_set_value (item, "somevalue");
    zconfig_t *search = zconfig_locate (root, "section/value");
    assert (search == item);
    zchunk_t *chunk = zconfig_chunk_save (root);
    assert (strlen ((char *) zchunk_data (chunk)) == 32);
    char *string = zconfig_str_save (root);
    assert (string);
    assert (streq (string, (char *) zchunk_data (chunk)));
    freen (string);
    assert (chunk);
    zconfig_destroy (&root);

    root = zconfig_chunk_load (chunk);
    assert (root);
    char *value = zconfig_get (root, "/section/value", NULL);
    assert (value);
    assert (streq (value, "somevalue"));

    //  Test config can't be saved to a file in a path that doesn't
    //  exist or isn't writable
    rc = zconfig_savef (root, "%s/path/that/doesnt/exist/%s", basedirpath, testfile);
    assert (rc == -1);

    zconfig_destroy (&root);
    zchunk_destroy (&chunk);

    //  Test subtree removal
	{
		zconfig_t *root = zconfig_str_load (
			"context\n"
			"    iothreads = 1\n"
			"    verbose = 1      #   Ask for a trace\n"
			"main\n"
			"    type = zqueue    #  ZMQ_DEVICE type\n"
			"    frontend\n"
			"        option\n"
			"            hwm = 1000\n"
			"            swap = 25000000     #  25MB\n"
			"        bind = 'inproc://addr1'\n"
			"        bind = 'ipc://addr2'\n"
			"    backend\n"
			"        bind = inproc://addr3\n"
		);

        zconfig_t *to_delete = zconfig_locate (root, "main/frontend");
        assert (to_delete);

        zconfig_remove (to_delete);

        char *value = zconfig_get (root, "/main/type", NULL);
        assert (value);
        assert (streq (value, "zqueue"));

        value = zconfig_get (root, "/main/backend/bind", NULL);
        assert (value);
        assert (streq (value, "inproc://addr3"));

        value = zconfig_get (root, "/main/frontend", NULL);
        assert (value);

        value = zconfig_get (root, "/main/frontend/option", NULL);
        assert (value == NULL);

        value = zconfig_get (root, "/main/frontend/option/swap", NULL);
        assert (value == NULL);

        zconfig_destroy (&root);
	}

    // Test str_load
    zconfig_t *config = zconfig_str_load (
        "malamute\n"
        "    endpoint = ipc://@/malamute\n"
        "    producer = STREAM\n"
        "    consumer\n"
        "        STREAM2 = .*\n"
        "        STREAM3 = HAM\n"
        "server\n"
        "    verbose = true\n"
        );
    assert (config);
    assert (streq (zconfig_get (config, "malamute/endpoint", NULL), "ipc://@/malamute"));
    assert (streq (zconfig_get (config, "malamute/producer", NULL), "STREAM"));
    assert (zconfig_locate (config, "malamute/consumer"));

    zconfig_t *c = zconfig_child (zconfig_locate (config, "malamute/consumer"));
    assert (c);
    assert (streq (zconfig_name (c), "STREAM2"));
    assert (streq (zconfig_value (c), ".*"));

    c = zconfig_next (c);
    assert (c);
    assert (streq (zconfig_name (c), "STREAM3"));
    assert (streq (zconfig_value (c), "HAM"));

    c = zconfig_next (c);
    assert (!c);

    assert (streq (zconfig_get (config, "server/verbose", NULL), "true"));

    zconfig_destroy (&config);

    //  Delete all test files
    dir = zdir_new (basedirpath, NULL);
    assert (dir);
    zdir_remove (dir, true);
    zdir_destroy (&dir);

    zstr_free (&basedirpath);
    zstr_free (&filepath);

#if defined (__WINDOWS__)
    zsys_shutdown();
#endif
    //  @end

    printf ("OK\n");
}
Esempio n. 9
0
JNIEXPORT jlong JNICALL
Java_org_zeromq_czmq_Zconfig__1_1child (JNIEnv *env, jclass c, jlong self)
{
    jlong child_ = (jlong) (intptr_t) zconfig_child ((zconfig_t *) (intptr_t) self);
    return child_;
}