[f05edcb] | 1 | /*
|
---|
| 2 | * Copyright (c) 2021 Jiri Svoboda
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup libc
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file
|
---|
| 34 | * @brief
|
---|
| 35 | */
|
---|
| 36 |
|
---|
| 37 | #include <inet/eth_addr.h>
|
---|
| 38 | #include <mem.h>
|
---|
[3e6bca8] | 39 | #include <stdio.h>
|
---|
[f05edcb] | 40 |
|
---|
[2177b39] | 41 | /** Ethernet broadcast address */
|
---|
[3e6bca8] | 42 | const eth_addr_t eth_addr_broadcast =
|
---|
| 43 | ETH_ADDR_INITIALIZER(0xff, 0xff, 0xff, 0xff, 0xff, 0xff);
|
---|
[b4edc96] | 44 |
|
---|
[2177b39] | 45 | /** Encode Ethernet address to buffer.
|
---|
| 46 | *
|
---|
| 47 | * Encode Ethernet address as a sequence of ETH_ADDR_SIZE bytes into a buffer.
|
---|
| 48 | *
|
---|
| 49 | * @param addr Ethernet address
|
---|
| 50 | * @param buf Buffer (ETH_ADDR_SIZE bytes in size) to store bytes
|
---|
| 51 | */
|
---|
[241ab7e] | 52 | void eth_addr_encode(eth_addr_t *addr, uint8_t *buf)
|
---|
[f05edcb] | 53 | {
|
---|
[3e6bca8] | 54 | uint64_t a;
|
---|
| 55 | int i;
|
---|
[f05edcb] | 56 |
|
---|
[3e6bca8] | 57 | a = addr->a;
|
---|
| 58 |
|
---|
| 59 | for (i = 0; i < ETH_ADDR_SIZE; i++)
|
---|
[241ab7e] | 60 | buf[i] = (a >> (40 - 8 * i)) & 0xff;
|
---|
[f05edcb] | 61 | }
|
---|
| 62 |
|
---|
[2177b39] | 63 | /** Decode Ethernet address from buffer.
|
---|
| 64 | *
|
---|
| 65 | * Decode Ethernet address from a buffer containing a sequence of
|
---|
| 66 | * ETH_ADDR_SIZE bytes.
|
---|
| 67 | *
|
---|
| 68 | * @param buf Buffer (ETH_ADDR_SIZE bytes in size)
|
---|
| 69 | * @param addr Place to store Ethernet address
|
---|
| 70 | */
|
---|
[241ab7e] | 71 | void eth_addr_decode(const uint8_t *buf, eth_addr_t *addr)
|
---|
[f05edcb] | 72 | {
|
---|
[3e6bca8] | 73 | uint64_t a;
|
---|
| 74 | int i;
|
---|
| 75 |
|
---|
| 76 | a = 0;
|
---|
| 77 | for (i = 0; i < ETH_ADDR_SIZE; i++)
|
---|
[241ab7e] | 78 | a |= (uint64_t)buf[i] << (40 - 8 * i);
|
---|
[f05edcb] | 79 |
|
---|
[3e6bca8] | 80 | addr->a = a;
|
---|
[f05edcb] | 81 | }
|
---|
| 82 |
|
---|
[2177b39] | 83 | /** Compare Ethernet addresses.
|
---|
[b4edc96] | 84 | *
|
---|
[2177b39] | 85 | * @param a First address
|
---|
| 86 | * @param b Second address,
|
---|
| 87 | * @return -1, 0, 1 iff @a a is less than, equal to or greater than @a b,
|
---|
| 88 | * respectively.
|
---|
[b4edc96] | 89 | */
|
---|
| 90 | int eth_addr_compare(const eth_addr_t *a, const eth_addr_t *b)
|
---|
| 91 | {
|
---|
[3e6bca8] | 92 | if (a->a < b->a)
|
---|
| 93 | return -1;
|
---|
| 94 | else if (a->a == b->a)
|
---|
| 95 | return 0;
|
---|
| 96 | else
|
---|
| 97 | return 1;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
[2177b39] | 100 | /** Format Ethernet address as a string.
|
---|
| 101 | *
|
---|
| 102 | * @param addr Ethernet address
|
---|
| 103 | * @param saddr Structure for storing string representation
|
---|
| 104 | * of @a addr. The caller can access it as @a saddr->str.
|
---|
| 105 | */
|
---|
[3e6bca8] | 106 | void eth_addr_format(eth_addr_t *addr, eth_addr_str_t *saddr)
|
---|
| 107 | {
|
---|
| 108 | int i;
|
---|
| 109 |
|
---|
| 110 | snprintf(saddr->str, 3, "%02x",
|
---|
| 111 | (unsigned)((addr->a >> 40) & 0xff));
|
---|
| 112 | for (i = 1; i < ETH_ADDR_SIZE; i++) {
|
---|
| 113 | snprintf(saddr->str + 2 + 3 * (i - 1), 4, ":%02x",
|
---|
| 114 | (unsigned)((addr->a >> (40 - i * 8)) & 0xff));
|
---|
| 115 | }
|
---|
[b4edc96] | 116 | }
|
---|
| 117 |
|
---|
[f05edcb] | 118 | /** @}
|
---|
| 119 | */
|
---|