From: Marcel Poul Date: Sat, 15 Oct 2011 08:57:35 +0000 (+0000) Subject: First import of canl src files. Main features X-Git-Tag: before_mechglue_support~3 X-Git-Url: http://scientific.zcu.cz/git/?a=commitdiff_plain;h=7d3f732cac657095fc18b4b06b4e0d0d20db383d;p=jra1mw.git First import of canl src files. Main features --- diff --git a/emi.canl.canl-c/src/canl.c b/emi.canl.canl-c/src/canl.c new file mode 100644 index 0000000..f3681ee --- /dev/null +++ b/emi.canl.canl-c/src/canl.c @@ -0,0 +1,250 @@ +#include +#include +#include "canl.h" +#include "canl_locl.h" + +canl_ctx canl_create_ctx() +{ + struct glb_ctx *new_ctx = NULL; + int err = 0; + + /*create context*/ + new_ctx = (struct glb_ctx *) malloc(sizeof(*new_ctx)); + if (!new_ctx) { + err=1; //use errno instead + //set_error(ctx->err_msg); + goto end; + } + + /*openssl init. -check return value + ssl_library_init(); + ssl_load_error_strings(); + canl_ctx->ssl_ctx->ssl_meth = ;//choose ssl method SSLv3_method(); + canl_ctx->ssl_ctx = SSL_CTX_new (canl_ctx->ssl_ct->ssl_meth) + */ + + /*initial values ...*/ + new_ctx->io_ctx = NULL; + new_ctx->err_msg = NULL; +end: + return new_ctx; + +} + +void canl_free_ctx(canl_ctx cc) +{ + struct glb_ctx *ctx = (struct glb_ctx*) cc; + + if (!cc) { + goto end; + } + + + /*delete content*/ + if (ctx->io_ctx) { + canl_io_destroy(ctx, ctx->io_ctx); + ctx->io_ctx = NULL; + } + + free(ctx); + cc = ctx = NULL; + +end: + return; + +} + +canl_io_handler canl_create_io_handler(canl_ctx cc) +{ + struct io_handler *new_io_h = NULL; + + if (!cc) { + goto end; + } + + /*create io handler*/ + new_io_h = (struct io_handler *) malloc(sizeof(*new_io_h)); + if (!new_io_h) + //set_error(ctx->err_msg); + goto end; + + /*read cc and set io_handler accordingly ...*/ + +end: + return new_io_h; +} + +int canl_io_connect(canl_ctx cc, canl_io_handler io, char * host, int port, + int flags, cred_handler ch, struct timeval *timeout) +{ + int err; + struct io_handler *io_cc = (struct io_handler*) io; + struct glb_ctx *glb_cc = (struct glb_ctx*) cc; + + /*check cc and io*/ + if (!cc) { + err = 1; + goto end; + } + + if (!io) { + //set_error(ctx->err_msg); + err = 1; + goto end; + } + + /*dns*/ + //err = dns_resolve(&ret_addr, ipver, host, port, timeout); + + /*open socket*/ + + /*call openssl to make a secured connection, optional?*/ + + /*write succes or failure to cc, io*/ + //if (err) + /*cc or io set error*/ + //else + /*cc or io set succes*/ +end: + return err; +} + +int canl_io_accept(canl_ctx cc, canl_io_handler io, int port, + int flags, cred_handler ch, struct timeval *timeout, + canl_io_handler *new_io) +{ + int err; + struct io_handler *io_cc = (struct io_handler*) io; + struct glb_ctx *glb_cc = (struct glb_ctx*) cc; + + /*check cc and io*/ + if (!cc) { + err = 1; + goto end; + } + + if (!io) { + //set_error(ctx->err_msg); + err = 1; + goto end; + } + /*check cc and io*/ + + /*wait for client*/ + + /*call openssl to make a secured connection, optional?*/ + + /*write succes or failure to cc, io*/ + //if (err) + /*cc or io set error*/ + //else + /*cc or io set succes*/ + +end: + return err; +} + +/* close connection, preserve some info for the future reuse */ +int canl_io_close(canl_ctx cc, canl_io_handler io) +{ + int err = 0; + /*check cc and io*/ + if (!cc) { + err = 1; + goto end; + } + + if (!io) { + //set_error(ctx->err_msg); + err = 1; + goto end; + } + + /*ssl close*/ + + /*set cc and io accordingly*/ + +end: + return err; +} + +int canl_io_destroy(canl_ctx cc, canl_io_handler io) +{ + int err = 0; + /*check cc and io*/ + if (!cc) { + err = 1; + goto end; + } + + if (!io) { + //set_error(ctx->err_msg); + err = 1; + } + + // delete io_handle content + + // delete io itself + if (io) { + free (io); + io = NULL; + } +end: + return err; +} + +size_t canl_io_read(canl_ctx cc, canl_io_handler io, void *buffer, size_t size, struct timeval *timeout) +{ + int err = 0; + if (!cc) { + err = 1; + goto end; + } + + if (!io) { + //set_error(ctx->err_msg); + err = 1; + goto end; + } + + //read something using openssl + +end: + return err; +} + +size_t canl_io_write(canl_ctx cc, canl_io_handler io, void *buffer, size_t size, struct timeval *timeout) +{ + int err; + if (!cc) { + err = 1; + goto end; + } + + if (!io) { + //set_error(ctx->err_msg); + err = 1; + goto end; + } + + //write sometring using openssl + +end: + return err; +} + +/* what about reason pointer? */ +size_t canl_io_get_error(canl_ctx cc, char ** reason) +{ + int err = 0; + if (!cc) { + err = 1; + goto end; + } + + struct glb_ctx *my_ctx = (struct glb_ctx*) cc; + *reason = my_ctx->err_msg; + +end: + return err; +} diff --git a/emi.canl.canl-c/src/canl.h b/emi.canl.canl-c/src/canl.h new file mode 100644 index 0000000..d18cc13 --- /dev/null +++ b/emi.canl.canl-c/src/canl.h @@ -0,0 +1,23 @@ +#ifndef CANL_H +#define CANL_H +#include +typedef void *canl_io_handler; +typedef void *canl_ctx; +typedef void *cred_handler; + +canl_ctx canl_create_ctx(); +void canl_free_ctx(canl_ctx cc); +canl_io_handler canl_create_io_handler(canl_ctx cc); + +int canl_io_connect(canl_ctx cc, canl_io_handler io, char * host, + int port, int flags, cred_handler ch, struct timeval *timeout); +int canl_io_accept(canl_ctx cc, canl_io_handler io, int port, int flags, cred_handler ch, + struct timeval *timeout, canl_io_handler * new_io); +size_t canl_io_read(canl_ctx cc, canl_io_handler io, void *buffer, size_t size, struct timeval *timeout); +size_t canl_io_write(canl_ctx cc, canl_io_handler io, void *buffer, size_t size, struct timeval *timeout); + +size_t canl_io_get_error(canl_ctx cc, char ** reason); +int canl_io_close(canl_ctx cc, canl_io_handler io); +int canl_io_destroy(canl_ctx cc, canl_io_handler io); + +#endif diff --git a/emi.canl.canl-c/src/canl_locl.h b/emi.canl.canl-c/src/canl_locl.h new file mode 100644 index 0000000..8804aca --- /dev/null +++ b/emi.canl.canl-c/src/canl_locl.h @@ -0,0 +1,22 @@ +#ifndef CANL_CBIND_H +#define CANL_CBIND_H +struct glb_ctx +{ + int opened_ios; + struct io_handler * io_ctx; + char * err_msg; +}; +/* +struct ossl_ctx +{ + SSL_METHOD ssl_meth; + SSL_CTX ssl_ctx; + SSL ssl_conn_ctx; +} +*/ +struct io_handler +{ + int something; +}; + +#endif diff --git a/emi.canl.canl-c/src/canl_sample_client.c b/emi.canl.canl-c/src/canl_sample_client.c new file mode 100644 index 0000000..60c2d5a --- /dev/null +++ b/emi.canl.canl-c/src/canl_sample_client.c @@ -0,0 +1,57 @@ +#include +#include "canl.h" + +int main() +{ + canl_ctx my_ctx; + canl_io_handler my_io_h; + int err = 0; + char *err_msg = NULL; + + my_ctx = canl_create_ctx(); + if (!my_ctx){ + // set_error("context cannot be created\n"); + goto end; + } + + my_io_h = canl_create_io_handler(my_ctx); + if (!my_io_h) { + //set_error("io handler cannot be created\n"); + goto end; + } + + err = canl_io_connect(my_ctx, my_io_h, NULL, 1234, 0, NULL, NULL); + if (err) { + //set_error("cannot make a connection"); + goto end; + } + + err = canl_io_write (my_ctx, my_io_h, NULL, 0, NULL); + if (err) { + //set_error ("cannot write"); + } + + err = canl_io_read (my_ctx, my_io_h, NULL, 0, NULL); + if (err) { + //set_error ("cannot read"); + } + + err = canl_io_close(my_ctx, my_io_h); + if (err){ + //set_error ("cannot close io"); + } + + err = canl_io_destroy(my_ctx, my_io_h); + if (err){ + //set_error ("cannot destroy io"); + } + +end: + canl_io_get_error(my_ctx, &err_msg); + if (err_msg != NULL) + printf("%s\n", err_msg); + + canl_free_ctx(my_ctx); + + return err; +} diff --git a/emi.canl.canl-c/src/canl_sample_server.c b/emi.canl.canl-c/src/canl_sample_server.c new file mode 100644 index 0000000..482b263 --- /dev/null +++ b/emi.canl.canl-c/src/canl_sample_server.c @@ -0,0 +1,64 @@ +#include +#include "canl.h" + +int main() +{ + canl_ctx my_ctx; + canl_io_handler my_io_h; + int err = 0; + char *err_msg = NULL; + + my_ctx = canl_create_ctx(); + if (!my_ctx){ + // set_error("context cannot be created\n"); + goto end; + } + + my_io_h = canl_create_io_handler(my_ctx); + if (!my_io_h) { + //set_error("io handler cannot be created\n"); + goto end; + } + + err = canl_io_accept(my_ctx, my_io_h, 1234, 0, NULL, NULL, NULL); + if (err) { + //set_error("cannot make a connection"); + goto end; + } + + err = canl_io_connect(my_ctx, my_io_h, NULL, 1234, 0, NULL, NULL); + if (err) { + //set_error("cannot make a connection"); + canl_io_destroy(my_ctx, my_io_h); + goto end; + } + + err = canl_io_write (my_ctx, my_io_h, NULL, 0, NULL); + if (err) { + //set_error ("cannot write"); + } + + err = canl_io_read (my_ctx, my_io_h, NULL, 0, NULL); + if (err) { + //set_error ("cannot read"); + } + + err = canl_io_close(my_ctx, my_io_h); + if (err){ + //set_error ("cannot close io"); + } + + err = canl_io_destroy(my_ctx, my_io_h); + if (err){ + //set_error ("cannot destroy io"); + } + +end: + canl_io_get_error(my_ctx, &err_msg); + if (err_msg != NULL) + printf("%s\n", err_msg); + + canl_free_ctx(my_ctx); + + return err; +}