Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
pal_network.c
00001 /* 00002 * Copyright (c) 2016 ARM Limited. All rights reserved. 00003 * SPDX-License-Identifier: Apache-2.0 00004 * Licensed under the Apache License, Version 2.0 (the License); you may 00005 * not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an AS IS BASIS, WITHOUT 00012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 00018 #include "pal.h" 00019 #include "pal_network.h" 00020 #include "pal_plat_network.h" 00021 00022 typedef struct pal_in_addr { 00023 uint32_t s_addr; // that's a 32-bit int (4 bytes) 00024 } pal_in_addr_t; 00025 00026 typedef struct pal_socketAddressInternal { 00027 short int pal_sin_family; // address family 00028 unsigned short int pal_sin_port; // port 00029 pal_in_addr_t pal_sin_addr; // ipv4 address 00030 unsigned char pal_sin_zero[8]; // 00031 } pal_socketAddressInternal_t; 00032 00033 typedef struct pal_socketAddressInternal6{ 00034 uint16_t pal_sin6_family; // address family, 00035 uint16_t pal_sin6_port; // port number, Network Byte Order 00036 uint32_t pal_sin6_flowinfo; // IPv6 flow information 00037 palIpV6Addr_t pal_sin6_addr; // IPv6 address 00038 uint32_t pal_sin6_scope_id; // Scope ID 00039 } pal_socketAddressInternal6_t; 00040 00041 00042 00043 palStatus_t pal_registerNetworkInterface(void* networkInterfaceContext, uint32_t* interfaceIndex) 00044 { 00045 palStatus_t result = PAL_SUCCESS; 00046 result = pal_plat_RegisterNetworkInterface(networkInterfaceContext, interfaceIndex); 00047 return result; 00048 } 00049 00050 00051 palStatus_t pal_setSockAddrPort(palSocketAddress_t* address, uint16_t port) 00052 { 00053 palStatus_t result = PAL_SUCCESS; 00054 if (NULL == address) 00055 { 00056 return PAL_ERR_RTOS_PARAMETER; 00057 } 00058 00059 if (address->addressType == PAL_AF_INET) 00060 { 00061 pal_socketAddressInternal_t* innerAddr = (pal_socketAddressInternal_t*)address; 00062 innerAddr->pal_sin_port = port; 00063 } 00064 else if (address->addressType == PAL_AF_INET6) 00065 { 00066 pal_socketAddressInternal6_t * innerAddr = (pal_socketAddressInternal6_t*)address; 00067 innerAddr->pal_sin6_port = port; 00068 } 00069 else 00070 { 00071 result = PAL_ERR_SOCKET_INVALID_ADDRESS_FAMILY; 00072 } 00073 00074 return result; 00075 } 00076 00077 00078 palStatus_t pal_setSockAddrIPV4Addr(palSocketAddress_t* address, palIpV4Addr_t ipV4Addr) 00079 { 00080 if ((NULL == address) || (NULL == ipV4Addr)) 00081 { 00082 return PAL_ERR_RTOS_PARAMETER; 00083 } 00084 pal_socketAddressInternal_t* innerAddr = (pal_socketAddressInternal_t*)address; 00085 innerAddr->pal_sin_family = PAL_AF_INET; 00086 innerAddr->pal_sin_addr.s_addr = (ipV4Addr[0]) | (ipV4Addr[1] << 8) | (ipV4Addr[2] << 16) | (ipV4Addr[3] << 24); 00087 return PAL_SUCCESS; 00088 } 00089 00090 00091 palStatus_t pal_setSockAddrIPV6Addr(palSocketAddress_t* address, palIpV6Addr_t ipV6Addr) 00092 { 00093 int index; 00094 00095 if ((NULL == address) || (NULL == ipV6Addr)) 00096 { 00097 return PAL_ERR_RTOS_PARAMETER; 00098 } 00099 pal_socketAddressInternal6_t* innerAddr = (pal_socketAddressInternal6_t*)address; 00100 innerAddr->pal_sin6_family = PAL_AF_INET6; 00101 for (index = 0; index < PAL_IPV6_ADDRESS_SIZE; index++) // TODO: use mem copy? 00102 { 00103 innerAddr->pal_sin6_addr[index] = ipV6Addr[index]; 00104 } 00105 return PAL_SUCCESS; 00106 } 00107 00108 00109 palStatus_t pal_getSockAddrIPV4Addr(const palSocketAddress_t* address, palIpV4Addr_t ipV4Addr) 00110 { 00111 palStatus_t result = PAL_SUCCESS; 00112 if (NULL == address) 00113 { 00114 return PAL_ERR_RTOS_PARAMETER; 00115 } 00116 if (address->addressType == PAL_AF_INET) 00117 { 00118 pal_socketAddressInternal_t* innerAddr = (pal_socketAddressInternal_t*)address; 00119 ipV4Addr[0] = (innerAddr->pal_sin_addr.s_addr) & 0xFF; 00120 ipV4Addr[1] = (innerAddr->pal_sin_addr.s_addr >> 8) & 0xFF; 00121 ipV4Addr[2] = (innerAddr->pal_sin_addr.s_addr >> 16) & 0xFF; 00122 ipV4Addr[3] = (innerAddr->pal_sin_addr.s_addr >> 24) & 0xFF; 00123 00124 } 00125 else 00126 { 00127 result = PAL_ERR_SOCKET_INVALID_ADDRESS_FAMILY; 00128 } 00129 return result; 00130 } 00131 00132 00133 palStatus_t pal_getSockAddrIPV6Addr(const palSocketAddress_t* address, palIpV6Addr_t ipV6Addr) 00134 { 00135 palStatus_t result = PAL_SUCCESS; 00136 int index = 0; 00137 if (address->addressType == PAL_AF_INET6) 00138 { 00139 pal_socketAddressInternal6_t * innerAddr = (pal_socketAddressInternal6_t*)address; 00140 for (index = 0; index < PAL_IPV6_ADDRESS_SIZE; index++) // TODO: use mem copy? 00141 { 00142 ipV6Addr[index] = innerAddr->pal_sin6_addr[index]; 00143 } 00144 } 00145 else 00146 { 00147 result = PAL_ERR_SOCKET_INVALID_ADDRESS_FAMILY; 00148 } 00149 return result; 00150 } 00151 00152 00153 palStatus_t pal_getSockAddrPort(const palSocketAddress_t* address, uint16_t* port) 00154 { 00155 palStatus_t result = PAL_SUCCESS; 00156 if ((NULL == address) || (NULL == port)) 00157 { 00158 return PAL_ERR_RTOS_PARAMETER; 00159 } 00160 00161 if (address->addressType == PAL_AF_INET) 00162 { 00163 pal_socketAddressInternal_t* innerAddr = (pal_socketAddressInternal_t*)address; 00164 *port = innerAddr->pal_sin_port; 00165 } 00166 else if (address->addressType == PAL_AF_INET6) 00167 { 00168 pal_socketAddressInternal6_t * innerAddr = (pal_socketAddressInternal6_t*)address; 00169 *port = innerAddr->pal_sin6_port; 00170 } 00171 else 00172 { 00173 result = PAL_ERR_SOCKET_INVALID_ADDRESS_FAMILY; 00174 } 00175 00176 return result; 00177 } 00178 00179 00180 palStatus_t pal_socket(palSocketDomain_t domain, palSocketType_t type, bool nonBlockingSocket, uint32_t interfaceNum, palSocket_t* socket) 00181 { 00182 palStatus_t result = PAL_SUCCESS; 00183 if (NULL == socket) 00184 { 00185 return PAL_ERR_RTOS_PARAMETER; 00186 } 00187 result = pal_plat_socket(domain, type, nonBlockingSocket, interfaceNum, socket); 00188 return result; // TODO(nirson01) ADD debug print for error propagation(once debug print infrastructure is finalized) 00189 } 00190 00191 00192 palStatus_t pal_getSocketOptions(palSocket_t socket, palSocketOptionName_t optionName, void* optionValue, palSocketLength_t* optionLength) 00193 { 00194 palStatus_t result = PAL_SUCCESS; 00195 if ((NULL == optionValue) || (NULL == optionLength)) 00196 { 00197 return PAL_ERR_RTOS_PARAMETER; 00198 } 00199 result = pal_plat_getSocketOptions(socket, optionName, optionValue, optionLength); 00200 return result; // TODO(nirson01) ADD debug print for error propagation(once debug print infrastructure is finalized) 00201 } 00202 00203 00204 palStatus_t pal_setSocketOptions(palSocket_t socket, int optionName, const void* optionValue, palSocketLength_t optionLength) 00205 { 00206 palStatus_t result = PAL_SUCCESS; 00207 if (NULL == optionValue) 00208 { 00209 return PAL_ERR_RTOS_PARAMETER; 00210 } 00211 result = pal_plat_setSocketOptions( socket, optionName, optionValue, optionLength); 00212 return result; // TODO(nirson01) ADD debug print for error propagation(once debug print infrastructure is finalized) 00213 } 00214 00215 00216 palStatus_t pal_bind(palSocket_t socket, palSocketAddress_t* myAddress, palSocketLength_t addressLength) 00217 { 00218 palStatus_t result = PAL_SUCCESS; 00219 if (NULL == myAddress) 00220 { 00221 return PAL_ERR_RTOS_PARAMETER; 00222 } 00223 result = pal_plat_bind(socket, myAddress, addressLength); 00224 return result; // TODO(nirson01) ADD debug print for error propagation(once debug print infrastructure is finalized) 00225 } 00226 00227 00228 palStatus_t pal_receiveFrom(palSocket_t socket, void* buffer, size_t length, palSocketAddress_t* from, palSocketLength_t* fromLength, size_t* bytesReceived) 00229 { 00230 palStatus_t result = PAL_SUCCESS; 00231 if ((NULL == buffer) || (NULL == bytesReceived)) 00232 { 00233 return PAL_ERR_RTOS_PARAMETER; 00234 } 00235 result = pal_plat_receiveFrom(socket, buffer, length, from, fromLength, bytesReceived); 00236 return result; // TODO(nirson01) ADD debug print for error propagation(once debug print infrastructure is finalized) 00237 } 00238 00239 00240 palStatus_t pal_sendTo(palSocket_t socket, const void* buffer, size_t length, const palSocketAddress_t* to, palSocketLength_t toLength, size_t* bytesSent) 00241 { 00242 palStatus_t result = PAL_SUCCESS; 00243 if ((NULL == buffer) || (NULL == bytesSent) || (NULL == to)) 00244 { 00245 return PAL_ERR_RTOS_PARAMETER; 00246 } 00247 result = pal_plat_sendTo(socket, buffer, length, to, toLength, bytesSent); 00248 return result; // TODO(nirson01) ADD debug print for error propagation(once debug print infrastructure is finalized) 00249 } 00250 00251 00252 palStatus_t pal_close(palSocket_t* socket) 00253 { 00254 palStatus_t result = PAL_SUCCESS; 00255 if (NULL == socket ) 00256 { 00257 return PAL_ERR_RTOS_PARAMETER; 00258 } 00259 result = pal_plat_close(socket); 00260 return result; // TODO(nirson01) ADD debug print for error propagation(once debug print infrastructure is finalized) 00261 } 00262 00263 00264 palStatus_t pal_getNumberOfNetInterfaces( uint32_t* numInterfaces) 00265 { 00266 palStatus_t result = PAL_SUCCESS; 00267 if (NULL == numInterfaces) 00268 { 00269 return PAL_ERR_RTOS_PARAMETER; 00270 } 00271 result = pal_plat_getNumberOfNetInterfaces(numInterfaces); 00272 return result; // TODO(nirson01) ADD debug print for error propagation(once debug print infrastructure is finalized) 00273 } 00274 00275 00276 palStatus_t pal_getNetInterfaceInfo(uint32_t interfaceNum, palNetInterfaceInfo_t * interfaceInfo) 00277 { 00278 palStatus_t result = PAL_SUCCESS; 00279 00280 if (NULL == interfaceInfo) 00281 { 00282 return PAL_ERR_RTOS_PARAMETER; 00283 } 00284 result = pal_plat_getNetInterfaceInfo(interfaceNum, interfaceInfo); 00285 return result; // TODO(nirson01) ADD debug print for error propagation(once debug print infrastructure is finalized) 00286 } 00287 00288 00289 palStatus_t pal_socketMiniSelect(const palSocket_t socketsToCheck[PAL_NET_SOCKET_SELECT_MAX_SOCKETS], uint32_t numberOfSockets, 00290 pal_timeVal_t* timeout, uint8_t palSocketStatus[PAL_NET_SOCKET_SELECT_MAX_SOCKETS], uint32_t * numberOfSocketsSet) 00291 { 00292 palStatus_t result = PAL_SUCCESS; 00293 if ((NULL == socketsToCheck) || (NULL == numberOfSocketsSet) || (PAL_NET_SOCKET_SELECT_MAX_SOCKETS < numberOfSockets)) 00294 { 00295 return PAL_ERR_RTOS_PARAMETER; 00296 } 00297 result = pal_plat_socketMiniSelect(socketsToCheck, numberOfSockets, timeout, palSocketStatus, numberOfSocketsSet); 00298 return result; // TODO(nirson01) ADD debug print for error propagation(once debug print infrastructure is finalized) 00299 } 00300 00301 00302 #if PAL_NET_TCP_AND_TLS_SUPPORT // functionality below supported only in case TCP is supported. 00303 00304 palStatus_t pal_listen(palSocket_t socket, int backlog) 00305 { 00306 palStatus_t result = PAL_SUCCESS; 00307 result = pal_plat_listen(socket, backlog); 00308 return result; // TODO(nirson01) ADD debug print for error propagation(once debug print infrastructure is finalized) 00309 } 00310 00311 00312 palStatus_t pal_accept(palSocket_t socket, palSocketAddress_t* address, palSocketLength_t* addressLen, palSocket_t* acceptedSocket) 00313 { 00314 palStatus_t result = PAL_SUCCESS; 00315 if ((NULL == acceptedSocket) || (NULL == address)|| (NULL == addressLen)) 00316 { 00317 return PAL_ERR_RTOS_PARAMETER; 00318 } 00319 result = pal_plat_accept(socket, address, addressLen, acceptedSocket); 00320 return result; // TODO(nirson01) ADD debug print for error propagation(once debug print infrastructure is finalized) 00321 } 00322 00323 00324 palStatus_t pal_connect(palSocket_t socket, const palSocketAddress_t* address, palSocketLength_t addressLen) 00325 { 00326 palStatus_t result = PAL_SUCCESS; 00327 if (NULL == address) 00328 { 00329 return PAL_ERR_RTOS_PARAMETER; 00330 } 00331 result = pal_plat_connect( socket, address, addressLen); 00332 return result; // TODO(nirson01) ADD debug print for error propagation(once debug print infrastructure is finalized) 00333 } 00334 00335 00336 palStatus_t pal_recv(palSocket_t socket, void* buf, size_t len, size_t* recievedDataSize) 00337 { 00338 palStatus_t result = PAL_SUCCESS; 00339 if ((NULL == recievedDataSize) || (NULL == recievedDataSize)) 00340 { 00341 return PAL_ERR_RTOS_PARAMETER; 00342 } 00343 result = pal_plat_recv(socket, buf, len, recievedDataSize); 00344 return result; // TODO(nirson01) ADD debug print for error propagation(once debug print infrastructure is finalized) 00345 } 00346 00347 00348 palStatus_t pal_send(palSocket_t socket, const void* buf, size_t len, size_t* sentDataSize) 00349 { 00350 palStatus_t result = PAL_SUCCESS; 00351 if ((NULL == buf) || (NULL == sentDataSize)) 00352 { 00353 return PAL_ERR_RTOS_PARAMETER; 00354 } 00355 result = pal_plat_send( socket, buf, len, sentDataSize); 00356 return result; // TODO(nirson01) ADD debug print for error propagation(once debug print infrastructure is finalized) 00357 } 00358 00359 00360 #endif //PAL_NET_TCP_AND_TLS_SUPPORT 00361 00362 00363 #if PAL_NET_ASYNCHRONOUS_SOCKET_API 00364 00365 palStatus_t pal_asynchronousSocket(palSocketDomain_t domain, palSocketType_t type, bool nonBlockingSocket, uint32_t interfaceNum, palAsyncSocketCallback_t callback, palSocket_t* socket) 00366 { 00367 palStatus_t result = PAL_SUCCESS; 00368 if ((NULL == socket) || (NULL == callback)) 00369 { 00370 return PAL_ERR_RTOS_PARAMETER; 00371 } 00372 result = pal_plat_asynchronousSocket(domain, type, nonBlockingSocket, interfaceNum, callback, socket); 00373 return result; // TODO(nirson01) ADD debug print for error propagation(once debug print infrastructure is finalized) 00374 } 00375 00376 #endif 00377 00378 #if PAL_NET_DNS_SUPPORT 00379 00380 palStatus_t pal_getAddressInfo(const char *url, palSocketAddress_t *address, palSocketLength_t* addressLength) 00381 { 00382 palStatus_t result = PAL_SUCCESS; 00383 if ((NULL == url) || (NULL == address) || (NULL == addressLength)) 00384 { 00385 return PAL_ERR_RTOS_PARAMETER; 00386 } 00387 result = pal_plat_getAddressInfo(url, address, addressLength); 00388 return result; // TODO(nirson01) ADD debug print for error propagation(once debug print infrastructure is finalized) 00389 } 00390 00391 #endif 00392 00393 00394 00395 00396
Generated on Tue Jul 12 2022 21:20:28 by
1.7.2