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.

Committer:
Christopher Haster
Date:
Tue Apr 19 18:23:58 2016 -0500
Revision:
94:644df37bb05b
Parent:
89:b1d417383c0d
Child:
104:d28d8b508e7c
Added support for storing bytes directly in SocketAddress

Bytes are stored by default, however enough space is allocated in
a SocketAddress to generate the string representation if necessary.

Currently there is no support for shortened addresses

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Christopher Haster 89:b1d417383c0d 1 /* Socket
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 94:644df37bb05b 31 /** Enum of address families
Christopher Haster 94:644df37bb05b 32 * @enum nsapi_family_t
Christopher Haster 94:644df37bb05b 33 */
Christopher Haster 94:644df37bb05b 34 enum nsapi_version_t {
Christopher Haster 94:644df37bb05b 35 NSAPI_IPv4, /*!< Address is IPv4 */
Christopher Haster 94:644df37bb05b 36 NSAPI_IPv6, /*!< Address is IPv6 */
Christopher Haster 94:644df37bb05b 37 };
Christopher Haster 94:644df37bb05b 38
Christopher Haster 94:644df37bb05b 39 /** Size of IPv4 representation
Christopher Haster 94:644df37bb05b 40 */
Christopher Haster 94:644df37bb05b 41 #define NSAPI_IPv4_SIZE 16
Christopher Haster 94:644df37bb05b 42
Christopher Haster 94:644df37bb05b 43 /** Number of bytes in IPv4 address
Christopher Haster 94:644df37bb05b 44 */
Christopher Haster 94:644df37bb05b 45 #define NSAPI_IPv4_BYTES 4
Christopher Haster 94:644df37bb05b 46
Christopher Haster 94:644df37bb05b 47 /** Size of IPv6 representation
Christopher Haster 94:644df37bb05b 48 */
Christopher Haster 94:644df37bb05b 49 #define NSAPI_IPv6_SIZE 40
Christopher Haster 94:644df37bb05b 50
Christopher Haster 94:644df37bb05b 51 /** Number of bytes in IPv6 address
Christopher Haster 94:644df37bb05b 52 */
Christopher Haster 94:644df37bb05b 53 #define NSAPI_IPv6_BYTES 16
Christopher Haster 89:b1d417383c0d 54
Christopher Haster 89:b1d417383c0d 55 // Predeclared classes
Christopher Haster 89:b1d417383c0d 56 class NetworkInterface;
Christopher Haster 89:b1d417383c0d 57
Christopher Haster 94:644df37bb05b 58
Christopher Haster 94:644df37bb05b 59 /** A general address class composed of the IP address and optional port
Christopher Haster 89:b1d417383c0d 60 */
Christopher Haster 89:b1d417383c0d 61 class SocketAddress {
Christopher Haster 89:b1d417383c0d 62 public:
Christopher Haster 89:b1d417383c0d 63 /** SocketAddress construction using DNS resolution
Christopher Haster 94:644df37bb05b 64 * @param iface NetworkInterface to use for DNS resolution
Christopher Haster 94:644df37bb05b 65 * @param addr Null-terminated hostname that will be resolved
Christopher Haster 94:644df37bb05b 66 * @param port Optional 16-bit port
Christopher Haster 94:644df37bb05b 67 * @note on failure, IP address and port will be set to zero
Christopher Haster 89:b1d417383c0d 68 */
Christopher Haster 89:b1d417383c0d 69 SocketAddress(NetworkInterface *iface, const char *addr, uint16_t port = 0);
Christopher Haster 89:b1d417383c0d 70
Christopher Haster 89:b1d417383c0d 71 /** SocketAddress construction
Christopher Haster 94:644df37bb05b 72 * @param addr Null-terminated IP address
Christopher Haster 94:644df37bb05b 73 * @param port Optional 16-bit port
Christopher Haster 89:b1d417383c0d 74 */
Christopher Haster 89:b1d417383c0d 75 SocketAddress(const char *addr = 0, uint16_t port = 0);
Christopher Haster 89:b1d417383c0d 76
Christopher Haster 89:b1d417383c0d 77 /** SocketAddress construction
Christopher Haster 94:644df37bb05b 78 * @param bytes Bytes to assign to address in big-endian order
Christopher Haster 94:644df37bb05b 79 * @param version IP address version, NSAPI_IPv4 or NSAPI_IPv6
Christopher Haster 94:644df37bb05b 80 * @param port Optional 16-bit port
Christopher Haster 94:644df37bb05b 81 */
Christopher Haster 94:644df37bb05b 82 SocketAddress(const void *bytes, nsapi_version_t version, uint16_t port = 0);
Christopher Haster 94:644df37bb05b 83
Christopher Haster 94:644df37bb05b 84 /** SocketAddress construction
Christopher Haster 94:644df37bb05b 85 * @param addr SocketAddress to copy
Christopher Haster 89:b1d417383c0d 86 */
Christopher Haster 89:b1d417383c0d 87 SocketAddress(const SocketAddress &addr);
Christopher Haster 89:b1d417383c0d 88
Christopher Haster 89:b1d417383c0d 89 /** Set the IP address
Christopher Haster 94:644df37bb05b 90 * @param addr Null-terminated string representing the IP address
Christopher Haster 89:b1d417383c0d 91 */
Christopher Haster 89:b1d417383c0d 92 void set_ip_address(const char *addr);
Christopher Haster 89:b1d417383c0d 93
Christopher Haster 94:644df37bb05b 94 /** Set the IP address bytes directly
Christopher Haster 94:644df37bb05b 95 * @param bytes Bytes to assign to address in big-endian order
Christopher Haster 94:644df37bb05b 96 * @param version IP address version, NSAPI_IPv4 or NSAPI_IPv6
Christopher Haster 94:644df37bb05b 97 */
Christopher Haster 94:644df37bb05b 98 void set_ip_bytes(const void *bytes, nsapi_version_t version);
Christopher Haster 94:644df37bb05b 99
Christopher Haster 89:b1d417383c0d 100 /** Set the port
Christopher Haster 94:644df37bb05b 101 * @param port 16-bit port
Christopher Haster 89:b1d417383c0d 102 */
Christopher Haster 89:b1d417383c0d 103 void set_port(uint16_t port);
Christopher Haster 89:b1d417383c0d 104
Christopher Haster 89:b1d417383c0d 105 /** Get the IP address
Christopher Haster 94:644df37bb05b 106 * @return The string representation of the IP Address
Christopher Haster 89:b1d417383c0d 107 */
Christopher Haster 89:b1d417383c0d 108 const char *get_ip_address() const;
Christopher Haster 94:644df37bb05b 109
Christopher Haster 94:644df37bb05b 110 /** Get the IP address bytes directly
Christopher Haster 94:644df37bb05b 111 * @return IP address bytes
Christopher Haster 94:644df37bb05b 112 */
Christopher Haster 94:644df37bb05b 113 const void *get_ip_bytes() const;
Christopher Haster 94:644df37bb05b 114
Christopher Haster 94:644df37bb05b 115 /** Get the type of the IP address
Christopher Haster 94:644df37bb05b 116 * @return IP address version, NSAPI_IPv4 or NSAPI_IPv6
Christopher Haster 94:644df37bb05b 117 */
Christopher Haster 94:644df37bb05b 118 nsapi_version_t get_ip_version() const;
Christopher Haster 89:b1d417383c0d 119
Christopher Haster 89:b1d417383c0d 120 /** Get the port
Christopher Haster 94:644df37bb05b 121 * @return The 16-bit port
Christopher Haster 89:b1d417383c0d 122 */
Christopher Haster 94:644df37bb05b 123 uint16_t get_port() const;
Christopher Haster 94:644df37bb05b 124
Christopher Haster 94:644df37bb05b 125 /** Determine if address is all zeros
Christopher Haster 94:644df37bb05b 126 * @return True if address is not zero address
Christopher Haster 94:644df37bb05b 127 */
Christopher Haster 94:644df37bb05b 128 operator bool() const;
Christopher Haster 89:b1d417383c0d 129
Christopher Haster 89:b1d417383c0d 130 private:
Christopher Haster 89:b1d417383c0d 131 char _ip_address[NSAPI_IP_SIZE];
Christopher Haster 94:644df37bb05b 132 uint8_t _ip_bytes[NSAPI_IP_BYTES];
Christopher Haster 94:644df37bb05b 133 nsapi_version_t _ip_version;
Christopher Haster 89:b1d417383c0d 134 uint16_t _port;
Christopher Haster 89:b1d417383c0d 135 };
Christopher Haster 89:b1d417383c0d 136
Christopher Haster 89:b1d417383c0d 137 #endif