Changeset 3e6bca8 in mainline for uspace/lib/inet
- Timestamp:
- 2021-08-08T17:30:29Z (4 years ago)
- Branches:
- master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- a7f7b9c3
- Parents:
- b4edc96
- Location:
- uspace/lib/inet
- Files:
-
- 2 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/inet/include/inet/eth_addr.h
rb4edc96 r3e6bca8 41 41 42 42 #define ETH_ADDR_SIZE 6 43 #define ETH_ADDR_STR_SIZE (6 * 2 + 5) 43 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 */ 44 65 typedef struct { 45 uint 8_t b[ETH_ADDR_SIZE];66 uint64_t a; 46 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; 47 73 48 74 extern const eth_addr_t eth_addr_broadcast; … … 52 78 53 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 *); 54 81 55 82 #endif -
uspace/lib/inet/meson.build
rb4edc96 r3e6bca8 44 44 'src/udp.c', 45 45 ) 46 47 test_src = files( 48 'test/eth_addr.c', 49 'test/main.c', 50 ) -
uspace/lib/inet/src/addr.c
rb4edc96 r3e6bca8 55 55 const addr32_t addr32_broadcast_all_hosts = 0xffffffff; 56 56 57 static const eth_addr_t inet_eth_addr_solicited_node = { 58 0x33, 0x33, 0xff, 0, 0, 0 59 }; 57 static eth_addr_t inet_eth_addr_solicited_node = 58 ETH_ADDR_INITIALIZER(0x33, 0x33, 0xff, 0, 0, 0); 60 59 61 60 static const inet_addr_t inet_addr_any_addr = { … … 91 90 void eth_addr_solicited_node(const addr128_t ip, eth_addr_t *mac) 92 91 { 93 memcpy(&mac->b[0], &inet_eth_addr_solicited_node.b[0], 3); 94 memcpy(&mac->b[3], ip + 13, 3); 92 uint8_t b[6]; 93 mac->a = inet_eth_addr_solicited_node.a; 94 95 eth_addr_encode(&inet_eth_addr_solicited_node, b); 96 memcpy(&b[3], ip + 13, 3); 97 eth_addr_decode(b, mac); 95 98 } 96 99 -
uspace/lib/inet/src/eth_addr.c
rb4edc96 r3e6bca8 37 37 #include <inet/eth_addr.h> 38 38 #include <mem.h> 39 #include <stdio.h> 39 40 40 const eth_addr_t eth_addr_broadcast = { 41 0xff, 0xff, 0xff, 0xff, 0xff, 0xff 42 }; 41 const eth_addr_t eth_addr_broadcast = 42 ETH_ADDR_INITIALIZER(0xff, 0xff, 0xff, 0xff, 0xff, 0xff); 43 43 44 44 void eth_addr_encode(eth_addr_t *addr, void *buf) 45 45 { 46 46 uint8_t *bp = (uint8_t *)buf; 47 uint64_t a; 48 int i; 47 49 48 memcpy(bp, &addr->b[0], ETH_ADDR_SIZE); 50 a = addr->a; 51 52 for (i = 0; i < ETH_ADDR_SIZE; i++) 53 bp[i] = (a >> (40 - 8 * i)) & 0xff; 49 54 } 50 55 … … 52 57 { 53 58 const uint8_t *bp = (uint8_t *)buf; 59 uint64_t a; 60 int i; 54 61 55 memcpy(&addr->b[0], bp, ETH_ADDR_SIZE); 62 a = 0; 63 for (i = 0; i < ETH_ADDR_SIZE; i++) 64 a |= (uint64_t)bp[i] << (40 - 8 * i); 65 66 addr->a = a; 56 67 } 57 68 … … 62 73 int eth_addr_compare(const eth_addr_t *a, const eth_addr_t *b) 63 74 { 64 return memcmp(a->b, b->b, ETH_ADDR_SIZE) == 0; 75 if (a->a < b->a) 76 return -1; 77 else if (a->a == b->a) 78 return 0; 79 else 80 return 1; 81 } 82 83 void eth_addr_format(eth_addr_t *addr, eth_addr_str_t *saddr) 84 { 85 int i; 86 87 snprintf(saddr->str, 3, "%02x", 88 (unsigned)((addr->a >> 40) & 0xff)); 89 for (i = 1; i < ETH_ADDR_SIZE; i++) { 90 snprintf(saddr->str + 2 + 3 * (i - 1), 4, ":%02x", 91 (unsigned)((addr->a >> (40 - i * 8)) & 0xff)); 92 } 65 93 } 66 94
Note:
See TracChangeset
for help on using the changeset viewer.