summaryrefslogtreecommitdiffhomepage
path: root/lib/socket.c
AgeCommit message (Collapse)Author
2025-03-06socket: add missing documentation for socket.listen() reuseaddrFelix Fietkau
Signed-off-by: Felix Fietkau <nbd@nbd.name>
2025-03-06socket: add reuseaddr argument for socket.listen()Felix Fietkau
This socket option needs to be set before the bind call(). Since it's very commonly used, adding a parameter for it makes sense. Signed-off-by: Felix Fietkau <nbd@nbd.name>
2025-02-04socket: properly support SO_ATTACH_FILTER sockoptJo-Philipp Wich
The SO_ATTACH_FILTER socket option requires special handling as it is not passing a self-contained structure to the kernel but a pointer to user memory holding the actual BPF bytecode. In order to properly support this, first rework the ucode value to C struct conversion callback machinery to pass an indirect struct base pointer, allowing conversion callbacks to realloc the struct memory as needed. Finally introduce custom uv to C conversion for the BPF data which accepts either a raw bytecode string, an array of opcode arrays or a flat array of opcode values which are converted into a C array of struct sock_filter records appended to the reallocated struct memory. Attaching a BPF program equivalent to the tcpdump expression `vlan 20 && ether proto 0x1234` would then look like this: sock.setopt(SOL_SOCKET, SO_ATTACH_FILTER, { filter: [ [ 0x28, 0, 0, 0x0000000c ], [ 0x15, 2, 0, 0x00008100 ], [ 0x15, 1, 0, 0x000088a8 ], [ 0x15, 0, 6, 0x00009100 ], [ 0x28, 0, 0, 0x0000000e ], [ 0x54, 0, 0, 0x00000fff ], [ 0x15, 0, 3, 0x00000014 ], [ 0x28, 0, 0, 0x00000010 ], [ 0x15, 0, 1, 0x00001234 ], [ 0x6, 0, 0, 0x00040000 ], [ 0x6, 0, 0, 0x00000000 ], ] }); Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2025-01-25socket: fix AF_PACKET recvmsg() and sockaddr formattingJo-Philipp Wich
- Do not unconditionally pass the `MSG_CMSG_CLOEXEC` flag to `recvmsg()` invocations as not all protocol specific recvmsg implementations in the kernel tolerate it; `packet_recvmsg()` for example will immediately return yield `EINVAL` if any non-whitelisted flag is passed. - Ensure that the HW address string buffer is zero-terminated when converting MAC addresses from C to ucode values. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2024-10-17socket: provide local definition of `struct fanout_args`Jo-Philipp Wich
The `fanout_args` structure is a relatively recent addition to the kernel so provide a local definition for it, like we already do it for other like `timeval_old`. Fixes: #217 Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2024-05-21socket: implement recvmsg(), sendmsg() and cmsg supportJo-Philipp Wich
Implement socket.recvmsg(), socket.sendmsg() and support for encoding and decoding well known control message types. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2024-05-17socket: add IPv6 socket optionsJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2024-05-17socket: handle further socket option value typesJo-Philipp Wich
- Handle varying integer sizes for socket option values - Support interfaces name and index option values Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2024-05-17socket: improve uc_socket_listen() behaviorJo-Philipp Wich
- Treat address string values containing slashes as AF_UNIX addresses - Default to SOCK_DGRAM for non AF_INET, AF_INET6 sockets - Gracefully handle EOPNOTSUPP condition after listen() call Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2024-05-17socket: improve uc_socket_connect() behaviorJo-Philipp Wich
Treat address strings containing slashes as AF_UNIX paths and do not attempt to resolve them. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2024-05-17socket: support IPv6 addresses in struct conversion routinesJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2024-05-17socket: uv_to_sockaddr(): fix length calculation for AF_UNIX addressesJo-Philipp Wich
Do not attempt to calculate a dynamic length for AF_UNIX socket addresses but return the total size of `struct sockaddr_un`, like we do it for the structures of other address families as well. Fixes incorrect domain socket path truncation. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2024-05-17socket: remove wrong documentation fragmentJo-Philipp Wich
Remove an accidentially copy-pasted jsdoc comment block. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2024-05-14socket: optimize poll() argument handlingJo-Philipp Wich
Optimize the poll() function implementation to re-use passed in socket/flag tuple arrays as-is in the return value array, which simplifies attaching state to sockets for user code through subsequent fields in the tuple array. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2024-05-13socket: fix potential memory leak in connect()Jo-Philipp Wich
If a non-string value which cannot be interpreted as socket address structure is passed to connect(), the function will leak the internal address vector when returning the error. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2024-05-09socket: fix addrinfo() with omitted service argumentJo-Philipp Wich
Actually pass NULL to getaddrinfo() when the service argument is omitted, instead of incorrectly translating it to a string containing "null". Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2024-05-08socket: remove leftover debug codeJo-Philipp Wich
Fixes: 0662de6 ("socket: add AF_PACKET socket type support") Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2024-05-08socket: add AF_PACKET socket type supportJo-Philipp Wich
Add the required infrastructure to support Linux AF_PACKET sockets: - Add related constants - Add struct definitions for SOL_PACKET socket options - Add AF_PACKET family support to sockaddr routines Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2024-04-25socket: make socket.send() accept non-string dataJo-Philipp Wich
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2024-04-24socket: rework error handlingJo-Philipp Wich
- fix `error()` function documentation - change error message format order to `msg: strerror` - change `xxx() failed` messages to just `xxx()` Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2024-04-24lib: introduce socket libraryJo-Philipp Wich
Introduce a new socket module which provides bindings for the BSD sockets API to ucode scripts. Example usage: import * as socket from 'socket'; let sk = socket.create(socket.AF_INET, socket.SOCK_STREAM); sk.connect("192.168.1.1", 80); sk.send("GET / HTTP/1.0\r\n\r\n"); print(sk.recv(4096)); sk.close(); Signed-off-by: Jo-Philipp Wich <jo@mein.io>