Beispiel #1
0
static int cipher(void)
{
    ne_socket *sock;

#ifdef SOCKET_SSL
    char *ciph;

    CALL(begin(&sock, serve_cipher, NULL));

    ciph = ne_sock_cipher(sock);

    ONN("NULL/empty cipher", ciph == NULL || strlen(ciph) == 0);

    FULLREAD(ciph);
    
    ne_free(ciph);
    
#else
    CALL(begin(&sock, serve_cipher, NULL));

    ONN("non-NULL cipher for non-SSL socket", 
        ne_sock_cipher(sock) != NULL);

    FULLREAD("NULL");

#endif
    return finish(sock, 1);
}
Beispiel #2
0
static int socks_proxy(void)
{
    static const struct {
        enum ne_sock_sversion version;
        int addr;
        const char *fqdn;
        unsigned int port;
        const char *username, *password;
    } ts[] = {
        { NE_SOCK_SOCKSV4, 4, NULL, 55555, NULL, NULL },
        { NE_SOCK_SOCKSV4, 4, NULL, 55555, "foobar", NULL },
        { NE_SOCK_SOCKSV4A, 0, "www.example.com", 55555, NULL, NULL },
        { NE_SOCK_SOCKSV5, 0, "www.example.com", 55555, NULL, NULL },
        { NE_SOCK_SOCKSV5, 4, NULL, 55555, NULL, NULL },
#ifdef TEST_IPV6
        { NE_SOCK_SOCKSV5, 6, NULL, 55555, NULL, NULL },
#endif
        { NE_SOCK_SOCKSV5, 0, "www.example.com", 55555, "norman", "foobar" }
    };
    unsigned n;

    for (n = 0; n < sizeof(ts)/sizeof(ts[n]); n++) {
        ne_socket *sock;
        struct socks_server arg = {0};
        int ret;

        arg.version = ts[n].version;
        arg.expect_port = ts[n].port;
        if (ts[n].addr == 4)
            arg.expect_addr = ne_iaddr_make(ne_iaddr_ipv4, raw_127);
#ifdef TEST_IPV6
        else if (ts[n].addr == 6)
            arg.expect_addr = ne_iaddr_make(ne_iaddr_ipv4, raw6_cafe);
#endif
        else
            arg.expect_fqdn = ts[n].fqdn;
        arg.username = ts[n].username;
        arg.password = ts[n].password;
        
        CALL(begin_socks(&sock, &arg, echo_server, NULL));

        ret = ne_sock_proxy(sock, ts[n].version, arg.expect_addr, 
                            ts[n].fqdn, ts[n].port,
                            ts[n].username, ts[n].password);
        ONV(ret, ("proxy connect #%u gave %d", n, ret));
        FULLREAD("ok!\n");
        ECHO("hello,\n");
        ECHO("\n");
        ECHO("world\n");
        
        if (ts[n].addr)
            ne_iaddr_free(arg.expect_addr);

        CALL(finish(sock, 0));
    }

    return OK;
}
Beispiel #3
0
static int line_long_chunked(void)
{
    ne_socket *sock;
    ssize_t ret;
    DECL_LONG(line, 'Z', OVERLEN);

    CALL(begin(&sock, serve_sstring_slowly, &line));
    
    FULLREAD("ZZZZZZZZ"); /* fill the buffer */
    ret = ne_sock_readline(sock, buffer, sizeof buffer);
    ONV(ret != NE_SOCK_ERROR,
        ("readline gave %" NE_FMT_SSIZE_T " not failure", ret));

    reap_server();
    ne_sock_close(sock);
    ne_free(line.data);
    return OK;
}
int
stratcon_realtime_recv_handler(eventer_t e, int mask, void *closure,
                               struct timeval *now) {
  static u_int32_t livestream_cmd = 0;
  noit_connection_ctx_t *nctx = closure;
  realtime_recv_ctx_t *ctx = nctx->consumer_ctx;
  int len;
  u_int32_t nint;
  char uuid_str[37];

  if(!livestream_cmd) livestream_cmd = htonl(NOIT_LIVESTREAM_DATA_FEED);

  if(mask & EVENTER_EXCEPTION || nctx->wants_shutdown) {
 socket_error:
    ctx->state = REALTIME_HTTP_WANT_INITIATE;
    ctx->count = 0;
    ctx->bytes_read = 0;
    ctx->bytes_written = 0;
    ctx->bytes_expected = 0;
    if(ctx->buffer) free(ctx->buffer);
    ctx->buffer = NULL;
    /* We close the event here and null it in the context
     * because the noit_connection_ctx_dealloc() will both close
     * it and free it (which our caller will double free) and
     * we consider double frees to be harmful.
     */
    eventer_remove_fd(e->fd);
    e->opset->close(e->fd, &mask, e);
    nctx->e = NULL;
    noit_connection_ctx_dealloc(nctx);
    return 0;
  }

#define full_nb_write(data, wlen) do { \
  if(!ctx->bytes_expected) { \
    ctx->bytes_written = 0; \
    ctx->bytes_expected = wlen; \
  } \
  while(ctx->bytes_written < ctx->bytes_expected) { \
    while(-1 == (len = e->opset->write(e->fd, ((char *)data) + ctx->bytes_written, \
                                       ctx->bytes_expected - ctx->bytes_written, \
                                       &mask, e)) && errno == EINTR); \
    if(len < 0) { \
      if(errno == EAGAIN) return mask | EVENTER_EXCEPTION; \
      goto socket_error; \
    } \
    ctx->bytes_written += len; \
  } \
  if(ctx->bytes_written != ctx->bytes_expected) { \
    noitL(noit_error, "short write on initiating stream [%d != %d].\n", \
          ctx->bytes_written, ctx->bytes_expected); \
    goto socket_error; \
  } \
  ctx->bytes_expected = 0; \
} while(0)

  noit_connection_update_timeout(nctx);
  while(1) {
    u_int32_t net_body_len;

    switch(ctx->state) {
      case REALTIME_HTTP_WANT_INITIATE:
        full_nb_write(&livestream_cmd, sizeof(livestream_cmd));
        ctx->state = REALTIME_HTTP_WANT_SEND_INTERVAL;
        /* FALLTHROUGH */
      case REALTIME_HTTP_WANT_SEND_INTERVAL:
        nint = htonl(ctx->rt->interval);
        full_nb_write(&nint, sizeof(nint));
        ctx->state = REALTIME_HTTP_WANT_SEND_UUID;
        /* FALLTHROUGH */
      case REALTIME_HTTP_WANT_SEND_UUID:
        uuid_unparse_lower(ctx->rt->checkid, uuid_str);
        full_nb_write(uuid_str, 36);
        ctx->state = REALTIME_HTTP_WANT_HEADER;
        /* FALLTHROUGH */
      case REALTIME_HTTP_WANT_HEADER:
        FULLREAD(e, ctx, sizeof(u_int32_t));
        memcpy(&net_body_len, ctx->buffer, sizeof(u_int32_t));
        ctx->body_len = ntohl(net_body_len);
        free(ctx->buffer); ctx->buffer = NULL;
        ctx->state = REALTIME_HTTP_WANT_BODY;
        break;
      case REALTIME_HTTP_WANT_BODY:
        FULLREAD(e, ctx, ctx->body_len);
        if(stratcon_line_to_javascript(ctx->ctx, ctx->buffer, &ctx->hack_inc_id)) goto socket_error;
        free(ctx->buffer); ctx->buffer = NULL;
        ctx->state = REALTIME_HTTP_WANT_HEADER;
        break;
    }
  }

}