Exemplo n.º 1
0
void pkr_relay_incoming(pk_lua_t* LUA, int result, void* void_data) {
  struct incoming_conn_state* ics = (struct incoming_conn_state*) void_data;

  PK_TRACE_FUNCTION;

  if (ics->parse_state == PARSE_FAILED) {
    pk_log(PK_LOG_TUNNEL_DATA|PK_LOG_ERROR,
           "pkr_relay_incoming() invoked for dead ics");
    return; /* Should never happen */
  }

  if (result == PARSE_WANT_MORE_DATA) {
    if (time(NULL) - ics->created > 5) {
      /* We don't wait forever for more data; that would make resource
       * exhaustion DOS attacks against the server trivially easy. */
      _pkr_close(ics, 0, "Timed out");
    }
    else {
      /* Slow down a tiny bit, unless there are jobs in the queue waiting
       * for us to finish. */
      if (ics->pkm->blocking_jobs.count < 1) sleep_ms(100);
      _pkr_process_readable(ics);
    }
  }
  else {
    /* If we get this far, then the request has been parsed and we have
     * in ics details about what needs to be done. So we do it! :)
     */
    _pkr_close(ics, 0, "FIXME");
  }
}
Exemplo n.º 2
0
void pkr_new_conn_readable_cb(EV_P_ ev_io* w, int revents)
{
  struct incoming_conn_state* ics = (struct incoming_conn_state*) w->data;

  PK_TRACE_FUNCTION;

  ev_io_stop(EV_A_ w);
  _pkr_process_readable(ics);

  /* -Wall dislikes unused arguments */
  (void) loop;
  (void) revents;
}
Exemplo n.º 3
0
void pkr_relay_incoming(int result, void* void_data) {
  struct incoming_conn_state* ics = (struct incoming_conn_state*) void_data;

  PK_TRACE_FUNCTION;

  if (ics->parse_state == PARSE_FAILED) {
    pk_log(PK_LOG_TUNNEL_DATA|PK_LOG_ERROR,
           "pkr_relay_incoming() invoked for dead ics");
    return; /* Should never happen */
  }

  if (result == PARSE_WANT_MORE_DATA) {
    if (pk_time() - ics->created > 5) {
      /* We don't wait forever for more data; that would make resource
       * exhaustion DOS attacks against the server trivially easy. */
      _pkr_close(ics, 0, "Timed out");
    }
    else {
      /* Slow down a tiny bit, unless there are jobs in the queue waiting
       * for us to finish. */
      if (ics->pkm->blocking_jobs.count < 1) sleep_ms(100);
      _pkr_process_readable(ics);
    }
  }
  else {
    /* If we get this far, then the request has been parsed and we have
     * in ics details about what needs to be done. So we do it! :)
     */

    if (ics->parsed_as == PROTO_PAGEKITE) {
      char* response = NULL;
      struct pke_event* ev = pke_post_blocking_event(NULL,
        PK_EV_TUNNEL_REQUEST, 0, ics->unparsed_data, NULL, &response);

      int rlen = 0;
      if (response && *response) {
        rlen = strlen(response);
        pkc_write(&(ics->pkb->conn), response, rlen);
      }

      int result_ok = rlen && ev && (ev->response_code & PK_EV_RESPOND_TRUE);
      if (result_ok) {
        /* FIXME: Upgrade this connection to a tunnel */

        int flying = 0;
        struct pk_kite_request* pkr = pk_parse_pagekite_response(
          response, rlen + 1, NULL, NULL);
        if (pkr != NULL) {
          struct pk_kite_request* p;
          for (p = pkr; p->status != PK_KITE_UNKNOWN; p++) {
            if (p->status == PK_KITE_FLYING) {
              /* FIXME: Record kite->tunnel mapping */
              pk_log(PK_LOG_MANAGER_DEBUG, "Accepted kite: %s://%s:%d",
                                           p->kite->protocol,
                                           p->kite->public_domain,
                                           p->kite->public_port);
              flying++;
            }
            else {
              pk_log(PK_LOG_MANAGER_DEBUG, "Rejected kite: %s://%s:%d (%d)",
                                           p->kite->protocol,
                                           p->kite->public_domain,
                                           p->kite->public_port,
                                           p->status);
            }
          }
          free(pkr);

          /* FIXME: Add to event loop */
        }

        if (!flying) result_ok = 0;
      }

      if (ev) pke_free_event(NULL, ev->event_code);
      if (response) free(response);
      if (!result_ok) _pkr_close(ics, 0, "Rejected");
    }
    else {
      _pkr_close(ics, 0, "FIXME");
    }
  }
}