| 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 inet
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /**
|
|---|
| 33 | * @file
|
|---|
| 34 | * @brief
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 | #ifndef _LIBC_INET_ETH_ADDR_H_
|
|---|
| 38 | #define _LIBC_INET_ETH_ADDR_H_
|
|---|
| 39 |
|
|---|
| 40 | #include <stdint.h>
|
|---|
| 41 |
|
|---|
| 42 | #define ETH_ADDR_SIZE 6
|
|---|
| 43 | #define ETH_ADDR_STR_SIZE (6 * 2 + 5)
|
|---|
| 44 |
|
|---|
| 45 | #define ETH_ADDR_INITIALIZER(aa, bb, cc, dd, ee, ff) \
|
|---|
| 46 | { .a = ((uint64_t)(aa) << 40) | ((uint64_t)(bb) << 32) | \
|
|---|
| 47 | ((uint64_t)(cc) << 24) | ((uint64_t)(dd) << 16) | \
|
|---|
| 48 | ((uint64_t)(ee) << 8) | (ff) }
|
|---|
| 49 |
|
|---|
| 50 | /** Ethernet address.
|
|---|
| 51 | *
|
|---|
| 52 | * Defined as a structure. This provides strong type checking.
|
|---|
| 53 | *
|
|---|
| 54 | * Since the structure is not opaque, this allows eth_addr_t to be
|
|---|
| 55 | * allocated statically and copied around using the assignment operator.
|
|---|
| 56 | *
|
|---|
| 57 | * It is stored in the lower 48 bits of a 64-bit integer. This is an internal
|
|---|
| 58 | * representation that allows simple and efficient operation. Most CPUs
|
|---|
| 59 | * will be much faster (and we will need less instructions) operating
|
|---|
| 60 | * on a single 64-bit integer than on six individual 8-bit integers.
|
|---|
| 61 | *
|
|---|
| 62 | * Kind reader will appreciate the cleverness and elegance of this
|
|---|
| 63 | * representation.
|
|---|
| 64 | */
|
|---|
| 65 | typedef struct {
|
|---|
| 66 | uint64_t a;
|
|---|
| 67 | } eth_addr_t;
|
|---|
| 68 |
|
|---|
| 69 | /** Ethernet address in the form of a string */
|
|---|
| 70 | typedef struct {
|
|---|
| 71 | char str[ETH_ADDR_STR_SIZE + 1];
|
|---|
| 72 | } eth_addr_str_t;
|
|---|
| 73 |
|
|---|
| 74 | extern const eth_addr_t eth_addr_broadcast;
|
|---|
| 75 |
|
|---|
| 76 | extern void eth_addr_encode(eth_addr_t *, uint8_t *);
|
|---|
| 77 | extern void eth_addr_decode(const uint8_t *, eth_addr_t *);
|
|---|
| 78 |
|
|---|
| 79 | extern int eth_addr_compare(const eth_addr_t *, const eth_addr_t *);
|
|---|
| 80 | extern void eth_addr_format(eth_addr_t *, eth_addr_str_t *);
|
|---|
| 81 |
|
|---|
| 82 | #endif
|
|---|
| 83 |
|
|---|
| 84 | /** @}
|
|---|
| 85 | */
|
|---|