First commit
[anna.git] / source / comm / AccessPoint.cpp
1 // ANNA - Anna is Not 'N' Anymore
2 //
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
4 //
5 // https://bitbucket.org/testillano/anna
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 //
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
16 // distribution.
17 //     * Neither the name of Google Inc. 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.
20 //
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.
32 //
33 // Authors: eduardo.ramos.testillano@gmail.com
34 //          cisco.tierra@gmail.com
35
36
37 #include <strings.h>
38 #include <sys/socket.h>
39
40 #include <anna/xml/Node.hpp>
41
42 #include <anna/comm/AccessPoint.hpp>
43 #include <anna/comm/Socket.hpp>
44 #include <anna/comm/Device.hpp>
45
46 using namespace std;
47 using namespace anna;
48
49 bool comm::AccessPoint::operator == (const comm::AccessPoint& rhs) const
50 throw() {
51   if(rhs.isNull() && this->isNull())
52     return true;
53
54   if(a_path != NULL && rhs.a_path != NULL)
55     return *a_path == *rhs.a_path;
56
57   if(a_inetAddress.isNull() == false && rhs.a_inetAddress.isNull() == false)
58     return a_inetAddress == rhs.a_inetAddress;
59
60   return false;
61 }
62
63 comm::AccessPoint& comm::AccessPoint::operator = (const INetAddress & inetAddress)
64 throw() {
65   if(a_path != NULL) {
66     delete a_path;
67     a_path = NULL;
68   }
69
70   a_inetAddress = inetAddress;
71   return *this;
72 }
73
74 comm::AccessPoint& comm::AccessPoint::operator = (const std::string & path)
75 throw() {
76   if(a_path == &path)
77     return *this;
78
79   a_inetAddress.clear();
80
81   if(a_path == NULL)
82     a_path = new std::string(path);
83   else
84     *a_path = path;
85
86   return *this;
87 }
88
89 comm::AccessPoint& comm::AccessPoint::operator = (const AccessPoint & rhs)
90 throw() {
91   if(this == &rhs)
92     return *this;
93
94   if(rhs.isNull()) {
95     a_inetAddress.clear();
96
97     if(a_path != NULL) {
98       delete a_path;
99       a_path = NULL;
100     }
101
102     return *this;
103   }
104
105   return (rhs.a_path != NULL) ? operator= (*rhs.a_path) : operator= (rhs.a_inetAddress);
106 }
107
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:
112
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);
117     }
118
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);
125     break;
126   case Socket::Domain::Unix:
127
128     if(a_path  == NULL) {
129       string msg(socket.asString());
130       msg += "Cannot attach UNIX socket without file";
131       throw RuntimeException(msg, ANNA_FILE_LOCATION);
132     }
133
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());
138     break;
139   default:
140     s = NULL;
141     len = 0;
142
143     if(true) {
144       string msg(socket.asString());
145       msg += "Domain type unsupported at this version";
146       throw RuntimeException(msg, ANNA_FILE_LOCATION);
147     }
148
149     break;
150   }
151 }
152
153 void comm::AccessPoint::asString(std::string& msg) const
154 throw() {
155   msg += "{ ";
156
157   if(a_inetAddress.isNull() == false)
158     msg += a_inetAddress.asString();
159   else if(a_path != NULL) {
160     msg += "File: ";
161     msg += *a_path;
162   } else
163     msg += "<null>";
164
165   msg += "  }";
166 }
167
168 void comm::AccessPoint::asXML(const char* name, xml::Node* parent) const
169 throw(RuntimeException) {
170   xml::Node* accessPoint  = parent->createChild(name);
171
172   if(a_inetAddress.isNull() == false)
173     a_inetAddress.asXML(accessPoint);
174   else if(a_path != NULL)
175     accessPoint->createAttribute("File", *a_path);
176 }
177
178 std::string comm::AccessPoint::serialize() const
179 throw() {
180   std::string result;
181
182   if(a_inetAddress.isNull() == false)
183     result = a_inetAddress.serialize();
184   else if(a_path != NULL)
185     result = *a_path;
186   else
187     result = "unknown";
188
189   return result;
190 }
191