-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
42 lines (37 loc) · 946 Bytes
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import socket
import struct
import sys
import threading
import time
import binascii
import string
from ctypes import create_string_buffer
# helper functions
# return the bit width of a value
def bitLen(i):
length = 0
while i:
i >>= 1
length += 1
return length
# quickly swap the endianness of a 32 bit int
def swap32(val):
return (((val << 24) & 0xFF000000) |
((val << 8) & 0x00FF0000) |
((val >> 8) & 0x0000FF00) |
((val >> 24) & 0x000000FF))
# return the count of bits that match in the prefix (32 bit values only)
def prefixMatchSize32(v1, v2):
#print "[DBUG]: prefix for " + str(v1) + " and " + str(v2)
length = 0
i = 1 << 31
s1 = swap32(v1)
s2 = swap32(v2)
while (v1 & i) == (v2 & i):
i >>= 1
length += 1
return length
# turn a MAC address string into a set of 6 bytes
def deMACify(macStr):
ret = string.replace(macStr, ":", "")
return binascii.unhexlify(ret)