tcp.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /* Minimal TCP client (RFC 793 subset) for outgoing connections.
  2. *
  3. * Single connection, in-order receive only, no congestion control, no
  4. * window scaling, no out-of-order reassembly, no fragmentation. Everything
  5. * is polled from the netdev rx() call, matching the rest of the kernel.
  6. * The caller owns the NIC (netdev_acquire) from connect until close. */
  7. #ifndef TCP_H
  8. #define TCP_H
  9. struct tcp_conn {
  10. unsigned char dst_mac[6];
  11. unsigned char dst_ip[4];
  12. unsigned int sport, dport;
  13. unsigned int snd_una; /* oldest unacknowledged sequence */
  14. unsigned int snd_nxt; /* next sequence number to send */
  15. unsigned int rcv_nxt; /* next sequence number expected from peer */
  16. unsigned int peer_mss; /* from the SYN-ACK option; 536 when absent */
  17. unsigned int peer_wnd; /* peer's advertised receive window (raw) */
  18. int peer_fin; /* peer FIN consumed (EOF) */
  19. int need_wnd; /* re-send the window update after the final ACK */
  20. };
  21. /* Open a connection (SYN -> SYN-ACK -> ACK). Returns:
  22. * 0 = established,
  23. * -1 = no reply (timeout),
  24. * -2 = refused (RST),
  25. * -3 = transmit failed / no NIC / no IP. */
  26. int tcp_connect(const unsigned char *dst_mac, const unsigned char *dst_ip,
  27. unsigned int dport, struct tcp_conn *c);
  28. /* Send one segment (len must be <= peer_mss), retransmitting until acked.
  29. * Returns 0, or -1 timeout / -2 reset / -3 transmit failed. */
  30. int tcp_send(struct tcp_conn *c, const void *data, unsigned int len);
  31. /* Read the next in-order payload bytes into out. Returns >0 = byte count,
  32. * 0 = peer closed (EOF), -1 = deadline passed, -2 = reset. */
  33. int tcp_recv(struct tcp_conn *c, unsigned char *out, unsigned int maxlen,
  34. unsigned int deadline_ms);
  35. /* Send FIN and drain the close handshake briefly (best effort). */
  36. void tcp_close(struct tcp_conn *c);
  37. /* ---- Server side (WebDAV listener) ----
  38. *
  39. * One listening port, one active connection and a small SYN backlog. The
  40. * caller owns the NIC. Incoming frames are fed to tcp_srv_input() (which
  41. * also answers SYNs and RSTs unknown ports); tcp_srv_accept() promotes a
  42. * backlog connection for send/recv with the normal tcp_send()/tcp_recv(). */
  43. void tcp_srv_init(unsigned int port);
  44. void tcp_srv_reset(void);
  45. /* Handle one frame. Returns 1 when it was a TCP frame for us (consumed),
  46. * 0 when it is not TCP / not addressed to us (the caller may answer ARP). */
  47. int tcp_srv_input(const unsigned char *frame, unsigned int len);
  48. /* Promote the oldest pending connection (NULL when none is ready). */
  49. struct tcp_conn *tcp_srv_accept(void);
  50. /* Send len bytes, split to the peer's MSS. 0 / -1 / -2 / -3. */
  51. int tcp_srv_send(struct tcp_conn *c, const void *data, unsigned int len);
  52. /* FIN once, then remember the tuple briefly so a late FIN/ACK is absorbed
  53. * instead of answered with RST. */
  54. void tcp_srv_close(struct tcp_conn *c);
  55. #endif