0.01.00
udp6.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016, The OpenThread Authors.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * 1. Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * 3. Neither the name of the copyright holder nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
34 #ifndef UDP6_HPP_
35 #define UDP6_HPP_
36 
37 #include "openthread-core-config.h"
38 
39 #include <openthread/udp.h>
40 
41 #include "common/locator.hpp"
42 #include "net/ip6_headers.hpp"
43 
44 namespace ot {
45 namespace Ip6 {
46 
47 class Udp;
48 
63 class UdpSocket: public otUdpSocket
64 {
65  friend class Udp;
66 
67 public:
74  UdpSocket(Udp &aUdp);
75 
84  Message *NewMessage(uint16_t aReserved);
85 
96  otError Open(otUdpReceive aHandler, void *aContext);
97 
106  otError Bind(const SockAddr &aSockAddr);
107 
115  otError Connect(const SockAddr &aSockAddr);
116 
123  otError Close(void);
124 
136  otError SendTo(Message &aMessage, const MessageInfo &aMessageInfo);
137 
144  SockAddr &GetSockName(void) { return *static_cast<SockAddr *>(&mSockName); }
145 
152  SockAddr &GetPeerName(void) { return *static_cast<SockAddr *>(&mPeerName); }
153 
154 private:
155  UdpSocket *GetNext(void) { return static_cast<UdpSocket *>(mNext); }
156  void SetNext(UdpSocket *socket) { mNext = static_cast<otUdpSocket *>(socket); }
157 
158  void HandleUdpReceive(Message &aMessage, const MessageInfo &aMessageInfo) {
159  mHandler(mContext, &aMessage, &aMessageInfo);
160  }
161 };
162 
167 class Udp: public InstanceLocator
168 {
169  friend class UdpSocket;
170 
171 public:
178  Udp(otInstance &aInstance);
179 
188  otError AddSocket(UdpSocket &aSocket);
189 
198  otError RemoveSocket(UdpSocket &aSocket);
199 
206  uint16_t GetEphemeralPort(void);
207 
216  Message *NewMessage(uint16_t aReserved);
217 
229  otError SendDatagram(Message &aMessage, MessageInfo &aMessageInfo, IpProto aIpProto);
230 
241  otError HandleMessage(Message &aMessage, MessageInfo &aMessageInfo);
242 
253  otError UpdateChecksum(Message &aMessage, uint16_t aPseudoHeaderChecksum);
254 
255 private:
256  enum
257  {
258  kDynamicPortMin = 49152,
259  kDynamicPortMax = 65535,
260  };
261  uint16_t mEphemeralPort;
262  UdpSocket *mSockets;
263 };
264 
267 {
268  uint16_t mSource;
269  uint16_t mDestination;
270  uint16_t mLength;
271  uint16_t mChecksum;
273 
279 class UdpHeader: private UdpHeaderPoD
280 {
281 public:
288  uint16_t GetSourcePort(void) const { return HostSwap16(mSource); }
289 
296  void SetSourcePort(uint16_t aPort) { mSource = HostSwap16(aPort); }
297 
304  uint16_t GetDestinationPort(void) const { return HostSwap16(mDestination); }
305 
312  void SetDestinationPort(uint16_t aPort) { mDestination = HostSwap16(aPort); }
313 
320  uint16_t GetLength(void) const { return HostSwap16(mLength); }
321 
328  void SetLength(uint16_t aLength) { mLength = HostSwap16(aLength); }
329 
336  uint16_t GetChecksum(void) const { return HostSwap16(mChecksum); }
337 
344  void SetChecksum(uint16_t aChecksum) { mChecksum = HostSwap16(aChecksum); }
345 
352  static uint8_t GetLengthOffset(void) { return offsetof(UdpHeaderPoD, mLength); }
353 
360  static uint8_t GetChecksumOffset(void) { return offsetof(UdpHeaderPoD, mChecksum); }
361 
363 
369 } // namespace Ip6
370 } // namespace ot
371 
372 #endif // NET_UDP6_HPP_
void SetSourcePort(uint16_t aPort)
This method sets the UDP Source Port.
Definition: udp6.hpp:296
This type represents all the static / global variables used by OpenThread allocated in one place...
Definition: openthread-instance.h:59
struct otUdpSocket * mNext
A pointer to the next UDP socket (internal use only).
Definition: udp.h:72
Definition: cli.cpp:90
SockAddr & GetSockName(void)
This method returns the local socket address.
Definition: udp6.hpp:144
static uint8_t GetChecksumOffset(void)
This static method returns the byte offset for the UDP Checksum.
Definition: udp6.hpp:360
This file includes definitions for locator class for OpenThread objects.
otError Bind(const SockAddr &aSockAddr)
This method binds the UDP socket.
Definition: udp6.cpp:67
void SetDestinationPort(uint16_t aPort)
This method sets the UDP Destination Port.
Definition: udp6.hpp:312
otError Close(void)
This method closes the UDP socket.
Definition: udp6.cpp:85
This class implements message information for an IPv6 message.
Definition: socket.hpp:57
Definition: udp6.hpp:266
uint16_t GetLength(void) const
This method returns the UDP Length.
Definition: udp6.hpp:320
void SetChecksum(uint16_t aChecksum)
This method sets the UDP Checksum.
Definition: udp6.hpp:344
UdpSocket(Udp &aUdp)
This constructor initializes the object.
Definition: udp6.cpp:47
#define OT_TOOL_PACKED_BEGIN
Compiler-specific indication that a class or struct must be byte packed.
Definition: toolchain.h:170
uint16_t GetDestinationPort(void) const
This method returns the UDP Destination Port.
Definition: udp6.hpp:304
IpProto
Internet Protocol Numbers.
Definition: ip6_headers.hpp:93
otSockAddr mSockName
The local IPv6 socket address.
Definition: udp.h:67
otError SendTo(Message &aMessage, const MessageInfo &aMessageInfo)
This method sends a UDP message.
Definition: udp6.cpp:97
void SetLength(uint16_t aLength)
This method sets the UDP Length.
Definition: udp6.hpp:328
This class implements a socket address.
Definition: socket.hpp:191
SockAddr & GetPeerName(void)
This method returns the peer&#39;s socket address.
Definition: udp6.hpp:152
This class represents a message.
Definition: message.hpp:195
otError Open(otUdpReceive aHandler, void *aContext)
This method opens the UDP socket.
Definition: udp6.cpp:57
otUdpReceive mHandler
A function pointer to the application callback.
Definition: udp.h:69
This class implements locator for otInstance object.
Definition: locator.hpp:63
static uint8_t GetLengthOffset(void)
This static method returns the byte offset for the UDP Length.
Definition: udp6.hpp:352
This class implements core UDP message handling.
Definition: udp6.hpp:167
void(* otUdpReceive)(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo)
This callback allows OpenThread to inform the application of a received UDP message.
Definition: udp.h:59
#define OT_TOOL_PACKED_END
Compiler-specific indication at the end of a byte packed class or struct.
Definition: toolchain.h:172
uint16_t GetSourcePort(void) const
This method returns the UDP Source Port.
Definition: udp6.hpp:288
This class implements a UDP/IPv6 socket.
Definition: udp6.hpp:63
otError Connect(const SockAddr &aSockAddr)
This method connects the UDP socket.
Definition: udp6.cpp:79
Message * NewMessage(uint16_t aReserved)
This method returns a new UDP message with sufficient header space reserved.
Definition: udp6.cpp:52
This class implements UDP header generation and parsing.
Definition: udp6.hpp:279
otSockAddr mPeerName
The peer IPv6 socket address.
Definition: udp.h:68
This structure represents a UDP socket.
Definition: udp.h:65
This file includes definitions for IPv6 packet processing.
void * mContext
A pointer to application-specific context.
Definition: udp.h:70
otError
This enumeration represents error codes used throughout OpenThread.
Definition: types.h:107
This file includes compile-time configuration constants for OpenThread.
This file defines the OpenThread UDP API.
uint16_t GetChecksum(void) const
This method returns the UDP Checksum.
Definition: udp6.hpp:336