tcp.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. int peer_fin; /* peer FIN consumed (EOF) */
  18. };
  19. /* Open a connection (SYN -> SYN-ACK -> ACK). Returns:
  20. * 0 = established,
  21. * -1 = no reply (timeout),
  22. * -2 = refused (RST),
  23. * -3 = transmit failed / no NIC / no IP. */
  24. int tcp_connect(const unsigned char *dst_mac, const unsigned char *dst_ip,
  25. unsigned int dport, struct tcp_conn *c);
  26. /* Send one segment (len must be <= peer_mss), retransmitting until acked.
  27. * Returns 0, or -1 timeout / -2 reset / -3 transmit failed. */
  28. int tcp_send(struct tcp_conn *c, const void *data, unsigned int len);
  29. /* Read the next in-order payload bytes into out. Returns >0 = byte count,
  30. * 0 = peer closed (EOF), -1 = deadline passed, -2 = reset. */
  31. int tcp_recv(struct tcp_conn *c, unsigned char *out, unsigned int maxlen,
  32. unsigned int deadline_ms);
  33. /* Send FIN and drain the close handshake briefly (best effort). */
  34. void tcp_close(struct tcp_conn *c);
  35. #endif