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
|
/*
* <sys/socket.h> wrapper functions.
*
* Authors:
* Steffen Kiess (s-kiess@web.de)
*
* Copyright (C) 2015 Steffen Kiess
*/
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <sys/un.h>
#include <unistd.h>
#include <stddef.h>
#include "map.h"
#include "mph.h"
#include "sys-uio.h"
G_BEGIN_DECLS
int
Mono_Posix_SockaddrStorage_get_size (void)
{
return sizeof (struct sockaddr_storage);
}
int
Mono_Posix_SockaddrUn_get_sizeof_sun_path (void)
{
struct sockaddr_un sun;
return sizeof (sun.sun_path);
}
int
Mono_Posix_Cmsghdr_getsize (void)
{
return sizeof (struct cmsghdr);
}
int
Mono_Posix_FromInAddr (struct Mono_Posix_InAddr* source, void* destination)
{
memcpy (&((struct in_addr*)destination)->s_addr, &source->s_addr, 4);
return 0;
}
int
Mono_Posix_ToInAddr (void* source, struct Mono_Posix_InAddr* destination)
{
memcpy (&destination->s_addr, &((struct in_addr*)source)->s_addr, 4);
return 0;
}
int
Mono_Posix_FromIn6Addr (struct Mono_Posix_In6Addr* source, void* destination)
{
memcpy (&((struct in6_addr*)destination)->s6_addr, &source->addr0, 16);
return 0;
}
int
Mono_Posix_ToIn6Addr (void* source, struct Mono_Posix_In6Addr* destination)
{
memcpy (&destination->addr0, &((struct in6_addr*)source)->s6_addr, 16);
return 0;
}
int
Mono_Posix_Syscall_socketpair (int domain, int type, int protocol, int* socket1, int* socket2)
{
int filedes[2] = {-1, -1};
int r;
r = socketpair (domain, type, protocol, filedes);
*socket1 = filedes[0];
*socket2 = filedes[1];
return r;
}
int
Mono_Posix_Syscall_getsockopt (int socket, int level, int option_name, void* option_value, gint64* option_len)
{
socklen_t len;
int r;
mph_return_if_socklen_t_overflow (*option_len);
len = *option_len;
r = getsockopt (socket, level, option_name, option_value, &len);
*option_len = len;
return r;
}
int
Mono_Posix_Syscall_getsockopt_timeval (int socket, int level, int option_name, struct Mono_Posix_Timeval* option_value)
{
struct timeval tv;
int r;
socklen_t size;
size = sizeof (struct timeval);
r = getsockopt (socket, level, option_name, &tv, &size);
if (r != -1 && size == sizeof (struct timeval)) {
if (Mono_Posix_ToTimeval (&tv, option_value) != 0)
return -1;
} else {
memset (option_value, 0, sizeof (struct Mono_Posix_Timeval));
if (r != -1)
errno = EINVAL;
}
return r;
}
int
Mono_Posix_Syscall_getsockopt_linger (int socket, int level, int option_name, struct Mono_Posix_Linger* option_value)
{
struct linger ling;
int r;
socklen_t size;
size = sizeof (struct linger);
r = getsockopt (socket, level, option_name, &ling, &size);
if (r != -1 && size == sizeof (struct linger)) {
if (Mono_Posix_ToLinger (&ling, option_value) != 0)
return -1;
} else {
memset (option_value, 0, sizeof (struct Mono_Posix_Linger));
if (r != -1)
errno = EINVAL;
}
return r;
}
int
Mono_Posix_Syscall_setsockopt (int socket, int level, int option_name, void* option_value, gint64 option_len)
{
mph_return_if_socklen_t_overflow (option_len);
return setsockopt (socket, level, option_name, option_value, option_len);
}
int
Mono_Posix_Syscall_setsockopt_timeval (int socket, int level, int option_name, struct Mono_Posix_Timeval* option_value)
{
struct timeval tv;
if (Mono_Posix_FromTimeval (option_value, &tv) != 0)
return -1;
return setsockopt (socket, level, option_name, &tv, sizeof (struct timeval));
}
int
Mono_Posix_Syscall_setsockopt_linger (int socket, int level, int option_name, struct Mono_Posix_Linger* option_value)
{
struct linger ling;
if (Mono_Posix_FromLinger (option_value, &ling) != 0)
return -1;
return setsockopt (socket, level, option_name, &ling, sizeof (struct linger));
}
static int
get_addrlen (struct Mono_Posix__SockaddrHeader* address, socklen_t* addrlen)
{
if (!address) {
*addrlen = 0;
return 0;
}
switch (address->type) {
case Mono_Posix_SockaddrType_SockaddrStorage:
mph_return_if_socklen_t_overflow (((struct Mono_Posix__SockaddrDynamic*) address)->len);
*addrlen = ((struct Mono_Posix__SockaddrDynamic*) address)->len;
return 0;
case Mono_Posix_SockaddrType_SockaddrUn:
mph_return_if_socklen_t_overflow (offsetof (struct sockaddr_un, sun_path) + ((struct Mono_Posix__SockaddrDynamic*) address)->len);
*addrlen = offsetof (struct sockaddr_un, sun_path) + ((struct Mono_Posix__SockaddrDynamic*) address)->len;
return 0;
case Mono_Posix_SockaddrType_Sockaddr: *addrlen = sizeof (struct sockaddr); return 0;
case Mono_Posix_SockaddrType_SockaddrIn: *addrlen = sizeof (struct sockaddr_in); return 0;
case Mono_Posix_SockaddrType_SockaddrIn6: *addrlen = sizeof (struct sockaddr_in6); return 0;
default:
*addrlen = 0;
errno = EINVAL;
return -1;
}
}
int
Mono_Posix_Sockaddr_GetNativeSize (struct Mono_Posix__SockaddrHeader* address, gint64* size)
{
socklen_t value;
int r;
r = get_addrlen (address, &value);
*size = value;
return r;
}
int
Mono_Posix_FromSockaddr (struct Mono_Posix__SockaddrHeader* source, void* destination)
{
if (!source)
return 0;
switch (source->type) {
case Mono_Posix_SockaddrType_SockaddrStorage:
// Do nothing, don't copy source->sa_family into addr->sa_family
return 0;
case Mono_Posix_SockaddrType_SockaddrUn:
memcpy (((struct sockaddr_un*) destination)->sun_path, ((struct Mono_Posix__SockaddrDynamic*) source)->data, ((struct Mono_Posix__SockaddrDynamic*) source)->len);
break;
case Mono_Posix_SockaddrType_Sockaddr:
break;
case Mono_Posix_SockaddrType_SockaddrIn:
if (Mono_Posix_FromSockaddrIn ((struct Mono_Posix_SockaddrIn*) source, (struct sockaddr_in*) destination) != 0)
return -1;
break;
case Mono_Posix_SockaddrType_SockaddrIn6:
if (Mono_Posix_FromSockaddrIn6 ((struct Mono_Posix_SockaddrIn6*) source, (struct sockaddr_in6*) destination) != 0)
return -1;
break;
default:
errno = EINVAL;
return -1;
}
int family;
if (Mono_Posix_FromUnixAddressFamily (source->sa_family, &family) != 0)
return -1;
((struct sockaddr*) destination)->sa_family = family;
return 0;
}
int
Mono_Posix_ToSockaddr (void* source, gint64 size, struct Mono_Posix__SockaddrHeader* destination)
{
struct Mono_Posix__SockaddrDynamic* destination_dyn;
if (!destination)
return 0;
switch (destination->type) {
case Mono_Posix_SockaddrType_Sockaddr:
if (size < offsetof (struct sockaddr, sa_family) + sizeof (sa_family_t)) {
errno = ENOBUFS;
return -1;
}
break;
case Mono_Posix_SockaddrType_SockaddrStorage:
destination_dyn = ((struct Mono_Posix__SockaddrDynamic*) destination);
if (size > destination_dyn->len) {
errno = ENOBUFS;
return -1;
}
destination_dyn->len = size;
break;
case Mono_Posix_SockaddrType_SockaddrUn:
destination_dyn = ((struct Mono_Posix__SockaddrDynamic*) destination);
if (size - offsetof (struct sockaddr_un, sun_path) > destination_dyn->len) {
errno = ENOBUFS;
return -1;
}
destination_dyn->len = size - offsetof (struct sockaddr_un, sun_path);
memcpy (destination_dyn->data, ((struct sockaddr_un*) source)->sun_path, size);
break;
case Mono_Posix_SockaddrType_SockaddrIn:
if (size != sizeof (struct sockaddr_in)) {
errno = ENOBUFS;
return -1;
}
if (Mono_Posix_ToSockaddrIn ((struct sockaddr_in*) source, (struct Mono_Posix_SockaddrIn*) destination) != 0)
return -1;
break;
case Mono_Posix_SockaddrType_SockaddrIn6:
if (size != sizeof (struct sockaddr_in6)) {
errno = ENOBUFS;
return -1;
}
if (Mono_Posix_ToSockaddrIn6 ((struct sockaddr_in6*) source, (struct Mono_Posix_SockaddrIn6*) destination) != 0)
return -1;
break;
default:
errno = EINVAL;
return -1;
}
if (Mono_Posix_ToUnixAddressFamily (((struct sockaddr*) source)->sa_family, &destination->sa_family) != 0)
destination->sa_family = Mono_Posix_UnixAddressFamily_Unknown;
return 0;
}
// Macro for allocating space for the native sockaddr_* structure
// Must be a macro because it is using alloca()
#define ALLOC_SOCKADDR \
socklen_t addrlen; \
struct sockaddr* addr; \
gboolean need_free = 0; \
\
if (get_addrlen (address, &addrlen) != 0) \
return -1; \
if (address == NULL) { \
addr = NULL; \
} else if (address->type == Mono_Posix_SockaddrType_SockaddrStorage) { \
addr = (struct sockaddr*) ((struct Mono_Posix__SockaddrDynamic*) address)->data; \
} else if (address->type == Mono_Posix_SockaddrType_SockaddrUn) { \
/* Use alloca() for up to 2048 bytes, use malloc() otherwise */ \
need_free = addrlen > 2048; \
addr = need_free ? malloc (addrlen) : alloca (addrlen); \
if (!addr) \
return -1; \
} else { \
addr = alloca (addrlen); \
}
int
Mono_Posix_Syscall_bind (int socket, struct Mono_Posix__SockaddrHeader* address)
{
int r;
ALLOC_SOCKADDR
if (Mono_Posix_FromSockaddr (address, addr) != 0) {
if (need_free)
free (addr);
return -1;
}
r = bind (socket, addr, addrlen);
if (need_free)
free (addr);
return r;
}
int
Mono_Posix_Syscall_connect (int socket, struct Mono_Posix__SockaddrHeader* address)
{
int r;
ALLOC_SOCKADDR
if (Mono_Posix_FromSockaddr (address, addr) != 0) {
if (need_free)
free (addr);
return -1;
}
r = connect (socket, addr, addrlen);
if (need_free)
free (addr);
return r;
}
int
Mono_Posix_Syscall_accept (int socket, struct Mono_Posix__SockaddrHeader* address)
{
int r;
ALLOC_SOCKADDR
r = accept (socket, addr, &addrlen);
if (r != -1 && Mono_Posix_ToSockaddr (addr, addrlen, address) != 0) {
close (r);
r = -1;
}
if (need_free)
free (addr);
return r;
}
#ifdef HAVE_ACCEPT4
int
Mono_Posix_Syscall_accept4 (int socket, struct Mono_Posix__SockaddrHeader* address, int flags)
{
int r;
ALLOC_SOCKADDR
r = accept4 (socket, addr, &addrlen, flags);
if (r != -1 && Mono_Posix_ToSockaddr (addr, addrlen, address) != 0) {
close (r);
r = -1;
}
if (need_free)
free (addr);
return r;
}
#endif
int
Mono_Posix_Syscall_getpeername (int socket, struct Mono_Posix__SockaddrHeader* address)
{
int r;
ALLOC_SOCKADDR
r = getpeername (socket, addr, &addrlen);
if (r != -1 && Mono_Posix_ToSockaddr (addr, addrlen, address) != 0)
r = -1;
if (need_free)
free (addr);
return r;
}
int
Mono_Posix_Syscall_getsockname (int socket, struct Mono_Posix__SockaddrHeader* address)
{
int r;
ALLOC_SOCKADDR
r = getsockname (socket, addr, &addrlen);
if (r != -1 && Mono_Posix_ToSockaddr (addr, addrlen, address) != 0)
r = -1;
if (need_free)
free (addr);
return r;
}
gint64
Mono_Posix_Syscall_recv (int socket, void* message, guint64 length, int flags)
{
mph_return_if_size_t_overflow (length);
return recv (socket, message, length, flags);
}
gint64
Mono_Posix_Syscall_send (int socket, void* message, guint64 length, int flags)
{
mph_return_if_size_t_overflow (length);
return send (socket, message, length, flags);
}
gint64
Mono_Posix_Syscall_recvfrom (int socket, void* buffer, guint64 length, int flags, struct Mono_Posix__SockaddrHeader* address)
{
int r;
mph_return_if_size_t_overflow (length);
ALLOC_SOCKADDR
r = recvfrom (socket, buffer, length, flags, addr, &addrlen);
if (r != -1 && Mono_Posix_ToSockaddr (addr, addrlen, address) != 0)
r = -1;
if (need_free)
free (addr);
return r;
}
gint64
Mono_Posix_Syscall_sendto (int socket, void* message, guint64 length, int flags, struct Mono_Posix__SockaddrHeader* address)
{
int r;
mph_return_if_size_t_overflow (length);
ALLOC_SOCKADDR
if (Mono_Posix_FromSockaddr (address, addr) != 0) {
if (need_free)
free (addr);
return -1;
}
r = sendto (socket, message, length, flags, addr, addrlen);
if (need_free)
free (addr);
return r;
}
gint64
Mono_Posix_Syscall_recvmsg (int socket, struct Mono_Posix_Syscall__Msghdr* message, struct Mono_Posix__SockaddrHeader* address, int flags)
{
struct msghdr hdr;
int r;
ALLOC_SOCKADDR
memset (&hdr, 0, sizeof (struct msghdr));
hdr.msg_name = addr;
hdr.msg_namelen = addrlen;
hdr.msg_iovlen = message->msg_iovlen;
hdr.msg_control = message->msg_control;
hdr.msg_controllen = message->msg_controllen;
hdr.msg_iov = _mph_from_iovec_array (message->msg_iov, message->msg_iovlen);
r = recvmsg (socket, &hdr, flags);
if (r != -1 && Mono_Posix_ToSockaddr (addr, hdr.msg_namelen, address) != 0)
r = -1;
free (hdr.msg_iov);
if (need_free)
free (addr);
message->msg_controllen = hdr.msg_controllen;
message->msg_flags = hdr.msg_flags;
return r;
}
gint64
Mono_Posix_Syscall_sendmsg (int socket, struct Mono_Posix_Syscall__Msghdr* message, struct Mono_Posix__SockaddrHeader* address, int flags)
{
struct msghdr hdr;
int r;
ALLOC_SOCKADDR
if (Mono_Posix_FromSockaddr (address, addr) != 0) {
if (need_free)
free (addr);
return -1;
}
memset (&hdr, 0, sizeof (struct msghdr));
hdr.msg_name = addr;
hdr.msg_namelen = addrlen;
hdr.msg_iovlen = message->msg_iovlen;
hdr.msg_control = message->msg_control;
hdr.msg_controllen = message->msg_controllen;
hdr.msg_iov = _mph_from_iovec_array (message->msg_iov, message->msg_iovlen);
r = sendmsg (socket, &hdr, flags);
free (hdr.msg_iov);
if (need_free)
free (addr);
return r;
}
static void make_msghdr (struct msghdr* hdr, unsigned char* msg_control, gint64 msg_controllen)
{
memset (hdr, 0, sizeof (struct msghdr));
hdr->msg_control = msg_control;
hdr->msg_controllen = msg_controllen;
}
static struct cmsghdr* from_offset (unsigned char* msg_control, gint64 offset)
{
if (offset == -1)
return NULL;
return (struct cmsghdr*) (msg_control + offset);
}
static gint64 to_offset (unsigned char* msg_control, void* hdr)
{
if (!hdr)
return -1;
return ((unsigned char*) hdr) - msg_control;
}
#ifdef CMSG_FIRSTHDR
gint64
Mono_Posix_Syscall_CMSG_FIRSTHDR (unsigned char* msg_control, gint64 msg_controllen)
{
struct msghdr hdr;
make_msghdr (&hdr, msg_control, msg_controllen);
return to_offset (msg_control, CMSG_FIRSTHDR (&hdr));
}
#endif
#ifdef CMSG_NXTHDR
gint64
Mono_Posix_Syscall_CMSG_NXTHDR (unsigned char* msg_control, gint64 msg_controllen, gint64 cmsg)
{
struct msghdr hdr;
make_msghdr (&hdr, msg_control, msg_controllen);
return to_offset (msg_control, CMSG_NXTHDR (&hdr, from_offset (msg_control, cmsg)));
}
#endif
#ifdef CMSG_DATA
gint64
Mono_Posix_Syscall_CMSG_DATA (unsigned char* msg_control, gint64 msg_controllen, gint64 cmsg)
{
return to_offset (msg_control, CMSG_DATA (from_offset (msg_control, cmsg)));
}
#endif
#ifdef CMSG_ALIGN
guint64
Mono_Posix_Syscall_CMSG_ALIGN (guint64 length)
{
return CMSG_ALIGN (length);
}
#endif
#ifdef CMSG_SPACE
guint64
Mono_Posix_Syscall_CMSG_SPACE (guint64 length)
{
return CMSG_SPACE (length);
}
#endif
#ifdef CMSG_LEN
guint64
Mono_Posix_Syscall_CMSG_LEN (guint64 length)
{
return CMSG_LEN (length);
}
#endif
/*
* vim: noexpandtab
*/
// vim: noexpandtab
// Local Variables:
// tab-width: 4
// c-basic-offset: 4
// indent-tabs-mode: t
// End:
|