1/* 2 * Copyright 2017, Data61, CSIRO (ABN 41 687 119 230) 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6#include <stdint.h> 7#include <stdio.h> 8#include "util.h" 9 10/* Number of bytes in a MAC address. */ 11#define SIZEOF_MAC (48 / 8) 12 13uint64_t make_mac(const char *data) { 14 uint64_t x = 0; 15 16 /* Consume bytes from the input string until we've exhausted it and then 17 * just pad the rest of the MAC with 0s. 18 */ 19 for (unsigned int i = 0; i < SIZEOF_MAC; i++) { 20 if (*data == '\0') { 21 x <<= 8; 22 } else { 23 x = (x << 8) | (uint64_t)(*data); 24 data++; 25 } 26 } 27 return x; 28} 29 30/* Note that MAC addresses are stored big endian and IP addresses are stored 31 * little endian. Only chosen this way to make construction simpler. 32 */ 33 34void mac_to_string(uint64_t input, char *output) { 35 sprintf(output, "%02x:%02x:%02x:%02x:%02x:%02x", 36 (unsigned int)(input & 0xff), (unsigned int)((input >> 8) & 0xff), 37 (unsigned int)((input >> 16) & 0xff), 38 (unsigned int)((input >> 24) & 0xff), 39 (unsigned int)((input >> 32) & 0xff), 40 (unsigned int)((input >> 40) & 0xff)); 41} 42 43void ip_to_string(uint32_t input, char *output) { 44 sprintf(output, "%u.%u.%u.%u", input >> 24, (input >> 16) & 0xff, 45 (input >> 8) & 0xff, input & 0xff); 46} 47