Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SocketAddress.cpp Source File

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 SocketAddress::SocketAddress(nsapi_addr_t addr, uint16_t port)
00028 {
00029     mem_init();
00030     _ip_address = NULL;
00031     set_addr(addr);
00032     set_port(port);
00033 }
00034 
00035 SocketAddress::SocketAddress(const char *addr, uint16_t port)
00036 {
00037     mem_init();
00038     _ip_address = NULL;
00039     set_ip_address(addr);
00040     set_port(port);
00041 }
00042 
00043 SocketAddress::SocketAddress(const void *bytes, nsapi_version_t version, uint16_t port)
00044 {
00045     mem_init();
00046     _ip_address = NULL;
00047     set_ip_bytes(bytes, version);
00048     set_port(port);
00049 }
00050 
00051 SocketAddress::SocketAddress(const SocketAddress &addr)
00052 {
00053     mem_init();
00054     _ip_address = NULL;
00055     set_addr(addr.get_addr());
00056     set_port(addr.get_port());
00057 }
00058 
00059 void SocketAddress::mem_init(void)
00060 {
00061     _addr.version = NSAPI_UNSPEC ;
00062     memset(_addr.bytes, 0, NSAPI_IP_BYTES);
00063     _port = 0;
00064 }
00065 
00066 bool SocketAddress::set_ip_address(const char *addr)
00067 {
00068     delete[] _ip_address;
00069     _ip_address = NULL;
00070 
00071     if (addr && stoip4(addr, strlen(addr), _addr.bytes)) {
00072         _addr.version = NSAPI_IPv4 ;
00073         return true;
00074     } else if (addr && stoip6(addr, strlen(addr), _addr.bytes)) {
00075         _addr.version = NSAPI_IPv6 ;
00076         return true;
00077     } else {
00078         _addr = nsapi_addr_t();
00079         return false;
00080     }
00081 }
00082 
00083 void SocketAddress::set_ip_bytes(const void *bytes, nsapi_version_t version)
00084 {
00085     nsapi_addr_t addr;
00086 
00087     addr = nsapi_addr_t();
00088     addr.version = version;
00089     if (version == NSAPI_IPv6 ) {
00090         memcpy(addr.bytes, bytes, NSAPI_IPv6_BYTES);
00091     } else if (version == NSAPI_IPv4 ) {
00092         memcpy(addr.bytes, bytes, NSAPI_IPv4_BYTES);
00093     }
00094     set_addr(addr);
00095 }
00096 
00097 void SocketAddress::set_addr(nsapi_addr_t addr)
00098 {
00099     delete[] _ip_address;
00100     _ip_address = NULL;
00101     _addr = addr;
00102 }
00103 
00104 void SocketAddress::set_port(uint16_t port)
00105 {
00106     _port = port;
00107 }
00108 
00109 const char *SocketAddress::get_ip_address() const
00110 {
00111     if (_addr.version == NSAPI_UNSPEC ) {
00112         return NULL;
00113     }
00114 
00115     if (!_ip_address) {
00116         _ip_address = new char[NSAPI_IP_SIZE];
00117         if (_addr.version == NSAPI_IPv4 ) {
00118             ip4tos(_addr.bytes, _ip_address);
00119         } else if (_addr.version == NSAPI_IPv6 ) {
00120             ip6tos(_addr.bytes, _ip_address);
00121         }
00122     }
00123 
00124     return _ip_address;
00125 }
00126 
00127 const void *SocketAddress::get_ip_bytes() const
00128 {
00129     return _addr.bytes;
00130 }
00131 
00132 nsapi_version_t SocketAddress::get_ip_version() const
00133 {
00134     return _addr.version;
00135 }
00136 
00137 nsapi_addr_t SocketAddress::get_addr() const
00138 {
00139     return _addr;
00140 }
00141 
00142 uint16_t SocketAddress::get_port() const
00143 {
00144     return _port;
00145 }
00146 
00147 SocketAddress::operator bool() const
00148 {
00149     if (_addr.version == NSAPI_IPv4 ) {
00150         for (int i = 0; i < NSAPI_IPv4_BYTES; i++) {
00151             if (_addr.bytes[i]) {
00152                 return true;
00153             }
00154         }
00155 
00156         return false;
00157     } else if (_addr.version == NSAPI_IPv6 ) {
00158         for (int i = 0; i < NSAPI_IPv6_BYTES; i++) {
00159             if (_addr.bytes[i]) {
00160                 return true;
00161             }
00162         }
00163 
00164         return false;
00165     } else {
00166         return false;
00167     }
00168 }
00169 
00170 SocketAddress &SocketAddress::operator=(const SocketAddress &addr)
00171 {
00172     delete[] _ip_address;
00173     _ip_address = NULL;
00174     set_addr(addr.get_addr());
00175     set_port(addr.get_port());
00176     return *this;
00177 }
00178 
00179 bool operator==(const SocketAddress &a, const SocketAddress &b)
00180 {
00181     if (!a && !b) {
00182         return true;
00183     } else if (a._addr.version != b._addr.version) {
00184         return false;
00185     } else if (a._port != b._port) {
00186         return false;
00187     } else if (a._addr.version == NSAPI_IPv4 ) {
00188         return memcmp(a._addr.bytes, b._addr.bytes, NSAPI_IPv4_BYTES) == 0;
00189     } else if (a._addr.version == NSAPI_IPv6 ) {
00190         return memcmp(a._addr.bytes, b._addr.bytes, NSAPI_IPv6_BYTES) == 0;
00191     }
00192 
00193     MBED_UNREACHABLE;
00194 }
00195 
00196 bool operator!=(const SocketAddress &a, const SocketAddress &b)
00197 {
00198     return !(a == b);
00199 }
00200 
00201 void SocketAddress::_SocketAddress(NetworkStack *iface, const char *host, uint16_t port)
00202 {
00203     _ip_address = NULL;
00204 
00205     // gethostbyname must check for literals, so can call it directly
00206     int err = iface->gethostbyname(host, this);
00207     _port = port;
00208     if (err) {
00209         _addr = nsapi_addr_t();
00210         _port = 0;
00211     }
00212 }
00213 
00214 SocketAddress::~SocketAddress()
00215 {
00216     delete[] _ip_address;
00217 }