您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #ifndef _GPXE_UDP_H
  2. #define _GPXE_UDP_H
  3. /** @file
  4. *
  5. * UDP protocol
  6. *
  7. * This file defines the gPXE UDP API.
  8. *
  9. */
  10. #include <stddef.h>
  11. #include <gpxe/pkbuff.h>
  12. #include <gpxe/tcpip.h>
  13. #include <gpxe/if_ether.h>
  14. /**
  15. * UDP constants
  16. */
  17. #define UDP_MAX_HLEN 72
  18. #define UDP_MAX_TXPKB ETH_MAX_MTU
  19. #define UDP_MIN_TXPKB ETH_ZLEN
  20. typedef uint16_t port_t;
  21. /**
  22. * A UDP header
  23. */
  24. struct udp_header {
  25. port_t source_port;
  26. port_t dest_port;
  27. uint16_t len;
  28. uint16_t chksum;
  29. };
  30. struct udp_connection;
  31. /**
  32. * UDP operations
  33. *
  34. */
  35. struct udp_operations {
  36. /**
  37. * Transmit data
  38. *
  39. * @v conn UDP connection
  40. * @v buf Temporary data buffer
  41. * @v len Length of temporary data buffer
  42. * @ret rc Return status code
  43. *
  44. * The application may use the temporary data buffer to
  45. * construct the data to be sent. Note that merely filling
  46. * the buffer will do nothing; the application must call
  47. * udp_send() in order to actually transmit the data. Use of
  48. * the buffer is not compulsory; the application may call
  49. * udp_send() on any block of data.
  50. */
  51. int ( * senddata ) ( struct udp_connection *conn, void *buf,
  52. size_t len );
  53. /**
  54. * New data received
  55. *
  56. * @v conn UDP connection
  57. * @v data Data
  58. * @v len Length of data
  59. * @v st_src Source address
  60. * @v st_dest Destination address
  61. * @ret rc Return status code
  62. */
  63. int ( * newdata ) ( struct udp_connection *conn, void *data,
  64. size_t len, struct sockaddr_tcpip *st_src,
  65. struct sockaddr_tcpip *st_dest );
  66. };
  67. /**
  68. * A UDP connection
  69. *
  70. */
  71. struct udp_connection {
  72. /** Address of the remote end of the connection */
  73. struct sockaddr_tcpip peer;
  74. /** Local port on which the connection receives packets */
  75. port_t local_port;
  76. /** Transmit buffer */
  77. struct pk_buff *tx_pkb;
  78. /** List of registered connections */
  79. struct list_head list;
  80. /** Operations table for this connection */
  81. struct udp_operations *udp_op;
  82. };
  83. /*
  84. * Functions provided to the application layer
  85. */
  86. /**
  87. * Bind UDP connection to all local ports
  88. *
  89. * @v conn UDP connection
  90. *
  91. * A promiscuous UDP connection will receive packets with any
  92. * destination UDP port. This is required in order to support the PXE
  93. * UDP API.
  94. *
  95. * If the promiscuous connection is not the only UDP connection, the
  96. * behaviour is undefined.
  97. */
  98. static inline void udp_bind_promisc ( struct udp_connection *conn ) {
  99. conn->local_port = 0;
  100. }
  101. /**
  102. * Connect UDP connection to remote host and port
  103. *
  104. * @v conn UDP connection
  105. * @v peer Destination socket address
  106. *
  107. * This function sets the default address for transmitted packets,
  108. * i.e. the address used when udp_send() is called rather than
  109. * udp_sendto().
  110. */
  111. static inline void udp_connect ( struct udp_connection *conn,
  112. struct sockaddr_tcpip *peer ) {
  113. memcpy ( &conn->peer, peer, sizeof ( conn->peer ) );
  114. }
  115. /**
  116. * Connect UDP connection to remote port
  117. *
  118. * @v conn UDP connection
  119. * @v port Destination port
  120. *
  121. * This function sets only the port part of the default address for
  122. * transmitted packets.
  123. */
  124. static inline void udp_connect_port ( struct udp_connection *conn,
  125. uint16_t port ) {
  126. conn->peer.st_port = port;
  127. }
  128. /**
  129. * Get default address for transmitted packets
  130. *
  131. * @v conn UDP connection
  132. * @ret peer Default destination socket address
  133. */
  134. static inline struct sockaddr_tcpip *
  135. udp_peer ( struct udp_connection *conn ) {
  136. return &conn->peer;
  137. }
  138. extern int udp_bind ( struct udp_connection *conn, uint16_t local_port );
  139. extern int udp_open ( struct udp_connection *conn, uint16_t local_port );
  140. extern void udp_close ( struct udp_connection *conn );
  141. extern int udp_senddata ( struct udp_connection *conn );
  142. extern int udp_send ( struct udp_connection *conn,
  143. const void *data, size_t len );
  144. extern int udp_sendto ( struct udp_connection *conn,
  145. struct sockaddr_tcpip *peer,
  146. const void *data, size_t len );
  147. #endif /* _GPXE_UDP_H */