mirror of
https://github.com/lunarmodules/luasocket.git
synced 2025-02-07 04:22:49 +01:00
internet class.
This commit is contained in:
parent
88026bef8a
commit
46828c1a7d
92
src/inet.c
92
src/inet.c
@ -1,11 +1,11 @@
|
|||||||
/*=========================================================================*\
|
/*=========================================================================*\
|
||||||
* Internet socket methods implementation
|
* Internet domain class
|
||||||
|
* Lua methods:
|
||||||
|
* getpeername: gets socket peer ip address and port
|
||||||
|
* getsockname: gets local socket ip address and port
|
||||||
* Global Lua fuctions:
|
* Global Lua fuctions:
|
||||||
* toip(hostname)
|
* toip: gets resolver info on host name
|
||||||
* tohostname(dotted-quad)
|
* tohostname: gets resolver info on dotted-quad
|
||||||
* Socket table methods:
|
|
||||||
* getpeername()
|
|
||||||
* getsockname()
|
|
||||||
\*=========================================================================*/
|
\*=========================================================================*/
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
@ -46,7 +46,9 @@ void inet_open(lua_State *L)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Hook object methods to methods table.
|
* Hook lua methods to methods table.
|
||||||
|
* Input
|
||||||
|
* lsclass: class name
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
void inet_inherit(lua_State *L, cchar *lsclass)
|
void inet_inherit(lua_State *L, cchar *lsclass)
|
||||||
{
|
{
|
||||||
@ -62,6 +64,9 @@ void inet_inherit(lua_State *L, cchar *lsclass)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*-------------------------------------------------------------------------*\
|
||||||
|
* Constructs the object
|
||||||
|
\*-------------------------------------------------------------------------*/
|
||||||
void inet_construct(lua_State *L, p_inet inet)
|
void inet_construct(lua_State *L, p_inet inet)
|
||||||
{
|
{
|
||||||
sock_construct(L, (p_sock) inet);
|
sock_construct(L, (p_sock) inet);
|
||||||
@ -71,7 +76,8 @@ void inet_construct(lua_State *L, p_inet inet)
|
|||||||
* Global Lua functions
|
* Global Lua functions
|
||||||
\*=========================================================================*/
|
\*=========================================================================*/
|
||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Returns the list of ip addresses associated with a host name
|
* Returns all information provided by the resolver given a host name
|
||||||
|
* or ip address
|
||||||
* Lua Input: address
|
* Lua Input: address
|
||||||
* address: ip address or hostname to dns lookup
|
* address: ip address or hostname to dns lookup
|
||||||
* Lua Returns
|
* Lua Returns
|
||||||
@ -98,7 +104,8 @@ static int inet_lua_toip(lua_State *L)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Returns the list of host names associated with an ip address
|
* Returns all information provided by the resolver given a host name
|
||||||
|
* or ip address
|
||||||
* Lua Input: address
|
* Lua Input: address
|
||||||
* address: ip address or host name to reverse dns lookup
|
* address: ip address or host name to reverse dns lookup
|
||||||
* Lua Returns
|
* Lua Returns
|
||||||
@ -124,7 +131,7 @@ static int inet_lua_tohostname(lua_State *L)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*=========================================================================*\
|
/*=========================================================================*\
|
||||||
* Socket table methods
|
* Lua methods
|
||||||
\*=========================================================================*/
|
\*=========================================================================*/
|
||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Retrieves socket peer name
|
* Retrieves socket peer name
|
||||||
@ -220,42 +227,28 @@ static void inet_pushresolved(lua_State *L, struct hostent *hp)
|
|||||||
* Returns
|
* Returns
|
||||||
* NULL in case of success, error message otherwise
|
* NULL in case of success, error message otherwise
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
cchar *inet_tryconnect(p_inet inet, cchar *address, ushort port,
|
cchar *inet_tryconnect(p_inet inet, cchar *address, ushort port)
|
||||||
int type)
|
|
||||||
{
|
{
|
||||||
struct sockaddr_in remote;
|
struct sockaddr_in remote;
|
||||||
cchar *err = NULL;
|
|
||||||
memset(&remote, 0, sizeof(remote));
|
memset(&remote, 0, sizeof(remote));
|
||||||
if (strlen(address) && inet_aton(address, &remote.sin_addr)) {
|
|
||||||
remote.sin_family = AF_INET;
|
remote.sin_family = AF_INET;
|
||||||
remote.sin_port = htons(port);
|
remote.sin_port = htons(port);
|
||||||
err = inet_trysocket(inet, type);
|
if (!strlen(address) || !inet_aton(address, &remote.sin_addr)) {
|
||||||
if (err) return err;
|
|
||||||
if (compat_connect(inet->fd, (SA *) &remote, sizeof(remote)) < 0) {
|
|
||||||
compat_close(inet->fd);
|
|
||||||
inet->fd = COMPAT_INVALIDFD;
|
|
||||||
return compat_connectstrerror();
|
|
||||||
} else return NULL;
|
|
||||||
/* go ahead and try by hostname resolution */
|
|
||||||
} else {
|
|
||||||
struct hostent *hp = gethostbyname(address);
|
struct hostent *hp = gethostbyname(address);
|
||||||
struct in_addr **addr;
|
struct in_addr **addr;
|
||||||
if (!hp) return compat_hoststrerror();
|
if (!hp) return compat_hoststrerror();
|
||||||
addr = (struct in_addr **) hp->h_addr_list;
|
addr = (struct in_addr **) hp->h_addr_list;
|
||||||
for (; *addr != NULL; addr++) {
|
|
||||||
memcpy(&remote.sin_addr, *addr, sizeof(struct in_addr));
|
memcpy(&remote.sin_addr, *addr, sizeof(struct in_addr));
|
||||||
remote.sin_family = AF_INET;
|
}
|
||||||
remote.sin_port = htons(port);
|
compat_setblocking(inet->fd);
|
||||||
err = inet_trysocket(inet, type);
|
|
||||||
if (err) return err;
|
|
||||||
if (compat_connect(inet->fd, (SA *) &remote, sizeof(remote)) < 0) {
|
if (compat_connect(inet->fd, (SA *) &remote, sizeof(remote)) < 0) {
|
||||||
|
const char *err = compat_connectstrerror();
|
||||||
compat_close(inet->fd);
|
compat_close(inet->fd);
|
||||||
inet->fd = COMPAT_INVALIDFD;
|
inet->fd = COMPAT_INVALIDFD;
|
||||||
} else return NULL;
|
return err;
|
||||||
memset(&remote, 0, sizeof(remote));
|
|
||||||
}
|
|
||||||
return compat_connectstrerror();
|
|
||||||
}
|
}
|
||||||
|
compat_setnonblocking(inet->fd);
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
@ -263,51 +256,34 @@ cchar *inet_tryconnect(p_inet inet, cchar *address, ushort port,
|
|||||||
* Input
|
* Input
|
||||||
* address: host name or ip address
|
* address: host name or ip address
|
||||||
* port: port number to bind to
|
* port: port number to bind to
|
||||||
* backlog: backlog to set
|
|
||||||
* Returns
|
* Returns
|
||||||
* NULL in case of success, error message otherwise
|
* NULL in case of success, error message otherwise
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
cchar *inet_trybind(p_inet inet, cchar *address, ushort port,
|
cchar *inet_trybind(p_inet inet, cchar *address, ushort port)
|
||||||
int type)
|
|
||||||
{
|
{
|
||||||
struct sockaddr_in local;
|
struct sockaddr_in local;
|
||||||
cchar *err = NULL;
|
|
||||||
memset(&local, 0, sizeof(local));
|
memset(&local, 0, sizeof(local));
|
||||||
/* address is either wildcard or a valid ip address */
|
/* address is either wildcard or a valid ip address */
|
||||||
local.sin_addr.s_addr = htonl(INADDR_ANY);
|
local.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||||
if (!strcmp(address, "*") ||
|
|
||||||
(strlen(address) && inet_aton(address, &local.sin_addr))) {
|
|
||||||
local.sin_port = htons(port);
|
local.sin_port = htons(port);
|
||||||
local.sin_family = AF_INET;
|
local.sin_family = AF_INET;
|
||||||
err = inet_trysocket(inet, type);
|
if (strcmp(address, "*") &&
|
||||||
if (err) return err;
|
(!strlen(address) || !inet_aton(address, &local.sin_addr))) {
|
||||||
compat_setreuseaddr(inet->fd);
|
|
||||||
if (compat_bind(inet->fd, (SA *) &local, sizeof(local)) < 0) {
|
|
||||||
compat_close(inet->fd);
|
|
||||||
inet->fd = COMPAT_INVALIDFD;
|
|
||||||
return compat_bindstrerror();
|
|
||||||
} else return NULL;
|
|
||||||
/* otherwise, proceed with domain name resolution */
|
|
||||||
} else {
|
|
||||||
struct hostent *hp = gethostbyname(address);
|
struct hostent *hp = gethostbyname(address);
|
||||||
struct in_addr **addr;
|
struct in_addr **addr;
|
||||||
if (!hp) return compat_hoststrerror();
|
if (!hp) return compat_hoststrerror();
|
||||||
addr = (struct in_addr **) hp->h_addr_list;
|
addr = (struct in_addr **) hp->h_addr_list;
|
||||||
for (; *addr != NULL; addr++) {
|
|
||||||
memcpy(&local.sin_addr, *addr, sizeof(struct in_addr));
|
memcpy(&local.sin_addr, *addr, sizeof(struct in_addr));
|
||||||
local.sin_port = htons(port);
|
}
|
||||||
local.sin_family = AF_INET;
|
compat_setblocking(inet->fd);
|
||||||
err = inet_trysocket(inet, type);
|
|
||||||
if (err) return err;
|
|
||||||
compat_setreuseaddr(inet->fd);
|
|
||||||
if (compat_bind(inet->fd, (SA *) &local, sizeof(local)) < 0) {
|
if (compat_bind(inet->fd, (SA *) &local, sizeof(local)) < 0) {
|
||||||
|
const char *err = compat_bindstrerror();
|
||||||
compat_close(inet->fd);
|
compat_close(inet->fd);
|
||||||
inet->fd = COMPAT_INVALIDFD;
|
inet->fd = COMPAT_INVALIDFD;
|
||||||
} else return NULL;
|
return err;
|
||||||
memset(&local, 0, sizeof(local));
|
|
||||||
}
|
|
||||||
return compat_bindstrerror();
|
|
||||||
}
|
}
|
||||||
|
compat_setnonblocking(inet->fd);
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
|
Loading…
x
Reference in New Issue
Block a user