|  | 1 | // [License] | 
|---|
|  | 2 | // The Ariba-Underlay Copyright | 
|---|
|  | 3 | // | 
|---|
|  | 4 | // Copyright (c) 2008-2009, Institute of Telematics, UniversitÀt Karlsruhe (TH) | 
|---|
|  | 5 | // | 
|---|
|  | 6 | // Institute of Telematics | 
|---|
|  | 7 | // UniversitÀt Karlsruhe (TH) | 
|---|
|  | 8 | // Zirkel 2, 76128 Karlsruhe | 
|---|
|  | 9 | // Germany | 
|---|
|  | 10 | // | 
|---|
|  | 11 | // Redistribution and use in source and binary forms, with or without | 
|---|
|  | 12 | // modification, are permitted provided that the following conditions are | 
|---|
|  | 13 | // met: | 
|---|
|  | 14 | // | 
|---|
|  | 15 | // 1. Redistributions of source code must retain the above copyright | 
|---|
|  | 16 | // notice, this list of conditions and the following disclaimer. | 
|---|
|  | 17 | // 2. Redistributions in binary form must reproduce the above copyright | 
|---|
|  | 18 | // notice, this list of conditions and the following disclaimer in the | 
|---|
|  | 19 | // documentation and/or other materials provided with the distribution. | 
|---|
|  | 20 | // | 
|---|
|  | 21 | // THIS SOFTWARE IS PROVIDED BY THE INSTITUTE OF TELEMATICS ``AS IS'' AND | 
|---|
|  | 22 | // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 
|---|
|  | 23 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | 
|---|
|  | 24 | // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ARIBA PROJECT OR | 
|---|
|  | 25 | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | 
|---|
|  | 26 | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | 
|---|
|  | 27 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | 
|---|
|  | 28 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | 
|---|
|  | 29 | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | 
|---|
|  | 30 | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | 
|---|
|  | 31 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
|---|
|  | 32 | // | 
|---|
|  | 33 | // The views and conclusions contained in the software and documentation | 
|---|
|  | 34 | // are those of the authors and should not be interpreted as representing | 
|---|
|  | 35 | // official policies, either expressed or implied, of the Institute of | 
|---|
|  | 36 | // Telematics. | 
|---|
|  | 37 | // [License] | 
|---|
|  | 38 |  | 
|---|
|  | 39 | #include "BaseCommunication.h" | 
|---|
|  | 40 |  | 
|---|
|  | 41 | #ifdef UNDERLAY_OMNET | 
|---|
|  | 42 | #include "ariba/communication/modules/transport/omnet/AribaOmnetModule.h" | 
|---|
|  | 43 | #include "ariba/communication/modules/network/omnet/OmnetNetworkProtocol.h" | 
|---|
|  | 44 | #include "ariba/utility/system/StartupWrapper.h" | 
|---|
|  | 45 |  | 
|---|
|  | 46 | using ariba::communication::AribaOmnetModule; | 
|---|
|  | 47 | using ariba::communication::OmnetNetworkProtocol; | 
|---|
|  | 48 | using ariba::utility::StartupWrapper; | 
|---|
|  | 49 | #endif | 
|---|
|  | 50 |  | 
|---|
|  | 51 | namespace ariba { | 
|---|
|  | 52 | namespace communication { | 
|---|
|  | 53 |  | 
|---|
|  | 54 | use_logging_cpp(BaseCommunication); | 
|---|
|  | 55 | const BaseCommunication::LinkDescriptor BaseCommunication::LinkDescriptor::UNSPECIFIED; | 
|---|
|  | 56 |  | 
|---|
|  | 57 | BaseCommunication::BaseCommunication() | 
|---|
|  | 58 | : messageReceiver(NULL), network(NULL), transport(NULL), basecommStarted(false){ | 
|---|
|  | 59 | } | 
|---|
|  | 60 |  | 
|---|
|  | 61 | BaseCommunication::~BaseCommunication(){ | 
|---|
|  | 62 | } | 
|---|
|  | 63 |  | 
|---|
|  | 64 | void BaseCommunication::start(const NetworkLocator* _locallocator, const uint16_t _listenport){ | 
|---|
|  | 65 |  | 
|---|
|  | 66 | currentSeqnum = 0; | 
|---|
|  | 67 | listenport = _listenport; | 
|---|
|  | 68 |  | 
|---|
|  | 69 | logging_info( "starting up base communication and creating transports ..." ); | 
|---|
|  | 70 | logging_info( "using port " << listenport ); | 
|---|
|  | 71 |  | 
|---|
|  | 72 | #ifdef UNDERLAY_OMNET | 
|---|
|  | 73 | AribaOmnetModule* module = StartupWrapper::getCurrentModule(); | 
|---|
|  | 74 | module->setServerPort( listenport ); | 
|---|
|  | 75 |  | 
|---|
|  | 76 | transport = module; | 
|---|
|  | 77 | network = new OmnetNetworkProtocol( module ); | 
|---|
|  | 78 | #else | 
|---|
|  | 79 | transport = new TCPTransport( listenport ); | 
|---|
|  | 80 | network = new IPv4NetworkProtocol(); | 
|---|
|  | 81 | #endif | 
|---|
|  | 82 |  | 
|---|
|  | 83 | logging_debug( "searching for local locators ..." ); | 
|---|
|  | 84 |  | 
|---|
|  | 85 | NetworkProtocol::NetworkLocatorSet locators = network->getAddresses(); | 
|---|
|  | 86 | NetworkProtocol::NetworkLocatorSet::iterator i = locators.begin(); | 
|---|
|  | 87 | NetworkProtocol::NetworkLocatorSet::iterator iend = locators.end(); | 
|---|
|  | 88 |  | 
|---|
|  | 89 | // | 
|---|
|  | 90 | // choose the first locator that is not localhost | 
|---|
|  | 91 | // | 
|---|
|  | 92 |  | 
|---|
|  | 93 | bool foundLocator = false; | 
|---|
|  | 94 |  | 
|---|
|  | 95 | for( ; i != iend; i++){ | 
|---|
|  | 96 | logging_debug( "local locator found " << (*i)->toString() ); | 
|---|
|  | 97 | IPv4Locator* ipv4locator = dynamic_cast<IPv4Locator*>(*i); | 
|---|
|  | 98 |  | 
|---|
|  | 99 | // TODO: which locators can we find to bind to? | 
|---|
|  | 100 | // localhost is not too bad, works when testing locally | 
|---|
|  | 101 | // with several instances. the manual override currently | 
|---|
|  | 102 | // enables to use an arbitrary address, guess this is fine. | 
|---|
|  | 103 | // so the manual override also can use ANY, LOCALHOST, BROADCAST | 
|---|
|  | 104 |  | 
|---|
|  | 105 | if( *ipv4locator != IPv4Locator::LOCALHOST && | 
|---|
|  | 106 | *ipv4locator != IPv4Locator::ANY       && | 
|---|
|  | 107 | *ipv4locator != IPv4Locator::BROADCAST  ){ | 
|---|
|  | 108 |  | 
|---|
|  | 109 | ipv4locator->setPort(listenport); | 
|---|
|  | 110 | localDescriptor.locator = ipv4locator; | 
|---|
|  | 111 | localDescriptor.isUnspec = false; | 
|---|
|  | 112 | logging_info( "binding to addr = " << ipv4locator->toString() ); | 
|---|
|  | 113 | foundLocator = true; | 
|---|
|  | 114 | break; | 
|---|
|  | 115 | } | 
|---|
|  | 116 | } // for( ; i != iend; i++) | 
|---|
|  | 117 |  | 
|---|
|  | 118 |  | 
|---|
|  | 119 | if( _locallocator != NULL ) { | 
|---|
|  | 120 | if( localDescriptor.locator != NULL) delete localDescriptor.locator; | 
|---|
|  | 121 | localDescriptor.locator = new IPv4Locator( IPv4Locator::fromString( _locallocator->toString()) ); | 
|---|
|  | 122 | localDescriptor.isUnspec = false; | 
|---|
|  | 123 | logging_debug( "manual locator override, using locator=" << | 
|---|
|  | 124 | localDescriptor.locator->toString() ); | 
|---|
|  | 125 | foundLocator = true; | 
|---|
|  | 126 | } | 
|---|
|  | 127 |  | 
|---|
|  | 128 | // if we found no local locator, exit using logging fatal | 
|---|
|  | 129 | if( !foundLocator ) | 
|---|
|  | 130 | logging_fatal( "did not find a useable local locator!" ); | 
|---|
|  | 131 |  | 
|---|
|  | 132 | transport->addMessageReceiver( this ); | 
|---|
|  | 133 | transport->start(); | 
|---|
|  | 134 |  | 
|---|
|  | 135 | #ifndef UNDERLAY_OMNET | 
|---|
|  | 136 | // | 
|---|
|  | 137 | // bind to the network change detection | 
|---|
|  | 138 | // | 
|---|
|  | 139 |  | 
|---|
|  | 140 | networkMonitor.registerNotification( this ); | 
|---|
|  | 141 | #endif | 
|---|
|  | 142 |  | 
|---|
|  | 143 | // | 
|---|
|  | 144 | // base comm startup done | 
|---|
|  | 145 | // | 
|---|
|  | 146 |  | 
|---|
|  | 147 | basecommStarted = true; | 
|---|
|  | 148 | logging_info( "base communication started up" ); | 
|---|
|  | 149 | } | 
|---|
|  | 150 |  | 
|---|
|  | 151 | void BaseCommunication::stop() { | 
|---|
|  | 152 |  | 
|---|
|  | 153 | logging_info( "stopping base communication and transport ..." ); | 
|---|
|  | 154 |  | 
|---|
|  | 155 | transport->stop(); | 
|---|
|  | 156 | delete transport; | 
|---|
|  | 157 | delete network; | 
|---|
|  | 158 |  | 
|---|
|  | 159 | basecommStarted = false; | 
|---|
|  | 160 | logging_info( "base communication stopped" ); | 
|---|
|  | 161 | } | 
|---|
|  | 162 |  | 
|---|
|  | 163 | bool BaseCommunication::isStarted(){ | 
|---|
|  | 164 | return basecommStarted; | 
|---|
|  | 165 | } | 
|---|
|  | 166 |  | 
|---|
|  | 167 | const LinkID BaseCommunication::establishLink( | 
|---|
|  | 168 | const EndpointDescriptor& descriptor, | 
|---|
|  | 169 | const LinkID& link_id, | 
|---|
|  | 170 | const QoSParameterSet& qos, | 
|---|
|  | 171 | const SecurityParameterSet& sec) { | 
|---|
|  | 172 |  | 
|---|
|  | 173 | // copy link id | 
|---|
|  | 174 | LinkID linkid = link_id; | 
|---|
|  | 175 |  | 
|---|
|  | 176 | // debug | 
|---|
|  | 177 | logging_debug( "request to establish link" ); | 
|---|
|  | 178 |  | 
|---|
|  | 179 | // | 
|---|
|  | 180 | // just use the first locator in the endp descriptors | 
|---|
|  | 181 | // | 
|---|
|  | 182 | if( descriptor.locator == NULL ){ | 
|---|
|  | 183 | logging_error( "invalid destination endpoint" ); | 
|---|
|  | 184 | return LinkID::UNSPECIFIED; | 
|---|
|  | 185 | } | 
|---|
|  | 186 |  | 
|---|
|  | 187 | if( localDescriptor.locator == NULL ){ | 
|---|
|  | 188 | logging_error( "invalid local endpoint" ); | 
|---|
|  | 189 | return LinkID::UNSPECIFIED; | 
|---|
|  | 190 | } | 
|---|
|  | 191 |  | 
|---|
|  | 192 | const NetworkLocator* remote = descriptor.locator; | 
|---|
|  | 193 | const NetworkLocator* local =  localDescriptor.locator; | 
|---|
|  | 194 |  | 
|---|
|  | 195 | // create link identifier and link descriptor | 
|---|
|  | 196 | if (linkid.isUnspecified()){ | 
|---|
|  | 197 | linkid = LinkID::create(); | 
|---|
|  | 198 | assert(!linkid.isUnspecified()); | 
|---|
|  | 199 | } | 
|---|
|  | 200 |  | 
|---|
|  | 201 | logging_debug( "creating new local descriptor entry with local link id " << linkid.toString() ); | 
|---|
|  | 202 | LinkDescriptor linkDescriptor( linkid, local, LinkID::UNSPECIFIED, remote, descriptor, false ); | 
|---|
|  | 203 | addLink( linkDescriptor ); | 
|---|
|  | 204 |  | 
|---|
|  | 205 | // | 
|---|
|  | 206 | // create a base msg with our link id and | 
|---|
|  | 207 | // a request to open a link on the other side | 
|---|
|  | 208 | // | 
|---|
|  | 209 |  | 
|---|
|  | 210 | logging_debug( "sending out base messages with request to open link to " << remote->toString() ); | 
|---|
|  | 211 | AribaBaseMsg baseMsg( | 
|---|
|  | 212 | remote, | 
|---|
|  | 213 | AribaBaseMsg::LINK_STATE_OPEN_REQUEST, | 
|---|
|  | 214 | linkid, | 
|---|
|  | 215 | LinkID::UNSPECIFIED ); | 
|---|
|  | 216 |  | 
|---|
|  | 217 | transport->sendMessage(&baseMsg); | 
|---|
|  | 218 | return linkid; | 
|---|
|  | 219 | } | 
|---|
|  | 220 |  | 
|---|
|  | 221 | void BaseCommunication::dropLink(const LinkID link) { | 
|---|
|  | 222 |  | 
|---|
|  | 223 | logging_debug( "starting to drop link " + link.toString() ); | 
|---|
|  | 224 |  | 
|---|
|  | 225 | // see if we have the link | 
|---|
|  | 226 | LinkDescriptor& descriptor = queryLocalLink( link ); | 
|---|
|  | 227 | if( descriptor.isUnspecified() ){ | 
|---|
|  | 228 | logging_error( "don't know the link you want to drop "+ link.toString() ); | 
|---|
|  | 229 | return; | 
|---|
|  | 230 | } | 
|---|
|  | 231 |  | 
|---|
|  | 232 | // create message to drop the link | 
|---|
|  | 233 | logging_debug( "sending out link close request. for us, the link is closed now" ); | 
|---|
|  | 234 | AribaBaseMsg msg( | 
|---|
|  | 235 | descriptor.remoteLocator, | 
|---|
|  | 236 | AribaBaseMsg::LINK_STATE_CLOSE_REQUEST, | 
|---|
|  | 237 | descriptor.localLink, | 
|---|
|  | 238 | descriptor.remoteLink | 
|---|
|  | 239 | ); | 
|---|
|  | 240 |  | 
|---|
|  | 241 | // send message to drop the link | 
|---|
|  | 242 | transport->sendMessage( &msg ); | 
|---|
|  | 243 |  | 
|---|
|  | 244 | // tell the registered listeners | 
|---|
|  | 245 | BOOST_FOREACH( CommunicationEvents* i, eventListener ){ | 
|---|
|  | 246 | i->onLinkDown( link, descriptor.localLocator, descriptor.remoteLocator ); | 
|---|
|  | 247 | } | 
|---|
|  | 248 |  | 
|---|
|  | 249 | // remove from map | 
|---|
|  | 250 | removeLink(link); | 
|---|
|  | 251 | } | 
|---|
|  | 252 |  | 
|---|
|  | 253 | seqnum_t BaseCommunication::sendMessage( const LinkID lid, const Message* message) { | 
|---|
|  | 254 |  | 
|---|
|  | 255 | logging_debug( "sending out message to link " << lid.toString() ); | 
|---|
|  | 256 |  | 
|---|
|  | 257 | // query local link info | 
|---|
|  | 258 | LinkDescriptor& linkDesc = queryLocalLink(lid); | 
|---|
|  | 259 | if( linkDesc.isUnspecified() ){ | 
|---|
|  | 260 | logging_error( "don't know the link with id " << lid.toString() ); | 
|---|
|  | 261 | return -1; | 
|---|
|  | 262 | } | 
|---|
|  | 263 |  | 
|---|
|  | 264 | // create message | 
|---|
|  | 265 | AribaBaseMsg msg( | 
|---|
|  | 266 | linkDesc.remoteLocator, | 
|---|
|  | 267 | AribaBaseMsg::LINK_STATE_DATA, | 
|---|
|  | 268 | linkDesc.localLink, | 
|---|
|  | 269 | linkDesc.remoteLink | 
|---|
|  | 270 | ); | 
|---|
|  | 271 |  | 
|---|
|  | 272 | // encapsulate the payload message | 
|---|
|  | 273 | msg.encapsulate( const_cast<Message*>(message) ); | 
|---|
|  | 274 |  | 
|---|
|  | 275 | if( !linkDesc.linkup ){ | 
|---|
|  | 276 | logging_error("cant send message on link " << lid.toString() << ", link not up"); | 
|---|
|  | 277 | return -1; | 
|---|
|  | 278 | } | 
|---|
|  | 279 |  | 
|---|
|  | 280 | // send message | 
|---|
|  | 281 | transport->sendMessage( &msg ); | 
|---|
|  | 282 | return ++currentSeqnum; | 
|---|
|  | 283 | } | 
|---|
|  | 284 |  | 
|---|
|  | 285 | const EndpointDescriptor& BaseCommunication::getEndpointDescriptor(const LinkID link) const { | 
|---|
|  | 286 |  | 
|---|
|  | 287 | if( link == LinkID::UNSPECIFIED){ | 
|---|
|  | 288 | return localDescriptor; | 
|---|
|  | 289 | } else { | 
|---|
|  | 290 | LinkDescriptor& linkDesc = queryLocalLink(link); | 
|---|
|  | 291 | if (linkDesc.isUnspecified()) return EndpointDescriptor::UNSPECIFIED; | 
|---|
|  | 292 | return linkDesc.remoteEndpoint; | 
|---|
|  | 293 | } | 
|---|
|  | 294 | } | 
|---|
|  | 295 |  | 
|---|
|  | 296 | void BaseCommunication::registerMessageReceiver(MessageReceiver* _receiver) { | 
|---|
|  | 297 | messageReceiver = _receiver; | 
|---|
|  | 298 | } | 
|---|
|  | 299 |  | 
|---|
|  | 300 | void BaseCommunication::unregisterMessageReceiver(MessageReceiver* _receiver) { | 
|---|
|  | 301 | messageReceiver = NULL; | 
|---|
|  | 302 | } | 
|---|
|  | 303 |  | 
|---|
|  | 304 | void BaseCommunication::registerEventListener(CommunicationEvents* _events){ | 
|---|
|  | 305 |  | 
|---|
|  | 306 | if( eventListener.find( _events ) == eventListener.end() ) | 
|---|
|  | 307 | eventListener.insert( _events ); | 
|---|
|  | 308 | } | 
|---|
|  | 309 |  | 
|---|
|  | 310 | void BaseCommunication::unregisterEventListener(CommunicationEvents* _events){ | 
|---|
|  | 311 |  | 
|---|
|  | 312 | EventListenerSet::iterator i = eventListener.find( _events ); | 
|---|
|  | 313 | if( i != eventListener.end() ) | 
|---|
|  | 314 | eventListener.erase( i ); | 
|---|
|  | 315 | } | 
|---|
|  | 316 |  | 
|---|
|  | 317 |  | 
|---|
|  | 318 | bool BaseCommunication::receiveMessage(const Message* message, const LinkID& /*invalid*/, const NodeID& ){ | 
|---|
|  | 319 |  | 
|---|
|  | 320 | // | 
|---|
|  | 321 | // these messages arrive from the Transport module | 
|---|
|  | 322 | // and are incoming network messages. Unpack the | 
|---|
|  | 323 | // AribaBaseMsg and handle control packets, | 
|---|
|  | 324 | // deliver data packets to the overlay | 
|---|
|  | 325 | // | 
|---|
|  | 326 |  | 
|---|
|  | 327 | AribaBaseMsg* spovmsg = ((Message*)message)->decapsulate<AribaBaseMsg>(); | 
|---|
|  | 328 | logging_debug( "receiving base comm message of type " << spovmsg->getTypeString() ); | 
|---|
|  | 329 |  | 
|---|
|  | 330 | // | 
|---|
|  | 331 | // deliver data to the overlays. we just give the | 
|---|
|  | 332 | // inner packet to every registered overlay ... | 
|---|
|  | 333 | // | 
|---|
|  | 334 |  | 
|---|
|  | 335 | if( spovmsg->getType() == AribaBaseMsg::LINK_STATE_DATA ){ | 
|---|
|  | 336 |  | 
|---|
|  | 337 | logging_debug( "received data message, forwarding to overlay" ); | 
|---|
|  | 338 |  | 
|---|
|  | 339 | // | 
|---|
|  | 340 | // put the linkid as address into the message | 
|---|
|  | 341 | // and sent it to the receiver | 
|---|
|  | 342 | // | 
|---|
|  | 343 |  | 
|---|
|  | 344 | if( messageReceiver != NULL ) { | 
|---|
|  | 345 | messageReceiver->receiveMessage( | 
|---|
|  | 346 | spovmsg, | 
|---|
|  | 347 | spovmsg->getRemoteLink(), | 
|---|
|  | 348 | NodeID::UNSPECIFIED | 
|---|
|  | 349 | ); | 
|---|
|  | 350 | } | 
|---|
|  | 351 |  | 
|---|
|  | 352 | } // LINK_STATE_DATA | 
|---|
|  | 353 |  | 
|---|
|  | 354 | // | 
|---|
|  | 355 | // handle link open requests | 
|---|
|  | 356 | // | 
|---|
|  | 357 |  | 
|---|
|  | 358 | else if( spovmsg->getType() == AribaBaseMsg::LINK_STATE_OPEN_REQUEST ){ | 
|---|
|  | 359 |  | 
|---|
|  | 360 | logging_debug( "received link open request" ); | 
|---|
|  | 361 |  | 
|---|
|  | 362 | // | 
|---|
|  | 363 | // create a link context | 
|---|
|  | 364 | // | 
|---|
|  | 365 |  | 
|---|
|  | 366 | //  in an incoming packet the localLink is from | 
|---|
|  | 367 | // the sender perspective local and from our | 
|---|
|  | 368 | // perspective remote | 
|---|
|  | 369 |  | 
|---|
|  | 370 | logging_debug( "creating local link" ); | 
|---|
|  | 371 |  | 
|---|
|  | 372 | LinkID localLink  = LinkID::create(); | 
|---|
|  | 373 | LinkID remoteLink = spovmsg->getLocalLink(); | 
|---|
|  | 374 |  | 
|---|
|  | 375 | if(localLink.isUnspecified()){ | 
|---|
|  | 376 | logging_error("local link is unspecified"); | 
|---|
|  | 377 | return false; | 
|---|
|  | 378 | } | 
|---|
|  | 379 |  | 
|---|
|  | 380 | if(remoteLink.isUnspecified()){ | 
|---|
|  | 381 | logging_error("remote link is unspecified"); | 
|---|
|  | 382 | return false; | 
|---|
|  | 383 | } | 
|---|
|  | 384 |  | 
|---|
|  | 385 | const NetworkLocator* localLocator  = dynamic_cast<const NetworkLocator*>(localDescriptor.locator); | 
|---|
|  | 386 | const NetworkLocator* remoteLocator = dynamic_cast<const NetworkLocator*>(message->getSourceAddress()); | 
|---|
|  | 387 |  | 
|---|
|  | 388 | logging_debug( "localLocator=" << localLocator->toString() | 
|---|
|  | 389 | << " remoteLocator=" << remoteLocator->toString()); | 
|---|
|  | 390 |  | 
|---|
|  | 391 | // ask the registered listeners if this link | 
|---|
|  | 392 | // creation is fine. we will only allow the | 
|---|
|  | 393 | // link if all of them agree | 
|---|
|  | 394 |  | 
|---|
|  | 395 | bool allowlink = true; | 
|---|
|  | 396 | BOOST_FOREACH( CommunicationEvents* i, eventListener ){ | 
|---|
|  | 397 | allowlink &= i->onLinkRequest( localLink, localLocator, remoteLocator ); | 
|---|
|  | 398 | } | 
|---|
|  | 399 |  | 
|---|
|  | 400 | if( !allowlink ){ | 
|---|
|  | 401 | logging_warn( "overlay denied creation of link" ); | 
|---|
|  | 402 | return true; | 
|---|
|  | 403 | } | 
|---|
|  | 404 |  | 
|---|
|  | 405 | // | 
|---|
|  | 406 | // create and save the descriptor for the link | 
|---|
|  | 407 | // | 
|---|
|  | 408 |  | 
|---|
|  | 409 | LinkDescriptor linkDescriptor(localLink, localLocator, remoteLink, | 
|---|
|  | 410 | remoteLocator, EndpointDescriptor(remoteLocator), true); | 
|---|
|  | 411 |  | 
|---|
|  | 412 | logging_debug( "saving new link descriptor with " << | 
|---|
|  | 413 | "[local link " << localLink.toString() << "] " << | 
|---|
|  | 414 | "[local locator " << localLocator->toString() << "] " << | 
|---|
|  | 415 | "[remote link " << remoteLink.toString() << "] " << | 
|---|
|  | 416 | "[remote locator " << remoteLocator->toString() << "]" << | 
|---|
|  | 417 | "[link up true]" ); | 
|---|
|  | 418 |  | 
|---|
|  | 419 | addLink( linkDescriptor ); | 
|---|
|  | 420 |  | 
|---|
|  | 421 | // | 
|---|
|  | 422 | // send out a link reply | 
|---|
|  | 423 | // | 
|---|
|  | 424 |  | 
|---|
|  | 425 | logging_debug( "sending back link open reply for " << | 
|---|
|  | 426 | "[local link " << localLink.toString() << "] " << | 
|---|
|  | 427 | "[remote link " << remoteLink.toString() << "]" ); | 
|---|
|  | 428 |  | 
|---|
|  | 429 | AribaBaseMsg reply(remoteLocator, | 
|---|
|  | 430 | AribaBaseMsg::LINK_STATE_OPEN_REPLY, | 
|---|
|  | 431 | localLink, | 
|---|
|  | 432 | remoteLink); | 
|---|
|  | 433 |  | 
|---|
|  | 434 | transport->sendMessage( &reply ); | 
|---|
|  | 435 |  | 
|---|
|  | 436 | // | 
|---|
|  | 437 | // the link is now open | 
|---|
|  | 438 | // | 
|---|
|  | 439 |  | 
|---|
|  | 440 | BOOST_FOREACH( CommunicationEvents* i, eventListener ){ | 
|---|
|  | 441 | i->onLinkUp( localLink, localLocator, remoteLocator ); | 
|---|
|  | 442 | } | 
|---|
|  | 443 |  | 
|---|
|  | 444 | } // LINK_STATE_OPEN_REQUEST | 
|---|
|  | 445 |  | 
|---|
|  | 446 | // | 
|---|
|  | 447 | // handle link open replies | 
|---|
|  | 448 | // | 
|---|
|  | 449 |  | 
|---|
|  | 450 | else if( spovmsg->getType() == AribaBaseMsg::LINK_STATE_OPEN_REPLY ){ | 
|---|
|  | 451 |  | 
|---|
|  | 452 | logging_debug( "received link open reply for a link we initiated" ); | 
|---|
|  | 453 |  | 
|---|
|  | 454 | // this is a reply to a link open request, so we have already | 
|---|
|  | 455 | // a link mapping and can now set the remote link to valid | 
|---|
|  | 456 | LinkDescriptor& linkDesc = queryLocalLink( spovmsg->getRemoteLink() ); | 
|---|
|  | 457 |  | 
|---|
|  | 458 | if (linkDesc.isUnspecified()) { | 
|---|
|  | 459 | logging_warn("failed to find local link " << spovmsg->getRemoteLink().toString()); | 
|---|
|  | 460 | return false; | 
|---|
|  | 461 | } | 
|---|
|  | 462 |  | 
|---|
|  | 463 | linkDesc.remoteLink = spovmsg->getLocalLink(); | 
|---|
|  | 464 | linkDesc.linkup = true; | 
|---|
|  | 465 |  | 
|---|
|  | 466 | logging_debug( "the link is now up with local link id " << linkDesc.localLink.toString() << | 
|---|
|  | 467 | " and remote link id " << linkDesc.remoteLink.toString() ); | 
|---|
|  | 468 |  | 
|---|
|  | 469 | // notify the baseoverlay that the link is up, so | 
|---|
|  | 470 | // it can exchange nodeids over this link. then we | 
|---|
|  | 471 | // can send the queued messages, as both nodes have | 
|---|
|  | 472 | // to know their nodeids first | 
|---|
|  | 473 |  | 
|---|
|  | 474 | BOOST_FOREACH( CommunicationEvents* i, eventListener ){ | 
|---|
|  | 475 | i->onLinkUp( linkDesc.localLink, linkDesc.localLocator, linkDesc.remoteLocator ); | 
|---|
|  | 476 | } | 
|---|
|  | 477 |  | 
|---|
|  | 478 | } // LINK_STATE_OPEN_REPLY | 
|---|
|  | 479 |  | 
|---|
|  | 480 | // | 
|---|
|  | 481 | // handle link close requests | 
|---|
|  | 482 | // | 
|---|
|  | 483 |  | 
|---|
|  | 484 | else if( spovmsg->getType() == AribaBaseMsg::LINK_STATE_CLOSE_REQUEST ){ | 
|---|
|  | 485 |  | 
|---|
|  | 486 | const LinkID& localLink = spovmsg->getRemoteLink(); | 
|---|
|  | 487 | logging_debug( "received link close request for link " << localLink.toString() ); | 
|---|
|  | 488 |  | 
|---|
|  | 489 | // | 
|---|
|  | 490 | // the link is closed immediately, we | 
|---|
|  | 491 | // don't need to send out a reply, so we | 
|---|
|  | 492 | // delete the mapping and inform | 
|---|
|  | 493 | // | 
|---|
|  | 494 |  | 
|---|
|  | 495 | LinkDescriptor& linkDesc = queryLocalLink( localLink ); | 
|---|
|  | 496 | if (linkDesc.isUnspecified()) { | 
|---|
|  | 497 | logging_warn("Failed to find local link " << localLink.toString()); | 
|---|
|  | 498 | return false; | 
|---|
|  | 499 | } | 
|---|
|  | 500 |  | 
|---|
|  | 501 | BOOST_FOREACH( CommunicationEvents* i, eventListener ){ | 
|---|
|  | 502 | i->onLinkDown( linkDesc.localLink, linkDesc.localLocator, linkDesc.remoteLocator ); | 
|---|
|  | 503 | } | 
|---|
|  | 504 |  | 
|---|
|  | 505 | // | 
|---|
|  | 506 | // remove the link descriptor | 
|---|
|  | 507 | // | 
|---|
|  | 508 |  | 
|---|
|  | 509 | removeLink( localLink ); | 
|---|
|  | 510 |  | 
|---|
|  | 511 | } // LINK_STATE_CLOSE_REQUEST | 
|---|
|  | 512 |  | 
|---|
|  | 513 | // | 
|---|
|  | 514 | // handle locator updates | 
|---|
|  | 515 | // | 
|---|
|  | 516 |  | 
|---|
|  | 517 | else if( spovmsg->getType() == AribaBaseMsg::LINK_STATE_UPDATE ){ | 
|---|
|  | 518 |  | 
|---|
|  | 519 | const LinkID& localLink = spovmsg->getRemoteLink(); | 
|---|
|  | 520 | logging_debug( "received link update for link " << localLink.toString() ); | 
|---|
|  | 521 |  | 
|---|
|  | 522 | // | 
|---|
|  | 523 | // find the link description | 
|---|
|  | 524 | // | 
|---|
|  | 525 |  | 
|---|
|  | 526 | LinkDescriptor& linkDesc = queryLocalLink( localLink ); | 
|---|
|  | 527 | if (linkDesc.isUnspecified()) { | 
|---|
|  | 528 | logging_warn("Failed to update local link " << localLink.toString()); | 
|---|
|  | 529 | return false; | 
|---|
|  | 530 | } | 
|---|
|  | 531 |  | 
|---|
|  | 532 | // | 
|---|
|  | 533 | // update the remote locator | 
|---|
|  | 534 | // | 
|---|
|  | 535 |  | 
|---|
|  | 536 | const NetworkLocator* oldremote = linkDesc.remoteLocator; | 
|---|
|  | 537 | linkDesc.remoteLocator = dynamic_cast<const NetworkLocator*>(message->getSourceAddress()); | 
|---|
|  | 538 |  | 
|---|
|  | 539 | // | 
|---|
|  | 540 | // inform the listeners (local link has _not_ changed!) | 
|---|
|  | 541 | // | 
|---|
|  | 542 |  | 
|---|
|  | 543 | BOOST_FOREACH( CommunicationEvents* i, eventListener ){ | 
|---|
|  | 544 | i->onLinkChanged( | 
|---|
|  | 545 | linkDesc.localLink,     // linkid | 
|---|
|  | 546 | linkDesc.localLocator,  // old local | 
|---|
|  | 547 | linkDesc.localLocator,  // new local | 
|---|
|  | 548 | oldremote,              // old remote | 
|---|
|  | 549 | linkDesc.remoteLocator  // new remote | 
|---|
|  | 550 | ); | 
|---|
|  | 551 | } | 
|---|
|  | 552 |  | 
|---|
|  | 553 | } // LINK_STATE_UPDATE | 
|---|
|  | 554 |  | 
|---|
|  | 555 | return true; | 
|---|
|  | 556 | } | 
|---|
|  | 557 |  | 
|---|
|  | 558 | void BaseCommunication::addLink( const LinkDescriptor& link ) { | 
|---|
|  | 559 | linkSet.push_back( link ); | 
|---|
|  | 560 | } | 
|---|
|  | 561 |  | 
|---|
|  | 562 | void BaseCommunication::removeLink( const LinkID& localLink ) { | 
|---|
|  | 563 |  | 
|---|
|  | 564 | LinkSet::iterator i = linkSet.begin(); | 
|---|
|  | 565 | LinkSet::iterator iend = linkSet.end(); | 
|---|
|  | 566 |  | 
|---|
|  | 567 | for( ; i != iend; i++){ | 
|---|
|  | 568 | if( (*i).localLink != localLink) continue; | 
|---|
|  | 569 |  | 
|---|
|  | 570 | linkSet.erase( i ); | 
|---|
|  | 571 | break; | 
|---|
|  | 572 | } | 
|---|
|  | 573 | } | 
|---|
|  | 574 |  | 
|---|
|  | 575 | BaseCommunication::LinkDescriptor& BaseCommunication::queryLocalLink( const LinkID& link ) const { | 
|---|
|  | 576 | for (int i=0; i<linkSet.size();i++) | 
|---|
|  | 577 | if (linkSet[i].localLink == link) return (LinkDescriptor&)linkSet[i]; | 
|---|
|  | 578 | return (LinkDescriptor&)LinkDescriptor::UNSPECIFIED; | 
|---|
|  | 579 | } | 
|---|
|  | 580 |  | 
|---|
|  | 581 | BaseCommunication::LinkDescriptor& BaseCommunication::queryRemoteLink( const LinkID& link ) const { | 
|---|
|  | 582 | for (int i=0; i<linkSet.size();i++) | 
|---|
|  | 583 | if (linkSet[i].remoteLink == link) return (LinkDescriptor&)linkSet[i]; | 
|---|
|  | 584 | return (LinkDescriptor&)LinkDescriptor::UNSPECIFIED; | 
|---|
|  | 585 | } | 
|---|
|  | 586 |  | 
|---|
|  | 587 | LinkIDs BaseCommunication::getLocalLinks( const EndpointDescriptor& ep ) const { | 
|---|
|  | 588 | LinkIDs ids; | 
|---|
|  | 589 |  | 
|---|
|  | 590 | for (int i=0; i<linkSet.size(); i++){ | 
|---|
|  | 591 | if( ep == EndpointDescriptor::UNSPECIFIED ){ | 
|---|
|  | 592 | ids.push_back( linkSet[i].localLink ); | 
|---|
|  | 593 | } else { | 
|---|
|  | 594 | if ( linkSet[i].remoteLocator == ep.locator ) | 
|---|
|  | 595 | ids.push_back( linkSet[i].localLink ); | 
|---|
|  | 596 | } | 
|---|
|  | 597 | } | 
|---|
|  | 598 |  | 
|---|
|  | 599 | return ids; | 
|---|
|  | 600 | } | 
|---|
|  | 601 |  | 
|---|
|  | 602 | void BaseCommunication::onNetworkChange(const NetworkChangeInterface::NetworkChangeInfo& info){ | 
|---|
|  | 603 |  | 
|---|
|  | 604 | #ifdef UNDERLAY_OMNET | 
|---|
|  | 605 |  | 
|---|
|  | 606 | // we have no mobility support for simulations | 
|---|
|  | 607 | return | 
|---|
|  | 608 |  | 
|---|
|  | 609 | #endif // UNDERLAY_OMNET | 
|---|
|  | 610 |  | 
|---|
|  | 611 | // | 
|---|
|  | 612 | // we only care about address changes, not about interface changes | 
|---|
|  | 613 | // as address changes are triggered by interface changes, we are safe here | 
|---|
|  | 614 | // | 
|---|
|  | 615 |  | 
|---|
|  | 616 | if( info.type != NetworkChangeInterface::EventTypeAddressNew && | 
|---|
|  | 617 | info.type != NetworkChangeInterface::EventTypeAddressDelete ) return; | 
|---|
|  | 618 |  | 
|---|
|  | 619 | logging_info( "base communication is handling network address changes" ); | 
|---|
|  | 620 |  | 
|---|
|  | 621 | // | 
|---|
|  | 622 | // get all now available addresses | 
|---|
|  | 623 | // | 
|---|
|  | 624 |  | 
|---|
|  | 625 | NetworkInformation networkInformation; | 
|---|
|  | 626 | AddressInformation addressInformation; | 
|---|
|  | 627 |  | 
|---|
|  | 628 | NetworkInterfaceList interfaces = networkInformation.getInterfaces(); | 
|---|
|  | 629 | AddressList addresses; | 
|---|
|  | 630 |  | 
|---|
|  | 631 | for( NetworkInterfaceList::iterator i = interfaces.begin(); i != interfaces.end(); i++ ){ | 
|---|
|  | 632 | AddressList newaddr = addressInformation.getAddresses(*i); | 
|---|
|  | 633 | addresses.insert( addresses.end(), newaddr.begin(), newaddr.end() ); | 
|---|
|  | 634 | } | 
|---|
|  | 635 |  | 
|---|
|  | 636 | // | 
|---|
|  | 637 | // get current locators for the local endpoint | 
|---|
|  | 638 | // TODO: this code is dublicate of the ctor code!!! cleanup! | 
|---|
|  | 639 | // | 
|---|
|  | 640 |  | 
|---|
|  | 641 | NetworkProtocol::NetworkLocatorSet locators = network->getAddresses(); | 
|---|
|  | 642 | NetworkProtocol::NetworkLocatorSet::iterator i = locators.begin(); | 
|---|
|  | 643 | NetworkProtocol::NetworkLocatorSet::iterator iend = locators.end(); | 
|---|
|  | 644 |  | 
|---|
|  | 645 | // | 
|---|
|  | 646 | // remember the old local endpoint, in case it changes | 
|---|
|  | 647 | // | 
|---|
|  | 648 |  | 
|---|
|  | 649 | EndpointDescriptor oldLocalDescriptor( localDescriptor ); | 
|---|
|  | 650 |  | 
|---|
|  | 651 | // | 
|---|
|  | 652 | // look for local locators that we can use in communication | 
|---|
|  | 653 | // | 
|---|
|  | 654 | // choose the first locator that is not localhost | 
|---|
|  | 655 | // | 
|---|
|  | 656 |  | 
|---|
|  | 657 | bool foundLocator = false; | 
|---|
|  | 658 | bool changedLocator = false; | 
|---|
|  | 659 |  | 
|---|
|  | 660 | for( ; i != iend; i++){ | 
|---|
|  | 661 | logging_debug( "local locator found " << (*i)->toString() ); | 
|---|
|  | 662 | IPv4Locator* ipv4locator = dynamic_cast<IPv4Locator*>(*i); | 
|---|
|  | 663 |  | 
|---|
|  | 664 | if( *ipv4locator != IPv4Locator::LOCALHOST && | 
|---|
|  | 665 | *ipv4locator != IPv4Locator::ANY       && | 
|---|
|  | 666 | *ipv4locator != IPv4Locator::BROADCAST  ){ | 
|---|
|  | 667 |  | 
|---|
|  | 668 | ipv4locator->setPort( listenport ); | 
|---|
|  | 669 | changedLocator = *localDescriptor.locator != *ipv4locator; | 
|---|
|  | 670 | localDescriptor.locator = ipv4locator; | 
|---|
|  | 671 | logging_info( "binding to addr = " << ipv4locator->toString() ); | 
|---|
|  | 672 | foundLocator = true; | 
|---|
|  | 673 | break; | 
|---|
|  | 674 | } | 
|---|
|  | 675 | } // for( ; i != iend; i++) | 
|---|
|  | 676 |  | 
|---|
|  | 677 | // | 
|---|
|  | 678 | // if we found no locator, bind to localhost | 
|---|
|  | 679 | // | 
|---|
|  | 680 |  | 
|---|
|  | 681 | if( !foundLocator ){ | 
|---|
|  | 682 | changedLocator = *localDescriptor.locator != IPv4Locator::LOCALHOST; | 
|---|
|  | 683 | localDescriptor.locator = new IPv4Locator( IPv4Locator::LOCALHOST ); | 
|---|
|  | 684 | ((IPv4Locator*)(localDescriptor.locator))->setPort( listenport ); | 
|---|
|  | 685 | logging_info( "found no good local lcoator, binding to addr = " << | 
|---|
|  | 686 | localDescriptor.locator->toString() ); | 
|---|
|  | 687 | } | 
|---|
|  | 688 |  | 
|---|
|  | 689 | // | 
|---|
|  | 690 | // if we have connections that have no more longer endpoints | 
|---|
|  | 691 | // close these. they will be automatically built up again. | 
|---|
|  | 692 | // also update the local locator in the linkset mapping | 
|---|
|  | 693 | // | 
|---|
|  | 694 |  | 
|---|
|  | 695 | if( changedLocator ){ | 
|---|
|  | 696 |  | 
|---|
|  | 697 | logging_debug( "local endp locator has changed to " << localDescriptor.toString() << | 
|---|
|  | 698 | ", resettings connections that end at old locator " << | 
|---|
|  | 699 | oldLocalDescriptor.toString()); | 
|---|
|  | 700 |  | 
|---|
|  | 701 | LinkSet::iterator i = linkSet.begin(); | 
|---|
|  | 702 | LinkSet::iterator iend = linkSet.end(); | 
|---|
|  | 703 |  | 
|---|
|  | 704 | for( ; i != iend; i++ ){ | 
|---|
|  | 705 |  | 
|---|
|  | 706 | logging_debug( "checking connection for locator change: " << | 
|---|
|  | 707 | " local " << (*i).localLocator->toString() << | 
|---|
|  | 708 | " old " << oldLocalDescriptor.locator->toString() ); | 
|---|
|  | 709 |  | 
|---|
|  | 710 | if( *((*i).localLocator) == *(oldLocalDescriptor.locator) ){ | 
|---|
|  | 711 |  | 
|---|
|  | 712 | logging_debug("terminating connection to " << (*i).remoteLocator->toString() ); | 
|---|
|  | 713 | transport->terminate( oldLocalDescriptor.locator, (*i).remoteLocator ); | 
|---|
|  | 714 |  | 
|---|
|  | 715 | (*i).localLocator = localDescriptor.locator; | 
|---|
|  | 716 | } | 
|---|
|  | 717 | } // for( ; i != iend; i++ ) | 
|---|
|  | 718 |  | 
|---|
|  | 719 | // wait 500ms to give the sockets time to shut down | 
|---|
|  | 720 | usleep( 500000 ); | 
|---|
|  | 721 |  | 
|---|
|  | 722 | } else { | 
|---|
|  | 723 |  | 
|---|
|  | 724 | logging_debug( "locator has not changed, not resetting connections" ); | 
|---|
|  | 725 |  | 
|---|
|  | 726 | } | 
|---|
|  | 727 |  | 
|---|
|  | 728 | // | 
|---|
|  | 729 | // handle the connections that have no longer any | 
|---|
|  | 730 | // valid locator. send update messages with the new | 
|---|
|  | 731 | // locator,  so the remote node updates its locator/link mapping | 
|---|
|  | 732 | // | 
|---|
|  | 733 |  | 
|---|
|  | 734 | LinkSet::iterator iAffected = linkSet.begin(); | 
|---|
|  | 735 | LinkSet::iterator endAffected = linkSet.end(); | 
|---|
|  | 736 |  | 
|---|
|  | 737 | for( ; iAffected != endAffected; iAffected++ ){ | 
|---|
|  | 738 | LinkDescriptor descr = *iAffected; | 
|---|
|  | 739 | logging_debug( "sending out link locator update to " << descr.remoteLocator->toString() ); | 
|---|
|  | 740 |  | 
|---|
|  | 741 | AribaBaseMsg updateMsg(         descr.remoteLocator, | 
|---|
|  | 742 | AribaBaseMsg::LINK_STATE_UPDATE, | 
|---|
|  | 743 | descr.localLink, descr.remoteLink ); | 
|---|
|  | 744 |  | 
|---|
|  | 745 | transport->sendMessage( &updateMsg ); | 
|---|
|  | 746 | } | 
|---|
|  | 747 | } | 
|---|
|  | 748 |  | 
|---|
|  | 749 | }} // namespace ariba, communication | 
|---|