Skip to content

Commit 1fcd025

Browse files
committed
Add net:gethostname/0
Signed-off-by: Paul Guyot <pguyot@kallisys.net>
1 parent f4b9821 commit 1fcd025

File tree

7 files changed

+71
-1
lines changed

7 files changed

+71
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
- Added menuconfig option for enabling USE_USB_SERIAL, eg. serial over USB for certain ESP32-S2 boards etc.
1313
- Partial support for `erlang:fun_info/2`
1414
- Added support for `registered_name` in `erlang:process_info/2` and `Process.info/2`
15+
- Added `net:gethostname/0` on platforms with gethostname(3).
1516

1617
### Fixed
1718
- ESP32: improved sntp sync speed from a cold boot.

libs/estdlib/src/net.erl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
-module(net).
2222

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

2525
%% nif call (so we can use guards at the API)
2626
-export([getaddrinfo_nif/2]).
@@ -78,3 +78,12 @@ getaddrinfo(Host, Service) when
7878
%% @hidden
7979
getaddrinfo_nif(_Host, _Service) ->
8080
erlang:nif_error(undefined).
81+
82+
%%-----------------------------------------------------------------------------
83+
%% @returns The (usually short) name of the host.
84+
%% @doc Get the hostname
85+
%% @end
86+
%%-----------------------------------------------------------------------------
87+
-spec gethostname() -> {ok, string()} | {error, any()}.
88+
gethostname() ->
89+
erlang:nif_error(undefined).

src/libAtomVM/otp_net.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <nifs.h>
2727
#include <otp_net.h>
2828
#include <port.h>
29+
#include <posix_nifs.h>
2930
#include <term.h>
3031

3132
#include <errno.h>
@@ -325,6 +326,38 @@ static term nif_net_getaddrinfo(Context *ctx, int argc, term argv[])
325326
return ret;
326327
}
327328

329+
//
330+
// net:gethostname/0
331+
//
332+
#ifdef HAVE_GETHOSTNAME
333+
static term nif_net_gethostname(Context *ctx, int argc, term argv[])
334+
{
335+
TRACE("nif_net_gethostname\n");
336+
UNUSED(argc);
337+
UNUSED(argv);
338+
339+
char buf[256];
340+
int r = gethostname(buf, sizeof(buf));
341+
if (UNLIKELY(r != 0)) {
342+
if (UNLIKELY(memory_ensure_free_opt(ctx, TUPLE_SIZE(2), MEMORY_CAN_SHRINK) != MEMORY_GC_OK)) {
343+
RAISE_ERROR(OUT_OF_MEMORY_ATOM);
344+
}
345+
return make_error_tuple(posix_errno_to_term(errno, ctx->global), ctx);
346+
}
347+
348+
size_t len = strlen(buf);
349+
if (UNLIKELY(memory_ensure_free_opt(ctx, TUPLE_SIZE(2) + LIST_SIZE(len, 1), MEMORY_CAN_SHRINK) != MEMORY_GC_OK)) {
350+
RAISE_ERROR(OUT_OF_MEMORY_ATOM);
351+
}
352+
353+
term result = term_alloc_tuple(2, &ctx->heap);
354+
term_put_tuple_element(result, 0, OK_ATOM);
355+
term_put_tuple_element(result, 1, interop_bytes_to_list(buf, len, &ctx->heap));
356+
357+
return result;
358+
}
359+
#endif
360+
328361
//
329362
// Nifs
330363
//
@@ -333,6 +366,12 @@ static const struct Nif net_getaddrinfo_nif = {
333366
.base.type = NIFFunctionType,
334367
.nif_ptr = nif_net_getaddrinfo
335368
};
369+
#ifdef HAVE_GETHOSTNAME
370+
static const struct Nif net_gethostname_nif = {
371+
.base.type = NIFFunctionType,
372+
.nif_ptr = nif_net_gethostname
373+
};
374+
#endif
336375

337376
//
338377
// Entrypoints
@@ -346,6 +385,12 @@ const struct Nif *otp_net_nif_get_nif(const char *nifname)
346385
TRACE("Resolved platform nif %s ...\n", nifname);
347386
return &net_getaddrinfo_nif;
348387
}
388+
#ifdef HAVE_GETHOSTNAME
389+
if (strcmp("gethostname/0", rest) == 0) {
390+
TRACE("Resolved platform nif %s ...\n", nifname);
391+
return &net_gethostname_nif;
392+
}
393+
#endif
349394
}
350395
return NULL;
351396
}

src/platforms/esp32/components/avm_sys/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ set(CMAKE_REQUIRED_INCLUDES ${CMAKE_REQUIRED_INCLUDES} ${soc_target_include_dir}
7070
include(CheckSymbolExists)
7171
include(CheckCSourceCompiles)
7272

73+
# Both don't exist in ESP32 at the moment
74+
define_if_function_exists(libAtomVM${PLATFORM_LIB_SUFFIX} getservbyname "netdb.h" PRIVATE HAVE_SERVBYNAME)
75+
define_if_function_exists(libAtomVM${PLATFORM_LIB_SUFFIX} gethostname "unistd.h" PRIVATE HAVE_GETHOSTNAME)
76+
7377
if ("${pthread_srcs}" MATCHES pthread_rwlock.c)
7478
set(HAVE_PTHREAD_RWLOCK YES)
7579
message(STATUS "pthread component includes pthread_rwlock.c, assuming it is available")

src/platforms/generic_unix/lib/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ endif()
5858
include(DefineIfExists)
5959
define_if_function_exists(libAtomVM${PLATFORM_LIB_SUFFIX} signal "signal.h" PRIVATE HAVE_SIGNAL)
6060
define_if_function_exists(libAtomVM${PLATFORM_LIB_SUFFIX} getservbyname "netdb.h" PRIVATE HAVE_SERVBYNAME)
61+
define_if_function_exists(libAtomVM${PLATFORM_LIB_SUFFIX} gethostname "unistd.h" PRIVATE HAVE_GETHOSTNAME)
6162

6263
target_link_libraries(libAtomVM${PLATFORM_LIB_SUFFIX} PUBLIC libAtomVM)
6364
include_directories(${CMAKE_SOURCE_DIR}/src/platforms/generic_unix/lib)

src/platforms/rp2/src/lib/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ if (PICO_CYW43_SUPPORTED)
102102
otp_net_lwip_raw.h)
103103
target_link_libraries(libAtomVM${PLATFORM_LIB_SUFFIX} PUBLIC pico_cyw43_arch_lwip_threadsafe_background pico_lwip_sntp INTERFACE pan_lwip_dhserver)
104104
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")
105+
106+
include(CheckSymbolExists)
107+
108+
define_if_function_exists(libAtomVM${PLATFORM_LIB_SUFFIX} getservbyname "netdb.h" PRIVATE HAVE_SERVBYNAME)
109+
define_if_function_exists(libAtomVM${PLATFORM_LIB_SUFFIX} gethostname "unistd.h" PRIVATE HAVE_GETHOSTNAME)
105110
endif()
106111

107112
target_link_options(libAtomVM${PLATFORM_LIB_SUFFIX} PUBLIC "SHELL:-Wl,-u -Wl,gpio_nif -Wl,-u -Wl,otp_crypto_nif")

tests/libs/estdlib/test_net.erl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
test() ->
2626
ok = test_getaddrinfo(),
2727
ok = test_getaddrinfo2(),
28+
ok = test_gethostname(),
2829
ok.
2930

3031
test_getaddrinfo() ->
@@ -141,3 +142,7 @@ get_addr(AddrInfo) ->
141142
_ ->
142143
maps:get(addr, maps:get(address, AddrInfo))
143144
end.
145+
146+
test_gethostname() ->
147+
{ok, [_ | _]} = net:gethostname(),
148+
ok.

0 commit comments

Comments
 (0)