Пример #1
0
static int test_client(struct harness_t *harness_p)
{
    struct ssl_context_t context;
    struct ssl_socket_t ssl_socket;
    struct socket_t socket;
    struct inet_addr_t addr;
    char buf[8];

    /* Create a context with default settings. */
    BTASSERT(ssl_context_init(&context) == 0);

    /* Create a socket and connect to the server. */
    BTASSERT(socket_open_tcp(&socket) == 0);

    inet_aton("1.2.3.4", &addr.ip);
    addr.port = 1234;
    BTASSERT(socket_connect(&socket, &addr) == 0);

    /* Wrap the socket in SSL. */
    BTASSERT(ssl_socket_init(&ssl_socket,
                             &context,
                             &socket,
                             ssl_socket_mode_client_t) == 0);
    BTASSERT(ssl_socket_handshake(&ssl_socket) == 0);

    /* Transfer data to and from the server. */
    BTASSERT(ssl_socket_write(&ssl_socket, "hello", 6) == 6);
    BTASSERT(ssl_socket_read(&ssl_socket, &buf[0], 8) == 8);
    BTASSERT(strcmp("goodbye", buf) == 0);

    /* Close the connection. */
    BTASSERT(socket_close(&socket) == 0);

    return (0);
}
Пример #2
0
static int test_server(struct harness_t *harness_p)
{
    struct ssl_context_t context;
    struct ssl_socket_t ssl_socket;
    struct socket_t listener, socket;
    struct inet_addr_t addr;
    char buf[8];

    /* Create a context with default settings. */
    BTASSERT(ssl_context_init(&context) == 0);

    /* Create a socket and connect to the server. */
    BTASSERT(socket_open_tcp(&listener) == 0);
    BTASSERT(socket_listen(&listener, 5) == 0);
    BTASSERT(socket_accept(&listener, &socket, &addr) == 0);

    /* Wrap the socket in SSL. */
    BTASSERT(ssl_socket_init(&ssl_socket,
                             &context,
                             &socket,
                             ssl_socket_mode_server_t) == 0);
    BTASSERT(ssl_socket_handshake(&ssl_socket) == 0);

    /* Transfer data to and from the server. */
    BTASSERT(ssl_socket_read(&ssl_socket, &buf[0], 6) == 6);
    BTASSERT(strcmp("hello", buf) == 0);
    BTASSERT(ssl_socket_write(&ssl_socket, "goodbye", 8) == 8);

    /* Close the connection. */
    BTASSERT(socket_close(&socket) == 0);

    return (0);
}
Пример #3
0
static int net_http_new_socket(struct http_connection_t *conn)
{
   int ret;
   struct addrinfo *addr = NULL, *next_addr = NULL;
   int fd                = socket_init(
         (void**)&addr, conn->port, conn->domain, SOCKET_TYPE_STREAM);
#ifdef HAVE_SSL
   if (conn->sock_state.ssl)
   {
      if (!(conn->sock_state.ssl_ctx = ssl_socket_init(fd, conn->domain)))
         return -1;
   }
#endif

   next_addr = addr;
   while(fd >= 0)
   {
#ifdef HAVE_SSL
      if (conn->sock_state.ssl)
      {
         ret = ssl_socket_connect(conn->sock_state.ssl_ctx, (void*)next_addr, true, true);

         if (ret >= 0)
            break;

         ssl_socket_close(conn->sock_state.ssl_ctx);
      }
      else
#endif
      {
         ret = socket_connect(fd, (void*)next_addr, true);

         if (ret >= 0 && socket_nonblock(fd))
            break;

         socket_close(fd);
      }

      fd = socket_next((void**)&next_addr);
   }

   if (addr)
      freeaddrinfo_retro(addr);

   conn->sock_state.fd = fd;

   return fd;
}