Exemple #1
0
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;
}
Exemple #2
0
/** 
 * @brief Allows to return the noPollCtx object associated to the
 * provided connection.
 *
 * @param ctx The MyQttCtx for which we want access to the noPollCtx
 * object.
 *
 * @return A reference to the associated noPollCtx or NULL if it
 * fails.
 */
noPollCtx       * myqtt_web_socket_get_ctx              (MyQttCtx * ctx)
{
	noPollCtx * nopoll_ctx;

	if (ctx == NULL)
		return NULL;

	/* get current nopoll context configured */
	myqtt_mutex_lock (&ctx->ref_mutex);
	nopoll_ctx =  myqtt_ctx_get_data (ctx, "__my:ws:ctx");
	if (nopoll_ctx) {
		/* release lock */
		myqtt_mutex_unlock (&ctx->ref_mutex);

		return nopoll_ctx;
	} /* end if */

	/* reached this point, it looks like no context is configured,
	   create one and associate */
	nopoll_ctx = nopoll_ctx_new ();

	/* associate context */
	myqtt_ctx_set_data_full (ctx, "__my:ws:ctx", nopoll_ctx, 
				 NULL, (axlDestroyFunc) __myqtt_web_socket_ctx_unref);

	myqtt_mutex_unlock (&ctx->ref_mutex);

	return nopoll_ctx;
}
Exemple #3
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);
}
Exemple #4
0
noPollCtx * create_ctx (void) {

  /* create a context */
  noPollCtx * ctx = nopoll_ctx_new ();
  nopoll_log_enable (ctx, nopoll_true);
  nopoll_log_color_enable (ctx, true);
  return ctx;
}
void channel_np_connect(PeerServer * ps, ChannelConnectCallBack callback, void * callback_args) {
    const char * host = peer_server_getprop(ps, "Host", NULL);
    const char * port = peer_server_getprop(ps, "Port", NULL);
    const char * get_url = peer_server_getprop(ps, "GetUrl", NULL);
    const char * host_name = peer_server_getprop(ps, "HostName", NULL);
#if ENABLE_WebSocket_SOCKS_V5
    const char * proxy = peer_server_getprop(ps, "Proxy", NULL);
#endif
    ChannelConnectInfo * info = NULL;
    char port_str[16];
    struct addrinfo hints;
    struct addrinfo * reslist = NULL;
    int error;

    ini_nopoll();
    if (port == NULL) {
        sprintf(port_str, "%d", DISCOVERY_TCF_PORT);
        port = port_str;
    }

    memset(&hints, 0, sizeof hints);
    hints.ai_family = PF_INET;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;
    error = loc_getaddrinfo(host, port, &hints, &reslist);
    if (error) error = set_gai_errno(error);
    if (!error) {
        struct addrinfo * res;
        info = (ChannelConnectInfo *)loc_alloc_zero(sizeof(ChannelConnectInfo));
        for (res = reslist; res != NULL; res = res->ai_next) {
            info->addr_len = res->ai_addrlen;
            info->addr_buf = (struct sockaddr *)loc_alloc(res->ai_addrlen);
            memcpy(info->addr_buf, res->ai_addr, res->ai_addrlen);
            error = 0;
            break;
        }
        loc_freeaddrinfo(reslist);
    }

    if (!error && info->addr_buf == NULL) error = ENOENT;
    if (error) {
        if (info != NULL) {
            loc_free(info->addr_buf);
            loc_free(info);
        }
        callback(callback_args, error, NULL);
    } else {
	noPollCtx * np_ctx_client;
	np_ctx_client = nopoll_ctx_new();
	/*nopoll_log_enable(np_ctx_client, nopoll_true);*/
#if ENABLE_WebSocket_SOCKS_V5
        if (proxy != NULL) {
            /* save global proxy values */
            char * proxy_host = socks_v5_host;
            char * proxy_port = socks_v5_port;
            if (parse_socks_v5_proxy(proxy) != 0) {
                socks_v5_host = proxy_host;
                socks_v5_port = proxy_port;
                if (info != NULL) {
                    loc_free(info->addr_buf);
                    loc_free(info);
                }
                callback(callback_args, errno, NULL);
                return;
            }
            if (socks_v5_host != NULL) nopoll_conn_set_socks_v5_proxy(np_ctx_client, socks_v5_host, socks_v5_port);
            /* Restore global proxy values */
            socks_v5_host = proxy_host;
            socks_v5_port = proxy_port;
        }
#endif

        info->callback = callback;
        info->callback_args = callback_args;
        info->is_ssl = strcmp(peer_server_getprop(ps, "TransportName", ""), "WSS") == 0;
        info->req.client_data = info;
        info->req.done = channel_np_connect_done;
        info->req.type = AsyncReqUser;
        info->req.u.user.func = np_wt_connect;
        info->req.u.user.data = info;
        info->host = loc_strdup(host == NULL ? "127.0.0.1" : host);
        info->port = loc_strdup(port);
        info->get_url = get_url;
        info->host_name = host_name;
        info->np_ctx = np_ctx_client;
        async_req_post(&info->req);
    }
}
ChannelServer * channel_np_server(PeerServer * ps) {
/*    const char * host = peer_server_getprop(ps, "Host", NULL);*/
    const char * port = peer_server_getprop(ps, "Port", NULL);
    char port_str[16];
    const char * host = peer_server_getprop(ps, "Host", NULL);
    const char * certificate = peer_server_getprop(ps, "CertificateFile", NULL);
    const char * key = peer_server_getprop(ps, "KeyFile", NULL);
    noPollCtx * np_ctx;
    noPollConn * np_listener;
    int ssl;

    assert(is_dispatch_thread());
    if (port == NULL) {
        sprintf(port_str, "%d", DISCOVERY_TCF_PORT);
        port = port_str;
    }

    ini_nopoll();
    /* create the nopoll ctx */
    np_ctx = nopoll_ctx_new ();
    if(np_ctx == NULL) {
        trace(LOG_ALWAYS, "Unable to create nopoll context: %s", errno_to_str(errno));
        return NULL;
    }
    /*nopoll_log_enable(np_ctx, nopoll_true);*/

    ssl = strcmp(peer_server_getprop(ps, "TransportName", ""), "WSS") == 0;
    if (ssl) np_listener = nopoll_listener_tls_new (np_ctx, host == NULL ? "0.0.0.0" : host, port);
    else np_listener = nopoll_listener_new (np_ctx, host == NULL ? "0.0.0.0" : host, port);


    if (np_listener == NULL) {
        trace(LOG_ALWAYS, "Unable to create nopoll listener: %s", errno_to_str(errno));
        nopoll_ctx_unref(np_ctx);
        return NULL;
    }

    /* configure certificates to be used by this listener */
    if (ssl) {
        errno = 0;
        if (! nopoll_listener_set_certificate (np_listener, certificate, key, NULL)) {
            printf ("ERROR: unable to configure certificates for TLS websocket..\n");
            if (errno == 0) errno = EINVAL;
            nopoll_conn_close (np_listener);
            nopoll_ctx_unref(np_ctx);
            return NULL;
        }

        /* register certificates at context level */
        errno = 0;
        if (! nopoll_ctx_set_certificate (np_ctx, NULL, certificate, key, NULL)) {
            printf ("ERROR: unable to setup certificates at context level..\n");
            if (errno == 0) errno = EINVAL;
            nopoll_conn_close (np_listener);
            nopoll_ctx_unref(np_ctx);
            return NULL;
        }
    }

    peer_server_addprop(ps, loc_strdup("Port"), loc_strdup(port));
    return channel_server_create(ps, np_ctx, np_listener, ssl);
}
Exemple #7
0
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;
}
Exemple #8
0
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;
}