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 05 12:02:56 2016 -0500
Revision:
80:9c6673c93082
Parent:
79:43a7e8c0d6cc
Child:
87:94e2cf3a06be
Added support for DNS resolution

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Christopher Haster 78:0914f9b9b24b 1 /* Socket
Christopher Haster 78:0914f9b9b24b 2 * Copyright (c) 2015 ARM Limited
Christopher Haster 75:dea0cdb42241 3 *
Christopher Haster 78:0914f9b9b24b 4 * Licensed under the Apache License, Version 2.0 (the "License");
Christopher Haster 78:0914f9b9b24b 5 * you may not use this file except in compliance with the License.
Christopher Haster 78:0914f9b9b24b 6 * You may obtain a copy of the License at
Christopher Haster 75:dea0cdb42241 7 *
Christopher Haster 78:0914f9b9b24b 8 * http://www.apache.org/licenses/LICENSE-2.0
Christopher Haster 75:dea0cdb42241 9 *
Christopher Haster 78:0914f9b9b24b 10 * Unless required by applicable law or agreed to in writing, software
Christopher Haster 78:0914f9b9b24b 11 * distributed under the License is distributed on an "AS IS" BASIS,
Christopher Haster 78:0914f9b9b24b 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Christopher Haster 78:0914f9b9b24b 13 * See the License for the specific language governing permissions and
Christopher Haster 78:0914f9b9b24b 14 * limitations under the License.
Christopher Haster 75:dea0cdb42241 15 */
Christopher Haster 78:0914f9b9b24b 16
Christopher Haster 76:bbe51641f405 17 #ifndef SOCKET_ADDRESS_H
Christopher Haster 76:bbe51641f405 18 #define SOCKET_ADDRESS_H
Christopher Haster 75:dea0cdb42241 19
Christopher Haster 79:43a7e8c0d6cc 20 #include <stdint.h>
Christopher Haster 79:43a7e8c0d6cc 21
Christopher Haster 80:9c6673c93082 22 // Predeclared classes
Christopher Haster 80:9c6673c93082 23 class NetworkInterface;
Christopher Haster 80:9c6673c93082 24
Christopher Haster 75:dea0cdb42241 25 /**
Christopher Haster 75:dea0cdb42241 26 * A general socket address composed of the IP address and port
Christopher Haster 75:dea0cdb42241 27 */
Christopher Haster 75:dea0cdb42241 28 class SocketAddress {
Christopher Haster 75:dea0cdb42241 29 public:
Christopher Haster 80:9c6673c93082 30 /** Maximum size of IP address
Christopher Haster 80:9c6673c93082 31 */
Christopher Haster 80:9c6673c93082 32 static const int IP_SIZE = 16;
Christopher Haster 80:9c6673c93082 33
Christopher Haster 80:9c6673c93082 34 /** SocketAddress construction using DNS resolution
Christopher Haster 80:9c6673c93082 35 /param iface NetworkInterface to use for DNS resolution
Christopher Haster 80:9c6673c93082 36 /param addr Null-terminated hostname that will be resolved
Christopher Haster 80:9c6673c93082 37 /param port 16-bit port
Christopher Haster 80:9c6673c93082 38 /note on failure, IP address and port will be set to null
Christopher Haster 80:9c6673c93082 39 */
Christopher Haster 80:9c6673c93082 40 SocketAddress(NetworkInterface *iface, const char *addr, uint16_t port = 0);
Christopher Haster 80:9c6673c93082 41
Christopher Haster 79:43a7e8c0d6cc 42 /** SocketAddress construction
Christopher Haster 80:9c6673c93082 43 /param addr Null-terminated IP address
Christopher Haster 80:9c6673c93082 44 /param port 16-bit port
Christopher Haster 80:9c6673c93082 45 /note on failure, IP address and port will be set to null
Christopher Haster 80:9c6673c93082 46 */
Christopher Haster 76:bbe51641f405 47 SocketAddress(const char *addr = 0, uint16_t port = 0);
Christopher Haster 80:9c6673c93082 48
Christopher Haster 80:9c6673c93082 49 /** SocketAddress construction
Christopher Haster 80:9c6673c93082 50 /param addr SocketAddress to copy
Christopher Haster 80:9c6673c93082 51 */
Christopher Haster 80:9c6673c93082 52 SocketAddress(const SocketAddress &addr);
Christopher Haster 75:dea0cdb42241 53
Christopher Haster 75:dea0cdb42241 54 /** Set the IP address
Christopher Haster 80:9c6673c93082 55 \param addr Null-terminated string representing the IP address
Christopher Haster 75:dea0cdb42241 56 */
Christopher Haster 79:43a7e8c0d6cc 57 void set_ip_address(const char *addr);
Christopher Haster 75:dea0cdb42241 58
Christopher Haster 75:dea0cdb42241 59 /** Set the port
Christopher Haster 80:9c6673c93082 60 \param port 16-bit port
Christopher Haster 75:dea0cdb42241 61 */
Christopher Haster 75:dea0cdb42241 62 void set_port(uint16_t port);
Christopher Haster 75:dea0cdb42241 63
Christopher Haster 75:dea0cdb42241 64 /** Get the IP address
Christopher Haster 80:9c6673c93082 65 \return The string representation of the IP Address
Christopher Haster 75:dea0cdb42241 66 */
Christopher Haster 79:43a7e8c0d6cc 67 const char *get_ip_address() const;
Christopher Haster 75:dea0cdb42241 68
Christopher Haster 75:dea0cdb42241 69 /** Get the port
Christopher Haster 80:9c6673c93082 70 \return The 16-bit port
Christopher Haster 75:dea0cdb42241 71 */
Christopher Haster 79:43a7e8c0d6cc 72 uint16_t get_port(void) const;
Christopher Haster 79:43a7e8c0d6cc 73
Christopher Haster 79:43a7e8c0d6cc 74 private:
Christopher Haster 80:9c6673c93082 75 char _ip_address[IP_SIZE];
Christopher Haster 79:43a7e8c0d6cc 76 uint16_t _port;
Christopher Haster 75:dea0cdb42241 77 };
Christopher Haster 75:dea0cdb42241 78
Christopher Haster 75:dea0cdb42241 79 #endif