forked from esp8266/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUdpContext.h
270 lines (220 loc) · 6.2 KB
/
UdpContext.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
UdpContext.h - emulation of UDP connection handling on top of lwIP
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
This file is part of the esp8266 core for Arduino environment.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef UDPCONTEXT_H
#define UDPCONTEXT_H
#include <functional>
class UdpContext;
#define GET_IP_HDR(pb) reinterpret_cast<ip_hdr*>(((uint8_t*)((pb)->payload)) - UDP_HLEN - IP_HLEN);
#define GET_UDP_HDR(pb) reinterpret_cast<udp_hdr*>(((uint8_t*)((pb)->payload)) - UDP_HLEN);
extern netif netif0;
class UdpContext
{
public:
typedef std::function<void(void)> rxhandler_t;
UdpContext(): _on_rx(nullptr), _refcnt(0)
{
_sock = mockUDPSocket();
}
~UdpContext()
{
}
void ref()
{
++_refcnt;
}
void unref()
{
if(--_refcnt == 0) {
delete this;
}
}
bool connect (const ip_addr_t* addr, uint16_t port)
{
_dst = *addr;
_dstport = port;
return true;
}
bool listen(const ip_addr_t* addr, uint16_t port)
{
bool ret = mockUDPListen(_sock, addr->addr, port, staticMCastAddr);
register_udp(_sock, this);
return ret;
}
void disconnect()
{
if (_sock >= 0)
{
close(_sock);
register_udp(_sock, nullptr);
}
_sock = -1;
}
void setMulticastInterface(const ip_addr_t& addr)
{
(void)addr;
// user multicast, and this is how it works with posix: send to multicast address:
_dst.addr = staticMCastAddr;
}
void setMulticastInterface(netif* p_pNetIf)
{
(void)p_pNetIf;
// user multicast, and this is how it works with posix: send to multicast address:
_dst.addr = staticMCastAddr;
}
void setMulticastTTL(int ttl)
{
(void)ttl;
//mockverbose("TODO: UdpContext::setMulticastTTL\n");
}
netif* getInputNetif() const
{
return &netif0;
}
// warning: handler is called from tcp stack context
// esp_yield and non-reentrant functions which depend on it will fail
void onRx(rxhandler_t handler) {
_on_rx = handler;
}
size_t getSize()
{
return _inbufsize;
}
size_t tell() const
{
return 0;
}
void seek(const size_t pos)
{
if (!isValidOffset(pos))
{
mockverbose("UDPContext::seek too far (%zd >= %zd)\n", pos, _inbufsize);
exit(EXIT_FAILURE);
}
mockUDPSwallow(pos, _inbuf, _inbufsize);
}
bool isValidOffset(const size_t pos) const {
return pos <= _inbufsize;
}
uint32_t getRemoteAddress()
{
return _dst.addr;
}
uint16_t getRemotePort()
{
return _dstport;
}
uint32_t getDestAddress()
{
mockverbose("TODO: implement UDP getDestAddress\n");
return 0; //ip_hdr* iphdr = GET_IP_HDR(_rx_buf);
}
uint16_t getLocalPort()
{
mockverbose("TODO: implement UDP getLocalPort\n");
return 0; //
}
bool next()
{
_inbufsize = 0;
mockUDPFillInBuf(_sock, _inbuf, _inbufsize, addrsize, addr, _dstport);
if (_inbufsize > 0)
{
translate_addr();
return true;
}
return false;
}
int read()
{
char c;
return read(&c, 1)? c: -1;
}
size_t read(char* dst, size_t size)
{
return mockUDPRead(_sock, dst, size, _timeout_ms, _inbuf, _inbufsize);
}
int peek()
{
char c;
return mockUDPPeekBytes(_sock, &c, 1, _timeout_ms, _inbuf, _inbufsize)?: -1;
}
void flush()
{
//mockverbose("UdpContext::flush() does not follow arduino's flush concept\n");
//exit(EXIT_FAILURE);
// would be:
_inbufsize = 0;
}
size_t append (const char* data, size_t size)
{
if (size + _outbufsize > sizeof _outbuf)
{
mockverbose("UdpContext::append: increase CCBUFSIZE (%d -> %zd)\n", CCBUFSIZE, (size + _outbufsize));
exit(EXIT_FAILURE);
}
memcpy(_outbuf + _outbufsize, data, size);
_outbufsize += size;
return size;
}
bool send (ip_addr_t* addr = 0, uint16_t port = 0)
{
uint32_t dst = addr? addr->addr: _dst.addr;
uint16_t dstport = port?: _dstport;
size_t ret = mockUDPWrite(_sock, (const uint8_t*)_outbuf, _outbufsize, _timeout_ms, dst, dstport);
_outbufsize = 0;
return ret > 0;
}
void mock_cb (void)
{
if (_on_rx) _on_rx();
}
public:
static uint32_t staticMCastAddr;
private:
void translate_addr ()
{
if (addrsize == 4)
{
uint32_t ipv4;
memcpy(&ipv4, addr, 4);
ip4_addr_set_u32(&ip_2_ip4(_dst), ipv4);
// ^ this is a workaround for "type-punned pointer" with "*(uint32*)addr"
//ip4_addr_set_u32(&ip_2_ip4(_dst), *(uint32_t*)addr);
}
else
mockverbose("TODO unhandled udp address of size %d\n", (int)addrsize);
}
int _sock = -1;
rxhandler_t _on_rx;
int _refcnt = 0;
ip_addr_t _dst;
uint16_t _dstport;
char _inbuf [CCBUFSIZE];
size_t _inbufsize = 0;
char _outbuf [CCBUFSIZE];
size_t _outbufsize = 0;
int _timeout_ms = 0;
uint8_t addrsize;
uint8_t addr[16];
};
extern "C" inline err_t igmp_joingroup (const ip4_addr_t *ifaddr, const ip4_addr_t *groupaddr)
{
(void)ifaddr;
UdpContext::staticMCastAddr = groupaddr->addr;
return ERR_OK;
}
#endif//UDPCONTEXT_H