Skip to content

Commit

Permalink
Merge branch 'bmk/kernel/20241205/gen_x_socket_open_dbg_improvement/O…
Browse files Browse the repository at this point in the history
…TP-19386'
  • Loading branch information
bmk committed Jan 20, 2025
2 parents 6c63e29 + 018fe58 commit e1bd3f9
Show file tree
Hide file tree
Showing 8 changed files with 267 additions and 87 deletions.
3 changes: 3 additions & 0 deletions erts/emulator/nifs/common/socket_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ typedef int BOOLEAN_T;

#define B2S(__B__) ((__B__) ? "true" : "false")

#define DOM2STR(__D__) esock_domain_to_string((__D__))
#define PROTO2STR(__P__) esock_protocol_to_string((__P__))

#define TYPE2STR(T) (((T) == SOCK_STREAM) ? "stream" : \
(((T) == SOCK_DGRAM) ? "dgram" : \
(((T) == SOCK_RAW) ? "raw" : \
Expand Down
94 changes: 94 additions & 0 deletions erts/emulator/nifs/common/socket_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -1871,6 +1871,100 @@ void esock_encode_domain(ErlNifEnv* env,



/*
* This is only intended for debugging.
* Transforms a 'domain' to a printable string.
*/
extern
char* esock_domain_to_string(int domain)
{
switch (domain) {
case AF_INET:
return "inet";
break;

#if defined(HAVE_IN6) && defined(AF_INET6)
case AF_INET6:
return "inet6";
break;
#endif

#ifdef HAS_AF_LOCAL
case AF_LOCAL:
return "local";
break;
#endif

#ifdef AF_UNSPEC
case AF_UNSPEC:
return "unspec";
break;
#endif

default:
return "undefined";
}
}



/*
* This is only intended for debugging.
* Transforms a 'protocol' to a printable string.
*/
extern
char* esock_protocol_to_string(int protocol)
{
switch (protocol) {
#if defined(IPPROTO_IP)
case IPPROTO_IP:
return "ip";
break;
#endif

#if defined(IPPROTO_ICMP)
case IPPROTO_ICMP:
return "icmp";
break;
#endif

#if defined(IPPROTO_IGMP)
case IPPROTO_IGMP:
return "igmp";
break;
#endif

#if defined(IPPROTO_TCP)
case IPPROTO_TCP:
return "tcp";
break;
#endif

#if defined(IPPROTO_UDP)
case IPPROTO_UDP:
return "udp";
break;
#endif

#if defined(IPPROTO_SCTP)
case IPPROTO_SCTP:
return "sctp";
break;
#endif

#if defined(IPPROTO_RAW)
case IPPROTO_RAW:
return "raw";
break;
#endif

default:
return "undefined";
}
}



/* +++ esock_decode_type +++
*
* Decode the Erlang form of the 'type' type, that is:
Expand Down
6 changes: 6 additions & 0 deletions erts/emulator/nifs/common/socket_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ extern BOOLEAN_T esock_decode_timeval(ErlNifEnv* env,
ERL_NIF_TERM eTime,
struct timeval* timeP);

extern
char* esock_domain_to_string(int domain);

extern
void esock_encode_domain(ErlNifEnv* env,
int domain,
Expand All @@ -198,6 +201,9 @@ int esock_decode_domain(ErlNifEnv* env,
ERL_NIF_TERM eDomain,
int* domain);

extern
char* esock_protocol_to_string(int domain);

extern
BOOLEAN_T esock_decode_type(ErlNifEnv* env,
ERL_NIF_TERM eType,
Expand Down
47 changes: 28 additions & 19 deletions erts/emulator/nifs/unix/unix_socket_syncio.c
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,7 @@ ERL_NIF_TERM essio_open_with_fd(ErlNifEnv* env,
ESOCK_ASSERT( enif_self(env, &self) != NULL );

SSDBG2( dbg,
("UNIX-ESSIO", "essio_open2 -> entry with"
("UNIX-ESSIO", "essio_open_with_fd -> entry with"
"\r\n fd: %d"
"\r\n eopts: %T"
"\r\n", fd, eopts) );
Expand All @@ -851,7 +851,7 @@ ERL_NIF_TERM essio_open_with_fd(ErlNifEnv* env,
if (! open_which_domain(fd, &domain)) {
SSDBG2( dbg,
("UNIX-ESSIO",
"essio_open2 -> failed get domain from system\r\n") );
"essio_open_with_fd -> failed get domain from system\r\n") );

if (! open_get_domain(env, eopts, &domain)) {
return esock_make_invalid(env, esock_atom_domain);
Expand All @@ -861,7 +861,7 @@ ERL_NIF_TERM essio_open_with_fd(ErlNifEnv* env,
if (! open_which_type(fd, &type)) {
SSDBG2( dbg,
("UNIX-ESSIO",
"essio_open2 -> failed get type from system\r\n") );
"essio_open_with_fd -> failed get type from system\r\n") );

if (! open_get_type(env, eopts, &type))
return esock_make_invalid(env, esock_atom_type);
Expand All @@ -870,12 +870,12 @@ ERL_NIF_TERM essio_open_with_fd(ErlNifEnv* env,
if (! esock_open_which_protocol(fd, &protocol)) {
SSDBG2( dbg,
("UNIX-ESSIO",
"essio_open2 -> failed get protocol from system\r\n") );
"essio_open_with_fd -> failed get protocol from system\r\n") );

if (! open_get_protocol(env, eopts, &protocol)) {
SSDBG2( dbg,
("UNIX-ESSIO",
"essio_open2 -> "
"essio_open_with_fd -> "
"failed get protocol => try protocol 0\r\n") );
protocol = 0;
}
Expand All @@ -884,11 +884,14 @@ ERL_NIF_TERM essio_open_with_fd(ErlNifEnv* env,

SSDBG2( dbg,
("UNIX-ESSIO",
"essio_open2 -> "
"\r\n domain: %d"
"\r\n type: %d"
"\r\n protocol: %d"
"\r\n", domain, type, protocol) );
"essio_open_with_fd -> "
"\r\n domain: %d (%s)"
"\r\n type: %d (%s)"
"\r\n protocol: %d (%s)"
"\r\n",
domain, DOM2STR(domain),
type, TYPE2STR(type),
protocol, PROTO2STR(protocol)) );


if (open_todup(env, eopts)) {
Expand All @@ -898,7 +901,7 @@ ERL_NIF_TERM essio_open_with_fd(ErlNifEnv* env,

SSDBG2( dbg,
("UNIX-ESSIO",
"essio_open2 -> dup failed: %d\r\n",
"essio_open_with_fd -> dup failed: %d\r\n",
save_errno) );

return esock_make_error_errno(env, save_errno);
Expand Down Expand Up @@ -929,18 +932,20 @@ ERL_NIF_TERM essio_open_with_fd(ErlNifEnv* env,
if (sock_peer(descP->sock,
(struct sockaddr*) &remote,
&addrLen) == 0) {
SSDBG2( dbg, ("UNIX-ESSIO", "essio_open2 -> connected\r\n") );
SSDBG2( dbg, ("UNIX-ESSIO",
"essio_open_with_fd -> connected\r\n") );
descP->writeState |= ESOCK_STATE_CONNECTED;
} else {
SSDBG2( dbg, ("UNIX-ESSIO", "essio_open2 -> not connected\r\n") );
SSDBG2( dbg, ("UNIX-ESSIO",
"essio_open_with_fd -> not connected\r\n") );
}
}

/* And create the 'socket' resource */
sockRef = enif_make_resource(env, descP);
enif_release_resource(descP);

ESOCK_ASSERT( MONP("essio_open2 -> ctrl",
ESOCK_ASSERT( MONP("essio_open_with_fd -> ctrl",
env, descP,
&descP->ctrlPid,
&descP->ctrlMon) == 0 );
Expand All @@ -955,7 +960,7 @@ ERL_NIF_TERM essio_open_with_fd(ErlNifEnv* env,
if (descP->useReg) esock_send_reg_add_msg(env, descP, sockRef);

SSDBG2( dbg,
("UNIX-ESSIO", "essio_open2 -> done: %T\r\n", sockRef) );
("UNIX-ESSIO", "essio_open_with_fd -> done: %T\r\n", sockRef) );

return esock_make_ok2(env, sockRef);
}
Expand Down Expand Up @@ -1071,11 +1076,15 @@ ERL_NIF_TERM essio_open_plain(ErlNifEnv* env,

SSDBG2( dbg,
("UNIX-ESSIO", "essio_open4 -> entry with"
"\r\n domain: %d"
"\r\n type: %d"
"\r\n protocol: %d"
"\r\n domain: %d (%s)"
"\r\n type: %d (%s)"
"\r\n protocol: %d (%s)"
"\r\n eopts: %T"
"\r\n", domain, type, protocol, eopts) );
"\r\n",
domain, DOM2STR(domain),
type, TYPE2STR(type),
protocol, PROTO2STR(protocol),
eopts) );


#ifdef HAVE_SETNS
Expand Down
Loading

0 comments on commit e1bd3f9

Please sign in to comment.