def addlinkendpoints(self, message, servers1, servers2): """ For a link message that is not handled locally, inform the remote servers of the IP addresses used as tunnel endpoints by adding opaque data to the link message. :param core.api.coreapi.CoreMessage message: message to link end points :param servers1: :param servers2: :return: core link message :rtype: coreapi.CoreLinkMessage """ ip1 = "" for server in servers1: if server.host is not None: ip1 = server.host break ip2 = "" for server in servers2: if server.host is not None: ip2 = server.host break tlvdata = message.raw_message[coreapi.CoreMessage.header_len:] tlvdata += coreapi.CoreLinkTlv.pack(LinkTlvs.OPAQUE.value, "%s:%s" % (ip1, ip2)) newraw = coreapi.CoreLinkMessage.pack(message.flags, tlvdata) msghdr = newraw[:coreapi.CoreMessage.header_len] return coreapi.CoreLinkMessage(message.flags, msghdr, tlvdata)
def recv(self, server): """ Receive data on an emulation server socket and broadcast it to all connected session handlers. Returns the length of data recevied and forwarded. Return value of zero indicates the socket has closed and should be removed from the self.servers dict. :param CoreDistributedServer server: server to receive from :return: message length :rtype: int """ msghdr = server.sock.recv(coreapi.CoreMessage.header_len) if len(msghdr) == 0: # server disconnected logging.info("server disconnected, closing server") server.close() return 0 if len(msghdr) != coreapi.CoreMessage.header_len: logging.warning("warning: broker received not enough data len=%s", len(msghdr)) return len(msghdr) msgtype, msgflags, msglen = coreapi.CoreMessage.unpack_header(msghdr) msgdata = server.sock.recv(msglen) data = msghdr + msgdata count = None logging.debug("received message type: %s", MessageTypes(msgtype)) # snoop exec response for remote interactive TTYs if msgtype == MessageTypes.EXECUTE.value and msgflags & MessageFlags.TTY.value: data = self.fixupremotetty(msghdr, msgdata, server.host) logging.debug("created remote tty message: %s", data) elif msgtype == MessageTypes.NODE.value: # snoop node delete response to decrement node counts if msgflags & MessageFlags.DELETE.value: msg = coreapi.CoreNodeMessage(msgflags, msghdr, msgdata) nodenum = msg.get_tlv(NodeTlvs.NUMBER.value) if nodenum is not None: count = self.delnodemap(server, nodenum) elif msgtype == MessageTypes.LINK.value: # this allows green link lines for remote WLANs msg = coreapi.CoreLinkMessage(msgflags, msghdr, msgdata) self.session.sdt.handle_distributed(msg) elif msgtype == MessageTypes.EVENT.value: msg = coreapi.CoreEventMessage(msgflags, msghdr, msgdata) eventtype = msg.get_tlv(EventTlvs.TYPE.value) if eventtype == EventTypes.INSTANTIATION_COMPLETE.value: server.instantiation_complete = True if self.instantiation_complete(): self.session.check_runtime() else: logging.error("unknown message type received: %s", msgtype) try: for session_client in self.session_clients: session_client.sendall(data) except IOError: logging.exception("error sending message") if count is not None and count < 1: return 0 else: return len(data)