added hcl_get_sockaddr_info()

This commit is contained in:
hyung-hwan 2018-03-25 02:53:03 +00:00
parent 0e2c892d8b
commit e5fbf1967f
3 changed files with 70 additions and 20 deletions

View File

@ -636,6 +636,16 @@ HCL_EXPORT int hcl_bcharstosckaddr (
# define hcl_oochars_to_sckaddr hcl_bchars_to_sckaddr # define hcl_oochars_to_sckaddr hcl_bchars_to_sckaddr
#endif #endif
/**
* The hcl_get_sckaddr_info() function returns the socket family.
* if \a scklen is not #HCL_NULL, it also sets the actual address length
* in the memory pointed to by it.
*/
HCL_EXPORT int hcl_get_sckaddr_info (
const hcl_sckaddr_t* sckaddr,
hcl_scklen_t* scklen
);
#if defined(__cplusplus) #if defined(__cplusplus)
} }
#endif #endif

View File

@ -510,7 +510,7 @@ static int feed_data (hcl_client_t* client, const void* ptr, hcl_oow_t len)
/* ========================================================================= */ /* ========================================================================= */
static int handle_request (hcl_client_t* client, const char* ipaddr, const char* script, int shut_wr_after_req) static int handle_request (hcl_client_t* client, const char* ipaddr, const char* script, int reuse_addr, int shut_wr_after_req)
{ {
hcl_sckaddr_t sckaddr; hcl_sckaddr_t sckaddr;
hcl_scklen_t scklen; hcl_scklen_t scklen;
@ -542,23 +542,26 @@ static int handle_request (hcl_client_t* client, const char* ipaddr, const char*
goto oops; goto oops;
} }
if (sckfam == AF_INET) if (reuse_addr)
{ {
struct sockaddr_in anyaddr; if (sckfam == AF_INET)
int opt = 1; {
setsockopt(sck, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt)); struct sockaddr_in anyaddr;
memset (&anyaddr, 0, HCL_SIZEOF(anyaddr)); int opt = 1;
anyaddr.sin_family = sckfam; setsockopt(sck, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt));
bind(sck, (struct sockaddr *)&anyaddr, scklen); memset (&anyaddr, 0, HCL_SIZEOF(anyaddr));
} anyaddr.sin_family = sckfam;
else if (sckfam == AF_INET6) bind(sck, (struct sockaddr *)&anyaddr, scklen);
{ }
struct sockaddr_in6 anyaddr; else if (sckfam == AF_INET6)
int opt = 1; {
setsockopt(sck, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt)); struct sockaddr_in6 anyaddr;
memset (&anyaddr, 0, HCL_SIZEOF(anyaddr)); int opt = 1;
anyaddr.sin6_family = sckfam; setsockopt(sck, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt));
bind(sck, (struct sockaddr *)&anyaddr, scklen); memset (&anyaddr, 0, HCL_SIZEOF(anyaddr));
anyaddr.sin6_family = sckfam;
bind(sck, (struct sockaddr *)&anyaddr, scklen);
}
} }
if (connect(sck, (struct sockaddr*)&sckaddr, scklen) <= -1) if (connect(sck, (struct sockaddr*)&sckaddr, scklen) <= -1)
@ -678,6 +681,7 @@ int main (int argc, char* argv[])
static hcl_bopt_lng_t lopt[] = static hcl_bopt_lng_t lopt[] =
{ {
{ ":log", 'l' }, { ":log", 'l' },
{ "reuseaddr", '\0' },
{ "shutwr", '\0' }, { "shutwr", '\0' },
{ HCL_NULL, '\0' } { HCL_NULL, '\0' }
}; };
@ -692,6 +696,7 @@ int main (int argc, char* argv[])
hcl_client_prim_t client_prim; hcl_client_prim_t client_prim;
int n; int n;
const char* logopt = HCL_NULL; const char* logopt = HCL_NULL;
int reuse_addr = 0;
int shut_wr_after_req = 0; int shut_wr_after_req = 0;
setlocale (LC_ALL, ""); setlocale (LC_ALL, "");
@ -699,7 +704,7 @@ int main (int argc, char* argv[])
if (argc < 2) if (argc < 2)
{ {
print_usage: print_usage:
fprintf (stderr, "Usage: %s [-l/--log log-options] [--shutwr] bind-address:port script-to-run\n", argv[0]); fprintf (stderr, "Usage: %s [-l/--log log-options] [--reuseaddr] [--shutwr] bind-address:port script-to-run\n", argv[0]);
return -1; return -1;
} }
@ -712,7 +717,11 @@ int main (int argc, char* argv[])
break; break;
case '\0': case '\0':
if (hcl_compbcstr(opt.lngopt, "shutwr") == 0) if (hcl_compbcstr(opt.lngopt, "reuseaddr") == 0)
{
reuse_addr = 1;
}
else if (hcl_compbcstr(opt.lngopt, "shutwr") == 0)
{ {
shut_wr_after_req = 1; shut_wr_after_req = 1;
} }
@ -770,7 +779,7 @@ int main (int argc, char* argv[])
set_signal (SIGINT, handle_sigint); set_signal (SIGINT, handle_sigint);
set_signal_to_ignore (SIGPIPE); set_signal_to_ignore (SIGPIPE);
n = handle_request (client, argv[opt.ind], argv[opt.ind + 1], shut_wr_after_req); n = handle_request (client, argv[opt.ind], argv[opt.ind + 1], reuse_addr, shut_wr_after_req);
set_signal_to_default (SIGINT); set_signal_to_default (SIGINT);
set_signal_to_default (SIGPIPE); set_signal_to_default (SIGPIPE);

View File

@ -1115,7 +1115,10 @@ hcl_uint128_t hcl_hton128 (hcl_uint128_t x)
union sockaddr_t union sockaddr_t
{ {
struct sockaddr sa;
#if (HCL_SIZEOF_STRUCT_SOCKADDR_IN > 0)
struct sockaddr_in in4; struct sockaddr_in in4;
#endif
#if (HCL_SIZEOF_STRUCT_SOCKADDR_IN6 > 0) #if (HCL_SIZEOF_STRUCT_SOCKADDR_IN6 > 0)
struct sockaddr_in6 in6; struct sockaddr_in6 in6;
#endif #endif
@ -1147,3 +1150,31 @@ typedef union sockaddr_t sockaddr_t;
#define str_to_ipv6 uchars_to_ipv6 #define str_to_ipv6 uchars_to_ipv6
#define str_to_sockaddr hcl_ucharstosckaddr #define str_to_sockaddr hcl_ucharstosckaddr
#include "sa-utl.h" #include "sa-utl.h"
int hcl_get_sckaddr_info (const hcl_sckaddr_t* sckaddr, hcl_scklen_t* scklen)
{
sockaddr_t* sa = (sockaddr_t*)sckaddr;
if (scklen)
{
switch (sa->sa.sa_family)
{
#if (HCL_SIZEOF_STRUCT_SOCKADDR_IN > 0)
case AF_INET:
*scklen = HCL_SIZEOF(sa->in4);
break;
#endif
#if (HCL_SIZEOF_STRUCT_SOCKADDR_IN6 > 0)
case AF_INET6:
*scklen = HCL_SIZEOF(sa->in6);
break;
#endif
default:
*scklen = 0; /* unknown */
break;
}
}
return sa->sa.sa_family;
}