ssl_wrapper.c
Go to the documentation of this file.
00001 // Copyright (c) 2014 Cesanta Software Limited
00002 // All rights reserved
00003 //
00004 // This software is dual-licensed: you can redistribute it and/or modify
00005 // it under the terms of the GNU General Public License version 2 as
00006 // published by the Free Software Foundation. For the terms of this
00007 // license, see <http://www.gnu.org/licenses/>.
00008 //
00009 // You are free to use this software under the terms of the GNU General
00010 // Public License, but WITHOUT ANY WARRANTY; without even the implied
00011 // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00012 // See the GNU General Public License for more details.
00013 //
00014 // Alternatively, you can license this software under a commercial
00015 // license, as set out in <http://cesanta.com/products.html>.
00016 //
00017 // $Date$
00018 
00019 #include "net_skeleton.h"
00020 #include "ssl_wrapper.h"
00021 
00022 static void ev_handler(struct ns_connection *nc, int ev, void *p) {
00023   const char *target_addr = (const char *) nc->mgr->user_data;
00024   struct ns_connection *pc = (struct ns_connection *) nc->user_data;
00025   struct iobuf *io = &nc->recv_iobuf;
00026 
00027   (void) p;
00028   switch (ev) {
00029     case NS_ACCEPT:
00030       // Create a connection to the target, and interlink both connections
00031       nc->user_data = ns_connect(nc->mgr, target_addr, ev_handler, nc);
00032       if (nc->user_data == NULL) {
00033         nc->flags |= NSF_CLOSE_IMMEDIATELY;
00034       }
00035       break;
00036 
00037     case NS_CLOSE:
00038       // If either connection closes, unlink them and shedule closing
00039       if (pc != NULL) {
00040         pc->flags |= NSF_FINISHED_SENDING_DATA;
00041         pc->user_data = NULL;
00042       }
00043       nc->user_data = NULL;
00044       break;
00045 
00046     case NS_RECV:
00047       // Forward arrived data to the other connection, and discard from buffer
00048       if (pc != NULL) {
00049         ns_send(pc, io->buf, io->len);
00050         iobuf_remove(io, io->len);
00051       }
00052       break;
00053 
00054     default:
00055       break;
00056   }
00057 }
00058 
00059 void *ssl_wrapper_init(const char *local_addr, const char *target_addr,
00060                        const char **err_msg) {
00061   struct ns_mgr *mgr = (struct ns_mgr *) calloc(1, sizeof(mgr[0]));
00062   *err_msg = NULL;
00063 
00064   if (mgr == NULL) {
00065     *err_msg = "malloc failed";
00066   } else {
00067     ns_mgr_init(mgr, (void *) target_addr);
00068     if (ns_bind(mgr, local_addr, ev_handler, NULL) == NULL) {
00069       *err_msg = "ns_bind() failed: bad listening_port";
00070       ns_mgr_free(mgr);
00071       free(mgr);
00072       mgr = NULL;
00073     }
00074   }
00075 
00076   return mgr;
00077 }
00078 
00079 void ssl_wrapper_serve(void *param, volatile int *quit) {
00080   struct ns_mgr *mgr = (struct ns_mgr *) param;
00081 
00082   while (*quit == 0) {
00083     ns_mgr_poll(mgr, 1000);
00084   }
00085   ns_mgr_free(mgr);
00086   free(mgr);
00087 }
00088 
00089 #ifndef SSL_WRAPPER_USE_AS_LIBRARY
00090 static int s_received_signal = 0;
00091 
00092 static void signal_handler(int sig_num) {
00093   signal(sig_num, signal_handler);
00094   s_received_signal = sig_num;
00095 }
00096 
00097 static void show_usage_and_exit(const char *prog) {
00098   fprintf(stderr, "Usage: %s <listening_address> <target_address>\n", prog);
00099   exit(EXIT_FAILURE);
00100 }
00101 
00102 int main(int argc, char *argv[]) {
00103   void *wrapper;
00104   const char *err_msg;
00105 
00106   if (argc != 3) {
00107     show_usage_and_exit(argv[0]);
00108   }
00109 
00110   // Setup signal handlers
00111   signal(SIGTERM, signal_handler);
00112   signal(SIGINT, signal_handler);
00113   signal(SIGPIPE, SIG_IGN);
00114 
00115   if ((wrapper = ssl_wrapper_init(argv[1], argv[2], &err_msg)) == NULL) {
00116     fprintf(stderr, "Error: %s\n", err_msg);
00117     exit(EXIT_FAILURE);
00118   }
00119   ssl_wrapper_serve(wrapper, &s_received_signal);
00120 
00121   return EXIT_SUCCESS;
00122 }
00123 #endif // SSL_WRAPPER_USE_AS_LIBRARY


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:06