Пример #1
0
    def decode(self):
        msg.decode(self)
        self._register_options()
        self['options'] = {}
        while self.offset < len(self.buf):
            code = struct.unpack('B', self.buf[self.offset:self.offset + 1])[0]
            if code == 0:
                self.offset += 1
                continue
            if code == 255:
                return self
            # code is unknown -- bypass it
            if code not in self._decode_map:
                length = struct.unpack(
                    'B', self.buf[self.offset + 1:self.offset + 2])[0]
                self.offset += length + 2
                continue

            # code is known, work on it
            option_class = getattr(self, self._decode_map[code]['format'])
            option = option_class(buf=self.buf, offset=self.offset)
            option.decode()
            self.offset += option.length
            if option.value is not None:
                value = option.value
            else:
                value = option
            self['options'][self._decode_map[code]['name']] = value
        return self
Пример #2
0
    def decode(self):
        self.data_length = struct.unpack(
            'B', self.buf[self.offset + 1:self.offset + 2])[0]
        if self.policy is not None:
            if self.policy['format'] == 'string':
                fmt = '%is' % self.data_length
            else:
                fmt = self.policy['format']
            value = struct.unpack(
                fmt,
                self.buf[self.offset + 2:self.offset + 2 + self.data_length])
            if len(value) == 1:
                value = value[0]
            value = self.policy.get('decode', lambda x: x)(value)
            if isinstance(value, basestring) and \
                    self.policy['format'] == 'string':
                value = value[:value.find('\x00')]
            self.value = value
        else:
            # remember current offset as msg.decode() will advance it
            offset = self.offset
            # move past the code and option length bytes so that msg.decode()
            # starts parsing at the right location
            self.offset += 2
            msg.decode(self)
            # restore offset so that dhcpmsg.decode() advances it correctly
            self.offset = offset

        return self
Пример #3
0
    def decode(self):
        msg.decode(self)
        self._register_options()
        self['options'] = {}
        while self.offset < len(self.buf):
            code = struct.unpack('B', self.buf[self.offset:self.offset + 1])[0]
            if code == 0:
                self.offset += 1
                continue
            if code == 255:
                return self
            # code is unknown -- bypass it
            if code not in self._decode_map:
                length = struct.unpack('B', self.buf[self.offset + 1:
                                                     self.offset + 2])[0]
                self.offset += length + 2
                continue

            # code is known, work on it
            option_class = getattr(self, self._decode_map[code]['format'])
            option = option_class(buf=self.buf, offset=self.offset)
            option.decode()
            self.offset += option.length
            if option.value is not None:
                value = option.value
            else:
                value = option
            self['options'][self._decode_map[code]['name']] = value
        return self
Пример #4
0
    def decode(self):
        self.data_length = struct.unpack('B', self.buf[self.offset + 1:
                                                       self.offset + 2])[0]
        if self.policy is not None:
            if self.policy['format'] == 'string':
                fmt = '%is' % self.data_length
            else:
                fmt = self.policy['format']
            value = struct.unpack(fmt, self.buf[self.offset + 2:
                                                self.offset + 2 +
                                                self.data_length])
            if len(value) == 1:
                value = value[0]
            value = self.policy.get('decode', lambda x: x)(value)
            if isinstance(value, basestring) and \
                    self.policy['format'] == 'string':
                value = value[:value.find('\x00')]
            self.value = value
        else:
            # remember current offset as msg.decode() will advance it
            offset = self.offset
            # move past the code and option length bytes so that msg.decode()
            # starts parsing at the right location
            self.offset += 2
            msg.decode(self)
            # restore offset so that dhcpmsg.decode() advances it correctly
            self.offset = offset

        return self
 def decode(self):
     if self.policy is not None:
         self.data_length = struct.unpack('B', self.buf[self.offset + 1:
                                                        self.offset + 2])[0]
         if self.policy['fmt'] == 'string':
             fmt = '%is' % self.data_length
         else:
             fmt = self.policy['fmt']
         value = struct.unpack(fmt, self.buf[self.offset + 2:
                                             self.offset + 2 +
                                             self.data_length])
         if len(value) == 1:
             value = value[0]
         value = self.policy.get('decode', lambda x: x)(value)
         if isinstance(value, basestring) and \
                 self.policy['fmt'] == 'string':
             value = value[:value.find('\x00')]
         self.value = value
     else:
         msg.decode(self)
     return self
Пример #6
0
 def decode(self):
     if self.policy is not None:
         self.data_length = struct.unpack(
             'B', self.buf[self.offset + 1:self.offset + 2])[0]
         if self.policy['fmt'] == 'string':
             fmt = '%is' % self.data_length
         else:
             fmt = self.policy['fmt']
         value = struct.unpack(
             fmt,
             self.buf[self.offset + 2:self.offset + 2 + self.data_length])
         if len(value) == 1:
             value = value[0]
         value = self.policy.get('decode', lambda x: x)(value)
         if isinstance(value, basestring) and \
                 self.policy['fmt'] == 'string':
             value = value[:value.find('\x00')]
         self.value = value
     else:
         msg.decode(self)
     return self