int create_listener(const char * port) { memset(serverResponseBuffer, 0xab, 33000); instanceID = strdup("server"); noPollCtx * ctx = nopoll_ctx_new (); if (!ctx) { LogPrintf("Could not create context"); return 1; } if (debug_nopoll) { nopoll_log_set_handler(ctx, noPollLogger, NULL); } noPollConn * listener = nopoll_listener_new(ctx, "0.0.0.0", port); if (!nopoll_conn_is_ok (listener)) { LogPrintf("Could not listen on port %s", port); return 1; } LogPrintf("noPoll listener started at: %s:%s", nopoll_conn_host(listener), nopoll_conn_port(listener)); nopoll_ctx_set_on_msg(ctx, listener_on_message, NULL); nopoll_loop_wait(ctx, 0); nopoll_ctx_unref(ctx); return 0; }
int main(int argc, char *argv[]) { bool enough_args; enough_args = checkargs(argc); if(!enough_args) { return false; } //Create a nopoll context noPollCtx * ctx = nopoll_ctx_new(); if(! ctx) { //handle error here } noPollConn * listener = nopoll_listener_new (ctx, argv[1], argv[2]); //noPollConn * listener = nopoll_listener_new (ctx,"172.23.153.112" , "50004"); if(! nopoll_conn_is_ok (listener)) { //handle error here printf("not okay"); } nopoll_ctx_set_on_msg(ctx, listener_on_message, NULL); nopoll_loop_wait(ctx,0); // release the context printf("got here"); nopoll_ctx_unref(ctx); }
void* webSocketTest() { noPollConn * conn; noPollCtx * ctx; /* init context */ ctx = create_ctx (); /* create connection */ conn = nopoll_conn_new (ctx, "echo.websocket.org", "80", NULL, NULL, NULL, NULL); if (! nopoll_conn_is_ok (conn)) { printf ("ERROR: Expected to find proper client connection status, but found error..\n"); return nopoll_false; } /* end if */ /* check test */ if (! test_sending_and_check_echo (conn, "Test 26", "This is a test")) return nopoll_false; /* close the connection */ nopoll_conn_close (conn); /* release context */ nopoll_ctx_unref (ctx); return nopoll_true; }
static int np_wt_connect(void * args) { ChannelConnectInfo * info = (ChannelConnectInfo *)args; noPollConn * conn; if (info->is_ssl) { noPollConnOpts * opts = NULL; opts = nopoll_conn_opts_new (); #ifdef _WRS_KERNEL /* For VxWorks SSL peer certificate verification does not work; let's * disable this for now. */ nopoll_conn_opts_ssl_peer_verify (opts, nopoll_false); #endif nopoll_conn_opts_set_ssl_protocol (opts, NOPOLL_METHOD_TLSV1_1); conn = nopoll_conn_tls_new (info->np_ctx, opts, info->host, info->port, NULL, info->get_url, info->host_name, NULL); } else { conn = nopoll_conn_new (info->np_ctx, info->host, info->port, NULL, info->get_url, NULL, NULL); } if (! nopoll_conn_is_ok (conn)) { nopoll_conn_close(conn); errno = ECONNREFUSED; return -1; } /* nopoll_conn_wait_until_connection_ready() can return true even if * the connection is not ready but simply ok; no clue why. Let's check * again that the connection is ready. */ if (! nopoll_conn_wait_until_connection_ready (conn, 10) || ! nopoll_conn_is_ready(conn)) { nopoll_conn_close(conn); errno = EPERM; return -1; } assert (nopoll_conn_is_ready (conn)); assert (nopoll_conn_is_ok (conn)); /* Set the socket in blocking mode */ (void) nopoll_conn_set_sock_block(nopoll_conn_socket(conn), nopoll_true); info->np_sock = conn; return 0; }
static int channel_np_wait_until_connection_ready(noPollConn * conn, int timeout, int is_ssl) { long int total_timeout = timeout; int socket = nopoll_conn_socket(conn); /* check if the connection already finished its connection handshake */ do { int rc; #if defined(__linux__) struct pollfd ufd; memset(&ufd, 0, sizeof ufd); ufd.fd = socket; ufd.events = POLLIN; rc = poll(&ufd, 1, 10 * 1000); #else struct timeval tv; fd_set readfds; fd_set writefds; fd_set errorfds; FD_ZERO(&readfds); FD_ZERO(&writefds); FD_ZERO(&errorfds); FD_SET(socket, &readfds); tv.tv_usec = 10 * 1000; tv.tv_sec = 0; /* Wait for some event to occur on file descriptor */ rc = select(socket + 1, &readfds, &writefds, &errorfds, &tv); #endif if (rc == -1) break; /* For SSL connection, we need to call nopoll_conn_get_msg * in order to handle SSL accept. One may expect this is * done in nopoll_conn_is_ready()... */ if (is_ssl) nopoll_conn_get_msg (conn); /* check if the connection is ok */ if (! nopoll_conn_is_ok (conn)) return nopoll_false; /* reduce the amount of time we have to wait. This computation * is not fully accurate but overall it is okay*/ total_timeout = total_timeout - 10; } while (! nopoll_conn_is_ready (conn) && (total_timeout > 0)); /* report if the connection is ok */ return nopoll_conn_is_ready (conn); }
static int np_wt_write_request (void * args) { NpIOReq * req = (NpIOReq *)args; int rval; if(!nopoll_conn_is_ok(req->np_sock)) rval = -1; else { trace(LOG_PROTOCOL, "write request size:%d",req->bufsz); rval = nopoll_conn_send_binary(req->np_sock, req->bufp, req->bufsz); nopoll_conn_flush_writes (req->np_sock, 10, 0); /* assert(nopoll_conn_pending_write_bytes(req->np_sock) == 0);*/ } if (rval < 0) errno = ERR_OTHER; return rval; }
/** * @brief Allows to start a MQTT server on the provided local host * address and port running MQTT over WebSocket protocol. * * <b>Important note:</b> you must call to \ref myqtt_storage_set_path * to define the path first before creating any listener. This is * because creating a listener activates all server side code which * among other things includes the storage loading (client * subscriptions, offline publishing, etc). In the case direction, * once the storage path is loaded it cannot be changed after * restarting the particular context used in this operation (\ref * MyQttCtx). * * @param ctx The context where the operation takes place. * * @param listener The webSocket listener connection created on top of which MQTT over WebSocket is expected. * * @param opts Optional connection options to modify default behaviour. * * @param on_ready Optional on ready notification handler that gets * called when the listener is created or a failure was * found. Providing this handler makes this function to not block the * caller. * * @param user_data Optional user defined pointer that is passed into * the on_ready function (in the case the former is defined too). * * See \ref myqtt_listener_new for more information. * * @return A newly created connection listener reference (\ref * MyQttConn). Use \ref myqtt_conn_is_ok to check listener was created * without errors. */ MyQttConn * myqtt_web_socket_listener_new (MyQttCtx * ctx, noPollConn * listener, MyQttConnOpts * opts, MyQttListenerReady on_ready, axlPointer user_data) { MyQttWebSocketReady * websocket_ready = NULL; MyQttConn * myqtt_listener; if (! nopoll_conn_is_ok (listener)) { myqtt_log (MYQTT_LEVEL_CRITICAL, "Unable to create listener, received reference is not working"); return NULL; } /* end if */ /* create object for the function proxy but only in the user on_ready function is defined */ if (on_ready) { websocket_ready = axl_new (MyQttWebSocketReady, 1); if (websocket_ready == NULL) { myqtt_log (MYQTT_LEVEL_CRITICAL, "Unable to allocate memory to start the listener.."); return NULL; } /* end if */ websocket_ready->user_data = user_data; websocket_ready->listener = listener; websocket_ready->user_on_ready = on_ready; } /* end if */ /* associate context */ __myqtt_web_socket_associate_ctx (ctx, nopoll_conn_ctx (listener)); myqtt_listener = __myqtt_listener_new_common (ctx, nopoll_conn_host (listener), __myqtt_listener_get_port (nopoll_conn_port (listener)), /* register connection and socket */ axl_true, nopoll_conn_socket (listener), opts, on_ready, MYQTT_IPv6, __myqtt_web_socket_accept_connection, /* configure proxy on ready */ __myqtt_web_socket_listener_ready, websocket_ready); if (myqtt_listener) { /* configure here reference to the noPoll listener */ myqtt_conn_set_data_full (myqtt_listener, "__my:ws:lstnr", listener, NULL, (axlDestroyFunc) __myqtt_web_socket_close_conn); /* configure on ready */ } /* end if */ return myqtt_listener; }
static void np_flush_with_flags(ChannelNP * c, int flags) { unsigned char * p = c->obuf->buf; assert(is_dispatch_thread()); assert(c->magic == CHANNEL_MAGIC); assert(c->chan.out.end == p + sizeof(c->obuf->buf)); assert(c->out_bin_block == NULL); assert(c->chan.out.cur >= p); assert(c->chan.out.cur <= p + sizeof(c->obuf->buf)); if (c->chan.out.cur == p) return; if (c->chan.state != ChannelStateDisconnected && c->out_errno == 0) { #if ENABLE_OutputQueue c->obuf->buf_len = c->chan.out.cur - p; c->out_queue.post_io_request = post_write_request; trace(LOG_PROTOCOL, "Outbuf add size:%d",c->obuf->buf_len); output_queue_add_obuf(&c->out_queue, c->obuf); c->obuf = output_queue_alloc_obuf(); c->chan.out.end = c->obuf->buf + sizeof(c->obuf->buf); #else while (p < c->chan.out.cur) { int rval = 0; size_t sz = c->chan.out.cur - p; if (!nopoll_conn_is_ok(c->np_socket)) rval = -1; if (rval >= 0) { rval = nopoll_conn_send_binary(c->np_socket, (const char *)p, sz); nopoll_conn_flush_writes (c->np_socket, 10, 0); /* assert(nopoll_conn_pending_write_bytes(c->np_socket) == 0);*/ } if (rval < 0) errno = ERR_OTHER; if (rval < 0) { int err = errno; trace(LOG_PROTOCOL, "Can't send() on channel %#lx: %s", c, errno_to_str(err)); c->out_errno = err; c->chan.out.cur = c->obuf->buf; c->out_eom_cnt = 0; return; } p += rval; } assert(p == c->chan.out.cur); #endif } c->chan.out.cur = c->obuf->buf; c->out_eom_cnt = 0; }
/* @internal Function used to read content from the provided connection. */ int __myqtt_web_socket_receive (MyQttConn * conn, unsigned char * buffer, int buffer_len) { noPollConn * _conn; int result; MyQttCtx * ctx; MyQttMutex * mutex; /* check if the connection has the greetings completed and it * is initiator role */ _conn = myqtt_conn_get_data (conn, "__my:ws:conn"); ctx = conn->ctx; /* get mutex */ mutex = myqtt_conn_get_data (conn, "ws:mutex"); if (mutex == NULL) { myqtt_log (MYQTT_LEVEL_CRITICAL, "unable to find mutex to protect ssl object to read data"); return -1; } /* end if */ /* call to acquire mutex, read and release */ myqtt_mutex_lock (mutex); result = nopoll_conn_read (_conn, (char *) buffer, buffer_len, nopoll_false, 0); myqtt_mutex_unlock (mutex); if (result == -1) { /* check connection status to notify that no data was * available */ if (nopoll_conn_is_ok (_conn)) return -2; myqtt_log (MYQTT_LEVEL_CRITICAL, "Found noPollConn-id=%d (%p) read error myqtt conn-id=%d (session: %d), errno=%d (shutting down)", nopoll_conn_get_id (_conn), _conn, myqtt_conn_get_id (conn), myqtt_conn_get_socket (conn), errno); /* shutdown connection */ nopoll_conn_set_socket (_conn, -1); myqtt_conn_shutdown (conn); } /* end if */ return result; }
/********** Client Side Code **********/ int getResponse(noPollConn * conn, int requestLen, int index) { noPollMsg * msg; int count = 0; uint64_t beginTime = GetTime(); int msglen = 0; while (true) { count++; msg = nopoll_conn_get_msg(conn); if (msg != NULL) { noPollOpCode code = nopoll_msg_opcode(msg); if (!((code == NOPOLL_BINARY_FRAME) || (code == NOPOLL_CONTINUATION_FRAME))) { LogPrintf("Received unexpected message type %d", code); return -1; } msglen += nopoll_msg_get_payload_size(msg); DebugPrintf("Retrieved message %d frag %d:%d of size %d (collected %d of %d)", index, nopoll_msg_is_fragment(msg), nopoll_msg_is_final(msg), nopoll_msg_get_payload_size(msg), msglen, requestLen); if (nopoll_msg_is_final(msg) && (msglen != requestLen)) { DebugPrintf("Final flag set arbitrarily??"); } if (msglen == requestLen) { if (!nopoll_msg_is_final(msg)) DebugPrintf("Final flag not set??"); break; } nopoll_msg_unref(msg); } if (!nopoll_conn_is_ok(conn)) { LogPrintf("Client disconnected!!"); return -1; } nopoll_sleep(100); } DebugPrintf("Retrieved full message %d in %d ms looped: %d", index, (int) (GetTime() - beginTime), count); if (msglen != requestLen) { LogPrintf("Received invalid response length %d != %d", msglen, requestLen); return -1; } nopoll_msg_unref(msg); return 0; }
static int np_wt_read_request (void * args) { NpIOReq * req = (NpIOReq *)args; int rval = 0; trace(LOG_PROTOCOL, "read request size:%d",req->bufsz); while (rval == 0) { rval = nopoll_conn_read (req->np_sock, req->bufp, req->bufsz, nopoll_false, 10); if ((rval <= 0) && !nopoll_conn_is_ok(req->np_sock)) { rval = 0; /* Treat error as EOF */ break; } else if (rval < 0) { errno = ERR_OTHER; req->error_msg = loc_strdup("Failure reading socket"); break; } } trace(LOG_PROTOCOL, "read size:%d",rval); return rval; }
/** * @internal Function used by nopoll_loop_wait to register all * connections into the io waiting object. */ nopoll_bool nopoll_loop_register (noPollCtx * ctx, noPollConn * conn, noPollPtr user_data) { /* do not add connections that aren't working */ if (! nopoll_conn_is_ok (conn)) { /* remove this connection from registry */ nopoll_ctx_unregister_conn (ctx, conn); return nopoll_false; /* keep foreach, don't stop */ } /* register the connection socket */ /* nopoll_log (ctx, NOPOLL_LEVEL_DEBUG, "Adding socket id: %d", conn->session);*/ if (! ctx->io_engine->addto (conn->session, ctx, conn, ctx->io_engine->io_object)) { /* remove this connection from registry */ nopoll_ctx_unregister_conn (ctx, conn); nopoll_log (ctx, NOPOLL_LEVEL_WARNING, "Failed to add socket %d to the watching set", conn->session); } return nopoll_false; /* keep foreach, don't stop */ }
int create_client(const char * host, const char * port, int duration) { int rc = 0; // Create noPoll context noPollCtx * ctx = nopoll_ctx_new (); if (!ctx) { LogPrintf("Could not create context"); return 1; } if (debug_nopoll) { nopoll_log_set_handler(ctx, noPollLogger, NULL); } // Create connection uint64_t begin_time = GetTime(); noPollConn * conn = nopoll_conn_new (ctx, host, port, NULL, NULL, NULL, NULL); if (!nopoll_conn_is_ok(conn)) { LogPrintf("Failed setting up connection to %s:%s", host, port); return 1; } // Wait till connected if (!nopoll_conn_wait_until_connection_ready (conn, 120)) { LogPrintf("Could not get connected to %s:%s within 120 seconds. Failed in %d ms", host, port, (int)(GetTime() - begin_time)); return 1; } LogPrintf("Connected in %d ms.", (int)(GetTime() - begin_time)); // Don't make any requests. Just sleep for given duration and exit // sleep(duration); // LogPrintf("Exiting..."); // return 0; // Start the client's request/response loop char message[32]; int messageCount = 0; begin_time = GetTime(); while (true) { // Send request int requestLen = (rand() % 32000) + 1; int requestCount = (rand() % 64) + 1; DebugPrintf("Requesting %d messages of %d bytes each [%d]", requestCount, requestLen, messageCount); int len = sprintf(message, "Get %d %d", requestLen, requestCount); if (nopoll_conn_send_text(conn, message, len) != len) { rc = 1; LogPrintf("Could not send message: %d", errno); break; } // Collect responses for (int index = 0; index < requestCount; index++) { if (getResponse(conn, requestLen, index) < 0) { rc = 1; break; } messageCount++; } if (rc == 1) break; if ((int)(GetTime() - begin_time) > duration * 1000) break; } LogPrintf("Exited after %d ms. Received %d messages.", (int)(GetTime() - begin_time), messageCount); if (rc == 1) { LogPrintf("Error: Client Failed."); } nopoll_ctx_unref(ctx); return rc; }
void* webSocketery() { noPollCtx *ctx = nopoll_ctx_new(); noPollMsg *msg; noPollRole *role = nopoll_conn_role(NOPOLL_ROLE_UNKNOWN); struct noPollHandshake *fetch; noPollConnOpts *opts; /* Comment the log lines to disable loging Debug Warnings and Critical errors (Better not)*/ //nopoll_log_color_enable(ctx, true); //nopoll_log_enable(ctx, nopoll_true); /* Initializing the cookie options */ opts = nopoll_conn_opts_new(); if (!ctx) puts("error ctx is nill"); //To add Cookies use this method below /* nopoll_conn_opts_set_cookie(opts, "BAYEUX_BROWSER=56a9-mchhnynonz6ji8a6hs1sh49; JSESSIONID=8gz8e00htqrl15vcm3o9yi95f"); */ // Websocketery Works for mtgox and others servers but not for m.zpush.ovh it keeps rejecting me for an unknown f*****g 400 error ! Use Methods below to connect to server, a working example is provided //nopoll_conn_new(ctx, ip, port, host, get, protocols, origin) nopoll_conn_new_opts(ctx, opts, ip, port, host, get, protocols, origin)// //noPollConn *conn = nopoll_conn_new(ctx , "54.171.156.38" ,"80" ,"m.zpush.ovh:8080" ,"ws://m.zpush.ovh:8080/str/strd" ,NULL, "Authorize"); noPollConn *conn = nopoll_conn_new(ctx, "54.238.149.121", "80", "websocket.mtgox.com", "/mtgox", NULL, "chat"); if (!nopoll_conn_wait_until_connection_ready(conn, 50) ) { puts("nopoll_conn failed, timeout"); return (0);} if (nopoll_conn_send_text (conn, "hello how are you doing, do we connect ?", 40) != 40) {puts("send text just failed...."); return(0);} else { while (! nopoll_conn_is_ready (conn)) { if (! nopoll_conn_is_ok (conn)) { printf ("ERROR (4.1 jkd412): expected to find proper connection handshake finished, but found connection is broken: session=%d, errno=%d : %s..\n", (int) nopoll_conn_socket (conn), errno, strerror (errno)); return nopoll_false; } /* end if */ /* wait a bit 10ms */ nopoll_sleep (10000); } /* end if */ nopoll_conn_close (conn); /* finish */ nopoll_ctx_unref (ctx); puts("nopoll conn sucess"); while (nopoll_true) { if (nopoll_conn_is_ready(conn)) { puts("break"); break; } nopoll_sleep(10000); } msg = nopoll_conn_get_msg(conn); if (msg) printf("Msg received = %s\n", nopoll_msg_get_payload(msg)); if (!nopoll_conn_is_ok(conn)) { puts("------------ Connection Dead ----------------"); return nopoll_false; } } nopoll_ctx_unref(ctx); return (0); }
int main (int argc, char ** argv) { noPollConn * listener; noPollConn * listener_6; noPollConn * listener2; noPollConn * listener_62; #if defined(NOPOLL_HAVE_SSLv23_ENABLED) noPollConn * listener3; #endif #if defined(NOPOLL_HAVE_SSLv3_ENABLED) noPollConn * listener4; #endif #if defined(NOPOLL_HAVE_TLSv11_ENABLED) noPollConn * listener5; noPollConn * listener_65; #endif noPollConn * listener6; #if defined(NOPOLL_HAVE_TLSv12_ENABLED) noPollConn * listener7; #endif int iterator; noPollConnOpts * opts; signal (SIGTERM, __terminate_listener); #if defined(__NOPOLL_PTHREAD_SUPPORT__) printf ("INFO: install default threading functions to check noPoll locking code..\n"); nopoll_thread_handlers (__nopoll_regtest_mutex_create, __nopoll_regtest_mutex_destroy, __nopoll_regtest_mutex_lock, __nopoll_regtest_mutex_unlock); #endif /* create the context */ ctx = nopoll_ctx_new (); iterator = 1; while (iterator < argc) { /* check for debug */ printf ("Checking agument: %s\n", argv[iterator]); if (nopoll_cmp (argv[iterator], "--debug")) { printf ("Activating debug..\n"); nopoll_log_enable (ctx, nopoll_true); #if !defined(NOPOLL_OS_WIN32) nopoll_log_color_enable (ctx, nopoll_true); #endif } /* end if */ /* next position */ iterator++; } /* call to create a listener */ listener = nopoll_listener_new (ctx, "0.0.0.0", "1234"); if (! nopoll_conn_is_ok (listener)) { printf ("ERROR: Expected to find proper listener connection status, but found..\n"); return -1; } printf ("noPoll listener started at: %s:%s (refs: %d)..\n", nopoll_conn_host (listener), nopoll_conn_port (listener), nopoll_conn_ref_count (listener)); /* call to create a listener */ listener_6 = nopoll_listener_new6 (ctx, "::1", "2234"); if (! nopoll_conn_is_ok (listener_6)) { printf ("ERROR: Expected to find proper listener connection status, but found (IPv6 -- .1.1)..\n"); return -1; } /* end if */ printf ("noPoll listener started at (IPv6): %s:%s (refs: %d)..\n", nopoll_conn_host (listener_6), nopoll_conn_port (listener_6), nopoll_conn_ref_count (listener_6)); /* now start a TLS version */ printf ("Test: starting listener with TLS (TLSv1) at :1235\n"); listener2 = nopoll_listener_tls_new (ctx, "0.0.0.0", "1235"); if (! nopoll_conn_is_ok (listener2)) { printf ("ERROR: Expected to find proper listener TLS connection status, but found..\n"); return -1; } /* end if */ /* configure certificates to be used by this listener */ if (! nopoll_listener_set_certificate (listener2, "test-certificate.crt", "test-private.key", NULL)) { printf ("ERROR: unable to configure certificates for TLS websocket..\n"); return -1; } /* register certificates at context level */ if (! nopoll_ctx_set_certificate (ctx, NULL, "test-certificate.crt", "test-private.key", NULL)) { printf ("ERROR: unable to setup certificates at context level..\n"); return -1; } /* now start a TLS version */ printf ("Test: starting listener with TLS IPv6 (TLSv1) at :2235\n"); listener_62 = nopoll_listener_tls_new6 (ctx, "::1", "2235"); if (! nopoll_conn_is_ok (listener_62)) { printf ("ERROR: Expected to find proper listener TLS connection status, but found..\n"); return -1; } /* end if */ /* configure certificates to be used by this listener */ if (! nopoll_listener_set_certificate (listener_62, "test-certificate.crt", "test-private.key", NULL)) { printf ("ERROR: unable to configure certificates for TLS websocket..\n"); return -1; } /* register certificates at context level */ if (! nopoll_ctx_set_certificate (ctx, NULL, "test-certificate.crt", "test-private.key", NULL)) { printf ("ERROR: unable to setup certificates at context level..\n"); return -1; } #if defined(NOPOLL_HAVE_SSLv23_ENABLED) /* start listener with sslv23 */ printf ("Test: starting listener with TLS (SSLv23) at :1236 (all methods)\n"); opts = nopoll_conn_opts_new (); nopoll_conn_opts_set_ssl_protocol (opts, NOPOLL_METHOD_SSLV23); listener3 = nopoll_listener_tls_new_opts (ctx, opts, "0.0.0.0", "1236"); if (! nopoll_conn_is_ok (listener3)) { printf ("ERROR: Expected to find proper listener TLS connection status (:1236, SSLv23), but found..\n"); return -1; } /* end if */ #endif #if defined(NOPOLL_HAVE_SSLv3_ENABLED) printf ("Test: starting listener with TLS (SSLv3) at :1237\n"); opts = nopoll_conn_opts_new (); nopoll_conn_opts_set_ssl_protocol (opts, NOPOLL_METHOD_SSLV3); listener4 = nopoll_listener_tls_new_opts (ctx, opts, "0.0.0.0", "1237"); if (! nopoll_conn_is_ok (listener4)) { printf ("ERROR: Expected to find proper listener TLS connection status (:1237, SSLv3), but found..\n"); return -1; } /* end if */ #endif #if defined(NOPOLL_HAVE_TLSv11_ENABLED) printf ("Test: starting listener with TLS (TLSv1.1) at :1238\n"); opts = nopoll_conn_opts_new (); nopoll_conn_opts_set_ssl_protocol (opts, NOPOLL_METHOD_TLSV1_1); listener5 = nopoll_listener_tls_new_opts (ctx, opts, "0.0.0.0", "1238"); if (! nopoll_conn_is_ok (listener5)) { printf ("ERROR: Expected to find proper listener TLS connection status (:1238, TLSv1.1), but found..\n"); return -1; } /* end if */ printf ("Test: starting listener with TLS (TLSv1.1) (IPv6) at :2238\n"); opts = nopoll_conn_opts_new (); nopoll_conn_opts_set_ssl_protocol (opts, NOPOLL_METHOD_TLSV1_1); listener_65 = nopoll_listener_tls_new_opts6 (ctx, opts, "::1", "2238"); if (! nopoll_conn_is_ok (listener_65)) { printf ("ERROR: Expected to find proper listener TLS connection status (::1:2238, TLSv1.1, IPv6), but found..\n"); return -1; } /* end if */ #endif #if defined(NOPOLL_HAVE_TLSv12_ENABLED) printf ("Test: starting listener with TLS (TLSv1.2) at :1240\n"); opts = nopoll_conn_opts_new (); nopoll_conn_opts_set_ssl_protocol (opts, NOPOLL_METHOD_TLSV1_2); listener7 = nopoll_listener_tls_new_opts (ctx, opts, "0.0.0.0", "1240"); if (! nopoll_conn_is_ok (listener7)) { printf ("ERROR: Expected to find proper listener TLS connection status (:1240, TLSv1.2), but found..\n"); return -1; } /* end if */ #endif opts = nopoll_conn_opts_new (); /* configure server certificates (server.pem) signed by the * provided ca (root.pem) also configured in the last * parameter */ if (! nopoll_conn_opts_set_ssl_certs (opts, "server.pem", "server.pem", NULL, "root.pem")) { printf ("ERROR: unable to setup certificates...\n"); return -1; } /* configure peer verification */ nopoll_conn_opts_ssl_peer_verify (opts, nopoll_true); listener6 = nopoll_listener_tls_new_opts (ctx, opts, "0.0.0.0", "1239"); if (! nopoll_conn_is_ok (listener6)) { printf ("ERROR: Expected to find proper listener TLS connection status (:1236, SSLv23), but found..\n"); return -1; } /* end if */ /* configure ssl context creator */ /* nopoll_ctx_set_ssl_context_creator (ctx, ssl_context_creator, NULL); */ /* set on message received */ nopoll_ctx_set_on_msg (ctx, listener_on_message, NULL); /* set on open */ nopoll_ctx_set_on_open (ctx, on_connection_opened, NULL); /* process events */ nopoll_loop_wait (ctx, 0); /* unref connection */ nopoll_conn_close (listener); nopoll_conn_close (listener_6); /* ipv6 */ nopoll_conn_close (listener2); nopoll_conn_close (listener_62); /* ipv6 */ #if defined(NOPOLL_HAVE_SSLv23_ENABLED) nopoll_conn_close (listener3); #endif #if defined(NOPOLL_HAVE_SSLv3_ENABLED) nopoll_conn_close (listener4); #endif #if defined(NOPOLL_HAVE_TLSv12_ENABLED) nopoll_conn_close (listener5); nopoll_conn_close (listener_65); #endif nopoll_conn_close (listener6); #if defined(NOPOLL_HAVE_TLSv12_ENABLED) nopoll_conn_close (listener7); #endif /* finish */ printf ("Listener: finishing references: %d\n", nopoll_ctx_ref_count (ctx)); if (previous_msg) { printf ("..reference counting for previous msg: %d\n", nopoll_msg_ref_count (previous_msg)); nopoll_msg_unref (previous_msg); } /* end if */ nopoll_ctx_unref (ctx); /* call to release all pending memory allocated as a * consequence of using nopoll (especially TLS) */ nopoll_cleanup_library (); return 0; }