0

We have a Java web service that gets a String representing a MAC address. We want to validate if the given String actually matches the required format. Further we want to create a normalized form to make them comparable.

I searched quite a while but found only some "loose regular expressions". We would really prefer to have a library that can parse different formats and return a normalized (String) representation (i.e. 01-23-45-67-89-ab and 01:23:45:67:89:ab would return the same representation and be comparable).

I expected to find some mature and well tested library, which could do that kind of task. Can anyone please point me to it? I just cannot believe that it doesn't exist yet.

I would be very thankful to not see any RegExes as possible solutions (we know how to do that if necessary).

Egga Hartung
  • 1,061
  • 11
  • 23
  • 1
    Possible duplicate of [How do I validate the format of a MAC address?](http://stackoverflow.com/questions/7629643/how-do-i-validate-the-format-of-a-mac-address). – Kei Minagawa Mar 17 '14 at 09:51
  • @user2931409 Not a duplicate. as I asked explicitly for a Java library. The other question asks for Python and RegExes are given as answers... – Egga Hartung Mar 17 '14 at 10:00
  • What is the definition of `valid` string format of MAC address? – Kei Minagawa Mar 17 '14 at 11:54
  • We are looking for MAC-48 specifically as defined in [IEEE Std 802-2001 section 6.2.3](http://standards.ieee.org/getieee802/download/802-2001.pdf) – Egga Hartung Mar 17 '14 at 12:31
  • I can't find the library for MAC address. But I found some IP address validation class. See [here](http://stackoverflow.com/questions/5667371/validate-ip-address). I think you want like these classes. – Kei Minagawa Mar 17 '14 at 13:44
  • @user2931409 like these would be very good. Like [UUID](http://docs.oracle.com/javase/7/docs/api/java/util/UUID.html) would be perfect (including multiple constructors and normalization). – Egga Hartung Mar 17 '14 at 14:08

2 Answers2

2

The IPAddress Java library will do it. The javadoc is available at the link. Disclaimer: I am the project manager.

The library will read various common formats for MAC addresses, like aa:bb:cc:dd:ee:ff, aa-bb-cc-dd-ee-ff, aabb.ccdd.eeff, it supports addresses that are 48 or 64 bits, and also allows you to specify ranges of addresses like aa-ff:bb:cc:*:ee:ff

Verify if an address is valid:

    String str = "aa:bb:cc:dd:ee:ff";
    MACAddressString addrString = new MACAddressString(str);
    try {
         MACAddress addr = addrString.toAddress();
         ...
    } catch(AddressStringException e) {
        //e.getMessage provides validation issue
    }

The library is well tested, it has a test suite with thousands of tests.

Sean F
  • 4,344
  • 16
  • 30
1

mature and well tested library

To verify MAC addresses? It's 6 bytes in hex optionally separated by a delimiter. It's a homework assignment or light interview question, no need to write a library. My solution is 10 lines, and it's more paranoid than necessary...

Tassos Bassoukos
  • 16,017
  • 2
  • 36
  • 40
  • 3
    I heard statements like yours about timezones, character sets, email addresses, ... Due to an incomplete knowledge of the stated problem domains they introduced bugs. I don't know much about MAC addresses and don't want to invest time in studying the minutiae of different possible formats. Note: I'm not accusing you to not know anything. Just had bad experiences. And the fact that I couldn't find anything actually supports your argument. – Egga Hartung Mar 17 '14 at 12:37
  • 3
    Timezones: there's a database of them in Java. Chareacter sets: ditto. Email addresses: only way to verify is to send an email and wait for a response. Look, I understand where you're coming from (I've been there), but some times there actually is [an authoritative source](http://en.wikipedia.org/wiki/MAC_address#Notational_conventions), and it usually pays to keep simple things simple. – Tassos Bassoukos Mar 17 '14 at 14:17