Rtos API example

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     /** Set the IP address
00088      *
00089      *  @param addr     Null-terminated represention of the IP address
00090      *  @return         True if address is a valid representation of an IP address,
00091      *                  otherwise False and SocketAddress is set to null
00092      */
00093     bool set_ip_address(const char *addr);
00094 
00095     /** Set the raw IP bytes and IP version
00096      *
00097      *  @param bytes    Raw IP address in big-endian order
00098      *  @param version  IP address version, NSAPI_IPv4 or NSAPI_IPv6
00099      */
00100     void set_ip_bytes(const void *bytes, nsapi_version_t version);
00101 
00102     /** Set the raw IP address
00103      *
00104      *  @param addr     Raw IP address
00105      */
00106     void set_addr(nsapi_addr_t addr);
00107 
00108     /** Set the port
00109      *
00110      *  @param port     16-bit port
00111      */
00112     void set_port(uint16_t port);
00113     
00114     /** Get the IP address
00115      *
00116      *  @return         Null-terminated representation of the IP Address
00117      */
00118     const char *get_ip_address() const;
00119 
00120     /*  Get the raw IP bytes
00121      *
00122      *  @return         Raw IP address in big-endian order
00123      */
00124     const void *get_ip_bytes() const;
00125 
00126     /** Get the IP address version
00127      *
00128      *  @return         IP address version, NSAPI_IPv4 or NSAPI_IPv6
00129      */
00130     nsapi_version_t get_ip_version() const;
00131 
00132     /** Get the raw IP address
00133      *
00134      *  @return         Raw IP address
00135      */
00136     nsapi_addr_t get_addr() const;
00137     
00138     /** Get the port
00139      *
00140      *  @return         The 16-bit port
00141      */
00142     uint16_t get_port() const;
00143 
00144     /** Test if address is zero
00145      *
00146      *  @return         True if address is not zero
00147      */
00148     operator bool() const;
00149 
00150     /** Compare two addresses for equality
00151      *
00152      *  @return         True if both addresses are equal
00153      */
00154     friend bool operator==(const SocketAddress &a, const SocketAddress &b);
00155 
00156     /** Compare two addresses for equality
00157      *
00158      *  @return         True if both addresses are not equal
00159      */
00160     friend bool operator!=(const SocketAddress &a, const SocketAddress &b);
00161 
00162 private:
00163     void _SocketAddress(NetworkStack *iface, const char *host, uint16_t port);
00164 
00165     mutable char _ip_address[NSAPI_IP_SIZE];
00166     nsapi_addr_t _addr;
00167     uint16_t _port;
00168 };
00169 
00170 
00171 #endif
00172 
00173 /** @}*/