refactored code for legacy platforms

This commit is contained in:
hyunghwan.chung 2018-01-19 09:33:47 +00:00
parent 84e91d5682
commit d3a57db467
3 changed files with 43 additions and 12 deletions

View File

@ -71,6 +71,7 @@ struct sck_t
typedef moo_uint64_t sck_len_t; typedef moo_uint64_t sck_len_t;
#endif #endif
#else #else
# undef MOO_SIZEOF_SOCKLEN_T
# define MOO_SIZEOF_SOCKLEN_T MOO_SIZEOF_INT # define MOO_SIZEOF_SOCKLEN_T MOO_SIZEOF_INT
# define MOO_SOCKLEN_T_IS_SIGNED # define MOO_SOCKLEN_T_IS_SIGNED
typedef int sck_len_t; typedef int sck_len_t;
@ -101,6 +102,7 @@ struct sck_t
typedef moo_uint64_t sck_addr_family_t; typedef moo_uint64_t sck_addr_family_t;
#endif #endif
#else #else
# undef MOO_SIZEOF_SA_FAMILY_T
# define MOO_SIZEOF_SA_FAMILY_T MOO_SIZEOF_INT # define MOO_SIZEOF_SA_FAMILY_T MOO_SIZEOF_INT
# define MOO_SA_FAMILY_T_IS_SIGNED # define MOO_SA_FAMILY_T_IS_SIGNED
typedef int sck_addr_family_t; typedef int sck_addr_family_t;

View File

@ -79,22 +79,22 @@ struct sck_addr_trailer_t
{ {
moo_ooch_t path[100]; moo_ooch_t path[100];
} local; */ } local; */
}; } u;
}; };
union sockaddr_t union sockaddr_t
{ {
#if defined(MOO_SIZEOF_STRUCT_SOCKADDR_IN) #if (MOO_SIZEOF_STRUCT_SOCKADDR_IN > 0)
struct sockaddr_in in4; struct sockaddr_in in4;
#endif #endif
#if defined(MOO_SIZEOF_STRUCT_SOCKADDR_IN6) #if (MOO_SIZEOF_STRUCT_SOCKADDR_IN6 > 0)
struct sockaddr_in6 in6; struct sockaddr_in6 in6;
#endif #endif
#if defined(MOO_SIZEOF_STRUCT_SOCKADDR_LL) #if (MOO_SIZEOF_STRUCT_SOCKADDR_LL > 0)
struct sockaddr_ll ll; struct sockaddr_ll ll;
#endif #endif
#if defined(MOO_SIZEOF_STRUCT_SOCKADDR_UN) #if (MOO_SIZEOF_STRUCT_SOCKADDR_UN > 0)
struct sockaddr_un un; struct sockaddr_un un;
#endif #endif
}; };
@ -142,6 +142,7 @@ static int str_to_ipv4 (const moo_ooch_t* str, moo_oow_t len, struct in_addr* in
} }
#if (MOO_SIZEOF_STRUCT_SOCKADDR_IN6 > 0)
static int str_to_ipv6 (const moo_ooch_t* src, moo_oow_t len, struct in6_addr* inaddr) static int str_to_ipv6 (const moo_ooch_t* src, moo_oow_t len, struct in6_addr* inaddr)
{ {
moo_uint8_t* tp, * endp, * colonp; moo_uint8_t* tp, * endp, * colonp;
@ -251,6 +252,7 @@ static int str_to_ipv6 (const moo_ooch_t* src, moo_oow_t len, struct in6_addr* i
return 0; return 0;
} }
#endif
static int str_to_sockaddr (moo_t* moo, const moo_ooch_t* str, moo_oow_t len, sockaddr_t* nwad) static int str_to_sockaddr (moo_t* moo, const moo_ooch_t* str, moo_oow_t len, sockaddr_t* nwad)
@ -291,6 +293,7 @@ static int str_to_sockaddr (moo_t* moo, const moo_ooch_t* str, moo_oow_t len, so
} }
#endif #endif
#if (MOO_SIZEOF_STRUCT_SOCKADDR_IN6 > 0)
if (*p == '[') if (*p == '[')
{ {
/* IPv6 address */ /* IPv6 address */
@ -353,6 +356,7 @@ TODO:
} }
else else
{ {
#endif
/* IPv4 address */ /* IPv4 address */
tmp.ptr = (moo_ooch_t*)p; tmp.ptr = (moo_ooch_t*)p;
while (p < end && *p != ':') p++; while (p < end && *p != ':') p++;
@ -360,6 +364,7 @@ TODO:
if (str_to_ipv4(tmp.ptr, tmp.len, &nwad->in4.sin_addr) <= -1) if (str_to_ipv4(tmp.ptr, tmp.len, &nwad->in4.sin_addr) <= -1)
{ {
#if (MOO_SIZEOF_STRUCT_SOCKADDR_IN6 > 0)
/* check if it is an IPv6 address not enclosed in []. /* check if it is an IPv6 address not enclosed in [].
* the port number can't be specified in this format. */ * the port number can't be specified in this format. */
if (p >= end || *p != ':') if (p >= end || *p != ':')
@ -423,10 +428,15 @@ TODO
nwad->in6.sin6_family = AF_INET6; nwad->in6.sin6_family = AF_INET6;
return 0; return 0;
#else
goto unrecog;
#endif
} }
nwad->in4.sin_family = AF_INET; nwad->in4.sin_family = AF_INET;
#if (MOO_SIZEOF_STRUCT_SOCKADDR_IN6 > 0)
} }
#endif
if (p < end && *p == ':') if (p < end && *p == ':')
{ {
@ -450,10 +460,14 @@ TODO
return -1; return -1;
} }
#if (MOO_SIZEOF_STRUCT_SOCKADDR_IN6 > 0)
if (nwad->in4.sin_family == AF_INET) if (nwad->in4.sin_family == AF_INET)
nwad->in4.sin_port = moo_hton16(port); nwad->in4.sin_port = moo_hton16(port);
else else
nwad->in6.sin6_port = moo_hton16(port); nwad->in6.sin6_port = moo_hton16(port);
#else
nwad->in4.sin_port = moo_hton16(port);
#endif
} }
return 0; return 0;
@ -546,7 +560,7 @@ sck_len_t moo_sck_addr_len (sck_addr_t* addr)
case AF_INET: case AF_INET:
return MOO_SIZEOF(struct sockaddr_in); return MOO_SIZEOF(struct sockaddr_in);
#endif #endif
#if defined(AF_INET) #if defined(AF_INET6)
case AF_INET6: case AF_INET6:
return MOO_SIZEOF(struct sockaddr_in6); return MOO_SIZEOF(struct sockaddr_in6);
#endif #endif

View File

@ -88,14 +88,22 @@ create_socket:
if (!(typev & (SOCK_NONBLOCK | SOCK_CLOEXEC))) if (!(typev & (SOCK_NONBLOCK | SOCK_CLOEXEC)))
#endif #endif
{ {
int oldfl; int fl;
oldfl = fcntl(fd, F_GETFL, 0); fl = fcntl(fd, F_GETFL, 0);
if (oldfl == -1 || fcntl(fd, F_SETFL, oldfl | O_CLOEXEC | O_NONBLOCK) == -1) if (fl == -1)
{ {
fcntl_failure:
moo_seterrwithsyserr (moo, errno); moo_seterrwithsyserr (moo, errno);
goto oops; goto oops;
} }
fl |= O_NONBLOCK;
#if defined(O_CLOEXEC)
fl |= O_CLOEXEC;
#endif
if (fcntl(fd, F_SETFL, fl) == -1) goto fcntl_failure;
} }
sck->handle = MOO_SMOOI_TO_OOP(fd); sck->handle = MOO_SMOOI_TO_OOP(fd);
@ -179,7 +187,7 @@ static moo_pfrc_t pf_accept_socket (moo_t* moo, moo_ooi_t nargs)
oop_sck_t sck, newsck; oop_sck_t sck, newsck;
moo_oop_t arg; moo_oop_t arg;
sck_len_t addrlen; sck_len_t addrlen;
int fd, newfd, oldfl; int fd, newfd, fl;
sck = (oop_sck_t)MOO_STACK_GETRCV(moo, nargs); sck = (oop_sck_t)MOO_STACK_GETRCV(moo, nargs);
arg = MOO_STACK_GETARG(moo, nargs, 0); arg = MOO_STACK_GETARG(moo, nargs, 0);
@ -219,14 +227,21 @@ static moo_pfrc_t pf_accept_socket (moo_t* moo, moo_ooi_t nargs)
moo_seterrwithsyserr (moo, errno); moo_seterrwithsyserr (moo, errno);
return MOO_PF_FAILURE; return MOO_PF_FAILURE;
} }
oldfl = fcntl(newfd, F_GETFL, 0); fl = fcntl(newfd, F_GETFL, 0);
if (oldfl == -1 || fcntl(newfd, F_SETFL, oldfl | O_NONBLOCK | O_CLOEXEC) == -1) if (fl == -1)
{ {
fcntl_failure:
moo_seterrwithsyserr (moo, errno); moo_seterrwithsyserr (moo, errno);
close (newfd); close (newfd);
return MOO_PF_FAILURE; return MOO_PF_FAILURE;
} }
fl |= O_NONBLOCK;
#if defined(O_CLOEXEC)
fl |= O_CLOEXEC;
#endif
if (fcntl(newfd, F_SETFL, fl) == -1) goto fcntl_failure;
accept_done: accept_done:
newsck = (oop_sck_t)moo_instantiate (moo, MOO_OBJ_GET_CLASS(sck), MOO_NULL, 0); newsck = (oop_sck_t)moo_instantiate (moo, MOO_OBJ_GET_CLASS(sck), MOO_NULL, 0);
if (!newsck) if (!newsck)