SOCKET_FASTOPEN_CONNECT4(3)

πŸ“¦
version 0.34 Β· 2 versions
  • 0.34 Β· alpine 3.24, debian bookworm, debian trixie, nixos 26.05, nixpkgs unstable, opensuse-leap 16.0, ubuntu noble, ubuntu questing, ubuntu resolute, ubuntu stonking (latest)
  • 0.30 Β· fedora 43, fedora 44, ubuntu jammy
, in alpine 3.24, debian bookworm, debian trixie, nixos 26.05, nixpkgs unstable, opensuse-leap 16.0, ubuntu noble, ubuntu questing, ubuntu resolute, ubuntu stonking

πŸ’– socket_fastopen_connect4(3) πŸ’–

okay bestie, so this is a page from the Library Functions Manual β€” it's all about socket_fastopen_connect4, and honestly? she's iconic. sit down, grab a strawberry, we're doing sockets ✨


🌸 NAME 🌸

socket_fastopen_connect4 β€” make a TCP connection and send some data 🫢


✨ SYNTAX ✨

#include <libowfat/socket.h>

ssize_t socket_fastopen_connect4(int s,const char ip[4],uint16 port,
                                 const char* buf,size_t len);

no cap, that's the whole signature. s is your socket, ip is 4 bytes of IP, port is the port, and buf/len are the data you're dying to send πŸ’…


πŸ’œ DESCRIPTION πŸ’œ

so here's the tea: socket_fastopen_connect4 attempts to make a connection from TCP socket s to TCP port port on IP address ip. and if that succeeds? it attempts to send len bytes from buf. two birds, one function, main character energy fr fr πŸ”₯

the difference between this and just calling socket_connect4 followed by write is... okay this is the good part. on platforms that support TCP Fast Open, socket_fastopen_connect4 will send the first data packet in the third packet of the TCP handshake, saving one useless ACK packet in network traffic. like, we're not doing extra trips, we're not doing extra handshakes, we're just sending. efficient queen πŸ‘‘

this is only useful for protocols where the client sends the first bytes. iykyk 🌈

socket_connect4 may return:

  • >=0, to indicate that the connection succeeded and this many bytes were sent. slay, that's your byte count πŸŽ€
  • -1, setting errno to error_inprogress or error_wouldblock, to indicate that the socket is non-blocking. it's giving "not yet, babes" 😭
  • -1, setting errno to something else, to indicate that the connection failed (and failed immediately, if the socket is non-blocking). yeah that's a πŸ’€ moment

when a background connection succeeds or fails, s becomes writable; you can use socket_connected to see whether the connection succeeded. and if the connection failed, socket_connected returns 0, setting errno appropriately. so like, vibe check your socket before you assume anything bestie πŸ¦‹

once a TCP socket is connected, you can use the read and write system calls to transmit data. periodt. πŸ’–

you can call socket_connect4 without calling socket_bind4. this has the effect as first calling socket_bind4 with IP address 0.0.0.0 and port 0. so like, no bind? fine, it just defaults, we move πŸ’«

ngl, keep in mind this whole Fast Open magic only kicks in on platforms that support it β€” the doc says "on platforms supporting TCP Fast Open", so don't come for me if your kernel isn't in on the plan πŸ™ˆ

πŸ“ EXAMPLE πŸ“

#include <libowfat/socket.h>

int s;
char ip[4];
uint16 p;

s = socket_tcp4();
socket_bind4(s,ip,p);
socket_fastopen_connect4(s,ip,p,"hello",5);

look at her go. socket, bind, connect-and-send, done. "hello" and 5 bytes, she said what she said πŸ’…


🌈 SEE ALSO 🌈


and that's the whole page babes πŸ’– this one lives in the Library Functions Manual as socket_fastopen_connect4(3), and yeah β€” same name up top and down at the bottom, because symmetry is cute and so is she. go forth, open those connections fast, and remember: the client sends the first bytes or we simply do not participate πŸ‘‘βœ¨