tcp.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /* Minimal TCP (RFC 793 subset) for outgoing client connections and the
  2. * single WebDAV listener.
  3. *
  4. * Client side: a small table of per-process connections. Each connection has
  5. * its own in-order receive queue (64 KB preferred, 32/16 KB when the frame
  6. * allocator is fragmented); every stack entry point drains the NIC through
  7. * net_pump(), so several processes can download at the same time. There is no
  8. * out-of-order reassembly, window scaling or congestion control.
  9. *
  10. * Server side: one listening port, one active connection and a small SYN
  11. * backlog (see the WebDAV command); the caller owns the raw NIC lease.
  12. */
  13. #ifndef TCP_H
  14. #define TCP_H
  15. #define TCP_STATE_CLOSED 0
  16. #define TCP_STATE_SYN_SENT 1
  17. #define TCP_STATE_ESTABLISHED 2
  18. #define TCP_STATE_FIN_WAIT 3
  19. struct tcp_conn {
  20. unsigned char dst_mac[6];
  21. unsigned char dst_ip[4];
  22. unsigned int sport, dport;
  23. unsigned int snd_una; /* oldest unacknowledged sequence */
  24. unsigned int snd_nxt; /* next sequence number to send */
  25. unsigned int rcv_nxt; /* next sequence number expected from peer */
  26. unsigned int peer_mss; /* from the SYN-ACK option; 536 when absent */
  27. unsigned int peer_wnd; /* peer's advertised receive window (raw) */
  28. int peer_fin; /* peer FIN consumed (EOF) */
  29. int need_wnd; /* re-send the window update after the final ACK */
  30. int state; /* TCP_STATE_* */
  31. unsigned char *rxq; /* receive queue (power-of-two size) */
  32. unsigned int rxq_mask;
  33. unsigned int rxq_head, rxq_tail;
  34. };
  35. /* Initialise a connection's receive queue. `size` must be a power of two. */
  36. void tcp_conn_init(struct tcp_conn *c, unsigned char *buf, unsigned int size);
  37. /* ---- Client table (one connection per process) ----
  38. *
  39. * `pid` is the calling process. Returns:
  40. * connect: 0 established, -1 timeout, -2 refused/reset, -3 no NIC/tx,
  41. * -4 busy (raw NIC owner) or no free slot / already connected.
  42. * send: 0 / -1 timeout / -2 reset / -3 transmit or bad length /
  43. * -4 no socket.
  44. * recv: >0 bytes, 0 EOF, -1 timeout, -2 reset/-4 no socket.
  45. */
  46. int tcp_client_connect(unsigned int pid, const unsigned char mac[6],
  47. const unsigned char ip[4], unsigned int port);
  48. int tcp_client_send(unsigned int pid, const void *buf, unsigned int len);
  49. int tcp_client_recv(unsigned int pid, void *buf, unsigned int max,
  50. unsigned int deadline_ms);
  51. void tcp_client_close(unsigned int pid);
  52. int tcp_client_peer_mss(unsigned int pid);
  53. /* Feed one frame from net_pump() to the client table. Returns 1 when it
  54. * matched a client connection (consumed), 0 otherwise. */
  55. int tcp_client_input(const unsigned char *frame, unsigned int len);
  56. /* Reaper: close every socket owned by `pid` without waiting. */
  57. void tcp_client_reap(unsigned int pid);
  58. int tcp_client_count(void);
  59. /* ---- Server side (WebDAV listener) ----
  60. *
  61. * One listening port, one active connection and a small SYN backlog. The
  62. * caller owns the NIC. Incoming frames are fed to tcp_srv_input() (which
  63. * also answers SYNs and RSTs unknown ports); tcp_srv_accept() promotes a
  64. * backlog connection for send/recv with the normal tcp_srv_send/recv path. */
  65. void tcp_srv_init(unsigned int port);
  66. void tcp_srv_reset(void);
  67. /* Handle one frame. Returns 1 when it was a TCP frame for us (consumed),
  68. * 0 when it is not TCP / not addressed to us (the caller may answer ARP). */
  69. int tcp_srv_input(const unsigned char *frame, unsigned int len);
  70. /* Promote the oldest pending connection (NULL when none is ready). */
  71. struct tcp_conn *tcp_srv_accept(void);
  72. /* Send len bytes, split to the peer's MSS. 0 / -1 / -2 / -3. */
  73. int tcp_srv_send(struct tcp_conn *c, const void *data, unsigned int len);
  74. /* Read the next in-order payload bytes from a connection's receive queue
  75. * (used by the server side). Returns >0 = byte count, 0 = peer closed (EOF),
  76. * -1 = deadline passed, -2 = reset. */
  77. int tcp_recv(struct tcp_conn *c, unsigned char *out, unsigned int maxlen,
  78. unsigned int deadline_ms);
  79. /* FIN once, then remember the tuple briefly so a late FIN/ACK is absorbed
  80. * instead of answered with RST. */
  81. void tcp_srv_close(struct tcp_conn *c);
  82. #endif