def getHardwareAddress(self): """ Extracts the client's MAC address from the DHCP packet. @rtype: str @return: The client's MAC address. """ length = self.getOption("hlen")[0] full_hw = self.getOption("chaddr") if length and length < len(full_hw): return hwmac(full_hw[0:length]).str() return hwmac(full_hw).str()
def _OptionsToBinary(self,parameter,value) : # Transform textual data into dhcp binary data p = parameter.strip() # 1- Search for header informations or specific parameter if p == 'op' or p == 'htype' : value = value.strip() if value.isdigit() : return [int(value)] try : value = DhcpNames[value.strip()] return [value] except KeyError : return [0] elif p == 'hlen' or p == 'hops' : try : value = int(value) return [value] except ValueError : return [0] elif p == 'secs' or p == 'flags' : try : value = ipv4(int(value)).list() except ValueError : value = [0,0,0,0] return value[2:] elif p == 'xid' : try : value = ipv4(int(value)).list() except ValueError : value = [0,0,0,0] return value elif p == 'ciaddr' or p == 'yiaddr' or p == 'siaddr' or p == 'giaddr' : try : ip = ipv4(value).list() except ValueError : ip = [0,0,0,0] return ip elif p == 'chaddr' : try: value = hwmac(value).list()+[0]*10 except ValueError,TypeError : value = [0]*16 return value
def _OptionsToBinary(self, parameter, value): # Transform textual data into dhcp binary data p = parameter.strip() # 1- Search for header informations or specific parameter if p == 'op' or p == 'htype': value = value.strip() if value.isdigit(): return [int(value)] try: value = DhcpNames[value.strip()] return [value] except KeyError: return [0] elif p == 'hlen' or p == 'hops': try: value = int(value) return [value] except ValueError: return [0] elif p == 'secs' or p == 'flags': try: value = ipv4(int(value)).list() except ValueError: value = [0, 0, 0, 0] return value[2:] elif p == 'xid': try: value = ipv4(int(value)).list() except ValueError: value = [0, 0, 0, 0] return value elif p == 'ciaddr' or p == 'yiaddr' or p == 'siaddr' or p == 'giaddr': try: ip = ipv4(value).list() except ValueError: ip = [0, 0, 0, 0] return ip elif p == 'chaddr': try: value = hwmac(value).list() + [0] * 10 except ValueError, TypeError: value = [0] * 16 return value
def _OptionsToBinary(self, parameter, value): # Transform textual data into dhcp binary data p = parameter.strip() # 1- Search for header informations or specific parameter if p == 'op' or p == 'htype': value = value.strip() if value.isdigit(): return [int(value)] try: value = DhcpNames[value.strip()] return [value] except KeyError: return [0] elif p == 'hlen' or p == 'hops': try: value = int(value) return [value] except ValueError: return [0] elif p == 'secs' or p == 'flags': try: value = ipv4(int(value)).list() except ValueError: value = [0, 0, 0, 0] return value[2:] elif p == 'xid': try: value = ipv4(int(value)).list() except ValueError: value = [0, 0, 0, 0] return value elif p == 'ciaddr' or p == 'yiaddr' or p == 'siaddr' or p == 'giaddr': try: ip = ipv4(value).list() except ValueError: ip = [0, 0, 0, 0] return ip elif p == 'chaddr': try: value = hwmac(value).list() + [0] * 10 except (ValueError, TypeError): value = [0] * 16 return value elif p == 'sname': return elif p == 'file': return elif p == 'parameter_request_list': value = value.strip().split(',') tmp = [] for each in value: if DhcpOptions.has_key(each): tmp.append(DhcpOptions[each]) return tmp elif p == 'dhcp_message_type': try: return [DhcpNames[value]] except KeyError: return # 2- Search for options try: option_type = DhcpOptionsTypes[DhcpOptions[parameter]] except KeyError: return False if option_type == "ipv4": # this is a single ip address try: binary_value = map(int, value.split(".")) except ValueError: return False elif option_type == "ipv4+": # this is multiple ip address iplist = value.split(",") opt = [] for single in iplist: opt += (ipv4(single).list()) binary_value = opt elif option_type == "32-bits": # This is probably a number... try: digit = int(value) binary_value = [ digit >> 24 & 0xFF, (digit >> 16) & 0xFF, (digit >> 8) & 0xFF, digit & 0xFF ] except ValueError: return False elif option_type == "16-bits": try: digit = int(value) binary_value = [(digit >> 8) & 0xFF, digit & 0xFF] except ValueError: return False elif option_type == "char": try: digit = int(value) binary_value = [digit & 0xFF] except ValueError: return False elif option_type == "bool": if value == "False" or value == "false" or value == 0: binary_value = [0] else: binary_value = [1] elif option_type == "string": binary_value = strlist(value).list() else: binary_value = strlist(value).list() return binary_value