Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: NetRelais TCP_Client_Example TCP_Server_Example UDP_Server_Example ... more
ip/address.cpp@3:d30db8752485, 2012-07-18 (annotated)
- Committer:
- NegativeBlack
- Date:
- Wed Jul 18 11:22:37 2012 +0000
- Revision:
- 3:d30db8752485
- Parent:
- 2:e283a0062097
- Child:
- 8:cdee0f2b6ff0
Implemented UDP and TCP socket. UDP is fully tested, TCP still needs to be tested.
Who changed what in which revision?
| User | Revision | Line number | New 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 | 3:d30db8752485 | 75 | &this->_address[0], | 
| NegativeBlack | 3:d30db8752485 | 76 | &this->_address[1], | 
| NegativeBlack | 3:d30db8752485 | 77 | &this->_address[2], | 
| NegativeBlack | 3:d30db8752485 | 78 | &this->_address[3]); | 
| 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 | 3:d30db8752485 | 95 | (int)this->_address[0], | 
| NegativeBlack | 3:d30db8752485 | 96 | (int)this->_address[1], | 
| NegativeBlack | 3:d30db8752485 | 97 | (int)this->_address[2], | 
| NegativeBlack | 3:d30db8752485 | 98 | (int)this->_address[3]); | 
| NegativeBlack | 0:00d5bc4b46e1 | 99 | |
| NegativeBlack | 3:d30db8752485 | 100 | // Check if atleast 8 and at maximum 15 bytes has been 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 | 3:d30db8752485 | 122 | // Resolve hostname by DNS | 
| NegativeBlack | 2:e283a0062097 | 123 | struct hostent *address = ::gethostbyname(hostname.c_str()); | 
| NegativeBlack | 0:00d5bc4b46e1 | 124 | if (address == NULL) { | 
| NegativeBlack | 0:00d5bc4b46e1 | 125 | return -1; | 
| NegativeBlack | 0:00d5bc4b46e1 | 126 | } | 
| NegativeBlack | 0:00d5bc4b46e1 | 127 | |
| NegativeBlack | 3:d30db8752485 | 128 | // Check if the address has the correct size | 
| NegativeBlack | 3:d30db8752485 | 129 | if (address->h_length != sizeof(this->_address)) { | 
| NegativeBlack | 3:d30db8752485 | 130 | return -1; | 
| NegativeBlack | 3:d30db8752485 | 131 | } | 
| NegativeBlack | 3:d30db8752485 | 132 | |
| NegativeBlack | 3:d30db8752485 | 133 | // Copy network address | 
| NegativeBlack | 3:d30db8752485 | 134 | this->_address[0] = address->h_addr_list[0][0]; | 
| NegativeBlack | 3:d30db8752485 | 135 | this->_address[1] = address->h_addr_list[0][1]; | 
| NegativeBlack | 3:d30db8752485 | 136 | this->_address[2] = address->h_addr_list[0][2]; | 
| NegativeBlack | 3:d30db8752485 | 137 | this->_address[3] = address->h_addr_list[0][3]; | 
| NegativeBlack | 3:d30db8752485 | 138 | |
| NegativeBlack | 0:00d5bc4b46e1 | 139 | // Todo: Free hostent structure? I have no idea... | 
| NegativeBlack | 0:00d5bc4b46e1 | 140 | return 0; | 
| NegativeBlack | 0:00d5bc4b46e1 | 141 | } | 
| NegativeBlack | 0:00d5bc4b46e1 | 142 | |
| NegativeBlack | 0:00d5bc4b46e1 | 143 | void | 
| NegativeBlack | 3:d30db8752485 | 144 | Address::fromNative(int address) | 
| NegativeBlack | 0:00d5bc4b46e1 | 145 | { | 
| NegativeBlack | 3:d30db8752485 | 146 | // Copy address | 
| NegativeBlack | 3:d30db8752485 | 147 | std::memcpy(this->_address, &address, sizeof(this->_address)); | 
| NegativeBlack | 0:00d5bc4b46e1 | 148 | } | 
| NegativeBlack | 0:00d5bc4b46e1 | 149 | |
| NegativeBlack | 0:00d5bc4b46e1 | 150 | int | 
| NegativeBlack | 0:00d5bc4b46e1 | 151 | Address::toNative() | 
| NegativeBlack | 0:00d5bc4b46e1 | 152 | { | 
| NegativeBlack | 0:00d5bc4b46e1 | 153 | int address; | 
| NegativeBlack | 0:00d5bc4b46e1 | 154 | |
| NegativeBlack | 3:d30db8752485 | 155 | // Copy address | 
| NegativeBlack | 3:d30db8752485 | 156 | std::memcpy(&address, this->_address, sizeof(address)); | 
| NegativeBlack | 0:00d5bc4b46e1 | 157 | |
| NegativeBlack | 0:00d5bc4b46e1 | 158 | return address; | 
| NegativeBlack | 0:00d5bc4b46e1 | 159 | } | 
| NegativeBlack | 0:00d5bc4b46e1 | 160 | |
| NegativeBlack | 3:d30db8752485 | 161 | bool | 
| NegativeBlack | 3:d30db8752485 | 162 | Address::isEmpty() | 
| NegativeBlack | 3:d30db8752485 | 163 | { | 
| NegativeBlack | 3:d30db8752485 | 164 | return ((this->_address[0] == 0) && | 
| NegativeBlack | 3:d30db8752485 | 165 | (this->_address[1] == 0) && | 
| NegativeBlack | 3:d30db8752485 | 166 | (this->_address[2] == 0) && | 
| NegativeBlack | 3:d30db8752485 | 167 | (this->_address[3] == 0)); | 
| NegativeBlack | 3:d30db8752485 | 168 | } | 
| NegativeBlack | 3:d30db8752485 | 169 | |
| NegativeBlack | 0:00d5bc4b46e1 | 170 | Address & | 
| NegativeBlack | 0:00d5bc4b46e1 | 171 | Address::operator=(const Address &other) { | 
| NegativeBlack | 0:00d5bc4b46e1 | 172 | std::memcpy(&this->_address, &other._address, sizeof(this->_address)); | 
| NegativeBlack | 0:00d5bc4b46e1 | 173 | return (*this); | 
| NegativeBlack | 0:00d5bc4b46e1 | 174 | } | 
| NegativeBlack | 0:00d5bc4b46e1 | 175 | |
| NegativeBlack | 0:00d5bc4b46e1 | 176 | bool | 
| NegativeBlack | 0:00d5bc4b46e1 | 177 | Address::operator==(const Address &other) { | 
| NegativeBlack | 0:00d5bc4b46e1 | 178 | return (std::memcmp(&this->_address, &other._address, sizeof(this->_address)) == 0); | 
| NegativeBlack | 0:00d5bc4b46e1 | 179 | } | 
| NegativeBlack | 0:00d5bc4b46e1 | 180 | |
| NegativeBlack | 0:00d5bc4b46e1 | 181 | bool | 
| NegativeBlack | 0:00d5bc4b46e1 | 182 | Address::operator!=(const Address &other) { | 
| NegativeBlack | 0:00d5bc4b46e1 | 183 | return !((*this) == other); | 
| NegativeBlack | 0:00d5bc4b46e1 | 184 | } |