Exemplo n.º 1
0
static int l_load_music_async_callback(lua_State *L)
{
    load_music_async_t *async = (load_music_async_t*)lua_touserdata(L, 1);

    // Replace light UD with full UD
    lua_pushvalue(L, 1);
    lua_gettable(L, LUA_REGISTRYINDEX);
    lua_insert(L, 1);
    lua_pushnil(L);
    lua_settable(L, LUA_REGISTRYINDEX);

    // Get CB state and function
    lua_pushvalue(L, 1);
    lua_gettable(L, LUA_REGISTRYINDEX);
    lua_rawgeti(L, -1, 1);
    lua_State *cbL = lua_tothread(L, -1);
    // NB: cbL may equal L, or it may not
    lua_pop(L, 1);
    lua_rawgeti(L, -1, 2);
    if(L != cbL)
        lua_xmove(L, cbL, 1);

    // Push CB arg
    int nargs = 1;
    if(async->music == NULL)
    {
        lua_pushnil(cbL);
        if(async->err)
        {
            if(*async->err)
            {
                lua_pushstring(cbL, async->err);
                nargs = 2;
            }
            free(async->err);
        }
    }
    else
    {
        lua_rawgeti(L, 2, 3);
        if(L != cbL)
            lua_xmove(L, cbL, 1);
        music_t* pLMusic = (music_t*)lua_touserdata(cbL, -1);
        pLMusic->pMusic = async->music;
        pLMusic->pRWop = async->rwop;
        async->music = NULL;
        async->rwop = NULL;
    }

    // Finish cleanup
    if(async->rwop)
        SDL_FreeRW(async->rwop);
    lua_pushvalue(L, 1);
    lua_pushnil(L);
    lua_settable(L, LUA_REGISTRYINDEX);

    // Callback
    if(cbL == L)
    {
        lua_call(cbL, nargs, 0);
        return 0;
    }
    if(lua_pcall(cbL, nargs, 0, 0) != 0)
    {
        lua_pushliteral(L, "Error in async music load callback: ");
        lua_xmove(cbL, L, 1);
        lua_tostring(L, -1);
        lua_concat(L, 2);
        lua_error(L);
    }
    return 0;
}
static int
ngx_http_lua_socket_udp_receive(lua_State *L)
{
    ngx_http_request_t                  *r;
    ngx_http_lua_socket_udp_upstream_t  *u;
    ngx_int_t                            rc;
    ngx_http_lua_ctx_t                  *ctx;
    ngx_http_lua_co_ctx_t               *coctx;
    size_t                               size;
    int                                  nargs;
    ngx_http_lua_loc_conf_t             *llcf;

    nargs = lua_gettop(L);
    if (nargs != 1 && nargs != 2) {
        return luaL_error(L, "expecting 1 or 2 arguments "
                          "(including the object), but got %d", nargs);
    }

    r = ngx_http_lua_get_req(L);
    if (r == NULL) {
        return luaL_error(L, "no request found");
    }

    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                   "lua udp socket calling receive() method");

    luaL_checktype(L, 1, LUA_TTABLE);

    lua_rawgeti(L, 1, SOCKET_CTX_INDEX);
    u = lua_touserdata(L, -1);
    lua_pop(L, 1);

    if (u == NULL || u->udp_connection.connection == NULL) {
        llcf = ngx_http_get_module_loc_conf(r, ngx_http_lua_module);

        if (llcf->log_socket_errors) {
            ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
                          "attempt to receive data on a closed socket: u:%p, "
                          "c:%p", u, u ? u->udp_connection.connection : NULL);
        }

        lua_pushnil(L);
        lua_pushliteral(L, "closed");
        return 2;
    }

    if (u->request != r) {
        return luaL_error(L, "bad request");
    }

    if (u->ft_type) {
        u->ft_type = 0;
    }

#if 1
    if (u->waiting) {
        lua_pushnil(L);
        lua_pushliteral(L, "socket busy");
        return 2;
    }
#endif

    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                   "lua udp socket read timeout: %M", u->read_timeout);

    size = (size_t) luaL_optnumber(L, 2, UDP_MAX_DATAGRAM_SIZE);
    size = ngx_min(size, UDP_MAX_DATAGRAM_SIZE);

    u->recv_buf_size = size;

    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                   "lua udp socket receive buffer size: %uz", u->recv_buf_size);

    rc = ngx_http_lua_socket_udp_read(r, u);

    if (rc == NGX_ERROR) {
        dd("read failed: %d", (int) u->ft_type);
        rc = ngx_http_lua_socket_udp_receive_retval_handler(r, u, L);
        dd("udp receive retval returned: %d", (int) rc);
        return rc;
    }

    if (rc == NGX_OK) {

        ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                       "lua udp socket receive done in a single run");

        return ngx_http_lua_socket_udp_receive_retval_handler(r, u, L);
    }

    /* n == NGX_AGAIN */

    u->read_event_handler = ngx_http_lua_socket_udp_read_handler;

    ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
    if (ctx == NULL) {
        return luaL_error(L, "no request ctx found");
    }

    coctx = ctx->cur_co_ctx;

    ngx_http_lua_cleanup_pending_operation(coctx);
    coctx->cleanup = ngx_http_lua_udp_socket_cleanup;
    coctx->data = u;

    if (ctx->entered_content_phase) {
        r->write_event_handler = ngx_http_lua_content_wev_handler;

    } else {
        r->write_event_handler = ngx_http_core_run_phases;
    }

    u->co_ctx = coctx;
    u->waiting = 1;
    u->prepare_retvals = ngx_http_lua_socket_udp_receive_retval_handler;

    return lua_yield(L, 0);
}
Exemplo n.º 3
0
ngx_int_t
ngx_http_lua_wev_handler(ngx_http_request_t *r)
{
    ngx_int_t                    rc;
    ngx_http_lua_ctx_t          *ctx;
    ngx_http_lua_main_conf_t    *lmcf;
    lua_State                   *cc;
    ngx_str_t                   *body_str;
    ngx_http_headers_out_t      *sr_headers;
    ngx_list_part_t             *part;
    ngx_table_elt_t             *header;
    ngx_uint_t                   i, index;

    dd("wev handler %.*s %.*s a:%d, postponed:%p",
       (int) r->uri.len, r->uri.data,
       (int) ngx_cached_err_log_time.len,
       ngx_cached_err_log_time.data,
       r == r->connection->data,
       r->postponed);
#if 0
    ngx_http_lua_dump_postponed(r);
#endif

    ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
    if (ctx == NULL) {
        goto error;
    }

    dd("ctx = %p", ctx);
    dd("request done: %d", (int) r->done);
    dd("cleanup done: %p", ctx->cleanup);

    if (ctx->cleanup == NULL) {
        /* already done */
        dd("cleanup is null: %.*s", (int) r->uri.len, r->uri.data);

        if (ctx->entered_content_phase) {
            ngx_http_finalize_request(r,
                                      ngx_http_lua_flush_postponed_outputs(r));
        }

        return NGX_DONE;
    }

    dd("waiting: %d, done: %d", (int) ctx->waiting,
       ctx->done);

    if (ctx->waiting && ! ctx->done) {
        dd("%.*s waiting and not done", (int) r->uri.len, r->uri.data);

#if 0
        ngx_http_lua_dump_postponed(r);
#endif

        if (r == r->connection->data && r->postponed) {
            if (r->postponed->request) {
                r->connection->data = r->postponed->request;

#if defined(nginx_version) && nginx_version >= 8012
                ngx_http_post_request(r->postponed->request, NULL);
#else
                ngx_http_post_request(r->postponed->request);
#endif

            } else {
                ngx_http_lua_flush_postponed_outputs(r);
            }
        }

        return NGX_DONE;
    }

    ctx->done = 0;

    dd("nsubreqs: %d", (int) ctx->nsubreqs);

    for (index = 0; index < ctx->nsubreqs; index++) {
        dd("summary: reqs %d, subquery %d, waiting %d, req %.*s",
           (int) ctx->nsubreqs,
           (int) index,
           (int) ctx->waiting,
           (int) r->uri.len, r->uri.data);

        cc = ctx->cc;

        /*  {{{ construct ret value */
        lua_newtable(cc);

        /*  copy captured status */
        lua_pushinteger(cc, ctx->sr_statuses[index]);
        lua_setfield(cc, -2, "status");

        /*  copy captured body */

        body_str = &ctx->sr_bodies[index];

        lua_pushlstring(cc, (char *) body_str->data, body_str->len);
        lua_setfield(cc, -2, "body");

        if (body_str->data) {
            dd("free body buffer ASAP");
            ngx_pfree(r->pool, body_str->data);
        }

        /* copy captured headers */

        lua_newtable(cc); /* res.header */

        sr_headers = ctx->sr_headers[index];

        if (sr_headers->content_length == NULL
                && sr_headers->content_length_n >= 0)
        {
            lua_pushliteral(cc, "Content-Length"); /* header key */

            lua_pushnumber(cc, sr_headers->content_length_n);
            /* head key value */

            lua_rawset(cc, -3); /* head */
        }

        if (sr_headers->content_type.len) {
            lua_pushliteral(cc, "Content-Type"); /* header key */
            lua_pushlstring(cc, (char *) sr_headers->content_type.data,
                            sr_headers->content_type.len); /* head key value */
            lua_rawset(cc, -3); /* head */
        }

        dd("saving subrequest response headers");

        part = &sr_headers->headers.part;
        header = part->elts;

        for (i = 0; /* void */; i++) {

            if (i >= part->nelts) {
                if (part->next == NULL) {
                    break;
                }

                part = part->next;
                header = part->elts;
                i = 0;
            }

            dd("checking sr header %.*s", (int) header[i].key.len,
               header[i].key.data);

#if 1
            if (header[i].hash == 0) {
                continue;
            }
#endif

            dd("pushing sr header %.*s", (int) header[i].key.len,
               header[i].key.data);

            lua_pushlstring(cc, (char *) header[i].key.data,
                            header[i].key.len); /* header key */
            lua_pushvalue(cc, -1); /* stack: table key key */

            /* check if header already exists */
            lua_rawget(cc, -3); /* stack: table key value */

            if (lua_isnil(cc, -1)) {
                lua_pop(cc, 1); /* stack: table key */

                lua_pushlstring(cc, (char *) header[i].value.data,
                                header[i].value.len); /* stack: table key value */

                lua_rawset(cc, -3); /* stack: table */

            } else {
                if (! lua_istable(cc, -1)) { /* already inserted one value */
                    lua_createtable(cc, 4, 0);
                    /* stack: table key value table */

                    lua_insert(cc, -2); /* stack: table key table value */
                    lua_rawseti(cc, -2, 1); /* stack: table key table */

                    lua_pushlstring(cc, (char *) header[i].value.data,
                                    header[i].value.len);
                    /* stack: table key table value */

                    lua_rawseti(cc, -2, lua_objlen(cc, -2) + 1);
                    /* stack: table key table */

                    lua_rawset(cc, -3); /* stack: table */

                } else {
                    lua_pushlstring(cc, (char *) header[i].value.data,
                                    header[i].value.len);
                    /* stack: table key table value */

                    lua_rawseti(cc, -2, lua_objlen(cc, -2) + 1);
                    /* stack: table key table */

                    lua_pop(cc, 2); /* stack: table */
                }
            }
        }

        lua_setfield(cc, -2, "header");

        /*  }}} */
    }

    dd("free sr_statues/headers/bodies memory ASAP");

#if 1
    ngx_pfree(r->pool, ctx->sr_statuses);

    ctx->sr_statuses = NULL;
    ctx->sr_headers = NULL;
    ctx->sr_bodies = NULL;
#endif

    lmcf = ngx_http_get_module_main_conf(r, ngx_http_lua_module);

    dd("about to run thread for %.*s...", (int) r->uri.len, r->uri.data);

    rc = ngx_http_lua_run_thread(lmcf->lua, r, ctx, ctx->nsubreqs);

    dd("already run thread for %.*s...", (int) r->uri.len, r->uri.data);

    if (rc == NGX_AGAIN || rc == NGX_DONE) {
        return NGX_DONE;
    }

    dd("entered content phase: %d", (int) ctx->entered_content_phase);

    if (ctx->entered_content_phase) {
        ngx_http_finalize_request(r, rc);
    }

    if (rc == NGX_OK) {
        return NGX_DECLINED;
    }

    return rc;

error:
    if (ctx->entered_content_phase) {
        ngx_http_finalize_request(r,
                                  ctx->headers_sent ? NGX_ERROR: NGX_HTTP_INTERNAL_SERVER_ERROR);
    }

    return NGX_ERROR;
}
static int
ngx_http_lua_socket_udp_setpeername(lua_State *L)
{
    ngx_http_request_t          *r;
    ngx_http_lua_ctx_t          *ctx;
    ngx_str_t                    host;
    int                          port;
    ngx_resolver_ctx_t          *rctx, temp;
    ngx_http_core_loc_conf_t    *clcf;
    int                          saved_top;
    int                          n;
    u_char                      *p;
    size_t                       len;
    ngx_url_t                    url;
    ngx_int_t                    rc;
    ngx_http_lua_loc_conf_t     *llcf;
    int                          timeout;
    ngx_http_lua_co_ctx_t       *coctx;

    ngx_http_lua_udp_connection_t           *uc;
    ngx_http_lua_socket_udp_upstream_t      *u;

    /*
     * TODO: we should probably accept an extra argument to setpeername()
     * to allow the user bind the datagram unix domain socket himself,
     * which is necessary for systems without autobind support.
     */

    n = lua_gettop(L);
    if (n != 2 && n != 3) {
        return luaL_error(L, "ngx.socket.udp setpeername: expecting 2 or 3 "
                          "arguments (including the object), but seen %d", n);
    }

    r = ngx_http_lua_get_req(L);
    if (r == NULL) {
        return luaL_error(L, "no request found");
    }

    ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
    if (ctx == NULL) {
        return luaL_error(L, "no ctx found");
    }

    ngx_http_lua_check_context(L, ctx, NGX_HTTP_LUA_CONTEXT_REWRITE
                               | NGX_HTTP_LUA_CONTEXT_ACCESS
                               | NGX_HTTP_LUA_CONTEXT_CONTENT
                               | NGX_HTTP_LUA_CONTEXT_TIMER
                               | NGX_HTTP_LUA_CONTEXT_SSL_CERT);

    luaL_checktype(L, 1, LUA_TTABLE);

    p = (u_char *) luaL_checklstring(L, 2, &len);

    host.data = ngx_palloc(r->pool, len + 1);
    if (host.data == NULL) {
        return luaL_error(L, "no memory");
    }

    host.len = len;

    ngx_memcpy(host.data, p, len);
    host.data[len] = '\0';

    if (n == 3) {
        port = luaL_checkinteger(L, 3);

        if (port < 0 || port > 65536) {
            lua_pushnil(L);
            lua_pushfstring(L, "bad port number: %d", port);
            return 2;
        }

    } else { /* n == 2 */
        port = 0;
    }

    lua_rawgeti(L, 1, SOCKET_CTX_INDEX);
    u = lua_touserdata(L, -1);
    lua_pop(L, 1);

    if (u) {
        if (u->request && u->request != r) {
            return luaL_error(L, "bad request");
        }

        if (u->waiting) {
            lua_pushnil(L);
            lua_pushliteral(L, "socket busy");
            return 2;
        }

        if (u->udp_connection.connection) {
            ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                           "lua udp socket reconnect without shutting down");

            ngx_http_lua_socket_udp_finalize(r, u);
        }

        ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                       "lua reuse socket upstream ctx");

    } else {
        u = lua_newuserdata(L, sizeof(ngx_http_lua_socket_udp_upstream_t));
        if (u == NULL) {
            return luaL_error(L, "no memory");
        }

#if 1
        lua_pushlightuserdata(L, &ngx_http_lua_udp_udata_metatable_key);
        lua_rawget(L, LUA_REGISTRYINDEX);
        lua_setmetatable(L, -2);
#endif

        lua_rawseti(L, 1, SOCKET_CTX_INDEX);
    }

    ngx_memzero(u, sizeof(ngx_http_lua_socket_udp_upstream_t));

    u->request = r; /* set the controlling request */
    llcf = ngx_http_get_module_loc_conf(r, ngx_http_lua_module);

    u->conf = llcf;

    uc = &u->udp_connection;

    uc->log = *r->connection->log;

    dd("lua peer connection log: %p", &uc->log);

    lua_rawgeti(L, 1, SOCKET_TIMEOUT_INDEX);
    timeout = (ngx_int_t) lua_tointeger(L, -1);
    lua_pop(L, 1);

    if (timeout > 0) {
        u->read_timeout = (ngx_msec_t) timeout;

    } else {
        u->read_timeout = u->conf->read_timeout;
    }

    ngx_memzero(&url, sizeof(ngx_url_t));

    url.url.len = host.len;
    url.url.data = host.data;
    url.default_port = (in_port_t) port;
    url.no_resolve = 1;

    if (ngx_parse_url(r->pool, &url) != NGX_OK) {
        lua_pushnil(L);

        if (url.err) {
            lua_pushfstring(L, "failed to parse host name \"%s\": %s",
                            host.data, url.err);

        } else {
            lua_pushfstring(L, "failed to parse host name \"%s\"", host.data);
        }

        return 2;
    }

    u->resolved = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_resolved_t));
    if (u->resolved == NULL) {
        return luaL_error(L, "no memory");
    }

    if (url.addrs && url.addrs[0].sockaddr) {
        ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                       "lua udp socket network address given directly");

        u->resolved->sockaddr = url.addrs[0].sockaddr;
        u->resolved->socklen = url.addrs[0].socklen;
        u->resolved->naddrs = 1;
        u->resolved->host = url.addrs[0].name;

    } else {
        u->resolved->host = host;
        u->resolved->port = (in_port_t) port;
    }

    if (u->resolved->sockaddr) {
        rc = ngx_http_lua_socket_resolve_retval_handler(r, u, L);
        if (rc == NGX_AGAIN) {
            return lua_yield(L, 0);
        }

        return rc;
    }

    clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);

    temp.name = host;
    rctx = ngx_resolve_start(clcf->resolver, &temp);
    if (rctx == NULL) {
        u->ft_type |= NGX_HTTP_LUA_SOCKET_FT_RESOLVER;
        lua_pushnil(L);
        lua_pushliteral(L, "failed to start the resolver");
        return 2;
    }

    if (rctx == NGX_NO_RESOLVER) {
        u->ft_type |= NGX_HTTP_LUA_SOCKET_FT_RESOLVER;
        lua_pushnil(L);
        lua_pushfstring(L, "no resolver defined to resolve \"%s\"", host.data);
        return 2;
    }

    rctx->name = host;
#if !defined(nginx_version) || nginx_version < 1005008
    rctx->type = NGX_RESOLVE_A;
#endif
    rctx->handler = ngx_http_lua_socket_resolve_handler;
    rctx->data = u;
    rctx->timeout = clcf->resolver_timeout;

    u->co_ctx = ctx->cur_co_ctx;
    u->resolved->ctx = rctx;

    saved_top = lua_gettop(L);

    coctx = ctx->cur_co_ctx;
    ngx_http_lua_cleanup_pending_operation(coctx);
    coctx->cleanup = ngx_http_lua_udp_resolve_cleanup;

    if (ngx_resolve_name(rctx) != NGX_OK) {
        ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                       "lua udp socket fail to run resolver immediately");

        u->ft_type |= NGX_HTTP_LUA_SOCKET_FT_RESOLVER;

        u->resolved->ctx = NULL;
        lua_pushnil(L);
        lua_pushfstring(L, "%s could not be resolved", host.data);

        return 2;
    }

    if (u->waiting == 1) {
        /* resolved and already connecting */
        return lua_yield(L, 0);
    }

    n = lua_gettop(L) - saved_top;
    if (n) {
        /* errors occurred during resolving or connecting
         * or already connected */
        return n;
    }

    /* still resolving */

    u->waiting = 1;
    u->prepare_retvals = ngx_http_lua_socket_resolve_retval_handler;

    coctx->data = u;

    if (ctx->entered_content_phase) {
        r->write_event_handler = ngx_http_lua_content_wev_handler;

    } else {
        r->write_event_handler = ngx_http_core_run_phases;
    }

    return lua_yield(L, 0);
}
static int
ngx_http_lua_socket_resolve_retval_handler(ngx_http_request_t *r,
    ngx_http_lua_socket_udp_upstream_t *u, lua_State *L)
{
    ngx_http_lua_ctx_t              *ctx;
    ngx_http_lua_co_ctx_t           *coctx;
    ngx_connection_t                *c;
    ngx_http_cleanup_t              *cln;
    ngx_http_upstream_resolved_t    *ur;
    ngx_int_t                        rc;
    ngx_http_lua_udp_connection_t   *uc;

    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                   "lua udp socket resolve retval handler");

    if (u->ft_type & NGX_HTTP_LUA_SOCKET_FT_RESOLVER) {
        return 2;
    }

    uc = &u->udp_connection;

    ur = u->resolved;

    if (ur->sockaddr) {
        uc->sockaddr = ur->sockaddr;
        uc->socklen = ur->socklen;
        uc->server = ur->host;

    } else {
        lua_pushnil(L);
        lua_pushliteral(L, "resolver not working");
        return 2;
    }

    rc = ngx_http_lua_udp_connect(L, uc);

    if (rc != NGX_OK) {
        u->socket_errno = ngx_socket_errno;
    }

    if (u->cleanup == NULL) {
        cln = ngx_http_cleanup_add(r, 0);
        if (cln == NULL) {
            u->ft_type |= NGX_HTTP_LUA_SOCKET_FT_ERROR;
            lua_pushnil(L);
            lua_pushliteral(L, "no memory");
            return 2;
        }

        cln->handler = ngx_http_lua_socket_udp_cleanup;
        cln->data = u;
        u->cleanup = &cln->handler;
    }

    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                   "lua udp socket connect: %i", rc);

    if (rc != NGX_OK) {
        return ngx_http_lua_socket_error_retval_handler(r, u, L);
    }

    /* rc == NGX_OK */

    c = uc->connection;

    c->data = u;

    c->write->handler = NULL;
    c->read->handler = ngx_http_lua_socket_udp_handler;
    c->read->resolver = 0;

    c->pool = r->pool;
    c->log = r->connection->log;
    c->read->log = c->log;
    c->write->log = c->log;

    ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);

    coctx = ctx->cur_co_ctx;

    coctx->data = u;

    u->read_event_handler = ngx_http_lua_socket_dummy_handler;

    lua_pushinteger(L, 1);
    return 1;
}
Exemplo n.º 6
0
static void *alien_loadfunc (lua_State *L, void *lib, const char *sym) {
  (void)lib; (void)sym;  /* to avoid warnings */
  lua_pushliteral(L, DLMSG);
  return NULL;
}
Exemplo n.º 7
0
static void *lsys_load (lua_State *L, const char *path, int seeglb) {
  (void)(path); (void)(seeglb);  /* not used */
  lua_pushliteral(L, DLMSG);
  return NULL;
}
Exemplo n.º 8
0
/**
 * @brief SetBindings loads standard lua libraries and binds the functions and data defined above
 * @param L the lua state to which to bind
 */
void SetBindings(lua_State *L)
{
    luaL_openlibs(L);

    lua_pushcfunction(L, Exit);
    lua_setglobal(L, "exit");

    lua_pushcfunction(L, CreateEntity);
    lua_setglobal(L, "create_entity");

    lua_pushcfunction(L, DestroyEntity);
    lua_setglobal(L, "destroy_entity");

    lua_pushcfunction(L, Wait);
    lua_setglobal(L, "wait");

    lua_pushcfunction(L, GetWindowSize);
    lua_setglobal(L, "get_window_size");

    lua_pushcfunction(L, GetCursorPosition);
    lua_setglobal(L, "get_cursor_position");

    lua_pushcfunction(L, isKeyPressed);
    lua_setglobal(L, "is_key_pressed");

    lua_pushcfunction(L, GetEntityTag);
    lua_setglobal(L, "get_entity_tag");

    lua_pushcfunction(L, GetEntityPosition);
    lua_setglobal(L, "get_entity_position");

    lua_pushcfunction(L, GetEntityVelocity);
    lua_setglobal(L, "get_entity_velocity");

    lua_pushcfunction(L, MoveEntity);
    lua_setglobal(L, "move_entity");

    lua_pushcfunction(L, PushEntity);
    lua_setglobal(L, "push_entity");

    lua_newtable(L);
    lua_pushliteral(L, "W");
    lua_pushnumber(L, sf::Keyboard::W);
    lua_settable(L, -3);

    lua_pushliteral(L, "S");
    lua_pushnumber(L, sf::Keyboard::S);
    lua_settable(L, -3);

    lua_pushliteral(L, "A");
    lua_pushnumber(L, sf::Keyboard::A);
    lua_settable(L, -3);

    lua_pushliteral(L, "D");
    lua_pushnumber(L, sf::Keyboard::D);
    lua_settable(L, -3);

    lua_pushliteral(L, "Up");
    lua_pushnumber(L, sf::Keyboard::Up);
    lua_settable(L, -3);

    lua_pushliteral(L, "Down");
    lua_pushnumber(L, sf::Keyboard::Down);
    lua_settable(L, -3);

    lua_pushliteral(L, "Left");
    lua_pushnumber(L, sf::Keyboard::Left);
    lua_settable(L, -3);

    lua_pushliteral(L, "Right");
    lua_pushnumber(L, sf::Keyboard::Right);
    lua_settable(L, -3);
    lua_setglobal(L, "Keys");
}
Exemplo n.º 9
0
LUALIB_API int
luaopen_posix_signal(lua_State *L)
{
    luaL_register(L, "posix.signal", posix_signal_fns);
    lua_pushliteral(L, "posix.signal for " LUA_VERSION " / " PACKAGE_STRING);
    lua_setfield(L, -2, "version");

    /* Signals table stored in registry for Psignal and sig_handle */
    lua_pushlightuserdata(L, &signalL);
    lua_newtable(L);
    lua_rawset(L, LUA_REGISTRYINDEX);

    signalL = L; /* For sig_postpone */

    /* Signals */
#ifdef SIGABRT
    LPOSIX_CONST( SIGABRT		);
#endif
#ifdef SIGALRM
    LPOSIX_CONST( SIGALRM		);
#endif
#ifdef SIGBUS
    LPOSIX_CONST( SIGBUS		);
#endif
#ifdef SIGCHLD
    LPOSIX_CONST( SIGCHLD		);
#endif
#ifdef SIGCONT
    LPOSIX_CONST( SIGCONT		);
#endif
#ifdef SIGFPE
    LPOSIX_CONST( SIGFPE		);
#endif
#ifdef SIGHUP
    LPOSIX_CONST( SIGHUP		);
#endif
#ifdef SIGILL
    LPOSIX_CONST( SIGILL		);
#endif
#ifdef SIGINT
    LPOSIX_CONST( SIGINT		);
#endif
#ifdef SIGKILL
    LPOSIX_CONST( SIGKILL		);
#endif
#ifdef SIGPIPE
    LPOSIX_CONST( SIGPIPE		);
#endif
#ifdef SIGQUIT
    LPOSIX_CONST( SIGQUIT		);
#endif
#ifdef SIGSEGV
    LPOSIX_CONST( SIGSEGV		);
#endif
#ifdef SIGSTOP
    LPOSIX_CONST( SIGSTOP		);
#endif
#ifdef SIGTERM
    LPOSIX_CONST( SIGTERM		);
#endif
#ifdef SIGTSTP
    LPOSIX_CONST( SIGTSTP		);
#endif
#ifdef SIGTTIN
    LPOSIX_CONST( SIGTTIN		);
#endif
#ifdef SIGTTOU
    LPOSIX_CONST( SIGTTOU		);
#endif
#ifdef SIGUSR1
    LPOSIX_CONST( SIGUSR1		);
#endif
#ifdef SIGUSR2
    LPOSIX_CONST( SIGUSR2		);
#endif
#ifdef SIGSYS
    LPOSIX_CONST( SIGSYS		);
#endif
#ifdef SIGTRAP
    LPOSIX_CONST( SIGTRAP		);
#endif
#ifdef SIGURG
    LPOSIX_CONST( SIGURG		);
#endif
#ifdef SIGVTALRM
    LPOSIX_CONST( SIGVTALRM	);
#endif
#ifdef SIGXCPU
    LPOSIX_CONST( SIGXCPU		);
#endif
#ifdef SIGXFSZ
    LPOSIX_CONST( SIGXFSZ		);
#endif

    /* String constants */
    lua_pushliteral(L, "SIG_DFL");
    lua_setfield(L, -2, "SIG_DFL");

    lua_pushliteral(L, "SIG_IGN");
    lua_setfield(L, -2, "SIG_IGN");


    /* Signal flags */
#ifdef SA_NOCLDSTOP
    LPOSIX_CONST( SA_NOCLDSTOP	);
#endif
#ifdef SA_NOCLDWAIT
    LPOSIX_CONST( SA_NOCLDWAIT	);
#endif
#ifdef SA_RESETHAND
    LPOSIX_CONST( SA_RESETHAND	);
#endif
#ifdef SA_NODEFER
    LPOSIX_CONST( SA_NODEFER	);
#endif

    return 1;
}
Exemplo n.º 10
0
/*
** function to (not) close the standard files stdin, stdout, and stderr
*/
static int io_noclose(lua_State* L)
{
	lua_pushnil(L);
	lua_pushliteral(L, "cannot close standard file");
	return 2;
}
static int
ngx_http_lua_ngx_timer_at(lua_State *L)
{
    int                      nargs, co_ref;
    u_char                  *p;
    lua_State               *vm;  /* the main thread */
    lua_State               *co;
    ngx_msec_t               delay;
    ngx_event_t             *ev;
    ngx_http_request_t      *r;
    ngx_connection_t        *saved_c = NULL;
    ngx_http_lua_ctx_t      *ctx;
#if 0
    ngx_http_connection_t   *hc;
#endif

    ngx_http_lua_timer_ctx_t      *tctx = NULL;
    ngx_http_lua_main_conf_t      *lmcf;
#if 0
    ngx_http_core_main_conf_t     *cmcf;
#endif

    nargs = lua_gettop(L);
    if (nargs < 2) {
        return luaL_error(L, "expecting at least 2 arguments but got %d",
                          nargs);
    }

    delay = (ngx_msec_t) (luaL_checknumber(L, 1) * 1000);

    luaL_argcheck(L, lua_isfunction(L, 2) && !lua_iscfunction(L, 2), 2,
                 "Lua function expected");

    r = ngx_http_lua_get_req(L);
    if (r == NULL) {
        return luaL_error(L, "no request");
    }

    ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);

    if (ngx_exiting && delay > 0) {
        lua_pushnil(L);
        lua_pushliteral(L, "process exiting");
        return 2;
    }

    lmcf = ngx_http_get_module_main_conf(r, ngx_http_lua_module);

    if (lmcf->pending_timers >= lmcf->max_pending_timers) {
        lua_pushnil(L);
        lua_pushliteral(L, "too many pending timers");
        return 2;
    }

    if (lmcf->watcher == NULL) {
        /* create the watcher fake connection */

        ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
                       "lua creating fake watcher connection");

        if (ngx_cycle->files) {
            saved_c = ngx_cycle->files[0];
        }

        lmcf->watcher = ngx_get_connection(0, ngx_cycle->log);

        if (ngx_cycle->files) {
            ngx_cycle->files[0] = saved_c;
        }

        if (lmcf->watcher == NULL) {
            return luaL_error(L, "no memory");
        }

        /* to work around the -1 check in ngx_worker_process_cycle: */
        lmcf->watcher->fd = (ngx_socket_t) -2;

        lmcf->watcher->idle = 1;
        lmcf->watcher->read->handler = ngx_http_lua_abort_pending_timers;
        lmcf->watcher->data = lmcf;
    }

    vm = ngx_http_lua_get_lua_vm(r, ctx);

    co = lua_newthread(vm);

    /* L stack: time func [args] thread */

    ngx_http_lua_probe_user_coroutine_create(r, L, co);

    lua_createtable(co, 0, 0);  /* the new globals table */

    /* co stack: global_tb */

    lua_createtable(co, 0, 1);  /* the metatable */
    ngx_http_lua_get_globals_table(co);
    lua_setfield(co, -2, "__index");
    lua_setmetatable(co, -2);

    /* co stack: global_tb */

    ngx_http_lua_set_globals_table(co);

    /* co stack: <empty> */

    dd("stack top: %d", lua_gettop(L));

    lua_xmove(vm, L, 1);    /* move coroutine from main thread to L */

    /* L stack: time func [args] thread */
    /* vm stack: empty */

    lua_pushvalue(L, 2);    /* copy entry function to top of L*/

    /* L stack: time func [args] thread func */

    lua_xmove(L, co, 1);    /* move entry function from L to co */

    /* L stack: time func [args] thread */
    /* co stack: func */

    ngx_http_lua_get_globals_table(co);
    lua_setfenv(co, -2);

    /* co stack: func */

    lua_pushlightuserdata(L, &ngx_http_lua_coroutines_key);
    lua_rawget(L, LUA_REGISTRYINDEX);

    /* L stack: time func [args] thread corountines */

    lua_pushvalue(L, -2);

    /* L stack: time func [args] thread coroutines thread */

    co_ref = luaL_ref(L, -2);
    lua_pop(L, 1);

    /* L stack: time func [args] thread */

    if (nargs > 2) {
        lua_pop(L, 1);  /* L stack: time func [args] */
        lua_xmove(L, co, nargs - 2);  /* L stack: time func */

        /* co stack: func [args] */
    }

    p = ngx_alloc(sizeof(ngx_event_t) + sizeof(ngx_http_lua_timer_ctx_t),
                  r->connection->log);
    if (p == NULL) {
        goto nomem;
    }

    ev = (ngx_event_t *) p;

    ngx_memzero(ev, sizeof(ngx_event_t));

    p += sizeof(ngx_event_t);

    tctx = (ngx_http_lua_timer_ctx_t *) p;

    tctx->premature = 0;
    tctx->co_ref = co_ref;
    tctx->co = co;
    tctx->main_conf = r->main_conf;
    tctx->srv_conf = r->srv_conf;
    tctx->loc_conf = r->loc_conf;
    tctx->lmcf = lmcf;

    tctx->pool = ngx_create_pool(128, ngx_cycle->log);
    if (tctx->pool == NULL) {
        goto nomem;
    }

    if (r->connection) {
        tctx->listening = r->connection->listening;

    } else {
        tctx->listening = NULL;
    }

    if (r->connection->addr_text.len) {
        tctx->client_addr_text.data = ngx_palloc(tctx->pool,
                                                 r->connection->addr_text.len);
        if (tctx->client_addr_text.data == NULL) {
            goto nomem;
        }

        ngx_memcpy(tctx->client_addr_text.data, r->connection->addr_text.data,
                   r->connection->addr_text.len);
        tctx->client_addr_text.len = r->connection->addr_text.len;

    } else {
        tctx->client_addr_text.len = 0;
        tctx->client_addr_text.data = NULL;
    }

    if (ctx && ctx->vm_state) {
        tctx->vm_state = ctx->vm_state;
        tctx->vm_state->count++;

    } else {
        tctx->vm_state = NULL;
    }

    ev->handler = ngx_http_lua_timer_handler;
    ev->data = tctx;
    ev->log = ngx_cycle->log;

    lmcf->pending_timers++;

    ngx_add_timer(ev, delay);

    lua_pushinteger(L, 1);
    return 1;

nomem:

    if (tctx && tctx->pool) {
        ngx_destroy_pool(tctx->pool);
    }

    lua_pushlightuserdata(L, &ngx_http_lua_coroutines_key);
    lua_rawget(L, LUA_REGISTRYINDEX);
    luaL_unref(L, -1, co_ref);

    return luaL_error(L, "no memory");
}
Exemplo n.º 12
0
Arquivo: core.c Projeto: esmil/lem
static int
parse_http_process(lua_State *T, struct lem_inputbuf *b)
{
	struct parse_http_state *s = (struct parse_http_state *)&b->pstate;
	unsigned int w = s->w;
	unsigned int r = b->start;
	unsigned char state = s->state;

	while (r < b->end) {
		unsigned char ch = b->buf[r++];

		state = state_table[state][ch > 127 ? C_ETC : ascii_class[ch]];
		/*lem_debug("char = %c (%hhu), state = %hhu", ch, ch, state);*/
		switch (state) {
		case SMTD:
		case SURI:
		case SMAV:
		case SDOT:
		case SMIV:
		case CMAV:
		case CDOT:
		case CMIV:
		case CNUM:
		case CTXT:
		case SVAL:
			b->buf[w++] = ch;
			break;

		case XKEY:
			state = SKEY;
			lua_pushlstring(T, b->buf, w);
			lua_rawset(T, -3);
			w = 0;
			/* fallthrough */

		case SKEY:
			if (ch >= 'A' && ch <= 'Z')
				ch += ('a' - 'A');
			b->buf[w++] = ch;
			break;

		case SRE1:
			lua_pushlstring(T, b->buf, w);
			lua_setfield(T, -2, "version");
			w = 0;
			lua_newtable(T);
			break;

		case X___:
			lem_debug("HTTP parse error");
			lua_settop(T, 0);
			lua_pushnil(T);
			lua_pushliteral(T, "parse error");
			return 2;

		case XMUS:
			state = SMUS;
			lua_pushlstring(T, b->buf, w);
			lua_setfield(T, -2, "method");
			w = 0;
			break;

		case XUHS:
			state = SUHS;
			lua_pushlstring(T, b->buf, w);
			lua_setfield(T, -2, "uri");
			w = 0;
			break;

		case XVNS:
			state = CVNS;
			lua_pushlstring(T, b->buf, w);
			lua_setfield(T, -2, "version");
			w = 0;
			break;

		case XNTS:
			state = CNTS;
			{
				unsigned int n = 0;
				unsigned int k;

				for (k = 0; k < w; k++) {
					n *= 10;
					n += b->buf[k] - '0';
				}

				lua_pushinteger(T, n);
			}
			lua_setfield(T, -2, "status");
			w = 0;
			break;

		case XRE1:
			state = SRE1;
			lua_pushlstring(T, b->buf, w);
			lua_setfield(T, -2, "text");
			w = 0;
			lua_newtable(T);
			break;

		case XCOL:
			state = SCOL;
			lua_pushlstring(T, b->buf, w);
			w = 0;
			break;

		case XVAL:
			state = SVAL;
			b->buf[w++] = ' ';
			b->buf[w++] = ch;
			break;

		case XEND:
			/* in case there are no headers this is false */
			if (lua_type(T, -1) == LUA_TSTRING) {
				lua_pushlstring(T, b->buf, w);
				lua_rawset(T, -3);
			}
			lua_setfield(T, -2, "headers");

			if (r == b->end)
				b->start = b->end = 0;
			else
				b->start = r;
			return 1;
		}
	}

	if (w == LEM_INPUTBUF_SIZE - 1) {
		b->start = b->end = 0;
		lua_settop(T, 0);
		lua_pushnil(T);
		lua_pushliteral(T, "out of buffer space");
		return 2;
	}

	b->start = b->end = w + 1;
	s->w = w;
	s->state = state;
	return 0;
}
Exemplo n.º 13
0
static int test_eof (lua_State *L, FILE *f) {
  int c = getc(f);
  ungetc(c, f);  /* no-op when c == EOF */
  lua_pushliteral(L, "");
  return (c != EOF);
}
Exemplo n.º 14
0
static int
config_env_os_execute(lua_State *L)
{
    if(lua_type(L, 1) == LUA_TTABLE) {
        struct execute_options options;

        memset(&options, 0, sizeof(struct execute_options));

        process_options(L, &options);

        pid_t pid;

        pid = fork();

        if(pid == -1) {
            lua_pushnil(L);
            lua_pushstring(L, strerror(errno));
            lua_pushinteger(L, errno);

            return 3;
        } else {
            if(pid) {
                int status;

                if(options.stdout_handle) {
                    fclose(options.stdout_handle);
                }

                if(options.stderr_handle) {
                    fclose(options.stderr_handle);
                }

                waitpid(pid, &status, 0);

                if(status) {
                    lua_pushnil(L);
                    lua_pushliteral(L, "execution failed");
                    lua_pushinteger(L, status);

                    return 3;
                } else {
                    lua_pushboolean(L, 1);
                    return 1;
                }
            } else {
                char **argv;
                int nargs;
                int i;

                if(options.stdout_handle) {
                    dup2(fileno(options.stdout_handle), fileno(stdout));
                }

                if(options.stderr_handle) {
                    dup2(fileno(options.stderr_handle), fileno(stderr));
                }

                nargs = lua_objlen(L, 1);
                argv  = lua_newuserdata(L, sizeof(char *) * (nargs + 1));

                for(i = 1; i <= nargs; i++) {
                    const char *arg;

                    lua_rawgeti(L, 1, i);
                    arg         = luaL_checkstring(L, -1);
                    argv[i - 1] = arg; /* we don't need to make a copy, since
                                        * we're keeping the table on the stack
                                        */
                    lua_pop(L, 1);
                }
                argv[nargs] = NULL;

                execvp(argv[0], argv);
                exit(255); /* this should never be reached */
            }
        }
    } else {
        lua_pushvalue(L, lua_upvalueindex(1));
        lua_insert(L, 1);
        lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);

        return lua_gettop(L);
    }
}
Exemplo n.º 15
0
/*
* Push a proxy userdata on the stack.
*
* Initializes necessary structures if it's the first time 'idfunc' is being
* used in this Lua state (metatable, registring it). Otherwise, increments the
* reference count.
*/
void luaG_push_proxy( lua_State *L, lua_CFunction idfunc, DEEP_PRELUDE *prelude ) {
    DEEP_PRELUDE **proxy;

    MUTEX_LOCK( &deep_lock );
      ++(prelude->refcount);  // one more proxy pointing to this deep data
    MUTEX_UNLOCK( &deep_lock );

    STACK_GROW(L,4);

  STACK_CHECK(L)

    proxy= lua_newuserdata( L, sizeof( DEEP_PRELUDE* ) );
    ASSERT_L(proxy);
    *proxy= prelude;

    // Get/create metatable for 'idfunc' (in this state)
    //
    lua_pushcfunction( L, idfunc );    // key
    get_deep_lookup(L);
        //
        // [-2]: proxy
        // [-1]: metatable / nil
    
    if (lua_isnil(L,-1)) {
        // No metatable yet; make one and register it
        //
        lua_pop(L,1);

        // tbl= idfunc( "metatable" )
        //
        lua_pushcfunction( L, idfunc );
        lua_pushliteral( L, "metatable" );
        lua_call( L, 1 /*args*/, 1 /*results*/ );
            //
            // [-2]: proxy
            // [-1]: metatable (returned by 'idfunc')

        if (!lua_istable(L,-1))
            luaL_error( L, "Bad idfunc on \"metatable\": did not return one" );

        // Add '__gc' method
        //
        lua_pushcfunction( L, deep_userdata_gc );
        lua_setfield( L, -2, "__gc" );

        // Memorize for later rounds
        //
        lua_pushvalue( L,-1 );
        lua_pushcfunction( L, idfunc );
            //
            // [-4]: proxy
            // [-3]: metatable (2nd ref)
            // [-2]: metatable
            // [-1]: idfunc

        set_deep_lookup(L);
    } 
  STACK_MID(L,2)
    ASSERT_L( lua_isuserdata(L,-2) );
    ASSERT_L( lua_istable(L,-1) );

    // [-2]: proxy userdata
    // [-1]: metatable to use

    lua_setmetatable( L, -2 );
    
  STACK_END(L,1)
    // [-1]: proxy userdata
}
static int
ngx_stream_lua_uthread_wait(lua_State *L)
{
    int                           i, nargs, nrets;
    lua_State                    *sub_co;
    ngx_stream_session_t          *s;
    ngx_stream_lua_ctx_t          *ctx;
    ngx_stream_lua_co_ctx_t       *coctx, *sub_coctx;

    s = ngx_stream_lua_get_session(L);
    if (s == NULL) {
        return luaL_error(L, "no session found");
    }

    ctx = ngx_stream_get_module_ctx(s, ngx_stream_lua_module);
    if (ctx == NULL) {
        return luaL_error(L, "no session ctx found");
    }

    ngx_stream_lua_check_context(L, ctx, NGX_STREAM_LUA_CONTEXT_CONTENT
                                 | NGX_STREAM_LUA_CONTEXT_TIMER);

    coctx = ctx->cur_co_ctx;

    nargs = lua_gettop(L);

    for (i = 1; i <= nargs; i++) {
        sub_co = lua_tothread(L, i);

        luaL_argcheck(L, sub_co, i, "stream lua thread expected");

        sub_coctx = ngx_stream_lua_get_co_ctx(sub_co, ctx);
        if (sub_coctx == NULL) {
            return luaL_error(L, "no co ctx found");
        }

        if (!sub_coctx->is_uthread) {
            return luaL_error(L, "attempt to wait on a coroutine that is "
                              "not a user thread");
        }

        if (sub_coctx->parent_co_ctx != coctx) {
            return luaL_error(L, "only the parent coroutine can wait on the "
                              "thread");
        }

        switch (sub_coctx->co_status) {
        case NGX_STREAM_LUA_CO_ZOMBIE:

            ngx_stream_lua_probe_info("found zombie child");

            nrets = lua_gettop(sub_coctx->co);

            dd("child retval count: %d, %s: %s", (int) nrets,
               luaL_typename(sub_coctx->co, -1),
               lua_tostring(sub_coctx->co, -1));

            if (nrets) {
                lua_xmove(sub_coctx->co, L, nrets);
            }

#if 1
            ngx_stream_lua_del_thread(s, L, ctx, sub_coctx);
            ctx->uthreads--;
#endif

            return nrets;

        case NGX_STREAM_LUA_CO_DEAD:
            dd("uthread already waited: %p (parent %p)", sub_coctx,
               coctx);

            if (i < nargs) {
                /* just ignore it if it is not the last one */
                continue;
            }

            /* being the last one */
            lua_pushnil(L);
            lua_pushliteral(L, "already waited or killed");
            return 2;

        default:
            dd("uthread %p still alive, status: %d, parent %p", sub_coctx,
               sub_coctx->co_status, coctx);
            break;
        }

#if 0
        /* TODO */
        ngx_stream_lua_probe_user_thread_wait(L, sub_coctx->co);
#endif
        sub_coctx->waited_by_parent = 1;
    }

    return lua_yield(L, 0);
}
Exemplo n.º 17
0
static void *alien_load (lua_State *L, const char *path) {
  (void)path;  /* to avoid warnings */
  lua_pushliteral(L, DLMSG);
  return NULL;
}
static int
ngx_stream_lua_uthread_kill(lua_State *L)
{
    lua_State                     *sub_co;
    ngx_stream_session_t          *s;
    ngx_stream_lua_ctx_t          *ctx;
    ngx_stream_lua_co_ctx_t       *coctx, *sub_coctx;

    s = ngx_stream_lua_get_session(L);
    if (s == NULL) {
        return luaL_error(L, "no session found");
    }

    ctx = ngx_stream_get_module_ctx(s, ngx_stream_lua_module);
    if (ctx == NULL) {
        return luaL_error(L, "no session ctx found");
    }

    ngx_stream_lua_check_context(L, ctx, NGX_STREAM_LUA_CONTEXT_CONTENT
                                 | NGX_STREAM_LUA_CONTEXT_TIMER);

    coctx = ctx->cur_co_ctx;

    sub_co = lua_tothread(L, 1);
    luaL_argcheck(L, sub_co, 1, "stream lua thread expected");

    sub_coctx = ngx_stream_lua_get_co_ctx(sub_co, ctx);

    if (sub_coctx == NULL) {
        return luaL_error(L, "no co ctx found");
    }

    if (!sub_coctx->is_uthread) {
        lua_pushnil(L);
        lua_pushliteral(L, "not user thread");
        return 2;
    }

    if (sub_coctx->parent_co_ctx != coctx) {
        lua_pushnil(L);
        lua_pushliteral(L, "killer not parent");
        return 2;
    }

    switch (sub_coctx->co_status) {
    case NGX_STREAM_LUA_CO_ZOMBIE:
        ngx_stream_lua_del_thread(s, L, ctx, sub_coctx);
        ctx->uthreads--;

        lua_pushnil(L);
        lua_pushliteral(L, "already terminated");
        return 2;

    case NGX_STREAM_LUA_CO_DEAD:
        lua_pushnil(L);
        lua_pushliteral(L, "already waited or killed");
        return 2;

    default:
        ngx_stream_lua_cleanup_pending_operation(sub_coctx);
        ngx_stream_lua_del_thread(s, L, ctx, sub_coctx);
        ctx->uthreads--;

        lua_pushinteger(L, 1);
        return 1;
    }

    /* not reacheable */
}
Exemplo n.º 19
0
LUAMOD_API int luaopen_utf8 (lua_State *L) {
  luaL_newlib(L, funcs);
  lua_pushliteral(L, UTF8PATT);
  lua_setfield(L, -2, "charpattern");
  return 1;
}
Exemplo n.º 20
0
static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) {
  (void)lib; (void)sym;  /* to avoid warnings */
  lua_pushliteral(L, DLMSG);
  return NULL;
}
Exemplo n.º 21
0
static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) {
  (void)(lib); (void)(sym);  /* not used */
  lua_pushliteral(L, DLMSG);
  return NULL;
}
Exemplo n.º 22
0
int luabins_save(lua_State * L, int index_from, int index_to)
{
  unsigned char num_to_save = 0;
  int index = index_from;
  int base = lua_gettop(L);
  luabins_SaveBuffer sb;

  /*
  * TODO: If lua_error() would happen below, would leak the buffer.
  */

  if (index_to - index_from > LUABINS_MAXTUPLE)
  {
    lua_pushliteral(L, "can't save that many items");
    return LUABINS_EFAILURE;
  }

  /* Allowing to call luabins_save(L, 1, lua_gettop(L))
     from C function, called from Lua with no arguments
     (when lua_gettop() would return 0)
  */
  if (index_to < index_from)
  {
    index_from = 0;
    index_to = 0;
    num_to_save = 0;
  }
  else
  {
    if (
        index_from < 0 || index_from > base ||
        index_to < 0 || index_to > base
      )
    {
      lua_pushliteral(L, "can't save: inexistant indices");
      return LUABINS_EFAILURE;
    }

    num_to_save = index_to - index_from + 1;
  }

  {
    void * alloc_ud = NULL;
    lua_Alloc alloc_fn = lua_getallocf(L, &alloc_ud);
    lbsSB_init(&sb, alloc_fn, alloc_ud);
  }

  lbs_writeTupleSize(&sb, num_to_save);
  for ( ; index <= index_to; ++index)
  {
    int result = 0;

    result = save_value(L, &sb, index, 0);
    if (result != LUABINS_ESUCCESS)
    {
      switch (result)
      {
      case LUABINS_EBADTYPE:
        lua_pushliteral(L, "can't save: unsupported type detected");
        break;

      case LUABINS_ETOODEEP:
        lua_pushliteral(L, "can't save: nesting is too deep");
        break;

      case LUABINS_ETOOLONG:
        lua_pushliteral(L, "can't save: not enough memory");
        break;

      default: /* Should not happen */
        lua_pushliteral(L, "save failed");
        break;
      }

      lbsSB_destroy(&sb);

      return result;
    }
  }

  {
    size_t len = 0UL;
    const unsigned char * buf = lbsSB_buffer(&sb, &len);
    lua_pushlstring(L, (const char *)buf, len);
    lbsSB_destroy(&sb);
  }

  return LUABINS_ESUCCESS;
}
static void
ngx_http_lua_socket_resolve_handler(ngx_resolver_ctx_t *ctx)
{
    ngx_http_request_t                  *r;
    ngx_connection_t                    *c;
    ngx_http_upstream_resolved_t        *ur;
    ngx_http_lua_ctx_t                  *lctx;
    lua_State                           *L;
    ngx_http_lua_socket_udp_upstream_t  *u;
    u_char                              *p;
    size_t                               len;
#if defined(nginx_version) && nginx_version >= 1005008
    socklen_t                            socklen;
    struct sockaddr                     *sockaddr;
#else
    struct sockaddr_in                  *sin;
#endif
    ngx_uint_t                           i;
    unsigned                             waiting;

    u = ctx->data;
    r = u->request;
    c = r->connection;
    ur = u->resolved;

    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
                   "lua udp socket resolve handler");

    lctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
    if (lctx == NULL) {
        return;
    }

    lctx->cur_co_ctx = u->co_ctx;

    u->co_ctx->cleanup = NULL;

    L = lctx->cur_co_ctx->co;

    dd("setting socket_ready to 1");

    waiting = u->waiting;

    if (ctx->state) {
        ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
                       "lua udp socket resolver error: %s (waiting: %d)",
                       ngx_resolver_strerror(ctx->state), (int) u->waiting);

        lua_pushnil(L);
        lua_pushlstring(L, (char *) ctx->name.data, ctx->name.len);
        lua_pushfstring(L, " could not be resolved (%d: %s)",
                        (int) ctx->state,
                        ngx_resolver_strerror(ctx->state));
        lua_concat(L, 2);

#if 1
        ngx_resolve_name_done(ctx);
        ur->ctx = NULL;
#endif

        u->prepare_retvals = ngx_http_lua_socket_error_retval_handler;
        ngx_http_lua_socket_udp_handle_error(r, u,
                                             NGX_HTTP_LUA_SOCKET_FT_RESOLVER);

        if (waiting) {
            ngx_http_run_posted_requests(c);
        }

        return;
    }

    ur->naddrs = ctx->naddrs;
    ur->addrs = ctx->addrs;

#if (NGX_DEBUG)
    {
#   if defined(nginx_version) && nginx_version >= 1005008
    u_char      text[NGX_SOCKADDR_STRLEN];
    ngx_str_t   addr;
#   else
    in_addr_t   addr;
#   endif
    ngx_uint_t  i;

#   if defined(nginx_version) && nginx_version >= 1005008
    addr.data = text;

    for (i = 0; i < ctx->naddrs; i++) {
        addr.len = ngx_sock_ntop(ur->addrs[i].sockaddr, ur->addrs[i].socklen,
                                 text, NGX_SOCKADDR_STRLEN, 0);

        ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                       "name was resolved to %V", &addr);
    }
#   else
    for (i = 0; i < ctx->naddrs; i++) {
        dd("addr i: %d %p", (int) i,  &ctx->addrs[i]);

        addr = ntohl(ctx->addrs[i]);

        ngx_log_debug4(NGX_LOG_DEBUG_HTTP, c->log, 0,
                       "name was resolved to %ud.%ud.%ud.%ud",
                       (addr >> 24) & 0xff, (addr >> 16) & 0xff,
                       (addr >> 8) & 0xff, addr & 0xff);
    }
#   endif
    }
#endif

    ngx_http_lua_assert(ur->naddrs > 0);

    if (ur->naddrs == 1) {
        i = 0;

    } else {
        i = ngx_random() % ur->naddrs;
    }

    dd("selected addr index: %d", (int) i);

#if defined(nginx_version) && nginx_version >= 1005008
    socklen = ur->addrs[i].socklen;

    sockaddr = ngx_palloc(r->pool, socklen);
    if (sockaddr == NULL) {
        goto nomem;
    }

    ngx_memcpy(sockaddr, ur->addrs[i].sockaddr, socklen);

    switch (sockaddr->sa_family) {
#if (NGX_HAVE_INET6)
    case AF_INET6:
        ((struct sockaddr_in6 *) sockaddr)->sin6_port = htons(ur->port);
        break;
#endif
    default: /* AF_INET */
        ((struct sockaddr_in *) sockaddr)->sin_port = htons(ur->port);
    }

    p = ngx_pnalloc(r->pool, NGX_SOCKADDR_STRLEN);
    if (p == NULL) {
        goto nomem;
    }

    len = ngx_sock_ntop(sockaddr, socklen, p, NGX_SOCKADDR_STRLEN, 1);
    ur->sockaddr = sockaddr;
    ur->socklen = socklen;

#else
    /* for nginx older than 1.5.8 */

    len = NGX_INET_ADDRSTRLEN + sizeof(":65536") - 1;

    p = ngx_pnalloc(r->pool, len + sizeof(struct sockaddr_in));
    if (p == NULL) {
        goto nomem;
    }

    sin = (struct sockaddr_in *) &p[len];
    ngx_memzero(sin, sizeof(struct sockaddr_in));

    len = ngx_inet_ntop(AF_INET, &ur->addrs[i], p, NGX_INET_ADDRSTRLEN);
    len = ngx_sprintf(&p[len], ":%d", ur->port) - p;

    sin->sin_family = AF_INET;
    sin->sin_port = htons(ur->port);
    sin->sin_addr.s_addr = ur->addrs[i];

    ur->sockaddr = (struct sockaddr *) sin;
    ur->socklen = sizeof(struct sockaddr_in);
#endif

    ur->host.data = p;
    ur->host.len = len;
    ur->naddrs = 1;

    ngx_resolve_name_done(ctx);
    ur->ctx = NULL;

    u->waiting = 0;

    if (waiting) {
        lctx->resume_handler = ngx_http_lua_socket_udp_resume;
        r->write_event_handler(r);
        ngx_http_run_posted_requests(c);

    } else {
        (void) ngx_http_lua_socket_resolve_retval_handler(r, u, L);
    }

    return;

nomem:

    if (ur->ctx) {
        ngx_resolve_name_done(ctx);
        ur->ctx = NULL;
    }

    u->prepare_retvals = ngx_http_lua_socket_error_retval_handler;
    ngx_http_lua_socket_udp_handle_error(r, u,
                                         NGX_HTTP_LUA_SOCKET_FT_NOMEM);

    if (waiting) {
        ngx_http_run_posted_requests(c);

    } else {
        lua_pushnil(L);
        lua_pushliteral(L, "no memory");
    }
}
Exemplo n.º 24
0
/*
 * Pop a Lua table representing a DFUI form from the Lua stack,
 * create a new DFUI form from it, and return it.
 */
static struct dfui_form *
dfui_form_from_lua_table(lua_State *L, int table_idx)
{
	struct dfui_form *f;
	struct dfui_action *a;
	struct dfui_field *fi;
	struct dfui_dataset *ds;
	const char *id, *name, *short_desc, *long_desc;
	int list_idx, subtable_idx, counter, done;

	/*
	 * Get the basic properties of the form.
	 */
	id = lua_access_table_string(L, table_idx, "id");
	name = lua_access_table_string(L, table_idx, "name");
	short_desc = lua_access_table_string(L, table_idx, "short_desc");
	long_desc = lua_access_table_string(L, table_idx, "long_desc");

	/*
	 * Create the initial form.
	 */
	f = dfui_form_new(id, dfui_info_new(name, short_desc, long_desc));

	set_dfui_properties_from_lua_table(L, table_idx, DFUI_OBJ_FORM, f);

	/*
	 * Get the list of actions attached to the form.
	 */
	lua_pushliteral(L, "actions");
	lua_gettable(L, table_idx);
	list_idx = lua_gettop(L);
	if (lua_istable(L, list_idx)) {
		/*
		 * Loop through all entries in this table, creating
		 * and attaching a new action for each one.
		 */
		counter = 1;
		done = 0;
		while (!done) {
			lua_pushnumber(L, counter++);
			lua_gettable(L, list_idx);
			subtable_idx = lua_gettop(L);
			if (lua_istable(L, subtable_idx)) {
				a = dfui_action_from_lua_table(L, subtable_idx);
				dfui_form_action_attach(f, a);
			} else {
				done = 1;
			}
		}
	} else {
		/* No actions */
	}
	lua_pop(L, 1);

	/*
	 * Get the list of fields attached to the form.
	 */
	lua_pushliteral(L, "fields");
	lua_gettable(L, table_idx);
	list_idx = lua_gettop(L);
	if (lua_istable(L, list_idx)) {
		/*
		 * Loop through all entries in this table, creating
		 * and attaching a new field for each one.
		 */
		counter = 1;
		done = 0;
		while (!done) {
			lua_pushnumber(L, counter++);
			lua_gettable(L, list_idx);
			subtable_idx = lua_gettop(L);
			if (lua_istable(L, subtable_idx)) {
				fi = dfui_field_from_lua_table(L, subtable_idx);
				dfui_form_field_attach(f, fi);
			} else {
				done = 1;
			}
		}
	} else {
		/* No fields */
	}
	lua_pop(L, 1);

	/*
	 * Get the list of datasets attached to the form.
	 */
	lua_pushliteral(L, "datasets");
	lua_gettable(L, table_idx);
	list_idx = lua_gettop(L);
	if (lua_istable(L, list_idx)) {
		/*
		 * Loop through all entries in this table, creating
		 * and attaching a new dataset for each one.
		 */
		counter = 1;
		done = 0;
		while (!done) {
			lua_pushnumber(L, counter++);
			lua_gettable(L, list_idx);
			subtable_idx = lua_gettop(L);
			if (lua_istable(L, subtable_idx)) {
				ds = dfui_dataset_from_lua_table(L, subtable_idx);
				dfui_form_dataset_add(f, ds);
			} else {
				done = 1;
			}
		}
	} else {
		/* No datasets */
	}
	lua_pop(L, 1);

	/*
	 * Finally, delete the table representing the form by
	 * popping it from the top of the stack.
	 */
	lua_pop(L, 1);

	return(f);
}
static int
ngx_http_lua_socket_udp_send(lua_State *L)
{
    ssize_t                              n;
    ngx_http_request_t                  *r;
    u_char                              *p;
    size_t                               len;
    ngx_http_lua_socket_udp_upstream_t  *u;
    int                                  type;
    const char                          *msg;
    ngx_str_t                            query;
    ngx_http_lua_loc_conf_t             *llcf;

    if (lua_gettop(L) != 2) {
        return luaL_error(L, "expecting 2 arguments (including the object), "
                          "but got %d", lua_gettop(L));
    }

    r = ngx_http_lua_get_req(L);
    if (r == NULL) {
        return luaL_error(L, "request object not found");
    }

    luaL_checktype(L, 1, LUA_TTABLE);

    lua_rawgeti(L, 1, SOCKET_CTX_INDEX);
    u = lua_touserdata(L, -1);
    lua_pop(L, 1);

    if (u == NULL || u->udp_connection.connection == NULL) {
        llcf = ngx_http_get_module_loc_conf(r, ngx_http_lua_module);

        if (llcf->log_socket_errors) {
            ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
                          "attempt to send data on a closed socket: u:%p, c:%p",
                          u, u ? u->udp_connection.connection : NULL);
        }

        lua_pushnil(L);
        lua_pushliteral(L, "closed");
        return 2;
    }

    if (u->request != r) {
        return luaL_error(L, "bad request");
    }

    if (u->ft_type) {
        u->ft_type = 0;
    }

    if (u->waiting) {
        lua_pushnil(L);
        lua_pushliteral(L, "socket busy");
        return 2;
    }

    type = lua_type(L, 2);
    switch (type) {
        case LUA_TNUMBER:
        case LUA_TSTRING:
            lua_tolstring(L, 2, &len);
            break;

        case LUA_TTABLE:
            len = ngx_http_lua_calc_strlen_in_table(L, 2, 2, 1 /* strict */);
            break;

        default:
            msg = lua_pushfstring(L, "string, number, boolean, nil, "
                                  "or array table expected, got %s",
                                  lua_typename(L, type));

            return luaL_argerror(L, 2, msg);
    }

    query.data = lua_newuserdata(L, len);
    query.len = len;

    switch (type) {
        case LUA_TNUMBER:
        case LUA_TSTRING:
            p = (u_char *) lua_tolstring(L, 2, &len);
            ngx_memcpy(query.data, (u_char *) p, len);
            break;

        case LUA_TTABLE:
            (void) ngx_http_lua_copy_str_in_table(L, 2, query.data);
            break;

        default:
            return luaL_error(L, "impossible to reach here");
    }

    u->ft_type = 0;

    /* mimic ngx_http_upstream_init_request here */

#if 1
    u->waiting = 0;
#endif

    dd("sending query %.*s", (int) query.len, query.data);

    n = ngx_send(u->udp_connection.connection, query.data, query.len);

    dd("ngx_send returns %d (query len %d)", (int) n, (int) query.len);

    if (n == NGX_ERROR || n == NGX_AGAIN) {
        u->socket_errno = ngx_socket_errno;

        return ngx_http_lua_socket_error_retval_handler(r, u, L);
    }

    if (n != (ssize_t) query.len) {
        dd("not the while query was sent");

        u->ft_type |= NGX_HTTP_LUA_SOCKET_FT_PARTIALWRITE;
        return ngx_http_lua_socket_error_retval_handler(r, u, L);
    }

    dd("n == len");

    lua_pushinteger(L, 1);
    return 1;
}
Exemplo n.º 26
0
/*
 * Push a new Lua table representing the given DFUI response
 * onto the Lua stack.
 */
static int
lua_table_from_dfui_response(lua_State *L, struct dfui_response *r)
{
	int table_idx, list_idx, subtable_idx;
	struct dfui_dataset *ds;
	struct dfui_celldata *cd;
	const char *value;
	int counter = 1;
	const char *f_id, *a_id;

	lua_newtable(L);
	table_idx = lua_gettop(L);

	/*
	 * Add response id's to the table.
	 */
	f_id = dfui_response_get_form_id(r);
	a_id = dfui_response_get_action_id(r);

	lua_pushliteral(L, "form_id");
	lua_pushlstring(L, f_id, strlen(f_id));
	lua_settable(L, table_idx);

	lua_pushliteral(L, "action_id");
	lua_pushlstring(L, a_id, strlen(a_id));
	lua_settable(L, table_idx);

	/*
	 * Create 'datasets' lists to the table.
	 */
	lua_pushliteral(L, "datasets");
	lua_newtable(L);
	list_idx = lua_gettop(L);
	
	/*
	 * Add response datasets to the 'datasets' list.
	 */
	for (ds = dfui_response_dataset_get_first(r); ds != NULL;
	     ds = dfui_dataset_get_next(ds)) {
		lua_pushnumber(L, counter++);
		lua_newtable(L);
		subtable_idx = lua_gettop(L);
		/*
		 * Populate this subtable with the celldatas...
		 */
		for (cd = dfui_dataset_celldata_get_first(ds); cd != NULL;
		     cd = dfui_celldata_get_next(cd)) {
			f_id = dfui_celldata_get_field_id(cd);
			value = dfui_celldata_get_value(cd);
			lua_pushlstring(L, f_id, strlen(f_id));
			lua_pushlstring(L, value, strlen(value));
			lua_settable(L, subtable_idx);
		}
		/*
		 * Add this subtable to the list
		 */
		lua_settable(L, list_idx);
	}

	/*
	 * Add the 'datasets' list to the table.
	 */
	lua_settable(L, table_idx);

	return(table_idx);
}
Exemplo n.º 27
0
int
sys_start(int argc, char *argv[])
{
	int status;
	const char* f;
	lua_State *L;
	
	if (argc<2) {
		printf("%s: <filename.lua>\n", argv[0]);
		return 1;
	}
	
    s_init();
    log_print("sound> initialized\n");
	
	L = lua_open();
	/*luaopen_base(L);*/
	luaL_openlibs(L);
	luaopen_sys(L);
	luaopen_log(L);
	luaopen_mat4(L);
	luaopen_quat(L);
	luaopen_vec3(L);
	luaopen_event(L);
	luaopen_image(L);
	luaopen_render(L);
	luaopen_shader(L);
	luaopen_sound(L);
	luaopen_net(L);
	luaopen_util(L);
	luaopen_font(L);
	luaopen_md2(L);
	//luaopen_blender(L);
	
	lua_getglobal(L, "package");
	if (LUA_TTABLE != lua_type(L, 1)) {
		log_print("lua> 'package' is not a table\n");
		return 1;
	}
	lua_getfield(L, 1, "path");
	if (LUA_TSTRING != lua_type(L, 2)) {
		log_print("lua> 'package.path' is not a string\n");
		lua_pop(L, 1);
		return 1;
	}
	lua_pop(L, 1);
	
#if defined(__IPHONE__)
	f = full_path();
#else
	f = ".";
#endif

	/* package.path = f .. "/?.lua" */
	lua_pushlstring(L, f, strlen(f));
	lua_pushliteral(L, "/?.lua");
	lua_concat(L, 2);
	lua_setfield(L, 1, "path");

	lua_bootstrap(L, f);

#if defined(__IPHONE__)
	f = full_path_to_file(argv[1]);
#else
	f = argv[1];
#endif

	log_print("lua> initialized\n");
	printf("lua> loading %s\n", f);
	
	if (luaL_loadfile(L,f)==0) { 
		/* function runme = file_content */
		lua_setglobal(L, "runme");
		lua_getglobal(L, "runme");

		status = lua_pcall(L,0,LUA_MULTRET,0);
		assert(0 == report(L, status));
        
        if (lua_gettop(L) > 0) {
            status = lua_toboolean(L, -1);
            if (!status) {
                printf("lua> %s returned false\n", f);
                sys_quit();
            }
        }
		
        log_print("lua> loaded\n");
	} else {
		printf("lua> unable to load %s\n",f);
		report(L, status);
		sys_quit();
	}

	lua_state = L;
	
	/* TODO: Double check this stuff some day when bored. */
	return status > 0 ? false : true;	
}
Exemplo n.º 28
0
LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) {
  LoadF lf;
  int status, readstatus;
  int c;
  char fullFilename[128];
  int fnameindex = lua_gettop(L) + 1;  /* index of filename on the stack */
  lf.extraline = 0;
  if (filename == NULL) {
    lua_pushliteral(L, "=stdin");
    lf.f = stdin;
  }
  else {
    // Note: Always open as the requested file, Lua should not care about
    // our crazy directory remapping.
    lua_pushfstring(L, "@%s", filename);

#if (PS3)
    // On the PS3, check always the /app_home/Lua/ directory!
    // This works both for dev-testing via remote HDD and on the game disc.
    // These work probably too just for developing, but app_home is fine!
    // /host_root/Lua/ or /dev_bdvd/PS3_GAME/USRDIR/Lua/
    CheckForValidFilenameWithPath(fullFilename, filename, "/app_home/Lua/");
#elif XBOX
    // For the Xbox we have to make sure always to load files from D:\ because
    // fopen ALWAYS expects a full paths, there are no current directories on the
    // Xbox 360 and therefore no relative paths! Check always "D:\Lua\<file>"
    CheckForValidFilenameWithPath(fullFilename, filename, "D:\\Lua\\");
#else
    // On the PC we just use the default search logic (see luaconf.h) and we
    // don't care about directories since we will already be in the correct one!
    // In earlier versions we had a lot of extra checks here.
    strcpy(fullFilename, filename);
#endif

    // Rest of this code is untouched, we just use fullFilename now!
    lf.f = fopen(fullFilename, "r");
    if (lf.f == NULL)
      return errfile(L, "open", fnameindex);
  }
  c = getc(lf.f);
  if (c == '#') {  /* Unix exec. file? */
    lf.extraline = 1;
    while ((c = getc(lf.f)) != EOF && c != '\n') ;  /* skip first line */
    if (c == '\n') c = getc(lf.f);
  }
  if (c == LUA_SIGNATURE[0] && fullFilename) {  /* binary file? */
    lf.f = freopen(fullFilename, "rb", lf.f);  /* reopen in binary mode */
    if (lf.f == NULL) return errfile(L, "reopen", fnameindex);
    /* skip eventual `#!...' */
   while ((c = getc(lf.f)) != EOF && c != LUA_SIGNATURE[0]) ;
    lf.extraline = 0;
  }
  ungetc(c, lf.f);
  status = lua_load(L, getF, &lf, lua_tostring(L, -1));
  readstatus = ferror(lf.f);
  if (filename) fclose(lf.f);  /* close file (even in case of errors) */
  if (readstatus) {
    lua_settop(L, fnameindex);  /* ignore results from `lua_load' */
    return errfile(L, "read", fnameindex);
  }
  lua_remove(L, fnameindex);
  return status;
}
Exemplo n.º 29
0
int
luaopen_tls(lua_State *L)
{
  luaL_newmetatable(L, TLS_SECURE_CONTEXT_HANDLE);
  lua_pushliteral(L, "__index");
  lua_pushvalue(L, -2);  /* push metatable */
  lua_rawset(L, -3);  /* metatable.__index = metatable */
  luaL_openlib(L, NULL, tls_sc_lib, 0);
  lua_pushvalue(L, -1);

  lev__lua_tls_conn_init(L);

  luaL_openlib(L, "_tls", tls_lib, 1);

#ifdef SSL_OP_ALL
  LUVIT_DEFINE_CONSTANT(L, SSL_OP_ALL);
#endif

#ifdef SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION
  LUVIT_DEFINE_CONSTANT(L, SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION);
#endif

#ifdef SSL_OP_CIPHER_SERVER_PREFERENCE
  LUVIT_DEFINE_CONSTANT(L, SSL_OP_CIPHER_SERVER_PREFERENCE);
#endif

#ifdef SSL_OP_CISCO_ANYCONNECT
  LUVIT_DEFINE_CONSTANT(L, SSL_OP_CISCO_ANYCONNECT);
#endif

#ifdef SSL_OP_COOKIE_EXCHANGE
  LUVIT_DEFINE_CONSTANT(L, SSL_OP_COOKIE_EXCHANGE);
#endif

#ifdef SSL_OP_CRYPTOPRO_TLSEXT_BUG
  LUVIT_DEFINE_CONSTANT(L, SSL_OP_CRYPTOPRO_TLSEXT_BUG);
#endif

#ifdef SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS
  LUVIT_DEFINE_CONSTANT(L, SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS);
#endif

#ifdef SSL_OP_EPHEMERAL_RSA
  LUVIT_DEFINE_CONSTANT(L, SSL_OP_EPHEMERAL_RSA);
#endif

#ifdef SSL_OP_LEGACY_SERVER_CONNECT
  LUVIT_DEFINE_CONSTANT(L, SSL_OP_LEGACY_SERVER_CONNECT);
#endif

#ifdef SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER
  LUVIT_DEFINE_CONSTANT(L, SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER);
#endif

#ifdef SSL_OP_MICROSOFT_SESS_ID_BUG
  LUVIT_DEFINE_CONSTANT(L, SSL_OP_MICROSOFT_SESS_ID_BUG);
#endif

#ifdef SSL_OP_MSIE_SSLV2_RSA_PADDING
  LUVIT_DEFINE_CONSTANT(L, SSL_OP_MSIE_SSLV2_RSA_PADDING);
#endif

#ifdef SSL_OP_NETSCAPE_CA_DN_BUG
  LUVIT_DEFINE_CONSTANT(L, SSL_OP_NETSCAPE_CA_DN_BUG);
#endif

#ifdef SSL_OP_NETSCAPE_CHALLENGE_BUG
  LUVIT_DEFINE_CONSTANT(L, SSL_OP_NETSCAPE_CHALLENGE_BUG);
#endif

#ifdef SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG
  LUVIT_DEFINE_CONSTANT(L, SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG);
#endif

#ifdef SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG
  LUVIT_DEFINE_CONSTANT(L, SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG);
#endif

#ifdef SSL_OP_NO_COMPRESSION
  LUVIT_DEFINE_CONSTANT(L, SSL_OP_NO_COMPRESSION);
#endif

#ifdef SSL_OP_NO_QUERY_MTU
  LUVIT_DEFINE_CONSTANT(L, SSL_OP_NO_QUERY_MTU);
#endif

#ifdef SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
  LUVIT_DEFINE_CONSTANT(L, SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION);
#endif

#ifdef SSL_OP_NO_SSLv2
  LUVIT_DEFINE_CONSTANT(L, SSL_OP_NO_SSLv2);
#endif

#ifdef SSL_OP_NO_SSLv3
  LUVIT_DEFINE_CONSTANT(L, SSL_OP_NO_SSLv3);
#endif

#ifdef SSL_OP_NO_TICKET
  LUVIT_DEFINE_CONSTANT(L, SSL_OP_NO_TICKET);
#endif

#ifdef SSL_OP_NO_TLSv1
  LUVIT_DEFINE_CONSTANT(L, SSL_OP_NO_TLSv1);
#endif

#ifdef SSL_OP_NO_TLSv1_1
  LUVIT_DEFINE_CONSTANT(L, SSL_OP_NO_TLSv1_1);
#endif

#ifdef SSL_OP_NO_TLSv1_2
  LUVIT_DEFINE_CONSTANT(L, SSL_OP_NO_TLSv1_2);
#endif

#ifdef SSL_OP_PKCS1_CHECK_1
  LUVIT_DEFINE_CONSTANT(L, SSL_OP_PKCS1_CHECK_1);
#endif

#ifdef SSL_OP_PKCS1_CHECK_2
  LUVIT_DEFINE_CONSTANT(L, SSL_OP_PKCS1_CHECK_2);
#endif

#ifdef SSL_OP_SINGLE_DH_USE
  LUVIT_DEFINE_CONSTANT(L, SSL_OP_SINGLE_DH_USE);
#endif

#ifdef SSL_OP_SINGLE_ECDH_USE
  LUVIT_DEFINE_CONSTANT(L, SSL_OP_SINGLE_ECDH_USE);
#endif

#ifdef SSL_OP_SSLEAY_080_CLIENT_DH_BUG
  LUVIT_DEFINE_CONSTANT(L, SSL_OP_SSLEAY_080_CLIENT_DH_BUG);
#endif

#ifdef SSL_OP_SSLREF2_REUSE_CERT_TYPE_BUG
  LUVIT_DEFINE_CONSTANT(L, SSL_OP_SSLREF2_REUSE_CERT_TYPE_BUG);
#endif

#ifdef SSL_OP_TLS_BLOCK_PADDING_BUG
  LUVIT_DEFINE_CONSTANT(L, SSL_OP_TLS_BLOCK_PADDING_BUG);
#endif

#ifdef SSL_OP_TLS_D5_BUG
  LUVIT_DEFINE_CONSTANT(L, SSL_OP_TLS_D5_BUG);
#endif

#ifdef SSL_OP_TLS_ROLLBACK_BUG
  LUVIT_DEFINE_CONSTANT(L, SSL_OP_TLS_ROLLBACK_BUG);
#endif

  return 1;
}
Exemplo n.º 30
0
static int
parse(lua_State *L)
{
	size_t len;
	const char *str = luaL_checklstring(L, 1, &len);
	const char *start;
	unsigned char state = S_BEG;
	int arg = 0;

	lua_settop(L, 1);
	lua_createtable(L, 4, 4);

	for (; len > 0; len--, str++) {
		unsigned char c = *str;

		state = state_table[state][c < sizeof(ascii_class) ?
			                   ascii_class[c] : C_ETC];
		switch (state) {
		case S_NI1:
		case S_LP1:
			start = str + 1;
			break;

		case S_CM1:
		case S_PA1:
			start = str;
			break;

		case S_US1:
		case S_NIS:
			lua_pushlstring(L, start, str - start);
			lua_setfield(L, -2, "nick");
			start = str + 1;
			break;

		case S_USS:
		case S_HO1:
			lua_pushlstring(L, start, str - start);
			lua_setfield(L, -2, "user");
			start = str + 1;
			break;

		case S_HSS:
			lua_pushlstring(L, start, str - start);
			lua_setfield(L, -2, "host");
			break;

		case S_CMS:
			lua_pushlstring(L, start, str - start);
			lua_setfield(L, -2, "command");
			break;

		case S_PAS:
		case S_CR1:
			lua_pushlstring(L, start, str - start);
			lua_rawseti(L, -2, ++arg);
			break;

		case S____:
			goto error;
		}
	}

	if (state == S_LFF)
		return 1;

error:
	lua_pushnil(L);
	lua_pushliteral(L, "parse error");
	return 2;
}