]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
2 | /* | |
3 | * This file is open source software, licensed to you under the terms | |
4 | * of the Apache License, Version 2.0 (the "License"). See the NOTICE file | |
5 | * distributed with this work for additional information regarding copyright | |
6 | * ownership. You may not use this file except in compliance with the License. | |
7 | * | |
8 | * You may obtain a copy of the License at | |
9 | * | |
10 | * http://www.apache.org/licenses/LICENSE-2.0 | |
11 | * | |
12 | * Unless required by applicable law or agreed to in writing, | |
13 | * software distributed under the License is distributed on an | |
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
15 | * KIND, either express or implied. See the License for the | |
16 | * specific language governing permissions and limitations | |
17 | * under the License. | |
18 | */ | |
19 | /* | |
20 | * Copyright (C) 2014 Cloudius Systems, Ltd. | |
21 | */ | |
7c673cae FG |
22 | |
23 | #ifndef CEPH_MSG_CHECKSUM_H_ | |
24 | #define CEPH_MSG_CHECKSUM_H_ | |
25 | ||
26 | #include <cstdint> | |
27 | #include <cstddef> | |
28 | #include <arpa/inet.h> | |
29 | ||
30 | #include "Packet.h" | |
31 | ||
32 | uint16_t ip_checksum(const void* data, size_t len); | |
33 | ||
34 | struct checksummer { | |
35 | __int128 csum = 0; | |
36 | bool odd = false; | |
37 | void sum(const char* data, size_t len); | |
38 | void sum(const Packet& p); | |
39 | void sum(uint8_t data) { | |
40 | if (!odd) { | |
41 | csum += data << 8; | |
42 | } else { | |
43 | csum += data; | |
44 | } | |
45 | odd = !odd; | |
46 | } | |
47 | void sum(uint16_t data) { | |
48 | if (odd) { | |
49 | sum(uint8_t(data >> 8)); | |
50 | sum(uint8_t(data)); | |
51 | } else { | |
52 | csum += data; | |
53 | } | |
54 | } | |
55 | void sum(uint32_t data) { | |
56 | if (odd) { | |
57 | sum(uint16_t(data)); | |
58 | sum(uint16_t(data >> 16)); | |
59 | } else { | |
60 | csum += data; | |
61 | } | |
62 | } | |
63 | void sum_many() {} | |
64 | template <typename T0, typename... T> | |
65 | void sum_many(T0 data, T... rest) { | |
66 | sum(data); | |
67 | sum_many(rest...); | |
68 | } | |
69 | uint16_t get() const; | |
70 | }; | |
71 | ||
72 | #endif /* CEPH_MSG_CHECKSUM_H_ */ |