Esempio n. 1
0
static int noit_lua_dns_lookup(lua_State *L) {
  dns_lookup_ctx_t *dlc, **holder;
  const char *c, *query = "", *ctype = "IN", *rtype = "A";
  char *ctype_up, *rtype_up, *d;
  void *vnv_pair;
  noit_lua_resume_info_t *ci;

  ci = noit_lua_get_resume_info(L);
  assert(ci);

  holder = (dns_lookup_ctx_t **)lua_touserdata(L, lua_upvalueindex(1));
  if(holder != lua_touserdata(L,1))
    luaL_error(L, "Must be called as method\n");
  dlc = *holder;

  if(lua_gettop(L) > 1) query = lua_tostring(L, 2);
  if(lua_gettop(L) > 2) rtype = lua_tostring(L, 3);
  if(lua_gettop(L) > 3) ctype = lua_tostring(L, 4);

  ctype_up = alloca(strlen(ctype)+1);
  for(d = ctype_up, c = ctype; *c; d++, c++) *d = toupper(*c);
  *d = '\0';
  rtype_up = alloca(strlen(rtype)+1);
  for(d = rtype_up, c = rtype; *c; d++, c++) *d = toupper(*c);
  *d = '\0';

  if(!noit_hash_retrieve(&dns_ctypes, ctype_up, strlen(ctype_up), &vnv_pair))
    dlc->error = strdup("bad class");
  else
    dlc->query_ctype = ((struct dns_nameval *)vnv_pair)->val;

  if(!noit_hash_retrieve(&dns_rtypes, rtype_up, strlen(rtype_up), &vnv_pair)) 
    dlc->error = strdup("bad rr type");
  else
    dlc->query_rtype = ((struct dns_nameval *)vnv_pair)->val;

  dlc->active = 1;
  noit_atomic_inc32(&dlc->refcnt);
  if(!dlc->error) {
    int abs;
    if(!dns_ptodn(query, strlen(query), dlc->dn, sizeof(dlc->dn), &abs) ||
       !dns_submit_dn(dlc->h->ctx, dlc->dn, dlc->query_ctype, dlc->query_rtype,
                      abs | DNS_NOSRCH, NULL, dns_cb, dlc)) {
      dlc->error = strdup("submission error");
      noit_atomic_dec32(&dlc->refcnt);
    }
    else {
      struct timeval now;
      gettimeofday(&now, NULL);
      dns_timeouts(dlc->h->ctx, -1, now.tv_sec);
    }
  }
  if(dlc->error) {
    dlc->active = 0;
    luaL_error(L, "dns: %s\n", dlc->error);
  }
  return noit_lua_yield(ci, 0);
}
Esempio n. 2
0
int nl_dns_lookup(lua_State *L) {
  dns_lookup_ctx_t *dlc, **holder;
  const char *nameserver = NULL;
  noit_lua_resume_info_t *ci;

  ci = noit_lua_get_resume_info(L);
  assert(ci);
  if(lua_gettop(L) > 0)
    nameserver = lua_tostring(L, 1);
  holder = (dns_lookup_ctx_t **)lua_newuserdata(L, sizeof(*holder));
  dlc = calloc(1, sizeof(*dlc));
  dlc->refcnt = 1;
  dlc->ci = ci;
  dlc->h = dns_ctx_alloc(nameserver);
  *holder = dlc;
  luaL_getmetatable(L, "noit.dns");
  lua_setmetatable(L, -2);
  return 1;
}
Esempio n. 3
0
static int
lua_general_coroutine_spawn(lua_State *Lp) {
  int nargs;
  lua_State *L;
  noit_lua_resume_info_t *ri_parent = NULL, *ri = NULL;

  nargs = lua_gettop(Lp);
  if(nargs < 1 || !lua_isfunction(Lp,1))
    luaL_error(Lp, "noit.coroutine_spawn(func, ...): bad arguments");
  ri_parent = noit_lua_get_resume_info(Lp);
  assert(ri_parent);

  ri = lua_general_new_resume_info(ri_parent->lmc);
  L = ri->coro_state;
  lua_xmove(Lp, L, nargs);
#if !defined(LUA_JITLIBNAME) && LUA_VERSION_NUM < 502
  lua_setlevel(Lp, L);
#endif
  ri->lmc->resume(ri, nargs-1);
  return 0;
}