takashi kadono / Mbed OS Nucleo_446

Dependencies:   ssd1331

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SocketAddress.h Source File

SocketAddress.h

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