]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
1 | /* |
2 | * This file is open source software, licensed to you under the terms | |
3 | * of the Apache License, Version 2.0 (the "License"). See the NOTICE file | |
4 | * distributed with this work for additional information regarding copyright | |
5 | * ownership. You may not use this file except in compliance with the License. | |
6 | * | |
7 | * You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, | |
12 | * software distributed under the License is distributed on an | |
13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
14 | * KIND, either express or implied. See the License for the | |
15 | * specific language governing permissions and limitations | |
16 | * under the License. | |
17 | */ | |
18 | /* | |
19 | * Copyright (C) 2014 Cloudius Systems, Ltd. | |
20 | */ | |
21 | ||
22 | #pragma once | |
23 | ||
24 | #include <array> | |
9f95a23c TL |
25 | #include <assert.h> |
26 | #include <algorithm> | |
11fdf7f2 | 27 | #include <seastar/net/byteorder.hh> |
11fdf7f2 TL |
28 | |
29 | namespace seastar { | |
30 | ||
31 | namespace net { | |
32 | ||
33 | struct ethernet_address { | |
34 | ethernet_address() | |
35 | : mac{} {} | |
36 | ||
37 | ethernet_address(const uint8_t *eaddr) { | |
38 | std::copy(eaddr, eaddr + 6, mac.begin()); | |
39 | } | |
40 | ||
41 | ethernet_address(std::initializer_list<uint8_t> eaddr) { | |
42 | assert(eaddr.size() == mac.size()); | |
43 | std::copy(eaddr.begin(), eaddr.end(), mac.begin()); | |
44 | } | |
45 | ||
46 | std::array<uint8_t, 6> mac; | |
47 | ||
48 | template <typename Adjuster> | |
49 | void adjust_endianness(Adjuster a) {} | |
50 | ||
51 | static ethernet_address read(const char* p) { | |
52 | ethernet_address ea; | |
53 | std::copy_n(p, size(), reinterpret_cast<char*>(ea.mac.data()));\ | |
54 | return ea; | |
55 | } | |
56 | static ethernet_address consume(const char*& p) { | |
57 | auto ea = read(p); | |
58 | p += size(); | |
59 | return ea; | |
60 | } | |
61 | void write(char* p) const { | |
62 | std::copy_n(reinterpret_cast<const char*>(mac.data()), size(), p); | |
63 | } | |
64 | void produce(char*& p) const { | |
65 | write(p); | |
66 | p += size(); | |
67 | } | |
68 | static constexpr size_t size() { | |
69 | return 6; | |
70 | } | |
71 | } __attribute__((packed)); | |
72 | ||
73 | std::ostream& operator<<(std::ostream& os, ethernet_address ea); | |
74 | ||
75 | struct ethernet { | |
76 | using address = ethernet_address; | |
77 | static address broadcast_address() { | |
78 | return {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; | |
79 | } | |
80 | static constexpr uint16_t arp_hardware_type() { return 1; } | |
81 | }; | |
82 | ||
83 | struct eth_hdr { | |
84 | ethernet_address dst_mac; | |
85 | ethernet_address src_mac; | |
86 | packed<uint16_t> eth_proto; | |
87 | template <typename Adjuster> | |
88 | auto adjust_endianness(Adjuster a) { | |
89 | return a(eth_proto); | |
90 | } | |
91 | } __attribute__((packed)); | |
92 | ||
93 | ethernet_address parse_ethernet_address(std::string addr); | |
94 | } | |
95 | ||
96 | } |