A new object oriented network api that can be used to replace the one provided by the EthernetInterface library.

Dependents:   NetRelais TCP_Client_Example TCP_Server_Example UDP_Server_Example ... more

Object oriented network interface for the mbed platform

Currently implemented:

  • Address
  • Endpoint
  • UDP Socket
  • TCP Socket
  • Databuffer
  • Select API

It depends on the EthernetInterface for the lwip network stack.

Please do not hesitate to contact me with any remarks, improvements or questions.

The API is also available for unix at GitHub: LibNosa

Examples

Committer:
NegativeBlack
Date:
Tue Jul 17 18:31:22 2012 +0000
Revision:
2:e283a0062097
Parent:
1:6956f6f96fef
Child:
3:d30db8752485
Implemented udp socket class, needs to be tested.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
NegativeBlack 0:00d5bc4b46e1 1 /**
NegativeBlack 0:00d5bc4b46e1 2 * Copyright (c) 2012, Roy van Dam <roy@vandam-innovations.com>
NegativeBlack 0:00d5bc4b46e1 3 * All rights reserved.
NegativeBlack 0:00d5bc4b46e1 4 *
NegativeBlack 0:00d5bc4b46e1 5 * Redistribution and use in source and binary forms, with or without
NegativeBlack 0:00d5bc4b46e1 6 * modification, are permitted provided that the following conditions are met:
NegativeBlack 0:00d5bc4b46e1 7 *
NegativeBlack 0:00d5bc4b46e1 8 * 1. Redistributions of source code must retain the above copyright notice, this
NegativeBlack 0:00d5bc4b46e1 9 * list of conditions and the following disclaimer.
NegativeBlack 0:00d5bc4b46e1 10 * 2. Redistributions in binary form must reproduce the above copyright notice,
NegativeBlack 0:00d5bc4b46e1 11 * this list of conditions and the following disclaimer in the documentation
NegativeBlack 0:00d5bc4b46e1 12 * and/or other materials provided with the distribution.
NegativeBlack 0:00d5bc4b46e1 13 *
NegativeBlack 0:00d5bc4b46e1 14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
NegativeBlack 0:00d5bc4b46e1 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
NegativeBlack 0:00d5bc4b46e1 16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
NegativeBlack 0:00d5bc4b46e1 17 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
NegativeBlack 0:00d5bc4b46e1 18 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
NegativeBlack 0:00d5bc4b46e1 19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
NegativeBlack 0:00d5bc4b46e1 20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
NegativeBlack 0:00d5bc4b46e1 21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
NegativeBlack 0:00d5bc4b46e1 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
NegativeBlack 0:00d5bc4b46e1 23 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
NegativeBlack 0:00d5bc4b46e1 24 */
NegativeBlack 0:00d5bc4b46e1 25
NegativeBlack 1:6956f6f96fef 26 #include "address.hpp"
NegativeBlack 0:00d5bc4b46e1 27 using namespace network::ip;
NegativeBlack 0:00d5bc4b46e1 28
NegativeBlack 0:00d5bc4b46e1 29 Address::Address()
NegativeBlack 0:00d5bc4b46e1 30 {
NegativeBlack 0:00d5bc4b46e1 31 std::memset(&this->_address, 0, sizeof(this->_address));
NegativeBlack 0:00d5bc4b46e1 32 }
NegativeBlack 0:00d5bc4b46e1 33
NegativeBlack 0:00d5bc4b46e1 34 Address::Address(const int address)
NegativeBlack 0:00d5bc4b46e1 35 {
NegativeBlack 0:00d5bc4b46e1 36 this->fromNative(address);
NegativeBlack 0:00d5bc4b46e1 37 }
NegativeBlack 0:00d5bc4b46e1 38
NegativeBlack 0:00d5bc4b46e1 39 Address::Address(const Address &other)
NegativeBlack 0:00d5bc4b46e1 40 {
NegativeBlack 0:00d5bc4b46e1 41 std::memcpy(&this->_address, &other._address, sizeof(this->_address));
NegativeBlack 0:00d5bc4b46e1 42 }
NegativeBlack 0:00d5bc4b46e1 43
NegativeBlack 0:00d5bc4b46e1 44 Address::Address(const char *address)
NegativeBlack 0:00d5bc4b46e1 45 {
NegativeBlack 0:00d5bc4b46e1 46 std::string _address(address);
NegativeBlack 0:00d5bc4b46e1 47 if (this->fromString(_address) < 0) {
NegativeBlack 0:00d5bc4b46e1 48 std::memset(&this->_address, 0, sizeof(this->_address));
NegativeBlack 0:00d5bc4b46e1 49 }
NegativeBlack 0:00d5bc4b46e1 50 }
NegativeBlack 0:00d5bc4b46e1 51
NegativeBlack 0:00d5bc4b46e1 52 Address::Address(const std::string &address)
NegativeBlack 0:00d5bc4b46e1 53 {
NegativeBlack 0:00d5bc4b46e1 54 if (this->fromString(address) < 0) {
NegativeBlack 0:00d5bc4b46e1 55 std::memset(&this->_address, 0, sizeof(this->_address));
NegativeBlack 0:00d5bc4b46e1 56 }
NegativeBlack 0:00d5bc4b46e1 57 }
NegativeBlack 2:e283a0062097 58
NegativeBlack 2:e283a0062097 59 int
NegativeBlack 2:e283a0062097 60 Address::fromString(const char *address)
NegativeBlack 2:e283a0062097 61 {
NegativeBlack 2:e283a0062097 62 if (address == NULL) {
NegativeBlack 2:e283a0062097 63 return -1;
NegativeBlack 2:e283a0062097 64 }
NegativeBlack 2:e283a0062097 65
NegativeBlack 2:e283a0062097 66 std::string _address(address);
NegativeBlack 2:e283a0062097 67 return this->fromString(_address);
NegativeBlack 2:e283a0062097 68 }
NegativeBlack 0:00d5bc4b46e1 69
NegativeBlack 0:00d5bc4b46e1 70 int
NegativeBlack 0:00d5bc4b46e1 71 Address::fromString(const std::string &address)
NegativeBlack 0:00d5bc4b46e1 72 {
NegativeBlack 0:00d5bc4b46e1 73 // Decode the ASCI string into integer values.
NegativeBlack 0:00d5bc4b46e1 74 int result = std::sscanf(address.c_str(), "%3u.%3u.%3u.%3u",
NegativeBlack 0:00d5bc4b46e1 75 (int *)&this->_address[3],
NegativeBlack 0:00d5bc4b46e1 76 (int *)&this->_address[2],
NegativeBlack 0:00d5bc4b46e1 77 (int *)&this->_address[1],
NegativeBlack 0:00d5bc4b46e1 78 (int *)&this->_address[0]);
NegativeBlack 0:00d5bc4b46e1 79
NegativeBlack 0:00d5bc4b46e1 80 // Check if all four fields got set.
NegativeBlack 0:00d5bc4b46e1 81 if (result != 4) {
NegativeBlack 0:00d5bc4b46e1 82 return -1;
NegativeBlack 0:00d5bc4b46e1 83 }
NegativeBlack 0:00d5bc4b46e1 84
NegativeBlack 0:00d5bc4b46e1 85 return 0;
NegativeBlack 0:00d5bc4b46e1 86 }
NegativeBlack 0:00d5bc4b46e1 87
NegativeBlack 0:00d5bc4b46e1 88 std::string
NegativeBlack 0:00d5bc4b46e1 89 Address::toString()
NegativeBlack 0:00d5bc4b46e1 90 {
NegativeBlack 0:00d5bc4b46e1 91 char address[16];
NegativeBlack 0:00d5bc4b46e1 92
NegativeBlack 0:00d5bc4b46e1 93 // Encode integer fields into the ASCI string.
NegativeBlack 1:6956f6f96fef 94 int result = std::sprintf(address, "%u.%u.%u.%u",
NegativeBlack 0:00d5bc4b46e1 95 (unsigned int)this->_address[3],
NegativeBlack 0:00d5bc4b46e1 96 (unsigned int)this->_address[2],
NegativeBlack 0:00d5bc4b46e1 97 (unsigned int)this->_address[1],
NegativeBlack 0:00d5bc4b46e1 98 (unsigned int)this->_address[0]);
NegativeBlack 0:00d5bc4b46e1 99
NegativeBlack 0:00d5bc4b46e1 100 // Check if atleast 8 and at maximum 15 bytes got written.
NegativeBlack 0:00d5bc4b46e1 101 if (result < 8 || result > 16) {
NegativeBlack 0:00d5bc4b46e1 102 return std::string("0.0.0.0");
NegativeBlack 0:00d5bc4b46e1 103 }
NegativeBlack 0:00d5bc4b46e1 104
NegativeBlack 0:00d5bc4b46e1 105 return std::string(address);
NegativeBlack 0:00d5bc4b46e1 106 }
NegativeBlack 0:00d5bc4b46e1 107
NegativeBlack 0:00d5bc4b46e1 108 int
NegativeBlack 0:00d5bc4b46e1 109 Address::fromHostname(const char *hostname)
NegativeBlack 0:00d5bc4b46e1 110 {
NegativeBlack 0:00d5bc4b46e1 111 if (hostname == NULL) {
NegativeBlack 0:00d5bc4b46e1 112 return -1;
NegativeBlack 0:00d5bc4b46e1 113 }
NegativeBlack 0:00d5bc4b46e1 114
NegativeBlack 2:e283a0062097 115 std::string _hostname(hostname);
NegativeBlack 2:e283a0062097 116 return this->fromHostname(_hostname);
NegativeBlack 2:e283a0062097 117 }
NegativeBlack 2:e283a0062097 118
NegativeBlack 2:e283a0062097 119 int
NegativeBlack 2:e283a0062097 120 Address::fromHostname(const std::string &hostname)
NegativeBlack 2:e283a0062097 121 {
NegativeBlack 2:e283a0062097 122 struct hostent *address = ::gethostbyname(hostname.c_str());
NegativeBlack 0:00d5bc4b46e1 123 if (address == NULL) {
NegativeBlack 0:00d5bc4b46e1 124 return -1;
NegativeBlack 0:00d5bc4b46e1 125 }
NegativeBlack 0:00d5bc4b46e1 126
NegativeBlack 0:00d5bc4b46e1 127 std::memcpy(this->_address, address->h_addr_list[0], sizeof(this->_address));
NegativeBlack 0:00d5bc4b46e1 128 // Todo: Free hostent structure? I have no idea...
NegativeBlack 0:00d5bc4b46e1 129
NegativeBlack 0:00d5bc4b46e1 130 return 0;
NegativeBlack 0:00d5bc4b46e1 131 }
NegativeBlack 0:00d5bc4b46e1 132
NegativeBlack 0:00d5bc4b46e1 133 void
NegativeBlack 0:00d5bc4b46e1 134 Address::fromNative(const int address)
NegativeBlack 0:00d5bc4b46e1 135 {
NegativeBlack 0:00d5bc4b46e1 136 // Todo: Check byteorder
NegativeBlack 0:00d5bc4b46e1 137 std::memcpy(&this->_address, &address, sizeof(address));
NegativeBlack 0:00d5bc4b46e1 138 }
NegativeBlack 0:00d5bc4b46e1 139
NegativeBlack 0:00d5bc4b46e1 140 int
NegativeBlack 0:00d5bc4b46e1 141 Address::toNative()
NegativeBlack 0:00d5bc4b46e1 142 {
NegativeBlack 0:00d5bc4b46e1 143 int address;
NegativeBlack 0:00d5bc4b46e1 144
NegativeBlack 0:00d5bc4b46e1 145 // Todo: Check byteorder
NegativeBlack 0:00d5bc4b46e1 146 std::memcpy(&address, &this->_address, sizeof(address));
NegativeBlack 0:00d5bc4b46e1 147
NegativeBlack 0:00d5bc4b46e1 148 return address;
NegativeBlack 0:00d5bc4b46e1 149 }
NegativeBlack 0:00d5bc4b46e1 150
NegativeBlack 0:00d5bc4b46e1 151 Address &
NegativeBlack 0:00d5bc4b46e1 152 Address::operator=(const Address &other) {
NegativeBlack 0:00d5bc4b46e1 153 std::memcpy(&this->_address, &other._address, sizeof(this->_address));
NegativeBlack 0:00d5bc4b46e1 154 return (*this);
NegativeBlack 0:00d5bc4b46e1 155 }
NegativeBlack 0:00d5bc4b46e1 156
NegativeBlack 0:00d5bc4b46e1 157 bool
NegativeBlack 0:00d5bc4b46e1 158 Address::operator==(const Address &other) {
NegativeBlack 0:00d5bc4b46e1 159 return (std::memcmp(&this->_address, &other._address, sizeof(this->_address)) == 0);
NegativeBlack 0:00d5bc4b46e1 160 }
NegativeBlack 0:00d5bc4b46e1 161
NegativeBlack 0:00d5bc4b46e1 162 bool
NegativeBlack 0:00d5bc4b46e1 163 Address::operator!=(const Address &other) {
NegativeBlack 0:00d5bc4b46e1 164 return !((*this) == other);
NegativeBlack 0:00d5bc4b46e1 165 }