src/p_net_ifc_lnx.c
int
p_net_ifc_get( lua_State *L, const char *if_name )
{
	struct ifaddrs   *all_ifas, *ifa;

	if (0 != getifaddrs( &all_ifas ))
		return t_push_error( L, 0, 0, "couldn't retrieve interface information" );

	if (NULL == if_name)
		lua_newtable( L );            // create table that lists ALL interfaces
	else
		lua_pushnil( L );             // "fake" empty lua_getfield() result

	for (ifa = all_ifas; ifa; ifa = ifa->ifa_next)
	{
		//printf("N: %s(%d)\n", ifa->ifa_name, (ifa->ifa_addr) ? ifa->ifa_addr->sa_family : -1 );
		if (NULL != if_name && 0 != strcmp( ifa->ifa_name, if_name ))
			continue;
		if (NULL == if_name)
			lua_getfield( L, -1,  ifa->ifa_name );
		if (lua_isnil( L, -1 ))
		{
			lua_pop( L, 1 );        // pop nil
			lua_newtable( L );      // create table for specific named interface
			lua_pushstring( L, ifa->ifa_name );
			lua_setfield( L, -2, "name" );
			luaL_getmetatable( L, T_NET_IFC_TYPE );
			lua_setmetatable( L , -2 );
		}
		if (NULL == if_name)
		{
			lua_pushvalue( L, -1 ); // Repush table
			lua_setfield( L, -3, ifa->ifa_name );
		}
		p_net_ifc_parseFlags( L, ifa );      // only executes once per ifa_name
		p_net_ifs_getAddresses( L, ifa );    // only executes for sa_family = AF_INET || AF_INET6
		p_net_ifs_getStats( L, ifa );        // only executes for sa_family = AF_PACKET

		if (NULL == if_name)
			lua_pop( L, 1 );
	}

	freeifaddrs( all_ifas );
	return 1;
}
/**--------------------------------------------------------------------------
 * Create an t.Net.Interface Lua table and push to LuaStack.
 * \param   L      Lua state.
 * \lreturn table  t.Net.Interface instance or Lua table with all available
 *                 adapters instances.
 * \return  int    # of values pushed onto the stack.
 * --------------------------------------------------------------------------*/
Create an t.Net.Interface Lua table and push to LuaStack.
\param   L      Lua state.
\lreturn table  t.Net.Interface instance or Lua table with all available
adapters instances.
\return  int    # of values pushed onto the stack.
Create an t . Net . Interface Lua table and push to LuaStack . \ param L Lua state . \ lreturn table t . Net . Interface instance or Lua table with all available adapters instances . \ return int # of values pushed onto the stack .
int
p_net_ifc_get( lua_State *L, const char *if_name )
{
	struct ifaddrs   *all_ifas, *ifa;
	if (0 != getifaddrs( &all_ifas ))
		return t_push_error( L, 0, 0, "couldn't retrieve interface information" );
	if (NULL == if_name)
		lua_newtable( L );            
	else
		lua_pushnil( L );             
	for (ifa = all_ifas; ifa; ifa = ifa->ifa_next)
	{
		if (NULL != if_name && 0 != strcmp( ifa->ifa_name, if_name ))
			continue;
		if (NULL == if_name)
			lua_getfield( L, -1,  ifa->ifa_name );
		if (lua_isnil( L, -1 ))
		{
			lua_pop( L, 1 );        
			lua_newtable( L );      
			lua_pushstring( L, ifa->ifa_name );
			lua_setfield( L, -2, "name" );
			luaL_getmetatable( L, T_NET_IFC_TYPE );
			lua_setmetatable( L , -2 );
		}
		if (NULL == if_name)
		{
			lua_pushvalue( L, -1 ); 
			lua_setfield( L, -3, ifa->ifa_name );
		}
		p_net_ifc_parseFlags( L, ifa );      
		p_net_ifs_getAddresses( L, ifa );    
		p_net_ifs_getStats( L, ifa );        
		if (NULL == if_name)
			lua_pop( L, 1 );
	}
	freeifaddrs( all_ifas );
	return 1;
}
int p_net_ifc_get ( lua_State * L , const char * if_name ) { struct ifaddrs * all_ifas , * ifa ; if ( 0 != getifaddrs ( & all_ifas ) ) return t_push_error ( L , 0 , 0 , " " ) ; if ( NULL == if_name ) lua_newtable ( L ) ; else lua_pushnil ( L ) ; for ( ifa = all_ifas ; ifa ; ifa = ifa -> ifa_next ) { if ( NULL != if_name && 0 != strcmp ( ifa -> ifa_name , if_name ) ) continue ; if ( NULL == if_name ) lua_getfield ( L , -1 , ifa -> ifa_name ) ; if ( lua_isnil ( L , -1 ) ) { lua_pop ( L , 1 ) ; lua_newtable ( L ) ; lua_pushstring ( L , ifa -> ifa_name ) ; lua_setfield ( L , -2 , " " ) ; luaL_getmetatable ( L , T_NET_IFC_TYPE ) ; lua_setmetatable ( L , -2 ) ; } if ( NULL == if_name ) { lua_pushvalue ( L , -1 ) ; lua_setfield ( L , -3 , ifa -> ifa_name ) ; } p_net_ifc_parseFlags ( L , ifa ) ; p_net_ifs_getAddresses ( L , ifa ) ; p_net_ifs_getStats ( L , ifa ) ; if ( NULL == if_name ) lua_pop ( L , 1 ) ; } freeifaddrs ( all_ifas ) ; return 1 ; }
Create an t.Net.Interface Lua table and push to LuaStack.
Create an t . Net . Interface Lua table and push to LuaStack .
// create table that lists ALL interfaces // "fake" empty lua_getfield() result //printf("N: %s(%d)\n", ifa->ifa_name, (ifa->ifa_addr) ? ifa->ifa_addr->sa_family : -1 ); // pop nil // create table for specific named interface // Repush table // only executes once per ifa_name // only executes for sa_family = AF_INET || AF_INET6 // only executes for sa_family = AF_PACKET