tcp.h 4.0 KB

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