1717from scapy .consts import FREEBSD , NETBSD , OPENBSD , WINDOWS
1818from scapy .error import log_loading
1919from scapy .compat import plain_str
20+ import scapy .modules .six as six
2021
2122
2223############
@@ -194,6 +195,9 @@ class ManufDA(DADict):
194195 def fixname (self , val ):
195196 return plain_str (val )
196197
198+ def __dir__ (self ):
199+ return ["lookup" , "reverse_lookup" ]
200+
197201 def _get_manuf_couple (self , mac ):
198202 oui = ":" .join (mac .split (":" )[:3 ]).upper ()
199203 return self .__dict__ .get (oui , (mac , mac ))
@@ -210,24 +214,42 @@ def _resolve_MAC(self, mac):
210214 return ":" .join ([self [oui ][0 ]] + mac .split (":" )[3 :])
211215 return mac
212216
213- def __repr__ (self ):
214- return "\n " .join ("<%s %s, %s>" % (i [0 ], i [1 ][0 ], i [1 ][1 ]) for i in self .__dict__ .items ()) # noqa: E501
217+ def lookup (self , mac ):
218+ """Find OUI name matching to a MAC"""
219+ oui = ":" .join (mac .split (":" )[:3 ]).upper ()
220+ return self [oui ]
221+
222+ def reverse_lookup (self , name , case_sensitive = False ):
223+ """Find all MACs registered to a OUI
224+ params:
225+ - name: the OUI name
226+ - case_sensitive: default to False
227+ returns: a dict of mac:tuples (Name, Extended Name)
228+ """
229+ if case_sensitive :
230+ filtr = lambda x , l : any (x == z for z in l )
231+ else :
232+ name = name .lower ()
233+ filtr = lambda x , l : any (x == z .lower () for z in l )
234+ return {k : v for k , v in six .iteritems (self .__dict__ )
235+ if filtr (name , v )}
215236
216237
217238def load_manuf (filename ):
239+ """Load manuf file from Wireshark.
240+ param:
241+ - filename: the file to load the manuf file from"""
218242 manufdb = ManufDA (_name = filename )
219243 with open (filename , "rb" ) as fdesc :
220244 for line in fdesc :
221245 try :
222246 line = line .strip ()
223247 if not line or line .startswith (b"#" ):
224248 continue
225- oui , shrt = line .split ()[:2 ]
226- i = line .find (b"#" )
227- if i < 0 :
228- lng = shrt
229- else :
230- lng = line [i + 2 :]
249+ parts = line .split (None , 2 )
250+ oui , shrt = parts [:2 ]
251+ lng = parts [2 ].lstrip (b"#" ).strip () if len (parts ) > 2 else ""
252+ lng = lng or shrt
231253 manufdb [oui ] = plain_str (shrt ), plain_str (lng )
232254 except Exception :
233255 log_loading .warning ("Couldn't parse one line from [%s] [%r]" ,
0 commit comments