Skip to content

Commit

Permalink
Merge pull request #164 from NoelGraf/fix_conversion_errors
Browse files Browse the repository at this point in the history
Fixed some conversion errors that occur when the -Wc++-compat compiler flag is enabled
  • Loading branch information
LiamBindle authored May 24, 2022
2 parents 2a4052a + ad25f16 commit 99903af
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 135 deletions.
12 changes: 10 additions & 2 deletions examples/templates/mbedtls_sockets.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ struct mbedtls_context {
mbedtls_ctr_drbg_context ctr_drbg;
};

void failed(const char *fn, int rv);
void cert_verify_failed(uint32_t rv);
void open_nb_socket(struct mbedtls_context *ctx,
const char *hostname,
const char *port,
const char *ca_file);


void failed(const char *fn, int rv) {
char buf[100];
mbedtls_strerror(rv, buf, sizeof(buf));
Expand Down Expand Up @@ -124,7 +132,7 @@ void open_nb_socket(struct mbedtls_context *ctx,
} else {
break;
}
rv = mbedtls_net_poll(net_ctx, want, -1);
rv = mbedtls_net_poll(net_ctx, want, (uint32_t)-1);
if (rv < 0) {
failed("mbedtls_net_poll", rv);
}
Expand All @@ -135,7 +143,7 @@ void open_nb_socket(struct mbedtls_context *ctx,
uint32_t result = mbedtls_ssl_get_verify_result(ssl_ctx);
if (result != 0) {
if (result == (uint32_t)-1) {
failed("mbedtls_ssl_get_verify_result", result);
failed("mbedtls_ssl_get_verify_result", (int)result);
} else {
cert_verify_failed(result);
}
Expand Down
29 changes: 24 additions & 5 deletions examples/templates/openssl_sockets.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,20 @@
#include <openssl/ssl.h>
#include <openssl/err.h>

#include <string.h>

/*
A template for opening a non-blocking OpenSSL connection.
*/
void open_nb_socket(BIO** bio,
SSL_CTX** ssl_ctx,
const char* addr,
const char* port,
const char* ca_file,
const char* ca_path,
const char* cert_file,
const char* key_file);

void open_nb_socket(BIO** bio,
SSL_CTX** ssl_ctx,
const char* addr,
Expand Down Expand Up @@ -42,18 +53,26 @@ void open_nb_socket(BIO** bio,
}

/* open BIO socket */
char * addr_copy = (char*)malloc(strlen(addr) + 1);
strcpy(addr_copy,addr);
char * port_copy = (char*)malloc(strlen(port) + 1);
strcpy(port_copy,port);

*bio = BIO_new_ssl_connect(*ssl_ctx);
BIO_get_ssl(*bio, &ssl);
SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
BIO_set_conn_hostname(*bio, addr);
BIO_set_conn_hostname(*bio, addr_copy);
BIO_set_nbio(*bio, 1);
BIO_set_conn_port(*bio, port);
BIO_set_conn_port(*bio, port_copy);

free(addr_copy);
free(port_copy);

/* wait for connect with 10 second timeout */
int start_time = time(NULL);
int do_connect_rv = BIO_do_connect(*bio);
int start_time = (int)time(NULL);
int do_connect_rv = (int)BIO_do_connect(*bio);
while(do_connect_rv <= 0 && BIO_should_retry(*bio) && (int)time(NULL) - start_time < 10) {
do_connect_rv = BIO_do_connect(*bio);
do_connect_rv = (int)BIO_do_connect(*bio);
}
if (do_connect_rv <= 0) {
printf("error: %s\n", ERR_reason_error_string(ERR_get_error()));
Expand Down
2 changes: 2 additions & 0 deletions examples/templates/posix_sockets.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
/*
A template for opening a non-blocking POSIX socket.
*/
int open_nb_socket(const char* addr, const char* port);

int open_nb_socket(const char* addr, const char* port) {
struct addrinfo hints = {0};

Expand Down
2 changes: 1 addition & 1 deletion include/mqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ uint16_t __mqtt_unpack_uint16(const uint8_t *buf);
ssize_t __mqtt_pack_str(uint8_t *buf, const char* str);

/** @brief A macro to get the MQTT string length from a c-string. */
#define __mqtt_packed_cstrlen(x) (2 + strlen(x))
#define __mqtt_packed_cstrlen(x) (2 + (unsigned int)strlen(x))

/* RESPONSES */

Expand Down
Loading

0 comments on commit 99903af

Please sign in to comment.