-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathpico_bsd_sockets.c
1590 lines (1389 loc) · 45.6 KB
/
pico_bsd_sockets.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
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*********************************************************************
PicoTCP. Copyright (c) 2013 TASS Belgium NV. Some rights reserved.
See LICENSE and COPYING for usage.
Do not redistribute without a written permission by the Copyright
holders.
Author: Maxime Vincent, Daniele Lacamera
*********************************************************************/
#include "pico_defines.h"
#include "pico_config.h" /* for zalloc and free */
#include "pico_bsd_sockets.h"
#include "pico_osal.h"
#ifdef PICO_SUPPORT_SNTP_CLIENT
#include "pico_sntp_client.h"
#endif
#include <errno.h> /* should be there in C99 */
#define SOCK_OPEN 0
#define SOCK_BOUND 1
#define SOCK_LISTEN 2
#define SOCK_CONNECTED 3
#define SOCK_ERROR 4
#define SOCK_RESET_BY_PEER 5
#define SOCK_CLOSED 100
#define bsd_dbg(...) do {} while(0)
#define bsd_dbg_select(...) do {} while(0)
/* Global signal sent on any event (for select) */
void * picoLock = NULL; /* pico stack lock */
void * pico_signal_select = NULL; /* pico global signal for select */
void * pico_signal_tick = NULL; /* pico tick signal, e.g. coming from a driver ISR */
struct pico_bsd_endpoint {
struct pico_socket *s;
int socket_fd;
int posix_fd; /* TODO: ifdef... */
uint8_t in_use;
uint8_t state; /* for pico_state */
uint8_t nonblocking; /* The non-blocking flag, for non-blocking socket operations */
uint16_t events; /* events that we filter for */
uint16_t revents; /* received events */
uint16_t proto;
void * mutex_lock; /* mutex for clearing revents */
void * signal; /* signals new events */
uint32_t timeout; /* this is used for timeout sockets */
int error; /* used for SO_ERROR sockopt after connect() */
};
/* MACRO's */
#define VALIDATE_NULL(param) \
if(!param) \
{ \
return -1; \
}
#define VALIDATE_ONE(param,value) \
if(param != value) { \
pico_err = PICO_ERR_EINVAL; \
errno = pico_err; \
return -1; \
}
#define VALIDATE_TWO(param,value1,value2) \
if(param != value1 && param != value2) { \
pico_err = PICO_ERR_EINVAL; \
errno = pico_err; \
return -1; \
}
/* Private function prototypes */
static void pico_event_clear(struct pico_bsd_endpoint *ep, uint16_t events);
static uint16_t pico_bsd_wait(struct pico_bsd_endpoint * ep, int read, int write, int close);
static void pico_socket_event(uint16_t ev, struct pico_socket *s);
/************************/
/* Public API functions */
/************************/
void pico_bsd_init(void)
{
pico_signal_select = pico_signal_init();
pico_signal_tick = pico_signal_init();
picoLock = pico_mutex_init();
}
void pico_bsd_deinit(void)
{
pico_mutex_deinit(picoLock);
}
/* just ticks the stack twice */
void pico_bsd_stack_tick(void)
{
pico_mutex_lock(picoLock);
pico_stack_tick();
pico_stack_tick();
pico_mutex_unlock(picoLock);
}
/* ticks the stack, but wait for a signal with a timeout (e.g. from the driver interrupt) */
void pico_bsd_stack_tick_timeout(int timeout_ms)
{
pico_signal_wait_timeout(pico_signal_tick, timeout_ms);
pico_bsd_stack_tick();
}
/** Declarations of helper functions **/
static struct pico_bsd_endpoint *pico_bsd_create_socket(void);
static int get_free_sd(struct pico_bsd_endpoint *ep);
static int new_sd(struct pico_bsd_endpoint *ep);
static void free_up_ep(struct pico_bsd_endpoint *ep);
static struct pico_bsd_endpoint *get_endpoint(int sd, int set_err);
static int bsd_to_pico_addr(union pico_address *addr, const struct sockaddr *_saddr, socklen_t socklen);
static uint16_t bsd_to_pico_port(const struct sockaddr *_saddr, socklen_t socklen);
static int pico_addr_to_bsd(struct sockaddr *_saddr, socklen_t socklen, union pico_address *addr, uint16_t net);
static int pico_port_to_bsd(struct sockaddr *_saddr, socklen_t socklen, uint16_t port);
/** Global Sockets descriptors array **/
static struct pico_bsd_endpoint **PicoSockets = NULL;
static int PicoSocket_max = 0;
/*** Public socket functions ***/
/* Socket interface. */
int pico_newsocket(int domain, int type, int proto)
{
struct pico_bsd_endpoint * ep = NULL;
(void)proto;
#ifdef PICO_SUPPORT_IPV6
VALIDATE_TWO(domain,AF_INET, AF_INET6);
#else
VALIDATE_ONE(domain, AF_INET);
#endif
VALIDATE_TWO(type,SOCK_STREAM,SOCK_DGRAM);
if (AF_INET6 != PICO_PROTO_IPV6) {
if (domain == AF_INET6)
domain = PICO_PROTO_IPV6;
else
domain = PICO_PROTO_IPV4;
}
if (SOCK_STREAM != PICO_PROTO_TCP) {
if (type == SOCK_STREAM)
type = PICO_PROTO_TCP;
else
type = PICO_PROTO_UDP;
}
pico_mutex_lock(picoLock);
ep = pico_bsd_create_socket();
VALIDATE_NULL(ep);
ep->error = PICO_ERR_NOERR;
ep->proto = type;
ep->s = pico_socket_open(domain, type,&pico_socket_event);
if (!ep->s)
{
PICO_FREE(ep);
pico_mutex_unlock(picoLock);
return -1;
}
ep->s->priv = ep; /* let priv point to the endpoint struct */
/* open picotcp endpoint */
ep->state = SOCK_OPEN;
ep->mutex_lock = pico_mutex_init();
ep->signal = pico_signal_init();
ep->error = pico_err;
pico_mutex_unlock(picoLock);
return ep->socket_fd;
}
int pico_bind(int sd, struct sockaddr * local_addr, socklen_t socklen)
{
union pico_address addr = { .ip4 = { 0 } };
uint16_t port;
struct pico_bsd_endpoint *ep = get_endpoint(sd, 1);
VALIDATE_NULL(ep);
ep->error = PICO_ERR_NOERR;
VALIDATE_NULL(local_addr);
VALIDATE_TWO(socklen, SOCKSIZE, SOCKSIZE6);
if (bsd_to_pico_addr(&addr, local_addr, socklen) < 0)
{
ep->error = PICO_ERR_EINVAL;
errno = pico_err;
return -1;
}
port = bsd_to_pico_port(local_addr, socklen);
pico_mutex_lock(picoLock);
if(pico_socket_bind(ep->s, &addr, &port) < 0)
{
ep->error = pico_err;
errno = pico_err;
pico_mutex_unlock(picoLock);
return -1;
}
ep->state = SOCK_BOUND;
pico_mutex_unlock(picoLock);
return 0;
}
int pico_getsockname(int sd, struct sockaddr * local_addr, socklen_t *socklen)
{
union pico_address addr;
uint16_t port, proto;
struct pico_bsd_endpoint *ep = get_endpoint(sd, 1);
VALIDATE_NULL(ep);
ep->error = PICO_ERR_NOERR;
VALIDATE_NULL(local_addr);
VALIDATE_NULL(socklen);
pico_mutex_lock(picoLock);
if(pico_socket_getname(ep->s, &addr, &port, &proto) < 0)
{
ep->error = pico_err;
errno = pico_err;
pico_mutex_unlock(picoLock);
return -1;
}
if (proto == PICO_PROTO_IPV6)
*socklen = SOCKSIZE6;
else
*socklen = SOCKSIZE;
if (pico_addr_to_bsd(local_addr, *socklen, &addr, proto) < 0) {
ep->error = pico_err;
errno = pico_err;
pico_mutex_unlock(picoLock);
return -1;
}
pico_mutex_unlock(picoLock);
pico_port_to_bsd(local_addr, *socklen, port);
ep->error = pico_err;
return 0;
}
int pico_getpeername(int sd, struct sockaddr * remote_addr, socklen_t *socklen)
{
union pico_address addr;
uint16_t port, proto;
struct pico_bsd_endpoint *ep = get_endpoint(sd, 1);
VALIDATE_NULL(ep);
ep->error = PICO_ERR_NOERR;
VALIDATE_NULL(remote_addr);
VALIDATE_NULL(socklen);
pico_mutex_lock(picoLock);
if(pico_socket_getpeername(ep->s, &addr, &port, &proto) < 0)
{
pico_mutex_unlock(picoLock);
return -1;
}
if (proto == PICO_PROTO_IPV6)
*socklen = SOCKSIZE6;
else
*socklen = SOCKSIZE;
if (pico_addr_to_bsd(remote_addr, *socklen, &addr, proto) < 0) {
pico_mutex_unlock(picoLock);
return -1;
}
pico_mutex_unlock(picoLock);
pico_port_to_bsd(remote_addr, *socklen, port);
return 0;
}
int pico_listen(int sd, int backlog)
{
struct pico_bsd_endpoint *ep = get_endpoint(sd, 1);
VALIDATE_NULL(ep);
ep->error = PICO_ERR_NOERR;
VALIDATE_NULL(ep->s);
VALIDATE_ONE(ep->state, SOCK_BOUND);
pico_mutex_lock(picoLock);
if(pico_socket_listen(ep->s, backlog) < 0)
{
ep->error = pico_err;
errno = pico_err;
pico_mutex_unlock(picoLock);
return -1;
}
ep->state = SOCK_LISTEN;
ep->error = pico_err;
pico_mutex_unlock(picoLock);
return 0;
}
int pico_connect(int sd, const struct sockaddr *_saddr, socklen_t socklen)
{
struct pico_bsd_endpoint *ep = get_endpoint(sd, 1);
union pico_address addr;
uint16_t port;
uint16_t ev = 0;
int ret;
VALIDATE_NULL(ep);
ep->error = PICO_ERR_NOERR;
VALIDATE_NULL(_saddr);
if (bsd_to_pico_addr(&addr, _saddr, socklen) < 0)
{
ep->error = PICO_ERR_EINVAL;
errno = pico_err;
return -1;
}
port = bsd_to_pico_port(_saddr, socklen);
pico_mutex_lock(picoLock);
ret = pico_socket_connect(ep->s, &addr, port);
pico_mutex_unlock(picoLock);
if (ret < 0) {
ep->error = pico_err;
return -1;
}
if (ep->nonblocking) {
pico_err = PICO_ERR_EINPROGRESS;
ep->error = pico_err;
} else {
/* wait for event */
ev = pico_bsd_wait(ep, 0, 0, 0); /* wait for ERR, FIN and CONN */
}
if(ev & PICO_SOCK_EV_CONN)
{
/* clear the EV_CONN event */
pico_event_clear(ep, PICO_SOCK_EV_CONN);
ep->error = pico_err;
return 0;
} else {
if (!(ep->nonblocking))
pico_socket_close(ep->s);
}
ep->error = pico_err;
errno = pico_err;
return -1;
}
int pico_isconnected(int sd) {
struct pico_bsd_endpoint *ep = NULL;
int state = 0;
ep = get_endpoint(sd, 1);
VALIDATE_NULL(ep);
ep->error = PICO_ERR_NOERR;
pico_mutex_lock(picoLock);
if(ep->state == SOCK_CONNECTED) {
state = 1;
}
pico_mutex_unlock(picoLock);
return state;
}
int pico_accept(int sd, struct sockaddr *_orig, socklen_t *socklen)
{
struct pico_bsd_endpoint *ep, * client_ep = NULL;
uint16_t events;
union pico_address picoaddr;
uint16_t port;
ep = get_endpoint(sd, 1);
VALIDATE_NULL(ep);
ep->error = PICO_ERR_NOERR;
VALIDATE_ONE(ep->state, SOCK_LISTEN);
if (ep->nonblocking)
events = PICO_SOCK_EV_CONN;
else
events = pico_bsd_wait(ep, 0, 0, 0); /* Wait for CONN, FIN and ERR */
if(events & PICO_SOCK_EV_CONN)
{
struct pico_socket *s;
pico_mutex_lock(picoLock);
s = pico_socket_accept(ep->s,&picoaddr,&port);
if (!s)
{
ep->error = pico_err;
errno = pico_err;
pico_mutex_unlock(picoLock);
return -1;
}
/* Create a new client EP, only after the accept returned succesfully */
client_ep = pico_bsd_create_socket();
if (!client_ep)
{
ep->error = pico_err;
errno = pico_err;
pico_mutex_unlock(picoLock);
return -1;
}
client_ep->s = s;
client_ep->state = SOCK_OPEN;
client_ep->mutex_lock = pico_mutex_init();
client_ep->signal = pico_signal_init();
client_ep->s->priv = client_ep;
pico_event_clear(ep, PICO_SOCK_EV_CONN); /* clear the CONN event the listening socket */
if (client_ep->s->net->proto_number == PICO_PROTO_IPV4)
*socklen = SOCKSIZE;
else
*socklen = SOCKSIZE6;
client_ep->state = SOCK_CONNECTED;
if (pico_addr_to_bsd(_orig, *socklen, &picoaddr, client_ep->s->net->proto_number) < 0) {
client_ep->in_use = 0;
pico_mutex_unlock(picoLock);
return -1;
}
pico_port_to_bsd(_orig, *socklen, port);
client_ep->in_use = 1;
pico_mutex_unlock(picoLock);
ep->error = pico_err;
return client_ep->socket_fd;
}
client_ep->in_use = 0;
ep->error = pico_err;
errno = pico_err;
return -1;
}
int pico_sendto(int sd, void * buf, int len, int flags, struct sockaddr *_dst, socklen_t socklen)
{
int retval = 0;
int tot_len = 0;
uint16_t port;
union pico_address picoaddr;
struct pico_bsd_endpoint *ep = get_endpoint(sd, 1);
VALIDATE_NULL(ep);
ep->error = PICO_ERR_NOERR;
if (!buf || (len <= 0)) {
pico_err = PICO_ERR_EINVAL;
errno = pico_err;
ep->error = pico_err;
return -1;
}
while (tot_len < len) {
/* Write to the pico socket */
pico_mutex_lock(picoLock);
if (_dst == NULL) {
retval = pico_socket_send(ep->s, ((uint8_t *)buf) + tot_len, len - tot_len);
} else {
if (bsd_to_pico_addr(&picoaddr, _dst, socklen) < 0) {
ep->error = PICO_ERR_EINVAL;
errno = pico_err;
pico_mutex_unlock(picoLock);
return -1;
}
port = bsd_to_pico_port(_dst, socklen);
retval = pico_socket_sendto(ep->s, ((uint8_t *)buf) + tot_len, len - tot_len, &picoaddr, port);
}
pico_event_clear(ep, PICO_SOCK_EV_WR);
pico_mutex_unlock(picoLock);
/* If sending failed, return an error */
if (retval < 0)
{
ep->error = pico_err;
errno = pico_err;
pico_event_clear(ep, PICO_SOCK_EV_WR);
return -1;
}
if (retval > 0)
{
tot_len += retval;
break;
}
if (ep->nonblocking)
break;
/* If sent bytes (retval) < len-tot_len: socket full, we need to wait for a new WR event */
if (retval < (len - tot_len))
{
uint16_t ev = 0;
/* wait for a new WR or CLOSE event */
ev = pico_bsd_wait(ep, 0, 1, 1);
if (ev & (PICO_SOCK_EV_ERR | PICO_SOCK_EV_FIN | PICO_SOCK_EV_CLOSE))
{
ep->error = pico_err;
errno = pico_err;
pico_event_clear(ep, PICO_SOCK_EV_WR);
/* closing and freeing the socket is done in the event handler */
return -1;
}
}
tot_len += retval;
}
ep->error = pico_err;
return tot_len;
}
int pico_fcntl(int sd, int cmd, int arg)
{
struct pico_bsd_endpoint *ep = get_endpoint(sd, 1);
if (!ep) {
pico_err = PICO_ERR_EINVAL;
errno = pico_err;
return -1;
}
if (cmd == F_SETFL) {
if ((arg & O_NONBLOCK) != 0) {
ep->nonblocking = 1;
} else {
ep->nonblocking = 0;
}
ep->error = PICO_ERR_NOERR;
return 0;
}
if (cmd == F_GETFL) {
(void)arg; /* F_GETFL: arg is ignored */
ep->error = PICO_ERR_NOERR;
if (ep->nonblocking)
return O_NONBLOCK;
else
return 0;
}
if (cmd == F_SETFD) {
(void)arg;
ep->error = PICO_ERR_NOERR;
return 0;
}
pico_err = PICO_ERR_EINVAL;
errno = pico_err;
ep->error = pico_err;
return -1;
}
/*
* RETURN VALUE
* Upon successful completion, recv_from() shall return the length of the
* message in bytes. If no messages are available to be received and the
* peer has performed an orderly shutdown, recv() shall return 0. Otherwise,
* −1 shall be returned and errno set to indicate the error.
*/
int pico_recvfrom(int sd, void * _buf, int len, int flags, struct sockaddr *_addr, socklen_t *socklen)
{
int retval = 0;
int tot_len = 0;
struct pico_bsd_endpoint *ep = get_endpoint(sd, 1);
union pico_address picoaddr;
uint16_t port;
unsigned char *buf = (unsigned char *)_buf;
bsd_dbg("Recvfrom called \n");
VALIDATE_NULL(ep);
ep->error = PICO_ERR_NOERR;
if (ep->state == SOCK_RESET_BY_PEER) {
/* not much to do here. Peer has nothing to say. */
return 0;
}
if (!buf || (len <= 0)) {
pico_err = PICO_ERR_EINVAL;
errno = pico_err;
ep->error = pico_err;
return -1;
}
while (tot_len < len) {
pico_mutex_lock(picoLock);
retval = pico_socket_recvfrom(ep->s, buf + tot_len , len - tot_len, &picoaddr, &port);
pico_mutex_unlock(picoLock);
bsd_dbg("pico_socket_recvfrom returns %d, first bytes are %c-%c-%c-%c\n", retval, buf[0], buf[1], buf[2], buf[3]);
/* pico_socket_recvfrom failed */
if (retval < 0) {
/* data was received */
if (tot_len > 0)
{
bsd_dbg("Recvfrom returning %d\n", tot_len);
ep->error = pico_err;
return tot_len;
}
/* no data was received yet */
ep->error = pico_err;
if (pico_err == PICO_ERR_ESHUTDOWN) /* If no messages are available to be received and the peer has performed an orderly shutdown, recvfrom() shall return 0. */
{
return 0;
}
else /* Otherwise, the function shall return −1 and set errno to indicate the error. */
{
return -1;
}
}
/* If received 0 bytes, return -1 or amount of bytes received */
if (retval == 0) {
pico_event_clear(ep, PICO_SOCK_EV_RD);
if (tot_len > 0) {
bsd_dbg("Recvfrom returning %d\n", tot_len);
ep->error = pico_err;
return tot_len;
}
}
if (retval > 0) {
if (ep->proto == PICO_PROTO_UDP) {
if (_addr && socklen > 0)
{
if (pico_addr_to_bsd(_addr, *socklen, &picoaddr, ep->s->net->proto_number) < 0) {
pico_err = PICO_ERR_EINVAL;
errno = pico_err;
ep->error = pico_err;
return -1;
}
pico_port_to_bsd(_addr, *socklen, port);
}
/* If in a recvfrom call, for UDP we should return immediately after the first dgram */
ep->error = pico_err;
return retval + tot_len;
} else {
/* TCP: continue until recvfrom = 0, socket buffer empty */
tot_len += retval;
continue;
}
}
/* Only way to reach this point is when `retval` == 0 and `tot_len` <= 0.
* The event SOCK_EV_RD will aready be cleared. The socket buffer
* is thus completely empty when calling this function and this
* point is reached. */
if (ep->nonblocking) {
if (retval == 0) {
pico_err = PICO_ERR_EAGAIN; /* or EWOULDBLOCK */
ep->error = pico_err;
errno = pico_err;
}
return -1; /* BSD-speak: -1 == 0 bytes received */
}
/* We have a blocking socket. We need to wait until data becomes
* available to be able to return from this function. */
/* If recv bytes (retval) < len-tot_len: socket empty, we need to wait for a new RD event */
if (retval < (len - tot_len))
{
uint16_t ev = 0;
/* wait for a new RD event -- also wait for CLOSE event */
ev = pico_bsd_wait(ep, 1, 0, 1);
if (ev & (PICO_SOCK_EV_ERR | PICO_SOCK_EV_FIN | PICO_SOCK_EV_CLOSE))
{
/* closing and freeing the socket is done in the event handler */
pico_event_clear(ep, PICO_SOCK_EV_RD);
ep->error = pico_err;
return 0; /* return 0 on a properly closed socket */
}
}
tot_len += retval;
}
/* We received a complete buffer of size `len`. Don't clear SOCK_EV_RD
* because there might still be available in the socket buffer.
* And clearing the READ event here might cause the application to run
* behind all data is possibly received */
bsd_dbg("Recvfrom returning %d (full block)\n", tot_len);
ep->error = pico_err;
return tot_len;
}
int pico_write(int sd, void * buf, int len)
{
return pico_sendto(sd, buf, len, 0, NULL, 0);
}
int pico_send(int sd, void * buf, int len, int flags)
{
return pico_sendto(sd, buf, len, flags, NULL, 0);
}
int pico_read(int sd, void * buf, int len)
{
return pico_recvfrom(sd, buf, len, 0, NULL, 0);
}
int pico_recv(int sd, void * buf, int len, int flags)
{
return pico_recvfrom(sd, buf, len, flags, NULL, 0);
}
int pico_close(int sd)
{
struct pico_bsd_endpoint *ep = get_endpoint(sd, 1);
VALIDATE_NULL(ep);
ep->error = PICO_ERR_NOERR;
if (ep->s && ep->in_use) /* valid socket, try to close it */
{
pico_mutex_lock(picoLock);
pico_socket_close(ep->s);
ep->s->priv = NULL;
pico_mutex_unlock(picoLock);
}
ep->in_use = 0;
ep->error = pico_err;
return 0;
}
int pico_shutdown(int sd, int how)
{
struct pico_bsd_endpoint *ep = get_endpoint(sd, 1);
VALIDATE_NULL(ep);
ep->error = PICO_ERR_NOERR;
if(ep->s) /* valid socket, try to close it */
{
pico_mutex_lock(picoLock);
pico_socket_shutdown(ep->s, how);
ep->error = pico_err;
pico_mutex_unlock(picoLock);
} else {
ep->error = PICO_ERR_EINVAL;
errno = pico_err;
}
return 0;
}
int pico_join_multicast_group(int sd, const char *address, const char *local) {
int ret;
struct pico_ip_mreq mreq={};
pico_string_to_ipv4(address, &mreq.mcast_group_addr.ip4.addr);
pico_string_to_ipv4(local, &mreq.mcast_link_addr.ip4.addr);
ret = pico_setsockopt(sd, SOL_SOCKET, PICO_IP_ADD_MEMBERSHIP, &mreq, sizeof(struct pico_ip_mreq));
return ret;
}
/*** Helper functions ***/
static int bsd_to_pico_addr(union pico_address *addr, const struct sockaddr *_saddr, socklen_t socklen)
{
VALIDATE_TWO(socklen, SOCKSIZE, SOCKSIZE6);
if (socklen == SOCKSIZE6) {
struct sockaddr_in6 *saddr = (struct sockaddr_in6 *)_saddr;
memcpy(&addr->ip6.addr, &saddr->sin6_addr.s6_addr, 16);
saddr->sin6_family = AF_INET6;
} else {
struct sockaddr_in *saddr = (struct sockaddr_in *)_saddr;
addr->ip4.addr = saddr->sin_addr.s_addr;
saddr->sin_family = AF_INET;
}
return 0;
}
static uint16_t bsd_to_pico_port(const struct sockaddr *_saddr, socklen_t socklen)
{
VALIDATE_TWO(socklen, SOCKSIZE, SOCKSIZE6);
if (socklen == SOCKSIZE6) {
struct sockaddr_in6 *saddr = (struct sockaddr_in6 *)_saddr;
return saddr->sin6_port;
} else {
struct sockaddr_in *saddr = (struct sockaddr_in *)_saddr;
return saddr->sin_port;
}
}
static int pico_port_to_bsd(struct sockaddr *_saddr, socklen_t socklen, uint16_t port)
{
if (socklen == SOCKSIZE6) {
struct sockaddr_in6 *saddr = (struct sockaddr_in6 *)_saddr;
saddr->sin6_port = port;
return 0;
} else {
struct sockaddr_in *saddr = (struct sockaddr_in *)_saddr;
saddr->sin_port = port;
return 0;
}
pico_err = PICO_ERR_EINVAL;
errno = pico_err;
return -1;
}
static int pico_addr_to_bsd(struct sockaddr *_saddr, socklen_t socklen, union pico_address *addr, uint16_t net)
{
VALIDATE_TWO(socklen, SOCKSIZE, SOCKSIZE6);
if ((socklen == SOCKSIZE6) && (net == PICO_PROTO_IPV6)) {
struct sockaddr_in6 *saddr = (struct sockaddr_in6 *)_saddr;
memcpy(&saddr->sin6_addr.s6_addr, &addr->ip6.addr, 16);
saddr->sin6_family = AF_INET6;
} else if ((socklen == SOCKSIZE) && (net == PICO_PROTO_IPV4)) {
struct sockaddr_in *saddr = (struct sockaddr_in *)_saddr;
saddr->sin_addr.s_addr = addr->ip4.addr;
saddr->sin_family = AF_INET;
}
return 0;
}
static void free_up_ep(struct pico_bsd_endpoint *ep)
{
if (ep->signal)
pico_signal_deinit(ep->signal);
if (ep->mutex_lock)
pico_mutex_deinit(ep->mutex_lock);
PICO_FREE(ep);
}
static int get_free_sd(struct pico_bsd_endpoint *ep)
{
int i;
for (i = 0; i < PicoSocket_max; i++) {
if (!PicoSockets[i]->in_use) {
free_up_ep(PicoSockets[i]);
PicoSockets[i] = ep;
return i;
}
}
return -1;
}
/* DLA TODO: make a GC for freeing up the last socket descriptor periodically if not in use */
static int new_sd(struct pico_bsd_endpoint *ep)
{
int sd = PicoSocket_max;
struct pico_bsd_endpoint **new;
new = PICO_ZALLOC(sizeof(void *) * ++PicoSocket_max);
if (!new) {
PicoSocket_max--;
pico_err = PICO_ERR_ENOMEM;
errno = pico_err;
return -1;
}
if (sd > 0) {
memcpy(new, PicoSockets, sd * sizeof(void *));
PICO_FREE(PicoSockets);
}
PicoSockets = new;
new[sd] = ep;
return sd;
}
/* picoLock must be taken already ! */
static struct pico_bsd_endpoint *pico_bsd_create_socket(void)
{
struct pico_bsd_endpoint *ep = PICO_ZALLOC(sizeof(struct pico_bsd_endpoint));
if (!ep) {
pico_err = PICO_ERR_ENOMEM;
errno = pico_err;
}
ep->in_use = 1;
ep->socket_fd = get_free_sd(ep);
if (ep->socket_fd < 0) {
ep->socket_fd = new_sd(ep);
}
return ep;
}
#ifndef PICO_EBADFD
# define PICO_EBADFD 77 /* File descriptor in bad state */
#endif
static struct pico_bsd_endpoint *get_endpoint(int sd, int set_err)
{
if ((sd > PicoSocket_max) || (sd < 0) ||
(PicoSockets[sd]->in_use == 0)) {
if (set_err)
{
pico_err = PICO_EBADFD;
errno = pico_err;
}
return NULL;
}
return PicoSockets[sd];
}
/* wait for one of the selected events, return any of those that occurred */
uint16_t pico_bsd_select(struct pico_bsd_endpoint *ep)
{
uint16_t events = ep->events & ep->revents; /* maybe an event we are waiting for, was already queued ? */
/* wait for one of the selected events... */
while (!events)
{
pico_signal_wait(ep->signal);
events = (ep->revents & ep->events); /* filter for the events we were waiting for */
}
/* the event we were waiting for happened, now report it */
return events; /* return any event(s) that occurred, that we were waiting for */
}
/****************************/
/* Private helper functions */
/****************************/
static uint16_t pico_bsd_wait(struct pico_bsd_endpoint * ep, int read, int write, int close)
{
pico_mutex_lock(ep->mutex_lock);
ep->events = PICO_SOCK_EV_ERR;
ep->events |= PICO_SOCK_EV_FIN;
ep->events |= PICO_SOCK_EV_CONN;
if (close)
ep->events |= PICO_SOCK_EV_CLOSE;
if (read)
ep->events |= PICO_SOCK_EV_RD;
if (write)
ep->events |= PICO_SOCK_EV_WR;
pico_mutex_unlock(ep->mutex_lock);
return pico_bsd_select(ep);
}
static void pico_event_clear(struct pico_bsd_endpoint *ep, uint16_t events)
{
pico_mutex_lock(ep->mutex_lock);
ep->revents &= ~events; /* clear those events */
pico_mutex_unlock(ep->mutex_lock);
}
/* NOTE: __NO__ picoLock'ing here !! */
/* this is called from pico_stack_tick, so picoLock is already locked */
static void pico_socket_event(uint16_t ev, struct pico_socket *s)
{
struct pico_bsd_endpoint * ep = (struct pico_bsd_endpoint *)(s->priv);
if (!s)
return;
if(!ep || !ep->s || !ep->mutex_lock || !ep->signal )
{
/* DLA: do not call close upon SOCK_CLOSE, we might still write. */
if(ev & (PICO_SOCK_EV_FIN | PICO_SOCK_EV_ERR) )
{
pico_signal_send(pico_signal_select); /* Signal this event globally (e.g. for select()) */
pico_socket_close(s);
}
if (ev & PICO_SOCK_EV_CLOSE)
pico_signal_send(pico_signal_select);
/* endpoint not initialized yet! */
return;
}
if(ep->in_use != 1)
return;
pico_mutex_lock(ep->mutex_lock); /* lock over the complete body is needed,
as the event might get cleared in another process.. */
ep->revents |= ev; /* set those events */
if(ev & PICO_SOCK_EV_CONN)
{
if(ep->state != SOCK_LISTEN)
{
ep->state = SOCK_CONNECTED;
}
}
if(ev & PICO_SOCK_EV_ERR)
{
ep->state = SOCK_RESET_BY_PEER;
}
if (ev & PICO_SOCK_EV_CLOSE) {
ep->state = SOCK_RESET_BY_PEER;
/* DO NOT close: we might still write! */
}
if (ev & PICO_SOCK_EV_FIN) {
/* DO NOT set ep->s = NULL, we might still be transmitting stuff! */
ep->state = SOCK_CLOSED;