Packet.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #pragma once
  2. #include <MeshCore.h>
  3. namespace mesh {
  4. // Packet::header values
  5. #define PH_ROUTE_MASK 0x03 // 2-bits
  6. #define PH_TYPE_SHIFT 2
  7. #define PH_TYPE_MASK 0x0F // 4-bits
  8. #define PH_VER_SHIFT 6
  9. #define PH_VER_MASK 0x03 // 2-bits
  10. #define ROUTE_TYPE_TRANSPORT_FLOOD 0x00 // flood mode + transport codes
  11. #define ROUTE_TYPE_FLOOD 0x01 // flood mode, needs 'path' to be built up (max 64 bytes)
  12. #define ROUTE_TYPE_DIRECT 0x02 // direct route, 'path' is supplied
  13. #define ROUTE_TYPE_TRANSPORT_DIRECT 0x03 // direct route + transport codes
  14. #define PAYLOAD_TYPE_REQ 0x00 // request (prefixed with dest/src hashes, MAC) (enc data: timestamp, blob)
  15. #define PAYLOAD_TYPE_RESPONSE 0x01 // response to REQ or ANON_REQ (prefixed with dest/src hashes, MAC) (enc data: timestamp, blob)
  16. #define PAYLOAD_TYPE_TXT_MSG 0x02 // a plain text message (prefixed with dest/src hashes, MAC) (enc data: timestamp, text)
  17. #define PAYLOAD_TYPE_ACK 0x03 // a simple ack
  18. #define PAYLOAD_TYPE_ADVERT 0x04 // a node advertising its Identity
  19. #define PAYLOAD_TYPE_GRP_TXT 0x05 // an (unverified) group text message (prefixed with channel hash, MAC) (enc data: timestamp, "name: msg")
  20. #define PAYLOAD_TYPE_GRP_DATA 0x06 // an (unverified) group datagram (prefixed with channel hash, MAC) (enc data: timestamp, blob)
  21. #define PAYLOAD_TYPE_ANON_REQ 0x07 // generic request (prefixed with dest_hash, ephemeral pub_key, MAC) (enc data: ...)
  22. #define PAYLOAD_TYPE_PATH 0x08 // returned path (prefixed with dest/src hashes, MAC) (enc data: path, extra)
  23. #define PAYLOAD_TYPE_TRACE 0x09 // trace a path, collecting SNI for each hop
  24. #define PAYLOAD_TYPE_MULTIPART 0x0A // packet is one of a set of packets
  25. #define PAYLOAD_TYPE_CONTROL 0x0B // a control/discovery packet
  26. //...
  27. #define PAYLOAD_TYPE_RAW_CUSTOM 0x0F // custom packet as raw bytes, for applications with custom encryption, payloads, etc
  28. #define PAYLOAD_VER_1 0x00 // 1-byte src/dest hashes, 2-byte MAC
  29. #define PAYLOAD_VER_2 0x01 // FUTURE (eg. 2-byte hashes, 4-byte MAC ??)
  30. #define PAYLOAD_VER_3 0x02 // FUTURE
  31. #define PAYLOAD_VER_4 0x03 // FUTURE
  32. /**
  33. * \brief The fundamental transmission unit.
  34. */
  35. class Packet {
  36. public:
  37. Packet();
  38. uint8_t header;
  39. uint16_t payload_len, path_len;
  40. uint16_t transport_codes[2];
  41. uint8_t path[MAX_PATH_SIZE];
  42. uint8_t payload[MAX_PACKET_PAYLOAD];
  43. int8_t _snr;
  44. /**
  45. * \brief calculate the hash of payload + type
  46. * \param dest_hash destination to store the hash (must be MAX_HASH_SIZE bytes)
  47. */
  48. void calculatePacketHash(uint8_t* dest_hash) const;
  49. /**
  50. * \returns one of ROUTE_ values
  51. */
  52. uint8_t getRouteType() const { return header & PH_ROUTE_MASK; }
  53. bool isRouteFlood() const { return getRouteType() == ROUTE_TYPE_FLOOD || getRouteType() == ROUTE_TYPE_TRANSPORT_FLOOD; }
  54. bool isRouteDirect() const { return getRouteType() == ROUTE_TYPE_DIRECT || getRouteType() == ROUTE_TYPE_TRANSPORT_DIRECT; }
  55. bool hasTransportCodes() const { return getRouteType() == ROUTE_TYPE_TRANSPORT_FLOOD || getRouteType() == ROUTE_TYPE_TRANSPORT_DIRECT; }
  56. /**
  57. * \returns one of PAYLOAD_TYPE_ values
  58. */
  59. uint8_t getPayloadType() const { return (header >> PH_TYPE_SHIFT) & PH_TYPE_MASK; }
  60. /**
  61. * \returns one of PAYLOAD_VER_ values
  62. */
  63. uint8_t getPayloadVer() const { return (header >> PH_VER_SHIFT) & PH_VER_MASK; }
  64. uint8_t getPathHashSize() const { return (path_len >> 6) + 1; }
  65. uint8_t getPathHashCount() const { return path_len & 63; }
  66. uint8_t getPathByteLen() const { return getPathHashCount() * getPathHashSize(); }
  67. void setPathHashCount(uint8_t n) { path_len &= ~63; path_len |= n; }
  68. void setPathHashSizeAndCount(uint8_t sz, uint8_t n) { path_len = ((sz - 1) << 6) | (n & 63); }
  69. static uint8_t copyPath(uint8_t* dest, const uint8_t* src, uint8_t path_len); // returns path_len
  70. static size_t writePath(uint8_t* dest, const uint8_t* src, uint8_t path_len); // returns byte length written
  71. static bool isValidPathLen(uint8_t path_len);
  72. void markDoNotRetransmit() { header = 0xFF; }
  73. bool isMarkedDoNotRetransmit() const { return header == 0xFF; }
  74. float getSNR() const { return ((float)_snr) / 4.0f; }
  75. /**
  76. * \returns the encoded/wire format length of this packet
  77. */
  78. int getRawLength() const;
  79. /**
  80. * \brief save entire packet as a blob
  81. * \param dest (OUT) destination buffer (assumed to be MAX_MTU_SIZE)
  82. * \returns the packet length
  83. */
  84. uint8_t writeTo(uint8_t dest[]) const;
  85. /**
  86. * \brief restore this packet from a blob (as created using writeTo())
  87. * \param src (IN) buffer containing blob
  88. * \param len the packet length (as returned by writeTo())
  89. */
  90. bool readFrom(const uint8_t src[], uint8_t len);
  91. };
  92. }