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.
SocketAddress.cpp
00001 /* Socket 00002 * Copyright (c) 2015 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may 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, 00012 * WITHOUT 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 #include "SocketAddress.h" 00018 #include "NetworkInterface.h" 00019 #include "NetworkStack.h" 00020 #include <string.h> 00021 #include <stdio.h> 00022 #include "ip4string.h" 00023 #include "ip6string.h" 00024 00025 00026 00027 static bool ipv6_is_valid(const char *addr) 00028 { 00029 // Check each digit for [0-9a-fA-F:] 00030 // Must also have at least 2 colons 00031 int colons = 0; 00032 for (int i = 0; addr[i]; i++) { 00033 if (!(addr[i] >= '0' && addr[i] <= '9') && 00034 !(addr[i] >= 'a' && addr[i] <= 'f') && 00035 !(addr[i] >= 'A' && addr[i] <= 'F') && 00036 addr[i] != ':') { 00037 return false; 00038 } 00039 if (addr[i] == ':') { 00040 colons++; 00041 } 00042 } 00043 00044 return colons >= 2; 00045 } 00046 00047 SocketAddress::SocketAddress(nsapi_addr_t addr, uint16_t port) 00048 { 00049 _ip_address = NULL; 00050 set_addr(addr); 00051 set_port(port); 00052 } 00053 00054 SocketAddress::SocketAddress(const char *addr, uint16_t port) 00055 { 00056 _ip_address = NULL; 00057 set_ip_address(addr); 00058 set_port(port); 00059 } 00060 00061 SocketAddress::SocketAddress(const void *bytes, nsapi_version_t version, uint16_t port) 00062 { 00063 _ip_address = NULL; 00064 set_ip_bytes(bytes, version); 00065 set_port(port); 00066 } 00067 00068 SocketAddress::SocketAddress(const SocketAddress &addr) 00069 { 00070 _ip_address = NULL; 00071 set_addr(addr.get_addr()); 00072 set_port(addr.get_port()); 00073 } 00074 00075 bool SocketAddress::set_ip_address(const char *addr) 00076 { 00077 delete[] _ip_address; 00078 _ip_address = NULL; 00079 00080 if (addr && stoip4(addr, strlen(addr), _addr.bytes)) { 00081 _addr.version = NSAPI_IPv4 ; 00082 return true; 00083 } else if (addr && ipv6_is_valid(addr)) { 00084 _addr.version = NSAPI_IPv6 ; 00085 stoip6(addr, strlen(addr), _addr.bytes); 00086 return true; 00087 } else { 00088 _addr = nsapi_addr_t(); 00089 return false; 00090 } 00091 } 00092 00093 void SocketAddress::set_ip_bytes(const void *bytes, nsapi_version_t version) 00094 { 00095 nsapi_addr_t addr; 00096 00097 addr = nsapi_addr_t(); 00098 addr.version = version; 00099 if (version == NSAPI_IPv6 ) { 00100 memcpy(addr.bytes, bytes, NSAPI_IPv6_BYTES); 00101 } else if (version == NSAPI_IPv4 ) { 00102 memcpy(addr.bytes, bytes, NSAPI_IPv4_BYTES); 00103 } 00104 set_addr(addr); 00105 } 00106 00107 void SocketAddress::set_addr(nsapi_addr_t addr) 00108 { 00109 delete[] _ip_address; 00110 _ip_address = NULL; 00111 _addr = addr; 00112 } 00113 00114 void SocketAddress::set_port(uint16_t port) 00115 { 00116 _port = port; 00117 } 00118 00119 const char *SocketAddress::get_ip_address() const 00120 { 00121 if (_addr.version == NSAPI_UNSPEC ) { 00122 return NULL; 00123 } 00124 00125 if (!_ip_address) { 00126 _ip_address = new char[NSAPI_IP_SIZE]; 00127 if (_addr.version == NSAPI_IPv4 ) { 00128 ip4tos(_addr.bytes, _ip_address); 00129 } else if (_addr.version == NSAPI_IPv6 ) { 00130 ip6tos(_addr.bytes, _ip_address); 00131 } 00132 } 00133 00134 return _ip_address; 00135 } 00136 00137 const void *SocketAddress::get_ip_bytes() const 00138 { 00139 return _addr.bytes; 00140 } 00141 00142 nsapi_version_t SocketAddress::get_ip_version() const 00143 { 00144 return _addr.version; 00145 } 00146 00147 nsapi_addr_t SocketAddress::get_addr() const 00148 { 00149 return _addr; 00150 } 00151 00152 uint16_t SocketAddress::get_port() const 00153 { 00154 return _port; 00155 } 00156 00157 SocketAddress::operator bool() const 00158 { 00159 if (_addr.version == NSAPI_IPv4 ) { 00160 for (int i = 0; i < NSAPI_IPv4_BYTES; i++) { 00161 if (_addr.bytes[i]) { 00162 return true; 00163 } 00164 } 00165 00166 return false; 00167 } else if (_addr.version == NSAPI_IPv6 ) { 00168 for (int i = 0; i < NSAPI_IPv6_BYTES; i++) { 00169 if (_addr.bytes[i]) { 00170 return true; 00171 } 00172 } 00173 00174 return false; 00175 } else { 00176 return false; 00177 } 00178 } 00179 00180 SocketAddress &SocketAddress::operator=(const SocketAddress &addr) 00181 { 00182 delete[] _ip_address; 00183 _ip_address = NULL; 00184 set_addr(addr.get_addr()); 00185 set_port(addr.get_port()); 00186 return *this; 00187 } 00188 00189 bool operator==(const SocketAddress &a, const SocketAddress &b) 00190 { 00191 if (!a && !b) { 00192 return true; 00193 } else if (a._addr.version != b._addr.version) { 00194 return false; 00195 } else if (a._addr.version == NSAPI_IPv4 ) { 00196 return memcmp(a._addr.bytes, b._addr.bytes, NSAPI_IPv4_BYTES) == 0; 00197 } else if (a._addr.version == NSAPI_IPv6 ) { 00198 return memcmp(a._addr.bytes, b._addr.bytes, NSAPI_IPv6_BYTES) == 0; 00199 } 00200 00201 MBED_UNREACHABLE; 00202 } 00203 00204 bool operator!=(const SocketAddress &a, const SocketAddress &b) 00205 { 00206 return !(a == b); 00207 } 00208 00209 void SocketAddress::_SocketAddress(NetworkStack *iface, const char *host, uint16_t port) 00210 { 00211 _ip_address = NULL; 00212 00213 // gethostbyname must check for literals, so can call it directly 00214 int err = iface->gethostbyname(host, this); 00215 _port = port; 00216 if (err) { 00217 _addr = nsapi_addr_t(); 00218 _port = 0; 00219 } 00220 } 00221 00222 SocketAddress::~SocketAddress() 00223 { 00224 delete[] _ip_address; 00225 }
Generated on Tue Aug 9 2022 00:37:19 by
1.7.2