Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-2413

Native types for IP Address and MAC Address

    Details

      Description

      I am porting a network-oriented application from PostgreSQL to MongoDB. The application makes heavy use of the 'inet' and 'mac' data types, especially when searching for IP addresses in a netblock.

      One could argue that IP Addresses can simply be stored and searched as strings, which is true. However, it becomes increasingly difficult when you throw in netmasks and IPv6. For example, the IPv6 address "FFAB::1234" is really "FFAB:0000:0000:0000:0000:0000:0000:1234", and both forms are acceptable string representations.

      I could not find any plans for including this feature in MongoDB, so I decided to tackle it myself. The resulting work can be found at:

      https://github.com/redmeadowman/mongo

      The change involves adding the new types to BSON, and adding new classes to the JavaScript shell. I've added two new types, IpAddr() and MacAddr().

      IpAddr - Contains an IPv4 or IPv6 address along with a netmask
      Examples to try in the shell:
      var ip4 = IpAddr("192.168.1.1")
      ip4.mask
      ip4.version
      ip4.mask = 16
      ip4.mask
      ip4 = IpAddr("192.168.1.1/24")
      ip4.mask
      ip4.network
      ip4.broadcast
      var ip6 = IpAddr("1234::abcd");
      ip6.mask
      ip6.version
      ip6.mask = 64
      ip6.mask
      var ip6 = IpAddr("1234::abcd/32");
      ip6.network
      ip6.broadcast
      ip6 = IpAddr("::192.168.1.1/16");
      db.test.insert(

      {ip: IpAddr("192.168.1.1")}

      )
      db.test.insert(

      {ip: IpAddr("1234::")}

      )
      db.test.find()

      MacAddr - Contains a 6-octet Media Access Control Address (Ethernet hardware address)
      var mac = MacAddr("00:23:45:ab:cd:ef");
      db.test.insert(

      {mac: MacAddr("1:2:3:4:5:6")}

      )
      db.test.find()

      The application I'm porting also makes heavy use of UUIDs, so I've modified the Shell UUID type so it is now a native type of JavaScript. This now works (no need to say 'new UUID(...'
      var uuid = UUID("123456789abcdef12345678901234567");
      db.test.insert(

      {uuid: UUID("123456789abcdef12345678901234567")}

      )

      The branch listed above includes the code changes against the latest master (as of 24 Jan 2011), plus unit tests in both dbtests/ and jstests/.

        Attachments

          Issue Links

            Activity

              People

              • Votes:
                11 Vote for this issue
                Watchers:
                12 Start watching this issue

                Dates

                • Created:
                  Updated: