Statistics
| Branch: | Tag: | Revision:

root / snf-common / synnefo / util / mac2eui64.py @ 9e20fcee

History | View | Annotate | Download (2.4 kB)

1 9e20fcee Christos Stavrakakis
# Copyright 2011, 2012, 2013 GRNET S.A. All rights reserved.
2 74d988b0 Christos Stavrakakis
#
3 746c6bf4 Vangelis Koukis
# Redistribution and use in source and binary forms, with or
4 746c6bf4 Vangelis Koukis
# without modification, are permitted provided that the following
5 746c6bf4 Vangelis Koukis
# conditions are met:
6 74d988b0 Christos Stavrakakis
#
7 746c6bf4 Vangelis Koukis
#   1. Redistributions of source code must retain the above
8 746c6bf4 Vangelis Koukis
#      copyright notice, this list of conditions and the following
9 746c6bf4 Vangelis Koukis
#      disclaimer.
10 74d988b0 Christos Stavrakakis
#
11 746c6bf4 Vangelis Koukis
#   2. Redistributions in binary form must reproduce the above
12 746c6bf4 Vangelis Koukis
#      copyright notice, this list of conditions and the following
13 746c6bf4 Vangelis Koukis
#      disclaimer in the documentation and/or other materials
14 746c6bf4 Vangelis Koukis
#      provided with the distribution.
15 74d988b0 Christos Stavrakakis
#
16 746c6bf4 Vangelis Koukis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 746c6bf4 Vangelis Koukis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 746c6bf4 Vangelis Koukis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 746c6bf4 Vangelis Koukis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 746c6bf4 Vangelis Koukis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 746c6bf4 Vangelis Koukis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 746c6bf4 Vangelis Koukis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 746c6bf4 Vangelis Koukis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 746c6bf4 Vangelis Koukis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 746c6bf4 Vangelis Koukis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 746c6bf4 Vangelis Koukis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 746c6bf4 Vangelis Koukis
# POSSIBILITY OF SUCH DAMAGE.
28 74d988b0 Christos Stavrakakis
#
29 746c6bf4 Vangelis Koukis
# The views and conclusions contained in the software and
30 746c6bf4 Vangelis Koukis
# documentation are those of the authors and should not be
31 746c6bf4 Vangelis Koukis
# interpreted as representing official policies, either expressed
32 746c6bf4 Vangelis Koukis
# or implied, of GRNET S.A.
33 74d988b0 Christos Stavrakakis
#
34 746c6bf4 Vangelis Koukis
35 746c6bf4 Vangelis Koukis
from IPy import IP
36 746c6bf4 Vangelis Koukis
37 746c6bf4 Vangelis Koukis
# Adapted from NFDHCPD's mac2eui64 utility, repository commit:feca7bb95
38 746c6bf4 Vangelis Koukis
39 74d988b0 Christos Stavrakakis
40 746c6bf4 Vangelis Koukis
def mac2eui64(mac, prefixstr):
41 746c6bf4 Vangelis Koukis
    try:
42 746c6bf4 Vangelis Koukis
        prefix = IP(prefixstr)
43 746c6bf4 Vangelis Koukis
    except ValueError:
44 746c6bf4 Vangelis Koukis
        raise Exception("Invalid IPv6 prefix '%s'" % prefixstr)
45 746c6bf4 Vangelis Koukis
46 746c6bf4 Vangelis Koukis
    if prefix.version() != 6:
47 746c6bf4 Vangelis Koukis
        raise Exception("%s is not a valid IPv6 prefix" % prefixstr)
48 746c6bf4 Vangelis Koukis
49 746c6bf4 Vangelis Koukis
    if prefix.prefixlen() != 64:
50 746c6bf4 Vangelis Koukis
        raise Exception("Cannot generate an EUI-64 address on a non-64 subnet")
51 746c6bf4 Vangelis Koukis
52 746c6bf4 Vangelis Koukis
    mac_parts = mac.split(":")
53 746c6bf4 Vangelis Koukis
    pfx_parts = prefix.net().strFullsize().split(":")
54 746c6bf4 Vangelis Koukis
55 746c6bf4 Vangelis Koukis
    if len(mac_parts) != 6:
56 746c6bf4 Vangelis Koukis
        raise Exception("%s is not a valid MAC-48 address" % mac)
57 746c6bf4 Vangelis Koukis
58 74d988b0 Christos Stavrakakis
    eui64 = mac_parts[:3] + ["ff", "fe"] + mac_parts[3:]
59 746c6bf4 Vangelis Koukis
60 746c6bf4 Vangelis Koukis
    eui64[0] = "%02x" % (int(eui64[0], 16) ^ 0x02)
61 746c6bf4 Vangelis Koukis
62 746c6bf4 Vangelis Koukis
    ip = ":".join(pfx_parts[:4])
63 746c6bf4 Vangelis Koukis
    for l in range(0, len(eui64), 2):
64 74d988b0 Christos Stavrakakis
        ip += ":%s" % "".join(eui64[l:l + 2])
65 746c6bf4 Vangelis Koukis
66 746c6bf4 Vangelis Koukis
    return IP(ip).strCompressed()
67 746c6bf4 Vangelis Koukis
68 746c6bf4 Vangelis Koukis
# vim: set ts=4 sts=4 sw=4 et ai :