2
# ether.py - lookup functions for ethernet addresses
4
# Copyright (C) 2010, 2011 Arthur de Jong
6
# This library is free software; you can redistribute it and/or
7
# modify it under the terms of the GNU Lesser General Public
8
# License as published by the Free Software Foundation; either
9
# version 2.1 of the License, or (at your option) any later version.
11
# This library is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
# Lesser General Public License for more details.
16
# You should have received a copy of the GNU Lesser General Public
17
# License along with this library; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
27
def ether_aton(ether):
28
"""Converst an ethernet address to binary form in network byte order."""
29
return struct.pack('BBBBBB', *(int(x, 16) for x in ether.split(':')))
31
def ether_ntoa(ether):
32
"""Conversts an ethernet address in network byte order to the string
34
return ':'.join('%x' % x for x in struct.unpack('6B', ether))
37
attmap = common.Attributes(cn='cn', macAddress='macAddress')
38
filter = '(objectClass=ieee802Device)'
41
class EtherRequest(common.Request):
43
def write(self, dn, attributes, parameters):
44
# get name and check against requested name
45
names = attributes['cn']
47
print 'Error: entry %s does not contain %s value' % ( dn, attmap['cn'])
48
if 'cn' in parameters:
49
if parameters['cn'].lower() not in (x.lower() for x in names):
51
names = ( parameters['cn'], )
52
# get addresses and convert to binary form
53
addresses = [ether_aton(x) for x in attributes['macAddress']]
55
print 'Error: entry %s does not contain %s value' % ( dn, attmap['macAddress'])
56
if 'macAddress' in parameters:
57
if ether_aton(parameters['macAddress']) not in addresses:
59
addresses = ( ether_aton(parameters['macAddress']), )
62
for ether in addresses:
63
self.fp.write_int32(constants.NSLCD_RESULT_BEGIN)
64
self.fp.write_string(name)
68
class EtherByNameRequest(EtherRequest):
70
action = constants.NSLCD_ACTION_ETHER_BYNAME
72
def read_parameters(self, fp):
73
return dict(cn=fp.read_string())
76
class EtherByEtherRequest(EtherRequest):
78
action = constants.NSLCD_ACTION_ETHER_BYETHER
80
def read_parameters(self, fp):
81
return dict(macAddress=ether_ntoa(fp.read(6)))
84
class EtherAllRequest(EtherRequest):
86
action = constants.NSLCD_ACTION_ETHER_ALL