source: mainline/uspace/lib/inet/src/eth_addr.c

Last change on this file was edeee9f, checked in by Jiri Svoboda <jiri@…>, 4 years ago

Fix header guards and doxy groups of stuff moved out of libc

  • Property mode set to 100644
File size: 3.3 KB
Line 
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 libinet
30 * @{
31 */
32/**
33 * @file
34 * @brief
35 */
36
37#include <inet/eth_addr.h>
38#include <mem.h>
39#include <stdio.h>
40
41/** Ethernet broadcast address */
42const eth_addr_t eth_addr_broadcast =
43 ETH_ADDR_INITIALIZER(0xff, 0xff, 0xff, 0xff, 0xff, 0xff);
44
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 */
52void eth_addr_encode(eth_addr_t *addr, uint8_t *buf)
53{
54 uint64_t a;
55 int i;
56
57 a = addr->a;
58
59 for (i = 0; i < ETH_ADDR_SIZE; i++)
60 buf[i] = (a >> (40 - 8 * i)) & 0xff;
61}
62
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 */
71void eth_addr_decode(const uint8_t *buf, eth_addr_t *addr)
72{
73 uint64_t a;
74 int i;
75
76 a = 0;
77 for (i = 0; i < ETH_ADDR_SIZE; i++)
78 a |= (uint64_t)buf[i] << (40 - 8 * i);
79
80 addr->a = a;
81}
82
83/** Compare Ethernet addresses.
84 *
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.
89 */
90int eth_addr_compare(const eth_addr_t *a, const eth_addr_t *b)
91{
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
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 */
106void 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 }
116}
117
118/** @}
119 */
Note: See TracBrowser for help on using the repository browser.