From 8938d6ae186d5f839b486dc208a6a26ec42ed5ab Mon Sep 17 00:00:00 2001 From: Johannes Schriewer Date: Sun, 29 Jul 2018 03:42:35 +0200 Subject: [PATCH] Extend platform for hostname resolution functionality --- platform/linux.c | 41 ++++++++++++++++++++++++++++++++++++++--- platform/platform.h | 6 ++++++ 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/platform/linux.c b/platform/linux.c index a8371ed..68aa1cb 100644 --- a/platform/linux.c +++ b/platform/linux.c @@ -1,14 +1,22 @@ +#include +#include +#include +#include +#include + #include #include #include "platform.h" +const size_t max_receive_buffer_size = 4 * 4096; // 16 KB + struct _PlatformData { pthread_t read_thread; }; void initialize_platform(MQTTHandle *handle) { - handle->platform = calloc(sizeof(struct _PlatformData), 1); + handle->platform = calloc(1, sizeof(struct _PlatformData)); } MQTTStatus run_read_task(MQTTHandle *handle, Reader reader) { @@ -20,11 +28,38 @@ MQTTStatus run_read_task(MQTTHandle *handle, Reader reader) { } MQTTStatus join_read_task(MQTTHandle *handle) { - pthread_join(handle->platform->read_thread, NULL); - + if (handle->platform->read_thread) { + pthread_join(handle->platform->read_thread, NULL); + handle->platform->read_thread = 0; + } return MQTT_STATUS_OK; } void release_platform(MQTTHandle *handle) { free(handle->platform); } + + +bool hostname_to_ip(char *hostname , char *ip) { + struct addrinfo hints, *servinfo; + struct sockaddr_in *h; + + memset(&hints, 0, sizeof(struct addrinfo)); + hints.ai_family = AF_UNSPEC; // use AF_INET6 to force IPv6 + hints.ai_socktype = SOCK_STREAM; + + int ret = getaddrinfo(hostname, NULL, &hints, &servinfo); + if (ret != 0) { + // fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv)); + return false; + } + + // loop through all the results and connect to the first we can + for(struct addrinfo *p = servinfo; p != NULL; p = p->ai_next) { + h = (struct sockaddr_in *)p->ai_addr; + strcpy(ip , inet_ntoa( h->sin_addr ) ); + } + + freeaddrinfo(servinfo); // all done with this structure + return true; +} diff --git a/platform/platform.h b/platform/platform.h index 58b4577..e70e5e3 100644 --- a/platform/platform.h +++ b/platform/platform.h @@ -5,6 +5,12 @@ typedef void (*Reader)(MQTTHandle *handle); +/** maximum receiver buffer size, defined by platform */ +extern const size_t max_receive_buffer_size; + + +bool hostname_to_ip(char *hostname, char *ip); + /** * Initialize platform specific data *