示例#1
0
文件: util.c 项目: CaptainCPS/openisr
pk_err_t setup_signal_handlers(void (*caught_handler)(int sig),
			const int *caught_signals, const int *ignored_signals)
{
	int i;

	if (caught_signals != NULL) {
		for (i=0; caught_signals[i] != 0; i++) {
			if (set_signal_handler(caught_signals[i],
						caught_handler)) {
				pk_log(LOG_ERROR, "unable to register signal "
						"handler for signal %d",
						caught_signals[i]);
				return PK_CALLFAIL;
			}
		}
	}
	if (ignored_signals != NULL) {
		for (i=0; ignored_signals[i] != 0; i++) {
			if (set_signal_handler(ignored_signals[i], SIG_IGN)) {
				pk_log(LOG_ERROR, "unable to ignore signal %d",
						ignored_signals[i]);
				return PK_CALLFAIL;
			}
		}
	}
	return PK_SUCCESS;
}
示例#2
0
int pklua_lua_pklua_bridge_hook_to_lua(lua_State *L)
{
  pk_log(PK_LOG_LUA_DEBUG, "pklua_lua_pklua_bridge_hook_to_lua(%p)", L);
  int n = lua_gettop(L);
  if (n != 2 ||
      !lua_istable(L, 1) ||
      !lua_isnumber(L, 2)) {
    lua_pushstring(L, "Incorrect arguments in bridge_hook_to_lua");
    return lua_error(L);
  }
  int hook_id = lua_tointeger(L, 2);
  lua_remove(L, 1);
  lua_remove(L, 1);

  pagekite_callback2_t* cb = &pklua_hook_bridge;
  if ((hook_id < 0) ||
      (hook_id >= PK_HOOK_MAX) ||
      ((pk_hooks[hook_id] != NULL) && (pk_hooks[hook_id] != cb))) {
    pk_log(PK_LOG_ERROR, "Hook invalid or already registered: %d", hook_id);
    lua_pushstring(L, "Hook invalid or already registered");
    return lua_error(L);
  }

  pk_hooks[hook_id] = cb;
  return 0;
}
示例#3
0
int pkb_start_blockers(struct pk_manager *pkm, int n)
{
  int i;
  for (i = 0; i < MAX_BLOCKING_THREADS && n > 0; i++) {
    if (pkm->blocking_threads[i] == NULL) {
      pkm->blocking_threads[i] = malloc(sizeof(struct pk_blocker));
      pkm->blocking_threads[i]->manager = pkm;
      if (0 > pthread_create(&(pkm->blocking_threads[i]->thread), NULL,
                             pkb_run_blocker,
                             (void *) pkm->blocking_threads[i])) {
        pk_log(PK_LOG_MANAGER_ERROR, "Failed to start blocking thread.");
        free(pkm->blocking_threads[i]);
#if HAVE_LUA
        pklua_close_lua(pkm->blocking_threads[i]->lua);
#endif
        pkm->blocking_threads[i] = NULL;
        return (pk_error = ERR_NO_THREAD);
      }
      n--;
    }
    else {
      pk_log(PK_LOG_MANAGER_ERROR, "Blocking thread %d already started?", i);
    }
  }
  return 0;
}
示例#4
0
int pkb_check_frontend_dns(struct pk_manager* pkm)
{
  int i, changes, have_nulls;
  time_t obsolete;
  struct pk_tunnel* fe;
  char* last_fe_hostname;

  PK_TRACE_FUNCTION;

  /* Walk through frontend list, look each up in DNS and add new entries
   * as necessary.
   */
  changes = 0;
  have_nulls = 0;
  last_fe_hostname = "";
  for (i = 0, fe = pkm->tunnels; i < pkm->tunnel_max; i++, fe++) {
    if ((fe->fe_hostname != NULL) &&
        (0 != strcmp(fe->fe_hostname, last_fe_hostname)))
    {
      pk_log(PK_LOG_MANAGER_DEBUG, "Checking for new IPs: %s", fe->fe_hostname);
      changes += pkm_add_frontend(pkm, fe->fe_hostname, fe->fe_port, 0);
      last_fe_hostname = fe->fe_hostname;
    }
    if ((fe->fe_hostname != NULL) && (fe->ai.ai_addr == NULL)) {
      have_nulls += 1;
    }
  }
  pk_log(PK_LOG_MANAGER_DEBUG, "Found %d new IPs", changes);

  /* 2nd pass: walk through list and remove obsolete entries
   * We only do this if we have null records which will let us re-discover
   * everything again if DNS went away temporarily.
   */
  if (have_nulls)
  {
    obsolete = time(0) - (pkm->check_world_interval * 4);
    for (i = 0, fe = pkm->tunnels; i < pkm->tunnel_max; i++, fe++) {
      if ((fe->fe_hostname != NULL) &&
          (fe->ai.ai_addr != NULL) &&
          (fe->last_configured < obsolete) &&
          (fe->last_ping < obsolete) &&
          (fe->conn.sockfd <= 0))
      {
        free(fe->fe_hostname);
        free_addrinfo_data(&fe->ai);
        fe->fe_hostname = NULL;
      }
    }
  }

  PK_CHECK_MEMORY_CANARIES;
  return changes;
}
示例#5
0
void pk_perror(const char* prefix) {
  if (ERR_ALL_IS_WELL == pk_error) return;

  switch (pk_error) {
    /* FIXME: Make this prettier */
    case ERR_CONNECT_CONNECT:
      pk_log(PK_LOG_ERROR, "%s: %s", prefix, strerror(errno));
      break;
    default:
      pk_log(PK_LOG_ERROR, "%s: pk_error = %d", prefix, pk_error);
  }

  pk_error = ERR_ALL_IS_WELL;
}
示例#6
0
int pklua_hook_bridge(int hook_id, int iv, void* p1, void* p2) {
  int rv = -1;

  pk_lua_t* PL = pklua_get_locked_lua(NULL);
  if (PL == NULL) {
    pk_log(PK_LOG_ERROR, "No Lua state found for hook %d", hook_id);
    return -1;
  }

  lua_State* L = PL->lua;
  lua_getfield(L, LUA_GLOBALSINDEX, "pklua");
  lua_getfield(L, -1, "_hook_bridge");
  lua_pushvalue(L, -2);
  lua_pushinteger(L, hook_id);
  lua_pushinteger(L, iv);
  if (p1) lua_pushlightuserdata(L, p1); else lua_pushnil(L);
  if (p2) lua_pushlightuserdata(L, p2); else lua_pushnil(L);
  if (lua_pcall(L, 5, 1, 0) == 0) {
    rv = lua_tointeger(L, -1);
    lua_remove(L, -1);
  }
  lua_remove(L, -1);

  pklua_unlock_lua(PL);
  return rv;
}
示例#7
0
int pklua_configure(pk_lua_t *PL, struct pk_manager* pkm)
{
  lua_State* L = pklua_lock_lua(PL)->lua;

  pk_log(PK_LOG_LUA_DEBUG, "pklua_configure(%p, %p)", L, pkm);
  lua_getfield(L, LUA_GLOBALSINDEX, "pklua");

  lua_getfield(L, -1, "_enable_defaults");
  lua_pushvalue(L, -2);
  lua_pushboolean(L, pkm->lua_enable_defaults);
  lua_call(L, 2, 0);

  for (const char** c = pkm->lua_settings; c && *c; c++) {
    lua_getfield(L, -1, "_set_setting");
    lua_pushvalue(L, -2);
    lua_pushstring(L, *c);
    lua_call(L, 2, 0);
  }

  lua_getfield(L, -1, "_init_plugins");
  lua_pushvalue(L, -2);
  lua_call(L, 1, 0);

  lua_remove(L, -1);

  pklua_unlock_lua(PL);
  return 0;
}
示例#8
0
int pklua_lua_pklua_get_hook_list(lua_State* L)
{
  pk_log(PK_LOG_LUA_DEBUG, "pklua_lua_pklua_get_hook_list(%p)", L);

  lua_newtable(L);
  #define PKM_HOOK(n) lua_pushinteger(L, n); lua_setfield(L, -2, #n)
  PKM_HOOK(PK_HOOK_STOPPED);
  PKM_HOOK(PK_HOOK_START_EV_LOOP);
  PKM_HOOK(PK_HOOK_START_BLOCKER);
  PKM_HOOK(PK_HOOK_LOG);
  PKM_HOOK(PK_HOOK_TICK);
  PKM_HOOK(PK_HOOK_CHECK_WORLD);
  PKM_HOOK(PK_HOOK_CHECK_TUNNELS);
  PKM_HOOK(PK_HOOK_STATE_CHANGED);
  PKM_HOOK(PK_HOOK_BE_CONN_WRAP);
  PKM_HOOK(PK_HOOK_FE_CONN_WRAP);
  PKM_HOOK(PK_HOOK_BE_CONN_OPENED);
  PKM_HOOK(PK_HOOK_BE_CONN_CLOSED);
  PKM_HOOK(PK_HOOK_FE_CONN_OPENED);
  PKM_HOOK(PK_HOOK_FE_CONN_CLOSED);
  PKM_HOOK(PK_HOOK_FE_DISCONNECT);
  PKM_HOOK(PK_HOOK_CHUNK_INCOMING);
  PKM_HOOK(PK_HOOK_CHUNK_OUTGOING);
  PKM_HOOK(PK_HOOK_DATA_INCOMING);
  PKM_HOOK(PK_HOOK_DATA_OUTGOING);

  return 1;
}
示例#9
0
static int pklua_socket_server_accepted_cb(int sockfd, void* void_data) {
  pk_log(PK_LOG_LUA_DEBUG,
         "pklua_socket_server_accepted_cb(%d, %p)", sockfd, void_data);
  pklua_socket_server_cb_data* data = (pklua_socket_server_cb_data*) void_data;
  pkb_add_job(&(data->pkm->blocking_jobs), PK_ACCEPT_LUA, sockfd, void_data);
  return 0;
}
示例#10
0
int pklua_lua_socket_peek(lua_State *L)
{
  pk_log(PK_LOG_LUA_DEBUG, "pklua_lua_socket_peek(%p)", L);
  int sockfd = _pklua_lua_socket_get_sockfd(L);
  /* FIXME! This will be needed for implementing a pagekite front-end */
  return 0;
}
示例#11
0
static void _pkr_close(struct incoming_conn_state* ics,
                       int log_level,
                       const char* reason)
{
  char rbuf[128];
  char lbuf[128];

  /* FIXME: Log more useful things about the connection. */
  pk_log(PK_LOG_TUNNEL_CONNS | log_level,
        "%s; remote=%s; local=%s; hostname=%s; proto=%s; fd=%d",
        reason,
        in_addr_to_str((struct sockaddr*) &(ics->remote_addr), rbuf, 128),
        in_addr_to_str((struct sockaddr*) &(ics->local_addr), lbuf, 128),
        ics->hostname,
        known_protos[ics->parsed_as],
        ics->pkb->conn.sockfd);

  if (ics->pkb) {
    pkc_reset_conn(&(ics->pkb->conn), 0);
    pkm_free_be_conn(ics->pkb);
  }
  ics->parse_state = PARSE_FAILED; /* A horrible hack */

  if (ics->unparsed_data != NULL) free(ics->unparsed_data);
  free(ics);
}
示例#12
0
int pklua_lua_socket_close(lua_State* L)
{
  pk_log(PK_LOG_LUA_DEBUG, "pklua_lua_socket_close(%p)", L);
  int sockfd = _pklua_lua_socket_get_sockfd(L);
  if (sockfd > -1) PKS_close(sockfd);
  return 0;
}
示例#13
0
void pkb_log_fe_status(struct pk_manager* pkm)
{
  int j, ddnsup_ago;
  struct pk_tunnel* fe;
  char printip[128];
  char ddnsinfo[128];

  PK_TRACE_FUNCTION;

  for (j = 0, fe = pkm->tunnels; j < pkm->tunnel_max; j++, fe++) {
    if (fe->ai.ai_addr && fe->fe_hostname) {
      if (NULL != in_addr_to_str(fe->ai.ai_addr, printip, 128)) {
        ddnsinfo[0] = '\0';
        if (fe->last_ddnsup) {
          ddnsup_ago = time(0) - fe->last_ddnsup;
          sprintf(ddnsinfo, " (in DNS %us ago)", ddnsup_ago);
        }
        pk_log(PK_LOG_MANAGER_DEBUG,
               "Relay; status=0x%8.8x; errors=%d; info=%s%s%s%s%s%s%s%s",
               fe->conn.status,
               fe->error_count,
               printip,
               (fe->conn.status & FE_STATUS_REJECTED) ? " rejected": "",
               (fe->conn.status & FE_STATUS_WANTED) ? " wanted": "",
               (fe->conn.status & FE_STATUS_LAME) ? " lame": "",
               (fe->conn.status & FE_STATUS_IN_DNS) ? " in-DNS": "",
               (fe->conn.status & FE_STATUS_IS_FAST) ? " fast": "",
               (fe->conn.sockfd > 0) ? " live" : "",
               ddnsinfo);
      }
    }
  }
}
示例#14
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");
  }
}
示例#15
0
void pkb_check_tunnels(struct pk_manager* pkm)
{
  int problems = 0;
  int dns_is_down = 0;
  PK_TRACE_FUNCTION;

  pk_log(PK_LOG_MANAGER_DEBUG,
         "Checking network & tunnels... (v%s)", PK_VERSION);

  dns_is_down = (0 != pkb_check_kites_dns(pkm));

  if (!dns_is_down && (pkb_check_frontend_dns(pkm) > 0)) {
    pkb_update_state(pkm, dns_is_down, problems);
    pkb_check_world(pkm);
  }

  pkb_choose_tunnels(pkm);
  pkb_log_fe_status(pkm);
  problems += pkm_reconnect_all(pkm, dns_is_down);

  if (!problems) pkm_disconnect_unused(pkm);

  if (pkm->dynamic_dns_url && (pkm->status != PK_STATUS_REJECTED)) {
    problems += pkb_update_dns(pkm);
  }

  pkb_update_state(pkm, dns_is_down, problems);
}
示例#16
0
lua_State* pklua_get_lua(struct pk_manager* pkm) {
  lua_State* L = lua_open();
  const luaL_Reg* reg;

  /* FIXME: We would rather not use luaL_openlibs(L), because we probably
   *        want a stricter sandbox? */;
  luaL_openlibs(L);

  /* Load compiled-in pklua.lua + plugins */
  if ((0 != luaL_loadstring(L, pklualua)) ||
      (0 != lua_pcall(L, 0, LUA_MULTRET, 0))) {
    pk_log(PK_LOG_ERROR, "Lua init failed: %s", lua_tostring(L, -1));
    return NULL;
  }
  else {
    /* Add some native methods, register as pklua */
    for (reg = pklua_methods; reg->func != NULL; reg++) {
      luaL_register(L, NULL, reg);
    } 
    lua_setglobal(L, "pklua");

    lua_getfield(L, LUA_GLOBALSINDEX, "pklua");
    lua_pushstring(L, PK_VERSION);
    lua_setfield(L, -2, "version");
    lua_remove(L, -1);

    pklua_configure(L, pkm);
    luaL_dostring(L, "pklua:log('Loaded pklua.lua v' .. pklua.version);");
  }

  return L;
}
示例#17
0
void pkb_check_world(struct pk_manager* pkm)
{
  PK_TRACE_FUNCTION;

  if (pkm->status == PK_STATUS_NO_NETWORK) {
    pk_log(PK_LOG_MANAGER_DEBUG, "Waiting for network... (v%s)", PK_VERSION);
    return;
  }
  pk_log(PK_LOG_MANAGER_DEBUG,
         "Checking state of world... (v%s)", PK_VERSION);
  pkb_clear_transient_flags(pkm);
  pkb_check_tunnel_pingtimes(pkm);
  pkb_check_kites_dns(pkm);
  pkb_log_fe_status(pkm);
  pkm->last_world_update = time(0) + pkm->interval_fudge_factor;
  PK_CHECK_MEMORY_CANARIES;
}
示例#18
0
int pklua_add_listeners(lua_State* L, struct pk_manager* pkm) {
  pk_log(PK_LOG_LUA_DEBUG, "pklua_add_listeners(%p, %p)", L, pkm);
  lua_getfield(L, LUA_GLOBALSINDEX, "pklua");
  lua_getfield(L, -1, "_configure_socket_servers");
  lua_pushvalue(L, -2);
  pklua_wrap_pk_manager(L, pkm);
  lua_call(L, 2, 0);
  return 0;
}
示例#19
0
文件: util.c 项目: CaptainCPS/openisr
/* Fork, and have the parent wait for the child to indicate that the parent
   should exit.  In the parent, this returns only on error.  In the child, it
   returns success and sets *status_fd.  If the child writes a byte to the fd,
   the parent will exit with that byte as its exit status.  If the child closes
   the fd without writing anything, the parent will exit(0). */
pk_err_t fork_and_wait(int *status_fd)
{
	int fds[2];
	pid_t pid;
	char ret=1;
	int status;

	/* Make sure the child isn't killed if the parent dies */
	if (set_signal_handler(SIGPIPE, SIG_IGN)) {
		pk_log(LOG_ERROR, "Couldn't block SIGPIPE");
		return PK_CALLFAIL;
	}
	if (pipe(fds)) {
		pk_log(LOG_ERROR, "Can't create pipe");
		return PK_CALLFAIL;
	}

	pid=fork();
	if (pid == -1) {
		pk_log(LOG_ERROR, "fork() failed");
		return PK_CALLFAIL;
	} else if (pid) {
		/* Parent */
		close(fds[1]);
		if (read(fds[0], &ret, sizeof(ret)) == 0) {
			exit(0);
		} else {
			if (waitpid(pid, &status, 0) == -1)
				exit(ret);
			if (WIFEXITED(status))
				exit(WEXITSTATUS(status));
			if (WIFSIGNALED(status)) {
				set_signal_handler(WTERMSIG(status), SIG_DFL);
				raise(WTERMSIG(status));
			}
			exit(ret);
		}
	} else {
		/* Child */
		close(fds[0]);
		*status_fd=fds[1];
	}
	return PK_SUCCESS;
}
示例#20
0
static int _pkr_parse_pagekite_request(char* peeked,
                                       int bytes,
                                       char* first_nl,
                                       struct incoming_conn_state* ics)
{
  /* Since we know pagekite requests are small and peekable, we
   * can check if it's complete and process if so, otherwise defer.
   */
  if ((*(first_nl-1) == '\r') ? (strstr(peeked, "\r\n\r\n") != NULL)
                              : (strstr(peeked, "\n\n") != NULL)) {

    pk_log(PK_LOG_MANAGER_DEBUG, "FIXME: Complete PageKite request!");
    ics->parsed_as = PROTO_PAGEKITE;
    return PARSE_MATCH_SLOW;
  }

  pk_log(PK_LOG_MANAGER_DEBUG, "FIXME: Partial PageKite request...");
  return PARSE_WANT_MORE_DATA;
}
示例#21
0
int pklua_lua_socket_send(lua_State* L)
{
  pk_log(PK_LOG_LUA_DEBUG, "pklua_lua_socket_send(%p)", L);
  int sockfd = _pklua_lua_socket_get_sockfd(L);
  if (lua_gettop(L) > 1 && lua_isstring(L, 2)) {
    ssize_t dl;
    size_t data_len;
    const char *data = lua_tolstring(L, 2, &data_len);
    if ((dl = data_len) != PKS_write(sockfd, data, data_len)) {
      pk_log(PK_LOG_LUA_DEBUG, "write failed to %d", sockfd);
      return 0;
    }
    lua_pushinteger(L, data_len);
    return 1;
  }
  lua_pushstring(L, "Incorrect arguments");
  lua_error(L);
  return -1;
}
示例#22
0
int pklua_add_listeners(pk_lua_t* PL) {
  lua_State* L = pklua_lock_lua(PL)->lua;
  pk_log(PK_LOG_LUA_DEBUG, "pklua_add_listeners(%p)", L);
  lua_getfield(L, LUA_GLOBALSINDEX, "pklua");
  lua_getfield(L, -1, "_configure_socket_servers");
  lua_pushvalue(L, -2);
  int rv = lua_pcall(L, 1, 0, 0);
  pklua_unlock_lua(PL);
  return rv;
}
示例#23
0
文件: util.c 项目: CaptainCPS/openisr
int pending_signal(void)
{
	static int warned;

	if (sigstate.signal && !warned) {
		warned=1;
		pk_log(LOG_INFO, "Interrupt");
	}
	return sigstate.signal;
}
示例#24
0
int pklua_lua_pkm_add_socket_server(lua_State *L)
{
  pk_log(PK_LOG_LUA_DEBUG, "pklua_lua_pkm_add_socket_server(%p)", L);
  int n = lua_gettop(L);
  if (n != 4 ||
      !lua_istable(L, 1) ||
      !lua_isstring(L, 2) ||
      !lua_isstring(L, 3) ||
      !lua_isnumber(L, 4)) {
    lua_pushstring(L, "Incorrect arguments");
    return lua_error(L);
  } 
  lua_getfield(L, 1, "_pkm");
  if (!lua_islightuserdata(L, -1)) {
    lua_pushstring(L, "Incorrect arguments");
    return lua_error(L);
  }

  struct pk_manager* pkm = lua_touserdata(L, -1);
  lua_remove(L, -1);
  char* name = lua_tostring(L, 2);
  char* host = lua_tostring(L, 3);
  int port = lua_tointeger(L, 4);

  pklua_socket_server_cb_data* data = malloc(sizeof(pklua_socket_server_cb_data)
                                             + strlen(name) + 1); 
  strcpy(data->name, name);
  data->pkm = pkm;
  int lport = pkm_add_listener(pkm, host, port,
                               &pklua_socket_server_accepted_cb,
                               (void *) data);
  if (lport > 0) {
    pk_log(PK_LOG_LUA_INFO,
           "Listening for %s on %s:%d (port %d)", name, host, port, lport);
    return 0;
  }
  else {
    pk_log(PK_LOG_ERROR,
           "Failed to add listener %s on %s:%d", name, host, port);
    lua_pushstring(L, "Failed to add listener");
    return lua_error(L);
  }
}
示例#25
0
int pkr_conn_accepted_cb(int sockfd, void* void_pkm)
{
  struct incoming_conn_state* ics;
  struct pk_manager* pkm = (struct pk_manager*) void_pkm;
  struct pk_backend_conn* pkb;
  socklen_t slen;
  char rbuf[128];
  char lbuf[128];

  PK_TRACE_FUNCTION;

  ics = malloc(sizeof(struct incoming_conn_state));
  ics->pkm = pkm;
  ics->pkb = NULL;
  ics->tunnel = NULL;
  ics->hostname = NULL;
  ics->parsed_as = PROTO_UNKNOWN;
  ics->parse_state = PARSE_UNDECIDED;
  ics->created = pk_time();
  ics->unparsed_data = NULL;

  slen = sizeof(ics->local_addr);
  getsockname(sockfd, (struct sockaddr*) &(ics->local_addr), &slen);

  slen = sizeof(ics->remote_addr);
  getpeername(sockfd, (struct sockaddr*) &(ics->remote_addr), &slen);

  pk_log(PK_LOG_TUNNEL_DATA,
         "Accepted; remote=%s; local=%s; fd=%d",
         in_addr_to_str((struct sockaddr*) &(ics->remote_addr), rbuf, 128),
         in_addr_to_str((struct sockaddr*) &(ics->local_addr), lbuf, 128),
         sockfd);

  /* Allocate a connection for this request or die... */
  sprintf(lbuf, "!NEW:%d", ics->remote_addr.sin_port);
  if (NULL == (pkb = pkm_alloc_be_conn(pkm, NULL, lbuf))) {
    _pkr_close(ics, PK_LOG_ERROR, "BE alloc failed");
    PKS_close(sockfd);
    return -1;
  }

  pkb->kite = NULL;
  pkb->conn.sockfd = sockfd;
  ics->pkb = pkb;

  set_non_blocking(sockfd);

  ev_io_init(&(pkb->conn.watch_r),
             pkr_new_conn_readable_cb,
             PKS_EV_FD(sockfd),
             EV_READ);
  pkb->conn.watch_r.data = (void *) ics;
  ev_io_start(pkm->loop, &(pkb->conn.watch_r));
  return 0;
}
示例#26
0
文件: util.c 项目: CaptainCPS/openisr
void log_tag_mismatch(const void *expected, const void *found, unsigned len)
{
	gchar *fmt_expected;
	gchar *fmt_found;

	fmt_expected=format_tag(expected, len);
	fmt_found=format_tag(found, len);
	pk_log(LOG_WARNING, "Expected %s, found %s", fmt_expected, fmt_found);
	g_free(fmt_expected);
	g_free(fmt_found);
}
示例#27
0
void pklua_socket_server_accepted(lua_State* L, int sockfd, void* void_data) {
  pk_log(PK_LOG_LUA_DEBUG,
         "pklua_socket_server_accepted(%p, %d, %p)", L, sockfd, void_data);
  pklua_socket_server_cb_data* data = (pklua_socket_server_cb_data*) void_data;
  lua_getfield(L, LUA_GLOBALSINDEX, "pklua");
  lua_getfield(L, -1, "_socket_server_accept");
  lua_pushvalue(L, -2);
  pklua_wrap_pk_manager(L, data->pkm);
  lua_pushstring(L, data->name);
  pklua_wrap_sock(L, sockfd);
  lua_pcall(L, 4, 0, 0);
}
示例#28
0
void pklua_socket_server_accepted(pk_lua_t* PL, int sockfd, void* void_data) {
  lua_State* L = pklua_lock_lua(PL)->lua;
  pk_log(PK_LOG_LUA_DEBUG,
         "pklua_socket_server_accepted(%p, %d, %p)", L, sockfd, void_data);
  pklua_socket_server_cb_data* data = (pklua_socket_server_cb_data*) void_data;
  lua_getfield(L, LUA_GLOBALSINDEX, "pklua");
  lua_getfield(L, -1, "_socket_server_accept");
  lua_pushvalue(L, -2);
  lua_pushstring(L, data->name);
  _pklua_wrap_sock(L, sockfd);
  lua_pcall(L, 3, 0, 0);
  pklua_unlock_lua(PL);
}
示例#29
0
文件: util.c 项目: CaptainCPS/openisr
pk_err_t canonicalize_uuid(const char *in, gchar **out)
{
	uuid_t uuid;

	if (uuid_parse(in, uuid)) {
		pk_log(LOG_ERROR, "Invalid UUID");
		return PK_INVALID;
	}
	if (out != NULL) {
		*out=g_malloc(UUID_STR_LEN + 1);
		uuid_unparse_lower(uuid, *out);
	}
	return PK_SUCCESS;
}
示例#30
0
void pkb_update_state(struct pk_manager* pkm, int dns_is_down, int problems)
{
  /* An update has happened, clear this flag. */
  pk_state.force_update = 0;
  if (problems == 0 && pk_state.live_tunnels > 0) {
    PKS_STATE(pkm->status = PK_STATUS_FLYING);
  }
  else if (pkm->status != PK_STATUS_REJECTED) {
    if (dns_is_down)
      pk_log(PK_LOG_MANAGER_INFO, "Network appears to be down.");
    PKS_STATE(pkm->status = (dns_is_down ? PK_STATUS_NO_NETWORK
                                         : PK_STATUS_PROBLEMS));
  }
}