2006-08-08 17:07:07 +04:00
|
|
|
/*
|
2010-08-03 15:18:52 +04:00
|
|
|
* Copyright 2006-2010, Haiku, Inc. All Rights Reserved.
|
2006-08-08 17:07:07 +04:00
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*/
|
|
|
|
#ifndef NET_UTILITIES_H
|
|
|
|
#define NET_UTILITIES_H
|
|
|
|
|
|
|
|
|
2010-08-03 15:18:52 +04:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2006-08-08 17:07:07 +04:00
|
|
|
#include <net_buffer.h>
|
|
|
|
#include <net_datalink.h>
|
|
|
|
|
|
|
|
|
|
|
|
class Checksum {
|
2010-08-03 15:18:52 +04:00
|
|
|
public:
|
|
|
|
struct BufferHelper {
|
|
|
|
BufferHelper(net_buffer* _buffer, net_buffer_module_info* _bufferModule)
|
|
|
|
:
|
|
|
|
buffer(_buffer),
|
|
|
|
bufferModule(_bufferModule)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
net_buffer* buffer;
|
|
|
|
net_buffer_module_info* bufferModule;
|
|
|
|
};
|
|
|
|
|
|
|
|
Checksum()
|
|
|
|
:
|
|
|
|
fSum(0)
|
|
|
|
{
|
|
|
|
}
|
2006-08-08 17:07:07 +04:00
|
|
|
|
2010-08-03 15:18:52 +04:00
|
|
|
inline Checksum& operator<<(uint8 value)
|
|
|
|
{
|
2006-08-08 17:07:07 +04:00
|
|
|
#if B_HOST_IS_LENDIAN
|
2010-08-03 15:18:52 +04:00
|
|
|
fSum += value;
|
2006-08-08 17:07:07 +04:00
|
|
|
#else
|
2010-08-03 15:18:52 +04:00
|
|
|
fSum += (uint16)value << 8;
|
2006-08-08 17:07:07 +04:00
|
|
|
#endif
|
2010-08-03 15:18:52 +04:00
|
|
|
return *this;
|
|
|
|
}
|
2006-08-08 17:07:07 +04:00
|
|
|
|
2010-08-03 15:18:52 +04:00
|
|
|
inline Checksum& operator<<(uint16 value)
|
|
|
|
{
|
|
|
|
fSum += value;
|
|
|
|
return *this;
|
|
|
|
}
|
2006-08-08 17:07:07 +04:00
|
|
|
|
2010-08-03 15:18:52 +04:00
|
|
|
inline Checksum& operator<<(uint32 value)
|
|
|
|
{
|
|
|
|
fSum += (value & 0xffff) + (value >> 16);
|
|
|
|
return *this;
|
|
|
|
}
|
2006-08-08 17:07:07 +04:00
|
|
|
|
2010-08-03 15:18:52 +04:00
|
|
|
inline Checksum& operator<<(const BufferHelper& bufferHelper)
|
|
|
|
{
|
|
|
|
net_buffer* buffer = bufferHelper.buffer;
|
|
|
|
fSum += bufferHelper.bufferModule->checksum(buffer, 0, buffer->size,
|
|
|
|
false);
|
|
|
|
return *this;
|
|
|
|
}
|
2006-08-08 17:07:07 +04:00
|
|
|
|
2010-08-03 15:18:52 +04:00
|
|
|
inline operator uint16()
|
|
|
|
{
|
|
|
|
while (fSum >> 16) {
|
|
|
|
fSum = (fSum & 0xffff) + (fSum >> 16);
|
|
|
|
}
|
|
|
|
uint16 result = (uint16)fSum;
|
|
|
|
result ^= 0xFFFF;
|
|
|
|
return result;
|
2006-08-08 17:07:07 +04:00
|
|
|
}
|
2010-08-03 15:18:52 +04:00
|
|
|
|
|
|
|
static uint16 PseudoHeader(net_address_module_info* addressModule,
|
|
|
|
net_buffer_module_info* bufferModule, net_buffer* buffer,
|
|
|
|
uint16 protocol);
|
|
|
|
|
|
|
|
private:
|
|
|
|
uint32 fSum;
|
|
|
|
};
|
2006-08-08 17:07:07 +04:00
|
|
|
|
|
|
|
|
2007-04-16 03:18:15 +04:00
|
|
|
inline uint16
|
2010-08-03 15:18:52 +04:00
|
|
|
Checksum::PseudoHeader(net_address_module_info* addressModule,
|
|
|
|
net_buffer_module_info* bufferModule, net_buffer* buffer, uint16 protocol)
|
2007-04-16 03:18:15 +04:00
|
|
|
{
|
|
|
|
Checksum checksum;
|
2007-05-23 07:48:23 +04:00
|
|
|
addressModule->checksum_address(&checksum, buffer->source);
|
|
|
|
addressModule->checksum_address(&checksum, buffer->destination);
|
2007-04-16 03:18:15 +04:00
|
|
|
checksum << (uint16)htons(protocol) << (uint16)htons(buffer->size)
|
|
|
|
<< Checksum::BufferHelper(buffer, bufferModule);
|
|
|
|
return checksum;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-03 15:18:52 +04:00
|
|
|
/*! Helper class that prints an address (and optionally a port) into a buffer
|
|
|
|
that is automatically freed at end of scope.
|
|
|
|
*/
|
2006-08-08 17:07:07 +04:00
|
|
|
class AddressString {
|
|
|
|
public:
|
2010-08-03 15:18:52 +04:00
|
|
|
AddressString(net_domain* domain, const sockaddr* address,
|
2006-08-08 17:07:07 +04:00
|
|
|
bool printPort = false)
|
2010-08-03 15:18:52 +04:00
|
|
|
:
|
|
|
|
fBuffer(NULL)
|
2006-08-08 17:07:07 +04:00
|
|
|
{
|
|
|
|
domain->address_module->print_address(address, &fBuffer, printPort);
|
|
|
|
}
|
|
|
|
|
2010-08-03 15:18:52 +04:00
|
|
|
AddressString(net_domain* domain, const sockaddr& address,
|
2006-08-08 17:07:07 +04:00
|
|
|
bool printPort = false)
|
2010-08-03 15:18:52 +04:00
|
|
|
:
|
|
|
|
fBuffer(NULL)
|
2006-08-08 17:07:07 +04:00
|
|
|
{
|
|
|
|
domain->address_module->print_address(&address, &fBuffer, printPort);
|
|
|
|
}
|
|
|
|
|
2010-08-03 15:18:52 +04:00
|
|
|
AddressString(net_address_module_info* address_module,
|
|
|
|
const sockaddr* address, bool printPort = false)
|
|
|
|
:
|
|
|
|
fBuffer(NULL)
|
2006-08-08 17:07:07 +04:00
|
|
|
{
|
2007-04-28 13:37:26 +04:00
|
|
|
address_module->print_address(address, &fBuffer, printPort);
|
2006-08-08 17:07:07 +04:00
|
|
|
}
|
2007-04-28 13:37:26 +04:00
|
|
|
|
|
|
|
~AddressString()
|
|
|
|
{
|
2010-08-03 15:18:52 +04:00
|
|
|
free(fBuffer);
|
2007-04-28 13:37:26 +04:00
|
|
|
}
|
|
|
|
|
2010-08-03 15:18:52 +04:00
|
|
|
const char* Data() const
|
2006-08-08 17:07:07 +04:00
|
|
|
{
|
|
|
|
return fBuffer;
|
|
|
|
}
|
2007-04-28 13:37:26 +04:00
|
|
|
|
2006-08-08 17:07:07 +04:00
|
|
|
private:
|
2010-08-03 15:18:52 +04:00
|
|
|
char* fBuffer;
|
2006-08-08 17:07:07 +04:00
|
|
|
};
|
|
|
|
|
2010-08-03 15:18:52 +04:00
|
|
|
|
2006-08-08 17:07:07 +04:00
|
|
|
#endif // NET_UTILITIES_H
|