-
Notifications
You must be signed in to change notification settings - Fork 28
/
lwip-interface.c
672 lines (539 loc) · 16.3 KB
/
lwip-interface.c
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
#include "lwip-interface.h"
#include "lwip/tcp_impl.h"
#include "attribute.h"
#include "util.h"
#include "stats.h"
#include "sdk.h"
#include <lwip/udp.h>
#include <lwip/tcp.h>
#include <lwip/igmp.h>
#include <stdint.h>
#include <stdbool.h>
enum
{
lwip_udp_max_payload = 4800,
};
enum
{
lwip_error_strings_size = 17
};
static const char * const lwip_error_strings[lwip_error_strings_size] roflash =
{
"ok", // 0 ERR_OK
"out of memory", // -1 ERR_MEM
"buffer error", // -2 ERR_BUF
"timeout", // -3 ERR_TIMEOUT
"routing error", // -4 ERR_RTE
"in progress", // -5 ERR_INPROGRESS
"illegal value", // -6 ERR_VAL
"operation would block", // -7 ERR_WOULDBLOCK
"address in use", // -8 ERR_USE
"already connected", // -9 ERR_ISCONN
"connection aborted", // -10 ERR_ABRT
"connection reset", // -11 ERR_RST
"connection closed", // -12 ERR_CLSD
"not connected", // -13 ERR_CONN
"illegal argument", // -14 ERR_ARG
"low level netif error", // -15 ERR_IF
"unknown",
};
static void _log_error(const char *name, const char *flash_message, err_t error)
{
string_new(, message, 128);
int ix = error;
if(ix < 0)
ix = 0 - ix;
if(ix >= lwip_error_strings_size)
ix = lwip_error_strings_size - 1;
string_append_cstr(&message, name);
string_append(&message, ": ");
string_append_cstr_flash(&message, flash_message);
string_append(&message, ", lwip_error: ");
string_append_cstr_flash(&message, lwip_error_strings[ix]);
string_append(&message, "\n");
log_from_flash_0(string_to_cstr(&message));
}
#define log_error(n, s, e) \
do { \
static roflash const char log_str_flash[] = s; \
_log_error(n, log_str_flash, e); \
} while(0)
bool attr_nonnull attr_pure lwip_if_received_tcp(lwip_if_socket_t *socket)
{
return(socket->peer.port == 0);
}
bool attr_nonnull attr_pure lwip_if_received_udp(lwip_if_socket_t *socket)
{
return(socket->peer.port != 0);
}
attr_nonnull void lwip_if_receive_buffer_lock(lwip_if_socket_t *socket, lwip_if_proto_t proto)
{
if(proto & lwip_if_proto_tcp)
socket->receive_buffer_locked.tcp = 1;
if(proto & lwip_if_proto_udp)
socket->receive_buffer_locked.udp = 1;
}
attr_nonnull void lwip_if_receive_buffer_unlock(lwip_if_socket_t *socket, lwip_if_proto_t proto)
{
if(proto & lwip_if_proto_tcp)
socket->receive_buffer_locked.tcp = 0;
if(proto & lwip_if_proto_udp)
socket->receive_buffer_locked.udp = 0;
}
attr_nonnull attr_pure bool lwip_if_send_buffer_locked(lwip_if_socket_t *socket)
{
return(socket->sending_remaining > 0);
}
attr_nonnull unsigned int lwip_if_send_buffer_unacked(lwip_if_socket_t *socket)
{
return(socket->sent_unacked);
}
static err_t received_callback(bool tcp, lwip_if_socket_t *socket, struct pbuf *pbuf_received, const ip_addr_t *address, u16_t port)
{
struct pbuf *pbuf;
ip_addr_t _ip_addr_any = { IPADDR_ANY };
lwip_if_callback_context_t context;
bool locked = false;
int size;
context.tcp = !!tcp;
context.udp = !tcp;
context.multicast = false;
context.broadcast = false;
context.overflow = 0;
context.length = 0;
context.original_length = string_length(socket->receive_buffer);
context.parts = 0;
if(pbuf_received->flags & PBUF_FLAG_LLBCAST)
{
stat_lwip_broadcast_received++;
context.broadcast = true;
}
else
{
if(pbuf_received->flags & PBUF_FLAG_LLMCAST)
{
stat_lwip_multicast_received++;
context.multicast = true;
}
else
stat_lwip_unicast_received++;
}
if(tcp && socket->receive_buffer_locked.tcp)
{
stat_lwip_tcp_locked++;
locked = true;
}
if(!tcp && socket->receive_buffer_locked.udp)
{
stat_lwip_udp_locked++;
locked = true;
}
if(locked)
{
if(context.broadcast)
stat_lwip_broadcast_dropped++;
else
if(context.multicast)
stat_lwip_multicast_dropped++;
else
stat_lwip_unicast_dropped++;
pbuf_free(pbuf_received); // still processing previous buffer, simply drop the received data
return(ERR_OK);
}
if(((unsigned int)address >= 0x3ffe8000) && ((unsigned int)address < 0x40000000))
{
socket->peer.address = *address;
socket->peer.port = port;
}
else
{
if(address != (const ip_addr_t *)0)
log("lwip-interface: received callback address invalid: %p\n", address);
socket->peer.address = _ip_addr_any;
socket->peer.port = 0;
}
size = string_size(socket->receive_buffer);
for(pbuf = pbuf_received; pbuf; pbuf = pbuf->next)
{
context.parts++;
if(tcp)
{
stat_lwip_tcp_received_packets++;
stat_lwip_tcp_received_bytes += pbuf->len;
}
else
{
stat_lwip_udp_received_packets++;
stat_lwip_udp_received_bytes += pbuf->len;
}
if((pbuf->len + string_length(socket->receive_buffer)) > size)
context.overflow += pbuf->len;
else
string_append_bytes(socket->receive_buffer, pbuf->payload, pbuf->len);
}
pbuf_free(pbuf_received);
socket->receive_buffer_locked.tcp = 1;
socket->receive_buffer_locked.udp = 1;
context.length = string_length(socket->receive_buffer);
context.buffer_string = socket->receive_buffer;
context.buffer_size = string_size(socket->receive_buffer);
context.buffer = string_buffer_nonconst(socket->receive_buffer);
socket->callback_data_received(socket, &context);
return(ERR_OK);
}
static void udp_received_callback(void *callback_arg, struct udp_pcb *pcb, struct pbuf *pbuf_received, ip_addr_t *address, u16_t port)
{
lwip_if_socket_t *socket = (lwip_if_socket_t *)callback_arg;
received_callback(false, socket, pbuf_received, address, port);
}
static err_t tcp_received_callback(void *callback_arg, struct tcp_pcb *pcb, struct pbuf *pbuf, err_t error)
{
lwip_if_socket_t *socket = (lwip_if_socket_t *)callback_arg;
struct tcp_pcb **pcb_tcp = (struct tcp_pcb **)&socket->tcp.pcb;
err_t local_error;
/* connection closed */
if((pcb == (struct tcp_pcb *)0) || (pbuf == (struct pbuf *)0))
{
if(socket->reboot_pending)
{
reset();
return(ERR_ABRT);
}
if(pbuf)
pbuf_free(pbuf);
if(*pcb_tcp)
{
if((error = tcp_close(*pcb_tcp)) != ERR_OK)
log_error(socket->name, "tcp received callback: tcp close: error", error);
}
*pcb_tcp = (struct tcp_pcb *)0;
socket->sending_remaining = 0;
socket->sent_unacked = 0;
return(ERR_OK);
}
if(error != ERR_OK)
{
log_error(socket->name, "tcp received callback: error", error);
if(pbuf)
pbuf_free(pbuf);
if(*pcb_tcp)
tcp_abort(*pcb_tcp);
*pcb_tcp = (struct tcp_pcb *)0;
socket->sending_remaining = 0;
return(ERR_ABRT);
}
if(pcb != *pcb_tcp)
log("tcp received callback: pcb != *pcb_tcp\n");
if((local_error = received_callback(true, socket, pbuf, 0, 0)) != ERR_OK)
return(local_error);
tcp_recved(pcb, pbuf->tot_len);
return(ERR_OK);
}
static bool tcp_try_send_buffer(lwip_if_socket_t *socket)
{
struct tcp_pcb *pcb_tcp = (struct tcp_pcb *)socket->tcp.pcb;
unsigned int chunk_size, offset, apiflags;
unsigned int tcp_send_buffer_size;
bool success = false;
err_t error;
if(socket->sending_remaining == 0)
{
log("lwip tcp try send buffer: no more data left in send buffer\n");
return(true);
}
if((tcp_send_buffer_size = tcp_sndbuf(pcb_tcp)) == 0)
{
log("lwip tcp try send buffer: no more data left in tcp send buffer\n");
return(false);
}
chunk_size = socket->sending_remaining;
apiflags = TCP_WRITE_FLAG_COPY;
if(chunk_size > tcp_send_buffer_size)
{
chunk_size = tcp_send_buffer_size;
apiflags |= TCP_WRITE_FLAG_MORE;
}
offset = string_length(socket->send_buffer) - socket->sending_remaining;
if((error = tcp_write(pcb_tcp, string_buffer(socket->send_buffer) + offset, chunk_size, apiflags)) != ERR_OK)
stat_lwip_tcp_send_error++;
else
{
stat_lwip_tcp_sent_packets++;
stat_lwip_tcp_sent_bytes += chunk_size;
socket->sending_remaining -= chunk_size;
socket->sent_unacked += chunk_size;
success = true;
}
if((error = tcp_output(pcb_tcp)) != ERR_OK)
log_error(socket->name, "lwip: tcp send: tcp_output: error", error);
return(success);
}
static err_t tcp_sent_callback(void *callback_arg, struct tcp_pcb *pcb, u16_t len)
{
lwip_if_socket_t *socket = (lwip_if_socket_t *)callback_arg;
if(len > socket->sent_unacked)
{
log("tcp sent callback: acked (%u) > sent_unacked (%d)\n", len, socket->sent_unacked);
socket->sent_unacked = 0;
}
else
socket->sent_unacked -= len;
if((socket->sending_remaining > 0) && (!tcp_try_send_buffer(socket)))
socket->sending_remaining = 0;
return(ERR_OK);
}
static void tcp_error_callback(void *callback_arg, err_t error)
{
lwip_if_socket_t *socket = (lwip_if_socket_t *)callback_arg;
struct tcp_pcb **pcb_tcp = (struct tcp_pcb **)&socket->tcp.pcb;
if(error != ERR_ISCONN)
log_error(socket->name, "tcp error callback", error);
if(socket->reboot_pending)
reset();
*pcb_tcp = (struct tcp_pcb *)0;
socket->sending_remaining = 0;
}
static err_t tcp_accepted_callback(void *callback_arg, struct tcp_pcb *pcb, err_t error)
{
lwip_if_socket_t *socket = (lwip_if_socket_t *)callback_arg;
struct tcp_pcb **pcb_tcp = (struct tcp_pcb **)&socket->tcp.pcb;
if(error != ERR_OK)
log_error(socket->name, "tcp accepted callback", error);
if(*pcb_tcp != (struct tcp_pcb *)0)
{
log("tcp accepted callback: abort current\n");
tcp_abort(*pcb_tcp);
}
*pcb_tcp = pcb;
tcp_nagle_disable(*pcb_tcp);
tcp_arg(*pcb_tcp, socket);
tcp_err(*pcb_tcp, tcp_error_callback);
tcp_recv(*pcb_tcp, tcp_received_callback);
tcp_sent(*pcb_tcp, tcp_sent_callback);
return(ERR_OK);
}
attr_nonnull bool lwip_if_close(lwip_if_socket_t *socket)
{
err_t error;
if(lwip_if_received_udp(socket))
return(false);
if(!socket->tcp.listen_pcb)
{
log("lwip if close: tcp pcb is null\n");
return(false);
}
if(!socket->tcp.pcb)
{
log("lwip if close: not tcp connected\n");
return(false);
}
if(socket->reboot_pending)
{
if((error = tcp_close(socket->tcp.pcb)) != ERR_OK)
log_error(socket->name, "lwip if close: tcp_close failed", error);
}
else
tcp_abort(socket->tcp.pcb);
return(true);
}
attr_nonnull bool lwip_if_sendto(lwip_if_socket_t *socket, const ip_addr_t *address_in, unsigned int port)
{
err_t error;
struct pbuf *pbuf = (struct pbuf *)socket->udp.pbuf_send;
struct udp_pcb *pcb_udp = (struct udp_pcb *)socket->udp.pcb;
unsigned int offset, length, total_length;
ip_addr_t address = *address_in;
total_length = string_length(socket->send_buffer);
for(offset = 0; total_length > 0; offset += length, total_length -= length)
{
length = total_length;
if(length > lwip_udp_max_payload)
length = lwip_udp_max_payload;
pbuf->len = pbuf->tot_len = length;
pbuf->payload = string_buffer_nonconst(socket->send_buffer) + offset;
pbuf->eb = 0;
if((error = udp_sendto(pcb_udp, pbuf, &address, port)) != ERR_OK)
{
stat_lwip_udp_send_error++;
log_error(socket->name, "lwip if sendto: udp send failed", error);
return(false);
}
}
return(true);
}
attr_nonnull bool lwip_if_send(lwip_if_socket_t *socket)
{
err_t error;
/* this means the output buffer been changed+sent while it still was sending! */
if(socket->sending_remaining > 0)
{
log("lwip if send: still sending %d bytes\n", socket->sending_remaining);
return(false);
}
if(socket->peer.port) // received packet from UDP, reply using UDP
{
struct pbuf *pbuf = (struct pbuf *)socket->udp.pbuf_send;
struct udp_pcb *pcb_udp = (struct udp_pcb *)socket->udp.pcb;
unsigned int offset, length, total_length;
total_length = string_length(socket->send_buffer);
for(offset = 0; total_length > 0; offset += length, total_length -= length)
{
length = total_length;
if(length > lwip_udp_max_payload)
length = lwip_udp_max_payload;
pbuf->len = pbuf->tot_len = length;
pbuf->payload = string_buffer_nonconst(socket->send_buffer) + offset;
pbuf->eb = 0;
if((error = udp_sendto(pcb_udp, pbuf, &socket->peer.address, socket->peer.port)) != ERR_OK)
{
stat_lwip_udp_send_error++;
log_error(socket->name, "lwip if send: udp send failed", error);
}
stat_lwip_udp_sent_packets++;
stat_lwip_udp_sent_bytes += length;
}
}
else // received packet from TCP, reply using TCP
{
struct tcp_pcb *pcb_tcp = (struct tcp_pcb *)socket->tcp.pcb;
if(pcb_tcp == (struct tcp_pcb *)0)
{
socket->sending_remaining = 0;
socket->sent_unacked = 0;
return(false);
}
socket->sending_remaining = string_length(socket->send_buffer);
if(!tcp_try_send_buffer(socket))
{
socket->sending_remaining = 0;
return(false);
}
}
return(true);
}
attr_nonnull bool lwip_if_reboot(lwip_if_socket_t *socket)
{
/* reset after socket close, to be able to flush remaining data in buffer */
socket->reboot_pending = 1;
/* if connection is udp or tcp close failed, let the application layer handle the reset */
if(!lwip_if_close(socket))
return(false);
return(true);
}
attr_nonnull bool lwip_if_socket_create(lwip_if_socket_t *socket, const char *name, string_t *receive_buffer, string_t *send_buffer,
unsigned int port, bool create_tcp_socket, callback_data_received_fn_t callback_data_received)
{
err_t error;
ip_addr_t _ip_addr_any = { IPADDR_ANY };
socket->udp.pcb = (struct udp_pcb *)0;
socket->tcp.listen_pcb = (struct tcp_pcb *)0;
socket->tcp.pcb = (struct tcp_pcb *)0;
socket->peer.address = _ip_addr_any;
socket->peer.port = 0;
socket->receive_buffer = receive_buffer;
socket->send_buffer = send_buffer;
socket->sending_remaining = 0;
socket->sent_unacked = 0;
socket->receive_buffer_locked.tcp = 0;
socket->receive_buffer_locked.udp = 0;
socket->reboot_pending = 0;
strecpy(socket->name, name, sizeof(socket->name));
socket->callback_data_received = callback_data_received;
if(!(socket->udp.pbuf_send = pbuf_alloc(PBUF_TRANSPORT, 0, PBUF_ROM)))
{
log("lwip if socket create: pbuf_alloc failed\n");
return(false);
}
if(!(socket->udp.pcb = udp_new()))
{
log("lwip if socket create: udp_new failed\n");
return(false);
}
udp_recv(socket->udp.pcb, udp_received_callback, socket);
if((error = udp_bind(socket->udp.pcb, &_ip_addr_any, port)) != ERR_OK)
{
log_error(socket->name, "lwip if socket create: udp_bind failed", error);
return(false);
}
if(create_tcp_socket)
{
if(!(socket->tcp.listen_pcb = tcp_new()))
{
log("lwip if socket create: tcp_new failed\n");
return(false);
}
if((error = tcp_bind(socket->tcp.listen_pcb, &_ip_addr_any, port)) != ERR_OK)
{
log_error(socket->name, "lwip if socket create: tcp_bind failed", error);
return(false);
}
if(!(socket->tcp.listen_pcb = tcp_listen_with_backlog(socket->tcp.listen_pcb, 1)))
{
log("lwip if socket create: tcp_listen failed\n");
return(false);
}
tcp_arg(socket->tcp.listen_pcb, socket);
tcp_accept(socket->tcp.listen_pcb, tcp_accepted_callback);
}
else
socket->tcp.listen_pcb = (struct tcp_pcb *)0;
socket->local.port = port;
return(true);
}
bool attr_nonnull lwip_if_join_mc(ip_addr_t mc_ip)
{
ip_addr_t _ip_addr_any = { IPADDR_ANY };
return(igmp_joingroup(&_ip_addr_any, &mc_ip) == ERR_OK);
}
static void dump_tcp_pcbs(string_t *out, const struct tcp_pcb *p)
{
for(; p != (const struct tcp_pcb *)0; p = p->next)
{
string_format(out, "> local %lu.%lu.%lu.%lu@%u, ",
(p->local_ip.addr & 0x000000ff) >> 0,
(p->local_ip.addr & 0x0000ff00) >> 8,
(p->local_ip.addr & 0x00ff0000) >> 16,
(p->local_ip.addr & 0xff000000) >> 24, p->local_port);
string_format(out, "remote %lu.%lu.%lu.%lu@%u, ",
(p->remote_ip.addr & 0x000000ff) >> 0,
(p->remote_ip.addr & 0x0000ff00) >> 8,
(p->remote_ip.addr & 0x00ff0000) >> 16,
(p->remote_ip.addr & 0xff000000) >> 24, p->remote_port);
string_format(out, "options: %x, state: %x, tcp_flags: %x\n",
p->so_options, p->state, p->flags);
}
}
static void dump_tcp_listen_pcbs(string_t *out, const struct tcp_pcb_listen *p)
{
for(; p != (const struct tcp_pcb_listen *)0; p = p->next)
{
string_format(out, "> local %lu.%lu.%lu.%lu@%u, ",
(p->local_ip.addr & 0x000000ff) >> 0,
(p->local_ip.addr & 0x0000ff00) >> 8,
(p->local_ip.addr & 0x00ff0000) >> 16,
(p->local_ip.addr & 0xff000000) >> 24, p->local_port);
string_format(out, "remote %lu.%lu.%lu.%lu, ",
(p->remote_ip.addr & 0x000000ff) >> 0,
(p->remote_ip.addr & 0x0000ff00) >> 8,
(p->remote_ip.addr & 0x00ff0000) >> 16,
(p->remote_ip.addr & 0xff000000) >> 24);
string_format(out, "options: %x, state: %x\n",
p->so_options, p->state);
}
}
void lwip_netstat_bound(string_t *out)
{
dump_tcp_pcbs(out, tcp_bound_pcbs);
}
void lwip_netstat_listening(string_t *out)
{
dump_tcp_listen_pcbs(out, tcp_listen_pcbs.listen_pcbs);
}
void lwip_netstat_active(string_t *out)
{
dump_tcp_pcbs(out, tcp_active_pcbs);
}
void lwip_netstat_timewait(string_t *out)
{
dump_tcp_pcbs(out, tcp_tw_pcbs);
}