Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event callback system #149

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 22 additions & 22 deletions examples/bearssl_publisher.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ int testCerts(br_x509_trust_anchor *anch);

/**
* @brief The function that would be called whenever a PUBLISH is received.
*
* @note This function is not used in this example.
*
* @note This function is not used in this example.
*/
static void publish_callback(void** unused, struct mqtt_response_publish *published);

/**
* @brief Safely closes the socket in \p ctx before \c exit.
* @brief Safely closes the socket in \p ctx before \c exit.
*/
static void exit_example(int status, bearssl_context *ctx);

/* The next five functions decode a certificate into BearSSL format */

/**
* @brief Callback function to accumulate data in a buffer
*/
*/
static void vblob_append(void *cc, const void *data, size_t len);

/**
Expand All @@ -54,14 +54,14 @@ static int certificate_to_trust_anchor(br_x509_certificate *xc, br_x509_trust_an
/**
* @brief Generates trust anchors for BearSSL from the contents of \p ca_file and stores them
* in the \p anchoOut array (based on code in BearSSL tools)
*/
*/
static size_t get_trusted_anchors_from_file(const char *ca_file, br_x509_trust_anchor **anchOut);
/**
* @brief Generates trust anchors for BearSSL from the string \p ca and stores them
* in the \p anchOut array (based on code in BearSSL tools)
*
*
* @returns The number of trust anchors generated
*/
*/
static size_t get_trusted_anchors(const unsigned char *ca, size_t ca_len, br_x509_trust_anchor **anchOut);

// Global to return Ctrl-C event
Expand All @@ -76,9 +76,9 @@ void signalHandler(int signum) {
}

/**
* A simple program to that publishes the current time until Ctrl-C is pressed.
* A simple program to that publishes the current time until Ctrl-C is pressed.
*/
int main(int argc, const char *argv[])
int main(int argc, const char *argv[])
{
const char* addr;
const char* port;
Expand Down Expand Up @@ -126,20 +126,20 @@ int main(int argc, const char *argv[])

/* generate BearSSL trusted anchors - specifically kept out of open_nb_socket since it needs to malloc */

/*
Generate BearSSL trusted anchors
/*
Generate BearSSL trusted anchors

This code converts the certificate into a format that is readable by the BearSSL library. Sadly there isn't
This code converts the certificate into a format that is readable by the BearSSL library. Sadly there isn't
a way to accomplish this without the use of malloc thus I specifically kept this code out of open_nb_socket.
The author of the bearSSL library offers two options:

1) Do the conversion of the certificate in your code. There are examples of how to do this. The benefit of
1) Do the conversion of the certificate in your code. There are examples of how to do this. The benefit of
this is that you can run the same code against different servers by providing the appropriate trusted root
pem file. The function get_trusted_anchors does exactly this.

2) Use the tool provided with BearSSL to generate the C code that will initialize the trusted anchor structures.
Essentially it simply generates initialized C structures that you can copy into your code. You will not need
to use malloc but you will lose some flexibility. For information on the tool see
Essentially it simply generates initialized C structures that you can copy into your code. You will not need
to use malloc but you will lose some flexibility. For information on the tool see
this page: https://www.bearssl.org/api1.html
*/
ctx.ta_count = get_trusted_anchors_from_file(ca_file, &ctx.anchOut);
Expand Down Expand Up @@ -196,7 +196,7 @@ int main(int argc, const char *argv[])
exit_example(EXIT_FAILURE, &ctx);
}
close_socket(&ctx);

if (0 != open_nb_socket(&ctx, addr, port, bearssl_iobuf, sizeof(bearssl_iobuf)))
{
fprintf(stderr, "Unable to open socket: %d\n", errno);
Expand Down Expand Up @@ -249,7 +249,7 @@ int main(int argc, const char *argv[])
return 4;
}
usleep(100000U);
}
}

/* disconnect */
printf("\n%s disconnecting from %s\n", argv[0], addr);
Expand All @@ -266,7 +266,7 @@ int main(int argc, const char *argv[])

sleep(1);

/* exit */
/* exit */
exit_example(EXIT_SUCCESS, &ctx);
}

Expand All @@ -276,7 +276,7 @@ static void exit_example(int status, bearssl_context *ctx)
exit(status);
}

static void publish_callback(void** unused, struct mqtt_response_publish *published)
static void publish_callback(void** unused, struct mqtt_response_publish *published)
{
static const char *prelim = "Received publish('";
/* note that published->topic_name is NOT null-terminated (here we'll change it to a c-string) */
Expand All @@ -302,7 +302,7 @@ static void vblob_append(void *cc, const void *data, size_t len)
static void free_ta_contents(br_x509_trust_anchor *ta)
{
free(ta->dn.data);
switch (ta->pkey.key_type)
switch (ta->pkey.key_type)
{
case BR_KEYTYPE_RSA:
free(ta->pkey.key.rsa.n);
Expand Down Expand Up @@ -342,7 +342,7 @@ static int certificate_to_trust_anchor(br_x509_certificate *xc, br_x509_trust_an
ta->dn.len = vdn.data_length;
ta->flags = 0;

if (br_x509_decoder_isCA(&dc))
if (br_x509_decoder_isCA(&dc))
{
ta->flags |= BR_X509_TA_CA;
}
Expand Down Expand Up @@ -403,7 +403,7 @@ static size_t get_trusted_anchors_from_file(const char *ca_file, br_x509_trust_a

if (certs != NULL) {
size_t read = fread(certs, 1, fsize, f);

fclose(f);

if (read == fsize) {
Expand Down
28 changes: 14 additions & 14 deletions examples/bio_publisher.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

/**
* @file
* A simple program to that publishes the current time whenever ENTER is pressed.
* A simple program to that publishes the current time whenever ENTER is pressed.
*/
#include <unistd.h>
#include <stdlib.h>
Expand All @@ -14,30 +14,30 @@

/**
* @brief The function that would be called whenever a PUBLISH is received.
*
* @note This function is not used in this example.
*
* @note This function is not used in this example.
*/
void publish_callback(void** unused, struct mqtt_response_publish *published);

/**
* @brief The client's refresher. This function triggers back-end routines to
* @brief The client's refresher. This function triggers back-end routines to
* handle ingress/egress traffic to the broker.
*
* @note All this function needs to do is call \ref __mqtt_recv and
* \ref __mqtt_send every so often. I've picked 100 ms meaning that
*
* @note All this function needs to do is call \ref __mqtt_recv and
* \ref __mqtt_send every so often. I've picked 100 ms meaning that
* client ingress/egress traffic will be handled every 100 ms.
*/
void* client_refresher(void* client);

/**
* @brief Safelty closes the \p sockfd and cancels the \p client_daemon before \c exit.
* @brief Safelty closes the \p sockfd and cancels the \p client_daemon before \c exit.
*/
void exit_example(int status, BIO* sockfd, pthread_t *client_daemon);

/**
* A simple program to that publishes the current time whenever ENTER is pressed.
* A simple program to that publishes the current time whenever ENTER is pressed.
*/
int main(int argc, const char *argv[])
int main(int argc, const char *argv[])
{
const char* addr;
const char* port;
Expand Down Expand Up @@ -122,13 +122,13 @@ int main(int argc, const char *argv[])
fprintf(stderr, "error: %s\n", mqtt_error_str(client.error));
exit_example(EXIT_FAILURE, sockfd, &client_daemon);
}
}
}

/* disconnect */
printf("\n%s disconnecting from %s\n", argv[0], addr);
sleep(1);

/* exit */
/* exit */
exit_example(EXIT_SUCCESS, sockfd, &client_daemon);
}

Expand All @@ -141,14 +141,14 @@ void exit_example(int status, BIO* sockfd, pthread_t *client_daemon)



void publish_callback(void** unused, struct mqtt_response_publish *published)
void publish_callback(void** unused, struct mqtt_response_publish *published)
{
/* not used in this example */
}

void* client_refresher(void* client)
{
while(1)
while(1)
{
mqtt_sync((struct mqtt_client*) client);
usleep(100000U);
Expand Down
26 changes: 13 additions & 13 deletions examples/bio_publisher_win.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

/**
* @file
* A simple program to that publishes the current time whenever ENTER is pressed.
* A simple program to that publishes the current time whenever ENTER is pressed.
*/
#include <process.h>
#include <stdlib.h>
Expand All @@ -13,30 +13,30 @@

/**
* @brief The function that would be called whenever a PUBLISH is received.
*
* @note This function is not used in this example.
*
* @note This function is not used in this example.
*/
void publish_callback(void** unused, struct mqtt_response_publish *published);

/**
* @brief The client's refresher. This function triggers back-end routines to
* @brief The client's refresher. This function triggers back-end routines to
* handle ingress/egress traffic to the broker.
*
* @note All this function needs to do is call \ref __mqtt_recv and
* \ref __mqtt_send every so often. I've picked 100 ms meaning that
*
* @note All this function needs to do is call \ref __mqtt_recv and
* \ref __mqtt_send every so often. I've picked 100 ms meaning that
* client ingress/egress traffic will be handled every 100 ms.
*/
void client_refresher(void* client);

/**
* @brief Safelty closes the \p sockfd and cancels the \p client_daemon before \c exit.
* @brief Safelty closes the \p sockfd and cancels the \p client_daemon before \c exit.
*/
void exit_example(int status, BIO* sockfd);

/**
* A simple program to that publishes the current time whenever ENTER is pressed.
* A simple program to that publishes the current time whenever ENTER is pressed.
*/
int main(int argc, const char *argv[])
int main(int argc, const char *argv[])
{
const char* addr;
const char* port;
Expand Down Expand Up @@ -120,13 +120,13 @@ int main(int argc, const char *argv[])
fprintf(stderr, "\nerror: %s\n", mqtt_error_str(client.error));
exit_example(EXIT_FAILURE, sockfd);
}
}
}

/* disconnect */
printf("\n%s disconnecting from %s\n", argv[0], addr);
Sleep(1000);

/* exit */
/* exit */
exit_example(EXIT_SUCCESS, sockfd);
}

Expand All @@ -138,7 +138,7 @@ void exit_example(int status, BIO* sockfd)



void publish_callback(void** unused, struct mqtt_response_publish *published)
void publish_callback(void** unused, struct mqtt_response_publish *published)
{
/* not used in this example */
}
Expand Down
26 changes: 13 additions & 13 deletions examples/mbedtls_publisher.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,30 @@

/**
* @brief The function that would be called whenever a PUBLISH is received.
*
* @note This function is not used in this example.
*
* @note This function is not used in this example.
*/
void publish_callback(void** unused, struct mqtt_response_publish *published);

/**
* @brief The client's refresher. This function triggers back-end routines to
* @brief The client's refresher. This function triggers back-end routines to
* handle ingress/egress traffic to the broker.
*
* @note All this function needs to do is call \ref __mqtt_recv and
* \ref __mqtt_send every so often. I've picked 100 ms meaning that
*
* @note All this function needs to do is call \ref __mqtt_recv and
* \ref __mqtt_send every so often. I've picked 100 ms meaning that
* client ingress/egress traffic will be handled every 100 ms.
*/
void* client_refresher(void* client);

/**
* @brief Safelty closes the \p sockfd and cancels the \p client_daemon before \c exit.
* @brief Safelty closes the \p sockfd and cancels the \p client_daemon before \c exit.
*/
void exit_example(int status, mqtt_pal_socket_handle sockfd, pthread_t *client_daemon);

/**
* A simple program to that publishes the current time whenever ENTER is pressed.
* A simple program to that publishes the current time whenever ENTER is pressed.
*/
int main(int argc, const char *argv[])
int main(int argc, const char *argv[])
{
const char* addr;
const char* port;
Expand Down Expand Up @@ -127,13 +127,13 @@ int main(int argc, const char *argv[])
fprintf(stderr, "error: %s\n", mqtt_error_str(client.error));
exit_example(EXIT_FAILURE, sockfd, &client_daemon);
}
}
}

/* disconnect */
printf("\n%s disconnecting from %s\n", argv[0], addr);
sleep(1);

/* exit */
/* exit */
exit_example(EXIT_SUCCESS, sockfd, &client_daemon);
}

Expand All @@ -147,14 +147,14 @@ void exit_example(int status, mqtt_pal_socket_handle sockfd, pthread_t *client_d



void publish_callback(void** unused, struct mqtt_response_publish *published)
void publish_callback(void** unused, struct mqtt_response_publish *published)
{
/* not used in this example */
}

void* client_refresher(void* client)
{
while(1)
while(1)
{
mqtt_sync((struct mqtt_client*) client);
usleep(100000U);
Expand Down
Loading