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 
29 /**
30  * @file
31  *   This file implements the OpenThread UDP API.
32  */
33 
34 #include "openthread-core-config.h"
35 
36 #include <openthread/udp.h>
37 
38 #include "common/instance.hpp"
39 #include "common/locator_getters.hpp"
40 #include "common/new.hpp"
41 #include "net/udp6.hpp"
42 
43 using namespace ot;
44 
otUdpNewMessage(otInstance * aInstance,const otMessageSettings * aSettings)45 otMessage *otUdpNewMessage(otInstance *aInstance, const otMessageSettings *aSettings)
46 {
47     Instance &instance = *static_cast<Instance *>(aInstance);
48 
49     return instance.Get<Ip6::Udp>().NewMessage(0, Message::Settings(aSettings));
50 }
51 
otUdpOpen(otInstance * aInstance,otUdpSocket * aSocket,otUdpReceive aCallback,void * aContext)52 otError otUdpOpen(otInstance *aInstance, otUdpSocket *aSocket, otUdpReceive aCallback, void *aContext)
53 {
54     Instance &instance = *static_cast<Instance *>(aInstance);
55 
56     return instance.Get<Ip6::Udp>().Open(*static_cast<Ip6::Udp::SocketHandle *>(aSocket), aCallback, aContext);
57 }
58 
otUdpIsOpen(otInstance * aInstance,const otUdpSocket * aSocket)59 bool otUdpIsOpen(otInstance *aInstance, const otUdpSocket *aSocket)
60 {
61     Instance &instance = *static_cast<Instance *>(aInstance);
62 
63     return instance.Get<Ip6::Udp>().IsOpen(*static_cast<const Ip6::Udp::SocketHandle *>(aSocket));
64 }
65 
otUdpClose(otInstance * aInstance,otUdpSocket * aSocket)66 otError otUdpClose(otInstance *aInstance, otUdpSocket *aSocket)
67 {
68     Instance &instance = *static_cast<Instance *>(aInstance);
69 
70     return instance.Get<Ip6::Udp>().Close(*static_cast<Ip6::Udp::SocketHandle *>(aSocket));
71 }
72 
otUdpBind(otInstance * aInstance,otUdpSocket * aSocket,const otSockAddr * aSockName,otNetifIdentifier aNetif)73 otError otUdpBind(otInstance *aInstance, otUdpSocket *aSocket, const otSockAddr *aSockName, otNetifIdentifier aNetif)
74 {
75     Instance &instance = *static_cast<Instance *>(aInstance);
76 
77     return instance.Get<Ip6::Udp>().Bind(*static_cast<Ip6::Udp::SocketHandle *>(aSocket),
78                                          *static_cast<const Ip6::SockAddr *>(aSockName), aNetif);
79 }
80 
otUdpConnect(otInstance * aInstance,otUdpSocket * aSocket,const otSockAddr * aSockName)81 otError otUdpConnect(otInstance *aInstance, otUdpSocket *aSocket, const otSockAddr *aSockName)
82 {
83     Instance &instance = *static_cast<Instance *>(aInstance);
84 
85     return instance.Get<Ip6::Udp>().Connect(*static_cast<Ip6::Udp::SocketHandle *>(aSocket),
86                                             *static_cast<const Ip6::SockAddr *>(aSockName));
87 }
88 
otUdpSend(otInstance * aInstance,otUdpSocket * aSocket,otMessage * aMessage,const otMessageInfo * aMessageInfo)89 otError otUdpSend(otInstance *aInstance, otUdpSocket *aSocket, otMessage *aMessage, const otMessageInfo *aMessageInfo)
90 {
91     Instance &instance = *static_cast<Instance *>(aInstance);
92 
93     return instance.Get<Ip6::Udp>().SendTo(*static_cast<Ip6::Udp::SocketHandle *>(aSocket),
94                                            *static_cast<Message *>(aMessage),
95                                            *static_cast<const Ip6::MessageInfo *>(aMessageInfo));
96 }
97 
otUdpGetSockets(otInstance * aInstance)98 otUdpSocket *otUdpGetSockets(otInstance *aInstance)
99 {
100     Instance &instance = *static_cast<Instance *>(aInstance);
101 
102     return instance.Get<Ip6::Udp>().GetUdpSockets();
103 }
104 
105 #if OPENTHREAD_CONFIG_UDP_FORWARD_ENABLE
otUdpForwardSetForwarder(otInstance * aInstance,otUdpForwarder aForwarder,void * aContext)106 void otUdpForwardSetForwarder(otInstance *aInstance, otUdpForwarder aForwarder, void *aContext)
107 {
108     Instance &instance = *static_cast<Instance *>(aInstance);
109 
110     instance.Get<Ip6::Udp>().SetUdpForwarder(aForwarder, aContext);
111 }
112 
otUdpForwardReceive(otInstance * aInstance,otMessage * aMessage,uint16_t aPeerPort,const otIp6Address * aPeerAddr,uint16_t aSockPort)113 void otUdpForwardReceive(otInstance *        aInstance,
114                          otMessage *         aMessage,
115                          uint16_t            aPeerPort,
116                          const otIp6Address *aPeerAddr,
117                          uint16_t            aSockPort)
118 {
119     Ip6::MessageInfo messageInfo;
120     Instance &       instance = *static_cast<Instance *>(aInstance);
121 
122     OT_ASSERT(aMessage != nullptr && aPeerAddr != nullptr);
123 
124     messageInfo.SetSockAddr(instance.Get<Mle::MleRouter>().GetMeshLocal16());
125     messageInfo.SetSockPort(aSockPort);
126     messageInfo.SetPeerAddr(*static_cast<const ot::Ip6::Address *>(aPeerAddr));
127     messageInfo.SetPeerPort(aPeerPort);
128     messageInfo.SetIsHostInterface(true);
129 
130     instance.Get<Ip6::Udp>().HandlePayload(*static_cast<ot::Message *>(aMessage), messageInfo);
131 
132     static_cast<ot::Message *>(aMessage)->Free();
133 }
134 #endif // OPENTHREAD_CONFIG_UDP_FORWARD_ENABLE
135 
otUdpAddReceiver(otInstance * aInstance,otUdpReceiver * aUdpReceiver)136 otError otUdpAddReceiver(otInstance *aInstance, otUdpReceiver *aUdpReceiver)
137 {
138     Instance &instance = *static_cast<Instance *>(aInstance);
139 
140     return instance.Get<Ip6::Udp>().AddReceiver(*static_cast<Ip6::Udp::Receiver *>(aUdpReceiver));
141 }
142 
otUdpRemoveReceiver(otInstance * aInstance,otUdpReceiver * aUdpReceiver)143 otError otUdpRemoveReceiver(otInstance *aInstance, otUdpReceiver *aUdpReceiver)
144 {
145     Instance &instance = *static_cast<Instance *>(aInstance);
146 
147     return instance.Get<Ip6::Udp>().RemoveReceiver(*static_cast<Ip6::Udp::Receiver *>(aUdpReceiver));
148 }
149 
otUdpSendDatagram(otInstance * aInstance,otMessage * aMessage,otMessageInfo * aMessageInfo)150 otError otUdpSendDatagram(otInstance *aInstance, otMessage *aMessage, otMessageInfo *aMessageInfo)
151 {
152     Instance &instance = *static_cast<Instance *>(aInstance);
153 
154     return instance.Get<Ip6::Udp>().SendDatagram(*static_cast<ot::Message *>(aMessage),
155                                                  *static_cast<Ip6::MessageInfo *>(aMessageInfo), Ip6::kProtoUdp);
156 }
157 
otUdpIsPortInUse(otInstance * aInstance,uint16_t port)158 bool otUdpIsPortInUse(otInstance *aInstance, uint16_t port)
159 {
160     Instance &instance = *static_cast<Instance *>(aInstance);
161 
162     return instance.Get<Ip6::Udp>().IsPortInUse(port);
163 }
164