1 /*-------------------------------------------------------------------------
4 * PostgreSQL type definitions for 8 byte (EUI-64) MAC addresses.
6 * EUI-48 (6 byte) MAC addresses are accepted as input and are stored in
7 * EUI-64 format, with the 4th and 5th bytes set to FF and FE, respectively.
9 * Output is always in 8 byte (EUI-64) format.
11 * The following code is written with the assumption that the OUI field
14 * Portions Copyright (c) 1998-2017, PostgreSQL Global Development Group
17 * src/backend/utils/adt/mac8.c
19 *-------------------------------------------------------------------------
24 #include "access/hash.h"
25 #include "libpq/pqformat.h"
26 #include "utils/builtins.h"
27 #include "utils/inet.h"
30 * Utility macros used for sorting and comparing:
32 #define hibits(addr) \
33 ((unsigned long)(((addr)->a<<24) | ((addr)->b<<16) | ((addr)->c<<8) | ((addr)->d)))
35 #define lobits(addr) \
36 ((unsigned long)(((addr)->e<<24) | ((addr)->f<<16) | ((addr)->g<<8) | ((addr)->h)))
38 static unsigned char hex2_to_uchar(const unsigned char *str, const unsigned char *ptr);
40 static const signed char hexlookup[128] = {
41 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
42 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
43 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
44 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
45 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
46 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
47 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
48 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
52 * hex2_to_uchar - convert 2 hex digits to a byte (unsigned char)
54 * This will ereport() if the end of the string is reached ('\0' found), or if
55 * either character is not a valid hex digit.
57 * ptr is the pointer to where the digits to convert are in the string, str is
58 * the entire string, which is used only for error reporting.
60 static inline unsigned char
61 hex2_to_uchar(const unsigned char *ptr, const unsigned char *str)
63 unsigned char ret = 0;
66 /* Handle the first character */
70 lookup = hexlookup[*ptr];
76 /* Move to the second character */
82 lookup = hexlookup[*ptr];
92 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
93 errmsg("invalid input syntax for type %s: \"%s\"", "macaddr8",
96 /* We do not actually reach here */
101 * MAC address (EUI-48 and EUI-64) reader. Accepts several common notations.
104 macaddr8_in(PG_FUNCTION_ARGS)
106 const unsigned char *str = (unsigned char *) PG_GETARG_CSTRING(0);
107 const unsigned char *ptr = str;
118 unsigned char spacer = '\0';
120 /* skip leading spaces */
121 while (*ptr && isspace(*ptr))
124 /* digits must always come in pairs */
125 while (*ptr && *(ptr + 1))
128 * Attempt to decode each byte, which must be 2 hex digits in a row.
129 * If either digit is not hex, hex2_to_uchar will throw ereport() for
130 * us. Either 6 or 8 byte MAC addresses are supported.
133 /* Attempt to collect a byte */
139 a = hex2_to_uchar(ptr, str);
142 b = hex2_to_uchar(ptr, str);
145 c = hex2_to_uchar(ptr, str);
148 d = hex2_to_uchar(ptr, str);
151 e = hex2_to_uchar(ptr, str);
154 f = hex2_to_uchar(ptr, str);
157 g = hex2_to_uchar(ptr, str);
160 h = hex2_to_uchar(ptr, str);
163 /* must be trailing garbage... */
165 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
166 errmsg("invalid input syntax for type %s: \"%s\"", "macaddr8",
170 /* Move forward to where the next byte should be */
173 /* Check for a spacer, these are valid, anything else is not */
174 if (*ptr == ':' || *ptr == '-' || *ptr == '.')
176 /* remember the spacer used, if it changes then it isn't valid */
180 /* Have to use the same spacer throughout */
181 else if (spacer != *ptr)
183 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
184 errmsg("invalid input syntax for type %s: \"%s\"", "macaddr8",
187 /* move past the spacer */
191 /* allow trailing whitespace after if we have 6 or 8 bytes */
192 if (count == 6 || count == 8)
196 while (*++ptr && isspace(*ptr));
198 /* If we found a space and then non-space, it's invalid */
201 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
202 errmsg("invalid input syntax for type %s: \"%s\"", "macaddr8",
208 /* Convert a 6 byte MAC address to macaddr8 */
220 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
221 errmsg("invalid input syntax for type %s: \"%s\"", "macaddr8",
224 result = (macaddr8 *) palloc0(sizeof(macaddr8));
235 PG_RETURN_MACADDR8_P(result);
239 * MAC8 address (EUI-64) output function. Fixed format.
242 macaddr8_out(PG_FUNCTION_ARGS)
244 macaddr8 *addr = PG_GETARG_MACADDR8_P(0);
247 result = (char *) palloc(32);
249 snprintf(result, 32, "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
250 addr->a, addr->b, addr->c, addr->d,
251 addr->e, addr->f, addr->g, addr->h);
253 PG_RETURN_CSTRING(result);
257 * macaddr8_recv - converts external binary format(EUI-48 and EUI-64) to macaddr8
259 * The external representation is just the eight bytes, MSB first.
262 macaddr8_recv(PG_FUNCTION_ARGS)
264 StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
267 addr = (macaddr8 *) palloc0(sizeof(macaddr8));
269 addr->a = pq_getmsgbyte(buf);
270 addr->b = pq_getmsgbyte(buf);
271 addr->c = pq_getmsgbyte(buf);
280 addr->d = pq_getmsgbyte(buf);
281 addr->e = pq_getmsgbyte(buf);
284 addr->f = pq_getmsgbyte(buf);
285 addr->g = pq_getmsgbyte(buf);
286 addr->h = pq_getmsgbyte(buf);
288 PG_RETURN_MACADDR8_P(addr);
292 * macaddr8_send - converts macaddr8(EUI-64) to binary format
295 macaddr8_send(PG_FUNCTION_ARGS)
297 macaddr8 *addr = PG_GETARG_MACADDR8_P(0);
300 pq_begintypsend(&buf);
301 pq_sendbyte(&buf, addr->a);
302 pq_sendbyte(&buf, addr->b);
303 pq_sendbyte(&buf, addr->c);
304 pq_sendbyte(&buf, addr->d);
305 pq_sendbyte(&buf, addr->e);
306 pq_sendbyte(&buf, addr->f);
307 pq_sendbyte(&buf, addr->g);
308 pq_sendbyte(&buf, addr->h);
310 PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
315 * macaddr8_cmp_internal - comparison function for sorting:
318 macaddr8_cmp_internal(macaddr8 *a1, macaddr8 *a2)
320 if (hibits(a1) < hibits(a2))
322 else if (hibits(a1) > hibits(a2))
324 else if (lobits(a1) < lobits(a2))
326 else if (lobits(a1) > lobits(a2))
333 macaddr8_cmp(PG_FUNCTION_ARGS)
335 macaddr8 *a1 = PG_GETARG_MACADDR8_P(0);
336 macaddr8 *a2 = PG_GETARG_MACADDR8_P(1);
338 PG_RETURN_INT32(macaddr8_cmp_internal(a1, a2));
342 * Boolean comparison functions.
346 macaddr8_lt(PG_FUNCTION_ARGS)
348 macaddr8 *a1 = PG_GETARG_MACADDR8_P(0);
349 macaddr8 *a2 = PG_GETARG_MACADDR8_P(1);
351 PG_RETURN_BOOL(macaddr8_cmp_internal(a1, a2) < 0);
355 macaddr8_le(PG_FUNCTION_ARGS)
357 macaddr8 *a1 = PG_GETARG_MACADDR8_P(0);
358 macaddr8 *a2 = PG_GETARG_MACADDR8_P(1);
360 PG_RETURN_BOOL(macaddr8_cmp_internal(a1, a2) <= 0);
364 macaddr8_eq(PG_FUNCTION_ARGS)
366 macaddr8 *a1 = PG_GETARG_MACADDR8_P(0);
367 macaddr8 *a2 = PG_GETARG_MACADDR8_P(1);
369 PG_RETURN_BOOL(macaddr8_cmp_internal(a1, a2) == 0);
373 macaddr8_ge(PG_FUNCTION_ARGS)
375 macaddr8 *a1 = PG_GETARG_MACADDR8_P(0);
376 macaddr8 *a2 = PG_GETARG_MACADDR8_P(1);
378 PG_RETURN_BOOL(macaddr8_cmp_internal(a1, a2) >= 0);
382 macaddr8_gt(PG_FUNCTION_ARGS)
384 macaddr8 *a1 = PG_GETARG_MACADDR8_P(0);
385 macaddr8 *a2 = PG_GETARG_MACADDR8_P(1);
387 PG_RETURN_BOOL(macaddr8_cmp_internal(a1, a2) > 0);
391 macaddr8_ne(PG_FUNCTION_ARGS)
393 macaddr8 *a1 = PG_GETARG_MACADDR8_P(0);
394 macaddr8 *a2 = PG_GETARG_MACADDR8_P(1);
396 PG_RETURN_BOOL(macaddr8_cmp_internal(a1, a2) != 0);
400 * Support function for hash indexes on macaddr8.
403 hashmacaddr8(PG_FUNCTION_ARGS)
405 macaddr8 *key = PG_GETARG_MACADDR8_P(0);
407 return hash_any((unsigned char *) key, sizeof(macaddr8));
411 * Arithmetic functions: bitwise NOT, AND, OR.
414 macaddr8_not(PG_FUNCTION_ARGS)
416 macaddr8 *addr = PG_GETARG_MACADDR8_P(0);
419 result = (macaddr8 *) palloc0(sizeof(macaddr8));
420 result->a = ~addr->a;
421 result->b = ~addr->b;
422 result->c = ~addr->c;
423 result->d = ~addr->d;
424 result->e = ~addr->e;
425 result->f = ~addr->f;
426 result->g = ~addr->g;
427 result->h = ~addr->h;
429 PG_RETURN_MACADDR8_P(result);
433 macaddr8_and(PG_FUNCTION_ARGS)
435 macaddr8 *addr1 = PG_GETARG_MACADDR8_P(0);
436 macaddr8 *addr2 = PG_GETARG_MACADDR8_P(1);
439 result = (macaddr8 *) palloc0(sizeof(macaddr8));
440 result->a = addr1->a & addr2->a;
441 result->b = addr1->b & addr2->b;
442 result->c = addr1->c & addr2->c;
443 result->d = addr1->d & addr2->d;
444 result->e = addr1->e & addr2->e;
445 result->f = addr1->f & addr2->f;
446 result->g = addr1->g & addr2->g;
447 result->h = addr1->h & addr2->h;
449 PG_RETURN_MACADDR8_P(result);
453 macaddr8_or(PG_FUNCTION_ARGS)
455 macaddr8 *addr1 = PG_GETARG_MACADDR8_P(0);
456 macaddr8 *addr2 = PG_GETARG_MACADDR8_P(1);
459 result = (macaddr8 *) palloc0(sizeof(macaddr8));
460 result->a = addr1->a | addr2->a;
461 result->b = addr1->b | addr2->b;
462 result->c = addr1->c | addr2->c;
463 result->d = addr1->d | addr2->d;
464 result->e = addr1->e | addr2->e;
465 result->f = addr1->f | addr2->f;
466 result->g = addr1->g | addr2->g;
467 result->h = addr1->h | addr2->h;
469 PG_RETURN_MACADDR8_P(result);
473 * Truncation function to allow comparing macaddr8 manufacturers.
476 macaddr8_trunc(PG_FUNCTION_ARGS)
478 macaddr8 *addr = PG_GETARG_MACADDR8_P(0);
481 result = (macaddr8 *) palloc0(sizeof(macaddr8));
492 PG_RETURN_MACADDR8_P(result);
496 * Set 7th bit for modified EUI-64 as used in IPv6.
499 macaddr8_set7bit(PG_FUNCTION_ARGS)
501 macaddr8 *addr = PG_GETARG_MACADDR8_P(0);
504 result = (macaddr8 *) palloc0(sizeof(macaddr8));
506 result->a = addr->a | 0x02;
515 PG_RETURN_MACADDR8_P(result);
518 /*----------------------------------------------------------
519 * Conversion operators.
520 *---------------------------------------------------------*/
523 macaddrtomacaddr8(PG_FUNCTION_ARGS)
525 macaddr *addr6 = PG_GETARG_MACADDR_P(0);
528 result = (macaddr8 *) palloc0(sizeof(macaddr8));
530 result->a = addr6->a;
531 result->b = addr6->b;
532 result->c = addr6->c;
535 result->f = addr6->d;
536 result->g = addr6->e;
537 result->h = addr6->f;
540 PG_RETURN_MACADDR8_P(result);
544 macaddr8tomacaddr(PG_FUNCTION_ARGS)
546 macaddr8 *addr = PG_GETARG_MACADDR8_P(0);
549 result = (macaddr *) palloc0(sizeof(macaddr));
551 if ((addr->d != 0xFF) || (addr->e != 0xFE))
553 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
554 errmsg("macaddr8 data out of range to convert to macaddr"),
555 errhint("Only addresses that have FF and FE as values in the "
556 "4th and 5th bytes, from the left, for example: "
557 "XX-XX-XX-FF-FE-XX-XX-XX, are eligible to be converted "
558 "from macaddr8 to macaddr.")));
567 PG_RETURN_MACADDR_P(result);