aboutsummaryrefslogtreecommitdiffstats
path: root/include/jau/eui48.hpp
blob: d45efa8614963d21b55f9b2f4f8fe16e92d8e55c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*
 * Author: Sven Gothel <sgothel@jausoft.com>
 * Copyright (c) 2020 Gothel Software e.K.
 * Copyright (c) 2020 ZAFENA AB
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#ifndef JAU_EUI48_HPP_
#define JAU_EUI48_HPP_

#include <cstring>
#include <string>
#include <memory>
#include <cstdint>
#include <vector>

#include <jau/basic_types.hpp>
#include <jau/packed_attribute.hpp>

namespace jau {

    /** @defgroup NetUtils Network Utilities
     *  Networking types and functionality.
     *
     *  @{
     */

    /**
     * A 48 bit EUI-48 sub-identifier, see EUI48.
     *
     * Stores value in lb_endian::native byte order.
     */
    struct EUI48Sub {
      public:
        /** EUI48 MAC address matching any device, i.e. `0:0:0:0:0:0`. */
        static const EUI48Sub ANY_DEVICE;
        /** EUI48 MAC address matching all device, i.e. `ff:ff:ff:ff:ff:ff`. */
        static const EUI48Sub ALL_DEVICE;
        /** EUI48 MAC address matching local device, i.e. `0:0:0:ff:ff:ff`. */
        static const EUI48Sub LOCAL_DEVICE;

        /**
         * The <= 6 byte EUI48 sub-address.
         */
        uint8_t b[6]; // == sizeof(EUI48)

        /**
         * The actual length in bytes of the EUI48 sub-address, less or equal 6 bytes.
         */
        jau::nsize_t length;

        constexpr EUI48Sub() noexcept : b{0}, length{0} { }

        /**
         * Copy len_ address bytes from given source and byte_order,
         * while converting them to lb_endian::native byte order.
         *
         * @param b_ sub address bytes in lb_endian::native byte order
         * @param len_ length
         * @param byte_order lb_endian::little or lb_endian::big byte order of given sub_address, one may pass lb_endian::native.
         */
        EUI48Sub(const uint8_t * b_, const jau::nsize_t len_, const lb_endian_t byte_order) noexcept;

        /**
         * Fills given EUI48Sub instance via given string representation.
         * <p>
         * Implementation is consistent with EUI48Sub::toString().
         * </p>
         * @param str a string of less or equal of 17 characters representing less or equal of 6 bytes as hexadecimal numbers separated via colon,
         * e.g. `01:02:03:0A:0B:0C`, `01:02:03:0A`, `:`, (empty).
         * @param dest EUI48Sub to set its value
         * @param errmsg error parsing message if returning false
         * @return true if successful, otherwise false
         * @see EUI48Sub::EUI48Sub
         * @see EUI48Sub::toString()
         */
        static bool scanEUI48Sub(const std::string& str, EUI48Sub& dest, std::string& errmsg);

        /**
         * Construct a sub EUI48 via given string representation.
         * <p>
         * Implementation is consistent with EUI48Sub::toString().
         * </p>
         * @param str a string of less or equal of 17 characters representing less or equal of 6 bytes as hexadecimal numbers separated via colon,
         * e.g. `01:02:03:0A:0B:0C`, `01:02:03:0A`, `:`, (empty).
         * @see EUI48Sub::scanEUI48Sub()
         * @see EUI48Sub::toString()
         * @throws jau::IllegalArgumentException if given string doesn't comply with EUI48
         */
        EUI48Sub(const std::string& str);

        constexpr EUI48Sub(const EUI48Sub &o) noexcept = default;
        EUI48Sub(EUI48Sub &&o) noexcept = default;
        constexpr EUI48Sub& operator=(const EUI48Sub &o) noexcept = default;
        EUI48Sub& operator=(EUI48Sub &&o) noexcept = default;

        constexpr std::size_t hash_code() const noexcept {
            // 31 * x == (x << 5) - x
            std::size_t h = length;
            for(jau::nsize_t i=0; i<length; i++) {
                h = ( ( h << 5 ) - h ) + b[i];
            }
            return h;
        }

        /**
         * Method clears the underlying byte array {@link #b} and sets length to zero.
         */
        void clear() {
            b[0] = 0; b[1] = 0; b[2] = 0;
            b[3] = 0; b[4] = 0; b[5] = 0;
            length = 0;
        }

        /**
         * Find index of needle within haystack in the given byte order.
         *
         * The returned index will be adjusted for the desired byte order.
         * - lb_endian::big will return index 0 for the leading byte like the toString() representation from left (MSB) to right (LSB).
         * - lb_endian::little will return index 5 for the leading byte
         *
         * @param haystack_b haystack data
         * @param haystack_length haystack length
         * @param needle_b needle data
         * @param needle_length needle length
         * @param byte_order byte order will adjust the returned index, lb_endian::big is equivalent with toString() representation from left (MSB) to right (LSB).
         * @return index of first element of needle within haystack or -1 if not found. If the needle length is zero, 0 (found) is returned.
         */
        static jau::snsize_t indexOf(const uint8_t haystack_b[], const jau::nsize_t haystack_length,
                                     const uint8_t needle_b[], const jau::nsize_t needle_length,
                                     const lb_endian_t byte_order) noexcept;

        /**
         * Finds the index of given EUI48Sub needle within this instance haystack in the given byte order.
         *
         * The returned index will be adjusted for the desired byte order.
         * - lb_endian::big will return index 0 for the leading byte like the toString() representation from left (MSB) to right (LSB).
         * - lb_endian::little will return index 5 for the leading byte
         *
         * @param needle
         * @param byte_order byte order will adjust the returned index, lb_endian::big is equivalent with toString() representation from left (MSB) to right (LSB).
         * @return index of first element of needle within this instance haystack or -1 if not found. If the needle length is zero, 0 (found) is returned.
         * @see indexOf()
         */
        jau::snsize_t indexOf(const EUI48Sub& needle, const lb_endian_t byte_order) const noexcept {
            return indexOf(b, length, needle.b, needle.length, byte_order);
        }

        /**
         * Returns true, if given EUI48Sub needle is contained in this instance haystack.
         * <p>
         * If the sub is zero, true is returned.
         * </p>
         */
        bool contains(const EUI48Sub& needle) const noexcept {
            return 0 <= indexOf(needle, lb_endian_t::native);
        }

        /**
         * Returns the EUI48 sub-string representation with MSB first (lb_endian::big),
         * less or equal 17 characters representing less or equal 6 bytes as upper case hexadecimal numbers separated via colon,
         * e.g. `01:02:03:0A:0B:0C`, `01:02:03:0A`, `:`, (empty).
         */
        std::string toString() const noexcept;
    };
    inline std::string to_string(const EUI48Sub& a) noexcept { return a.toString(); }

    inline bool operator==(const EUI48Sub& lhs, const EUI48Sub& rhs) noexcept {
        if( &lhs == &rhs ) {
            return true;
        }
        if( lhs.length != rhs.length ) {
            return false;
        }
        return !memcmp(&lhs.b, &rhs.b, lhs.length);
    }

    inline bool operator!=(const EUI48Sub& lhs, const EUI48Sub& rhs) noexcept
    { return !(lhs == rhs); }


    /**
     * A packed 48 bit EUI-48 identifier, formerly known as MAC-48
     * or simply network device MAC address (Media Access Control address).
     *
     * Stores value in lb_endian::native byte order.
     */
    __pack ( struct EUI48 {
        /** EUI48 MAC address matching any device, i.e. `0:0:0:0:0:0`. */
        static const EUI48 ANY_DEVICE;
        /** EUI48 MAC address matching all device, i.e. `ff:ff:ff:ff:ff:ff`. */
        static const EUI48 ALL_DEVICE;
        /** EUI48 MAC address matching local device, i.e. `0:0:0:ff:ff:ff`. */
        static const EUI48 LOCAL_DEVICE;

        /**
         * The 6 byte EUI48 address.
         */
        uint8_t b[6]; // == sizeof(EUI48)

        constexpr EUI48() noexcept : b{0}  { }

        /**
         * Copy address bytes from given source and byte_order,
         * while converting them to lb_endian::native byte order.
         *
         * @param source address bytes
         * @param byte_order lb_endian::little or lb_endian::big byte order of given source, one may pass lb_endian::native.
         */
        EUI48(const uint8_t * source, const lb_endian_t byte_order) noexcept;

        /**
         * Fills given EUI48 instance via given string representation.
         * <p>
         * Implementation is consistent with EUI48::toString().
         * </p>
         * @param str a string of exactly 17 characters representing 6 bytes as hexadecimal numbers separated via colon `01:02:03:0A:0B:0C`.
         * @param dest EUI48 to set its value
         * @param errmsg error parsing message if returning false
         * @return true if successful, otherwise false
         * @see EUI48::EUI48
         * @see EUI48::toString()
         */
        static bool scanEUI48(const std::string& str, EUI48& dest, std::string& errmsg);

        /**
         * Construct instance via given string representation.
         * <p>
         * Implementation is consistent with EUI48::toString().
         * </p>
         * @param str a string of exactly 17 characters representing 6 bytes as hexadecimal numbers separated via colon `01:02:03:0A:0B:0C`.
         * @see EUI48::scanEUI48()
         * @see EUI48::toString()
         * @throws jau::IllegalArgumentException if given string doesn't comply with EUI48
         */
        EUI48(const std::string& str);

        constexpr EUI48(const EUI48 &o) noexcept = default;
        EUI48(EUI48 &&o) noexcept = default;
        constexpr EUI48& operator=(const EUI48 &o) noexcept = default;
        EUI48& operator=(EUI48 &&o) noexcept = default;

        constexpr std::size_t hash_code() const noexcept {
            // 31 * x == (x << 5) - x
            std::size_t h = b[0];
            h = ( ( h << 5 ) - h ) + b[1];
            h = ( ( h << 5 ) - h ) + b[2];
            h = ( ( h << 5 ) - h ) + b[3];
            h = ( ( h << 5 ) - h ) + b[4];
            h = ( ( h << 5 ) - h ) + b[5];
            return h;
        }

        /**
         * Method clears the underlying byte array {@link #b}.
         */
        void clear() {
            b[0] = 0; b[1] = 0; b[2] = 0;
            b[3] = 0; b[4] = 0; b[5] = 0;
        }

        /**
         * Finds the index of given EUI48Sub needle within this instance haystack.
         *
         * The returned index will be adjusted for the desired byte order.
         * - lb_endian::big will return index 0 for the leading byte like the string representation from left (MSB) to right (LSB).
         * - lb_endian::little will return index 5 for the leading byte
         *
         * @param needle
         * @param byte_order byte order will adjust the returned index, lb_endian::big is equivalent to the string representation from left (MSB) to right (LSB).
         * @return index of first element of needle within this instance haystack or -1 if not found. If the needle length is zero, 0 (found) is returned.
         * @see indexOf()
         */
        jau::snsize_t indexOf(const EUI48Sub& needle, const lb_endian_t byte_order) const noexcept {
            return EUI48Sub::indexOf(b, sizeof(b), needle.b, needle.length, byte_order);
        }

        /**
         * Returns true, if given EUI48Sub needle is contained in this instance haystack.
         * <p>
         * If the sub is zero, true is returned.
         * </p>
         */
        bool contains(const EUI48Sub& needle) const noexcept {
            return 0 <= indexOf(needle, lb_endian_t::native);
        }

        /**
         * Returns the EUI48 string representation with MSB first (lb_endian::big),
         * exactly 17 characters representing 6 bytes as upper case hexadecimal numbers separated via colon `01:02:03:0A:0B:0C`.
         * @see EUI48::EUI48()
         */
        std::string toString() const noexcept;

        /**
         * Method transfers all bytes representing this instance into the given
         * destination array at the given position and in the given byte order.
         * <p>
         * Implementation is consistent with {@link #EUI48(byte[], int, ByteOrder)}.
         * </p>
         * @param sink the destination array
         * @param byte_order destination buffer byte order
         * @see #EUI48(byte[], int, ByteOrder)
         */
        jau::nsize_t put(uint8_t * const sink, const lb_endian_t byte_order) const noexcept;
    } );
    inline std::string to_string(const EUI48& a) noexcept { return a.toString(); }

    inline bool operator==(const EUI48& lhs, const EUI48& rhs) noexcept {
        if( &lhs == &rhs ) {
            return true;
        }
        //return !memcmp(&lhs, &rhs, sizeof(EUI48));
        const uint8_t * a = lhs.b;
        const uint8_t * b = rhs.b;
        return a[0] == b[0] &&
               a[1] == b[1] &&
               a[2] == b[2] &&
               a[3] == b[3] &&
               a[4] == b[4] &&
               a[5] == b[5];
    }

    inline bool operator!=(const EUI48& lhs, const EUI48& rhs) noexcept
    { return !(lhs == rhs); }

    constexpr static void bswap_6bytes(uint8_t* sink, const uint8_t* source) noexcept {
        sink[0] = source[5];
        sink[1] = source[4];
        sink[2] = source[3];
        sink[3] = source[2];
        sink[4] = source[1];
        sink[5] = source[0];
    }

    constexpr EUI48 bswap(EUI48 const & source) noexcept {
        EUI48 dest;
        bswap_6bytes(dest.b, source.b);
        return dest;
    }

    // one static_assert is sufficient for whole compilation unit
    static_assert( is_defined_endian(endian_t::native) );
    static_assert( is_little_or_big_endian() );

    constexpr EUI48 be_to_cpu(EUI48 const & n) noexcept {
        if( is_little_endian() ) {
            return bswap(n);
        } else {
            return n;
        }
    }
    constexpr EUI48 cpu_to_be(EUI48 const & h) noexcept {
        if( is_little_endian() ) {
            return bswap(h);
        } else {
            return h;
        }
    }
    constexpr EUI48 le_to_cpu(EUI48 const & l) noexcept {
        if( is_little_endian() ) {
            return l;
        } else {
            return bswap(l);
        }
    }
    constexpr EUI48 cpu_to_le(EUI48 const & h) noexcept {
        if( is_little_endian() ) {
            return h;
        } else {
            return bswap(h);
        }
    }

    /**@}*/

} /* namespace jau */

// injecting specialization of std::hash to namespace std of our types above
namespace std
{
    /** \addtogroup NetUtils
     *
     */

    template<> struct hash<jau::EUI48Sub> {
        std::size_t operator()(jau::EUI48Sub const& a) const noexcept {
            return a.hash_code();
        }
    };

    template<> struct hash<jau::EUI48> {
        std::size_t operator()(jau::EUI48 const& a) const noexcept {
            return a.hash_code();
        }
    };

    /**@}*/
}

#endif /* JAU_EUI48_HPP_ */