Beispiel #1
0
apr_status_t use_new_connection(test_baton_t *tb,
                                apr_pool_t *pool)
{
    apr_uri_t url;
    apr_status_t status;

    if (tb->connection)
        cleanup_conn(tb->connection);
    tb->connection = NULL;

    status = apr_uri_parse(pool, tb->serv_url, &url);
    if (status != APR_SUCCESS)
        return status;

    status = serf_connection_create2(&tb->connection, tb->context,
                                     url,
                                     tb->conn_setup,
                                     tb,
                                     default_closed_connection,
                                     tb,
                                     pool);
    apr_pool_cleanup_register(pool, tb->connection, cleanup_conn,
                              apr_pool_cleanup_null);

    return status;
}
Beispiel #2
0
static int handle_conn_new(int passive_sock) {
  int ret = 0;
  struct conn *p_conn = *(free_conn_top - 1);
  struct pollfd *p_pollfd = conn_pollfds + num_active_conns;
  struct sockaddr_in addr;
  socklen_t addr_size = sizeof(addr);

  /* Accept an incoming connection request. */
  if ((p_conn->sock =
        r_accept(passive_sock, (struct sockaddr *)&addr, &addr_size)) == -1) {
    if (errno == EAGAIN || errno == EWOULDBLOCK) {
      ret = 0;
    } else {
      log_perror(NULL, "Couldn't accept connection request");
      ret = -1;
    }
    goto cleanup;
  }

  /* Format the client's IP address as a string. */
  if (!inet_ntop(AF_INET, &addr.sin_addr, p_conn->addr_str,
        sizeof(p_conn->addr_str))) {
    log_perror(NULL, "Couldn't format remote IP address");
    ret = -1;
    goto cleanup;
  }

  /* Set the new connection's socket in nonblocking mode. */
  if (fcntl(p_conn->sock, F_SETFL, O_NONBLOCK) == -1) {
    log_perror(p_conn->addr_str,
        "Couldn't put client socket in nonblocking mode");
    ret = -1;
    goto cleanup;
  }

  /* Update the pollfd--connection mapping table. */
  pollfd_idxs_to_conns[num_active_conns] = p_conn;
  p_conn->pollfd_idx = num_active_conns;

  /* Log some debug info. */
  log_debug(NULL, "Allocating pollfd %lu to connection %lu/socket %d",
      (unsigned long) p_conn->pollfd_idx,
      (unsigned long) p_conn->conn_idx,
      p_conn->sock);

cleanup:
  if (!ret && p_conn->sock != -1) {
    --free_conn_top;
    ++num_active_conns;

    p_conn->stage = STAGE_RESPONSE_START;
    p_pollfd->fd = p_conn->sock;
    p_pollfd->events = POLLIN;
    p_pollfd->revents = 0;
  } else if (ret == -1) {
    cleanup_conn(p_conn);
  }

  return ret;
}
Beispiel #3
0
static void handle_conn_delete(struct conn *p_conn, struct pollfd *p_pollfd) {
  struct pollfd *p_last_pollfd = conn_pollfds + --num_active_conns;
  struct conn *p_last_conn = pollfd_idxs_to_conns[num_active_conns];

  /* Log some debug info. */
  log_debug(NULL, "Deallocating pollfd %lu from connection %lu/socket %d",
      (unsigned long) p_conn->pollfd_idx,
      (unsigned long) p_conn->conn_idx,
      p_conn->sock);

  /* Return the connection slot to the free stack. */
  *(free_conn_top++) = p_conn;

  /* Overwrite the connection's pollfd with the last active pollfd, deleting
   * the old pollfd in constant time. */
  memcpy(p_pollfd, p_last_pollfd, sizeof(*p_pollfd));
  p_last_pollfd->fd = -1;
  p_last_pollfd->events = 0;
  p_last_pollfd->revents = 0;

  /* Update the pollfd--connection mappings. */
  pollfd_idxs_to_conns[p_conn->pollfd_idx] = p_last_conn;
  pollfd_idxs_to_conns[num_active_conns] = NULL;
  p_last_conn->pollfd_idx = p_conn->pollfd_idx;

  /* Close the connection socket and free its associated resources. */
  cleanup_conn(p_conn);
}
Beispiel #4
0
void sig_handler(int sig) {
    FILE *fp;
    int  i;
    mDB_exit();
    perror("Received signal, shutting down.");
    for (i=1; i<=contot; i++) if (pollfds[i].fd>0) cleanup_conn(i,pollfds[i].fd);
    exit(1);
}
Beispiel #5
0
/**********************************************************************
*%FUNCTION: rp_fatal
*%ARGUMENTS:
* str -- error message
*%RETURNS:
* Nothing
*%DESCRIPTION:
* Prints a message to stderr and syslog and exits.
***********************************************************************/
void
rp_fatal(char const *str)
{
    printErr(str);
    sendPADTf(conn, "RP-PPPoE: %.256s", str);
    close_conn(0);
    cleanup_conn();
    exit(1);
}
Beispiel #6
0
/**********************************************************************
*%FUNCTION: fatalSys
*%ARGUMENTS:
* str -- error message
*%RETURNS:
* Nothing
*%DESCRIPTION:
* Prints a message plus the errno value to stderr and syslog and exits.
*
***********************************************************************/
void
fatalSys(char const *str)
{
    char buf[1024];
    int i = errno;
    sprintf(buf, "%.256s: %.256s", str, strerror(i));
    printErr(buf);
    sprintf(buf, "RP-PPPoE: %.256s: %.256s", str, strerror(i));
    sendPADT(conn, buf);
    close_conn(0);
    cleanup_conn();
    exit(1);
}
Beispiel #7
0
/**********************************************************************
 * %FUNCTION: PPPOEInitDevice
 * %ARGUMENTS:
 * None
 * %RETURNS:
 *
 * %DESCRIPTION:
 * Initializes PPPoE device.
 ***********************************************************************/
static int
PPPOEInitDevice(void)
{
    cleanup_conn();
    if (strlen(devnam) >= IFNAMSIZ) {
	fatal("Device name %s is too long - max length %d",
	      devnam, (int) IFNAMSIZ);
    }
    conn = malloc(sizeof(PPPoEConnection));
    if (!conn) {
	fatal("Could not allocate memory for PPPoE session");
    }
    memset(conn, 0, sizeof(PPPoEConnection));
    conn->ifName = devnam;
    conn->discoverySocket = -1;
    conn->sessionSocket = -1;
    conn->useHostUniq = 1;
    conn->printACNames = printACNames;
    conn->discoveryTimeout = PADI_TIMEOUT;
    return 1;
}