]> Frank Brehm's Git Trees - my-stuff/ipv6-hostpart.git/commitdiff
Funktion hergestellt
authorFrank Brehm <frank@brehm-online.com>
Sun, 2 Jan 2011 21:34:32 +0000 (21:34 +0000)
committerFrank Brehm <frank@brehm-online.com>
Sun, 2 Jan 2011 21:34:32 +0000 (21:34 +0000)
git-svn-id: http://svn.brehm-online.com/svn/my-stuff/ipv6-hostpart/trunk@173 ec8d2aa5-1599-4edb-8739-2b3a1bc399aa

bin/ipv6-hostpart.py
bin/net/ll_hw_address.py

index 4bc8bda5dd6d59560101d90f779838c8db0bab5a..04e11d1be11248def2614098378234dc8d201eb7 100755 (executable)
@@ -40,6 +40,7 @@ if len( opt_parser.args ) < 1:
 for address in opt_parser.args:
     try:
         addr_object = LlHardwareAddress( address = address, verbose = verbose )
+        print addr_object.as_str() + " - " + addr_object.as_ipv6hostpart()
     except ValueError as e:
         msg = "Could not determine low level hardware address {0!r}.".format( address )
         print >> sys.stderr, msg
index 67852944cdb610e26a7b36c80be7f0bd1eb343f7..97e631ddc9bf4599d907b4a800bc830a095b3101 100755 (executable)
@@ -123,4 +123,80 @@ class LlHardwareAddress(LoggingObject):
         raise ValueError( "Could not import address {0!r}.".format( address ) )
         return
 
+    #------------------------------------------------------
+    def as_str( self ):
+        '''Returns a string with an usable display of the address
+           @return: The address as string
+           @rtype: str
+        '''
+
+        if self.address is None:
+            return None
+
+        def format_byte( byte ):
+            return "{0:02x}".format( byte )
+
+        return_value = '<undef>'
+        if self.type == 'guid':
+            return_value = '0x'
+            for byte in self.address:
+                return_value += "{0:02x}".format( byte )
+        elif self.type == 'mac':
+            return_value = ':'.join( map( format_byte, self.address ) )
+
+        return return_value
+
+    #------------------------------------------------------
+    def as_ipv6hostpart( self ):
+        '''Returns a string with an usable 64-bit host part of an IPv6 address
+           @return: The host part as string
+           @rtype: str
+        '''
+
+        if self.address is None:
+            return None
+
+        pp = pprint.PrettyPrinter( indent = 4, depth = 6, width = 120 )
+
+        return_value = '<undef>'
+        byte_list = []
+        for byte in self.address:
+            byte_list.append( byte )
+        if self.type == 'mac':
+            byte_list.insert( 3, 254 )
+            byte_list.insert( 3, 255 )
+
+        if self.verbose >= 2:
+            self.logger.debug( "Bytelist to display: {0!r}.".format( pp.pformat( byte_list ) ) )
+
+        first_byte = byte_list[0]
+        seventh_bit = ( first_byte & 2 ) >> 1
+        eighth_bit  = ( first_byte & 1 )
+        if seventh_bit == 0:
+            seventh_bit = 1
+        else:
+            seventh_bit = 0
+        if self.verbose >= 3:
+            self.logger.debug( "Seventh bit: {0!r}.".format( seventh_bit ) )
+        first_byte &= 252
+        first_byte += ( seventh_bit << 1 ) + eighth_bit
+        if self.verbose >= 3:
+            self.logger.debug( "First byte: {0!r}.".format( first_byte ) )
+        byte_list[0] = first_byte
+
+        nibble_list = []
+        nibble_list.append( ( byte_list[0] << 8 ) + byte_list[1] )
+        nibble_list.append( ( byte_list[2] << 8 ) + byte_list[3] )
+        nibble_list.append( ( byte_list[4] << 8 ) + byte_list[5] )
+        nibble_list.append( ( byte_list[6] << 8 ) + byte_list[7] )
+        if self.verbose >= 2:
+            self.logger.debug( "Nibblelist to display: {0!r}.".format( pp.pformat( nibble_list ) ) )
+
+        def format_nibble( nibble ):
+            return "{0:x}".format( nibble )
+
+        return ':'.join( map( format_nibble, nibble_list ) )
+
+    #------------------------------------------------------
+
 # vim: fileencoding=utf-8 filetype=python ts=4 expandtab