MCPcopy Create free account
hub / github.com/defold/defold / Sys_GetIfaddrs

Function Sys_GetIfaddrs

engine/script/src/script_sys.cpp:1015–1095  ·  view source on GitHub ↗

enumerate network interfaces * * Returns an array of tables with information on network interfaces. * * @name sys.get_ifaddrs * @return ifaddrs [type:table] an array of tables. Each table entry contain the following fields: * * `name` * : [type:string] Interface name * * `address` * : [type:string] IP address. [icon:attention] might be `nil` i

Source from the content-addressed store, hash-verified

1013 * ```
1014 */
1015 static int Sys_GetIfaddrs(lua_State* L)
1016 {
1017 int top = lua_gettop(L);
1018 const uint32_t max_count = 16;
1019 dmSocket::IfAddr addresses[max_count];
1020
1021 uint32_t count = 0;
1022 dmSocket::GetIfAddresses(addresses, max_count, &count);
1023 lua_createtable(L, count, 0);
1024 for (uint32_t i = 0; i < count; ++i)
1025 {
1026 dmSocket::IfAddr* ifa = &addresses[i];
1027
1028 lua_newtable(L);
1029
1030 lua_pushstring(L, ifa->m_Name);
1031 lua_setfield(L, -2, "name");
1032
1033 if (ifa->m_Flags & dmSocket::FLAGS_INET)
1034 {
1035 char* ip = dmSocket::AddressToIPString(ifa->m_Address);
1036 if (ip)
1037 lua_pushstring(L, ip);
1038 else
1039 lua_pushnil(L);
1040 free(ip);
1041 }
1042 else
1043 {
1044 lua_pushnil(L);
1045 }
1046 lua_setfield(L, -2, "address");
1047
1048 if (ifa->m_Address.m_family == dmSocket::DOMAIN_IPV4)
1049 {
1050 lua_pushliteral(L, "ipv4");
1051 }
1052 else if (ifa->m_Address.m_family == dmSocket::DOMAIN_IPV6)
1053 {
1054 lua_pushliteral(L, "ipv6");
1055 }
1056 else
1057 {
1058 lua_pushnil(L);
1059 }
1060 lua_setfield(L, -2, "family");
1061
1062 if (ifa->m_Flags & dmSocket::FLAGS_LINK)
1063 {
1064 char tmp[64];
1065 dmSnPrintf(tmp, sizeof(tmp), "%02x:%02x:%02x:%02x:%02x:%02x",
1066 ifa->m_MacAddress[0],
1067 ifa->m_MacAddress[1],
1068 ifa->m_MacAddress[2],
1069 ifa->m_MacAddress[3],
1070 ifa->m_MacAddress[4],
1071 ifa->m_MacAddress[5]);
1072 lua_pushstring(L, tmp);

Callers

nothing calls this directly

Calls 13

lua_gettopFunction · 0.85
lua_createtableFunction · 0.85
lua_pushstringFunction · 0.85
lua_setfieldFunction · 0.85
lua_pushnilFunction · 0.85
freeFunction · 0.85
dmSnPrintfFunction · 0.85
lua_pushbooleanFunction · 0.85
lua_rawsetiFunction · 0.85
GetIfAddressesFunction · 0.50
AddressToIPStringFunction · 0.50

Tested by

no test coverage detected