def get_bdt_from(self, network, ip_address, ip_port): a = _ip_format_str_as_ulong(ip_address) p = int(ip_port) addr = Addr(_ulong_as_str(a) + _ushort_as_str(p)) try: _module_lock.acquire() self.reports = {} finally: _module_lock.release() if debug > 1: _dump_send(network, addr.address, ReadBroadcastDistributionTable().encoding) send(network, addr.address, ReadBroadcastDistributionTable().encoding) timeout = 0 _module_lock.acquire() try: while (len(self.reports.values()) < 1) and (timeout < 10): _module_lock.release() pause(0.5) timeout += 1 _module_lock.acquire() finally: try: _module_lock.release() except: pass _module_lock.acquire() try: if (len(self.reports.values()) > 0): self.entries = self.reports.values()[0] finally: _module_lock.release() return self.report(network)
def forward_original_broadcast_message(self, network, addr, original_message_npdu): forwarded_message = ForwardedNPDU(addr, original_message_npdu) msg = forwarded_message.encoding for key in self.entries.keys(): if debug > 1: _dump_send(network, self.entries[key].addr.address, msg) send(network, self.entries[key].addr.address, msg)
def distribute(self, network, addr, forwarded_message): msg = forwarded_message.encoding for key in self.entries.keys(): entry = self.entries[key] if (entry.addr != addr): if debug > 1: _dump_send(network, self.entries[key].addr.address, msg) send(network, entry.addr.address, msg)
def distribute(self, network, addr, forwarded_message): if (self.entries): #we have a table for entry in self.entries: msg = forwarded_message.encoding if ((entry.ip_address != BBMD_servers[network].our_ip_address) or \ (entry.ip_port != BBMD_servers[network].our_ip_port)): if debug > 1: _dump_send(network, entry.asBBMDaddress().address, msg) send(network, entry.asBBMDaddress().address, msg)
def forward_original_broadcast_message(self, network, addr, original_message_npdu): if (self.entries): #we have a table #msg = ForwardedNPDU(addr, original_message_npdu).encoding for entry in self.entries: msg = ForwardedNPDU(addr, original_message_npdu).encoding if (entry.ip_address != BBMD_servers[network].our_ip_address) or \ (entry.ip_port != BBMD_servers[network].our_ip_port): if debug > 1: _dump_send(network, entry.asBBMDaddress().address, msg) send(network, entry.asBBMDaddress().address, msg)
def broadcast_forwarded_message(self, network, forwarded_message_packet): if self.ourBDTentry is None: self._set_our_bdt_entry() if self.ourBDTentry: if self.ourBDTentry.ip_mask & 1: #our mask indicates directed broadcasts are not used if debug > 1: _dump_send(network, self.interface.broadcast.address, forwarded_message_packet.encoding) send(network, self.interface.broadcast.address, forwarded_message_packet.encoding) else: if debug > 1: print "The f---ing thing did not go" else: if debug > 1: print "WE have no entry for ourself in the BDT but received a request to broadcast"
def report(self, network): self.reports = {} answer = [] if (self.entries): msg = ReadBroadcastDistributionTable().encoding for entry in self.entries: if debug > 1: _dump_send(network, entry.asBBMDaddress().address, msg) a = network b = entry.asBBMDaddress().address c = msg send(a, b, c) timeout = 0 _module_lock.acquire() try: while (len(self.reports.keys()) < len(self.entries)) and \ (timeout < 10): _module_lock.release() pause(0.5) timeout += 1 _module_lock.acquire() for entry in self.entries: status = 'no response' if self.reports.has_key(entry.asBBMDaddress().address): status = 'ok' if self.entries != \ self.reports[entry.asBBMDaddress().address]: status = 'mismatch' string = entry.as_text() answer.append(( string[0], string[1], string[2], status, )) finally: try: _module_lock.release() except: pass return answer
def update_bbmds(self,network, list): global our_ip_address global our_ip_port if self.node: if self.node.hasattr('allow_external_table_editor'): if not self.node.allow_external_table_editor: return #the supervising node will not allow the table to change self.from_text(list) #update our own table first try: if self.node: #persist the data self.node.save_table(list) except: #don't disturb proper response code if failed msglog.exception() if (self.entries): msg = WriteBroadcastDistributionTable(self).encoding for entry in self.entries: # if ((entry.ip_address != our_ip_address) or (entry.ip_port != our_ip_port)): #msg = WriteBroadcastDistributionTable(self).encoding if debug > 1: _dump_send(network, entry.asBBMDaddress().address, msg) send(network, entry.asBBMDaddress().address, msg) pass
def update_bbmds(self, network, list): global our_ip_address global our_ip_port if self.node: if self.node.hasattr('allow_external_table_editor'): if not self.node.allow_external_table_editor: return #the supervising node will not allow the table to change self.from_text(list) #update our own table first try: if self.node: #persist the data self.node.save_table(list) except: #don't disturb proper response code if failed msglog.exception() if (self.entries): msg = WriteBroadcastDistributionTable(self).encoding for entry in self.entries: # if ((entry.ip_address != our_ip_address) or (entry.ip_port != our_ip_port)): #msg = WriteBroadcastDistributionTable(self).encoding if debug > 1: _dump_send(network, entry.asBBMDaddress().address, msg) send(network, entry.asBBMDaddress().address, msg) pass
def report(self, network): self.reports = {} answer = [] if (self.entries): msg = ReadBroadcastDistributionTable().encoding for entry in self.entries: if debug > 1: _dump_send(network, entry.asBBMDaddress().address, msg) a=network b=entry.asBBMDaddress().address c=msg send(a,b,c) timeout = 0 _module_lock.acquire() try: while (len(self.reports.keys()) < len(self.entries)) and \ (timeout < 10): _module_lock.release() pause(0.5) timeout += 1 _module_lock.acquire() for entry in self.entries: status = 'no response' if self.reports.has_key(entry.asBBMDaddress().address): status = 'ok' if self.entries != \ self.reports[entry.asBBMDaddress().address]: status = 'mismatch' string = entry.as_text() answer.append((string[0], string[1], string[2], status,)) finally: try: _module_lock.release() except: pass return answer
def broadcast_forwarded_message(self, network, forwarded_message): msg = forwarded_message.encoding for key in self.entries.keys(): if debug > 1: _dump_send(network, self.entries[key].addr.address, msg) send(network, self.entries[key].addr.address, msg)
def _service_bbmd_queue( BBMD_queue ): #loop forever getting messages from the queue and servicing them global _module_lock while 1: # a tuple of network number, Addr, string a_message = BBMD_queue.get() #process the incoming message answer = None network = a_message[0] addr = Addr(a_message[1]) message_string = a_message[2] if debug > 1: print "BBMD recv: network=",network print "BBMD recv: from=",addr dump(message_string, "BBMD recv") if not BBMD_servers.has_key(network): continue bbmd = BBMD_servers[network] bdt = bbmd.bdt fdt = bbmd.fdt if ord(message_string[0]) != 0x81: #not bvlc frame # @fixme Just log it and toss the message. raise EInvalidValue('frame_type', ord(message_string[0]), text='Not a BVLC frame') # extract the 'BVLC Function' octet bvlc_function = ord(message_string[1]) if (bvlc_function == 0x0A): #OriginalUnicastNPDU if debug > 2: print "BBMD rcvd: OriginalUnicastNPDU:",network elif (bvlc_function == 0x0B): #OriginalBroadcastNPDU if debug > 2: print "BBMD rcvd: OriginalBroadcastNPDU:",network if bbmd.enable: obn = OriginalBroadcastNPDU(decode=message_string).npdu if debug > 2: print "BBMD send: ForwardedNPDU network:",network print "BBMD send: for=",addr dump(obn, "BBMD npdu") bdt.forward_original_broadcast_message(network, addr, obn) fdt.forward_original_broadcast_message(network, addr, obn) elif (bvlc_function == 0x00): #Result if debug > 2: print "BBMD rcvd: Result:",network pass elif (bvlc_function == 0x01): #WriteBroadcastDistributionTable if debug > 2: print "BBMD rcvd: WriteBroadcastDistributionTable:",network answer = bdt.write_new_table_contents( WriteBroadcastDistributionTable(decode=message_string)) elif (bvlc_function == 0x02): #ReadBroadcastDistributionTable if debug > 2: print "BBMD rcvd: ReadBroadcastDistributionTable:",network answer = bdt.read_table() elif (bvlc_function == 0x03): #ReadBroadcastDistributionTableAck if debug > 2: print "BBMD rcvd: ReadBroadcastDistributionTableAck:",network if bbmd.report_bdt: bbmd.report_bdt.read_broadcast_distributioan_table_ack( network, addr, ReadBroadcastDistributionTableAck( decode=message_string)) elif (bvlc_function == 0x04): #ForwardedNPDU if debug > 2: print "BBMD rcvd: ForwardedNPDU network",network if bbmd.enable: fnpdu = ForwardedNPDU(decode=message_string) if debug > 2: print "BBMD Broadcast ForwardedNPDU network",network print "BBMD broadcast for=", fnpdu.originating_address dump(fnpdu.npdu, "BBMD npdu") bdt.broadcast_forwarded_message(network, fnpdu) fdt.broadcast_forwarded_message(network, fnpdu) elif (bvlc_function == 0x05): #RegisterForeignDevice if debug > 2: print "BBMD rcvd: RegisterForeignDevice network",network if bbmd.enable: answer = fdt.register_foreign_device( RegisterForeignDevice(addr, decode=message_string)) elif (bvlc_function == 0x06): #ReadForeignDeviceTable answer = fdt.read_table() elif (bvlc_function == 0x07): #ReadForeignDeviceTableAck pass elif (bvlc_function == 0x08): #DeleteForeignDeviceTableEntry answer = fdt.delete_entry(DeleteForeignDeviceTableEntry( decode=message_string)) elif (bvlc_function == 0x09): #DistributeBroadcastToNetwork if debug > 2: print "BBMD rcvd: DistributeBroadcastToNetwork network",\ network if bbmd.enable: try: dbtn = DistributeBroadcastToNetwork( decode=message_string) answer = ForwardedNPDU (addr, dbtn.npdu) bdt.distribute(network, addr, answer) fdt.distribute(network, addr, answer) #locally broadcast the forwared npdu addr = Addr(bbmd.broadcast_address) except: msglog.exception() answer = Result(0x0060) else: if debug > 2: print "BBMD rcvd: Unknown message" if (answer): if debug > 1: _dump_send(network, addr.address, answer.encoding) send(network, addr.address, answer.encoding)
def _service_bbmd_queue(BBMD_queue): #loop forever getting messages from the queue and servicing them global _module_lock while 1: # a tuple of network number, Addr, string a_message = BBMD_queue.get() #process the incoming message answer = None network = a_message[0] addr = Addr(a_message[1]) message_string = a_message[2] if debug > 1: print "BBMD recv: network=", network print "BBMD recv: from=", addr dump(message_string, "BBMD recv") if not BBMD_servers.has_key(network): continue bbmd = BBMD_servers[network] bdt = bbmd.bdt fdt = bbmd.fdt if ord(message_string[0]) != 0x81: #not bvlc frame # @fixme Just log it and toss the message. raise EInvalidValue('frame_type', ord(message_string[0]), text='Not a BVLC frame') # extract the 'BVLC Function' octet bvlc_function = ord(message_string[1]) if (bvlc_function == 0x0A): #OriginalUnicastNPDU if debug > 2: print "BBMD rcvd: OriginalUnicastNPDU:", network elif (bvlc_function == 0x0B): #OriginalBroadcastNPDU if debug > 2: print "BBMD rcvd: OriginalBroadcastNPDU:", network if bbmd.enable: obn = OriginalBroadcastNPDU(decode=message_string).npdu if debug > 2: print "BBMD send: ForwardedNPDU network:", network print "BBMD send: for=", addr dump(obn, "BBMD npdu") bdt.forward_original_broadcast_message(network, addr, obn) fdt.forward_original_broadcast_message(network, addr, obn) elif (bvlc_function == 0x00): #Result if debug > 2: print "BBMD rcvd: Result:", network pass elif (bvlc_function == 0x01): #WriteBroadcastDistributionTable if debug > 2: print "BBMD rcvd: WriteBroadcastDistributionTable:", network answer = bdt.write_new_table_contents( WriteBroadcastDistributionTable(decode=message_string)) elif (bvlc_function == 0x02): #ReadBroadcastDistributionTable if debug > 2: print "BBMD rcvd: ReadBroadcastDistributionTable:", network answer = bdt.read_table() elif (bvlc_function == 0x03): #ReadBroadcastDistributionTableAck if debug > 2: print "BBMD rcvd: ReadBroadcastDistributionTableAck:", network if bbmd.report_bdt: bbmd.report_bdt.read_broadcast_distributioan_table_ack( network, addr, ReadBroadcastDistributionTableAck(decode=message_string)) elif (bvlc_function == 0x04): #ForwardedNPDU if debug > 2: print "BBMD rcvd: ForwardedNPDU network", network if bbmd.enable: fnpdu = ForwardedNPDU(decode=message_string) if debug > 2: print "BBMD Broadcast ForwardedNPDU network", network print "BBMD broadcast for=", fnpdu.originating_address dump(fnpdu.npdu, "BBMD npdu") bdt.broadcast_forwarded_message(network, fnpdu) fdt.broadcast_forwarded_message(network, fnpdu) elif (bvlc_function == 0x05): #RegisterForeignDevice if debug > 2: print "BBMD rcvd: RegisterForeignDevice network", network if bbmd.enable: answer = fdt.register_foreign_device( RegisterForeignDevice(addr, decode=message_string)) elif (bvlc_function == 0x06): #ReadForeignDeviceTable answer = fdt.read_table() elif (bvlc_function == 0x07): #ReadForeignDeviceTableAck pass elif (bvlc_function == 0x08): #DeleteForeignDeviceTableEntry answer = fdt.delete_entry( DeleteForeignDeviceTableEntry(decode=message_string)) elif (bvlc_function == 0x09): #DistributeBroadcastToNetwork if debug > 2: print "BBMD rcvd: DistributeBroadcastToNetwork network",\ network if bbmd.enable: try: dbtn = DistributeBroadcastToNetwork(decode=message_string) answer = ForwardedNPDU(addr, dbtn.npdu) bdt.distribute(network, addr, answer) fdt.distribute(network, addr, answer) #locally broadcast the forwared npdu addr = Addr(bbmd.broadcast_address) except: msglog.exception() answer = Result(0x0060) else: if debug > 2: print "BBMD rcvd: Unknown message" if (answer): if debug > 1: _dump_send(network, addr.address, answer.encoding) send(network, addr.address, answer.encoding)