btrfs: [] on the end of a struct field is a variable length array.
[haiku.git] / headers / private / net / NetUtilities.h
blob62ab1077edbc2723c27e3f8528c5c1b469f5cddb
1 /*
2 * Copyright 2006-2010, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
4 */
5 #ifndef NET_UTILITIES_H
6 #define NET_UTILITIES_H
9 #include <netinet/in.h>
10 #include <stdlib.h>
12 #include <net_buffer.h>
13 #include <net_datalink.h>
16 class Checksum {
17 public:
18 struct BufferHelper {
19 BufferHelper(net_buffer* _buffer, net_buffer_module_info* _bufferModule)
21 buffer(_buffer),
22 bufferModule(_bufferModule)
26 net_buffer* buffer;
27 net_buffer_module_info* bufferModule;
30 Checksum()
32 fSum(0)
36 inline Checksum& operator<<(uint8 value)
38 #if B_HOST_IS_LENDIAN
39 fSum += value;
40 #else
41 fSum += (uint16)value << 8;
42 #endif
43 return *this;
46 inline Checksum& operator<<(uint16 value)
48 fSum += value;
49 return *this;
52 inline Checksum& operator<<(uint32 value)
54 fSum += (value & 0xffff) + (value >> 16);
55 return *this;
58 inline Checksum& operator<<(const BufferHelper& bufferHelper)
60 net_buffer* buffer = bufferHelper.buffer;
61 fSum += bufferHelper.bufferModule->checksum(buffer, 0, buffer->size,
62 false);
63 return *this;
66 inline operator uint16()
68 while (fSum >> 16) {
69 fSum = (fSum & 0xffff) + (fSum >> 16);
71 uint16 result = (uint16)fSum;
72 result ^= 0xFFFF;
73 return result;
76 static uint16 PseudoHeader(net_address_module_info* addressModule,
77 net_buffer_module_info* bufferModule, net_buffer* buffer,
78 uint16 protocol);
80 private:
81 uint32 fSum;
85 inline uint16
86 Checksum::PseudoHeader(net_address_module_info* addressModule,
87 net_buffer_module_info* bufferModule, net_buffer* buffer, uint16 protocol)
89 Checksum checksum;
90 addressModule->checksum_address(&checksum, buffer->source);
91 addressModule->checksum_address(&checksum, buffer->destination);
92 checksum << (uint16)htons(protocol) << (uint16)htons(buffer->size)
93 << Checksum::BufferHelper(buffer, bufferModule);
94 return checksum;
98 /*! Helper class that prints an address (and optionally a port) into a buffer
99 that is automatically freed at end of scope.
101 class AddressString {
102 public:
103 AddressString(net_domain* domain, const sockaddr* address,
104 bool printPort = false)
106 fBuffer(NULL)
108 domain->address_module->print_address(address, &fBuffer, printPort);
111 AddressString(net_domain* domain, const sockaddr& address,
112 bool printPort = false)
114 fBuffer(NULL)
116 domain->address_module->print_address(&address, &fBuffer, printPort);
119 AddressString(net_address_module_info* address_module,
120 const sockaddr* address, bool printPort = false)
122 fBuffer(NULL)
124 address_module->print_address(address, &fBuffer, printPort);
127 ~AddressString()
129 free(fBuffer);
132 const char* Data() const
134 return fBuffer;
137 private:
138 char* fBuffer;
142 #endif // NET_UTILITIES_H