Maxim nexpaq / nexpaq_dev
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SocketAddress.h Source File

SocketAddress.h

00001 /* SocketAddress
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 #ifndef SOCKET_ADDRESS_H
00018 #define SOCKET_ADDRESS_H
00019 
00020 #include "nsapi_types.h"
00021 #include "toolchain.h"
00022 
00023 // Predeclared classes
00024 class NetworkStack;
00025 class NetworkInterface;
00026 
00027 
00028 /** SocketAddress class
00029  *
00030  *  Representation of an IP address and port pair. 
00031  */
00032 class SocketAddress {
00033 public:
00034     /** Create a SocketAddress from a hostname and port
00035      *
00036      *  The hostname may be either a domain name or an IP address. If the
00037      *  hostname is an IP address, no network transactions will be performed.
00038      *
00039      *  On failure, the IP address and port will be set to zero
00040      *
00041      *  @param stack    Network stack to use for DNS resolution
00042      *  @param host     Hostname to resolve
00043      *  @param port     Optional 16-bit port
00044      */
00045     template <typename S>
00046     SocketAddress(S *stack, const char *host, uint16_t port = 0)
00047     {
00048         _SocketAddress(nsapi_create_stack(stack), host, port);
00049     }
00050 
00051     /** Create a SocketAddress from a raw IP address and port
00052      *
00053      *  @param addr     Raw IP address
00054      *  @param port     Optional 16-bit port
00055      */
00056     SocketAddress(nsapi_addr_t addr = nsapi_addr_t(), uint16_t port = 0);
00057 
00058     /** Create a SocketAddress from an IP address and port
00059      *
00060      *  @param host     Null-terminated representation of the IP address
00061      *  @param port     Optional 16-bit port
00062      */
00063     SocketAddress(const char *addr, uint16_t port = 0);
00064 
00065     /** Create a SocketAddress from raw IP bytes, IP version, and port
00066      *
00067      *  @param bytes    Raw IP address in big-endian order
00068      *  @param version  IP address version, NSAPI_IPv4 or NSAPI_IPv6
00069      *  @param port     Optional 16-bit port
00070      */
00071     SocketAddress(const void *bytes, nsapi_version_t version, uint16_t port = 0);
00072 
00073     /** Create a SocketAddress from another SocketAddress
00074      *
00075      *  @param address  SocketAddress to copy
00076      */
00077     SocketAddress(const SocketAddress &addr);
00078    
00079     /** Set the IP address
00080      *
00081      *  @param addr     Null-terminated represention of the IP address
00082      */
00083     void set_ip_address(const char *addr);
00084 
00085     /** Set the raw IP bytes and IP version
00086      *
00087      *  @param bytes    Raw IP address in big-endian order
00088      *  @param version  IP address version, NSAPI_IPv4 or NSAPI_IPv6
00089      */
00090     void set_ip_bytes(const void *bytes, nsapi_version_t version);
00091 
00092     /** Set the raw IP address
00093      *
00094      *  @param addr     Raw IP address
00095      */
00096     void set_addr(nsapi_addr_t addr);
00097 
00098     /** Set the port
00099      *
00100      *  @param port     16-bit port
00101      */
00102     void set_port(uint16_t port);
00103     
00104     /** Get the IP address
00105      *
00106      *  @return         Null-terminated representation of the IP Address
00107      */
00108     const char *get_ip_address() const;
00109 
00110     /*  Get the raw IP bytes
00111      *
00112      *  @return         Raw IP address in big-endian order
00113      */
00114     const void *get_ip_bytes() const;
00115 
00116     /** Get the IP address version
00117      *
00118      *  @return         IP address version, NSAPI_IPv4 or NSAPI_IPv6
00119      */
00120     nsapi_version_t get_ip_version() const;
00121 
00122     /** Get the raw IP address
00123      *
00124      *  @return         Raw IP address
00125      */
00126     nsapi_addr_t get_addr() const;
00127     
00128     /** Get the port
00129      *
00130      *  @return         The 16-bit port
00131      */
00132     uint16_t get_port() const;
00133 
00134     /** Test if address is zero
00135      *
00136      *  @return         True if address is not zero
00137      */
00138     operator bool() const;
00139 
00140 private:
00141     void _SocketAddress(NetworkStack *iface, const char *host, uint16_t port);
00142 
00143     char _ip_address[NSAPI_IP_SIZE];
00144     nsapi_addr_t _addr;
00145     uint16_t _port;
00146 };
00147 
00148 
00149 #endif