Ignore:
Timestamp:
2021-08-08T17:30:29Z (4 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
a7f7b9c3
Parents:
b4edc96
Message:

Represent Ethernet address as a number instead of an array

Carefully document the design since this breaks the principle of least
surprise. Also add unit tests.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/inet/include/inet/eth_addr.h

    rb4edc96 r3e6bca8  
    4141
    4242#define ETH_ADDR_SIZE 6
     43#define ETH_ADDR_STR_SIZE (6 * 2 + 5)
    4344
     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 */
    4465typedef struct {
    45         uint8_t b[ETH_ADDR_SIZE];
     66        uint64_t a;
    4667} eth_addr_t;
     68
     69/** Ethernet address in the form of a string */
     70typedef struct {
     71        char str[ETH_ADDR_STR_SIZE + 1];
     72} eth_addr_str_t;
    4773
    4874extern const eth_addr_t eth_addr_broadcast;
     
    5278
    5379extern int eth_addr_compare(const eth_addr_t *, const eth_addr_t *);
     80extern void eth_addr_format(eth_addr_t *, eth_addr_str_t *);
    5481
    5582#endif
Note: See TracChangeset for help on using the changeset viewer.