You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

udp.h 4.0KB

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