Exemple #1
0
// read_cb: a TCP connection is readable so read the bytes that are on
// it and pass them to OpenSSL
void read_cb(uv_stream_t *s, ssize_t nread, const uv_buf_t *buf)
{
  connection_state *state = (connection_state *)s->data;

  if (nread > 0) {

    // If there's data to read then pass it to OpenSSL via the BIO
    // TODO: check return value

    BIO_write(state->read_bio, buf->base, nread);
  }

  if ((nread == UV_EOF) || (nread < 0)) {
    connection_terminate(state->tcp);
  } else {
    if (do_ssl(state)) {
      write_queued_messages(state);
      flush_write(state);
    } else {
      connection_terminate(state->tcp);
    }
  }

  // Buffer was previously allocated by us in a call to
  // allocate_cb. libuv will not reuse so we must free.

  if (buf) {
    free(buf->base);
  }
}
Exemple #2
0
int telnet_run(tcp_connection_t *conn)
{
    size_t bytes = 0;

    while(1)
    {
        /* Receive from socket */
        bytes = recv(conn->tcp_sock, buffer, 2048, 0);
        if(0 == bytes)
        {
            printf("Remote host closed connection.\n");
            return connection_close(conn);
        }
        if(0 > bytes)
        {
            fprintf(stderr, "error while receiving from remote host!\n");
            return -1;
        }


        /********************************************
         * Very sophisticated business logic        *
         ********************************************/
        if(0 == strncmp(buffer, "quit", 4))
        {
            return connection_close(conn);
        }
        else if(0 == strncmp(buffer, "halt", 4))
        {
            return connection_terminate(conn);
        }
        else
        {
            memcpy(buffer, "Unknown command!\n", 17);
            bytes = 17;
        }
        /********************************************/



        /* Answer on socket */
        bytes = send(conn->tcp_sock, buffer, bytes, 0);
        if(0 >= bytes)
        {
            fprintf(stderr, "error while sending to remote host!\n");
            return -1;
        }

        memset(buffer, 0, 2048);
    }

    return 0;
}