Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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
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
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
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
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