2 * Copyright 2006-2010, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
5 #ifndef NET_UTILITIES_H
6 #define NET_UTILITIES_H
9 #include <netinet/in.h>
12 #include <net_buffer.h>
13 #include <net_datalink.h>
19 BufferHelper(net_buffer
* _buffer
, net_buffer_module_info
* _bufferModule
)
22 bufferModule(_bufferModule
)
27 net_buffer_module_info
* bufferModule
;
36 inline Checksum
& operator<<(uint8 value
)
41 fSum
+= (uint16
)value
<< 8;
46 inline Checksum
& operator<<(uint16 value
)
52 inline Checksum
& operator<<(uint32 value
)
54 fSum
+= (value
& 0xffff) + (value
>> 16);
58 inline Checksum
& operator<<(const BufferHelper
& bufferHelper
)
60 net_buffer
* buffer
= bufferHelper
.buffer
;
61 fSum
+= bufferHelper
.bufferModule
->checksum(buffer
, 0, buffer
->size
,
66 inline operator uint16()
69 fSum
= (fSum
& 0xffff) + (fSum
>> 16);
71 uint16 result
= (uint16
)fSum
;
76 static uint16
PseudoHeader(net_address_module_info
* addressModule
,
77 net_buffer_module_info
* bufferModule
, net_buffer
* buffer
,
86 Checksum::PseudoHeader(net_address_module_info
* addressModule
,
87 net_buffer_module_info
* bufferModule
, net_buffer
* buffer
, uint16 protocol
)
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
);
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
{
103 AddressString(net_domain
* domain
, const sockaddr
* address
,
104 bool printPort
= false)
108 domain
->address_module
->print_address(address
, &fBuffer
, printPort
);
111 AddressString(net_domain
* domain
, const sockaddr
& address
,
112 bool printPort
= false)
116 domain
->address_module
->print_address(&address
, &fBuffer
, printPort
);
119 AddressString(net_address_module_info
* address_module
,
120 const sockaddr
* address
, bool printPort
= false)
124 address_module
->print_address(address
, &fBuffer
, printPort
);
132 const char* Data() const
142 #endif // NET_UTILITIES_H