1
0

PeerHeader.h 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #ifndef PeerHeader_H
  16. #define PeerHeader_H
  17. #include "util/Assert.h"
  18. #include "util/Endian.h"
  19. /**
  20. * 1 2 3
  21. * 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
  22. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  23. * 0 | ver | sessNum | congest | Timestamp |
  24. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  25. *
  26. * The PeerHeader is sent between direct peers in order to detect congestion.
  27. * It also provides for peers to wish to intentionally maintain multiple simultanious sessions,
  28. * for example a wifi connection and an eth connection.
  29. *
  30. * @ver the version number of the PeerHeader, set to PeerHeader_CURRENT_VERSION
  31. * @sessNum if one node connects to another one multiple times using different interfaces, this
  32. * number should be incremented for each connection between the two nodes.
  33. * @congest an explicit value indicating congestion in opposite direction of the link.
  34. * @param timestamp a (big endian) number containing which is set using the millisecond clock on
  35. * the sending node. The receiving node can use this to detect probable skew
  36. * and then notify via the congest field if packets begin arriving late.
  37. */
  38. struct PeerHeader
  39. {
  40. /** Version is set to PeerHeader_CURRENT_VERSION */
  41. uint8_t versionAndSessNum;
  42. /**
  43. * A number used to indicate that the link has become congested and traffic flow should be
  44. * decreased. In practice this may be the number of milliseconds of latency (clock - timestamp)
  45. * minus the smallest latency ever detected on the link. It should go without saying that if
  46. * the value is negative, it should be set to zero and if it is above 255, it should be 255.
  47. */
  48. uint8_t congest;
  49. /** The time when the packet left the last internal buffer in the sending node. */
  50. uint16_t timestamp_be;
  51. };
  52. #define PeerHeader_SIZE 4
  53. Assert_compileTime(sizeof(struct PeerHeader) == PeerHeader_SIZE);
  54. #define PeerHeader_CURRENT_VERSION 0
  55. static inline uint16_t PeerHeader_getTimestamp(struct PeerHeader* hdr)
  56. {
  57. return Endian_bigEndianToHost16(hdr->timestamp_be);
  58. }
  59. static inline void PeerHeader_setTimestamp(struct PeerHeader* hdr, uint16_t timestamp)
  60. {
  61. hdr->timestamp_be = Endian_hostToBigEndian16(timestamp);
  62. }
  63. static inline void PeerHeader_setVersion(struct PeerHeader* hdr, uint8_t version)
  64. {
  65. Assert_true(version < (1<<5));
  66. hdr->versionAndSessNum = (hdr->versionAndSessNum & ((1<<5)-1)) | (version << 5);
  67. }
  68. static inline uint8_t PeerHeader_getVersion(struct PeerHeader* hdr)
  69. {
  70. return hdr->versionAndSessNum >> 5;
  71. }
  72. static inline uint8_t PeerHeader_getSessionNum(struct PeerHeader* hdr)
  73. {
  74. return hdr->versionAndSessNum & ((1<<5)-1);
  75. }
  76. static inline uint8_t PeerHeader_setSessionNum(struct PeerHeader* hdr, uint8_t sessNum)
  77. {
  78. Assert_true(sessNum <= 1<<5);
  79. hdr->versionAndSessNum = ( hdr->versionAndSessNum & ((1<<5)-1) ) | sessNum;
  80. }
  81. #endif