2007-04-25 21:41:01 +04:00
|
|
|
/*
|
2008-05-31 16:04:09 +04:00
|
|
|
* Copyright 2007-2008, Haiku, Inc. All Rights Reserved.
|
2007-04-25 21:41:01 +04:00
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Hugo Santos, hugosantos@gmail.com
|
|
|
|
*/
|
|
|
|
|
2008-05-31 16:04:09 +04:00
|
|
|
#ifndef NET_ADDRESS_UTILITIES_H
|
|
|
|
#define NET_ADDRESS_UTILITIES_H
|
2007-04-25 21:41:01 +04:00
|
|
|
|
2007-04-28 13:37:26 +04:00
|
|
|
|
2008-05-31 16:04:09 +04:00
|
|
|
#include <NetUtilities.h>
|
2007-04-25 21:41:01 +04:00
|
|
|
#include <net_datalink.h>
|
|
|
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
2008-05-31 16:04:09 +04:00
|
|
|
|
2007-04-25 21:41:01 +04:00
|
|
|
class SocketAddress {
|
|
|
|
public:
|
|
|
|
SocketAddress(net_address_module_info *module, sockaddr *address)
|
2007-04-27 11:09:46 +04:00
|
|
|
: fModule(module), fAddress(address) {}
|
2007-04-25 21:41:01 +04:00
|
|
|
|
|
|
|
SocketAddress(net_address_module_info *module, sockaddr_storage *address)
|
2007-04-27 11:09:46 +04:00
|
|
|
: fModule(module), fAddress((sockaddr *)address) {}
|
2007-04-25 21:41:01 +04:00
|
|
|
|
|
|
|
SocketAddress(const SocketAddress &address)
|
2007-04-27 11:09:46 +04:00
|
|
|
: fModule(address.fModule), fAddress(address.fAddress) {}
|
2007-04-25 21:41:01 +04:00
|
|
|
|
|
|
|
void SetAddressTo(sockaddr *address)
|
|
|
|
{
|
|
|
|
fAddress = address;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsEmpty(bool checkPort) const
|
|
|
|
{
|
|
|
|
return fModule->is_empty_address(fAddress, checkPort);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32 HashPair(const sockaddr *second) const
|
|
|
|
{
|
|
|
|
return fModule->hash_address_pair(fAddress, second);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EqualTo(const sockaddr *address, bool checkPort = false) const
|
|
|
|
{
|
|
|
|
if (checkPort)
|
|
|
|
return fModule->equal_addresses_and_ports(fAddress, address);
|
|
|
|
else
|
|
|
|
return fModule->equal_addresses(fAddress, address);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EqualTo(const SocketAddress &second, bool checkPort = false) const
|
|
|
|
{
|
|
|
|
return EqualTo(second.fAddress, checkPort);
|
|
|
|
}
|
|
|
|
|
2007-04-27 11:10:11 +04:00
|
|
|
bool EqualPorts(const sockaddr *second) const
|
|
|
|
{
|
|
|
|
return fModule->equal_ports(fAddress, second);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MatchMasked(const sockaddr *address, const sockaddr *mask) const
|
|
|
|
{
|
|
|
|
return fModule->equal_masked_addresses(fAddress, address, mask);
|
|
|
|
}
|
|
|
|
|
2007-04-27 11:20:43 +04:00
|
|
|
uint16 Port() const
|
2007-04-25 21:41:01 +04:00
|
|
|
{
|
|
|
|
return fModule->get_port(fAddress);
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t SetTo(const sockaddr *from)
|
|
|
|
{
|
|
|
|
return fModule->set_to(fAddress, from);
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t SetTo(const sockaddr_storage *from)
|
|
|
|
{
|
|
|
|
return SetTo((sockaddr *)from);
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t CopyTo(sockaddr *to) const
|
|
|
|
{
|
|
|
|
return fModule->set_to(to, fAddress);
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t CopyTo(sockaddr_storage *to) const
|
|
|
|
{
|
|
|
|
return CopyTo((sockaddr *)to);
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t SetPort(uint16 port)
|
|
|
|
{
|
|
|
|
return fModule->set_port(fAddress, port);
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t SetToEmpty()
|
|
|
|
{
|
|
|
|
return fModule->set_to_empty_address(fAddress);
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t UpdateTo(const sockaddr *from)
|
|
|
|
{
|
|
|
|
return fModule->update_to(fAddress, from);
|
|
|
|
}
|
|
|
|
|
2007-04-28 13:37:26 +04:00
|
|
|
AddressString AsString(bool printPort = false) const
|
|
|
|
{
|
|
|
|
return AddressString(fModule, fAddress, printPort);
|
|
|
|
}
|
|
|
|
|
2007-04-29 23:03:06 +04:00
|
|
|
const char *AsString(char *buffer, size_t bufferSize,
|
|
|
|
bool printPort = false) const {
|
|
|
|
fModule->print_address_buffer(fAddress, buffer, bufferSize, printPort);
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2007-04-25 21:41:01 +04:00
|
|
|
const sockaddr *operator *() const { return fAddress; }
|
|
|
|
sockaddr *operator *() { return fAddress; }
|
|
|
|
|
|
|
|
net_address_module_info *Module() const { return fModule; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
net_address_module_info *fModule;
|
|
|
|
sockaddr *fAddress;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ConstSocketAddress {
|
|
|
|
public:
|
|
|
|
ConstSocketAddress(net_address_module_info *module,
|
|
|
|
const sockaddr *address)
|
2007-04-27 11:09:46 +04:00
|
|
|
: fModule(module), fAddress(address) {}
|
2007-04-25 21:41:01 +04:00
|
|
|
|
|
|
|
ConstSocketAddress(net_address_module_info *module,
|
|
|
|
const sockaddr_storage *address)
|
2007-04-27 11:09:46 +04:00
|
|
|
: fModule(module), fAddress((sockaddr *)address) {}
|
2007-04-25 21:41:01 +04:00
|
|
|
|
|
|
|
ConstSocketAddress(const ConstSocketAddress &address)
|
2007-04-27 11:09:46 +04:00
|
|
|
: fModule(address.fModule), fAddress(address.fAddress) {}
|
2007-04-25 21:41:01 +04:00
|
|
|
|
|
|
|
ConstSocketAddress(const SocketAddress &address)
|
2007-04-27 11:09:46 +04:00
|
|
|
: fModule(address.Module()), fAddress(*address) {}
|
2007-04-25 21:41:01 +04:00
|
|
|
|
|
|
|
void SetAddressTo(const sockaddr *address)
|
|
|
|
{
|
|
|
|
fAddress = address;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsEmpty(bool checkPort) const
|
|
|
|
{
|
|
|
|
return fModule->is_empty_address(fAddress, checkPort);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32 HashPair(const sockaddr *second) const
|
|
|
|
{
|
|
|
|
return fModule->hash_address_pair(fAddress, second);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EqualTo(const sockaddr *address, bool checkPort = false) const
|
|
|
|
{
|
|
|
|
if (checkPort)
|
|
|
|
return fModule->equal_addresses_and_ports(fAddress, address);
|
2008-05-31 16:04:09 +04:00
|
|
|
|
|
|
|
return fModule->equal_addresses(fAddress, address);
|
2007-04-25 21:41:01 +04:00
|
|
|
}
|
|
|
|
|
2007-04-27 11:10:11 +04:00
|
|
|
bool EqualPorts(const sockaddr *second) const
|
|
|
|
{
|
|
|
|
return fModule->equal_ports(fAddress, second);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MatchMasked(const sockaddr *address, const sockaddr *mask) const
|
|
|
|
{
|
|
|
|
return fModule->equal_masked_addresses(fAddress, address, mask);
|
|
|
|
}
|
|
|
|
|
2007-04-27 11:20:43 +04:00
|
|
|
uint16 Port() const
|
2007-04-25 21:41:01 +04:00
|
|
|
{
|
|
|
|
return fModule->get_port(fAddress);
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t CopyTo(sockaddr *to) const
|
|
|
|
{
|
|
|
|
return fModule->set_to(to, fAddress);
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t CopyTo(sockaddr_storage *to) const
|
|
|
|
{
|
|
|
|
return CopyTo((sockaddr *)to);
|
|
|
|
}
|
|
|
|
|
2007-04-28 13:37:26 +04:00
|
|
|
AddressString AsString(bool printPort = false) const
|
|
|
|
{
|
|
|
|
return AddressString(fModule, fAddress, printPort);
|
|
|
|
}
|
|
|
|
|
2007-04-29 23:03:06 +04:00
|
|
|
const char *AsString(char *buffer, size_t bufferSize,
|
2008-05-31 16:04:09 +04:00
|
|
|
bool printPort = false) const
|
|
|
|
{
|
2007-04-29 23:03:06 +04:00
|
|
|
fModule->print_address_buffer(fAddress, buffer, bufferSize, printPort);
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2007-04-25 21:41:01 +04:00
|
|
|
const sockaddr *operator *() const { return fAddress; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
net_address_module_info *fModule;
|
|
|
|
const sockaddr *fAddress;
|
|
|
|
};
|
|
|
|
|
|
|
|
class SocketAddressStorage : public SocketAddress {
|
|
|
|
public:
|
|
|
|
SocketAddressStorage(net_address_module_info *module)
|
|
|
|
: SocketAddress(module, &fAddressStorage)
|
|
|
|
{}
|
|
|
|
|
|
|
|
private:
|
|
|
|
sockaddr_storage fAddressStorage;
|
|
|
|
};
|
|
|
|
|
2008-05-31 16:04:09 +04:00
|
|
|
#endif // NET_ADDRESS_UTILITIES_H
|