Ejemplo n.º 1
0
void copy_data_test(SshStream s1, SshStream s2)
{
  ts1 = s1;
  ts2 = s2;

  create_testdata();
  if (received_data)
    ssh_buffer_clear(received_data);
  else
    received_data = ssh_buffer_allocate();
  test_data_index = 0;
  destroy_count = 0;
  reader_sent_eof = FALSE;

  ssh_stream_set_callback(s1, copy_writer, NULL);
  ssh_stream_set_callback(s2, copy_reader, NULL);

  ssh_event_loop_run();
  if (destroy_count != 2 || ts1 != NULL || ts2 != NULL)
    ssh_fatal("copy_data_test: one stream not destroyed");
  if (ssh_buffer_len(received_data) > ssh_buffer_len(testdata))
    ssh_fatal("copy_data_test: received more data than sent");
  if (break_test)
    ssh_buffer_consume_end(testdata,
                           ssh_buffer_len(testdata)
                           - ssh_buffer_len(received_data));
  if (ssh_buffer_len(testdata) != ssh_buffer_len(received_data))
    ssh_fatal("copy_data_test: data lens differ");
  if (memcmp(ssh_buffer_ptr(testdata), ssh_buffer_ptr(received_data),
             ssh_buffer_len(testdata)) != 0)
    ssh_fatal("copy_data_test: received data differs");
}
Ejemplo n.º 2
0
void t_tcpc_tcp_callback(SshTcpError      error,
                         SshStream    stream,
                         void*                context)
{
  t_tcpc_context  pcontext = context;




  SSH_TRACE(SSH_D_MY, ("%s", "t_tcpc_tcp_callback"));

  switch (error)
    {
    case SSH_TCP_OK:
      pcontext->pstream = stream;
      ssh_stream_set_callback(pcontext->pstream,
                              t_tcpc_stream_callback,
                              pcontext);
      ssh_xregister_timeout(pcontext->timeout,
                            0,
                            t_tcpc_timeout_callback,
                            pcontext);
      break;
    case SSH_TCP_NEW_CONNECTION:
      ssh_tcp_destroy_listener(pcontext->ptcplistener);
      pcontext->ptcplistener = 0;
      pcontext->pstream = stream;
      ssh_stream_set_callback(pcontext->pstream,
                              t_tcpc_stream_callback,
                              pcontext);
      break;
    case SSH_TCP_NO_ADDRESS:
      SSH_NOTREACHED;
      break;
    case SSH_TCP_NO_NAME:
      SSH_NOTREACHED;
      break;
    case SSH_TCP_UNREACHABLE:
      SSH_NOTREACHED;
      break;
    case SSH_TCP_REFUSED:
      SSH_NOTREACHED;
      break;
    case SSH_TCP_TIMEOUT:
      SSH_NOTREACHED;
      break;
    case SSH_TCP_FAILURE:
      SSH_NOTREACHED;
      break;
    default:
      SSH_NOTREACHED;
      break;
    }
}
Ejemplo n.º 3
0
void ssh_packet_wrapper_send_encode_va(SshPacketWrapper down,
                                       SshPacketType type,
                                       va_list va)
{
  /* Format the packet in a separate buffer. */
  ssh_buffer_clear(&down->outgoing_packet);
  ssh_packet_encode_va(&down->outgoing_packet, type, va);

  /* Check that we don't overflow maximum buffer size.  Drop the packet
     if we would. */
  if (ssh_buffer_len(&down->outgoing) +
      ssh_buffer_len(&down->outgoing_packet) >= BUFFER_MAX_SIZE)
    {
      ssh_debug("ssh_packet_wrapper_send_encode_va: flow control problems; "
                "outgoing packet dropped.");
      return;
    }

  /* Append the packet to the outgoing buffer. */
  ssh_buffer_append(&down->outgoing, ssh_buffer_ptr(&down->outgoing_packet),
                    ssh_buffer_len(&down->outgoing_packet));

  /* Reset the callback to ensure that our callback gets called. */
  ssh_stream_set_callback(down->stream, ssh_packet_wrapper_callback,
                          (void *)down);
}
Ejemplo n.º 4
0
static void
pkix_http_receive_data_operate(SshHttpClientContext ctx,
                               SshHttpResult result,
                               SshTcpError ip_error,
                               SshStream stream,
                               void *context)
{
  PkixHttpReadContext c = (PkixHttpReadContext)ssh_malloc(sizeof(*c));
  SshFSMThread thread = (SshFSMThread)context;
  SshPkiThreadData tdata = ssh_fsm_get_tdata(thread);
  SshPkiGlobalData gdata = ssh_fsm_get_gdata(thread);

  tdata->transport_op = NULL;
  if (c && result == SSH_HTTP_RESULT_SUCCESS)
    {
      c->http_stream = stream;
      c->upper_context = context;
      if ((c->input = ssh_buffer_allocate()) != NULL)
        {
          ssh_stream_set_callback(stream,
                                  pkix_http_stream_callback, (void *)c);
          pkix_http_stream_callback(SSH_STREAM_INPUT_AVAILABLE, (void *)c);
          return;
        }
    }
  ssh_free(c);
  tdata->input_type = SSH_PKI_MSG_ERRORREP;
  tdata->input_len = 1;
  ssh_fsm_continue(gdata->input_thread);
}
Ejemplo n.º 5
0
Archivo: t-tr.c Proyecto: AnthraX1/rk
void connect_callback(SshIpError status, SshStream stream, void *context)
{
  Handler c;
  TestCase *testcase = context;
  SshTransportParams params;
  
  if (status != SSH_IP_OK)
    ssh_fatal("connect_callback: status %d", status);

#ifdef DEBUG
  ssh_debug("connect successful");
#endif  

  params = ssh_transport_create_params();
  update_algs(params, testcase->c_to_s_algs);
  
  c = ssh_xcalloc(sizeof(*c), 1);
  c->stream = ssh_transport_client_wrap(stream, random_state, SSH_VERSION,
                                        testcase->service, params,
                                        SERVER_NAME, NULL, NULL, NULL, NULL);
  c->script = testcase->client_script;
  c->side = "client";
  c->name = testcase->name;
  ssh_stream_set_callback(c->stream, handler_callback, (void *)c);
  client_handler = c;
}
Ejemplo n.º 6
0
void ssh_pipe_stream_set_callback(void *context, SshStreamCallback callback,
                                  void *callback_context)
{
    SshPipeStream pipes = (SshPipeStream)context;

    pipes->callback = callback;
    pipes->callback_context = callback_context;
    ssh_stream_set_callback(pipes->stdio_stream, callback, callback_context);
}
Ejemplo n.º 7
0
void ssh_packet_wrapper_can_receive(SshPacketWrapper down, Boolean status)
{
  down->can_receive = status;
  if (status == TRUE)
    {
      /* Reset the callbacks to ensure that our callback gets called. */
      ssh_stream_set_callback(down->stream, ssh_packet_wrapper_callback,
                              (void *)down);
    }
}
Ejemplo n.º 8
0
void listener1_callback(SshStream stream, void *context)
{
  if (!stream)
    ssh_fatal("listener1 have no stream");

  ssh_stream_set_callback(stream, server1_callback, stream);

  ssh_local_destroy_listener(listener1);  
  unlink(lpath1);
}
Ejemplo n.º 9
0
void connect1_done(SshStream stream, void *context)
{
  unsigned char buf[100];

  if (context != (void *)3)
    ssh_fatal("connect1 bad context");
  if (!stream)
    ssh_fatal("connection failed");

  if (ssh_stream_read(stream, buf, sizeof(buf)) >= 0)
    ssh_fatal("connect1 read should have failed");
  
  ssh_stream_get_stats(stream, &stats);
  ssh_stream_set_callback(stream, connect1_callback, (void *)stream);
}
Ejemplo n.º 10
0
void ssh_packet_impl_set_callback(void *context, SshStreamCallback callback,
                                  void *callback_context)
{
  SshPacketImpl up = (SshPacketImpl)context;

  up->up_callback = callback;
  up->up_context = callback_context;

  up->up_read_blocked = TRUE;
  up->up_write_blocked = TRUE;
  ssh_packet_impl_restart_output(up);
  ssh_packet_impl_restart_input(up);

  /* If shortcircuiting, set the callbacks for the shortcircuited stream. */
  if (up->shortcircuit_stream)
    ssh_stream_set_callback(up->shortcircuit_stream, callback,
                            callback_context);
}
Ejemplo n.º 11
0
void ssh_packet_impl_shortcircuit_now(SshStream up_stream,
                                      SshStream down_stream)
{
  SshPacketImpl up;

  /* Verify that it is a SshPacketImpl stream. */
  if (ssh_stream_get_methods(up_stream) != &ssh_packet_impl_methods)
    ssh_fatal("ssh_packet_impl_can_receive: not a SshPacketImpl stream");
  /* Get the internal context. */
  up = (SshPacketImpl)ssh_stream_get_context(up_stream);

  /* Save shortcircuit stream. */
  up->shortcircuit_stream = down_stream;

  /* We currently require there to be no partial incoming packet. */
  SSH_ASSERT(ssh_buffer_len(&up->incoming) == 0);

  /* If it is non-NULL, make it use application callbacks directly. */
  if (down_stream)
    ssh_stream_set_callback(down_stream, up->up_callback, up->up_context);
}
Ejemplo n.º 12
0
Archivo: t-tr.c Proyecto: AnthraX1/rk
void listener_callback(SshIpError status, SshStream stream, void *context)
{
  SshPrivateKey private_host_key, private_server_key;
  unsigned char *blob;
  unsigned int blob_len = 0;
  Handler c;
  TestCase *testcase = context;
  SshTransportParams params;
  
  if (status != SSH_IP_NEW_CONNECTION)
    ssh_fatal("listener_callback: status %d", status);

#ifdef DEBUG
  ssh_debug("listener: new connection");
#endif
  
  create_server_keys(&private_host_key, &private_server_key, &blob, &blob_len);
  
  params = ssh_transport_create_params();
  update_algs(params, testcase->s_to_c_algs);

  c = ssh_xcalloc(sizeof(*c), 1);

  c->stream = ssh_transport_server_wrap(stream, random_state, SSH_VERSION,
                                        params,
                                        private_host_key, private_server_key,
                                        blob, blob_len, NULL, NULL);

  ssh_private_key_free(private_host_key);
  ssh_private_key_free(private_server_key);
  ssh_xfree(blob);
  c->script = testcase->server_script;
  c->side = "server";
  c->name = testcase->name;

  ssh_stream_set_callback(c->stream, handler_callback, (void *)c);
  server_handler = c;
  ssh_tcp_destroy_listener(listener);
}
Ejemplo n.º 13
0
SshPacketWrapper ssh_packet_wrap(SshStream down_stream,
                                 SshPacketReceiveProc received_packet,
                                 SshPacketEofProc received_eof,
                                 SshPacketCanSendProc can_send,
                                 void *context)
{
  SshPacketWrapper down;

  down = ssh_xcalloc(1, sizeof(*down));
  down->stream = down_stream;
  ssh_buffer_init(&down->incoming);
  ssh_buffer_init(&down->outgoing);
  ssh_buffer_init(&down->outgoing_packet);
  down->incoming_eof = FALSE;
  down->outgoing_eof = FALSE;
  down->send_blocked = TRUE;
  down->can_receive = FALSE;
  down->destroy_pending = FALSE;
  down->cannot_destroy = FALSE;
  down->destroy_requested = FALSE;
  down->shortcircuited = FALSE;

  /* Save the callback functions. */
  down->received_packet = received_packet;
  down->received_eof = received_eof;
  down->can_send = can_send;
  down->context = context;

  /* Set callback for the downward stream.  Note that this will also cause
     can_send to be called from the output callback. */
  ssh_stream_set_callback(down->stream, ssh_packet_wrapper_callback,
                          (void *)down);

  /* Enable receives. */
  ssh_packet_wrapper_can_receive(down, TRUE);

  return down;
}
Ejemplo n.º 14
0
void tcp_connect_socks_connect_done_cb(SshTcpError error,
                                       SshStream stream,
                                       void *context)
{
    SshFSMThread thread = (SshFSMThread) context;
    ConnectContext c = (ConnectContext) ssh_fsm_get_gdata(thread);
    struct SocksInfoRec socksinfo;
    SocksError ret;
    unsigned char host_port[64], *next = NULL;

    c->handle = NULL;

    if (error != SSH_TCP_OK)
    {
        /* Get next address. */
        if (ssh_ustrchr(c->socks_next_address, ','))
        {
            c->socks_next_address =
                ssh_ustrchr(c->socks_next_address, ',') + 1;
        }
        else
        {   /* At end of list; consider it as a failure. */
            if (tcp_connect_register_failure(thread, error))
            {
                SSH_FSM_CONTINUE_AFTER_CALLBACK(thread);
                return;
            }
            c->socks_next_address = c->socks_addresses;
        }
        /* Try connecting again. */
        SSH_FSM_SET_NEXT(tcp_connect_socks_connect);
        SSH_FSM_CONTINUE_AFTER_CALLBACK(thread);
        return;
    }

    /* Save the stream. */
    c->stream = stream;

    /* Set the callback so that we'll get any required read/write
       notifications. */
    ssh_stream_set_callback(stream, tcp_connect_socks_notify, thread);

    if (c->next_address &&
            (next = ssh_ustrchr(c->next_address, ',')) != NULL)
    {
        *next = '\0';
        next++;
    }

    if (c->socks_type == SSH_TCP_SOCKS5)
    {
        socksinfo.socks_version_number = 5;
        socksinfo.command_code = SSH_SOCKS5_COMMAND_CODE_CONNECT;
        if (c->next_address)
            socksinfo.ip = (unsigned char *) c->next_address;
        else
            socksinfo.ip = c->host_name;
    }
    else
    {
        socksinfo.socks_version_number = 4;
        socksinfo.command_code = SSH_SOCKS4_COMMAND_CODE_CONNECT;
        socksinfo.ip = (unsigned char *) c->next_address;
    }
    ssh_snprintf(host_port, sizeof(host_port), "%d", c->host_port);
    socksinfo.port = host_port;
    socksinfo.username = c->user_name;

    ssh_buffer_clear(c->socks_buf);



    SSH_FSM_SET_NEXT(tcp_connect_socks_send);
    ret = ssh_socks_client_generate_methods(c->socks_buf, &socksinfo);
    if (ret == SSH_SOCKS_SUCCESS)
        ret = ssh_socks_client_generate_open(c->socks_buf, &socksinfo);
    if (ret != SSH_SOCKS_SUCCESS)
    {
        if (next != NULL)
        {
            c->stream = NULL;
            ssh_stream_destroy(stream);
            c->next_address = next;
            SSH_FSM_SET_NEXT(tcp_connect_socks_lookup);
        }
        else
        {
            if (ret == SSH_SOCKS_ERROR_INVALID_ARGUMENT)
                c->error = SSH_TCP_NO_ADDRESS;
            else
                c->error = SSH_TCP_FAILURE;
            SSH_FSM_SET_NEXT(tcp_connect_finish);
        }
    }
    SSH_FSM_CONTINUE_AFTER_CALLBACK(thread);
}