source: package_tags/sipb-xen-python-pydhcplib/0.3.2-2/pydhcplib/type_hw_addr.py @ 2665

Last change on this file since 2665 was 361, checked in by broder, 16 years ago

Splitting pydhcplib off into its own package

Hold onto your hats, folks - this could get messy

File size: 2.8 KB
Line 
1# Anemon Dhcp
2# Copyright (C) 2005 Mathieu Ignacio -- mignacio@april.org
3#
4# This program is free software; you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation; either version 2 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program; if not, write to the Free Software
16# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA
17
18
19from binascii import unhexlify,hexlify
20
21# Check and convert hardware/nic/mac address type
22class hwmac:
23    def __init__(self,value="00:00:00:00:00:00") :
24        self._hw_numlist = []
25        self._hw_string = ""
26        hw_type = type(value)
27        if hw_type == str :
28            self._hw_string = value
29            self._StringToNumlist(value)
30            self._CheckNumList()
31        elif hw_type == list :
32            self._hw_numlist = value
33            self._CheckNumList()
34            self._NumlistToString()
35        else : raise TypeError , 'hwmac init : Valid types are  tr and list'
36
37
38
39    # Check if _hw_numlist is valid and raise error if not.
40    def _CheckNumList(self) :
41        if len(self._hw_numlist) != 6 : raise ValueError , "hwmac : wrong list length."
42        for part in self._hw_numlist :
43            if type (part) != int : raise TypeError , "hwmac : each element of list must be int"
44            if part < 0 or part > 255 : raise ValueError , "hwmac : need numbers between 0 and 255."
45        return True
46
47
48    def _StringToNumlist(self,value):
49        self._hw_string = self._hw_string.replace("-",":").replace(".",":")
50        self._hw_string = self._hw_string.lower()
51
52
53        for twochar in self._hw_string.split(":"):
54            self._hw_numlist.append(ord(unhexlify(twochar)))
55           
56    # Convert NumList type ip to String type ip
57    def _NumlistToString(self) :
58        self._hw_string = ":".join(map(hexlify,map(chr,self._hw_numlist)))
59
60    # Convert String type ip to NumList type ip
61    # return ip string
62    def str(self) :
63        return self._hw_string
64
65    # return ip list (useful for DhcpPacket class)
66    def list(self) :
67        return self._hw_numlist
68
69    def __hash__(self) :
70        return self._hw_string.__hash__()
71
72    def __repr__(self) :
73        return self._hw_string
74
75    def __cmp__(self,other) :
76        if self._hw_string == other : return 0
77        return 1
78
79    def __nonzero__(self) :
80        if self._hw_string != "00:00:00:00:00:00" : return 1
81        return 0
82
83
84
85
Note: See TracBrowser for help on using the repository browser.