5.8 KiB
libnx Q&A
What is libnx?
A portable C networking library for Unix-like systems: TCP and Unix sockets, an event loop, name resolution, URL parsing and an HTTP/1.1 client, with no dependency beyond libc and pthreads.
Why is there no HTTPS / TLS support?
TLS needs three things libc does not provide: X.509 certificate parsing, a
certificate trust store, and AEAD cipher implementations (AES-GCM,
ChaCha20-Poly1305) plus a safe random source and a TLS state machine. A correct
implementation of any one of those is a project of its own, and a subtly wrong
one is worse than none. https:// is recognised, rejected, and reported through
nx_url_error() and nx_http_error().
Can I add TLS later?
Yes. Nothing in the design prevents a separate module from wrapping a connected socket. The library deliberately does not ship a fake or best-effort TLS layer, because a TLS layer that silently downgrades or skips verification is a security hole.
Why not just link OpenSSL / mbedTLS / wolfSSL?
Because one of the project's constraints is zero external dependencies. If you want TLS, link the library of your choice on top of a libnx socket; libnx will not do it for you.
Why HTTP/1.1 and not HTTP/2 or HTTP/3?
Both need HPACK or QPACK and, for HTTP/3, QUIC over UDP. Those are large protocol implementations with their own state machines and congestion control. HTTP/1.1 covers the common request/response case and keeps the framing logic small enough to review.
Why is HTTP included if this is "just a networking library"?
The library was scoped to be useful for a network client without dragging in an HTTP library as a dependency. The HTTP client is deliberately small: one request at a time, no connection pooling, no pipelining.
Does it support Windows?
No. It targets Unix-like systems (Linux, macOS/XNU, the BSDs). Windows
networking differs enough (Winsock, IOCP, no epoll/kqueue) that supporting it
would be a separate port, not a build flag.
Why getaddrinfo instead of a custom resolver?
A custom resolver means shipping a stub resolver, /etc/hosts handling, DNS wire
format, caching and TCP fallback for truncated answers. getaddrinfo already
does all of that, is on every target, and respects the system's resolver
configuration. The cost is that it blocks, which the API states instead of
hiding.
Is name resolution non-blocking?
nx_resolve and nx_resolve_blocking block. nx_resolve_async runs the lookup
on a worker thread and delivers the result on the reactor thread through the
reactor's wakeup pipe. An in-flight async lookup cannot be cancelled, because
getaddrinfo cannot be interrupted.
Can I override DNS for specific names?
Yes. Load a hosts file with nx_hosts_load_default() (or
nx_hosts_set_global()); nx_resolve then checks it before getaddrinfo.
Names found in the file are authoritative, and names that are not found fall
through to the system resolver. See docs.md.
What if the hosts file is missing?
It is optional. A missing /var/lib/libnx/hosts.txt (or hosts.list) leaves the
table empty and resolution proceeds normally. Only a malformed file is an error.
What does an alias entry do?
alias a.example b.example makes a.example resolve to the addresses of
b.example, which must itself be defined in the same hosts file. If the target
is missing, the lookup fails instead of falling back to DNS.
How do I do non-blocking I/O?
Create the socket, put it in non-blocking mode with nx_set_nonblock, and drive
it from the reactor. nx_connect_timeout handles an in-progress connect and
restores the socket's original blocking mode when it returns.
Why is the reactor level-triggered?
Because level-triggered behaviour is the same on epoll, kqueue and poll. With
edge triggering, a socket that is not drained to EAGAIN never reports again,
and getting that wrong is silent data corruption. Level triggering trades a few
extra wakeups for a correctness property that holds on every backend.
Can I modify events from inside a callback?
Yes. nx_reactor_add, nx_reactor_mod and nx_reactor_del are all safe to call
from a callback. A deletion that targets the event currently being dispatched is
deferred until the dispatch loop finishes, so the event is not freed from under
its own callback.
Why does nx_reactor_add take the fd, callback and userdata again?
nx_event_new allocates the token and nx_reactor_add registers it. The add
call takes the same fields so that an nx_event embedded in a larger struct can
be registered without a separate nx_event_new allocation.
Is libnx thread-safe?
The reactor is not: it must be driven from one thread. nx_reactor_wakeup is the
one function safe to call from another thread, and it is what async DNS uses.
Error strings are kept in a thread-local buffer, so they never take a lock.
nx_selfpipe_notify is async-signal-safe.
Why is there no logging?
A library that logs decides for the application. Every failure returns an error
code, nx_strerror / nx_http_error / nx_url_error / nx_gai_strerror
provide the message, and the application decides whether and where to print it.
Why does nx_http_perform return 0 for a 404?
Because a 404 is a completed HTTP exchange, not a transport failure. Return
-1 means the request could not be sent or the response could not be parsed.
Check res.status for the HTTP result.
How do I build and install it?
make
sudo make install
See docs.md for the details and the list of backends.
How do I run the tests?
make test
The test suite under tests/ is not written yet, so there is currently nothing
to run.
Can I use libnx from C++?
Yes. All public headers are wrapped in extern "C" and compile as C++.
What license is libnx under?
MIT. See LICENSE.