Commit graph

1588 commits

Author SHA1 Message Date
Kim Alvefur
c769eae82b net.server_epoll: Improve readability of DANE noise
Serialized Lua is not the most readable form of TLSA records.
2025-02-27 21:36:43 +01:00
Matthew Wild
1acb229597 net.httpserver: Remove obsolete API 2025-02-14 14:50:22 +00:00
Kim Alvefur
b4e8a8c6c4 net.adns: Collect DNS lookup timing metrics
Nice to have this in OpenMetricts instead of debug logs
2025-02-07 09:22:52 +01:00
Martijn van Duren
e7f77807c9 net.server_epoll: Call :shutdown() on TLS sockets when supported
Comment from Matthew:

This fixes a potential issue where the Prosody process gets blocked on sockets
waiting for them to close. Unlike non-TLS sockets, closing a TLS socket sends
layer 7 data, and this can cause problems for sockets which are in the process
of being cleaned up.

This depends on LuaSec changes which are not yet upstream.

From Martijn's original email:

So first my analysis of luasec. in ssl.c the socket is put into blocking
mode right before calling SSL_shutdown() inside meth_destroy(). My best
guess to why this is is because meth_destroy is linked to the __close
and __gc methods, which can't exactly be called multiple times and
luasec does want to make sure that a tls session is shutdown as clean
as possible.
I can't say I disagree with this reasoning and don't want to change this
behaviour. My solution to this without changing the current behaviour is
to introduce a shutdown() method. I am aware that this overlaps in a
conflicting way with tcp's shutdown method, but it stays close to the
OpenSSL name. This method calls SSL_shutdown() in the current
(non)blocking mode of the underlying socket and returns a boolean
whether or not the shutdown is completed (matching SSL_shutdown()'s 0
or 1 return values), and returns the familiar ssl_ioerror() strings on
error with a false for completion. This error can then be used to
determine if we have wantread/wantwrite to finalize things. Once
meth_shutdown() has been called once a shutdown flag will be set, which
indicates to meth_destroy() that the SSL_shutdown() has been handled
by the application and it shouldn't be needed to set the socket to
blocking mode. I've left the SSL_shutdown() call in the
LSEC_STATE_CONNECTED to prevent TOCTOU if the application reaches a
timeout for the shutdown code, which might allow SSL_shutdown() to
clean up anyway at the last possible moment.
Another thing I've changed to luasec is the call to socket_setblocking()
right before calling close(2) in socket_destroy() in usocket.c.
According to the latest POSIX[0]:
  Note that the requirement for close() on a socket to block for up to
  the current linger interval is not conditional on the O_NONBLOCK
  setting.
Which I read to mean that removing O_NONBLOCK on the socket before close
doesn't impact the behaviour and only causes noise in system call
tracers. I didn't touch the windows bits of this, since I don't do
windows.

For the prosody side of things I've made the TLS shutdown bits resemble
interface:onwritable(), and put it under a combined guard of self._tls
and self.conn.shutdown. The self._tls bit is there to prevent getting
stuck on this condition, and self.conn.shutdown is there to prevent the
code being called by instances where the patched luasec isn't deployed.
The destroy() method can be called from various places and is read by
me as the "we give up" error path. To accommodate for these unexpected
entrypoints I've added a single call to self.conn:shutdown() to prevent
the socket being put into blocking mode. I have no expectations that
there is any other use here. Same as previous, the self.conn.shutdown
check is there to make sure it's not called on unpatched luasec
deployments and self._tls is there to make sure we don't call shutdown()
on tcp sockets.
I wouldn't recommend logging of the conn:shutdown() error inside
close(), since a lot of clients simply close the connection before
SSL_shutdown() is done.
2025-02-06 15:04:38 +00:00
Kim Alvefur
61b0c0cd3a net.server_epoll: Don't try to flush buffer on closed connections
Attempt to fix a bug where connections are somehow closed twice, leading
to bad things happening elsewhere.

With LuaSec, closed connections are generally already too closed to
write anything to anyway since it does not support unidirectional
shutdown.
2024-11-19 00:41:02 +01:00
Kim Alvefur
ff00c6707f net.server_epoll: Revert opportunistic_writes tweak
Added in f0b2c026e542 and not sure why
2024-11-19 00:31:21 +01:00
Kim Alvefur
52178d7430 net.server_epoll: Make running out of buffer space a fatal error
Prevent Bad Things from happening when the buffer gets full.
This of course opens up the possibility of intentionally killing
connections by sending much stuff, which need to be mitigated with rate
limits elsewhere.
2024-11-09 15:42:31 +01:00
Kim Alvefur
693079c619 net.server_epoll: Improve efficiency of sending much buffered data
Problem: The string slice operations when a lot of data gets buffered
ends up being expensive and memory-consuming. We have util.dbuffer for
precisely this kind of thing.

I want to keep the behavior of writebuffer being upgraded from nil to a
string to full buffer since the last step involves three table
allocations, where the previous buffer method only used one. Avoiding
those allocations for simple writes like white space keep alive feels
like it would keep memory churn down.

This work was started in 2020
2024-11-09 00:37:15 +01:00
Kim Alvefur
73b512d3a6 util.error: Use is_error() instead of is_err() everywhere
Continuation of 4b39691a274e
2024-10-29 14:10:02 +01:00
Kim Alvefur
9327c4342a Merge 0.12->trunk 2024-08-16 17:06:41 +02:00
Kim Alvefur
4f7c829b1e net.http: Throw error if missing TLS context for HTTPS request
Prevents the mistake of creating a http context without any TLS context
and then trying to use HTTPS, which doesn't work right.

Thanks nils
2024-08-08 19:18:22 +02:00
Kim Alvefur
dd657746b4 util.sslconfig: Support DH parameters as literal string
Simplifies shipping well-known DH parameters in the config
2024-07-12 15:21:08 +02:00
Kim Alvefur
b522fd0e05 Merge 0.12->trunk 2024-07-07 18:40:25 +02:00
Kim Alvefur
ca417ab776 net.server_event: Add 'wrapserver' API
This enables accepting admin stream socket (UNIX) connections trough the
same procedures as any other (TCP) socket, which avoids problems caused
by using the wrapclient API, which ends up discarding early data due to
only expecting early connection failure.

Fixes #1867
2024-07-07 18:28:17 +02:00
Kim Alvefur
2647c56106 net.server_epoll: Add support for systemd socket activation
Allows creating listening sockets and accepting client connections
before Prosody starts.
This is unlike normal Prosody dynamic resource management, where ports
may added and removed at any time, and the ports defined by the config.

Weird things happen if these are closed (e.g. due to reload) so here we
prevent closing and ensure sockets are reused when opened again.
2024-05-14 17:07:47 +02:00
Kim Alvefur
f29bc1c119 net.unbound: Show canonical name in textual format (e.g. in shell)
libunbound does not tell us the whole chain of CNAMEs, only the final
canonical name.

This is to aid in debugging since it will only be shown in the shell.
2024-04-14 14:06:57 +02:00
Kim Alvefur
1d5021a663 net.server: Restore epoll signalfd handling
Reverts 4a9a69659727
2024-03-02 13:40:37 +01:00
Kim Alvefur
ed0188ce81 net.server: Disable epoll signalfd handling by default until problems resolved 2024-03-01 19:22:49 +01:00
Kim Alvefur
865df3d373 net.server_epoll: Log creation of signalfd handles at noise level
To aid in tracking down signalfd-related problems
2024-03-01 19:20:45 +01:00
Kim Alvefur
f1e07782ed net.server_epoll: Log failure to hook signals
To make any such failures noticeable
2024-02-28 22:24:09 +01:00
Kim Alvefur
3d2bad3d4b Merge 0.12->trunk 2024-02-27 17:15:36 +01:00
Kim Alvefur
f7b35d516f net.http.files: Validate argument to setup function
Fixes error in #1765 by throwing an error earlier
2024-02-27 17:14:16 +01:00
Kim Alvefur
761643abcc util.signal: Wrap signalfd in an userdatum for gc handling etc 2024-02-24 01:00:44 +01:00
Kim Alvefur
49c296360d net.server_epoll: Support hooking signals via signalfd
Handling signal events the same way as all other events makes sense and
seems safer than the signal handling just jumping around in C and
messing with Lua states.
2024-02-24 00:20:35 +01:00
Kim Alvefur
5764e73a65 net.server_epoll: Prevent traceback when checking TLS after connection gone
Unclear why this would be done, but an error is not great.
2024-01-21 22:59:50 +01:00
Kim Alvefur
674b91b82b net.http.parser: Reject overlarge header section earlier
This case would eventually be rejected by the buffer size limit.
2023-08-23 12:18:34 +02:00
Matthew Wild
87ee5a2300 net.http.parser: Fix off-by-one error in chunk parser 2023-02-17 17:01:19 +00:00
Matthew Wild
bc1e51eb83 net.http.server: Add new API to get HTTP request from a connection
This information is sometimes necessary in the context where we have a
connection that we know (or believe to be) associated with an incoming HTTP
request.

For example, it can be used to retrieve the IP address of a request (which may
differ from the IP address of the connection, due to X-Forwarded-For and co).

Thanks to the Jitsi team for highlighting this gap in the API.
2023-02-16 15:59:26 +00:00
Matthew Wild
0fd88a07a4 net.http.parser: Improve handling of responses without content-length
This ensures that we support responses without a content-length header, and
allow streaming them through the streaming handler interface. An example of
such a response would be Server-Sent Events streams.
2023-02-09 22:57:32 +00:00
Matthew Wild
28e3b19ca1 net.http: Add missing log parameter 2023-02-09 22:37:54 +00:00
Kim Alvefur
fe97c773a6 net.http.server: Fix #1789
Unregistering the response before sending the trailer of the chunked
transfer encoding prevents opportunistic writes from being invoked and
running this code again when, which may cause an error when closing the
file handle a second time.

Normally the file size is known, so no chuck headers are sent.
2023-01-08 13:35:04 +01:00
Kim Alvefur
4f3ba05b4d net.unbound: Merge luaunbound and prosody defaults in absence of user config (fixes #1763) (thanks rgd)
add_defaults() is supposed to merge 3 tables, the defaults in
luaunbound, the defaults from prosody and any config from the prosody
config file.  In the case where no `unbound={}` has been in the config,
it skips over the merge and returns only the prosody built-in defaults.

This results in libunbound skipping reading resolv.conf and uses its
default behavior of full recursive resolution.

Prior to #1737 there were only two tables, the luaunbound defaults and
the prosody config, where bypassing the merge and returning the former
did the right thing.
2022-06-19 19:49:32 +02:00
Kim Alvefur
24288146ae net.unbound: Adjust log level of error to error to error
This error is an error, therefore it should be at the error level
2022-05-16 14:51:01 +02:00
Kim Alvefur
5e95ed633a net.unbound: Disable use of hosts file by default (fixes #1737)
This mirrors the behaviour with net.dns and avoids the initialization
issue in #1737
2022-05-16 12:45:51 +02:00
Kim Alvefur
43351d2b54 Spelling: Fix various spelling mistakes (thanks timeless)
Words, sometimes I wonder how they even work

Maybe I missed something.
2022-03-07 00:13:56 +01:00
Kim Alvefur
ece896d253 net.websocket.frames: Replace bit fiddling code with util.struct
Fallback code for not having either the string.pack and string.unpack
functions available in Lua 5.4 or the struct lib is no longer needed
since the struct lib was imported as util.struct in 3ce3633527af
2022-03-06 15:13:50 +01:00
Matthew Wild
fa68985f24 net.stun: Support for xor-relayed-address attribute 2022-03-05 11:30:55 +00:00
Matthew Wild
d405c11918 net.stun: Add xor-peer-address helper 2022-03-05 11:04:25 +00:00
Matthew Wild
ac06bd8c97 net.stun: Factor out address unpack, an operation common to multiple attributes 2022-03-05 11:03:44 +00:00
Matthew Wild
6e9d71259c net.stun: Name some more attributes from TURN 2022-03-05 11:00:08 +00:00
Matthew Wild
ab77f7fcdb net.stun: Add lookup table generation helper, reduces code duplication 2022-03-05 10:59:19 +00:00
Matthew Wild
572bff0087 net.stun: Hyphenate method names for consistency 2022-03-05 10:57:47 +00:00
Kim Alvefur
7579bee6bb net.stun: Use util.bitcompat to deal with bit module variances across Lua versions 2022-03-04 16:58:28 +01:00
Kim Alvefur
1f257ecbb4 net.stun: Use util.strbitop
Improves performance since the whole procedure is done in C
2022-03-04 16:55:32 +01:00
Kim Alvefur
a26b39e1b7 net.http.server: Fix whitespace-ignoring syntax 2023-12-01 22:00:18 +01:00
Kim Alvefur
6187c44ca9 Merge 0.12->trunk 2023-12-01 17:11:19 +01:00
Matthew Wild
5cfe0d220d net.stun: New library that implements STUN/TURN parsing/serialization 2022-03-04 15:23:32 +00:00
Kim Alvefur
9fbf5bf7ad net.server_epoll: Disable verbose mode by default 2022-02-12 18:47:22 +01:00
Kim Alvefur
c41d63004a net.http.server: Complete async waiter for non-persistent connections
Otherwise requests with Connection: close would be stuck in the async
wait that starts after the handle_request() call.

Together with the new async debugging, this makes the async thread stay
in the set of waiting runners forever, where previously it would simply
be garbage collected.
2023-11-24 00:31:08 +01:00
Kim Alvefur
07d2f9f2e8 net.server_epoll: Avoid call to update socket watch flags when nothing changed
Should skip a syscall for each write when using epoll.
2023-11-21 22:06:28 +01:00