SshStream ssh_stream_fd_wrap2(SshIOHandle readfd, SshIOHandle writefd,
                              Boolean close_on_destroy)
{
  SshFdStream sdata;
  SshStream str;

  sdata = ssh_malloc(sizeof(*sdata));

  if (sdata == NULL)
    return NULL;

  memset(sdata, 0, sizeof(*sdata));
  sdata->readfd = readfd;
  sdata->writefd = writefd;
  sdata->close_on_destroy = close_on_destroy;
  sdata->read_has_failed = FALSE;
  sdata->write_has_failed = FALSE;
  sdata->destroyed = FALSE;
  sdata->keep_nonblocking = FALSE;
  sdata->callback = NULL_FNPTR;
  if (readfd >= 0)
    {
      if (ssh_io_register_fd(readfd, ssh_stream_fd_callback, (void *)sdata)
           == FALSE )
        {
          ssh_free(sdata);
          return NULL;
        }
    }
  if (readfd != writefd && writefd >= 0)
    {
      if (ssh_io_register_fd(writefd, ssh_stream_fd_callback, (void *)sdata)
           == FALSE )
        {
          if (readfd >= 0)
            ssh_io_unregister_fd(readfd,TRUE);
          ssh_free(sdata);
          return NULL;
        }
    }
  str = ssh_stream_create(&ssh_stream_fd_methods, (void *)sdata);
  if (str == NULL)
    {
      ssh_free(sdata);
      if (readfd >= 0)
        ssh_io_unregister_fd(readfd, TRUE);
      if (readfd != writefd && writefd >= 0)
        ssh_io_unregister_fd(writefd, TRUE);
      return NULL;
    }
  return str;

}
Exemple #2
0
/* Open interface to tls acceleration */
Boolean tls_accel_open(void (*rd_cb)(unsigned int, void *))
{
#ifdef __linux__
  const char *name = "/proc/quicksec/hwaccel";

  if (device_fd >= 0)
    return TRUE;

  /* Try to open the device. */
  device_fd = open(name, O_RDWR);
  device_rd_fd = open(name, O_RDWR);

  if (device_fd == -1 || device_rd_fd == -1)
    {
      SSH_DEBUG(1 , ("Hardware accelerator n/a"));
      tls_accel_close();
      return FALSE;
    }

  registered = ssh_io_register_fd(device_rd_fd, rd_cb, NULL);
  if (!registered)
    {
      SSH_DEBUG(SSH_D_FAIL, ("ssh_io_register_fd failed"));
      tls_accel_close();
      return FALSE;
    }
  ssh_io_set_fd_request(device_rd_fd, SSH_IO_READ);

  return TRUE;
#else
  return FALSE;
#endif /* __linux__ */
}
Exemple #3
0
void ssh_local_connect(const char *path,
                       SshLocalCallback callback,
                       void *context)
{
    int sock;
    SshLocalConnect c;

    /* Create a unix-domain socket. */
    sock = socket(AF_UNIX, SOCK_STREAM, 0);
    if (sock < 0)
    {
        /* Failed to create the socket. */
        (*callback)(NULL, context);
        return;
    }

    /* Allocate and initialize a context structure. */
    c = ssh_xmalloc(sizeof(*c));
    c->path = ssh_xstrdup(path);
    c->sock = sock;
    c->callback = callback;
    c->context = context;

    /* Register the file descriptor.  Note that this also makes it
       non-blocking. */
    ssh_io_register_fd(sock, ssh_local_connect_try, c);

    /* Fake a callback to start asynchronous connect. This connect could be
       done on this current routine, but we want this to be similar with
       tcp/ip socket code, so we use the try-routines */
    ssh_local_connect_try(SSH_IO_WRITE, (void *)c);
}
Exemple #4
0
SshLocalListener ssh_local_make_listener(const char *path,
        SshLocalCallback callback,
        void *context)
{
    int sock;
    struct sockaddr_un sunaddr;
    SshLocalListener listener;

    /* Create a socket for the listener. */
    sock = socket(AF_UNIX, SOCK_STREAM, 0);
    if (sock < 0)
    {
        ssh_warning("Can not create local domain socket: %.200s",
                    strerror(errno));
        return NULL;

    }

    /* Initialize a unix-domain address structure. */
    memset(&sunaddr, 0, sizeof(sunaddr));
    sunaddr.sun_family = AF_UNIX;
    strncpy(sunaddr.sun_path, path, sizeof(sunaddr.sun_path));

    /* Bind the socket to the address.  This will create the socket in the file
       system, and will fail if the socket already exists. */
    if (bind(sock, (struct sockaddr *)&sunaddr, AF_UNIX_SIZE(sunaddr)) < 0)
    {
        close(sock);
        ssh_warning("Can not bind local address %.200s: %.200s",
                    path, strerror(errno));
        return NULL;
    }

    /* Start listening for connections to the socket. */
    if (listen(sock, 5) < 0)
    {
        close(sock);
        ssh_warning("Can not listen to local address %.200s: %.200s",
                    path, strerror(errno));
        return NULL;
    }

    /* Allocate and initialize the listener structure. */
    listener = ssh_xmalloc(sizeof(*listener));
    listener->sock = sock;
    listener->path = ssh_xstrdup(path);
    listener->callback = callback;
    listener->context = context;

    /* ssh_local_listen_callback will call the user supplied callback
       when after new connection is accepted. It also creates stream
       object for the new connection and calls callback. */
    ssh_io_register_fd(sock, ssh_local_listen_callback, (void *)listener);
    ssh_io_set_fd_request(sock, SSH_IO_READ);

    return listener;
}
Exemple #5
0
SshStream ssh_stream_fd_wrap2(int readfd, int writefd,
			      Boolean close_on_destroy)
{
  SshFdStream sdata;

  sdata = ssh_xmalloc(sizeof(*sdata));
  memset(sdata, 0, sizeof(*sdata));
  sdata->readfd = readfd;
  sdata->writefd = writefd;
  sdata->close_on_destroy = close_on_destroy;
  sdata->read_has_failed = FALSE;
  sdata->write_has_failed = FALSE;
  sdata->destroyed = FALSE;
  sdata->in_callback = FALSE;
  sdata->keep_nonblocking = FALSE;
  sdata->callback = NULL;
  if (readfd >= 0)
    ssh_io_register_fd(readfd, ssh_stream_fd_callback, (void *)sdata);
  if (readfd != writefd && writefd >= 0)
    ssh_io_register_fd(writefd, ssh_stream_fd_callback, (void *)sdata);
  return ssh_stream_create(&ssh_stream_fd_methods, (void *)sdata);
}