UniRec  2.9.3
macaddr.h
Go to the documentation of this file.
1 
7 /*
8  * Copyright (C) 2017 CESNET
9  *
10  * LICENSE TERMS
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  * notice, this list of conditions and the following disclaimer in
19  * the documentation and/or other materials provided with the
20  * distribution.
21  * 3. Neither the name of the Company nor the names of its contributors
22  * may be used to endorse or promote products derived from this
23  * software without specific prior written permission.
24  *
25  * ALTERNATIVELY, provided that this notice is retained in full, this
26  * product may be distributed under the terms of the GNU General Public
27  * License (GPL) version 2 or later, in which case the provisions
28  * of the GPL apply INSTEAD OF those given above.
29  *
30  * This software is provided ``as is'', and any express or implied
31  * warranties, including, but not limited to, the implied warranties of
32  * merchantability and fitness for a particular purpose are disclaimed.
33  * In no event shall the company or contributors be liable for any
34  * direct, indirect, incidental, special, exemplary, or consequential
35  * damages (including, but not limited to, procurement of substitute
36  * goods or services; loss of use, data, or profits; or business
37  * interruption) however caused and on any theory of liability, whether
38  * in contract, strict liability, or tort (including negligence or
39  * otherwise) arising in any way out of the use of this software, even
40  * if advised of the possibility of such damage.
41  *
42  */
43 
44 #ifndef _MACADDR_H_
45 #define _MACADDR_H_
46 
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50 
51 #include <stdint.h>
52 #include <stdio.h>
53 #include <inttypes.h>
54 #include <string.h>
55 #include "inline.h"
56 
57 #define MAC_STR_LEN 18
58 
59 #ifndef PRIx8
60 #define PRIx8 "x"
61 #endif
62 
63 #ifndef SCNx8
64 #define SCNx8 "hhx"
65 #endif
66 
67 #define MAC_ADD_FORMAT_SCN "%02" SCNx8 ":%02" SCNx8 ":%02" SCNx8 ":%02" SCNx8 ":%02" SCNx8 ":%02" SCNx8 ""
68 #define MAC_ADD_FORMAT_PRI "%02" PRIx8 ":%02" PRIx8 ":%02" PRIx8 ":%02" PRIx8 ":%02" PRIx8 ":%02" PRIx8 ""
69 
73 typedef struct __attribute__((packed)) mac_addr_s {
74  uint8_t bytes[6];
76 
84 {
85  mac_addr_t tmp;
86 
87  memcpy(&tmp.bytes, array, 6);
88 
89  return tmp;
90 }
91 
99 INLINE int mac_from_str(const char *str, mac_addr_t *addr)
100 {
101  int res = sscanf(str, MAC_ADD_FORMAT_SCN, &addr->bytes[0], &addr->bytes[1], &addr->bytes[2],
102  &addr->bytes[3], &addr->bytes[4], &addr->bytes[5]);
103  if (res == 6) {
104  return 1;
105  } else {
106  memset(addr->bytes, 0, 6);
107  return 0;
108  }
109 }
110 
118 INLINE int mac_cmp(const mac_addr_t *addr1, const mac_addr_t *addr2)
119 {
120  return memcmp(addr1->bytes, addr2->bytes, 6);
121 }
122 
129 INLINE void mac_to_str(const mac_addr_t *addr, char *str)
130 {
131  if (str != NULL) {
132  snprintf(str, MAC_STR_LEN, MAC_ADD_FORMAT_PRI,
133  addr->bytes[0], addr->bytes[1], addr->bytes[2],
134  addr->bytes[3], addr->bytes[4], addr->bytes[5]);
135  }
136 }
137 
144 INLINE void mac_to_bytes(const mac_addr_t *addr, uint8_t *array)
145 {
146  memcpy(array, (void *) addr->bytes, 6);
147 }
148 
149 #ifdef __cplusplus
150 }
151 #endif
152 
153 #endif
#define MAC_ADD_FORMAT_SCN
Definition: macaddr.h:67
INLINE void mac_to_str(const mac_addr_t *addr, char *str)
Definition: macaddr.h:129
mac_addr_t
Definition: macaddr.h:75
INLINE void mac_to_bytes(const mac_addr_t *addr, uint8_t *array)
Definition: macaddr.h:144
#define MAC_ADD_FORMAT_PRI
Definition: macaddr.h:68
INLINE mac_addr_t mac_from_bytes(uint8_t *array)
Definition: macaddr.h:83
INLINE int mac_from_str(const char *str, mac_addr_t *addr)
Definition: macaddr.h:99
#define INLINE
Definition: inline.h:12
memcpy(buffer, rec, ur_rec_size(tmplt, rec))
#define MAC_STR_LEN
Definition: macaddr.h:57
struct __attribute__((packed)) mac_addr_s
Definition: macaddr.h:73
INLINE int mac_cmp(const mac_addr_t *addr1, const mac_addr_t *addr2)
Definition: macaddr.h:118