Roy van Dam / NetworkAPI

Dependents:   NetRelais TCP_Client_Example TCP_Server_Example UDP_Server_Example ... more

Committer:
NegativeBlack
Date:
Tue Jul 17 15:19:47 2012 +0000
Revision:
1:6956f6f96fef
Parent:
0:00d5bc4b46e1
Child:
3:d30db8752485
Fixed relative include paths.

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 "endpoint.hpp"
NegativeBlack 0:00d5bc4b46e1 27 using namespace network::ip;
NegativeBlack 0:00d5bc4b46e1 28
NegativeBlack 0:00d5bc4b46e1 29 Endpoint::Endpoint():
NegativeBlack 0:00d5bc4b46e1 30 _port(0)
NegativeBlack 0:00d5bc4b46e1 31 {}
NegativeBlack 0:00d5bc4b46e1 32
NegativeBlack 0:00d5bc4b46e1 33 Endpoint::Endpoint(const Endpoint &other):
NegativeBlack 0:00d5bc4b46e1 34 _address(other._address),
NegativeBlack 0:00d5bc4b46e1 35 _port(other._port)
NegativeBlack 0:00d5bc4b46e1 36 {}
NegativeBlack 0:00d5bc4b46e1 37
NegativeBlack 0:00d5bc4b46e1 38 Endpoint::Endpoint(const Address &address):
NegativeBlack 0:00d5bc4b46e1 39 _address(address),
NegativeBlack 0:00d5bc4b46e1 40 _port(0)
NegativeBlack 0:00d5bc4b46e1 41 {}
NegativeBlack 0:00d5bc4b46e1 42
NegativeBlack 0:00d5bc4b46e1 43 Endpoint::Endpoint(const Address &address, int port):
NegativeBlack 0:00d5bc4b46e1 44 _address(address),
NegativeBlack 0:00d5bc4b46e1 45 _port(port)
NegativeBlack 0:00d5bc4b46e1 46 {}
NegativeBlack 0:00d5bc4b46e1 47
NegativeBlack 0:00d5bc4b46e1 48 int
NegativeBlack 0:00d5bc4b46e1 49 Endpoint::setPort(int port)
NegativeBlack 0:00d5bc4b46e1 50 {
NegativeBlack 0:00d5bc4b46e1 51 if (port > 65536) {
NegativeBlack 0:00d5bc4b46e1 52 return -1;
NegativeBlack 0:00d5bc4b46e1 53 }
NegativeBlack 0:00d5bc4b46e1 54
NegativeBlack 0:00d5bc4b46e1 55 this->_port = port;
NegativeBlack 0:00d5bc4b46e1 56 return 0;
NegativeBlack 0:00d5bc4b46e1 57 }
NegativeBlack 0:00d5bc4b46e1 58
NegativeBlack 0:00d5bc4b46e1 59 int
NegativeBlack 0:00d5bc4b46e1 60 Endpoint::getPort()
NegativeBlack 0:00d5bc4b46e1 61 {
NegativeBlack 0:00d5bc4b46e1 62 return this->_port;
NegativeBlack 0:00d5bc4b46e1 63 }
NegativeBlack 0:00d5bc4b46e1 64
NegativeBlack 0:00d5bc4b46e1 65 void
NegativeBlack 0:00d5bc4b46e1 66 Endpoint::setAddress(const Address &address)
NegativeBlack 0:00d5bc4b46e1 67 {
NegativeBlack 0:00d5bc4b46e1 68 this->_address = address;
NegativeBlack 0:00d5bc4b46e1 69 }
NegativeBlack 0:00d5bc4b46e1 70
NegativeBlack 0:00d5bc4b46e1 71 Address &
NegativeBlack 0:00d5bc4b46e1 72 Endpoint::getAddress()
NegativeBlack 0:00d5bc4b46e1 73 {
NegativeBlack 0:00d5bc4b46e1 74 return this->_address;
NegativeBlack 0:00d5bc4b46e1 75 }
NegativeBlack 0:00d5bc4b46e1 76
NegativeBlack 0:00d5bc4b46e1 77 int
NegativeBlack 0:00d5bc4b46e1 78 Endpoint::toNative(struct sockaddr_in *endpoint)
NegativeBlack 0:00d5bc4b46e1 79 {
NegativeBlack 0:00d5bc4b46e1 80 if (endpoint == NULL) {
NegativeBlack 0:00d5bc4b46e1 81 return 1;
NegativeBlack 0:00d5bc4b46e1 82 }
NegativeBlack 0:00d5bc4b46e1 83
NegativeBlack 0:00d5bc4b46e1 84 // Clear structure
NegativeBlack 0:00d5bc4b46e1 85 std::memset(endpoint, 0, sizeof(struct sockaddr_in));
NegativeBlack 0:00d5bc4b46e1 86
NegativeBlack 0:00d5bc4b46e1 87 // Export endpoint
NegativeBlack 0:00d5bc4b46e1 88 endpoint->sin_family = AF_INET;
NegativeBlack 0:00d5bc4b46e1 89 endpoint->sin_port = htons(this->_port);
NegativeBlack 0:00d5bc4b46e1 90 endpoint->sin_addr.s_addr = this->_address.toNative();
NegativeBlack 0:00d5bc4b46e1 91 return 0;
NegativeBlack 0:00d5bc4b46e1 92 }
NegativeBlack 0:00d5bc4b46e1 93
NegativeBlack 0:00d5bc4b46e1 94 int
NegativeBlack 0:00d5bc4b46e1 95 Endpoint::fromNative(struct sockaddr_in *endpoint)
NegativeBlack 0:00d5bc4b46e1 96 {
NegativeBlack 0:00d5bc4b46e1 97 if (endpoint == NULL) {
NegativeBlack 0:00d5bc4b46e1 98 return 1;
NegativeBlack 0:00d5bc4b46e1 99 }
NegativeBlack 0:00d5bc4b46e1 100
NegativeBlack 0:00d5bc4b46e1 101 // Import endpoint
NegativeBlack 0:00d5bc4b46e1 102 this->_port = ntohs(endpoint->sin_port);
NegativeBlack 0:00d5bc4b46e1 103 this->_address.fromNative(endpoint->sin_addr.s_addr);
NegativeBlack 0:00d5bc4b46e1 104 return 0;
NegativeBlack 0:00d5bc4b46e1 105 }