| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- /* Minimal TCP (RFC 793 subset) for outgoing client connections and the
- * single WebDAV listener.
- *
- * Client side: a small table of per-process connections. Each connection has
- * its own in-order receive queue (64 KB preferred, 32/16 KB when the frame
- * allocator is fragmented); every stack entry point drains the NIC through
- * net_pump(), so several processes can download at the same time. There is no
- * out-of-order reassembly, window scaling or congestion control.
- *
- * Server side: one listening port, one active connection and a small SYN
- * backlog (see the WebDAV command); the caller owns the raw NIC lease.
- */
- #ifndef TCP_H
- #define TCP_H
- #define TCP_STATE_CLOSED 0
- #define TCP_STATE_SYN_SENT 1
- #define TCP_STATE_ESTABLISHED 2
- #define TCP_STATE_FIN_WAIT 3
- struct tcp_conn {
- unsigned char dst_mac[6];
- unsigned char dst_ip[4];
- unsigned int sport, dport;
- unsigned int snd_una; /* oldest unacknowledged sequence */
- unsigned int snd_nxt; /* next sequence number to send */
- unsigned int rcv_nxt; /* next sequence number expected from peer */
- unsigned int peer_mss; /* from the SYN-ACK option; 536 when absent */
- unsigned int peer_wnd; /* peer's advertised receive window (raw) */
- int peer_fin; /* peer FIN consumed (EOF) */
- int need_wnd; /* re-send the window update after the final ACK */
- int state; /* TCP_STATE_* */
- unsigned char *rxq; /* receive queue (power-of-two size) */
- unsigned int rxq_mask;
- unsigned int rxq_head, rxq_tail;
- };
- /* Initialise a connection's receive queue. `size` must be a power of two. */
- void tcp_conn_init(struct tcp_conn *c, unsigned char *buf, unsigned int size);
- /* ---- Client table (one connection per process) ----
- *
- * `pid` is the calling process. Returns:
- * connect: 0 established, -1 timeout, -2 refused/reset, -3 no NIC/tx,
- * -4 busy (raw NIC owner) or no free slot / already connected.
- * send: 0 / -1 timeout / -2 reset / -3 transmit or bad length /
- * -4 no socket.
- * recv: >0 bytes, 0 EOF, -1 timeout, -2 reset/-4 no socket.
- */
- int tcp_client_connect(unsigned int pid, const unsigned char mac[6],
- const unsigned char ip[4], unsigned int port);
- int tcp_client_send(unsigned int pid, const void *buf, unsigned int len);
- int tcp_client_recv(unsigned int pid, void *buf, unsigned int max,
- unsigned int deadline_ms);
- void tcp_client_close(unsigned int pid);
- int tcp_client_peer_mss(unsigned int pid);
- /* Feed one frame from net_pump() to the client table. Returns 1 when it
- * matched a client connection (consumed), 0 otherwise. */
- int tcp_client_input(const unsigned char *frame, unsigned int len);
- /* Reaper: close every socket owned by `pid` without waiting. */
- void tcp_client_reap(unsigned int pid);
- int tcp_client_count(void);
- /* ---- Server side (WebDAV listener) ----
- *
- * One listening port, one active connection and a small SYN backlog. The
- * caller owns the NIC. Incoming frames are fed to tcp_srv_input() (which
- * also answers SYNs and RSTs unknown ports); tcp_srv_accept() promotes a
- * backlog connection for send/recv with the normal tcp_srv_send/recv path. */
- void tcp_srv_init(unsigned int port);
- void tcp_srv_reset(void);
- /* Handle one frame. Returns 1 when it was a TCP frame for us (consumed),
- * 0 when it is not TCP / not addressed to us (the caller may answer ARP). */
- int tcp_srv_input(const unsigned char *frame, unsigned int len);
- /* Promote the oldest pending connection (NULL when none is ready). */
- struct tcp_conn *tcp_srv_accept(void);
- /* Send len bytes, split to the peer's MSS. 0 / -1 / -2 / -3. */
- int tcp_srv_send(struct tcp_conn *c, const void *data, unsigned int len);
- /* Read the next in-order payload bytes from a connection's receive queue
- * (used by the server side). Returns >0 = byte count, 0 = peer closed (EOF),
- * -1 = deadline passed, -2 = reset. */
- int tcp_recv(struct tcp_conn *c, unsigned char *out, unsigned int maxlen,
- unsigned int deadline_ms);
- /* FIN once, then remember the tuple briefly so a late FIN/ACK is absorbed
- * instead of answered with RST. */
- void tcp_srv_close(struct tcp_conn *c);
- #endif
|