1/*
2Copyright (c) 2012, Broadcom Europe Ltd
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions are met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in the
11 documentation and/or other materials provided with the distribution.
12 * Neither the name of the copyright holder nor the
13 names of its contributors may be used to endorse or promote products
14 derived from this software without specific prior written permission.
15
16THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*/
27
28#include <errno.h>
29#include <unistd.h>
30
31#include "net_sockets.h"
32#include "net_sockets_priv.h"
33#include "containers/core/containers_common.h"
34
35/*****************************************************************************/
36
37/** Default maximum datagram size.
38 * This is based on the default Ethernet MTU size, less the IP and UDP headers.
39 */
40#define DEFAULT_MAXIMUM_DATAGRAM_SIZE (1500 - 20 - 8)
41
42/** Maximum socket buffer size to use. */
43#define MAXIMUM_BUFFER_SIZE 65536
44
45/*****************************************************************************/
46vc_container_net_status_t vc_container_net_private_last_error()
47{
48 switch (errno)
49 {
50 case EACCES: return VC_CONTAINER_NET_ERROR_ACCESS_DENIED;
51 case EFAULT: return VC_CONTAINER_NET_ERROR_INVALID_PARAMETER;
52 case EINVAL: return VC_CONTAINER_NET_ERROR_INVALID_PARAMETER;
53 case EMFILE: return VC_CONTAINER_NET_ERROR_TOO_BIG;
54 case EWOULDBLOCK: return VC_CONTAINER_NET_ERROR_WOULD_BLOCK;
55 case EINPROGRESS: return VC_CONTAINER_NET_ERROR_IN_PROGRESS;
56 case EALREADY: return VC_CONTAINER_NET_ERROR_IN_PROGRESS;
57 case EADDRINUSE: return VC_CONTAINER_NET_ERROR_IN_USE;
58 case EADDRNOTAVAIL: return VC_CONTAINER_NET_ERROR_INVALID_PARAMETER;
59 case ENETDOWN: return VC_CONTAINER_NET_ERROR_NETWORK;
60 case ENETUNREACH: return VC_CONTAINER_NET_ERROR_NETWORK;
61 case ENETRESET: return VC_CONTAINER_NET_ERROR_CONNECTION_LOST;
62 case ECONNABORTED: return VC_CONTAINER_NET_ERROR_CONNECTION_LOST;
63 case ECONNRESET: return VC_CONTAINER_NET_ERROR_CONNECTION_LOST;
64 case ENOBUFS: return VC_CONTAINER_NET_ERROR_NO_MEMORY;
65 case ENOTCONN: return VC_CONTAINER_NET_ERROR_NOT_CONNECTED;
66 case ESHUTDOWN: return VC_CONTAINER_NET_ERROR_CONNECTION_LOST;
67 case ETIMEDOUT: return VC_CONTAINER_NET_ERROR_TIMED_OUT;
68 case ECONNREFUSED: return VC_CONTAINER_NET_ERROR_CONNECTION_REFUSED;
69 case ELOOP: return VC_CONTAINER_NET_ERROR_INVALID_PARAMETER;
70 case ENAMETOOLONG: return VC_CONTAINER_NET_ERROR_INVALID_PARAMETER;
71 case EHOSTDOWN: return VC_CONTAINER_NET_ERROR_NETWORK;
72 case EHOSTUNREACH: return VC_CONTAINER_NET_ERROR_NETWORK;
73 case EUSERS: return VC_CONTAINER_NET_ERROR_NO_MEMORY;
74 case EDQUOT: return VC_CONTAINER_NET_ERROR_NO_MEMORY;
75 case ESTALE: return VC_CONTAINER_NET_ERROR_INVALID_SOCKET;
76
77 /* All other errors are unexpected, so just map to a general purpose error code. */
78 default:
79 return VC_CONTAINER_NET_ERROR_GENERAL;
80 }
81}
82
83/*****************************************************************************/
84vc_container_net_status_t vc_container_net_private_init()
85{
86 /* No additional initialization required */
87 return VC_CONTAINER_NET_SUCCESS;
88}
89
90/*****************************************************************************/
91void vc_container_net_private_deinit()
92{
93 /* No additional deinitialization required */
94}
95
96/*****************************************************************************/
97void vc_container_net_private_close( SOCKET_T sock )
98{
99 close(sock);
100}
101
102/*****************************************************************************/
103void vc_container_net_private_set_reusable( SOCKET_T sock, bool enable )
104{
105 int opt = enable ? 1 : 0;
106
107 (void)setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const char *)&opt, sizeof(opt));
108}
109
110/*****************************************************************************/
111size_t vc_container_net_private_maximum_datagram_size( SOCKET_T sock )
112{
113 (void)sock;
114
115 /* No easy way to determine this, just use the default. */
116 return DEFAULT_MAXIMUM_DATAGRAM_SIZE;
117}
118