1 // ANNA - Anna is Not Nothingness Anymore
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
5 // http://redmine.teslayout.com/projects/anna-suite
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
11 // * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
17 // * Neither the name of the copyright holder nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 // Authors: eduardo.ramos.testillano@gmail.com
34 // cisco.tierra@gmail.com
38 #include <sys/socket.h>
40 #include <anna/xml/Node.hpp>
42 #include <anna/comm/AccessPoint.hpp>
43 #include <anna/comm/Socket.hpp>
44 #include <anna/comm/Device.hpp>
49 bool comm::AccessPoint::operator == (const comm::AccessPoint& rhs) const
51 if(rhs.isNull() && this->isNull())
54 if(a_path != NULL && rhs.a_path != NULL)
55 return *a_path == *rhs.a_path;
57 if(a_inetAddress.isNull() == false && rhs.a_inetAddress.isNull() == false)
58 return a_inetAddress == rhs.a_inetAddress;
63 comm::AccessPoint& comm::AccessPoint::operator = (const INetAddress & inetAddress)
70 a_inetAddress = inetAddress;
74 comm::AccessPoint& comm::AccessPoint::operator = (const std::string & path)
79 a_inetAddress.clear();
82 a_path = new std::string(path);
89 comm::AccessPoint& comm::AccessPoint::operator = (const AccessPoint & rhs)
95 a_inetAddress.clear();
105 return (rhs.a_path != NULL) ? operator= (*rhs.a_path) : operator= (rhs.a_inetAddress);
108 void comm::AccessPoint::translate(const comm::Socket& socket, sockaddr*& s, int& len)
109 throw(RuntimeException) {
110 switch(socket.getDomain()) {
111 case Socket::Domain::Inet:
113 if(a_inetAddress.isNull() == true) {
114 string msg(socket.asString());
115 msg += " | Cannot attach INET socket without address";
116 throw RuntimeException(msg, ANNA_FILE_LOCATION);
119 anna_memset(&a_sockaddr_in, 0, sizeof(a_sockaddr_in));
120 a_sockaddr_in.sin_family = AF_INET;
121 a_sockaddr_in.sin_port = htons(a_inetAddress.getPort());
122 a_sockaddr_in.sin_addr.s_addr = (in_addr_t) a_inetAddress.getDevice()->getAddress();
123 s = (sockaddr*) & a_sockaddr_in;
124 len = sizeof(a_sockaddr_in);
126 case Socket::Domain::Unix:
129 string msg(socket.asString());
130 msg += "Cannot attach UNIX socket without file";
131 throw RuntimeException(msg, ANNA_FILE_LOCATION);
134 s = (sockaddr*) & a_sockaddr_un;
135 bzero(s, len = sizeof(a_sockaddr_un));
136 a_sockaddr_un.sun_family = AF_UNIX;
137 anna_strcpy(a_sockaddr_un.sun_path, a_path->c_str());
144 string msg(socket.asString());
145 msg += "Domain type unsupported at this version";
146 throw RuntimeException(msg, ANNA_FILE_LOCATION);
153 void comm::AccessPoint::asString(std::string& msg) const
157 if(a_inetAddress.isNull() == false)
158 msg += a_inetAddress.asString();
159 else if(a_path != NULL) {
168 void comm::AccessPoint::asXML(const char* name, xml::Node* parent) const
169 throw(RuntimeException) {
170 xml::Node* accessPoint = parent->createChild(name);
172 if(a_inetAddress.isNull() == false)
173 a_inetAddress.asXML(accessPoint);
174 else if(a_path != NULL)
175 accessPoint->createAttribute("File", *a_path);
178 std::string comm::AccessPoint::serialize() const
182 if(a_inetAddress.isNull() == false)
183 result = a_inetAddress.serialize();
184 else if(a_path != NULL)