Beispiel #1
0
static void
totimespec(lua_State *L, int index, struct timespec *ts)
{
	luaL_checktype(L, index, LUA_TTABLE);
	ts->tv_sec  = optintfield(L, index, "tv_sec", 0);
	ts->tv_nsec = optintfield(L, index, "tv_nsec", 0);

	checkfieldnames(L, index, Stimespec_fields);
}
Beispiel #2
0
static void
totm(lua_State *L, int index, struct tm *t)
{
	luaL_checktype(L, index, LUA_TTABLE);
	t->tm_sec   = optintfield(L, index, "tm_sec", 0);
	t->tm_min   = optintfield(L, index, "tm_min", 0);
	t->tm_hour  = optintfield(L, index, "tm_hour", 0);
	t->tm_mday  = optintfield(L, index, "tm_mday", 0);
	t->tm_mon   = optintfield(L, index, "tm_mon", 0);
	t->tm_year  = optintfield(L, index, "tm_year", 0);
	t->tm_wday  = optintfield(L, index, "tm_wday", 0);
	t->tm_yday  = optintfield(L, index, "tm_yday", 0);
	t->tm_isdst = optintfield(L, index, "tm_isdst", 0);

	checkfieldnames(L, index, Stm_fields);
}
Beispiel #3
0
/***
Network address and service translation.
@function getaddrinfo
@string host name of a host
@string service name of service
@tparam[opt] PosixAddrInfo hints table
@treturn[1] list of @{sockaddr} tables
@return[2] nil
@treturn[2] string error message
@treturn[2] int errnum
@see getaddrinfo(2)
@usage
local res, errmsg, errcode = posix.getaddrinfo ("www.lua.org", "http",
  { family = P.IF_INET, socktype = P.SOCK_STREAM }
)
*/
static int
Pgetaddrinfo(lua_State *L)
{
	int n = 1;
	const char *host = optstring(L, 1, NULL);
	const char *service = NULL;
	struct addrinfo *res, hints;
	hints.ai_family = PF_UNSPEC;

	checknargs(L, 3);

	switch (lua_type(L, 2))
	{
		case LUA_TNONE:
		case LUA_TNIL:
			if (host == NULL)
				argtypeerror(L, 2, "string or int");
			break;
		case LUA_TNUMBER:
		case LUA_TSTRING:
			service = lua_tostring(L, 2);
			break;
		default:
			argtypeerror(L, 2, "string, int or nil");
			break;
	}

	switch (lua_type(L, 3))
	{
		case LUA_TNONE:
		case LUA_TNIL:
			break;
		case LUA_TTABLE:
			checkfieldnames (L, 3, Sai_fields);
			hints.ai_family   = optintfield(L, 3, "family", PF_UNSPEC);
			hints.ai_socktype = optintfield(L, 3, "socktype", 0);
			hints.ai_protocol = optintfield(L, 3, "protocol", 0);
			hints.ai_flags    = optintfield(L, 3, "flags", 0);
			break;
		default:
			argtypeerror(L, 3, "table or nil");
			break;
	}

	{
		int r;
		if ((r = getaddrinfo(host, service, &hints, &res)) != 0)
		{
			lua_pushnil(L);
			lua_pushstring(L, gai_strerror(r));
			lua_pushinteger(L, r);
			return 3;
		}
	}

	/* Copy getaddrinfo() result into Lua table */
	{
		struct addrinfo *p;
		lua_newtable(L);
		for (p = res; p != NULL; p = p->ai_next)
		{
			lua_pushnumber(L, n++);
			pushsockaddrinfo(L, p->ai_family, p->ai_addr);
			pushnumberfield("socktype",  p->ai_socktype);
			pushstringfield("canonname", p->ai_canonname);
			pushnumberfield("protocol",  p->ai_protocol);
			lua_settable(L, -3);
		}
	}
	freeaddrinfo(res);


	return 1;
}