def unpack (data, negotiated): communities = Communities() while data: if data and len(data) < 4: raise Notify(3,1,'could not decode community %s' % str([hex(ord(_)) for _ in data])) communities.add(Community.unpack(data[:4],negotiated)) data = data[4:] return communities
def unpack (data,negotiated): communities = Communities() while data: if data and len(data) < 4: raise Notify(3,1,'could not decode community %s' % str([hex(ord(_)) for _ in data])) communities.add(Community.unpack(data[:4],negotiated)) data = data[4:] return communities
def _parse_community (self, scope, data): separator = data.find(':') if separator > 0: prefix = int(data[:separator]) suffix = int(data[separator+1:]) if prefix >= pow(2,16): raise ValueError('invalid community %s (prefix too large)' % data) if suffix >= pow(2,16): raise ValueError('invalid community %s (suffix too large)' % data) return Community.cached(pack('!L',(prefix << 16) + suffix)) elif len(data) >= 2 and data[1] in 'xX': value = long(data,16) if value >= pow(2,32): raise ValueError('invalid community %s (too large)' % data) return Community.cached(pack('!L',value)) else: low = data.lower() if low == 'no-export': return Community.cached(Community.NO_EXPORT) elif low == 'no-advertise': return Community.cached(Community.NO_ADVERTISE) elif low == 'no-export-subconfed': return Community.cached(Community.NO_EXPORT_SUBCONFED) # no-peer is not a correct syntax but I am sure someone will make the mistake :) elif low == 'nopeer' or low == 'no-peer': return Community.cached(Community.NO_PEER) elif data.isdigit(): value = long(data) if value >= pow(2,32): raise ValueError('invalid community %s (too large)' % data) # return Community.cached(pack('!L',value)) return Community.cached(pack('!L',value)) else: raise ValueError('invalid community name %s' % data)
def _community(value): separator = value.find(':') if separator > 0: prefix = value[:separator] suffix = value[separator + 1:] if not prefix.isdigit() or not suffix.isdigit(): raise ValueError('invalid community %s' % value) prefix, suffix = int(prefix), int(suffix) # XXX: FIXME: add a Community.MAX to pow(2,16) -1 if prefix >= pow(2, 16): raise ValueError('invalid community %s (prefix too large)' % value) # XXX: FIXME: add a Community.MAX to pow(2,16) -1 if suffix >= pow(2, 16): raise ValueError('invalid community %s (suffix too large)' % value) return Community(pack('!L', (prefix << 16) + suffix)) elif value[:2].lower() == '0x': value = long(value, 16) # XXX: FIXME: add a Community.MAX to pow(2,16) -1 if value >= pow(2, 32): raise ValueError('invalid community %s (too large)' % value) return Community(pack('!L', value)) else: low = value.lower() if low == 'no-export': return Community(Community.NO_EXPORT) elif low == 'no-advertise': return Community(Community.NO_ADVERTISE) elif low == 'no-export-subconfed': return Community(Community.NO_EXPORT_SUBCONFED) # no-peer is not a correct syntax but I am sure someone will make the mistake :) elif low == 'nopeer' or low == 'no-peer': return Community(Community.NO_PEER) elif value.isdigit(): value = unpack('!L', value)[0] if value >= pow(2, 32): raise ValueError('invalid community %s (too large)' % value) return Community(pack('!L', value)) else: raise ValueError('invalid community name %s' % value)
def _parse_community(self, scope, data): separator = data.find(':') if separator > 0: prefix = int(data[:separator]) suffix = int(data[separator + 1:]) if prefix >= pow(2, 16): raise ValueError('invalid community %s (prefix too large)' % data) if suffix >= pow(2, 16): raise ValueError('invalid community %s (suffix too large)' % data) return Community.cached(pack('!L', (prefix << 16) + suffix)) elif len(data) >= 2 and data[1] in 'xX': value = long(data, 16) if value >= pow(2, 32): raise ValueError('invalid community %s (too large)' % data) return Community.cached(pack('!L', value)) else: low = data.lower() if low == 'no-export': return Community.cached(Community.NO_EXPORT) elif low == 'no-advertise': return Community.cached(Community.NO_ADVERTISE) elif low == 'no-export-subconfed': return Community.cached(Community.NO_EXPORT_SUBCONFED) # no-peer is not a correct syntax but I am sure someone will make the mistake :) elif low == 'nopeer' or low == 'no-peer': return Community.cached(Community.NO_PEER) elif data.isdigit(): value = long(data) if value >= pow(2, 32): raise ValueError('invalid community %s (too large)' % data) # return Community.cached(pack('!L',value)) return Community.cached(pack('!L', value)) else: raise ValueError('invalid community name %s' % data)