Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add net:gethostname/0 #1411

Merged
merged 1 commit into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added menuconfig option for enabling USE_USB_SERIAL, eg. serial over USB for certain ESP32-S2 boards etc.
- Partial support for `erlang:fun_info/2`
- Added support for `registered_name` in `erlang:process_info/2` and `Process.info/2`
- Added `net:gethostname/0` on platforms with gethostname(3).

### Fixed
- ESP32: improved sntp sync speed from a cold boot.
Expand Down
11 changes: 10 additions & 1 deletion libs/estdlib/src/net.erl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

-module(net).

-export([getaddrinfo/1, getaddrinfo/2]).
-export([getaddrinfo/1, getaddrinfo/2, gethostname/0]).

%% nif call (so we can use guards at the API)
-export([getaddrinfo_nif/2]).
Expand Down Expand Up @@ -78,3 +78,12 @@ getaddrinfo(Host, Service) when
%% @hidden
getaddrinfo_nif(_Host, _Service) ->
erlang:nif_error(undefined).

%%-----------------------------------------------------------------------------
%% @returns The (usually short) name of the host.
%% @doc Get the hostname
%% @end
%%-----------------------------------------------------------------------------
-spec gethostname() -> {ok, string()} | {error, any()}.
gethostname() ->
erlang:nif_error(undefined).
45 changes: 45 additions & 0 deletions src/libAtomVM/otp_net.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <nifs.h>
#include <otp_net.h>
#include <port.h>
#include <posix_nifs.h>
#include <term.h>

#include <errno.h>
Expand Down Expand Up @@ -325,6 +326,38 @@
return ret;
}

//
// net:gethostname/0
//
#ifdef HAVE_GETHOSTNAME
static term nif_net_gethostname(Context *ctx, int argc, term argv[])
{
TRACE("nif_net_gethostname\n");
UNUSED(argc);
UNUSED(argv);

char buf[256];
int r = gethostname(buf, sizeof(buf));
if (UNLIKELY(r != 0)) {
if (UNLIKELY(memory_ensure_free_opt(ctx, TUPLE_SIZE(2), MEMORY_CAN_SHRINK) != MEMORY_GC_OK)) {
RAISE_ERROR(OUT_OF_MEMORY_ATOM);
}
return make_error_tuple(posix_errno_to_term(errno, ctx->global), ctx);
}

size_t len = strlen(buf);
if (UNLIKELY(memory_ensure_free_opt(ctx, TUPLE_SIZE(2) + LIST_SIZE(len, 1), MEMORY_CAN_SHRINK) != MEMORY_GC_OK)) {
RAISE_ERROR(OUT_OF_MEMORY_ATOM);
}

term result = term_alloc_tuple(2, &ctx->heap);
term_put_tuple_element(result, 0, OK_ATOM);

Check failure

Code scanning / CodeQL

Passing a non-term to a function expecting a term Error

Passing a non-term to a function expecting a term, without an explicit cast
term_put_tuple_element(result, 1, interop_bytes_to_list(buf, len, &ctx->heap));

return result;
}
#endif

//
// Nifs
//
Expand All @@ -333,6 +366,12 @@
.base.type = NIFFunctionType,
.nif_ptr = nif_net_getaddrinfo
};
#ifdef HAVE_GETHOSTNAME
static const struct Nif net_gethostname_nif = {
.base.type = NIFFunctionType,
.nif_ptr = nif_net_gethostname
};
#endif

//
// Entrypoints
Expand All @@ -346,6 +385,12 @@
TRACE("Resolved platform nif %s ...\n", nifname);
return &net_getaddrinfo_nif;
}
#ifdef HAVE_GETHOSTNAME
if (strcmp("gethostname/0", rest) == 0) {
TRACE("Resolved platform nif %s ...\n", nifname);
return &net_gethostname_nif;
}
#endif
}
return NULL;
}
Expand Down
4 changes: 4 additions & 0 deletions src/platforms/esp32/components/avm_sys/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ set(CMAKE_REQUIRED_INCLUDES ${CMAKE_REQUIRED_INCLUDES} ${soc_target_include_dir}
include(CheckSymbolExists)
include(CheckCSourceCompiles)

# Both don't exist in ESP32 at the moment
define_if_function_exists(libAtomVM${PLATFORM_LIB_SUFFIX} getservbyname "netdb.h" PRIVATE HAVE_SERVBYNAME)
define_if_function_exists(libAtomVM${PLATFORM_LIB_SUFFIX} gethostname "unistd.h" PRIVATE HAVE_GETHOSTNAME)

if ("${pthread_srcs}" MATCHES pthread_rwlock.c)
set(HAVE_PTHREAD_RWLOCK YES)
message(STATUS "pthread component includes pthread_rwlock.c, assuming it is available")
Expand Down
1 change: 1 addition & 0 deletions src/platforms/generic_unix/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ endif()
include(DefineIfExists)
define_if_function_exists(libAtomVM${PLATFORM_LIB_SUFFIX} signal "signal.h" PRIVATE HAVE_SIGNAL)
define_if_function_exists(libAtomVM${PLATFORM_LIB_SUFFIX} getservbyname "netdb.h" PRIVATE HAVE_SERVBYNAME)
define_if_function_exists(libAtomVM${PLATFORM_LIB_SUFFIX} gethostname "unistd.h" PRIVATE HAVE_GETHOSTNAME)

target_link_libraries(libAtomVM${PLATFORM_LIB_SUFFIX} PUBLIC libAtomVM)
include_directories(${CMAKE_SOURCE_DIR}/src/platforms/generic_unix/lib)
Expand Down
5 changes: 5 additions & 0 deletions src/platforms/rp2/src/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ if (PICO_CYW43_SUPPORTED)
otp_net_lwip_raw.h)
target_link_libraries(libAtomVM${PLATFORM_LIB_SUFFIX} PUBLIC pico_cyw43_arch_lwip_threadsafe_background pico_lwip_sntp INTERFACE pan_lwip_dhserver)
target_link_options(libAtomVM${PLATFORM_LIB_SUFFIX} PUBLIC "SHELL:-Wl,-u -Wl,networkregister_port_driver -Wl,-u -Wl,otp_socket_nif -Wl,-u -Wl,otp_net_nif -Wl,-u -Wl,otp_ssl_nif")

include(CheckSymbolExists)

define_if_function_exists(libAtomVM${PLATFORM_LIB_SUFFIX} getservbyname "netdb.h" PRIVATE HAVE_SERVBYNAME)
define_if_function_exists(libAtomVM${PLATFORM_LIB_SUFFIX} gethostname "unistd.h" PRIVATE HAVE_GETHOSTNAME)
endif()

target_link_options(libAtomVM${PLATFORM_LIB_SUFFIX} PUBLIC "SHELL:-Wl,-u -Wl,gpio_nif -Wl,-u -Wl,otp_crypto_nif")
5 changes: 5 additions & 0 deletions tests/libs/estdlib/test_net.erl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
test() ->
ok = test_getaddrinfo(),
ok = test_getaddrinfo2(),
ok = test_gethostname(),
ok.

test_getaddrinfo() ->
Expand Down Expand Up @@ -141,3 +142,7 @@ get_addr(AddrInfo) ->
_ ->
maps:get(addr, maps:get(address, AddrInfo))
end.

test_gethostname() ->
{ok, [_ | _]} = net:gethostname(),
ok.
Loading