-
Notifications
You must be signed in to change notification settings - Fork 215
/
os-impl-bsd-sockets.c
716 lines (640 loc) · 20.6 KB
/
os-impl-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
/*
* NASA Docket No. GSC-18,370-1, and identified as "Operating System Abstraction Layer"
*
* Copyright (c) 2019 United States Government as represented by
* the Administrator of the National Aeronautics and Space Administration.
* All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* \file os-impl-bsd-sockets.c
* \author joseph.p.hickey@nasa.gov
*
* Purpose: This file contains the network functionality for for
* systems which implement the BSD-style socket API.
*/
/****************************************************************************************
INCLUDE FILES
***************************************************************************************/
/*
* Inclusions Defined by OSAL layer.
*
* This must include whatever is required to get the prototypes of these functions:
*
* socket()
* getsockopt()
* setsockopt()
* fcntl()
* bind()
* listen()
* accept()
* connect()
* recvfrom()
* sendto()
* inet_pton()
* ntohl()/ntohs()
*
* As well as any headers for the struct sockaddr type and any address families in use
*/
#include <string.h>
#include <errno.h>
#include "os-impl-sockets.h"
#include "os-shared-file.h"
#include "os-shared-select.h"
#include "os-shared-sockets.h"
#include "os-shared-idmap.h"
/****************************************************************************************
DEFINES
****************************************************************************************/
typedef union
{
char data[OS_SOCKADDR_MAX_LEN];
struct sockaddr sa;
struct sockaddr_in sa_in;
#ifdef OS_NETWORK_SUPPORTS_IPV6
struct sockaddr_in6 sa_in6;
#endif
} OS_SockAddr_Accessor_t;
/****************************************************************************************
Sockets API
***************************************************************************************/
/*----------------------------------------------------------------
*
* Function: OS_SocketOpen_Impl
*
* Purpose: Implemented per internal OSAL API
* See prototype for argument/return detail
*
*-----------------------------------------------------------------*/
int32 OS_SocketOpen_Impl(const OS_object_token_t *token)
{
int os_domain;
int os_type;
int os_proto;
int os_flags;
OS_impl_file_internal_record_t *impl;
OS_stream_internal_record_t * stream;
impl = OS_OBJECT_TABLE_GET(OS_impl_filehandle_table, *token);
stream = OS_OBJECT_TABLE_GET(OS_stream_table, *token);
os_proto = 0;
switch (stream->socket_type)
{
case OS_SocketType_DATAGRAM:
os_type = SOCK_DGRAM;
break;
case OS_SocketType_STREAM:
os_type = SOCK_STREAM;
break;
default:
return OS_ERR_NOT_IMPLEMENTED;
}
switch (stream->socket_domain)
{
case OS_SocketDomain_INET:
os_domain = AF_INET;
break;
#ifdef OS_NETWORK_SUPPORTS_IPV6
case OS_SocketDomain_INET6:
os_domain = AF_INET6;
break;
#endif
default:
return OS_ERR_NOT_IMPLEMENTED;
}
/* Only AF_INET* at this point, can add cases if support is expanded */
switch (stream->socket_type)
{
case OS_SocketType_DATAGRAM:
os_proto = IPPROTO_UDP;
break;
case OS_SocketType_STREAM:
os_proto = IPPROTO_TCP;
break;
}
impl->fd = socket(os_domain, os_type, os_proto);
if (impl->fd < 0)
{
return OS_ERROR;
}
/*
* Setting the REUSEADDR flag helps during debugging when there might be frequent
* code restarts. However if setting the option fails then it is not worth bailing out over.
*/
os_flags = 1;
setsockopt(impl->fd, SOL_SOCKET, SO_REUSEADDR, &os_flags, sizeof(os_flags));
/*
* Set the standard options on the filehandle by default --
* this may set it to non-blocking mode if the implementation supports it.
* any blocking would be done explicitly via the select() wrappers
*/
os_flags = fcntl(impl->fd, F_GETFL);
os_flags |= OS_IMPL_SOCKET_FLAGS;
fcntl(impl->fd, F_SETFL, os_flags);
impl->selectable = ((os_flags & O_NONBLOCK) != 0);
return OS_SUCCESS;
} /* end OS_SocketOpen_Impl */
/*----------------------------------------------------------------
*
* Function: OS_SocketBind_Impl
*
* Purpose: Implemented per internal OSAL API
* See prototype for argument/return detail
*
*-----------------------------------------------------------------*/
int32 OS_SocketBind_Impl(const OS_object_token_t *token, const OS_SockAddr_t *Addr)
{
int os_result;
socklen_t addrlen;
const struct sockaddr * sa;
OS_impl_file_internal_record_t *impl;
OS_stream_internal_record_t * stream;
impl = OS_OBJECT_TABLE_GET(OS_impl_filehandle_table, *token);
stream = OS_OBJECT_TABLE_GET(OS_stream_table, *token);
sa = (const struct sockaddr *)&Addr->AddrData;
switch (sa->sa_family)
{
case AF_INET:
addrlen = sizeof(struct sockaddr_in);
break;
#ifdef OS_NETWORK_SUPPORTS_IPV6
case AF_INET6:
addrlen = sizeof(struct sockaddr_in6);
break;
#endif
default:
addrlen = 0;
break;
}
if (addrlen == 0 || addrlen > OS_SOCKADDR_MAX_LEN)
{
return OS_ERR_BAD_ADDRESS;
}
os_result = bind(impl->fd, sa, addrlen);
if (os_result < 0)
{
OS_DEBUG("bind: %s\n", strerror(errno));
return OS_ERROR;
}
/* Start listening on the socket (implied for stream sockets) */
if (stream->socket_type == OS_SocketType_STREAM)
{
os_result = listen(impl->fd, 10);
if (os_result < 0)
{
OS_DEBUG("listen: %s\n", strerror(errno));
return OS_ERROR;
}
}
return OS_SUCCESS;
} /* end OS_SocketBind_Impl */
/*----------------------------------------------------------------
*
* Function: OS_SocketConnect_Impl
*
* Purpose: Implemented per internal OSAL API
* See prototype for argument/return detail
*
*-----------------------------------------------------------------*/
int32 OS_SocketConnect_Impl(const OS_object_token_t *token, const OS_SockAddr_t *Addr, int32 timeout)
{
int32 return_code;
int os_status;
int sockopt;
socklen_t slen;
uint32 operation;
const struct sockaddr * sa;
OS_impl_file_internal_record_t *impl;
impl = OS_OBJECT_TABLE_GET(OS_impl_filehandle_table, *token);
sa = (const struct sockaddr *)&Addr->AddrData;
switch (sa->sa_family)
{
case AF_INET:
slen = sizeof(struct sockaddr_in);
break;
#ifdef OS_NETWORK_SUPPORTS_IPV6
case AF_INET6:
slen = sizeof(struct sockaddr_in6);
break;
#endif
default:
slen = 0;
break;
}
if (slen != Addr->ActualLength)
{
return_code = OS_ERR_BAD_ADDRESS;
}
else
{
return_code = OS_SUCCESS;
os_status = connect(impl->fd, sa, slen);
if (os_status < 0)
{
if (errno != EINPROGRESS)
{
return_code = OS_ERROR;
}
else
{
operation = OS_STREAM_STATE_WRITABLE;
if (impl->selectable)
{
return_code = OS_SelectSingle_Impl(token, &operation, timeout);
}
if (return_code == OS_SUCCESS)
{
if ((operation & OS_STREAM_STATE_WRITABLE) == 0)
{
return_code = OS_ERROR_TIMEOUT;
}
else
{
sockopt = 0;
slen = sizeof(sockopt);
os_status = getsockopt(impl->fd, SOL_SOCKET, SO_ERROR, &sockopt, &slen);
if (os_status < 0 || sockopt != 0)
{
return_code = OS_ERROR;
}
}
}
}
}
}
return return_code;
} /* end OS_SocketConnect_Impl */
/*----------------------------------------------------------------
*
* Function: OS_SocketAccept_Impl
*
* Purpose: Implemented per internal OSAL API
* See prototype for argument/return detail
*
*-----------------------------------------------------------------*/
int32 OS_SocketAccept_Impl(const OS_object_token_t *sock_token, const OS_object_token_t *conn_token,
OS_SockAddr_t *Addr, int32 timeout)
{
int32 return_code;
uint32 operation;
socklen_t addrlen;
int os_flags;
OS_impl_file_internal_record_t *sock_impl;
OS_impl_file_internal_record_t *conn_impl;
sock_impl = OS_OBJECT_TABLE_GET(OS_impl_filehandle_table, *sock_token);
conn_impl = OS_OBJECT_TABLE_GET(OS_impl_filehandle_table, *conn_token);
operation = OS_STREAM_STATE_READABLE;
if (sock_impl->selectable)
{
return_code = OS_SelectSingle_Impl(sock_token, &operation, timeout);
}
else
{
return_code = OS_SUCCESS;
}
if (return_code == OS_SUCCESS)
{
if ((operation & OS_STREAM_STATE_READABLE) == 0)
{
return_code = OS_ERROR_TIMEOUT;
}
else
{
addrlen = Addr->ActualLength;
conn_impl->fd = accept(sock_impl->fd, (struct sockaddr *)&Addr->AddrData, &addrlen);
if (conn_impl->fd < 0)
{
return_code = OS_ERROR;
}
else
{
Addr->ActualLength = addrlen;
/*
* Set the standard options on the filehandle by default --
* this may set it to non-blocking mode if the implementation supports it.
* any blocking would be done explicitly via the select() wrappers
*/
os_flags = fcntl(conn_impl->fd, F_GETFL);
os_flags |= OS_IMPL_SOCKET_FLAGS;
fcntl(conn_impl->fd, F_SETFL, os_flags);
conn_impl->selectable = ((os_flags & O_NONBLOCK) != 0);
}
}
}
return return_code;
} /* end OS_SocketAccept_Impl */
/*----------------------------------------------------------------
*
* Function: OS_SocketRecvFrom_Impl
*
* Purpose: Implemented per internal OSAL API
* See prototype for argument/return detail
*
*-----------------------------------------------------------------*/
int32 OS_SocketRecvFrom_Impl(const OS_object_token_t *token, void *buffer, size_t buflen, OS_SockAddr_t *RemoteAddr,
int32 timeout)
{
int32 return_code;
int os_result;
int waitflags;
uint32 operation;
struct sockaddr * sa;
socklen_t addrlen;
OS_impl_file_internal_record_t *impl;
impl = OS_OBJECT_TABLE_GET(OS_impl_filehandle_table, *token);
if (RemoteAddr == NULL)
{
sa = NULL;
addrlen = 0;
}
else
{
addrlen = OS_SOCKADDR_MAX_LEN;
sa = (struct sockaddr *)&RemoteAddr->AddrData;
}
operation = OS_STREAM_STATE_READABLE;
/*
* If "O_NONBLOCK" flag is set then use select()
* Note this is the only way to get a correct timeout
*/
if (impl->selectable)
{
waitflags = MSG_DONTWAIT;
return_code = OS_SelectSingle_Impl(token, &operation, timeout);
}
else
{
if (timeout == 0)
{
waitflags = MSG_DONTWAIT;
}
else
{
/* note timeout will not be honored if >0 */
waitflags = 0;
}
return_code = OS_SUCCESS;
}
if (return_code == OS_SUCCESS)
{
if ((operation & OS_STREAM_STATE_READABLE) == 0)
{
return_code = OS_ERROR_TIMEOUT;
}
else
{
os_result = recvfrom(impl->fd, buffer, buflen, waitflags, sa, &addrlen);
if (os_result < 0)
{
if (errno == EAGAIN || errno == EWOULDBLOCK)
{
return_code = OS_QUEUE_EMPTY;
}
else
{
OS_DEBUG("recvfrom: %s\n", strerror(errno));
return_code = OS_ERROR;
}
}
else
{
return_code = os_result;
if (RemoteAddr != NULL)
{
RemoteAddr->ActualLength = addrlen;
}
}
}
}
return return_code;
} /* end OS_SocketRecvFrom_Impl */
/*----------------------------------------------------------------
*
* Function: OS_SocketSendTo_Impl
*
* Purpose: Implemented per internal OSAL API
* See prototype for argument/return detail
*
*-----------------------------------------------------------------*/
int32 OS_SocketSendTo_Impl(const OS_object_token_t *token, const void *buffer, size_t buflen,
const OS_SockAddr_t *RemoteAddr)
{
int os_result;
socklen_t addrlen;
const struct sockaddr * sa;
OS_impl_file_internal_record_t *impl;
impl = OS_OBJECT_TABLE_GET(OS_impl_filehandle_table, *token);
sa = (const struct sockaddr *)&RemoteAddr->AddrData;
switch (sa->sa_family)
{
case AF_INET:
addrlen = sizeof(struct sockaddr_in);
break;
#ifdef OS_NETWORK_SUPPORTS_IPV6
case AF_INET6:
addrlen = sizeof(struct sockaddr_in6);
break;
#endif
default:
addrlen = 0;
break;
}
if (addrlen != RemoteAddr->ActualLength)
{
return OS_ERR_BAD_ADDRESS;
}
os_result = sendto(impl->fd, buffer, buflen, MSG_DONTWAIT, sa, addrlen);
if (os_result < 0)
{
OS_DEBUG("sendto: %s\n", strerror(errno));
return OS_ERROR;
}
return os_result;
} /* end OS_SocketSendTo_Impl */
/*----------------------------------------------------------------
*
* Function: OS_SocketGetInfo_Impl
*
* Purpose: Implemented per internal OSAL API
* See prototype for argument/return detail
*
*-----------------------------------------------------------------*/
int32 OS_SocketGetInfo_Impl(const OS_object_token_t *token, OS_socket_prop_t *sock_prop)
{
return OS_SUCCESS;
} /* end OS_SocketGetInfo_Impl */
/*----------------------------------------------------------------
*
* Function: OS_SocketAddrInit_Impl
*
* Purpose: Implemented per internal OSAL API
* See prototype for argument/return detail
*
*-----------------------------------------------------------------*/
int32 OS_SocketAddrInit_Impl(OS_SockAddr_t *Addr, OS_SocketDomain_t Domain)
{
sa_family_t sa_family;
socklen_t addrlen;
OS_SockAddr_Accessor_t *Accessor;
memset(Addr, 0, sizeof(OS_SockAddr_t));
Accessor = (OS_SockAddr_Accessor_t *)&Addr->AddrData;
switch (Domain)
{
case OS_SocketDomain_INET:
sa_family = AF_INET;
addrlen = sizeof(struct sockaddr_in);
break;
#ifdef OS_NETWORK_SUPPORTS_IPV6
case OS_SocketDomain_INET6:
sa_family = AF_INET6;
addrlen = sizeof(struct sockaddr_in6);
break;
#endif
default:
sa_family = 0;
addrlen = 0;
break;
}
if (addrlen == 0 || addrlen > OS_SOCKADDR_MAX_LEN)
{
return OS_ERR_NOT_IMPLEMENTED;
}
Addr->ActualLength = OSAL_SIZE_C(addrlen);
Accessor->sa.sa_family = sa_family;
return OS_SUCCESS;
} /* end OS_SocketAddrInit_Impl */
/*----------------------------------------------------------------
*
* Function: OS_SocketAddrToString_Impl
*
* Purpose: Implemented per internal OSAL API
* See prototype for argument/return detail
*
*-----------------------------------------------------------------*/
int32 OS_SocketAddrToString_Impl(char *buffer, size_t buflen, const OS_SockAddr_t *Addr)
{
const void * addrbuffer;
const OS_SockAddr_Accessor_t *Accessor;
Accessor = (const OS_SockAddr_Accessor_t *)&Addr->AddrData;
switch (Accessor->sa.sa_family)
{
case AF_INET:
addrbuffer = &Accessor->sa_in.sin_addr;
break;
#ifdef OS_NETWORK_SUPPORTS_IPV6
case AF_INET6:
addrbuffer = &Accessor->sa_in6.sin6_addr;
break;
#endif
default:
return OS_ERR_BAD_ADDRESS;
break;
}
if (inet_ntop(Accessor->sa.sa_family, addrbuffer, buffer, buflen) == NULL)
{
return OS_ERROR;
}
return OS_SUCCESS;
} /* end OS_SocketAddrToString_Impl */
/*----------------------------------------------------------------
*
* Function: OS_SocketAddrFromString_Impl
*
* Purpose: Implemented per internal OSAL API
* See prototype for argument/return detail
*
*-----------------------------------------------------------------*/
int32 OS_SocketAddrFromString_Impl(OS_SockAddr_t *Addr, const char *string)
{
void * addrbuffer;
OS_SockAddr_Accessor_t *Accessor;
Accessor = (OS_SockAddr_Accessor_t *)&Addr->AddrData;
switch (Accessor->sa.sa_family)
{
case AF_INET:
addrbuffer = &Accessor->sa_in.sin_addr;
break;
#ifdef OS_NETWORK_SUPPORTS_IPV6
case AF_INET6:
addrbuffer = &Accessor->sa_in6.sin6_addr;
break;
#endif
default:
return OS_ERR_BAD_ADDRESS;
break;
}
if (inet_pton(Accessor->sa.sa_family, string, addrbuffer) < 0)
{
return OS_ERROR;
}
return OS_SUCCESS;
} /* end OS_SocketAddrFromString_Impl */
/*----------------------------------------------------------------
*
* Function: OS_SocketAddrGetPort_Impl
*
* Purpose: Implemented per internal OSAL API
* See prototype for argument/return detail
*
*-----------------------------------------------------------------*/
int32 OS_SocketAddrGetPort_Impl(uint16 *PortNum, const OS_SockAddr_t *Addr)
{
in_port_t sa_port;
const OS_SockAddr_Accessor_t *Accessor;
Accessor = (const OS_SockAddr_Accessor_t *)&Addr->AddrData;
switch (Accessor->sa.sa_family)
{
case AF_INET:
sa_port = Accessor->sa_in.sin_port;
break;
#ifdef OS_NETWORK_SUPPORTS_IPV6
case AF_INET6:
sa_port = Accessor->sa_in6.sin6_port;
break;
#endif
default:
return OS_ERR_BAD_ADDRESS;
break;
}
*PortNum = ntohs(sa_port);
return OS_SUCCESS;
} /* end OS_SocketAddrGetPort_Impl */
/*----------------------------------------------------------------
*
* Function: OS_SocketAddrSetPort_Impl
*
* Purpose: Implemented per internal OSAL API
* See prototype for argument/return detail
*
*-----------------------------------------------------------------*/
int32 OS_SocketAddrSetPort_Impl(OS_SockAddr_t *Addr, uint16 PortNum)
{
in_port_t sa_port;
OS_SockAddr_Accessor_t *Accessor;
sa_port = htons(PortNum);
Accessor = (OS_SockAddr_Accessor_t *)&Addr->AddrData;
switch (Accessor->sa.sa_family)
{
case AF_INET:
Accessor->sa_in.sin_port = sa_port;
break;
#ifdef OS_NETWORK_SUPPORTS_IPV6
case AF_INET6:
Accessor->sa_in6.sin6_port = sa_port;
break;
#endif
default:
return OS_ERR_BAD_ADDRESS;
}
return OS_SUCCESS;
} /* end OS_SocketAddrSetPort_Impl */