1 /*
2  *  Copyright (c) 2021, 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 TCP API.
32  */
33 
34 #include "openthread-core-config.h"
35 
36 #if OPENTHREAD_CONFIG_TCP_ENABLE
37 
38 #include <openthread/tcp.h>
39 
40 #include "common/instance.hpp"
41 #include "net/tcp6.hpp"
42 
43 using namespace ot;
44 
otTcpEndpointInitialize(otInstance * aInstance,otTcpEndpoint * aEndpoint,otTcpEndpointInitializeArgs * aArgs)45 otError otTcpEndpointInitialize(otInstance *aInstance, otTcpEndpoint *aEndpoint, otTcpEndpointInitializeArgs *aArgs)
46 {
47     Ip6::Tcp::Endpoint &endpoint = *static_cast<Ip6::Tcp::Endpoint *>(aEndpoint);
48 
49     return endpoint.Initialize(*static_cast<Instance *>(aInstance), *aArgs);
50 }
51 
otTcpEndpointGetInstance(otTcpEndpoint * aEndpoint)52 otInstance *otTcpEndpointGetInstance(otTcpEndpoint *aEndpoint)
53 {
54     Ip6::Tcp::Endpoint &endpoint = *static_cast<Ip6::Tcp::Endpoint *>(aEndpoint);
55 
56     return &endpoint.GetInstance();
57 }
58 
otTcpEndpointGetContext(otTcpEndpoint * aEndpoint)59 void *otTcpEndpointGetContext(otTcpEndpoint *aEndpoint)
60 {
61     Ip6::Tcp::Endpoint &endpoint = *static_cast<Ip6::Tcp::Endpoint *>(aEndpoint);
62 
63     return endpoint.GetContext();
64 }
65 
otTcpGetLocalAddress(const otTcpEndpoint * aEndpoint)66 const otSockAddr *otTcpGetLocalAddress(const otTcpEndpoint *aEndpoint)
67 {
68     const Ip6::Tcp::Endpoint &endpoint = *static_cast<const Ip6::Tcp::Endpoint *>(aEndpoint);
69 
70     return &endpoint.GetLocalAddress();
71 }
72 
otTcpGetPeerAddress(const otTcpEndpoint * aEndpoint)73 const otSockAddr *otTcpGetPeerAddress(const otTcpEndpoint *aEndpoint)
74 {
75     const Ip6::Tcp::Endpoint &endpoint = *static_cast<const Ip6::Tcp::Endpoint *>(aEndpoint);
76 
77     return &endpoint.GetPeerAddress();
78 }
79 
otTcpBind(otTcpEndpoint * aEndpoint,const otSockAddr * aSockName)80 otError otTcpBind(otTcpEndpoint *aEndpoint, const otSockAddr *aSockName)
81 {
82     Ip6::Tcp::Endpoint &endpoint = *static_cast<Ip6::Tcp::Endpoint *>(aEndpoint);
83 
84     return endpoint.Bind(*static_cast<const Ip6::SockAddr *>(aSockName));
85 }
86 
otTcpConnect(otTcpEndpoint * aEndpoint,const otSockAddr * aSockName,uint32_t aFlags)87 otError otTcpConnect(otTcpEndpoint *aEndpoint, const otSockAddr *aSockName, uint32_t aFlags)
88 {
89     Ip6::Tcp::Endpoint &endpoint = *static_cast<Ip6::Tcp::Endpoint *>(aEndpoint);
90 
91     return endpoint.Connect(*static_cast<const Ip6::SockAddr *>(aSockName), aFlags);
92 }
93 
otTcpSendByReference(otTcpEndpoint * aEndpoint,otLinkedBuffer * aBuffer,uint32_t aFlags)94 otError otTcpSendByReference(otTcpEndpoint *aEndpoint, otLinkedBuffer *aBuffer, uint32_t aFlags)
95 {
96     Ip6::Tcp::Endpoint &endpoint = *static_cast<Ip6::Tcp::Endpoint *>(aEndpoint);
97 
98     return endpoint.SendByReference(*aBuffer, aFlags);
99 }
100 
otTcpSendByExtension(otTcpEndpoint * aEndpoint,size_t aNumBytes,uint32_t aFlags)101 otError otTcpSendByExtension(otTcpEndpoint *aEndpoint, size_t aNumBytes, uint32_t aFlags)
102 {
103     Ip6::Tcp::Endpoint &endpoint = *static_cast<Ip6::Tcp::Endpoint *>(aEndpoint);
104 
105     return endpoint.SendByExtension(aNumBytes, aFlags);
106 }
107 
otTcpReceiveByReference(const otTcpEndpoint * aEndpoint,const otLinkedBuffer ** aBuffer)108 otError otTcpReceiveByReference(const otTcpEndpoint *aEndpoint, const otLinkedBuffer **aBuffer)
109 {
110     const Ip6::Tcp::Endpoint &endpoint = *static_cast<const Ip6::Tcp::Endpoint *>(aEndpoint);
111 
112     return endpoint.ReceiveByReference(*aBuffer);
113 }
114 
otTcpReceiveContiguify(otTcpEndpoint * aEndpoint)115 otError otTcpReceiveContiguify(otTcpEndpoint *aEndpoint)
116 {
117     Ip6::Tcp::Endpoint &endpoint = *static_cast<Ip6::Tcp::Endpoint *>(aEndpoint);
118 
119     return endpoint.ReceiveContiguify();
120 }
121 
otTcpCommitReceive(otTcpEndpoint * aEndpoint,size_t aNumBytes,uint32_t aFlags)122 otError otTcpCommitReceive(otTcpEndpoint *aEndpoint, size_t aNumBytes, uint32_t aFlags)
123 {
124     Ip6::Tcp::Endpoint &endpoint = *static_cast<Ip6::Tcp::Endpoint *>(aEndpoint);
125 
126     return endpoint.CommitReceive(aNumBytes, aFlags);
127 }
128 
otTcpSendEndOfStream(otTcpEndpoint * aEndpoint)129 otError otTcpSendEndOfStream(otTcpEndpoint *aEndpoint)
130 {
131     Ip6::Tcp::Endpoint &endpoint = *static_cast<Ip6::Tcp::Endpoint *>(aEndpoint);
132 
133     return endpoint.SendEndOfStream();
134 }
135 
otTcpAbort(otTcpEndpoint * aEndpoint)136 otError otTcpAbort(otTcpEndpoint *aEndpoint)
137 {
138     Ip6::Tcp::Endpoint &endpoint = *static_cast<Ip6::Tcp::Endpoint *>(aEndpoint);
139 
140     return endpoint.Abort();
141 }
142 
otTcpEndpointDeinitialize(otTcpEndpoint * aEndpoint)143 otError otTcpEndpointDeinitialize(otTcpEndpoint *aEndpoint)
144 {
145     Ip6::Tcp::Endpoint &endpoint = *static_cast<Ip6::Tcp::Endpoint *>(aEndpoint);
146 
147     return endpoint.Deinitialize();
148 }
149 
otTcpListenerInitialize(otInstance * aInstance,otTcpListener * aListener,otTcpListenerInitializeArgs * aArgs)150 otError otTcpListenerInitialize(otInstance *aInstance, otTcpListener *aListener, otTcpListenerInitializeArgs *aArgs)
151 {
152     Ip6::Tcp::Listener &listener = *static_cast<Ip6::Tcp::Listener *>(aListener);
153 
154     return listener.Initialize(*static_cast<Instance *>(aInstance), *aArgs);
155 }
156 
otTcpListenerGetInstance(otTcpListener * aListener)157 otInstance *otTcpListenerGetInstance(otTcpListener *aListener)
158 {
159     Ip6::Tcp::Listener &listener = *static_cast<Ip6::Tcp::Listener *>(aListener);
160 
161     return &listener.GetInstance();
162 }
163 
otTcpListenerGetContext(otTcpListener * aListener)164 void *otTcpListenerGetContext(otTcpListener *aListener)
165 {
166     Ip6::Tcp::Listener &listener = *static_cast<Ip6::Tcp::Listener *>(aListener);
167 
168     return listener.GetContext();
169 }
170 
otTcpListen(otTcpListener * aListener,const otSockAddr * aSockName)171 otError otTcpListen(otTcpListener *aListener, const otSockAddr *aSockName)
172 {
173     Ip6::Tcp::Listener &listener = *static_cast<Ip6::Tcp::Listener *>(aListener);
174 
175     return listener.Listen(*static_cast<const Ip6::SockAddr *>(aSockName));
176 }
177 
otTcpStopListening(otTcpListener * aListener)178 otError otTcpStopListening(otTcpListener *aListener)
179 {
180     Ip6::Tcp::Listener &listener = *static_cast<Ip6::Tcp::Listener *>(aListener);
181 
182     return listener.StopListening();
183 }
184 
otTcpListenerDeinitialize(otTcpListener * aListener)185 otError otTcpListenerDeinitialize(otTcpListener *aListener)
186 {
187     Ip6::Tcp::Listener &listener = *static_cast<Ip6::Tcp::Listener *>(aListener);
188 
189     return listener.Deinitialize();
190 }
191 
192 #endif // OPENTHREAD_CONFIG_TCP_ENABLE
193