From db758cd938a3ee26f96377d304a3d7bc7f39fc26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ilja=20Karta=C5=A1ov?= Date: Wed, 17 Apr 2019 10:17:27 +0200 Subject: [PATCH] Correct names and fix declaration issues --- cStuff/string.c | 8 +- cStuff/string.h | 5 +- components/mail.c | 129 ++++++++++++----------- components/mail.h | 3 +- components/query.c | 106 +++++++++---------- components/query.h | 29 +++--- components/quick.c | 6 +- components/validate.c | 155 ++++++++++++++-------------- mods/ctx.c | 36 +++++++ mods/ctx.h | 48 +++++++++ mods/{mod-feedback.c => feedback.c} | 105 ++++++++++--------- mods/{mod-feedback.h => feedback.h} | 29 +++--- mods/interface.c | 75 -------------- mods/interface.h | 90 ---------------- mods/module.c | 54 ++++++++++ mods/module.h | 62 +++++++++++ 16 files changed, 493 insertions(+), 447 deletions(-) create mode 100644 mods/ctx.c create mode 100644 mods/ctx.h rename mods/{mod-feedback.c => feedback.c} (60%) rename mods/{mod-feedback.h => feedback.h} (76%) delete mode 100644 mods/interface.c delete mode 100644 mods/interface.h create mode 100644 mods/module.c create mode 100644 mods/module.h diff --git a/cStuff/string.c b/cStuff/string.c index 3aa1a91..446c8e1 100644 --- a/cStuff/string.c +++ b/cStuff/string.c @@ -13,6 +13,10 @@ * @see https://lowenware.com/ */ +#include +#include +#include +#include #include "string.h" @@ -86,13 +90,13 @@ cstuff_vsprintf(char **out, const char *format, va_list args) char tmp, *s; va_copy(vc, args); - result = vsprintf(&tmp, 1, format, vc); + result = vsnprintf(&tmp, 1, format, vc); va_end(vc); if (!(s = malloc(result + 1))) return -1; - result = vsprintf(s, result+1, format, args); + result = vsnprintf(s, result+1, format, args); return result; } diff --git a/cStuff/string.h b/cStuff/string.h index daffb55..a5cd9c7 100644 --- a/cStuff/string.h +++ b/cStuff/string.h @@ -16,7 +16,6 @@ #ifndef CSTUFF_STRING_H_938B242C_B750_40E9_8B67_A69F2F37EB87 #define CSTUFF_STRING_H_938B242C_B750_40E9_8B67_A69F2F37EB87 -#include #include #include "retcode.h" @@ -43,6 +42,10 @@ int cstuff_strset(char **out, const char *in); +int +cstuff_strnset(char **out, const char *in, int len); + + /** @brief Allocates memory and prints a formatted string into it * @param out a pointer to an address where new string must be stored * @param format a format for string (see manual for stdlib sprintf) diff --git a/components/mail.c b/components/mail.c index dc954c1..c054ae2 100644 --- a/components/mail.c +++ b/components/mail.c @@ -13,17 +13,19 @@ * @see https://lowenware.com/aisl/ */ +#include +#include #include #include #include "mail.h" -struct payload -{ +struct payload { char now[40]; char *to; char *from; char *subject; + char *msg_id; char *smtp_user; char *smtp_pass; char *smtp_url; @@ -41,12 +43,11 @@ payload_feed(void *ptr, size_t size, size_t nmemb, void *userp) left = payload->total - payload->offset; - if (left && (size = size*nmemb)) - { - cf (size > left) + if (left && (size = size*nmemb)) { + if (size > left) size = left; - memcpy(ptr, payload->data[payload->offset], size); + memcpy(ptr, &payload->data[payload->offset], size); payload->offset += size; return size; @@ -57,25 +58,23 @@ payload_feed(void *ptr, size_t size, size_t nmemb, void *userp) static void -payload__get_now(char * now, size_t size) +payload__get_now(char *now, size_t size) { - time_t now = time(NULL); + time_t time_now = time(NULL); struct tm tm; - gmtime_r(&now, &tm); + gmtime_r(&time_now, &tm); strftime(now, size, "%a, %d %b %Y %H:%M:%S GMT", &tm); } static char * -payload__get_id(uuid_t uuid, char * server) +payload__get_id(uuid_t uuid, const char *server) { size_t sz = strlen(server); + char *result = malloc( sz + 3 + 36 + 1 ); - char * result = malloc( sz + 3 + 36 + 1 ); - - if (result) - { + if (result) { char * p =result; *(p++) = '<'; uuid_unparse(uuid, p); @@ -91,8 +90,39 @@ payload__get_id(uuid_t uuid, char * server) } +static void +payload_free(struct payload *payload) +{ + if (payload->data) + free(payload->data); + + if (payload->to) + free(payload->to); + + if (payload->from) + free(payload->from); + + if (payload->subject) + free(payload->subject); + + if (payload->msg_id) + free(payload->msg_id); + + if (payload->smtp_user) + free(payload->smtp_user); + + if (payload->smtp_pass) + free(payload->smtp_pass); + + if (payload->smtp_url) + free(payload->smtp_url); + + free(payload); +} + + static struct payload * -payload_new(aislx_mail_t mail, const char * server) +payload_new(aislx_mail_t mail, const char *server) { size_t total; struct payload *payload; @@ -108,17 +138,17 @@ payload_new(aislx_mail_t mail, const char * server) if (cstuff_strcpy(&payload->from, mail->from) == -1) goto e_cleanup; - if ( !(payload->message_id = payload__get_id(mail->msg_id, server)) ) + if ( !(payload->msg_id = payload__get_id(mail->msg_id, server)) ) goto e_cleanup; total = cstuff_sprintf( &payload->data, AISLX_MAIL_FORMAT - , date - , payload->to - , payload->from - , mail->reply_to - , payload->message_id - , mail->subject - , mail->message ); + , payload->now + , payload->to + , payload->from + , mail->reply_to + , payload->msg_id + , mail->subject + , mail->msg ); if (!(total > 0)) goto e_cleanup; @@ -135,28 +165,16 @@ except: } -static void -payload_free(struct payload *payload) -{ - if (payload->data) - free(payload->data); - - free(payload); -} - - static void * -aislx_mail_execute(void * p_ctx) +aislx_mail_execute(void *p_ctx) { - void *result = NULL; struct payload * payload = (struct payload *)p_ctx; aisl_status_t status = AISL_MALLOC_ERROR; CURL *curl; struct curl_slist *rcpts; - if ((curl = curl_easy_init()) != NULL) - { + if ((curl = curl_easy_init()) != NULL) { curl_easy_setopt(curl, CURLOPT_USERNAME, payload->smtp_user); curl_easy_setopt(curl, CURLOPT_PASSWORD, payload->smtp_pass); curl_easy_setopt(curl, CURLOPT_URL, payload->smtp_url); @@ -169,18 +187,14 @@ aislx_mail_execute(void * p_ctx) curl_easy_setopt(curl, CURLOPT_MAIL_FROM, payload->from); - if ((rcpts = curl_slist_append(NULL, payload->to)) != NULL) - { + if ((rcpts = curl_slist_append(NULL, payload->to)) != NULL) { CURLcode res; - if ((res = curl_easy_perform(curl)) != CURLE_OK) - { + if ((res = curl_easy_perform(curl)) != CURLE_OK) { fprintf(stderr, "curl_easy_perform() failed: %s\n" , curl_easy_strerror(res)); status = AISL_INPUT_ERROR; - } - else - { + } else { status = AISL_SUCCESS; } curl_slist_free_all(rcpts); @@ -188,28 +202,28 @@ aislx_mail_execute(void * p_ctx) curl_easy_cleanup(curl); } - return result + (int)status; + return (void*)status; } aisl_status_t aislx_mail_send( aislx_mail_t mail, - const char *smtp_server, - const char *smtp_user, - const char *smtp_pass, - int flags ) + const char *smtp_server, + const char *smtp_user, + const char *smtp_pass, + int flags ) { - struct payload * payload; + int rc; + struct payload *payload; if (!(payload = payload_new(mail, smtp_server))) return AISL_MALLOC_ERROR; - if (pthread_create(&mail->thread, NULL, aislx_mail_execute, (void*)payload)) - { + rc = pthread_create(&mail->thread, NULL, aislx_mail_execute, (void*)payload); + if (rc) { payload_free(payload); return AISL_SYSCALL_ERROR; } - return AISL_SUCCESS; } @@ -222,13 +236,10 @@ aislx_mail_get_status(aislx_mail_t mail) rc = pthread_tryjoin_np(mail->thread, &retval); - if (rc == 0) - { - rc = (int)(retval-0); + if (rc == 0) { + rc = (aisl_status_t)retval; return rc; - } - else - { + } else { return AISL_IDLE; } } diff --git a/components/mail.h b/components/mail.h index 08836c7..d1e289b 100644 --- a/components/mail.h +++ b/components/mail.h @@ -39,8 +39,9 @@ struct aislx_mail { - pthread_t pthread; + pthread_t thread; const char *to; + const char *reply_to; const char *from; const char *subject; const char *msg; diff --git a/components/query.c b/components/query.c index a3426f1..4553695 100644 --- a/components/query.c +++ b/components/query.c @@ -13,14 +13,17 @@ * @see https://lowenware.com/aisl/ */ +#include +#include +#include #include "query.h" aisl_status_t aislx_query_init( aislx_query_t query, - size_t total, - aislx_query_on_var on_var, - void * p_ctx ) + size_t total, + aislx_query_on_var on_var, + void * p_ctx ) { query->on_var = on_var; query->p_ctx = p_ctx; @@ -43,13 +46,12 @@ aislx_query_release(aislx_query_t query) static void aislx_query_notify(const char * key, uint32_t k_len, - const cahr * val, uint32_t v_len, - aislx_query_t query) + const char * val, uint32_t v_len, + aislx_query_t query) { - if (query->on_var) - { + if (query->on_var) { if (query->on_var(key, k_len, val, v_len, query->p_ctx) != 0) - query->on_var == NULL; + query->on_var = NULL; } } @@ -57,55 +59,47 @@ aislx_query_notify(const char * key, uint32_t k_len, static int32_t aislx_query_process(aislx_query_t query, const char * data, int32_t length) { - const char * i = data, - * key = data, - * val = NULL; - - uint32_t k_len = 0; - + const char *i = data, + *key = data, + *val = NULL; + uint32_t k_len = 0; size_t result; - for (result=0; result < length; result++) - { + for (result=0; result < length; result++) { char c; - switch( (c = *i) ) - { - case '=': - if (val) - return -1; + switch( (c = *i) ) { + case '=': + if (val) + return -1; - k_len = i-key; - val = i+1; + k_len = i-key; + val = i+1; + break; + + case '&': + case ';': + if (!val) + return -1; + + if (!query->separator) + query->separator = c; + + if (query->separator == c) { + aislx_query_notify(key, k_len, val, i-val, query); + /* reset parser */ + key = (c == ';') ? i+2 : i+1; + k_len = 0; + val = NULL; break; - - case '&': - case ';' - if (!val) - return -1; - - if (!query->separator) - query->separator = c; - - if (query->separator == c) - { - aislx_query_notify(key, k_len, val, i-val, query); - /* reset parser */ - key = (c == ';') ? i+2 : i+1; - k_len = 0; - val = NULL; - break; - } + } } i++; } - if (query->total == result) - { + if (query->total == result) { aislx_query_notify(key, k_len, val, i-val, query); - } - else - { + } else { result = (key > i) ? i - data : key - data; } @@ -116,33 +110,31 @@ aislx_query_process(aislx_query_t query, const char * data, int32_t length) aisl_status_t -aislx_query_feed(aislx_query_t query, char * data, int32_t length) +aislx_query_feed(aislx_query_t query, const char *data, int32_t length) { size_t processed; - char * source; + const char *source; - if (query->data) - { + if (query->data) { if (!(source = realloc(query->data, length + query->size))) return AISL_MALLOC_ERROR; length += query->size; - query->data = source; + query->data = (char*)source; query->size = length; - memcpy(&source[query->size], data, length); - } - else + memcpy((char*)&source[query->size], data, length); + } else { source = data; + } processed = aislx_query_process(query, source, length); if (processed < 0) return AISL_INPUT_ERROR; - if (processed < length) - { - char * buffer; + if (processed < length) { + char *buffer; length -= processed; if (!(buffer = malloc(length))) diff --git a/components/query.h b/components/query.h index 503063e..5683025 100644 --- a/components/query.h +++ b/components/query.h @@ -17,33 +17,34 @@ #define AISLX_QUERY_H_9306EAEB_6DFC_4936_934A_6472F00E490C #include +#include +#include typedef int -(*aislx_query_on_var)( const char * key, uint32_t k_len, - const char * val, uint32_t v_len, - void * p_ctx ); +(*aislx_query_on_var)( const char *key, uint32_t k_len, + const char *val, uint32_t v_len, + void *p_ctx ); struct aislx_query { aislx_query_on_var on_var; - - void * p_ctx; - char * data; - size_t size; - size_t total; - char separator; + void *p_ctx; + char *data; + size_t size; + size_t total; + char separator; }; typedef struct aislx_query * aislx_query_t; aisl_status_t -aislx_query_init( aislx_query_t query, - size_t total, - aislx_query_on_var on_var, - void * p_ctx ); +aislx_query_init( aislx_query_t query, + size_t total, + aislx_query_on_var on_var, + void *p_ctx ); void @@ -51,7 +52,7 @@ aislx_query_release(aislx_query_t query); aisl_status_t -aislx_query_feed(aislx_query_t query, char * data, uint32_t length); +aislx_query_feed(aislx_query_t query, const char *data, int32_t length); #endif /* !AISLX_QUERY_H */ diff --git a/components/quick.c b/components/quick.c index 8f64fe5..eca5834 100644 --- a/components/quick.c +++ b/components/quick.c @@ -21,12 +21,12 @@ aislx_quick_response(aisl_stream_t stream, aisl_http_response_t http_response) { aisl_status_t result; - result = aisl_response(s, http_response, 0); + result = aisl_response(stream, http_response, 0); if (result == AISL_SUCCESS) - aisl_flush(s); + aisl_flush(stream); else - aisl_reject(s); + aisl_reject(stream); return result; } diff --git a/components/validate.c b/components/validate.c index b7a6f32..b140181 100644 --- a/components/validate.c +++ b/components/validate.c @@ -13,101 +13,96 @@ * @see https://lowenware.com/aisl/ */ +#include +#include +#include #include "validate.h" int -aislx_validate_email(const char * value) +aislx_validate_email(const char *value) { - const char * at = NULL, - * dot = NULL, - * i = value; - + const char *at = NULL, + *dot = NULL, + *i = value; char c; - while ( (c = *i) != 0 ) - { - switch(c) - { - case '@': - if (at || i == value) break; - - at = i++; - dot = NULL; - continue; - - case '"': - case '(': - case ')': - case ',': - case ';': - case ':': - case '<': - case '>': - case '[': - case '\\': - case ']': - case ' ': - /* These characters are allowed in double quotes only, but it is not - * implemented in this validator, so we count them as forbidden - */ + while ( (c = *i) != 0 ) { + switch(c) { + case '@': + if (at || i == value) break; - case '!': - case '#': - case '$': - case '%': - case '&': - case '\'': - case '*': - case '+': - case '/': - case '=': - case '?': - case '^': - case '_': - case '`': - case '{': - case '|': - case '}': - case '~': - /* Allowed special local-part characters */ - if (!at) - { - i++; - continue; - } - else - break; + at = i++; + dot = NULL; + continue; - case '-': - /* Allowed characters */ + case '"': + case '(': + case ')': + case ',': + case ';': + case ':': + case '<': + case '>': + case '[': + case '\\': + case ']': + case ' ': + /* These characters are allowed in double quotes only, but it is not + * implemented in this validator, so we count them as forbidden + */ + break; + + case '!': + case '#': + case '$': + case '%': + case '&': + case '\'': + case '*': + case '+': + case '/': + case '=': + case '?': + case '^': + case '_': + case '`': + case '{': + case '|': + case '}': + case '~': + /* Allowed special local-part characters */ + if (!at) { i++; continue; + } + break; - case '.': - if (dot && i == dot+1) - break; - - if (at) - { - if (i == at+1 || *(i+1) == 0) - break; - } - else - { - if (i == value || *(i+1) == '@') - break; - } - - dot = i++; - continue; - - default: - if ( isalnum(c) == 0 ) - continue; + case '-': + /* Allowed characters */ + i++; + continue; + case '.': + if (dot && i == dot+1) break; + + if (at) { + if (i == at+1 || *(i+1) == 0) + break; + } else { + if (i == value || *(i+1) == '@') + break; + } + + dot = i++; + continue; + + default: + if ( isalnum(c) == 0 ) + continue; + break; } return (int)(i-value)+1; } diff --git a/mods/ctx.c b/mods/ctx.c new file mode 100644 index 0000000..6c18f49 --- /dev/null +++ b/mods/ctx.c @@ -0,0 +1,36 @@ +/****************************************************************************** + * + * Copyright (c) 2017-2019 by Löwenware Ltd + * Please, refer LICENSE file for legal information + * + ******************************************************************************/ + +/** + * @file ctx.c + * @author Ilja Kartašov + * @brief + * + * @see https://lowenware.com/ + */ + +#include "ctx.h" + + +aislx_ctx_t +aislx_ctx_new(aislx_module_t mod) +{ + aislx_ctx_t ctx; + + if ((ctx = calloc(1, mod->ctx_size)) != NULL) { + ctx->mod = mod; + } + + return ctx; +} + + +void +aislx_ctx_free(aislx_ctx_t ctx) +{ + free(ctx); +} diff --git a/mods/ctx.h b/mods/ctx.h new file mode 100644 index 0000000..71be083 --- /dev/null +++ b/mods/ctx.h @@ -0,0 +1,48 @@ +/****************************************************************************** + * + * Copyright (c) 2017-2019 by Löwenware Ltd + * Please, refer LICENSE file for legal information + * + ******************************************************************************/ + +/** + * @file context.h + * @author Ilja Kartašov + * @brief + * + * @see https://lowenware.com/aisl/ + */ + +#ifndef AISLX_CTX_H_683B0A86_2A15_483D_B0F9_7BFBB4BA068F +#define AISLX_CTX_H_683B0A86_2A15_483D_B0F9_7BFBB4BA068F + +#define AISLX_CTX(x) ((aislx_ctx_t)x) + +#include + +/** @brief Root level ASIL mod's context structure + */ +struct aislx_ctx { + aislx_module_t mod; /**< Mod's pointer */ +}; + +/** @brief Root level ASIL mod's context structure pointer + */ +typedef struct aislx_ctx * aislx_ctx_t; + + +/** @brief Allocates zeroed #aislx_t context + * @param mod an instance of #aislx_t + * @return pointer to newly allocated #aisl_mox_ctx_t + */ +aislx_ctx_t +aislx_ctx_new(aislx_module_t mod); + + +/** @brief Frees previosly allocated #aislx_ctx_t + * @param ctx an instance of mod's context structure + * */ +void +aislx_ctx_free(aislx_ctx_t ctx); + +#endif /* !AISLX_CTX_H */ diff --git a/mods/mod-feedback.c b/mods/feedback.c similarity index 60% rename from mods/mod-feedback.c rename to mods/feedback.c index 412377e..0e0749f 100644 --- a/mods/mod-feedback.c +++ b/mods/feedback.c @@ -12,18 +12,21 @@ * * @see https://lowenware.com/aisl/ */ +#include +#include #include #include #include #include -#include "mod-feedback.h" +#include +#include "feedback.h" struct context { - struct aislx_mod_ctx root; - struct aislx_query qs; - struct aislx_mail mail; + struct aislx_ctx root; + struct aislx_query qs; + struct aislx_mail mail; char * email; char * msg; @@ -36,7 +39,6 @@ static void context_free(context_t ctx) { aislx_query_release(&ctx->qs); - aislx_mail_release(&ctx->mail); if (ctx->email) free(ctx->email); @@ -44,17 +46,17 @@ context_free(context_t ctx) if (ctx->msg) free(ctx->msg); - aislx_mod_ctx_free(ctx); + aislx_ctx_free((aislx_ctx_t)ctx); } static int -on_input_var( const char *key, int k_len, - const char *val, int v_len, - void *p_ctx ) +on_input_var( const char *key, uint32_t k_len, + const char *val, uint32_t v_len, + void *p_ctx ) { context_t ctx = (context_t) p_ctx; - aislx_mod_feedback_t mod = (aislx_mod_feedback_t)((aislx_mod_ctx_t)ctx)->mod; + aislx_feedback_t mod = (aislx_feedback_t)((aislx_ctx_t)ctx)->mod; if (!ctx->email && k_len == mod->name_email_length) { @@ -73,45 +75,42 @@ on_input_var( const char *key, int k_len, static aisl_status_t -on_stream_open(aislx_mod_feedback_t mod, aisl_evt_stream_open_t const evt) +on_stream_open(aislx_feedback_t mod, aisl_evt_stream_open_t const evt) { context_t ctx; aisl_stream_t s = (aisl_stream_t) (evt->evt.source); - if (!(ctx = (context_t)aislx_mod_ctx_new(mod))) + if (!(ctx = (context_t)aislx_ctx_new(AISLX_MODULE(mod)))) return AISL_MALLOC_ERROR; - if (aislx_mail_init(&ctx->mail) != 0) - goto except; - - ctx->mail.from = mod->mail_from; - ctx->mail.reply_to = mod->mail_reply_to; - ctx->mail.subject = mod->mail_subject; - ctx->mail.smtp_server = mod->smtp_server; - ctx->mail.smtp_user = mod->smtp_user; - ctx->mail.smtp_password = mod->smtp_password; - ctx->mail.smtp_port = mod->smtp_port; + ctx->mail.from = mod->mail_from; + ctx->mail.to = mod->mail_to; + ctx->mail.subject = mod->mail_subject; aisl_set_context(s, ctx); return AISL_SUCCESS; -except: - context_free(ctx); - return AISL_MALLOC_ERROR; + /* + except: + context_free(ctx); + return AISL_MALLOC_ERROR; + */ } static aisl_status_t -on_stream_header(aislx_mod_feedback_t mod, aisl_evt_stream_header_t const evt) +on_stream_header(aislx_feedback_t mod, aisl_evt_stream_header_t const evt) { + aisl_stream_t s = (aisl_stream_t)evt->evt.source; + context_t ctx = aisl_get_context(s); + if (strcmp(evt->key, "content-length")==0) { size_t total = strtoll(evt->value, NULL, 10); if(aislx_query_init(&ctx->qs, total, on_input_var, (void*)ctx) != 0) { - aisl_stream_t s = (aisl_stream_source_t)evt->source; aislx_quick_response(s, AISL_HTTP_INTERNAL_SERVER_ERROR); } } @@ -120,25 +119,23 @@ on_stream_header(aislx_mod_feedback_t mod, aisl_evt_stream_header_t const evt) static aisl_status_t -on_stream_input(aislx_mod_feedback_t mod, aisl_evt_stream_input_t const evt) +on_stream_input(aislx_feedback_t mod, aisl_evt_stream_input_t const evt) { context_t ctx = aisl_get_context((aisl_stream_t)evt->evt.source); - aislx_query_feed(ctx->qs, evt->data, evt->size); + aislx_query_feed(&ctx->qs, evt->data, evt->size); return AISL_SUCCESS; } static aisl_status_t -on_stream_request(aislx_mod_feedback_t mod, aisl_evt_t const evt) +on_stream_request(aislx_feedback_t mod, aisl_evt_t const evt) { - context_t ctx = aisl_get_context((aisl_stream_t)evt->evt.source); + aisl_status_t status; + context_t ctx = aisl_get_context((aisl_stream_t)evt->source); aisl_stream_t s = (aisl_stream_t)evt->source; - /* finalize input */ - aislx_query_flush(ctx->qs); - /* verify input */ if (!ctx->email || !ctx->msg || aislx_validate_email(ctx->email) != 0) aislx_quick_response(s, AISL_HTTP_BAD_REQUEST); @@ -148,7 +145,13 @@ on_stream_request(aislx_mod_feedback_t mod, aisl_evt_t const evt) uuid_generate(ctx->mail.msg_id); /* create thread */ - if (aislx_mail_send(&ctx->mail) != AISL_SUCCESS) + status = aislx_mail_send(&ctx->mail + , mod->smtp_host + , mod->smtp_user + , mod->smtp_pass + , 0); + + if (status != AISL_SUCCESS) aislx_quick_response(s, AISL_HTTP_INTERNAL_SERVER_ERROR); aisl_set_output_event(s, true); /**< enable output event */ @@ -158,9 +161,9 @@ on_stream_request(aislx_mod_feedback_t mod, aisl_evt_t const evt) static aisl_status_t -on_stream_output(aislx_mod_feedback_t mod, aisl_evt_t const evt) +on_stream_output(aislx_feedback_t mod, aisl_evt_t const evt) { - context_t ctx = aisl_get_context((aisl_stream_t)evt->evt.source); + context_t ctx = aisl_get_context((aisl_stream_t)evt->source); aisl_http_response_t rc = AISL_HTTP_OK; @@ -180,15 +183,15 @@ on_stream_output(aislx_mod_feedback_t mod, aisl_evt_t const evt) static aisl_status_t -on_stream_close(aislx_mod_feedback_t mod, aisl_evt_t const evt) +on_stream_close(aislx_feedback_t mod, aisl_evt_t const evt) { - context_free((context_t)aisl_get_context((aisl_stream_t)evt->evt.source)); + context_free((context_t)aisl_get_context((aisl_stream_t)evt->source)); return AISL_SUCCESS; } static aisl_status_t -aislx_mod_feedback_on_event(aislx_mod_feedback_t mod, aisl_evt_t const evt) +aislx_feedback_on_event(aislx_feedback_t mod, aisl_evt_t const evt) { switch(evt->code) { @@ -217,19 +220,19 @@ aislx_mod_feedback_on_event(aislx_mod_feedback_t mod, aisl_evt_t const evt) aisl_status_t -aislx_mod_feedback_init(aislx_mod_feedback_t mod, aislx_mod_feedback_cfg_t cfg) +aislx_feedback_init(aislx_feedback_t mod, aislx_feedback_cfg_t cfg) { - AISLX_MOD_INIT(aislx_mod_feedback, cfg->end_point); + AISLX_MODULE_INIT(aislx_feedback, cfg->end_point); mod->mail_subject = cfg->mail_subject; - mod->mail_from = cfg->mail_from; - mod->mail_reply_to = cfg->mail_reply_to; - mod->name_email = cfg->name_email; - mod->name_msg = cfg->name_msg; - mod->smtp_server = cfg->smtp_server; - mod->smtp_user = cfg->smtp_user; - mod->smtp_password = cfg->smtp_password; - mod->smtp_port = cfg->smtp_port; + mod->mail_from = cfg->mail_from; + mod->mail_to = cfg->mail_to; + mod->name_email = cfg->name_email; + mod->name_msg = cfg->name_msg; + mod->smtp_host = cfg->smtp_host; + mod->smtp_user = cfg->smtp_user; + mod->smtp_pass = cfg->smtp_pass; + mod->smtp_port = cfg->smtp_port; mod->name_email_length = strlen(cfg->name_email); mod->name_msg_length = strlen(cfg->name_msg); @@ -239,7 +242,7 @@ aislx_mod_feedback_init(aislx_mod_feedback_t mod, aislx_mod_feedback_cfg_t cfg) void -aislx_mod_feedback_release(aislx_mod_feedback_t mod) +aislx_feedback_release(aislx_feedback_t mod) { } diff --git a/mods/mod-feedback.h b/mods/feedback.h similarity index 76% rename from mods/mod-feedback.h rename to mods/feedback.h index 4cd8338..dd1576d 100644 --- a/mods/mod-feedback.h +++ b/mods/feedback.h @@ -16,34 +16,35 @@ #ifndef AISLX_MOD_FEEDBACK_H_6CC516E4_A7F2_4A9D_B467_75DCF6F58108 #define AISLX_MOD_FEEDBACK_H_6CC516E4_A7F2_4A9D_B467_75DCF6F58108 -#include +#include +#include - -struct aislx_mod_feedback_cfg +struct aislx_feedback_cfg { const char *end_point; + const char *name_email; + const char *name_msg; const char *mail_subject; const char *mail_from; const char *mail_to; - const char *name_email; - const char *name_msg; const char *smtp_host; const char *smtp_user; const char *smtp_pass; uint16_t smtp_port; }; -typedef struct aislx_mod_feedback_cfg * aislx_mod_feedback_cfg_t; +typedef struct aislx_feedback_cfg * aislx_feedback_cfg_t; -struct aislx_mod_feedback +struct aislx_feedback { - struct aislx_mod root; + struct aislx_module root; - const char *mail_subject; - const char *mail_from; const char *name_email; const char *name_msg; + const char *mail_subject; + const char *mail_from; + const char *mail_to; const char *smtp_host; const char *smtp_user; const char *smtp_pass; @@ -54,15 +55,15 @@ struct aislx_mod_feedback }; -typedef struct aislx_mod_feedback * aislx_mod_feedback_t; +typedef struct aislx_feedback * aislx_feedback_t; aisl_status_t -aislx_mod_feedback_init(aislx_mod_feedback_t mod, - aislx_mod_feedback_cfg_t cfg); +aislx_feedback_init(aislx_feedback_t mod, + aislx_feedback_cfg_t cfg); void -aislx_mod_feedback_release(aislx_mod_feedback_t mod); +aislx_feedback_release(aislx_feedback_t mod); #endif /* !AISLX_MOD_FEEDBACK_H */ diff --git a/mods/interface.c b/mods/interface.c deleted file mode 100644 index ed1313d..0000000 --- a/mods/interface.c +++ /dev/null @@ -1,75 +0,0 @@ -/****************************************************************************** - * - * Copyright (c) 2017-2019 by Löwenware Ltd - * Please, refer LICENSE file for legal information - * - ******************************************************************************/ - -/** - * @file interface.c - * @author Ilja Kartašov - * @brief Interface source file for AISL SDK modules - * - * @see https://lowenware.com/ - */ -#include -#include -#include "interface.h" - - -aisl_mod_ctx_t -aisl_mod_ctx_new(aislx_mod_t mod) -{ - aisl_mod_ctx_t ctx; - - if ((ctx = calloc(1, mod->ctx_size)) != NULL) - { - ctx->mod = mod; - } - - return ctx; -} - - -void -aislx_mod_ctx_free(aislx_mod_ctx_t ctx) -{ - free(ctx); -} - - -aisl_status_t -aislx_mod_on_event(aislx_mod_t mod, aisl_evt_t const evt) -{ - switch(evt->code) - { - case AISL_EVENT_STREAM_OPEN: - { - aisl_evt_stream_open_t so_evt = (aisl_evt_stream_open_t)evt; - if ( strncmp(so_evt->path, mod->end_point, mod->ep_length) != 0) - return AISL_IDLE; - - break; - } - - case AISL_EVENT_STREAM_HEADER: - case AISL_EVENT_STREAM_INPUT: - case AISL_EVENT_STREAM_REQUEST: - case AISL_EVENT_STREAM_OUTPUT: - case AISL_EVENT_STREAM_CLOSE: - { - aislx_mod_ctx_t ctx = aisl_get_context((aisl_stream_t)evt->source); - - if (!ctx) - return AISL_SUCCESS; - - if (ctx->mod == mod) - break; - } - - default: - return AISL_IDLE; - } - - return mod->on_event(mod, evt); -} diff --git a/mods/interface.h b/mods/interface.h deleted file mode 100644 index 42020b0..0000000 --- a/mods/interface.h +++ /dev/null @@ -1,90 +0,0 @@ -/****************************************************************************** - * - * Copyright (c) 2017-2019 by Löwenware Ltd - * Please, refer LICENSE file for legal information - * - ******************************************************************************/ - -/** - * @file interface.h - * @author Ilja Kartašov - * @brief Interface header file for AISL SDK modules - * - * @see https://lowenware.com/aisl/ - */ - -#ifndef AISL_SDK_MODS_INTERFACE_H_1AEFECA5_9341_431B_8194_EA0C2E76B5EA -#define AISL_SDK_MODS_INTERFACE_H_1AEFECA5_9341_431B_8194_EA0C2E76B5EA - -#include - - -#define AISLX_MOD_INIT(MOD, END_POINT) \ - do { \ - ((aislx_mod_t) mod)->end_point = END_POINT; \ - ((aislx_mod_t) mod)->ep_length = strlen(END_POINT); \ - ((aislx_mod_t) mod)->on_stream_event = MOD##_on_event; \ - ((aislx_mod_t) mod)->ctx_size = sizeof(struct context); \ - } while(0) \ - - -/** @brief AISL mod structure pointer - */ -typedef struct aislx_mod * aislx_mod_t; - - -/** @brief Pointer to AISL stream event observer - */ -typedef aisl_status_t -(* aislx_mod_observer_t)(aislx_mod_t mod, aisl_evt_t const evt); - - -/** @brief Root level AISL mod's structure - */ -struct aislx_mod -{ - const char * end_point; /**< Root mod's URL or NULL */ - aislx_mod_observer_t on_event; /**< Mod's stream event observer */ - size_t ctx_size; /**< Mod's context size */ - uint8_t ep_length; /**< Mod's context size */ -}; - - -/** @brief Root level ASIL mod's context structure - */ -struct aislx_mod_ctx -{ - aislx_mod_t mod; /**< Mod's pointer */ -}; - -/** @brief Root level ASIL mod's context structure pointer - */ -typedef struct aislx_mod_ctx * aislx_mod_ctx_t; - - -/** @brief Allocates zeroed #aislx_mod_t context - * @param mod an instance of #aislx_mod_t - * @return pointer to newly allocated #aisl_mox_ctx_t - */ -aislx_mod_ctx_t -aislx_mod_ctx_new(aislx_mod_t mod); - - -/** @brief Frees previosly allocated #aislx_mod_ctx_t - * @param ctx an instance of mod's context structure - * */ -void -aislx_mod_ctx_free(aislx_mod_ctx_t ctx); - - -/** @brief Wrapper function that calles mod's observer - * @param mod an instance of #aislx_mod_t - * @param evt an AISL stream event - * @return AISL_SUCCESS if event handled, AISL_IDLE if event not handled or an - * error code - */ -aisl_status_t -aislx_mod_on_event(aislx_mod_t mod, aisl_evt_t const evt); - - -#endif /* !AISL_SDK_MODS_INTERFACE_H */ diff --git a/mods/module.c b/mods/module.c new file mode 100644 index 0000000..8703a58 --- /dev/null +++ b/mods/module.c @@ -0,0 +1,54 @@ +/****************************************************************************** + * + * Copyright (c) 2017-2019 by Löwenware Ltd + * Please, refer LICENSE file for legal information + * + ******************************************************************************/ + +/** + * @file interface.c + * @author Ilja Kartašov + * @brief Interface source file for AISL SDK modules + * + * @see https://lowenware.com/ + */ +#include +#include +#include "module.h" +#include "ctx.h" + + +aisl_status_t +aislx_module_on_event(aislx_module_t mod, aisl_evt_t const evt) +{ + aislx_ctx_t ctx; + aisl_evt_stream_open_t so_evt; + + switch(evt->code) + { + case AISL_EVENT_STREAM_OPEN: + so_evt = (aisl_evt_stream_open_t)evt; + + if (strncmp(so_evt->path, mod->end_point, mod->ep_length)) + return AISL_IDLE; + break; + + case AISL_EVENT_STREAM_HEADER: + case AISL_EVENT_STREAM_INPUT: + case AISL_EVENT_STREAM_REQUEST: + case AISL_EVENT_STREAM_OUTPUT: + case AISL_EVENT_STREAM_CLOSE: + if (!(ctx = aisl_get_context((aisl_stream_t)evt->source))) + return AISL_SUCCESS; + + if (ctx->mod == mod) + break; + + /* go through default */ + + default: + return AISL_IDLE; + } + + return mod->on_event(mod, evt); +} diff --git a/mods/module.h b/mods/module.h new file mode 100644 index 0000000..277cf7b --- /dev/null +++ b/mods/module.h @@ -0,0 +1,62 @@ +/****************************************************************************** + * + * Copyright (c) 2017-2019 by Löwenware Ltd + * Please, refer LICENSE file for legal information + * + ******************************************************************************/ + +/** + * @file interface.h + * @author Ilja Kartašov + * @brief Interface header file for AISL SDK modules + * + * @see https://lowenware.com/aisl/ + */ + +#ifndef AISLX_MODULE_H_1AEFECA5_9341_431B_8194_EA0C2E76B5EA +#define AISLX_MODULE_H_1AEFECA5_9341_431B_8194_EA0C2E76B5EA + +#include + +#define AISLX_MODULE_INIT(MOD, END_POINT) \ + do { \ + ((aislx_module_t) mod)->end_point = END_POINT; \ + ((aislx_module_t) mod)->ep_length = strlen(END_POINT); \ + ((aislx_module_t) mod)->on_event = (aislx_observer_t)MOD##_on_event; \ + ((aislx_module_t) mod)->ctx_size = sizeof(struct context); \ + } while(0) \ + +#define AISLX_MODULE(x) ((aislx_module_t)x) + +/** @brief AISL module structure pointer + */ +typedef struct aislx_module * aislx_module_t; + + +/** @brief Pointer to AISL stream event observer + */ +typedef aisl_status_t +(* aislx_observer_t)(aislx_module_t mod, aisl_evt_t const evt); + + + +/** @brief Root level AISL mod's structure + */ +struct aislx_module { + const char *end_point; /**< Root mod's URL or NULL */ + aislx_observer_t on_event; /**< Mod's stream event observer */ + size_t ctx_size; /**< Mod's context size */ + uint8_t ep_length; /**< End-Point length */ +}; + + +/** @brief Wrapper function that calles mod's observer + * @param mod an instance of #aislx_t + * @param evt an AISL stream event + * @return AISL_SUCCESS if event handled, AISL_IDLE if event not handled or an + * error code + */ +aisl_status_t +aislx_module_on_event(aislx_module_t mod, aisl_evt_t const evt); + +#endif /* !AISLX_MODULE;_H */