Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

tcp.h 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #ifndef _GPXE_TCP_H
  2. #define _GPXE_TCP_H
  3. /** @file
  4. *
  5. * TCP protocol
  6. *
  7. * This file defines the gPXE TCP API.
  8. *
  9. */
  10. #include <stddef.h>
  11. #include <gpxe/in.h>
  12. #include <gpxe/list.h>
  13. #include <gpxe/pkbuff.h>
  14. struct tcp_connection;
  15. /**
  16. * TCP operations
  17. *
  18. */
  19. struct tcp_operations {
  20. /*
  21. * Connection closed
  22. *
  23. * @v conn TCP connection
  24. * @v status Error code, if any
  25. *
  26. * This is called when the connection is closed for any
  27. * reason, including timeouts or aborts. The status code
  28. * contains the negative error number, if the closure is due
  29. * to an error.
  30. *
  31. * Note that acked() and newdata() may be called after
  32. * closed(), if the packet containing the FIN also
  33. * acknowledged data or contained new data. Note also that
  34. * connected() may not have been called before closed(), if
  35. * the close is due to an error.
  36. */
  37. void ( * closed ) ( struct tcp_connection *conn, int status );
  38. /**
  39. * Connection established (SYNACK received)
  40. *
  41. * @v conn TCP connection
  42. */
  43. void ( * connected ) ( struct tcp_connection *conn );
  44. /**
  45. * Data acknowledged
  46. *
  47. * @v conn TCP connection
  48. * @v len Length of acknowledged data
  49. *
  50. * @c len is guaranteed to not exceed the outstanding amount
  51. * of unacknowledged data.
  52. */
  53. void ( * acked ) ( struct tcp_connection *conn, size_t len );
  54. /**
  55. * New data received
  56. *
  57. * @v conn TCP connection
  58. * @v data Data
  59. * @v len Length of data
  60. */
  61. void ( * newdata ) ( struct tcp_connection *conn,
  62. void *data, size_t len );
  63. /**
  64. * Transmit data
  65. *
  66. * @v conn TCP connection
  67. * @v buf Temporary data buffer
  68. * @v len Length of temporary data buffer
  69. *
  70. * The application should transmit whatever it currently wants
  71. * to send using tcp_send(). If retransmissions are required,
  72. * senddata() will be called again and the application must
  73. * regenerate the data. The easiest way to implement this is
  74. * to ensure that senddata() never changes the application's
  75. * state.
  76. *
  77. * The application may use the temporary data buffer to
  78. * construct the data to be sent. Note that merely filling
  79. * the buffer will do nothing; the application must call
  80. * tcp_send() in order to actually transmit the data. Use of
  81. * the buffer is not compulsory; the application may call
  82. * tcp_send() on any block of data.
  83. */
  84. void ( * senddata ) ( struct tcp_connection *conn, void *buf,
  85. size_t len );
  86. };
  87. #if USE_UIP
  88. /**
  89. * A TCP connection
  90. *
  91. */
  92. struct tcp_connection {
  93. /** Address of the remote end of the connection */
  94. struct sockaddr_in sin;
  95. /** Operations table for this connection */
  96. struct tcp_operations *tcp_op;
  97. };
  98. extern void tcp_connect ( struct tcp_connection *conn );
  99. extern void tcp_send ( struct tcp_connection *conn, const void *data,
  100. size_t len );
  101. extern void tcp_kick ( struct tcp_connection *conn );
  102. extern void tcp_close ( struct tcp_connection *conn );
  103. #else
  104. #define TCP_NOMSG ""
  105. #define TCP_NOMSG_LEN 0
  106. /* Smallest port number on which a TCP connection can listen */
  107. #define TCP_MIN_PORT 1
  108. /* Some PKB constants */
  109. #define MAX_HDR_LEN 100
  110. #define MAX_PKB_LEN 1500
  111. #define MIN_PKB_LEN MAX_HDR_LEN + 100 /* To account for padding by LL */
  112. /**
  113. * TCP states
  114. */
  115. #define TCP_CLOSED 0
  116. #define TCP_LISTEN 1
  117. #define TCP_SYN_SENT 2
  118. #define TCP_SYN_RCVD 3
  119. #define TCP_ESTABLISHED 4
  120. #define TCP_FIN_WAIT_1 5
  121. #define TCP_FIN_WAIT_2 6
  122. #define TCP_CLOSING 7
  123. #define TCP_TIME_WAIT 8
  124. #define TCP_CLOSE_WAIT 9
  125. #define TCP_LAST_ACK 10
  126. #define TCP_INVALID 11
  127. /**
  128. * A TCP connection
  129. */
  130. struct tcp_connection {
  131. struct sockaddr sa; /* Remote socket address */
  132. struct sockaddr_in sin; /* Internet socket address */
  133. uint16_t local_port; /* Local port, in network byte order */
  134. int tcp_state; /* TCP state */
  135. int tcp_lstate; /* Last TCP state */
  136. uint32_t snd_una; /* Lowest unacked byte on snd stream */
  137. uint32_t snd_win; /* Offered by remote end */
  138. uint32_t rcv_nxt; /* Next expected byte on rcv stream */
  139. uint32_t rcv_win; /* Advertised to receiver */
  140. uint8_t tcp_flags; /* TCP header flags */
  141. struct list_head list; /* List of TCP connections */
  142. struct pk_buff *tx_pkb; /* Transmit packet buffer */
  143. struct tcp_operations *tcp_op; /* Operations table for connection */
  144. };
  145. /**
  146. * Connection closed status codes
  147. */
  148. #define CONN_SNDCLOSE 0
  149. #define CONN_RESTART 1
  150. #define CONN_TIMEOUT 2
  151. #define CONN_RCVCLOSE 3
  152. /**
  153. * A TCP header
  154. */
  155. struct tcp_header {
  156. uint16_t src; /* Source port */
  157. uint16_t dest; /* Destination port */
  158. uint32_t seq; /* Sequence number */
  159. uint32_t ack; /* Acknowledgement number */
  160. uint8_t hlen; /* Header length (4), Reserved (4) */
  161. uint8_t flags; /* Reserved (2), Flags (6) */
  162. uint16_t win; /* Advertised window */
  163. uint16_t csum; /* Checksum */
  164. uint16_t urg; /* Urgent pointer */
  165. };
  166. /**
  167. * TCP masks
  168. */
  169. #define TCP_MASK_HLEN 0xf0
  170. #define TCP_MASK_FLAGS 0x3f
  171. /**
  172. * TCP flags
  173. */
  174. #define TCP_RST 0x20
  175. #define TCP_ACK 0x10
  176. #define TCP_PSH 0x08
  177. #define TCP_URG 0x04
  178. #define TCP_SYN 0x02
  179. #define TCP_FIN 0x01
  180. extern struct tcpip_protocol tcp_protocol;
  181. extern void tcp_init_conn ( struct tcp_connection *conn );
  182. extern int tcp_connect ( struct tcp_connection *conn );
  183. extern int tcp_connectto ( struct tcp_connection *conn, struct sockaddr *peer );
  184. extern int tcp_listen ( struct tcp_connection *conn, uint16_t port );
  185. extern int tcp_senddata ( struct tcp_connection *conn );
  186. extern int tcp_close ( struct tcp_connection *conn );
  187. extern int tcp_send ( struct tcp_connection *conn, const void *data,
  188. size_t len );
  189. #endif /* USE_UIP */
  190. #endif /* _GPXE_TCP_H */