/* following _wt_ functions are called from a worker thread so caution is required
 * to keep its operations thread safe
 */
static int np_wt_accept (void * args) {
    int rc;
    ServerNP * si = (ServerNP *)args;

    si->np_sock = nopoll_conn_accept (si->np_ctx, si->np_listener);
    if (si->np_sock == NULL) {
        /* nopoll library does not always set errno, in this case create 
         * a fake one.
         */
        if (errno == 0) errno = EINVAL;
        return -1;
    }

    (void) nopoll_conn_set_sock_block(nopoll_conn_socket(si->np_sock), nopoll_false);
    rc = channel_np_wait_until_connection_ready(si->np_sock, 1000, si->is_ssl);
    (void) nopoll_conn_set_sock_block(nopoll_conn_socket(si->np_sock), nopoll_true);

    if (rc == 0) {
        errno = EINVAL;
        return -1;
    }
    assert (rc);
    assert(nopoll_conn_is_ready(si->np_sock));
    return (si->np_sock != NULL);
}
Пример #2
0
nopoll_bool on_connection_opened (noPollCtx * ctx, noPollConn * conn, noPollPtr user_data)
{
	/* set connection close */
	nopoll_conn_set_on_close (conn, __nopoll_regression_on_close, NULL);

	if (! nopoll_conn_set_sock_block (nopoll_conn_socket (conn), nopoll_false)) {
		printf ("ERROR: failed to configure non-blocking state to connection..\n");
		return nopoll_false;
	} /* end if */

	/* check to reject */
	if (nopoll_cmp (nopoll_conn_get_origin (conn), "http://deny.aspl.es"))  {
		printf ("INFO: rejected connection from %s, with Host: %s and Origin: %s\n",
			nopoll_conn_host (conn), nopoll_conn_get_host_header (conn), nopoll_conn_get_origin (conn));
		return nopoll_false;
	} /* end if */

	/* get protocol to reply an especific case. This is an example
	   on how to detect protocols requested by the client and how
	   to reply with a particular value at the server. */
	printf ("Requested protocol: %s\n", nopoll_conn_get_requested_protocol (conn));
	if (nopoll_cmp (nopoll_conn_get_requested_protocol (conn), "hello-protocol")) {
		/* set hello-protocol-response */
		nopoll_conn_set_accepted_protocol (conn, "hello-protocol-response");
	} /* end if */

	/* notify connection accepted */
	/* printf ("INFO: connection received from %s, with Host: %s and Origin: %s\n",
	   nopoll_conn_host (conn), nopoll_conn_get_host_header (conn), nopoll_conn_get_origin (conn)); */
	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;
}