-
Notifications
You must be signed in to change notification settings - Fork 2
/
NetworkWin.cpp
57 lines (49 loc) · 1.09 KB
/
NetworkWin.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "include.h"
#ifdef _WIN32
#define _WIN32_WINNT 0x0A00 // Win10
#include <winsock2.h>
#include <ws2tcpip.h>
int network_getip(lua_State* L)
{
const char* host = luaL_checkstring(L, 1);
addrinfo* result = NULL;
if (getaddrinfo(host, NULL, NULL, &result) < 0)
{
int errcode = WSAGetLastError();
put_winerror(L, errcode, "getaddrinfo");
return lua_error(L);
}
int cnt = 0;
for (addrinfo* pinfo = result; pinfo; pinfo = pinfo->ai_next)
{
switch (pinfo->ai_family)
{
case AF_INET:
{
char buffer[1024] = { 0 };
if (inet_ntop(AF_INET, &((sockaddr_in*)(pinfo->ai_addr))->sin_addr, buffer, 1024))
{
int errcode = WSAGetLastError();
put_winerror(L, errcode, "inet_ntop");
freeaddrinfo(result);
return lua_error(L);
}
lua_pushstring(L, buffer);
cnt++;
break;
}
}
}
freeaddrinfo(result);
return cnt;
}
void InitNetwork(lua_State* L)
{
lua_getglobal(L, "package");
lua_getfield(L, -1, "loaded");
lua_newtable(L);
lua_setfield_function(L, "getip", network_getip);
lua_setfield(L, -2, "network");
lua_pop(L, 2);
}
#endif // End of _WIN32