added a function to return an address length given a socket address object.

fixed pf_connect to get the right address argument
This commit is contained in:
hyunghwan.chung 2018-01-12 16:06:05 +00:00
parent b95102f953
commit 38ac31d307
4 changed files with 40 additions and 6 deletions

View File

@ -248,7 +248,7 @@ class Socket(Object) from 'sck'
method(#primitive) open(domain, type, proto).
method(#primitive) _close.
method(#primitive) _connect(a,b,c).
method(#primitive) _connect(addr).
method(#primitive) endConnect.
method(#primitive) readBytes: bytes.
@ -329,7 +329,7 @@ extend Socket
signal: s2 afterSecs: 10;
addAsyncSemaphore: s1;
addAsyncSemaphore: s2.
self _connect(1, 2, 3).
self _connect((SocketAddress fromString: '192.168.1.1:80')).
] ifCurtailed: [
## rollback
sa value: s2.
@ -394,6 +394,7 @@ s basicSize dump.
'****************************' dump.
*)
(***********************************
s := ByteArray new: 100.
s basicFillFrom: 0 with: ($a asInteger) count: 100.
s basicFillFrom: 50 with: ($b asInteger) count: 50.
@ -420,6 +421,8 @@ s basicSize dump.
##s := IP4SocketAddress new.
##s dump.
thisProcess terminate.
**************************)
inact := [:sck :state |
| data n |
(*

View File

@ -116,6 +116,9 @@ extern "C" {
MOO_EXPORT int moo_mod_sck (moo_t* moo, moo_mod_t* mod);
MOO_EXPORT int moo_mod_sck_addr (moo_t* moo, moo_mod_t* mod);
sck_len_t moo_sck_addr_len (sck_addr_t* addr);
#if defined(__cplusplus)
}
#endif

View File

@ -480,11 +480,12 @@ static moo_pfrc_t pf_from_string (moo_t* moo, moo_ooi_t nargs)
MOO_PF_CHECK_RCV (moo, MOO_OBJ_IS_BYTE_POINTER(rcv) && MOO_OBJ_GET_SIZE(rcv) >= MOO_SIZEOF(sockaddr_t));
MOO_PF_CHECK_ARGS (moo, nargs, MOO_OBJ_IS_CHAR_POINTER(str));
if (str_to_sockaddr (moo, MOO_OBJ_GET_CHAR_SLOT(str), MOO_OBJ_GET_SIZE(str), (sockaddr_t*)MOO_OBJ_GET_BYTE_SLOT(rcv)) <= -1)
if (str_to_sockaddr(moo, MOO_OBJ_GET_CHAR_SLOT(str), MOO_OBJ_GET_SIZE(str), (sockaddr_t*)MOO_OBJ_GET_BYTE_SLOT(rcv)) <= -1)
{
return MOO_PF_FAILURE;
}
MOO_DEBUG1(moo, "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXxx %O\n", rcv);
MOO_STACK_SETRETTORCV (moo, nargs);
return MOO_PF_SUCCESS;
}
@ -536,3 +537,30 @@ int moo_mod_sck_addr (moo_t* moo, moo_mod_t* mod)
return 0;
}
/* -------------------------------------------------------------------------- */
sck_len_t moo_sck_addr_len (sck_addr_t* addr)
{
switch (addr->family)
{
#if defined(AF_INET)
case AF_INET:
return MOO_SIZEOF(struct sockaddr_in);
#endif
#if defined(AF_INET)
case AF_INET6:
return MOO_SIZEOF(struct sockaddr_in6);
#endif
#if defined(AF_PACKET)
case AF_PACKET:
return MOO_SIZEOF(struct sockaddr_ll);
#endif
#if defined(AF_UNIX)
case AF_UNIX:
return MOO_SIZEOF(struct sockaddr_un);
#endif
default:
return 0;
}
}

View File

@ -146,7 +146,7 @@ static moo_pfrc_t pf_connect (moo_t* moo, moo_ooi_t nargs)
moo_oop_t arg;
sck = (oop_sck_t)MOO_STACK_GETRCV(moo, nargs);
arg = MOO_STACK_GETARG(moo, nargs, 1);
arg = MOO_STACK_GETARG(moo, nargs, 0);
MOO_PF_CHECK_RCV (moo,
MOO_OOP_IS_POINTER(sck) &&
@ -161,7 +161,7 @@ static moo_pfrc_t pf_connect (moo_t* moo, moo_ooi_t nargs)
do
{
//n = connect(fd, (struct sockaddr*)MOO_OBJ_GET_BYTE_SLOT(arg), addrlen);
n = connect(fd, (struct sockaddr*)MOO_OBJ_GET_BYTE_SLOT(arg), moo_sck_addr_len((sck_addr_t*)MOO_OBJ_GET_BYTE_SLOT(arg)));
}
while (n == -1 && errno == EINTR);
@ -326,7 +326,7 @@ static moo_pfinfo_t pfinfos[] =
{
{ I, { 'b','i','n','d','\0' }, 0, { pf_bind_socket, 1, 1 } },
{ I, { 'c','l','o','s','e','\0' }, 0, { pf_close_socket, 0, 0 } },
{ I, { 'c','o','n','n','e','c','t','\0' }, 0, { pf_connect, 3, 3 } },
{ I, { 'c','o','n','n','e','c','t','\0' }, 0, { pf_connect, 1, 1 } },
{ I, { 'e','n','d','C','o','n','n','e','c','t','\0' }, 0, { pf_end_connect, 0, 0 } },
{ I, { 'o','p','e','n','\0' }, 0, { pf_open_socket, 3, 3 } },
{ I, { 'r','e','a','d','B','y','t','e','s',':','\0' }, 0, { pf_read_socket, 1, 1 } },