Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:27:58 2016 +0000
Revision:
0:6c56fb4bc5f0
Moving to library for sharing updates

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nexpaq 0:6c56fb4bc5f0 1 /* SocketAddress
nexpaq 0:6c56fb4bc5f0 2 * Copyright (c) 2015 ARM Limited
nexpaq 0:6c56fb4bc5f0 3 *
nexpaq 0:6c56fb4bc5f0 4 * Licensed under the Apache License, Version 2.0 (the "License");
nexpaq 0:6c56fb4bc5f0 5 * you may not use this file except in compliance with the License.
nexpaq 0:6c56fb4bc5f0 6 * You may obtain a copy of the License at
nexpaq 0:6c56fb4bc5f0 7 *
nexpaq 0:6c56fb4bc5f0 8 * http://www.apache.org/licenses/LICENSE-2.0
nexpaq 0:6c56fb4bc5f0 9 *
nexpaq 0:6c56fb4bc5f0 10 * Unless required by applicable law or agreed to in writing, software
nexpaq 0:6c56fb4bc5f0 11 * distributed under the License is distributed on an "AS IS" BASIS,
nexpaq 0:6c56fb4bc5f0 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
nexpaq 0:6c56fb4bc5f0 13 * See the License for the specific language governing permissions and
nexpaq 0:6c56fb4bc5f0 14 * limitations under the License.
nexpaq 0:6c56fb4bc5f0 15 */
nexpaq 0:6c56fb4bc5f0 16
nexpaq 0:6c56fb4bc5f0 17 #ifndef SOCKET_ADDRESS_H
nexpaq 0:6c56fb4bc5f0 18 #define SOCKET_ADDRESS_H
nexpaq 0:6c56fb4bc5f0 19
nexpaq 0:6c56fb4bc5f0 20 #include "nsapi_types.h"
nexpaq 0:6c56fb4bc5f0 21 #include "toolchain.h"
nexpaq 0:6c56fb4bc5f0 22
nexpaq 0:6c56fb4bc5f0 23 // Predeclared classes
nexpaq 0:6c56fb4bc5f0 24 class NetworkStack;
nexpaq 0:6c56fb4bc5f0 25 class NetworkInterface;
nexpaq 0:6c56fb4bc5f0 26
nexpaq 0:6c56fb4bc5f0 27
nexpaq 0:6c56fb4bc5f0 28 /** SocketAddress class
nexpaq 0:6c56fb4bc5f0 29 *
nexpaq 0:6c56fb4bc5f0 30 * Representation of an IP address and port pair.
nexpaq 0:6c56fb4bc5f0 31 */
nexpaq 0:6c56fb4bc5f0 32 class SocketAddress {
nexpaq 0:6c56fb4bc5f0 33 public:
nexpaq 0:6c56fb4bc5f0 34 /** Create a SocketAddress from a hostname and port
nexpaq 0:6c56fb4bc5f0 35 *
nexpaq 0:6c56fb4bc5f0 36 * The hostname may be either a domain name or an IP address. If the
nexpaq 0:6c56fb4bc5f0 37 * hostname is an IP address, no network transactions will be performed.
nexpaq 0:6c56fb4bc5f0 38 *
nexpaq 0:6c56fb4bc5f0 39 * On failure, the IP address and port will be set to zero
nexpaq 0:6c56fb4bc5f0 40 *
nexpaq 0:6c56fb4bc5f0 41 * @param stack Network stack to use for DNS resolution
nexpaq 0:6c56fb4bc5f0 42 * @param host Hostname to resolve
nexpaq 0:6c56fb4bc5f0 43 * @param port Optional 16-bit port
nexpaq 0:6c56fb4bc5f0 44 */
nexpaq 0:6c56fb4bc5f0 45 template <typename S>
nexpaq 0:6c56fb4bc5f0 46 SocketAddress(S *stack, const char *host, uint16_t port = 0)
nexpaq 0:6c56fb4bc5f0 47 {
nexpaq 0:6c56fb4bc5f0 48 _SocketAddress(nsapi_create_stack(stack), host, port);
nexpaq 0:6c56fb4bc5f0 49 }
nexpaq 0:6c56fb4bc5f0 50
nexpaq 0:6c56fb4bc5f0 51 /** Create a SocketAddress from a raw IP address and port
nexpaq 0:6c56fb4bc5f0 52 *
nexpaq 0:6c56fb4bc5f0 53 * @param addr Raw IP address
nexpaq 0:6c56fb4bc5f0 54 * @param port Optional 16-bit port
nexpaq 0:6c56fb4bc5f0 55 */
nexpaq 0:6c56fb4bc5f0 56 SocketAddress(nsapi_addr_t addr = nsapi_addr_t(), uint16_t port = 0);
nexpaq 0:6c56fb4bc5f0 57
nexpaq 0:6c56fb4bc5f0 58 /** Create a SocketAddress from an IP address and port
nexpaq 0:6c56fb4bc5f0 59 *
nexpaq 0:6c56fb4bc5f0 60 * @param host Null-terminated representation of the IP address
nexpaq 0:6c56fb4bc5f0 61 * @param port Optional 16-bit port
nexpaq 0:6c56fb4bc5f0 62 */
nexpaq 0:6c56fb4bc5f0 63 SocketAddress(const char *addr, uint16_t port = 0);
nexpaq 0:6c56fb4bc5f0 64
nexpaq 0:6c56fb4bc5f0 65 /** Create a SocketAddress from raw IP bytes, IP version, and port
nexpaq 0:6c56fb4bc5f0 66 *
nexpaq 0:6c56fb4bc5f0 67 * @param bytes Raw IP address in big-endian order
nexpaq 0:6c56fb4bc5f0 68 * @param version IP address version, NSAPI_IPv4 or NSAPI_IPv6
nexpaq 0:6c56fb4bc5f0 69 * @param port Optional 16-bit port
nexpaq 0:6c56fb4bc5f0 70 */
nexpaq 0:6c56fb4bc5f0 71 SocketAddress(const void *bytes, nsapi_version_t version, uint16_t port = 0);
nexpaq 0:6c56fb4bc5f0 72
nexpaq 0:6c56fb4bc5f0 73 /** Create a SocketAddress from another SocketAddress
nexpaq 0:6c56fb4bc5f0 74 *
nexpaq 0:6c56fb4bc5f0 75 * @param address SocketAddress to copy
nexpaq 0:6c56fb4bc5f0 76 */
nexpaq 0:6c56fb4bc5f0 77 SocketAddress(const SocketAddress &addr);
nexpaq 0:6c56fb4bc5f0 78
nexpaq 0:6c56fb4bc5f0 79 /** Set the IP address
nexpaq 0:6c56fb4bc5f0 80 *
nexpaq 0:6c56fb4bc5f0 81 * @param addr Null-terminated represention of the IP address
nexpaq 0:6c56fb4bc5f0 82 */
nexpaq 0:6c56fb4bc5f0 83 void set_ip_address(const char *addr);
nexpaq 0:6c56fb4bc5f0 84
nexpaq 0:6c56fb4bc5f0 85 /** Set the raw IP bytes and IP version
nexpaq 0:6c56fb4bc5f0 86 *
nexpaq 0:6c56fb4bc5f0 87 * @param bytes Raw IP address in big-endian order
nexpaq 0:6c56fb4bc5f0 88 * @param version IP address version, NSAPI_IPv4 or NSAPI_IPv6
nexpaq 0:6c56fb4bc5f0 89 */
nexpaq 0:6c56fb4bc5f0 90 void set_ip_bytes(const void *bytes, nsapi_version_t version);
nexpaq 0:6c56fb4bc5f0 91
nexpaq 0:6c56fb4bc5f0 92 /** Set the raw IP address
nexpaq 0:6c56fb4bc5f0 93 *
nexpaq 0:6c56fb4bc5f0 94 * @param addr Raw IP address
nexpaq 0:6c56fb4bc5f0 95 */
nexpaq 0:6c56fb4bc5f0 96 void set_addr(nsapi_addr_t addr);
nexpaq 0:6c56fb4bc5f0 97
nexpaq 0:6c56fb4bc5f0 98 /** Set the port
nexpaq 0:6c56fb4bc5f0 99 *
nexpaq 0:6c56fb4bc5f0 100 * @param port 16-bit port
nexpaq 0:6c56fb4bc5f0 101 */
nexpaq 0:6c56fb4bc5f0 102 void set_port(uint16_t port);
nexpaq 0:6c56fb4bc5f0 103
nexpaq 0:6c56fb4bc5f0 104 /** Get the IP address
nexpaq 0:6c56fb4bc5f0 105 *
nexpaq 0:6c56fb4bc5f0 106 * @return Null-terminated representation of the IP Address
nexpaq 0:6c56fb4bc5f0 107 */
nexpaq 0:6c56fb4bc5f0 108 const char *get_ip_address() const;
nexpaq 0:6c56fb4bc5f0 109
nexpaq 0:6c56fb4bc5f0 110 /* Get the raw IP bytes
nexpaq 0:6c56fb4bc5f0 111 *
nexpaq 0:6c56fb4bc5f0 112 * @return Raw IP address in big-endian order
nexpaq 0:6c56fb4bc5f0 113 */
nexpaq 0:6c56fb4bc5f0 114 const void *get_ip_bytes() const;
nexpaq 0:6c56fb4bc5f0 115
nexpaq 0:6c56fb4bc5f0 116 /** Get the IP address version
nexpaq 0:6c56fb4bc5f0 117 *
nexpaq 0:6c56fb4bc5f0 118 * @return IP address version, NSAPI_IPv4 or NSAPI_IPv6
nexpaq 0:6c56fb4bc5f0 119 */
nexpaq 0:6c56fb4bc5f0 120 nsapi_version_t get_ip_version() const;
nexpaq 0:6c56fb4bc5f0 121
nexpaq 0:6c56fb4bc5f0 122 /** Get the raw IP address
nexpaq 0:6c56fb4bc5f0 123 *
nexpaq 0:6c56fb4bc5f0 124 * @return Raw IP address
nexpaq 0:6c56fb4bc5f0 125 */
nexpaq 0:6c56fb4bc5f0 126 nsapi_addr_t get_addr() const;
nexpaq 0:6c56fb4bc5f0 127
nexpaq 0:6c56fb4bc5f0 128 /** Get the port
nexpaq 0:6c56fb4bc5f0 129 *
nexpaq 0:6c56fb4bc5f0 130 * @return The 16-bit port
nexpaq 0:6c56fb4bc5f0 131 */
nexpaq 0:6c56fb4bc5f0 132 uint16_t get_port() const;
nexpaq 0:6c56fb4bc5f0 133
nexpaq 0:6c56fb4bc5f0 134 /** Test if address is zero
nexpaq 0:6c56fb4bc5f0 135 *
nexpaq 0:6c56fb4bc5f0 136 * @return True if address is not zero
nexpaq 0:6c56fb4bc5f0 137 */
nexpaq 0:6c56fb4bc5f0 138 operator bool() const;
nexpaq 0:6c56fb4bc5f0 139
nexpaq 0:6c56fb4bc5f0 140 private:
nexpaq 0:6c56fb4bc5f0 141 void _SocketAddress(NetworkStack *iface, const char *host, uint16_t port);
nexpaq 0:6c56fb4bc5f0 142
nexpaq 0:6c56fb4bc5f0 143 char _ip_address[NSAPI_IP_SIZE];
nexpaq 0:6c56fb4bc5f0 144 nsapi_addr_t _addr;
nexpaq 0:6c56fb4bc5f0 145 uint16_t _port;
nexpaq 0:6c56fb4bc5f0 146 };
nexpaq 0:6c56fb4bc5f0 147
nexpaq 0:6c56fb4bc5f0 148
nexpaq 0:6c56fb4bc5f0 149 #endif