Extend platform for hostname resolution functionality

This commit is contained in:
Johannes Schriewer 2018-07-29 03:42:35 +02:00
parent 091e6bf4b9
commit 8938d6ae18
2 changed files with 44 additions and 3 deletions

View file

@ -1,14 +1,22 @@
#include<string.h>
#include<sys/socket.h>
#include<errno.h>
#include<netdb.h>
#include<arpa/inet.h>
#include <pthread.h>
#include <stdlib.h>
#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;
}

View file

@ -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
*