Advertisement
Guest User

c lib

a guest
Feb 26th, 2017
393
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.50 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #include "openssl/ssl.h"
  6. #include "openssl/bio.h"
  7. #include "openssl/err.h"
  8.  
  9. #include "lua.h"
  10. #include "lauxlib.h"
  11.  
  12. static void print_stringu (const char *string, size_t size) {
  13.     if (string) {
  14.         for(int i=0; i < size; i++) printf("%c", string[i]);
  15.         printf("\n");
  16.     }
  17.     else printf("");
  18. }
  19.  
  20. static char* concat(const char *dest, const char *src, size_t dest_size, size_t src_size) {
  21.     char *new = (char *) malloc(dest_size + src_size);
  22.  
  23.     if (!dest && !src) return NULL;
  24.  
  25.     if (!new) return NULL;
  26.  
  27.     if (!dest) for (int i=0; i < src_size; i++) new[i] = src[i];
  28.     else if (!src) for (int i=0; i < dest_size; i++) new[i] = dest[i];
  29.     else {
  30.         for (int i=0; i< dest_size; i++) new[i] = dest[i];
  31.         for (int i=dest_size, j=0; j < src_size ; i++, j++) new[i] = src[j];
  32.     }
  33.  
  34.     return new;
  35. }
  36.  
  37. static char* receive(BIO *bio, int chunksize, int *total_size) {
  38.     int bytes_read, total_bytes = 0;
  39.     char tmp[chunksize], *buf = NULL;
  40.  
  41.     while(1) {
  42.         bytes_read = BIO_read(bio, tmp, chunksize);
  43.         if(bytes_read <= 0) break;
  44.  
  45.         buf = concat(buf, tmp, total_bytes, bytes_read);
  46.         total_bytes += bytes_read;
  47.     }
  48.  
  49.     *total_size = total_bytes;
  50.     return buf;
  51. }
  52.  
  53. static int ssl_get(const char *host, const char *port, const char *request, char **response, const int datasize, const int hostsize) {
  54.     char host_port[hostsize];
  55.     int host_size, total_size = 0;
  56.  
  57.     /* ssl */
  58.     BIO *bio;
  59.     SSL *ssl;
  60.     SSL_CTX *ctx;
  61.  
  62.     /* init ssl lib */
  63.     SSL_library_init();
  64.     ERR_load_BIO_strings();
  65.     OpenSSL_add_all_algorithms();
  66.  
  67.     /* set up ssl context */
  68.     ctx = SSL_CTX_new(SSLv23_client_method());
  69.  
  70.     /* set up connection */
  71.     bio = BIO_new_ssl_connect(ctx);
  72.     BIO_get_ssl(bio, &ssl);
  73.     SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
  74.  
  75.     /* create connection */
  76.     /* host and port on the form of <host>:<port> */
  77.     strncpy(host_port, host, strlen(host));
  78.     host_size = strlen(host);
  79.     host_port[host_size] = ':';
  80.     host_port[host_size + 1] = '\0';
  81.     strncat(host_port, port, strlen(port));
  82.    
  83.  
  84.     BIO_set_conn_hostname(bio, host_port);
  85.    
  86.     if (BIO_do_connect(bio) <= 0) {
  87.         //fprintf(stderr, "Error attempting to connect\n");
  88.         //ERR_print_errors_fp(stderr);
  89.         BIO_free_all(bio);
  90.         SSL_CTX_free(ctx);
  91.         return -2;
  92.     }
  93.  
  94.     /* Send request */
  95.     BIO_write(bio, request, strlen(request));
  96.  
  97.     /* read response */
  98.     (*response) = receive(bio, datasize, &total_size);
  99.  
  100.     BIO_free_all(bio);
  101.     SSL_CTX_free(ctx);
  102.  
  103.     return total_size;
  104. }
  105.  
  106. /* lua wrapper */
  107. static int l_ssl_get (lua_State *L) {
  108.     const char *host, *port, *request;
  109.     char *response;
  110.     int datasize, hostsize, isnumber_datasize, isnumber_hostsize, total_size;
  111.     int arguments = lua_gettop(L);
  112.  
  113.     /* check number of arguments */
  114.     if(arguments != 5){
  115.         lua_pushnil(L);
  116.         lua_pushstring(L, "Incorrect number of arguments");
  117.         return 2;
  118.     }
  119.  
  120.     /* get arguments */
  121.     host = lua_tostring(L, 1);
  122.     port = lua_tostring(L, 2);
  123.     request = lua_tostring(L, 3);
  124.     datasize = lua_tointegerx(L, 4, &isnumber_datasize);  
  125.     hostsize = lua_tointegerx(L, 5, &isnumber_hostsize);
  126.  
  127.     /* return error if wrong argument type */
  128.     if (!host) {
  129.         lua_pushnil(L);
  130.         lua_pushstring(L, "Argument 1 needs to be a string");
  131.         return 2;
  132.     }
  133.     if (!port) {
  134.         lua_pushnil(L);
  135.         lua_pushstring(L, "Argument 2 needs to a string");
  136.         return 2;
  137.     }
  138.     if (!request) {
  139.         lua_pushnil(L);
  140.         lua_pushstring(L, "Argument 3 needs to a string");
  141.     }
  142.     if (!isnumber_datasize) {
  143.         lua_pushnil(L);
  144.         lua_pushstring(L, "Argument 4 needs to be an integer");
  145.         return 2;
  146.     }
  147.     if (!isnumber_hostsize) {
  148.         lua_pushnil(L);
  149.         lua_pushstring(L, "Argument 5 needs to be an integer");
  150.         return 2;
  151.     }
  152.  
  153.     /* call function */
  154.     total_size = ssl_get(host, port, request, &response, datasize, hostsize);
  155.  
  156.     if(total_size == -2) {
  157.         lua_pushnil(L);
  158.         lua_pushstring(L, "Error attempting to connect");
  159.         return 2;
  160.     }
  161.  
  162.     lua_pushlstring(L, response, total_size);
  163.     return 1;
  164. }
  165.  
  166. static const struct luaL_Reg sslrequest [] = {
  167.     {"ssl_get", l_ssl_get},
  168.     {NULL, NULL}
  169. };
  170.  
  171. int luaopen_sslrequest (lua_State *L) {
  172.     luaL_newlib(L, sslrequest);
  173.     return 1;
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement