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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers endpoint.cpp Source File

endpoint.cpp

00001 /**
00002  * Copyright (c) 2012, Roy van Dam <roy@vandam-innovations.com>
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  * 1. Redistributions of source code must retain the above copyright notice, this
00009  *    list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright notice,
00011  *    this list of conditions and the following disclaimer in the documentation
00012  *    and/or other materials provided with the distribution.
00013  *
00014  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00015  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00016  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00017  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
00018  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00019  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00020  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00021  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00022  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00023  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00024  */
00025  
00026 #include "endpoint.hpp"
00027 using namespace network::ip;
00028 
00029 Endpoint::Endpoint():
00030     _port(0)
00031 {}
00032 
00033 Endpoint::Endpoint(const Endpoint &other):
00034     _address(other._address),
00035     _port(other._port)
00036 {}
00037 
00038 Endpoint::Endpoint(const Address &address):
00039     _address(address),
00040     _port(0)
00041 {}
00042 
00043 Endpoint::Endpoint(const Address &address, int port):
00044     _address(address),
00045     _port(port)
00046 {}
00047 
00048 int
00049 Endpoint::setPort(int port)
00050 {
00051     if (port > 65536) {
00052         return -1;
00053     }
00054     
00055     this->_port = port;
00056     return 0;
00057 }
00058 
00059 int
00060 Endpoint::getPort()
00061 {
00062     return this->_port;
00063 }
00064 
00065 void
00066 Endpoint::setAddress(const Address &address)
00067 {
00068     this->_address = address;
00069 }
00070 
00071 Address &
00072 Endpoint::getAddress()
00073 {
00074     return this->_address;
00075 }
00076 
00077 int
00078 Endpoint::toNative(struct sockaddr_in *endpoint)
00079 {
00080     if (endpoint == NULL) {
00081         return 1;
00082     }
00083     
00084     // Clear structure
00085     std::memset(endpoint, 0, sizeof(struct sockaddr_in));
00086     
00087     // Export endpoint
00088     endpoint->sin_family = AF_INET;
00089     endpoint->sin_port = htons(this->_port);
00090     endpoint->sin_addr.s_addr = this->_address.toNative();
00091     return 0;
00092 }
00093 
00094 int
00095 Endpoint::fromNative(struct sockaddr_in *endpoint)
00096 {
00097     if (endpoint == NULL) {
00098         return 1;
00099     }
00100     
00101     // Import endpoint
00102     this->_port = ntohs(endpoint->sin_port);
00103     this->_address.fromNative(endpoint->sin_addr.s_addr);
00104     return 0;
00105 }
00106 
00107 Endpoint &
00108 Endpoint::operator=(const Endpoint &other) {
00109     this->_address = other._address;
00110     this->_port = other._port;
00111     
00112     return (*this);
00113 }