From 38ac31d30791a0acde2edfd932d41d8594801973 Mon Sep 17 00:00:00 2001 From: "hyunghwan.chung" Date: Fri, 12 Jan 2018 16:06:05 +0000 Subject: [PATCH] added a function to return an address length given a socket address object. fixed pf_connect to get the right address argument --- moo/kernel/Socket.moo | 7 +++++-- moo/mod/_sck.h | 3 +++ moo/mod/sck-addr.c | 30 +++++++++++++++++++++++++++++- moo/mod/sck.c | 6 +++--- 4 files changed, 40 insertions(+), 6 deletions(-) diff --git a/moo/kernel/Socket.moo b/moo/kernel/Socket.moo index 608724f..4c9446f 100644 --- a/moo/kernel/Socket.moo +++ b/moo/kernel/Socket.moo @@ -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 | (* diff --git a/moo/mod/_sck.h b/moo/mod/_sck.h index b1dfd78..f2c4aab 100644 --- a/moo/mod/_sck.h +++ b/moo/mod/_sck.h @@ -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 diff --git a/moo/mod/sck-addr.c b/moo/mod/sck-addr.c index 0197b1c..6f77fb2 100644 --- a/moo/mod/sck-addr.c +++ b/moo/mod/sck-addr.c @@ -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; + } +} + diff --git a/moo/mod/sck.c b/moo/mod/sck.c index 50d27b6..b752295 100644 --- a/moo/mod/sck.c +++ b/moo/mod/sck.c @@ -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 } },