1 /* Copyright (c) 2021-2021 Huawei Device Co., Ltd. All rights reserved.
2 *
3 * Redistribution and use in source and binary forms, with or without modification,
4 * are permitted provided that the following conditions are met:
5 *
6 * 1. Redistributions of source code must retain the above copyright notice, this list of
7 * conditions and the following disclaimer.
8 *
9 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
10 * of conditions and the following disclaimer in the documentation and/or other materials
11 * provided with the distribution.
12 *
13 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
14 * to endorse or promote products derived from this software without specific prior written
15 * permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #include "lwip/sockets.h"
31 #include "lwip/priv/tcpip_priv.h"
32 #include "lwip/priv/sockets_priv.h"
33 #include "lwip/prot/dhcp.h"
34 #include "lwip/dhcp.h"
35 #include "lwip/if_api.h"
36
37 #if LWIP_ENABLE_DISTRIBUTED_NET
38
39 #include "lwip/distributed_net/distributed_net.h"
40 #include "lwip/priv/sockets_priv.h"
41
42 static sys_mutex_t g_mutex = {0};
43
44 static u8_t g_is_distributed_net_enabled = 0;
45
46 static u16_t g_local_tcp_server_port = 0;
47
48 static u16_t g_local_udp_server_port = 0;
49
50 static u8_t g_is_distributed_net_socket[NUM_SOCKETS] = {0};
51
52 #if LWIP_DISTRIBUTED_NET_TRY_CONNECT
try_connect_to_local_tcp_server(u16_t tcp_port)53 static int try_connect_to_local_tcp_server(u16_t tcp_port)
54 {
55 struct sockaddr_in addr = {0};
56 (void)memset_s(&addr, sizeof(struct sockaddr_in), 0, sizeof(struct sockaddr_in));
57 INIT_SOCK_ADDR(&addr, LOCAL_SERVER_IP, tcp_port);
58
59 int sock = lwip_socket(AF_INET, SOCK_STREAM, 0);
60 if (sock < 0) {
61 return -1;
62 }
63
64 int ret = lwip_connect_internal(sock, (struct sockaddr *)&addr, sizeof(addr));
65 (void)lwip_close_internal(sock);
66 return ret;
67 }
68 #endif /* LWIP_DISTRIBUTED_NET_TRY_CONNECT */
69
set_distributed_net_socket(int sock)70 void set_distributed_net_socket(int sock)
71 {
72 int index = SOCKET_TO_INDEX(sock);
73 if (index >= 0 && index < NUM_SOCKETS) {
74 sys_mutex_lock(&g_mutex);
75 g_is_distributed_net_socket[index] = 1;
76 sys_mutex_unlock(&g_mutex);
77 }
78 }
79
reset_distributed_net_socket(int sock)80 void reset_distributed_net_socket(int sock)
81 {
82 int index = SOCKET_TO_INDEX(sock);
83 if (index >= 0 && index < NUM_SOCKETS) {
84 sys_mutex_lock(&g_mutex);
85 g_is_distributed_net_socket[index] = 0;
86 sys_mutex_unlock(&g_mutex);
87 }
88 }
89
get_local_tcp_server_port(void)90 u16_t get_local_tcp_server_port(void)
91 {
92 sys_mutex_lock(&g_mutex);
93 u16_t ret = g_local_tcp_server_port;
94 sys_mutex_unlock(&g_mutex);
95 return ret;
96 }
97
get_local_udp_server_port(void)98 u16_t get_local_udp_server_port(void)
99 {
100 sys_mutex_lock(&g_mutex);
101 u16_t ret = g_local_udp_server_port;
102 sys_mutex_unlock(&g_mutex);
103 return ret;
104 }
105
is_distributed_net_enabled(void)106 u8_t is_distributed_net_enabled(void)
107 {
108 sys_mutex_lock(&g_mutex);
109 u8_t ret = g_is_distributed_net_enabled;
110 sys_mutex_unlock(&g_mutex);
111 return ret;
112 }
113
enable_distributed_net(u16_t tcp_port,u16_t udp_port)114 int enable_distributed_net(u16_t tcp_port, u16_t udp_port)
115 {
116 LWIP_DEBUGF(SOCKETS_DEBUG, ("enable distributed_net"));
117 if (is_distributed_net_enabled()) {
118 if (get_local_tcp_server_port() == tcp_port && get_local_udp_server_port() == udp_port) {
119 return 0;
120 }
121 set_errno(EINVAL);
122 return -1;
123 }
124
125 #if LWIP_DISTRIBUTED_NET_TRY_CONNECT
126 if (try_connect_to_local_tcp_server(tcp_port) < 0) {
127 return -1;
128 }
129 #endif /* LWIP_DISTRIBUTED_NET_TRY_CONNECT */
130
131 sys_mutex_lock(&g_mutex);
132 g_is_distributed_net_enabled = 1;
133 g_local_tcp_server_port = tcp_port;
134 g_local_udp_server_port = udp_port;
135 sys_mutex_unlock(&g_mutex);
136 return 0;
137 }
138
disable_distributed_net(void)139 int disable_distributed_net(void)
140 {
141 LWIP_DEBUGF(SOCKETS_DEBUG, ("disable distributed_net"));
142 sys_mutex_lock(&g_mutex);
143 for (int i = 0; i < NUM_SOCKETS; ++i) {
144 if (g_is_distributed_net_socket[i]) {
145 (void)lwip_close_internal(INDEX_TO_SOCKET(i));
146 }
147 }
148 g_local_tcp_server_port = 0;
149 g_local_udp_server_port = 0;
150 g_is_distributed_net_enabled = 0;
151 (void)memset_s(g_is_distributed_net_socket, sizeof(g_is_distributed_net_socket), 0,
152 sizeof(g_is_distributed_net_socket));
153 sys_mutex_unlock(&g_mutex);
154 return 0;
155 }
156
157 #endif /* LWIP_ENABLE_DISTRIBUTED_NET */