ssl_wrapper.c
Go to the documentation of this file.
1 // Copyright (c) 2014 Cesanta Software Limited
2 // All rights reserved
3 //
4 // This software is dual-licensed: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License version 2 as
6 // published by the Free Software Foundation. For the terms of this
7 // license, see <http://www.gnu.org/licenses/>.
8 //
9 // You are free to use this software under the terms of the GNU General
10 // Public License, but WITHOUT ANY WARRANTY; without even the implied
11 // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 // See the GNU General Public License for more details.
13 //
14 // Alternatively, you can license this software under a commercial
15 // license, as set out in <http://cesanta.com/products.html>.
16 //
17 // $Date$
18 
19 #include "net_skeleton.h"
20 #include "ssl_wrapper.h"
21 
22 static void ev_handler(struct ns_connection *nc, int ev, void *p) {
23  const char *target_addr = (const char *) nc->mgr->user_data;
24  struct ns_connection *pc = (struct ns_connection *) nc->user_data;
25  struct iobuf *io = &nc->recv_iobuf;
26 
27  (void) p;
28  switch (ev) {
29  case NS_ACCEPT:
30  // Create a connection to the target, and interlink both connections
31  nc->user_data = ns_connect(nc->mgr, target_addr, ev_handler, nc);
32  if (nc->user_data == NULL) {
34  }
35  break;
36 
37  case NS_CLOSE:
38  // If either connection closes, unlink them and shedule closing
39  if (pc != NULL) {
40  pc->flags |= NSF_FINISHED_SENDING_DATA;
41  pc->user_data = NULL;
42  }
43  nc->user_data = NULL;
44  break;
45 
46  case NS_RECV:
47  // Forward arrived data to the other connection, and discard from buffer
48  if (pc != NULL) {
49  ns_send(pc, io->buf, io->len);
50  iobuf_remove(io, io->len);
51  }
52  break;
53 
54  default:
55  break;
56  }
57 }
58 
59 void *ssl_wrapper_init(const char *local_addr, const char *target_addr,
60  const char **err_msg) {
61  struct ns_mgr *mgr = (struct ns_mgr *) calloc(1, sizeof(mgr[0]));
62  *err_msg = NULL;
63 
64  if (mgr == NULL) {
65  *err_msg = "malloc failed";
66  } else {
67  ns_mgr_init(mgr, (void *) target_addr);
68  if (ns_bind(mgr, local_addr, ev_handler, NULL) == NULL) {
69  *err_msg = "ns_bind() failed: bad listening_port";
70  ns_mgr_free(mgr);
71  free(mgr);
72  mgr = NULL;
73  }
74  }
75 
76  return mgr;
77 }
78 
79 void ssl_wrapper_serve(void *param, volatile int *quit) {
80  struct ns_mgr *mgr = (struct ns_mgr *) param;
81 
82  while (*quit == 0) {
83  ns_mgr_poll(mgr, 1000);
84  }
85  ns_mgr_free(mgr);
86  free(mgr);
87 }
88 
89 #ifndef SSL_WRAPPER_USE_AS_LIBRARY
90 static int s_received_signal = 0;
91 
92 static void signal_handler(int sig_num) {
93  signal(sig_num, signal_handler);
94  s_received_signal = sig_num;
95 }
96 
97 static void show_usage_and_exit(const char *prog) {
98  fprintf(stderr, "Usage: %s <listening_address> <target_address>\n", prog);
99  exit(EXIT_FAILURE);
100 }
101 
102 int main(int argc, char *argv[]) {
103  void *wrapper;
104  const char *err_msg;
105 
106  if (argc != 3) {
107  show_usage_and_exit(argv[0]);
108  }
109 
110  // Setup signal handlers
111  signal(SIGTERM, signal_handler);
112  signal(SIGINT, signal_handler);
113  signal(SIGPIPE, SIG_IGN);
114 
115  if ((wrapper = ssl_wrapper_init(argv[1], argv[2], &err_msg)) == NULL) {
116  fprintf(stderr, "Error: %s\n", err_msg);
117  exit(EXIT_FAILURE);
118  }
120 
121  return EXIT_SUCCESS;
122 }
123 #endif // SSL_WRAPPER_USE_AS_LIBRARY
#define free(ptr)
Definition: curl_memory.h:130
static void ev_handler(struct ns_connection *nc, int ev, void *p)
Definition: ssl_wrapper.c:22
#define NS_RECV
Definition: net_skeleton.h:174
void * ssl_wrapper_init(const char *local_addr, const char *target_addr, const char **err_msg)
Definition: ssl_wrapper.c:59
void iobuf_remove(struct iobuf *, size_t data_size)
Definition: mongoose.c:392
char * buf
Definition: net_skeleton.h:154
time_t ns_mgr_poll(struct ns_mgr *, int milli)
Definition: mongoose.c:1071
int ns_send(struct ns_connection *, const void *buf, int len)
void ssl_wrapper_serve(void *param, volatile int *quit)
Definition: ssl_wrapper.c:79
const char ** p
Definition: unit1394.c:76
size_t len
Definition: net_skeleton.h:155
struct ns_connection * ns_bind(struct ns_mgr *, const char *, ns_callback_t, void *)
Definition: mongoose.c:784
int main(int argc, char *argv[])
Definition: ssl_wrapper.c:102
struct iobuf recv_iobuf
Definition: net_skeleton.h:194
#define NS_CLOSE
Definition: net_skeleton.h:176
void ns_mgr_free(struct ns_mgr *)
Definition: mongoose.c:1267
static void show_usage_and_exit(const char *prog)
Definition: ssl_wrapper.c:97
void ns_mgr_init(struct ns_mgr *, void *user_data)
Definition: mongoose.c:1243
#define NSF_FINISHED_SENDING_DATA
Definition: net_skeleton.h:204
unsigned int flags
Definition: net_skeleton.h:203
static int s_received_signal
Definition: ssl_wrapper.c:90
struct ns_connection * ns_connect(struct ns_mgr *, const char *, ns_callback_t, void *)
Definition: mongoose.c:1166
#define NSF_CLOSE_IMMEDIATELY
Definition: net_skeleton.h:208
#define NS_ACCEPT
Definition: net_skeleton.h:172
#define fprintf
Definition: curl_printf.h:41
void * user_data
Definition: net_skeleton.h:183
void * user_data
Definition: net_skeleton.h:198
struct ns_mgr * mgr
Definition: net_skeleton.h:190
#define calloc(nbelem, size)
Definition: curl_memory.h:126
static void signal_handler(int sig_num)
Definition: ssl_wrapper.c:92


rc_tagdetect_client
Author(s): Monika Florek-Jasinska , Raphael Schaller
autogenerated on Sat Feb 13 2021 03:42:16