ON Semiconductor / mbed-os

Dependents:   mbed-TFT-example-NCS36510 mbed-Accelerometer-example-NCS36510 mbed-Accelerometer-example-NCS36510

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SocketAddress.h Source File

SocketAddress.h

00001 
00002 /** \addtogroup netsocket */
00003 /** @{*/
00004 /* SocketAddress
00005  * Copyright (c) 2015 ARM Limited
00006  *
00007  * Licensed under the Apache License, Version 2.0 (the "License");
00008  * you may not use this file except in compliance with the License.
00009  * You may obtain a copy of the License at
00010  *
00011  *     http://www.apache.org/licenses/LICENSE-2.0
00012  *
00013  * Unless required by applicable law or agreed to in writing, software
00014  * distributed under the License is distributed on an "AS IS" BASIS,
00015  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016  * See the License for the specific language governing permissions and
00017  * limitations under the License.
00018  */
00019 
00020 #ifndef SOCKET_ADDRESS_H
00021 #define SOCKET_ADDRESS_H
00022 
00023 #include "nsapi_types.h"
00024 #include "toolchain.h"
00025 
00026 // Predeclared classes
00027 class NetworkStack;
00028 class NetworkInterface;
00029 
00030 
00031 /** SocketAddress class
00032  *
00033  *  Representation of an IP address and port pair. 
00034  */
00035 class SocketAddress {
00036 public:
00037     /** Create a SocketAddress from a hostname and port
00038      *
00039      *  The hostname may be either a domain name or an IP address. If the
00040      *  hostname is an IP address, no network transactions will be performed.
00041      *
00042      *  On failure, the IP address and port will be set to zero
00043      *
00044      *  @param stack    Network stack to use for DNS resolution
00045      *  @param host     Hostname to resolve
00046      *  @param port     Optional 16-bit port
00047      *  @deprecated
00048      *      Constructors hide possible errors. Replaced by
00049      *      NetworkInterface::gethostbyname.
00050      */
00051     template <typename S>
00052     MBED_DEPRECATED_SINCE("mbed-os-5.1.3",
00053         "Constructors hide possible errors. Replaced by "
00054         "NetworkInterface::gethostbyname.")
00055     SocketAddress(S *stack, const char *host, uint16_t port = 0)
00056     {
00057         _SocketAddress(nsapi_create_stack(stack), host, port);
00058     }
00059 
00060     /** Create a SocketAddress from a raw IP address and port
00061      *
00062      *  @param addr     Raw IP address
00063      *  @param port     Optional 16-bit port
00064      */
00065     SocketAddress(nsapi_addr_t addr = nsapi_addr_t(), uint16_t port = 0);
00066 
00067     /** Create a SocketAddress from an IP address and port
00068      *
00069      *  @param host     Null-terminated representation of the IP address
00070      *  @param port     Optional 16-bit port
00071      */
00072     SocketAddress(const char *addr, uint16_t port = 0);
00073 
00074     /** Create a SocketAddress from raw IP bytes, IP version, and port
00075      *
00076      *  @param bytes    Raw IP address in big-endian order
00077      *  @param version  IP address version, NSAPI_IPv4 or NSAPI_IPv6
00078      *  @param port     Optional 16-bit port
00079      */
00080     SocketAddress(const void *bytes, nsapi_version_t version, uint16_t port = 0);
00081 
00082     /** Create a SocketAddress from another SocketAddress
00083      *
00084      *  @param address  SocketAddress to copy
00085      */
00086     SocketAddress(const SocketAddress &addr);
00087    
00088     /** Set the IP address
00089      *
00090      *  @param addr     Null-terminated represention of the IP address
00091      *  @return         True if address is a valid representation of an IP address,
00092      *                  otherwise False and SocketAddress is set to null
00093      */
00094     bool set_ip_address(const char *addr);
00095 
00096     /** Set the raw IP bytes and IP version
00097      *
00098      *  @param bytes    Raw IP address in big-endian order
00099      *  @param version  IP address version, NSAPI_IPv4 or NSAPI_IPv6
00100      */
00101     void set_ip_bytes(const void *bytes, nsapi_version_t version);
00102 
00103     /** Set the raw IP address
00104      *
00105      *  @param addr     Raw IP address
00106      */
00107     void set_addr(nsapi_addr_t addr);
00108 
00109     /** Set the port
00110      *
00111      *  @param port     16-bit port
00112      */
00113     void set_port(uint16_t port);
00114     
00115     /** Get the IP address
00116      *
00117      *  @return         Null-terminated representation of the IP Address
00118      */
00119     const char *get_ip_address() const;
00120 
00121     /*  Get the raw IP bytes
00122      *
00123      *  @return         Raw IP address in big-endian order
00124      */
00125     const void *get_ip_bytes() const;
00126 
00127     /** Get the IP address version
00128      *
00129      *  @return         IP address version, NSAPI_IPv4 or NSAPI_IPv6
00130      */
00131     nsapi_version_t get_ip_version() const;
00132 
00133     /** Get the raw IP address
00134      *
00135      *  @return         Raw IP address
00136      */
00137     nsapi_addr_t get_addr() const;
00138     
00139     /** Get the port
00140      *
00141      *  @return         The 16-bit port
00142      */
00143     uint16_t get_port() const;
00144 
00145     /** Test if address is zero
00146      *
00147      *  @return         True if address is not zero
00148      */
00149     operator bool() const;
00150 
00151     /** Compare two addresses for equality
00152      *
00153      *  @return         True if both addresses are equal
00154      */
00155     friend bool operator==(const SocketAddress &a, const SocketAddress &b);
00156 
00157     /** Compare two addresses for equality
00158      *
00159      *  @return         True if both addresses are not equal
00160      */
00161     friend bool operator!=(const SocketAddress &a, const SocketAddress &b);
00162 
00163 private:
00164     void _SocketAddress(NetworkStack *iface, const char *host, uint16_t port);
00165 
00166     mutable char _ip_address[NSAPI_IP_SIZE];
00167     nsapi_addr_t _addr;
00168     uint16_t _port;
00169 };
00170 
00171 
00172 #endif
00173 
00174 /** @}*/