diff options
author | Micael Karlberg <[email protected]> | 2018-06-20 12:33:18 +0200 |
---|---|---|
committer | Micael Karlberg <[email protected]> | 2018-09-18 14:50:18 +0200 |
commit | 6632cb103336786f3ca12f9d1f1d3338fc76c237 (patch) | |
tree | e756e471b64e5a4a6790c4e5c999a76aa3467c19 /erts/emulator/nifs/common | |
parent | 44cfb3d222ba4d20607af7cc654746f84ece3989 (diff) | |
download | otp-6632cb103336786f3ca12f9d1f1d3338fc76c237.tar.gz otp-6632cb103336786f3ca12f9d1f1d3338fc76c237.tar.bz2 otp-6632cb103336786f3ca12f9d1f1d3338fc76c237.zip |
[net-nif] Changed return type of getaddrinfo
The returned address info was supposed to be a map,
by was instead (still) a record.
OTP-14831
Diffstat (limited to 'erts/emulator/nifs/common')
-rw-r--r-- | erts/emulator/nifs/common/net_nif.c | 72 | ||||
-rw-r--r-- | erts/emulator/nifs/common/socket_int.h | 2 | ||||
-rw-r--r-- | erts/emulator/nifs/common/socket_nif.c | 4 | ||||
-rw-r--r-- | erts/emulator/nifs/common/socket_util.c | 43 | ||||
-rw-r--r-- | erts/emulator/nifs/common/socket_util.h | 5 |
5 files changed, 90 insertions, 36 deletions
diff --git a/erts/emulator/nifs/common/net_nif.c b/erts/emulator/nifs/common/net_nif.c index 572813ac62..6829de509b 100644 --- a/erts/emulator/nifs/common/net_nif.c +++ b/erts/emulator/nifs/common/net_nif.c @@ -324,11 +324,12 @@ static ERL_NIF_TERM encode_address_info_type(ErlNifEnv* env, static ERL_NIF_TERM encode_address_info_proto(ErlNifEnv* env, int proto); -static ERL_NIF_TERM make_address_info(ErlNifEnv* env, - ERL_NIF_TERM fam, - ERL_NIF_TERM sockType, - ERL_NIF_TERM proto, - ERL_NIF_TERM addr); +static char* make_address_info(ErlNifEnv* env, + ERL_NIF_TERM fam, + ERL_NIF_TERM sockType, + ERL_NIF_TERM proto, + ERL_NIF_TERM addr, + ERL_NIF_TERM* ai); static int on_load(ErlNifEnv* env, void** priv_data, ERL_NIF_TERM load_info); @@ -1245,6 +1246,7 @@ BOOLEAN_T decode_addrinfo_string(ErlNifEnv* env, BOOLEAN_T result; if (IS_ATOM(env, eString)) { + if (COMPARE(eString, esock_atom_undefined) == 0) { *stringP = NULL; result = TRUE; @@ -1252,28 +1254,11 @@ BOOLEAN_T decode_addrinfo_string(ErlNifEnv* env, *stringP = NULL; result = FALSE; } - } else { - unsigned int len; - char* bufP; - if (!GET_LIST_LEN(env, eString, &len) && (len != 0)) { - *stringP = NULL; - result = FALSE; - } + } else { - NDBG( ("NET", "decode_addrinfo_string -> len: %d\r\n", len) ); - - bufP = MALLOC(len + 1); // We shall NULL-terminate + result = esock_decode_string(env, eString, stringP); - if (GET_STR(env, eString, bufP, len+1)) { - NDBG( ("NET", "decode_addrinfo_string -> buf: %s\r\n", bufP) ); - // bufP[len] = '\0'; - *stringP = bufP; - result = TRUE; - } else { - *stringP = NULL; - result = FALSE; - } } return result; @@ -1373,7 +1358,7 @@ static ERL_NIF_TERM encode_address_info(ErlNifEnv* env, struct addrinfo* addrInfoP) { - ERL_NIF_TERM result, fam, type, proto, addr; + ERL_NIF_TERM fam, type, proto, addr, addrInfo; fam = encode_address_info_family(env, addrInfoP->ai_family); type = encode_address_info_type(env, addrInfoP->ai_socktype); @@ -1383,9 +1368,10 @@ ERL_NIF_TERM encode_address_info(ErlNifEnv* env, addrInfoP->ai_addrlen, &addr); - result = make_address_info(env, fam, type, proto, addr); - - return result; + if (make_address_info(env, fam, type, proto, addr, &addrInfo) == NULL) + return addrInfo; + else + return esock_atom_undefined; // We should to better... } @@ -1401,7 +1387,7 @@ ERL_NIF_TERM encode_address_info_family(ErlNifEnv* env, { ERL_NIF_TERM efam; - if (NULL != esock_encode_type(env, family, &efam)) + if (NULL != esock_encode_domain(env, family, &efam)) efam = MKI(env, family); return efam; @@ -1448,13 +1434,29 @@ ERL_NIF_TERM encode_address_info_proto(ErlNifEnv* env, static -ERL_NIF_TERM make_address_info(ErlNifEnv* env, - ERL_NIF_TERM fam, - ERL_NIF_TERM sockType, - ERL_NIF_TERM proto, - ERL_NIF_TERM addr) +char* make_address_info(ErlNifEnv* env, + ERL_NIF_TERM fam, + ERL_NIF_TERM sockType, + ERL_NIF_TERM proto, + ERL_NIF_TERM addr, + ERL_NIF_TERM* ai) { - return MKT5(env, atom_address_info, fam, sockType, proto, addr); + ERL_NIF_TERM keys[] = {esock_atom_family, + esock_atom_type, + esock_atom_protocol, + esock_atom_addr}; + ERL_NIF_TERM vals[] = {fam, sockType, proto, addr}; + unsigned int numKeys = sizeof(keys) / sizeof(ERL_NIF_TERM); + unsigned int numVals = sizeof(vals) / sizeof(ERL_NIF_TERM); + + ESOCK_ASSERT( (numKeys == numVals) ); + + if (!MKMA(env, keys, vals, numKeys, ai)) { + *ai = esock_atom_undefined; + return ESOCK_STR_EINVAL; + } else { + return NULL; + } } diff --git a/erts/emulator/nifs/common/socket_int.h b/erts/emulator/nifs/common/socket_int.h index 878ed513c8..aa10260134 100644 --- a/erts/emulator/nifs/common/socket_int.h +++ b/erts/emulator/nifs/common/socket_int.h @@ -119,6 +119,7 @@ extern ERL_NIF_TERM esock_atom_loopback; extern ERL_NIF_TERM esock_atom_ok; extern ERL_NIF_TERM esock_atom_path; extern ERL_NIF_TERM esock_atom_port; +extern ERL_NIF_TERM esock_atom_protocol; extern ERL_NIF_TERM esock_atom_raw; extern ERL_NIF_TERM esock_atom_rdm; extern ERL_NIF_TERM esock_atom_scope_id; @@ -127,6 +128,7 @@ extern ERL_NIF_TERM esock_atom_seqpacket; extern ERL_NIF_TERM esock_atom_stream; extern ERL_NIF_TERM esock_atom_tcp; extern ERL_NIF_TERM esock_atom_true; +extern ERL_NIF_TERM esock_atom_type; extern ERL_NIF_TERM esock_atom_udp; extern ERL_NIF_TERM esock_atom_undefined; diff --git a/erts/emulator/nifs/common/socket_nif.c b/erts/emulator/nifs/common/socket_nif.c index 1534bd0766..c23f0b021d 100644 --- a/erts/emulator/nifs/common/socket_nif.c +++ b/erts/emulator/nifs/common/socket_nif.c @@ -1373,6 +1373,7 @@ ERL_NIF_TERM esock_atom_local; ERL_NIF_TERM esock_atom_loopback; ERL_NIF_TERM esock_atom_ok; ERL_NIF_TERM esock_atom_path; +ERL_NIF_TERM esock_atom_protocol; ERL_NIF_TERM esock_atom_port; ERL_NIF_TERM esock_atom_raw; ERL_NIF_TERM esock_atom_rdm; @@ -1382,6 +1383,7 @@ ERL_NIF_TERM esock_atom_seqpacket; ERL_NIF_TERM esock_atom_stream; ERL_NIF_TERM esock_atom_tcp; ERL_NIF_TERM esock_atom_true; +ERL_NIF_TERM esock_atom_type; ERL_NIF_TERM esock_atom_udp; ERL_NIF_TERM esock_atom_undefined; ERL_NIF_TERM esock_atom_unknown; @@ -7443,6 +7445,7 @@ int on_load(ErlNifEnv* env, void** priv_data, ERL_NIF_TERM load_info) esock_atom_ok = MKA(env, "ok"); esock_atom_path = MKA(env, "path"); esock_atom_port = MKA(env, "port"); + esock_atom_protocol = MKA(env, "protocol"); esock_atom_raw = MKA(env, "raw"); esock_atom_rdm = MKA(env, "rdm"); esock_atom_scope_id = MKA(env, "scope_id"); @@ -7451,6 +7454,7 @@ int on_load(ErlNifEnv* env, void** priv_data, ERL_NIF_TERM load_info) esock_atom_stream = MKA(env, "stream"); esock_atom_tcp = MKA(env, "tcp"); esock_atom_true = MKA(env, "true"); + esock_atom_type = MKA(env, "type"); esock_atom_udp = MKA(env, "udp"); esock_atom_undefined = MKA(env, "undefined"); esock_atom_unknown = MKA(env, "unknown"); diff --git a/erts/emulator/nifs/common/socket_util.c b/erts/emulator/nifs/common/socket_util.c index 397f69f58d..5014688852 100644 --- a/erts/emulator/nifs/common/socket_util.c +++ b/erts/emulator/nifs/common/socket_util.c @@ -890,7 +890,7 @@ char* esock_decode_type(ErlNifEnv* env, -/* +++ esock_decode_domain +++ +/* +++ esock_decode_type +++ * * Encode the native type to the Erlang form, that is: * @@ -1038,6 +1038,47 @@ char* esock_decode_protocol(ErlNifEnv* env, +/* *** esock_decode_string *** + * + * Decode a string value. A successful decode results in an + * allocation of the string, which the caller has to free + * once the string has been used. + */ +extern +BOOLEAN_T esock_decode_string(ErlNifEnv* env, + const ERL_NIF_TERM eString, + char** stringP) +{ + BOOLEAN_T result; + unsigned int len; + char* bufP; + + if (!GET_LIST_LEN(env, eString, &len) && (len != 0)) { + *stringP = NULL; + result = FALSE; + } else { + + UDBG( ("SUTIL", "esock_decode_string -> len: %d\r\n", len) ); + + bufP = MALLOC(len + 1); // We shall NULL-terminate + + if (GET_STR(env, eString, bufP, len+1)) { + UDBG( ("SUTIL", "esock_decode_string -> buf: %s\r\n", bufP) ); + // bufP[len] = '\0'; + *stringP = bufP; + result = TRUE; + } else { + *stringP = NULL; + result = FALSE; + FREE(bufP); + } + } + + return result; +} + + + /* *** esock_decode_bool *** * * Decode a boolean value. diff --git a/erts/emulator/nifs/common/socket_util.h b/erts/emulator/nifs/common/socket_util.h index add2c8f4be..cd8cc7e1fb 100644 --- a/erts/emulator/nifs/common/socket_util.h +++ b/erts/emulator/nifs/common/socket_util.h @@ -134,6 +134,11 @@ char* esock_encode_protocol(ErlNifEnv* env, ERL_NIF_TERM* eProtocol); extern +BOOLEAN_T esock_decode_string(ErlNifEnv* env, + const ERL_NIF_TERM eString, + char** stringP); + +extern BOOLEAN_T esock_decode_bool(ERL_NIF_TERM val); extern ERL_NIF_TERM esock_encode_bool(BOOLEAN_T val); |