Message.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * Message.h
8  *
9  ****/
10 
11 #pragma once
12 
13 #include "Question.h"
14 #include "Answer.h"
15 
16 namespace mDNS
17 {
18 constexpr uint32_t MDNS_IP{0xFB0000E0}; // 224.0.0.251
19 constexpr uint16_t MDNS_TARGET_PORT{5353};
20 constexpr uint16_t MDNS_SOURCE_PORT{5353};
21 constexpr uint16_t MDNS_TTL{255};
22 
23 constexpr uint16_t MAX_PACKET_SIZE{1024};
24 
28 class Message
29 {
30 public:
31  enum class Type {
32  query,
33  reply,
34  };
35 
37  : remoteIp(remoteIp), remotePort(remotePort), data(static_cast<uint8_t*>(data)), size(size)
38  {
39  }
40 
41  Message(const Message& other)
42  : Message(other.getRemoteIp(), other.getRemotePort(), other.getData(), other.getSize())
43  {
44  }
45 
52  bool parse();
53 
58  {
59  return remoteIp;
60  }
61 
66  {
67  return remotePort;
68  }
69 
73  bool isReply() const
74  {
75  return data[2] & 0x80;
76  }
77 
78  Type getType() const
79  {
80  return isReply() ? Type::reply : Type::query;
81  }
82 
86  bool isTruncated() const
87  {
88  return data[2] & 0x02;
89  }
90 
94  uint8_t getResponseCode() const
95  {
96  return data[3] & 0x0f;
97  }
98 
99  uint8_t* getData() const
100  {
101  return data;
102  }
103 
105  {
106  return size;
107  }
108 
110 
111  // Writing
112  void allocate(uint16_t recordSize)
113  {
114  size += recordSize;
115  }
116 
117  /*
118  * Resolve a 16-bit 'pointer' to a memory location
119  *
120  * Value represents a 16-bit offset from the start of the message data.
121  * DNS names may contain pointers, but we use this approach internally to improve
122  * data portability, reduce memory consumption and avoid data duplication during
123  * message parsing and construction.
124  */
125  uint8_t* resolvePointer(uint16_t pointer) const
126  {
127  return data + pointer;
128  }
129 
132 
133 protected:
136  uint8_t* data;
138 };
139 
140 } // namespace mDNS
A class to make it easier to handle and pass around IP addresses.
Definition: IpAddress.h:43
Answer * operator[](ResourceType type)
Question::OwnedList questions
Definition: Message.h:130
uint8_t getResponseCode() const
Non-zero indicates error.
Definition: Message.h:94
uint16_t getSize() const
Definition: Message.h:104
IpAddress getRemoteIp() const
Address of sender from UDP packet.
Definition: Message.h:57
uint8_t * resolvePointer(uint16_t pointer) const
Definition: Message.h:125
Message(IpAddress remoteIp, uint16_t remotePort, void *data, uint16_t size)
Definition: Message.h:36
A single mDNS Answer.
Definition: Answer.h:25
uint16_t remotePort
Definition: Message.h:135
Definition: Answer.h:17
constexpr uint16_t MDNS_SOURCE_PORT
Definition: Message.h:20
bool parse()
Parse message data.
void allocate(uint16_t recordSize)
Definition: Message.h:112
Type
Definition: Message.h:31
bool isTruncated() const
If set, indicates record is split across multiple packets.
Definition: Message.h:86
Type getType() const
Definition: Message.h:78
uint16_t getRemotePort() const
UDP port in message.
Definition: Message.h:65
Answer::OwnedList answers
Definition: Message.h:131
uint8_t * data
Definition: Message.h:136
IpAddress remoteIp
Definition: Message.h:134
constexpr uint16_t MDNS_TARGET_PORT
Definition: Message.h:19
Encapsulates a message packet for flexible introspection.
Definition: Message.h:28
Message(const Message &other)
Definition: Message.h:41
constexpr uint32_t MDNS_IP
Definition: Message.h:18
uint8_t * getData() const
Definition: Message.h:99
uint16_t size
Definition: Message.h:137
constexpr uint16_t MAX_PACKET_SIZE
Definition: Message.h:23
constexpr uint16_t MDNS_TTL
Definition: Message.h:21
Type
Definition: Resource.h:41
bool isReply() const
Check that message contains answers, not queries.
Definition: Message.h:73