Base class for IP Based Networking Libraries
Dependencies: DnsQuery
Dependents: TempTower BSDInterfaceTests HelloBSDInterface ESP8266InterfaceTests ... more
For a complete getting started guide see the wiki...
Network Socket API
The Network Socket API provides a common interface for using sockets on network devices. The API provides a simple class-based interface that should be familiar to users experienced with other socket APIs. Additionally, the API provides a simple interface for implementing network devices, making it easy to connect hardware agnostic programs to new devices.
Network Interfaces
The NetworkInterface provides an abstract class for network devices that support sockets. Devices should provide a DeviceInterface class that inherits this interface and adds implementation specific methods for using the device. A NetworkInterface must be provided to a Socket constructor to open a socket on the interface. Currently two subclasses are defined for common devices, EthernetInterface
and WiFiInterface.
Sockets
The Socket class is used for managing network sockets. Once opened, the socket provides a pipe through which data can sent and recieved to a specific endpoint. The socket class can be instantiated as either a TCPSocket
or a UDPSocket
which defines the protocol used for the connection.
SocketAddress.h@125:ea3a618e0818, 2016-05-25 (annotated)
- Committer:
- dkato
- Date:
- Wed May 25 02:45:51 2016 +0000
- Revision:
- 125:ea3a618e0818
- Parent:
- 114:964eba6394bc
Fix the path of DnsQuery.lib
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Christopher Haster |
104:d28d8b508e7c | 1 | /* SocketAddress |
Christopher Haster |
89:b1d417383c0d | 2 | * Copyright (c) 2015 ARM Limited |
Christopher Haster |
89:b1d417383c0d | 3 | * |
Christopher Haster |
89:b1d417383c0d | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Christopher Haster |
89:b1d417383c0d | 5 | * you may not use this file except in compliance with the License. |
Christopher Haster |
89:b1d417383c0d | 6 | * You may obtain a copy of the License at |
Christopher Haster |
89:b1d417383c0d | 7 | * |
Christopher Haster |
89:b1d417383c0d | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Christopher Haster |
89:b1d417383c0d | 9 | * |
Christopher Haster |
89:b1d417383c0d | 10 | * Unless required by applicable law or agreed to in writing, software |
Christopher Haster |
89:b1d417383c0d | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Christopher Haster |
89:b1d417383c0d | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Christopher Haster |
89:b1d417383c0d | 13 | * See the License for the specific language governing permissions and |
Christopher Haster |
89:b1d417383c0d | 14 | * limitations under the License. |
Christopher Haster |
89:b1d417383c0d | 15 | */ |
Christopher Haster |
89:b1d417383c0d | 16 | |
Christopher Haster |
89:b1d417383c0d | 17 | #ifndef SOCKET_ADDRESS_H |
Christopher Haster |
89:b1d417383c0d | 18 | #define SOCKET_ADDRESS_H |
Christopher Haster |
89:b1d417383c0d | 19 | |
Christopher Haster |
89:b1d417383c0d | 20 | #include <stdint.h> |
Christopher Haster |
89:b1d417383c0d | 21 | |
Christopher Haster |
94:644df37bb05b | 22 | |
Christopher Haster |
94:644df37bb05b | 23 | /** Maximum size of IP address representation |
Christopher Haster |
94:644df37bb05b | 24 | */ |
Christopher Haster |
94:644df37bb05b | 25 | #define NSAPI_IP_SIZE NSAPI_IPv6_SIZE |
Christopher Haster |
94:644df37bb05b | 26 | |
Christopher Haster |
94:644df37bb05b | 27 | /** Maximum number of bytes for IP address |
Christopher Haster |
94:644df37bb05b | 28 | */ |
Christopher Haster |
94:644df37bb05b | 29 | #define NSAPI_IP_BYTES NSAPI_IPv6_BYTES |
Christopher Haster |
89:b1d417383c0d | 30 | |
Christopher Haster |
114:964eba6394bc | 31 | /** Maximum size of MAC address representation |
Christopher Haster |
114:964eba6394bc | 32 | */ |
Christopher Haster |
114:964eba6394bc | 33 | #define NSAPI_MAC_SIZE 18 |
Christopher Haster |
114:964eba6394bc | 34 | |
Christopher Haster |
114:964eba6394bc | 35 | /** Maximum number of bytes for MAC address |
Christopher Haster |
114:964eba6394bc | 36 | */ |
Christopher Haster |
114:964eba6394bc | 37 | #define NSAPI_MAC_BYTES 6 |
Christopher Haster |
114:964eba6394bc | 38 | |
Christopher Haster |
104:d28d8b508e7c | 39 | /** Enum of IP address versions |
Christopher Haster |
104:d28d8b508e7c | 40 | * |
Christopher Haster |
104:d28d8b508e7c | 41 | * The IP version specifies the type of an IP address. |
Christopher Haster |
104:d28d8b508e7c | 42 | * |
Christopher Haster |
104:d28d8b508e7c | 43 | * @enum nsapi_version_t |
Christopher Haster |
94:644df37bb05b | 44 | */ |
Christopher Haster |
94:644df37bb05b | 45 | enum nsapi_version_t { |
Christopher Haster |
94:644df37bb05b | 46 | NSAPI_IPv4, /*!< Address is IPv4 */ |
Christopher Haster |
94:644df37bb05b | 47 | NSAPI_IPv6, /*!< Address is IPv6 */ |
Christopher Haster |
94:644df37bb05b | 48 | }; |
Christopher Haster |
94:644df37bb05b | 49 | |
Christopher Haster |
94:644df37bb05b | 50 | /** Size of IPv4 representation |
Christopher Haster |
94:644df37bb05b | 51 | */ |
Christopher Haster |
94:644df37bb05b | 52 | #define NSAPI_IPv4_SIZE 16 |
Christopher Haster |
94:644df37bb05b | 53 | |
Christopher Haster |
94:644df37bb05b | 54 | /** Number of bytes in IPv4 address |
Christopher Haster |
94:644df37bb05b | 55 | */ |
Christopher Haster |
94:644df37bb05b | 56 | #define NSAPI_IPv4_BYTES 4 |
Christopher Haster |
94:644df37bb05b | 57 | |
Christopher Haster |
94:644df37bb05b | 58 | /** Size of IPv6 representation |
Christopher Haster |
94:644df37bb05b | 59 | */ |
Christopher Haster |
94:644df37bb05b | 60 | #define NSAPI_IPv6_SIZE 40 |
Christopher Haster |
94:644df37bb05b | 61 | |
Christopher Haster |
94:644df37bb05b | 62 | /** Number of bytes in IPv6 address |
Christopher Haster |
94:644df37bb05b | 63 | */ |
Christopher Haster |
94:644df37bb05b | 64 | #define NSAPI_IPv6_BYTES 16 |
Christopher Haster |
89:b1d417383c0d | 65 | |
Christopher Haster |
89:b1d417383c0d | 66 | // Predeclared classes |
Christopher Haster |
105:2fd212f8da61 | 67 | class NetworkStack; |
Christopher Haster |
89:b1d417383c0d | 68 | |
Christopher Haster |
94:644df37bb05b | 69 | |
Christopher Haster |
104:d28d8b508e7c | 70 | /** SocketAddress class |
Christopher Haster |
104:d28d8b508e7c | 71 | * |
Christopher Haster |
104:d28d8b508e7c | 72 | * Representation of an IP address and port pair. |
Christopher Haster |
89:b1d417383c0d | 73 | */ |
Christopher Haster |
89:b1d417383c0d | 74 | class SocketAddress { |
Christopher Haster |
89:b1d417383c0d | 75 | public: |
Christopher Haster |
104:d28d8b508e7c | 76 | /** Create a SocketAddress from a hostname and port |
Christopher Haster |
104:d28d8b508e7c | 77 | * |
Christopher Haster |
104:d28d8b508e7c | 78 | * The hostname may be either a domain name or an IP address. If the |
Christopher Haster |
104:d28d8b508e7c | 79 | * hostname is an IP address, no network transactions will be performed. |
Christopher Haster |
104:d28d8b508e7c | 80 | * |
Christopher Haster |
104:d28d8b508e7c | 81 | * On failure, the IP address and port will be set to zero |
Christopher Haster |
104:d28d8b508e7c | 82 | * |
Christopher Haster |
104:d28d8b508e7c | 83 | * @param iface Network stack to use for DNS resolution |
Christopher Haster |
104:d28d8b508e7c | 84 | * @param host Hostname to resolve |
Christopher Haster |
94:644df37bb05b | 85 | * @param port Optional 16-bit port |
Christopher Haster |
89:b1d417383c0d | 86 | */ |
Christopher Haster |
105:2fd212f8da61 | 87 | SocketAddress(NetworkStack *iface, const char *host, uint16_t port = 0); |
Christopher Haster |
89:b1d417383c0d | 88 | |
Christopher Haster |
104:d28d8b508e7c | 89 | /** Create a SocketAddress from an IP address and port |
Christopher Haster |
104:d28d8b508e7c | 90 | * |
Christopher Haster |
104:d28d8b508e7c | 91 | * @param host Null-terminated representation of the IP address |
Christopher Haster |
94:644df37bb05b | 92 | * @param port Optional 16-bit port |
Christopher Haster |
89:b1d417383c0d | 93 | */ |
Christopher Haster |
89:b1d417383c0d | 94 | SocketAddress(const char *addr = 0, uint16_t port = 0); |
Christopher Haster |
89:b1d417383c0d | 95 | |
Christopher Haster |
104:d28d8b508e7c | 96 | /** Create a SocketAddress from a raw IP address and port |
Christopher Haster |
104:d28d8b508e7c | 97 | * |
Christopher Haster |
104:d28d8b508e7c | 98 | * @param bytes Raw IP address in big-endian order |
Christopher Haster |
94:644df37bb05b | 99 | * @param version IP address version, NSAPI_IPv4 or NSAPI_IPv6 |
Christopher Haster |
94:644df37bb05b | 100 | * @param port Optional 16-bit port |
Christopher Haster |
94:644df37bb05b | 101 | */ |
Christopher Haster |
94:644df37bb05b | 102 | SocketAddress(const void *bytes, nsapi_version_t version, uint16_t port = 0); |
Christopher Haster |
94:644df37bb05b | 103 | |
Christopher Haster |
104:d28d8b508e7c | 104 | /** Create a SocketAddress from another SocketAddress |
Christopher Haster |
104:d28d8b508e7c | 105 | * |
Christopher Haster |
104:d28d8b508e7c | 106 | * @param address SocketAddress to copy |
Christopher Haster |
89:b1d417383c0d | 107 | */ |
Christopher Haster |
89:b1d417383c0d | 108 | SocketAddress(const SocketAddress &addr); |
Christopher Haster |
89:b1d417383c0d | 109 | |
Christopher Haster |
89:b1d417383c0d | 110 | /** Set the IP address |
Christopher Haster |
104:d28d8b508e7c | 111 | * |
Christopher Haster |
104:d28d8b508e7c | 112 | * @param addr Null-terminated represention of the IP address |
Christopher Haster |
89:b1d417383c0d | 113 | */ |
Christopher Haster |
89:b1d417383c0d | 114 | void set_ip_address(const char *addr); |
Christopher Haster |
89:b1d417383c0d | 115 | |
Christopher Haster |
104:d28d8b508e7c | 116 | /** Set the raw IP address |
Christopher Haster |
104:d28d8b508e7c | 117 | * |
Christopher Haster |
104:d28d8b508e7c | 118 | * @param bytes Raw IP address in big-endian order |
Christopher Haster |
94:644df37bb05b | 119 | * @param version IP address version, NSAPI_IPv4 or NSAPI_IPv6 |
Christopher Haster |
94:644df37bb05b | 120 | */ |
Christopher Haster |
94:644df37bb05b | 121 | void set_ip_bytes(const void *bytes, nsapi_version_t version); |
Christopher Haster |
94:644df37bb05b | 122 | |
Christopher Haster |
89:b1d417383c0d | 123 | /** Set the port |
Christopher Haster |
104:d28d8b508e7c | 124 | * |
Christopher Haster |
94:644df37bb05b | 125 | * @param port 16-bit port |
Christopher Haster |
89:b1d417383c0d | 126 | */ |
Christopher Haster |
89:b1d417383c0d | 127 | void set_port(uint16_t port); |
Christopher Haster |
89:b1d417383c0d | 128 | |
Christopher Haster |
89:b1d417383c0d | 129 | /** Get the IP address |
Christopher Haster |
104:d28d8b508e7c | 130 | * |
Christopher Haster |
104:d28d8b508e7c | 131 | * @return Null-terminated representation of the IP Address |
Christopher Haster |
89:b1d417383c0d | 132 | */ |
Christopher Haster |
89:b1d417383c0d | 133 | const char *get_ip_address() const; |
Christopher Haster |
94:644df37bb05b | 134 | |
Christopher Haster |
104:d28d8b508e7c | 135 | /** Get the raw IP address |
Christopher Haster |
104:d28d8b508e7c | 136 | * |
Christopher Haster |
104:d28d8b508e7c | 137 | * @return Raw IP address in big-endian order |
Christopher Haster |
94:644df37bb05b | 138 | */ |
Christopher Haster |
94:644df37bb05b | 139 | const void *get_ip_bytes() const; |
Christopher Haster |
94:644df37bb05b | 140 | |
Christopher Haster |
104:d28d8b508e7c | 141 | /** Get the IP address version |
Christopher Haster |
104:d28d8b508e7c | 142 | * |
Christopher Haster |
94:644df37bb05b | 143 | * @return IP address version, NSAPI_IPv4 or NSAPI_IPv6 |
Christopher Haster |
94:644df37bb05b | 144 | */ |
Christopher Haster |
94:644df37bb05b | 145 | nsapi_version_t get_ip_version() const; |
Christopher Haster |
89:b1d417383c0d | 146 | |
Christopher Haster |
89:b1d417383c0d | 147 | /** Get the port |
Christopher Haster |
104:d28d8b508e7c | 148 | * |
Christopher Haster |
94:644df37bb05b | 149 | * @return The 16-bit port |
Christopher Haster |
89:b1d417383c0d | 150 | */ |
Christopher Haster |
94:644df37bb05b | 151 | uint16_t get_port() const; |
Christopher Haster |
94:644df37bb05b | 152 | |
Christopher Haster |
104:d28d8b508e7c | 153 | /** Test if address is zero |
Christopher Haster |
104:d28d8b508e7c | 154 | * |
Christopher Haster |
104:d28d8b508e7c | 155 | * @return True if address is not zero |
Christopher Haster |
94:644df37bb05b | 156 | */ |
Christopher Haster |
94:644df37bb05b | 157 | operator bool() const; |
Christopher Haster |
89:b1d417383c0d | 158 | |
Christopher Haster |
89:b1d417383c0d | 159 | private: |
Christopher Haster |
89:b1d417383c0d | 160 | char _ip_address[NSAPI_IP_SIZE]; |
Christopher Haster |
94:644df37bb05b | 161 | uint8_t _ip_bytes[NSAPI_IP_BYTES]; |
Christopher Haster |
94:644df37bb05b | 162 | nsapi_version_t _ip_version; |
Christopher Haster |
89:b1d417383c0d | 163 | uint16_t _port; |
Christopher Haster |
89:b1d417383c0d | 164 | }; |
Christopher Haster |
89:b1d417383c0d | 165 | |
Christopher Haster |
89:b1d417383c0d | 166 | #endif |