Ejemplo n.º 1
0
static int
fq_client_connect_internal(fq_conn_s *conn_s) {
  int rv = -1;
  uint32_t cmd = htonl(FQ_PROTO_CMD_MODE);
  fq_client_disconnect_internal(conn_s);
  conn_s->cmd_fd = fq_socket_connect(conn_s);
  if(conn_s->cmd_fd < 0) goto shutdown;
  fq_debug(FQ_DEBUG_CONN, "connect(cmd_fd) -> %d\n", conn_s->cmd_fd);
  if(write(conn_s->cmd_fd, &cmd, sizeof(cmd)) != sizeof(cmd))
    goto shutdown;
  if((rv = fq_client_do_auth(conn_s)) < 0) {
    fq_debug(FQ_DEBUG_CONN, "fq_client_do_auth -> %d\n", rv);
    goto shutdown;
  }
  if(conn_s->auth_hook) {
    if(conn_s->sync_hooks) enqueue_auth_hook_req(conn_s, 0);
    else conn_s->auth_hook((fq_client)conn_s, 0);
  }
  return 0;

 shutdown:
  if(conn_s->cmd_fd >= 0) {
    int toclose = conn_s->cmd_fd;
    conn_s->cmd_fd = -1;
    fq_debug(FQ_DEBUG_CONN, "close(cmd_fd) (in auth)\n");
    close(toclose);
    if(conn_s->disconnect_hook) conn_s->disconnect_hook(conn_s);
  }
  if(conn_s->auth_hook) {
    if(conn_s->sync_hooks) enqueue_auth_hook_req(conn_s, rv);
    else conn_s->auth_hook((fq_client)conn_s, rv);
  }
  return -1;
}
Ejemplo n.º 2
0
static int
fq_client_data_connect_internal(fq_conn_s *conn_s) {
  int flags;
  uint32_t cmd = htonl(conn_s->peermode ? FQ_PROTO_PEER_MODE
                                        : FQ_PROTO_DATA_MODE);
  /* We don't support data connections when the cmd connection is down */
  if(conn_s->cmd_fd < 0) return -1;

  if(conn_s->data_fd >= 0) {
    int toclose = conn_s->data_fd;
    conn_s->data_fd = -1;
    fq_debug(FQ_DEBUG_CONN, "close(data_fd)\n");
    close(toclose);
  }
  conn_s->data_fd = fq_socket_connect(conn_s);
  if(conn_s->data_fd < 0) goto shutdown;
  fq_debug(FQ_DEBUG_CONN, "connect(data_fd) -> %d\n", conn_s->data_fd);
  if(write(conn_s->data_fd, &cmd, sizeof(cmd)) != sizeof(cmd))
    goto shutdown;
  if(conn_s->peermode) {
    if(write(conn_s->data_fd, &conn_s->peermode,
             sizeof(conn_s->peermode)) != sizeof(conn_s->peermode))
      goto shutdown;
  }
#ifdef DEBUG
      {
        char hex[260];
        if(fq_rk_to_hex(hex, sizeof(hex), &conn_s->key) >= 0)
          fq_debug(FQ_DEBUG_CONN, "client keying:\n%s\n", hex);
      }
#endif
  if(fq_write_short_cmd(conn_s->data_fd,
                        conn_s->key.len, conn_s->key.name) < 0) {
    goto shutdown;
  }
  conn_s->tosend_offset = 0;
  if(((flags = fcntl(conn_s->data_fd, F_GETFL, 0)) == -1) ||
     (fcntl(conn_s->data_fd, F_SETFL, flags | O_NONBLOCK) == -1))
    goto shutdown;

  return 0;

 shutdown:
  if(conn_s->data_fd >= 0) {
    int toclose = conn_s->data_fd;
    conn_s->data_fd = -1;
    fq_debug(FQ_DEBUG_CONN, "close(data_fd)\n");
    close(toclose);
  }
  return -1;
}
Ejemplo n.º 3
0
static int
fq_client_connect_internal(fq_conn_s *conn_s) {
  int rv;
  uint32_t cmd = htonl(FQ_PROTO_CMD_MODE);
  fq_client_disconnect_internal(conn_s);
  conn_s->cmd_fd = fq_socket_connect(conn_s);
  if(conn_s->cmd_fd < 0) goto shutdown;
  if(write(conn_s->cmd_fd, &cmd, sizeof(cmd)) != sizeof(cmd))
    goto shutdown;
  if((rv = fq_client_do_auth(conn_s)) < 0) {
#ifdef DEBUG
    fq_debug(FQ_DEBUG_CONN, "fq_client_do_auth -> %d\n", rv);
#endif
    goto shutdown;
  }
  return 0;

 shutdown:
  if(conn_s->cmd_fd >= 0) {
    close(conn_s->cmd_fd);
    conn_s->cmd_fd = -1;
  }
  return -1;
}