2007-04-13 04:55:32 +04:00
|
|
|
/*
|
2009-06-25 20:01:11 +04:00
|
|
|
* Copyright 2007-2009, Haiku, Inc. All Rights Reserved.
|
2007-04-13 04:55:32 +04:00
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Hugo Santos, hugosantos@gmail.com
|
|
|
|
*/
|
|
|
|
#ifndef PROTOCOL_UTILITIES_H
|
|
|
|
#define PROTOCOL_UTILITIES_H
|
|
|
|
|
2007-10-22 00:10:43 +04:00
|
|
|
|
2007-04-13 04:55:32 +04:00
|
|
|
#include <lock.h>
|
2007-10-22 00:10:43 +04:00
|
|
|
#include <Select.h>
|
2007-04-13 04:55:32 +04:00
|
|
|
#include <util/AutoLock.h>
|
|
|
|
#include <util/DoublyLinkedList.h>
|
|
|
|
|
2007-10-22 00:10:43 +04:00
|
|
|
#include <AddressUtilities.h>
|
2007-04-13 04:55:32 +04:00
|
|
|
#include <net_buffer.h>
|
2007-04-27 11:09:46 +04:00
|
|
|
#include <net_protocol.h>
|
2007-04-13 04:55:32 +04:00
|
|
|
#include <net_socket.h>
|
|
|
|
#include <net_stack.h>
|
|
|
|
|
2007-04-27 11:09:46 +04:00
|
|
|
|
2008-05-29 06:08:23 +04:00
|
|
|
class MutexLocking {
|
2007-04-13 04:55:32 +04:00
|
|
|
public:
|
2008-05-29 06:08:23 +04:00
|
|
|
typedef mutex Type;
|
|
|
|
typedef MutexLocker AutoLocker;
|
|
|
|
|
2009-06-25 20:01:11 +04:00
|
|
|
static status_t Init(mutex* lock, const char* name)
|
2008-05-29 06:08:23 +04:00
|
|
|
{ mutex_init_etc(lock, name, MUTEX_FLAG_CLONE_NAME); return B_OK; }
|
2009-06-25 20:01:11 +04:00
|
|
|
static void Destroy(mutex* lock) { mutex_destroy(lock); }
|
|
|
|
static status_t Lock(mutex* lock) { return mutex_lock(lock); }
|
|
|
|
static status_t Unlock(mutex* lock) { mutex_unlock(lock); return B_OK; }
|
2007-04-13 04:55:32 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-06-25 20:01:11 +04:00
|
|
|
extern net_buffer_module_info* gBufferModule;
|
|
|
|
extern net_stack_module_info* gStackModule;
|
2007-04-13 04:55:32 +04:00
|
|
|
|
2007-04-13 07:06:36 +04:00
|
|
|
class NetModuleBundleGetter {
|
2007-04-13 04:55:32 +04:00
|
|
|
public:
|
2009-06-25 20:01:11 +04:00
|
|
|
static net_stack_module_info* Stack() { return gStackModule; }
|
|
|
|
static net_buffer_module_info* Buffer() { return gBufferModule; }
|
2007-04-13 04:55:32 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2007-04-27 11:09:46 +04:00
|
|
|
class ProtocolSocket {
|
|
|
|
public:
|
2009-06-25 20:01:11 +04:00
|
|
|
ProtocolSocket(net_socket* socket);
|
2007-04-27 11:09:46 +04:00
|
|
|
|
|
|
|
status_t Open();
|
|
|
|
|
|
|
|
SocketAddress LocalAddress()
|
|
|
|
{ return SocketAddress(fDomain->address_module, &fSocket->address); }
|
|
|
|
ConstSocketAddress LocalAddress() const
|
2009-06-25 20:01:11 +04:00
|
|
|
{ return ConstSocketAddress(fDomain->address_module,
|
|
|
|
&fSocket->address); }
|
2007-04-27 11:09:46 +04:00
|
|
|
|
|
|
|
SocketAddress PeerAddress()
|
|
|
|
{ return SocketAddress(fDomain->address_module, &fSocket->peer); }
|
|
|
|
ConstSocketAddress PeerAddress() const
|
|
|
|
{ return ConstSocketAddress(fDomain->address_module, &fSocket->peer); }
|
|
|
|
|
2009-06-25 20:01:11 +04:00
|
|
|
net_domain* Domain() const { return fDomain; }
|
|
|
|
net_address_module_info* AddressModule() const
|
2007-04-27 11:09:46 +04:00
|
|
|
{ return fDomain->address_module; }
|
|
|
|
|
|
|
|
protected:
|
2009-06-25 20:01:11 +04:00
|
|
|
net_socket* fSocket;
|
|
|
|
net_domain* fDomain;
|
2007-04-27 11:09:46 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-06-25 20:01:11 +04:00
|
|
|
inline
|
|
|
|
ProtocolSocket::ProtocolSocket(net_socket* socket)
|
|
|
|
:
|
|
|
|
fSocket(socket),
|
|
|
|
fDomain(NULL)
|
|
|
|
{
|
|
|
|
}
|
2007-04-27 11:09:46 +04:00
|
|
|
|
|
|
|
|
|
|
|
inline status_t
|
|
|
|
ProtocolSocket::Open()
|
|
|
|
{
|
|
|
|
fDomain = fSocket->first_protocol->module->get_domain(
|
|
|
|
fSocket->first_protocol);
|
|
|
|
|
|
|
|
if (fDomain == NULL || fDomain->address_module == NULL)
|
|
|
|
return EAFNOSUPPORT;
|
|
|
|
|
|
|
|
return B_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-29 06:08:23 +04:00
|
|
|
template<typename LockingBase = MutexLocking,
|
2007-04-13 07:06:36 +04:00
|
|
|
typename ModuleBundle = NetModuleBundleGetter>
|
2007-04-27 11:09:46 +04:00
|
|
|
class DatagramSocket : public ProtocolSocket {
|
2007-04-13 04:55:32 +04:00
|
|
|
public:
|
2009-06-25 20:01:11 +04:00
|
|
|
DatagramSocket(const char* name, net_socket* socket);
|
|
|
|
virtual ~DatagramSocket();
|
2007-04-13 04:55:32 +04:00
|
|
|
|
2009-06-25 20:01:11 +04:00
|
|
|
status_t InitCheck() const;
|
2007-04-13 04:55:32 +04:00
|
|
|
|
2009-06-25 20:01:11 +04:00
|
|
|
status_t Enqueue(net_buffer* buffer);
|
|
|
|
net_buffer* Dequeue(bool clone);
|
|
|
|
status_t BlockingDequeue(bool clone, bigtime_t timeout,
|
|
|
|
net_buffer** _buffer);
|
|
|
|
void Clear();
|
2007-04-13 04:55:32 +04:00
|
|
|
|
2009-06-25 20:01:11 +04:00
|
|
|
status_t SocketEnqueue(net_buffer* buffer);
|
|
|
|
status_t SocketDequeue(uint32 flags, net_buffer** _buffer);
|
2007-04-14 01:33:13 +04:00
|
|
|
|
2009-06-25 20:01:11 +04:00
|
|
|
ssize_t AvailableData() const;
|
2007-04-13 04:55:32 +04:00
|
|
|
|
2009-06-25 20:01:11 +04:00
|
|
|
void WakeAll();
|
2007-04-13 04:55:32 +04:00
|
|
|
|
2009-06-25 20:01:11 +04:00
|
|
|
net_socket* Socket() const { return fSocket; }
|
2007-04-13 08:20:39 +04:00
|
|
|
|
2007-04-13 04:55:32 +04:00
|
|
|
protected:
|
2009-06-25 20:01:11 +04:00
|
|
|
virtual status_t _SocketStatus() const;
|
2007-04-20 06:13:54 +04:00
|
|
|
|
2009-06-25 20:01:11 +04:00
|
|
|
status_t _Enqueue(net_buffer* buffer);
|
|
|
|
status_t _SocketEnqueue(net_buffer* buffer);
|
|
|
|
net_buffer* _Dequeue(bool clone);
|
|
|
|
void _Clear();
|
2007-04-13 04:55:32 +04:00
|
|
|
|
2009-06-25 20:01:11 +04:00
|
|
|
status_t _Wait(bigtime_t timeout);
|
|
|
|
void _NotifyOneReader(bool notifySocket);
|
2007-04-13 04:55:32 +04:00
|
|
|
|
2009-06-25 20:01:11 +04:00
|
|
|
bool _IsEmpty() const { return fBuffers.IsEmpty(); }
|
|
|
|
bigtime_t _SocketTimeout(uint32 flags) const;
|
2007-04-13 04:55:32 +04:00
|
|
|
|
|
|
|
typedef typename LockingBase::Type LockType;
|
|
|
|
typedef typename LockingBase::AutoLocker AutoLocker;
|
|
|
|
typedef DoublyLinkedListCLink<net_buffer> NetBufferLink;
|
|
|
|
typedef DoublyLinkedList<net_buffer, NetBufferLink> BufferList;
|
|
|
|
|
2009-06-25 20:01:11 +04:00
|
|
|
sem_id fNotify;
|
|
|
|
BufferList fBuffers;
|
|
|
|
size_t fCurrentBytes;
|
|
|
|
mutable LockType fLock;
|
2007-04-13 04:55:32 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#define DECL_DATAGRAM_SOCKET(args) \
|
|
|
|
template<typename LockingBase, typename ModuleBundle> args \
|
|
|
|
DatagramSocket<LockingBase, ModuleBundle>
|
|
|
|
|
|
|
|
|
2009-06-25 20:01:11 +04:00
|
|
|
DECL_DATAGRAM_SOCKET(inline)::DatagramSocket(const char* name,
|
|
|
|
net_socket* socket)
|
2007-04-27 11:09:46 +04:00
|
|
|
: ProtocolSocket(socket), fCurrentBytes(0)
|
2007-04-13 04:55:32 +04:00
|
|
|
{
|
2007-04-13 07:06:36 +04:00
|
|
|
status_t status = LockingBase::Init(&fLock, name);
|
2009-06-25 20:01:11 +04:00
|
|
|
if (status != B_OK)
|
2007-04-13 07:06:36 +04:00
|
|
|
fNotify = status;
|
|
|
|
else
|
2007-04-13 04:55:32 +04:00
|
|
|
fNotify = create_sem(0, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DECL_DATAGRAM_SOCKET(inline)::~DatagramSocket()
|
|
|
|
{
|
|
|
|
_Clear();
|
2007-04-13 07:06:36 +04:00
|
|
|
delete_sem(fNotify);
|
|
|
|
LockingBase::Destroy(&fLock);
|
2007-04-13 04:55:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DECL_DATAGRAM_SOCKET(inline status_t)::InitCheck() const
|
|
|
|
{
|
2009-06-25 20:01:11 +04:00
|
|
|
return fNotify >= 0 ? B_OK : fNotify;
|
2007-04-13 04:55:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-25 20:01:11 +04:00
|
|
|
DECL_DATAGRAM_SOCKET(inline status_t)::Enqueue(net_buffer* buffer)
|
2007-04-13 04:55:32 +04:00
|
|
|
{
|
|
|
|
AutoLocker _(fLock);
|
|
|
|
return _Enqueue(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-25 20:01:11 +04:00
|
|
|
DECL_DATAGRAM_SOCKET(inline status_t)::_Enqueue(net_buffer* buffer)
|
2007-04-13 04:55:32 +04:00
|
|
|
{
|
|
|
|
if (fSocket->receive.buffer_size > 0
|
|
|
|
&& (fCurrentBytes + buffer->size) > fSocket->receive.buffer_size)
|
|
|
|
return ENOBUFS;
|
|
|
|
|
|
|
|
fBuffers.Add(buffer);
|
|
|
|
fCurrentBytes += buffer->size;
|
|
|
|
|
|
|
|
_NotifyOneReader(true);
|
|
|
|
|
|
|
|
return B_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-25 20:01:11 +04:00
|
|
|
DECL_DATAGRAM_SOCKET(inline status_t)::SocketEnqueue(net_buffer* _buffer)
|
2007-04-13 04:55:32 +04:00
|
|
|
{
|
|
|
|
AutoLocker _(fLock);
|
2007-04-14 01:33:13 +04:00
|
|
|
return _SocketEnqueue(_buffer);
|
2007-04-13 04:55:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-25 20:01:11 +04:00
|
|
|
DECL_DATAGRAM_SOCKET(inline status_t)::_SocketEnqueue(net_buffer* _buffer)
|
2007-04-13 04:55:32 +04:00
|
|
|
{
|
2009-06-25 20:01:11 +04:00
|
|
|
net_buffer* buffer = ModuleBundle::Buffer()->clone(_buffer, false);
|
2007-04-13 04:55:32 +04:00
|
|
|
if (buffer == NULL)
|
|
|
|
return B_NO_MEMORY;
|
|
|
|
|
|
|
|
status_t status = _Enqueue(buffer);
|
|
|
|
if (status < B_OK)
|
|
|
|
ModuleBundle::Buffer()->free(buffer);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-25 20:01:11 +04:00
|
|
|
DECL_DATAGRAM_SOCKET(inline net_buffer*)::Dequeue(bool clone)
|
2007-04-13 04:55:32 +04:00
|
|
|
{
|
|
|
|
AutoLocker _(fLock);
|
|
|
|
return _Dequeue(clone);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-25 20:01:11 +04:00
|
|
|
DECL_DATAGRAM_SOCKET(inline net_buffer*)::_Dequeue(bool clone)
|
2007-04-13 04:55:32 +04:00
|
|
|
{
|
|
|
|
if (fBuffers.IsEmpty())
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (clone)
|
|
|
|
return ModuleBundle::Buffer()->clone(fBuffers.Head(), false);
|
|
|
|
|
2009-06-25 20:01:11 +04:00
|
|
|
net_buffer* buffer = fBuffers.RemoveHead();
|
2007-04-13 04:55:32 +04:00
|
|
|
fCurrentBytes -= buffer->size;
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DECL_DATAGRAM_SOCKET(inline status_t)::BlockingDequeue(bool clone,
|
2009-06-25 20:01:11 +04:00
|
|
|
bigtime_t timeout, net_buffer** _buffer)
|
2007-04-13 04:55:32 +04:00
|
|
|
{
|
|
|
|
AutoLocker _(fLock);
|
|
|
|
|
|
|
|
bool waited = false;
|
|
|
|
while (fBuffers.IsEmpty()) {
|
2007-04-20 06:13:54 +04:00
|
|
|
status_t status = _SocketStatus();
|
2007-04-13 04:55:32 +04:00
|
|
|
if (status < B_OK)
|
|
|
|
return status;
|
2007-04-20 06:13:54 +04:00
|
|
|
|
|
|
|
if ((status = _Wait(timeout)) < B_OK)
|
|
|
|
return status;
|
2007-04-13 04:55:32 +04:00
|
|
|
waited = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
*_buffer = _Dequeue(clone);
|
|
|
|
if (clone && waited) {
|
|
|
|
// we were signalled there was a new buffer in the
|
|
|
|
// list; but since we are cloning, notify the next
|
|
|
|
// waiting reader.
|
|
|
|
_NotifyOneReader(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*_buffer == NULL)
|
|
|
|
return B_NO_MEMORY;
|
|
|
|
|
|
|
|
return B_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DECL_DATAGRAM_SOCKET(inline status_t)::SocketDequeue(uint32 flags,
|
2009-06-25 20:01:11 +04:00
|
|
|
net_buffer** _buffer)
|
2007-04-13 04:55:32 +04:00
|
|
|
{
|
2009-06-25 20:01:11 +04:00
|
|
|
return BlockingDequeue((flags & MSG_PEEK) != 0, _SocketTimeout(flags),
|
|
|
|
_buffer);
|
2007-04-13 04:55:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DECL_DATAGRAM_SOCKET(inline void)::Clear()
|
|
|
|
{
|
|
|
|
AutoLocker _(fLock);
|
|
|
|
_Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DECL_DATAGRAM_SOCKET(inline void)::_Clear()
|
|
|
|
{
|
|
|
|
BufferList::Iterator it = fBuffers.GetIterator();
|
|
|
|
while (it.HasNext())
|
|
|
|
ModuleBundle::Buffer()->free(it.Next());
|
|
|
|
fCurrentBytes = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-20 06:13:54 +04:00
|
|
|
DECL_DATAGRAM_SOCKET(inline ssize_t)::AvailableData() const
|
2007-04-13 04:55:32 +04:00
|
|
|
{
|
|
|
|
AutoLocker _(fLock);
|
2007-04-20 06:13:54 +04:00
|
|
|
status_t status = _SocketStatus();
|
|
|
|
if (status < B_OK)
|
|
|
|
return status;
|
|
|
|
|
2007-04-13 04:55:32 +04:00
|
|
|
return fCurrentBytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-20 06:13:54 +04:00
|
|
|
DECL_DATAGRAM_SOCKET(inline status_t)::_SocketStatus() const
|
|
|
|
{
|
|
|
|
return B_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-13 04:55:32 +04:00
|
|
|
DECL_DATAGRAM_SOCKET(inline status_t)::_Wait(bigtime_t timeout)
|
|
|
|
{
|
|
|
|
LockingBase::Unlock(&fLock);
|
|
|
|
status_t status = acquire_sem_etc(fNotify, 1, B_CAN_INTERRUPT
|
|
|
|
| B_ABSOLUTE_TIMEOUT, timeout);
|
|
|
|
LockingBase::Lock(&fLock);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DECL_DATAGRAM_SOCKET(inline void)::WakeAll()
|
|
|
|
{
|
|
|
|
release_sem_etc(fNotify, 0, B_RELEASE_ALL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DECL_DATAGRAM_SOCKET(inline void)::_NotifyOneReader(bool notifySocket)
|
|
|
|
{
|
|
|
|
release_sem_etc(fNotify, 1, B_RELEASE_IF_WAITING_ONLY
|
|
|
|
| B_DO_NOT_RESCHEDULE);
|
|
|
|
|
|
|
|
if (notifySocket)
|
|
|
|
ModuleBundle::Stack()->notify_socket(fSocket, B_SELECT_READ,
|
|
|
|
fCurrentBytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DECL_DATAGRAM_SOCKET(inline bigtime_t)::_SocketTimeout(uint32 flags) const
|
|
|
|
{
|
|
|
|
bigtime_t timeout = fSocket->receive.timeout;
|
|
|
|
|
2009-06-25 20:01:11 +04:00
|
|
|
if ((flags & MSG_DONTWAIT) != 0)
|
2007-04-13 04:55:32 +04:00
|
|
|
timeout = 0;
|
|
|
|
else if (timeout != 0 && timeout != B_INFINITE_TIMEOUT)
|
|
|
|
timeout += system_time();
|
|
|
|
|
2008-02-17 19:25:07 +03:00
|
|
|
if (ModuleBundle::Stack()->is_restarted_syscall())
|
|
|
|
timeout = ModuleBundle::Stack()->restore_syscall_restart_timeout();
|
|
|
|
else
|
|
|
|
ModuleBundle::Stack()->store_syscall_restart_timeout(timeout);
|
|
|
|
|
2007-04-13 04:55:32 +04:00
|
|
|
return timeout;
|
|
|
|
}
|
|
|
|
|
2009-06-25 20:01:11 +04:00
|
|
|
#endif // PROTOCOL_UTILITIES_H
|