tftp.h 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #ifndef _GPXE_TFTP_H
  2. #define _GPXE_TFTP_H
  3. /** @file
  4. *
  5. * TFTP protocol
  6. *
  7. */
  8. #include <stdint.h>
  9. #include <gpxe/udp.h>
  10. #include <gpxe/async.h>
  11. #include <gpxe/retry.h>
  12. #include <gpxe/buffer.h>
  13. #define TFTP_PORT 69 /**< Default TFTP server port */
  14. #define TFTP_DEFAULT_BLKSIZE 512 /**< Default TFTP data block size */
  15. #define TFTP_MAX_BLKSIZE 1432
  16. #define TFTP_RRQ 1 /**< Read request opcode */
  17. #define TFTP_WRQ 2 /**< Write request opcode */
  18. #define TFTP_DATA 3 /**< Data block opcode */
  19. #define TFTP_ACK 4 /**< Data block acknowledgement opcode */
  20. #define TFTP_ERROR 5 /**< Error opcode */
  21. #define TFTP_OACK 6 /**< Options acknowledgement opcode */
  22. #define TFTP_ERR_FILE_NOT_FOUND 1 /**< File not found */
  23. #define TFTP_ERR_ACCESS_DENIED 2 /**< Access violation */
  24. #define TFTP_ERR_DISK_FULL 3 /**< Disk full or allocation exceeded */
  25. #define TFTP_ERR_ILLEGAL_OP 4 /**< Illegal TFTP operation */
  26. #define TFTP_ERR_UNKNOWN_TID 5 /**< Unknown transfer ID */
  27. #define TFTP_ERR_FILE_EXISTS 6 /**< File already exists */
  28. #define TFTP_ERR_UNKNOWN_USER 7 /**< No such user */
  29. #define TFTP_ERR_BAD_OPTS 8 /**< Option negotiation failed */
  30. /** A TFTP read request (RRQ) packet */
  31. struct tftp_rrq {
  32. uint16_t opcode;
  33. char data[0];
  34. } __attribute__ (( packed ));
  35. /** A TFTP data (DATA) packet */
  36. struct tftp_data {
  37. uint16_t opcode;
  38. uint16_t block;
  39. uint8_t data[0];
  40. } __attribute__ (( packed ));
  41. /** A TFTP acknowledgement (ACK) packet */
  42. struct tftp_ack {
  43. uint16_t opcode;
  44. uint16_t block;
  45. } __attribute__ (( packed ));
  46. /** A TFTP error (ERROR) packet */
  47. struct tftp_error {
  48. uint16_t opcode;
  49. uint16_t errcode;
  50. char errmsg[0];
  51. } __attribute__ (( packed ));
  52. /** A TFTP options acknowledgement (OACK) packet */
  53. struct tftp_oack {
  54. uint16_t opcode;
  55. char data[0];
  56. } __attribute__ (( packed ));
  57. /** The common header of all TFTP packets */
  58. struct tftp_common {
  59. uint16_t opcode;
  60. } __attribute__ (( packed ));
  61. /** A union encapsulating all TFTP packet types */
  62. union tftp_any {
  63. struct tftp_common common;
  64. struct tftp_rrq rrq;
  65. struct tftp_data data;
  66. struct tftp_ack ack;
  67. struct tftp_error error;
  68. struct tftp_oack oack;
  69. };
  70. /**
  71. * A TFTP session
  72. *
  73. * This data structure holds the state for an ongoing TFTP transfer.
  74. */
  75. struct tftp_session {
  76. /** URI being fetched */
  77. struct uri *uri;
  78. /** Data buffer to fill */
  79. struct buffer *buffer;
  80. /** Asynchronous operation */
  81. struct async async;
  82. /** Requested data block size
  83. *
  84. * This is the "blksize" option requested from the TFTP
  85. * server. It may or may not be honoured.
  86. */
  87. unsigned int request_blksize;
  88. /** Data block size
  89. *
  90. * This is the "blksize" option negotiated with the TFTP
  91. * server. (If the TFTP server does not support TFTP options,
  92. * this will default to 512).
  93. */
  94. unsigned int blksize;
  95. /** File size
  96. *
  97. * This is the value returned in the "tsize" option from the
  98. * TFTP server. If the TFTP server does not support the
  99. * "tsize" option, this value will be zero.
  100. */
  101. unsigned long tsize;
  102. /**
  103. * Transfer ID
  104. *
  105. * This is the transfer ID allocated by the server, used as
  106. * the server UDP port for all packets except the initial read
  107. * request.
  108. */
  109. uint16_t tid;
  110. /** Session state
  111. *
  112. * This is the block number to be used in the next ACK sent
  113. * back to the server, i.e. the number of the last received
  114. * data block. The value zero indicates that the last
  115. * received block was an OACK (i.e. that the next ACK will
  116. * contain a block number of zero), and any value less than
  117. * zero indicates that the connection has not yet been opened
  118. * (i.e. that no blocks have yet been received).
  119. */
  120. int state;
  121. /** UDP connection */
  122. struct udp_connection udp;
  123. /** Retransmission timer */
  124. struct retry_timer timer;
  125. };
  126. /* Function prototypes */
  127. extern int tftp_get ( struct uri *uri, struct buffer *buffer,
  128. struct async *parent );
  129. #endif /* _GPXE_TFTP_H */