hmbdc
simplify-high-performance-messaging-programming
Misc.h
1 #include "hmbdc/Copyright.hpp"
2 #pragma once
3 
4 #include <net/ethernet.h>
5 #include <netinet/in.h>
6 #include <netinet/ip.h>
7 #include <netinet/udp.h>
8 
9 namespace hmbdc { namespace comm { namespace eth {
10 inline
11 uint32_t
12 checksum(const void *data, uint16_t len, uint32_t sum) {
13  auto addr = (const uint8_t *)data;
14  uint32_t i;
15 
16  /* Checksum all the pairs of bytes first... */
17  for (i = 0; i < (len & ~1U); i += 2) {
18  sum += (u_int16_t)ntohs(*((u_int16_t *)(addr + i)));
19  if (sum > 0xFFFF)
20  sum -= 0xFFFF;
21  }
22  /*
23  * If there's a single byte left over, checksum it, too.
24  * Network byte order is big-endian, so the remaining byte is
25  * the high byte.
26  */
27  if (i < len) {
28  sum += addr[i] << 8;
29  if (sum > 0xFFFF)
30  sum -= 0xFFFF;
31  }
32  return sum;
33 }
34 
35 inline
36 uint16_t
37 cksum_add(uint16_t sum, uint16_t a) {
38  uint16_t res;
39 
40  res = sum + a;
41  return (res + (res < a));
42 }
43 
44 inline
45 uint16_t
46 wrapsum(uint32_t sum) {
47  sum = ~sum & 0xFFFF;
48  return (htons(sum));
49 }
50 
51 struct virt_header {
52  uint8_t fields[12];
53 } __attribute__((__packed__));
54 
55 struct pkt {
56  struct virt_header vh;
57  struct ether_header eh;
58  struct {
59  struct ip ip;
60  struct udphdr udp;
61  uint8_t body[1];
62  } ipv4;
63 } __attribute__((__packed__));
64 
65 static_assert(sizeof(pkt) == 58u, "wierd that size is not 55?");
66 
67 template <size_t N>
68 struct pkt_n {
69  struct virt_header vh;
70  struct ether_header eh;
71  struct {
72  struct ip ip;
73  struct udphdr udp;
74  uint8_t body[N];
75  } ipv4;
76 
77  operator pkt&() {
78  return reinterpret_cast<pkt&>(*this);
79  }
80 
81 } __attribute__((__packed__));
82 
83 }}}
Definition: Misc.h:55
Definition: Misc.h:51
Definition: Misc.h:68
Definition: Client.hpp:11