IP and MAC – format, domain of values, order, avoiding duplications

01.12.2019

IP and MAC addresses are a good example of data for which we typically care about format, domain of values and we do not want to have duplicate values. Another task is to secure a proper ordering because a usual ordering of text values will not suit us.
Let`s look now how to cope with these values in ObjectGears system and its Configuration database. IP and MAC addresses are defined as text columns of various entities in the network layer and layer of the operating system (OS Machine).

IP and MAC addresses are a good example of data for which we typically care about format, order, domain of values and we do not want to have duplicate values.

IP and MAC standards

Standards applicable for IP and MAC addresses define integer of hexadecimal domains, full or abbreviated notations, separators etc. A particular rule, which should be applied on particular IP and MAC addresses, will depend on the fact, whether we want to allow any value corresponding to the standard or whether we want to apply our own, more strict rules.
The standard enables e.g. both colon, dot and dash as separators in MAC address but not combination of these characters in a single address (Example:  3D:F2:C9:A6:B3:4F or 3D-F2-C9-A6-B3-4F but not 3D-F2:C9-A6:B3-4F). Valid formats are also: 3DF2.C9A6.B34F or 3df2.c9a6.b34f. However, we want to use just colon and lower case.
The standard allow the full or abbreviated notation for IPv6 addresses (Example:  2001:0db8:85a3:0000:0000:8a2e:0370:7334 or omitted leading zero 2001:db8:85a3: 0:0:8a2e:370:7334). It is also possible to replace one or more consecutive groups for one empty group 2001:db8:85a3::8a2e:370:7334. However, we are going to use our own notation corresponding to some other recommendations regarding IPv6 addresses.
The reason for that is a unified look that is generally preferred. In both cases regular expressions (Regex) will help us.

Regular expression

No matter whether you decide to create a corresponding regular expression yourself  or you will pick one out of many that were created by other people and shared on various webs supporting developer collaboration (e.g. Stackoverflow), you can just insert the expression in the definition of the text column (field Regular expression) in ObjectGears and fill in Error message of reg. expression, that will be displayed when the expression pattern is not met. Regular expression can be also used in a rule condition.

Ordering

Another issue might be ordering which should reflect selected format. E.g. address 10.123.222.105 should be ordered as a bigger one behind 10.123.222.98. Usual ordering would not do that. We will create a column (type Decimal) and convert the address into it so that we assign three digits to each IP octet. The update will be secured by a rule in the given class. Order will be then determined by values in this Decimal column.

Avoiding duplicate values

Similarly, we may want to avoid duplicate values. This can be secured by columns that define unique records in the class definition. Uniqueness can be ensured also by a rule. In some cases we may need to check whether certain address is not used in another class or which range is assigned to certain VLAN. This can be secured by a rule (type Script) that will be applied before or after record saving and will look up necessary connections in another classes.