Skip to content

Commit

Permalink
TLS stuff and some other config options
Browse files Browse the repository at this point in the history
  • Loading branch information
ganehag committed Feb 9, 2023
1 parent e1c95f7 commit c75090f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 5 deletions.
17 changes: 17 additions & 0 deletions src/config_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,23 @@ config_parse_file(FILE *file, config_t *config) {
strncmp(value, "0", 1) == 0) {
config->retain = 0;
}
} else if (strncmp(name, "mqtt_protocol", 13) == 0) {
if (strncmp(value, "3.1", 3) == 0) {
config->mqtt_protocol_version = MQTT_PROTOCOL_V31;
} else if (strncmp(value, "3.1.1", 5) == 0) {
config->mqtt_protocol_version = MQTT_PROTOCOL_V311;
} else if (strncmp(value, "5", 1) == 0) {
config->mqtt_protocol_version = MQTT_PROTOCOL_V5;
}
} else if (strncmp(name, "tls_version", 11) == 0) {
// For openssl >= 1.0.1, the available options are tlsv1.2,
// tlsv1.1 and tlsv1, with tlv1.2 being the default. For
// openssl < 1.0.1, the available options are tlsv1 and
// sslv3, with tlsv1 being the default.
strncpy(config->tls_version,
value,
sizeof(config->tls_version));

} else if (strncmp(name, "clean_session", 13) == 0) {
if (strncmp(value, "true", 4) == 0 ||
strncmp(value, "1", 1) == 0) {
Expand Down
6 changes: 6 additions & 0 deletions src/config_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,18 @@ typedef struct {
uint8_t retain;
uint8_t clean_session;

uint16_t timeout;
uint16_t reconnect_delay;

// tls stuff
char ca_cert_path[256];
char cert_path[256];
char key_path[256];
uint8_t verify;

int mqtt_protocol_version;
char tls_version[12];

char request_topic[1024];
char response_topic[1024];

Expand Down
46 changes: 41 additions & 5 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,20 @@ main(int argc, char *argv[]) {
}
}

// Default values for config
config.mqtt_protocol_version = MQTT_PROTOCOL_V31;
config.qos = 0;
config.retain = 0;
config.keepalive = 60;
config.port = 1883;
config.timeout = 10;
config.reconnect_delay = 5;
strncpy(config.host, "localhost", sizeof(config.host) - 1);
strncpy(config.request_topic, "request", sizeof(config.request_topic) - 1);
strncpy(
config.response_topic, "response", sizeof(config.response_topic) - 1);
strncpy(config.tls_version, "tlsv1", sizeof(config.tls_version) - 1);

if (configfile == NULL) {
// load config from default locations
char *config_files[] = {"/etc/omgw/omg.conf",
Expand Down Expand Up @@ -253,19 +267,41 @@ main(int argc, char *argv[]) {
mosquitto_connect_callback_set(mosq, mqtt_connect_callback);
mosquitto_message_callback_set(mosq, mqtt_message_callback);

// Set username and password
if (argc == 7) {
if (mosquitto_username_pw_set(mosq, argv[5], argv[6]) !=
// Set username and password if not null in config
if (strlen(config.username) > 0 && strlen(config.password) > 0) {
if (mosquitto_username_pw_set(
mosq, config.username, config.password) !=
MOSQ_ERR_SUCCESS) {
flog(logfile, "Unable to set username and password\n");
goto terminate;
}
}

// Set TLS options if not null in config
if (strlen(config.ca_cert_path) > 0) {
if (mosquitto_tls_set(mosq,
config.ca_cert_path,
NULL,
config.cert_path,
config.key_path,
NULL) != MOSQ_ERR_SUCCESS) {
flog(logfile, "Unable to set TLS options\n");
goto terminate;
}
}

// MQTT protocol version
mosquitto_opts_set(
mosq, MOSQ_OPT_PROTOCOL_VERSION, &config.mqtt_protocol_version);

// Connect to the broker
rc = mosquitto_connect(mosq, config.host, config.port, 60);

// printf("Connecting to %s:%d\n", mqtt_host, mqtt_port);
if (rc) {
flog(logfile,
"Unable to connect to broker: %s\n",
mosquitto_strerror(rc));
goto terminate;
}

// Start the main loop
while (run) {
Expand Down

0 comments on commit c75090f

Please sign in to comment.