Esempio n. 1
0
int client_opts_set(struct mosquitto *mosq, struct mosq_config *cfg)
{
    int rc;

    if(cfg->will_topic && mosquitto_will_set(mosq, cfg->will_topic,
            cfg->will_payloadlen, cfg->will_payload, cfg->will_qos,
            cfg->will_retain)) {

        if(!cfg->quiet) fprintf(stderr, "Error: Problem setting will.\n");
        mosquitto_lib_cleanup();
        return 1;
    }
    if(cfg->username && mosquitto_username_pw_set(mosq, cfg->username, cfg->password)) {
        if(!cfg->quiet) fprintf(stderr, "Error: Problem setting username and password.\n");
        mosquitto_lib_cleanup();
        return 1;
    }
#ifdef WITH_TLS
    if((cfg->cafile || cfg->capath)
            && mosquitto_tls_set(mosq, cfg->cafile, cfg->capath, cfg->certfile, cfg->keyfile, NULL)) {

        if(!cfg->quiet) fprintf(stderr, "Error: Problem setting TLS options.\n");
        mosquitto_lib_cleanup();
        return 1;
    }
    if(cfg->insecure && mosquitto_tls_insecure_set(mosq, true)) {
        if(!cfg->quiet) fprintf(stderr, "Error: Problem setting TLS insecure option.\n");
        mosquitto_lib_cleanup();
        return 1;
    }
#  ifdef WITH_TLS_PSK
    if(cfg->psk && mosquitto_tls_psk_set(mosq, cfg->psk, cfg->psk_identity, NULL)) {
        if(!cfg->quiet) fprintf(stderr, "Error: Problem setting TLS-PSK options.\n");
        mosquitto_lib_cleanup();
        return 1;
    }
#  endif
    if(cfg->tls_version && mosquitto_tls_opts_set(mosq, 1, cfg->tls_version, cfg->ciphers)) {
        if(!cfg->quiet) fprintf(stderr, "Error: Problem setting TLS options.\n");
        mosquitto_lib_cleanup();
        return 1;
    }
#endif
    mosquitto_max_inflight_messages_set(mosq, cfg->max_inflight);
#ifdef WITH_SOCKS
    if(cfg->socks5_host) {
        rc = mosquitto_socks5_set(mosq, cfg->socks5_host, cfg->socks5_port, cfg->socks5_username, cfg->socks5_password);
        if(rc) {
            mosquitto_lib_cleanup();
            return rc;
        }
    }
#endif
    mosquitto_opts_set(mosq, MOSQ_OPT_PROTOCOL_VERSION, &(cfg->protocol_version));
    return MOSQ_ERR_SUCCESS;
}
Esempio n. 2
0
static
int
mosq_tls_insecure_set(lua_State *L)
{
	mosq_t *ctx = mosq_get(L, 1);
	bool value = lua_toboolean(L, 2);

	int rc = mosquitto_tls_insecure_set(ctx->mosq, value);
	return make_mosq_status_result(L, rc);
}
Esempio n. 3
0
/* {{{ Mosquitto\Client::setTlsInsecure() */
PHP_METHOD(Mosquitto_Client, setTlsInsecure)
{
	mosquitto_client_object *object;
	zend_bool value = 0;
	int retval = 0;

	PHP_MOSQUITTO_ERROR_HANDLING();
	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "b", &value) == FAILURE) {
		PHP_MOSQUITTO_RESTORE_ERRORS();
		return;
	}
	PHP_MOSQUITTO_RESTORE_ERRORS();

	object = (mosquitto_client_object *) zend_object_store_get_object(getThis() TSRMLS_CC);

	retval = mosquitto_tls_insecure_set(object->client, value);

	php_mosquitto_handle_errno(retval, errno TSRMLS_CC);
}
Esempio n. 4
0
int mosquittopp::tls_insecure_set(bool value)
{
	return mosquitto_tls_insecure_set(m_mosq, value);
}
Esempio n. 5
0
int main(int argc, char **argv)
{
	struct tm gmt_init, local_init;
	char ch, *progname = *argv, *nodename, *topic, *prefix = DEFAULT_PREFIX;;
	int usage = 0, interval = 10, rc;
	struct utsname uts;
	char clientid[30];
	char *host = "localhost", *ca_file;
	int port = 1883, keepalive = 60;
	int do_tls = FALSE, tls_insecure = FALSE;
	int do_psk = FALSE;
	char *psk_key = NULL, *psk_identity = NULL;
	struct udata udata;

	udata.mid = 17;

	while ((ch = getopt(argc, argv, "i:t:h:p:C:LUK:I:")) != EOF) {
		switch (ch) {
			case 'C':
				ca_file = optarg;
				do_tls = TRUE;
				break;
			case 'h':
				host = optarg;
				break;
			case 'i':
				interval = atoi(optarg);
				interval = (interval < 1) ? 1 : interval;
				break;
			case 's':
				tls_insecure = TRUE;
				break;
			case 'p':
				port = atoi(optarg);
				break;
			case 't':
				prefix = optarg;
				break;
			case 'L':
				do_local = !do_local;
				break;
			case 'U':
				do_utc = !do_utc;
				break;
			case 'I':
				psk_identity = optarg;
				do_psk = TRUE;
				break;
			case 'K':
				psk_key = optarg;
				do_psk = TRUE;
				break;
			default:
				usage = 1;
				break;
		}
	}

	if (do_tls && do_psk)
		usage = 1;
	if (do_psk && (psk_key == NULL || psk_identity == NULL))
		usage = 1;

	if (usage) {
		fprintf(stderr, "Usage: %s [-h host] [-i interval] [-p port] [-t prefix] [-C CA-cert] [-L] [-U] [-K psk-key] [-I psk-identity] [-s]\n", progname);
		exit(1);
	}

	/* Find nodename; chop at first '.' */

	if (uname(&uts) == 0) {
		char *p;
		nodename = strdup(uts.nodename);

		if ((p = strchr(nodename, '.')) != NULL)
			*p = 0;
	} else {
		nodename = strdup("unknown");
	}

	mosquitto_lib_init();

	sprintf(clientid, "mqtt-tics-%d", getpid());
	m = mosquitto_new(clientid, TRUE, (void *)&udata);
	if (!m) {
		fprintf(stderr, "Out of memory.\n");
		exit(1);
	}

	if (do_psk) {
		rc = mosquitto_tls_psk_set(m, psk_key, psk_identity,NULL);
		if (rc != MOSQ_ERR_SUCCESS) {
			fprintf(stderr, "Cannot set TLS PSK: %s\n",
				mosquitto_strerror(rc));
			exit(3);
		}
	} else if (do_tls) {
		rc = mosquitto_tls_set(m, ca_file, NULL, NULL, NULL, NULL);
		if (rc != MOSQ_ERR_SUCCESS) {
			fprintf(stderr, "Cannot set TLS PSK: %s\n",
				mosquitto_strerror(rc));
			exit(3);
		}

		/* FIXME */
		// mosquitto_tls_opts_set(m, SSL_VERIFY_PEER, "tlsv1", NULL);
		
		if (tls_insecure) {
#if LIBMOSQUITTO_VERSION_NUMBER >= 1002000
			/* mosquitto_tls_insecure_set() requires libmosquitto 1.2. */
			mosquitto_tls_insecure_set(m, TRUE);
#endif
		}
	}

	mosquitto_publish_callback_set(m, cb_pub);
	mosquitto_disconnect_callback_set(m, cb_disconnect);
	if ((rc = mosquitto_connect(m, host, port, keepalive)) != MOSQ_ERR_SUCCESS) {
		fprintf(stderr, "Unable to connect to %s:%d: %s\n", host, port,
			mosquitto_strerror(rc));
		perror("");
		exit(2);
	}

	signal(SIGINT, catcher);

	mosquitto_loop_start(m);

	if ((topic = malloc(strlen(prefix) + (3 * strlen(nodename)) + 12)) == NULL) {
		fprintf(stderr, "ENOMEM\n");
		goto abort;
	}
	sprintf(topic, prefix, nodename, nodename, nodename);

	memset(&gmt_init, 0, sizeof(struct tm));
	memset(&local_init, 0, sizeof(struct tm));

	time(&start_tics);

	while (1) {
		pubtime(topic, &gmt_init, &local_init);
		sleep(interval);
	}

	free(topic);

   abort:
	free(nodename);

	mosquitto_disconnect(m);
	mosquitto_loop_stop(m, false);
	mosquitto_lib_cleanup();

	return 0;
}