2007-04-13 04:55:32 +04:00
|
|
|
/*
|
2013-05-03 23:03:54 +04:00
|
|
|
* Copyright 2007-2013, 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
|
|
|
|
2013-05-03 23:03:54 +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:
|
2010-07-21 15:22:54 +04:00
|
|
|
ProtocolSocket(net_socket* socket);
|
|
|
|
|
|
|
|
status_t Open();
|
|
|
|
|
|
|
|
SocketAddress LocalAddress()
|
|
|
|
{ return SocketAddress(
|
|
|
|
fDomain->address_module,
|
|
|
|
&fSocket->address); }
|
|
|
|
ConstSocketAddress LocalAddress() const
|
|
|
|
{ return ConstSocketAddress(
|
|
|
|
fDomain->address_module,
|
|
|
|
&fSocket->address); }
|
|
|
|
|
|
|
|
SocketAddress PeerAddress()
|
|
|
|
{ return SocketAddress(
|
|
|
|
fDomain->address_module,
|
|
|
|
&fSocket->peer); }
|
|
|
|
ConstSocketAddress PeerAddress() const
|
|
|
|
{ return ConstSocketAddress(
|
|
|
|
fDomain->address_module,
|
|
|
|
&fSocket->peer); }
|
|
|
|
|
|
|
|
net_domain* Domain() const { return fDomain; }
|
|
|
|
net_address_module_info* AddressModule() const
|
|
|
|
{ return fDomain->address_module; }
|
|
|
|
|
|
|
|
net_socket* Socket() const { return fSocket; }
|
2007-04-27 11:09:46 +04:00
|
|
|
|
|
|
|
protected:
|
2010-07-21 15:22:54 +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:
|
2010-07-21 15:22:54 +04:00
|
|
|
DatagramSocket(const char* name,
|
|
|
|
net_socket* socket);
|
|
|
|
virtual ~DatagramSocket();
|
2007-04-13 04:55:32 +04:00
|
|
|
|
2010-07-21 15:22:54 +04:00
|
|
|
status_t InitCheck() const;
|
2007-04-13 04:55:32 +04:00
|
|
|
|
2010-07-21 15:22:54 +04:00
|
|
|
status_t Enqueue(net_buffer* buffer);
|
2010-08-03 16:00:06 +04:00
|
|
|
status_t EnqueueClone(net_buffer* buffer);
|
|
|
|
|
|
|
|
status_t Dequeue(uint32 flags, net_buffer** _buffer);
|
2010-07-21 15:22:54 +04:00
|
|
|
net_buffer* Dequeue(bool clone);
|
2010-07-23 14:12:06 +04:00
|
|
|
status_t BlockingDequeue(bool peek, bigtime_t timeout,
|
2010-07-21 15:22:54 +04:00
|
|
|
net_buffer** _buffer);
|
2007-04-13 04:55:32 +04:00
|
|
|
|
2010-08-03 16:00:06 +04:00
|
|
|
void Clear();
|
2007-04-14 01:33:13 +04:00
|
|
|
|
2010-07-23 14:12:06 +04:00
|
|
|
bool IsEmpty() const { return fBuffers.IsEmpty(); }
|
2010-07-21 15:22:54 +04:00
|
|
|
ssize_t AvailableData() const;
|
2007-04-13 04:55:32 +04:00
|
|
|
|
2010-07-21 15:22:54 +04:00
|
|
|
void WakeAll();
|
2010-07-23 14:12:06 +04:00
|
|
|
void NotifyOne();
|
2007-04-13 08:20:39 +04:00
|
|
|
|
2007-04-13 04:55:32 +04:00
|
|
|
protected:
|
2010-07-23 14:12:06 +04:00
|
|
|
virtual status_t SocketStatus(bool peek) const;
|
2007-04-20 06:13:54 +04:00
|
|
|
|
2010-07-23 14:12:06 +04:00
|
|
|
private:
|
2010-07-21 15:22:54 +04:00
|
|
|
status_t _Enqueue(net_buffer* buffer);
|
2010-07-23 14:12:06 +04:00
|
|
|
net_buffer* _Dequeue(bool peek);
|
2010-07-21 15:22:54 +04:00
|
|
|
void _Clear();
|
2007-04-13 04:55:32 +04:00
|
|
|
|
2010-07-21 15:22:54 +04:00
|
|
|
status_t _Wait(bigtime_t timeout);
|
|
|
|
void _NotifyOneReader(bool notifySocket);
|
2007-04-13 04:55:32 +04:00
|
|
|
|
2010-07-21 15:22:54 +04:00
|
|
|
bigtime_t _SocketTimeout(uint32 flags) const;
|
2007-04-13 04:55:32 +04:00
|
|
|
|
2010-07-23 14:12:06 +04:00
|
|
|
protected:
|
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;
|
|
|
|
|
2010-07-21 15:22:54 +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)
|
2010-07-21 15:22:54 +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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-03 16:00:06 +04:00
|
|
|
DECL_DATAGRAM_SOCKET(inline status_t)::EnqueueClone(net_buffer* _buffer)
|
2007-04-13 04:55:32 +04:00
|
|
|
{
|
|
|
|
AutoLocker _(fLock);
|
|
|
|
|
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);
|
2010-08-03 15:19:22 +04:00
|
|
|
if (status != B_OK)
|
2007-04-13 04:55:32 +04:00
|
|
|
ModuleBundle::Buffer()->free(buffer);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-03 16:00:06 +04:00
|
|
|
DECL_DATAGRAM_SOCKET(inline status_t)::Dequeue(uint32 flags,
|
|
|
|
net_buffer** _buffer)
|
2007-04-13 04:55:32 +04:00
|
|
|
{
|
2010-08-03 16:00:06 +04:00
|
|
|
return BlockingDequeue((flags & MSG_PEEK) != 0, _SocketTimeout(flags),
|
|
|
|
_buffer);
|
2007-04-13 04:55:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-03 16:00:06 +04:00
|
|
|
DECL_DATAGRAM_SOCKET(inline net_buffer*)::Dequeue(bool peek)
|
2007-04-13 04:55:32 +04:00
|
|
|
{
|
2010-08-03 16:00:06 +04:00
|
|
|
AutoLocker _(fLock);
|
|
|
|
return _Dequeue(peek);
|
2007-04-13 04:55:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-23 14:12:06 +04:00
|
|
|
DECL_DATAGRAM_SOCKET(inline status_t)::BlockingDequeue(bool peek,
|
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()) {
|
2010-07-23 14:12:06 +04:00
|
|
|
status_t status = SocketStatus(peek);
|
|
|
|
if (status != B_OK) {
|
|
|
|
if (peek)
|
|
|
|
_NotifyOneReader(false);
|
2007-04-13 04:55:32 +04:00
|
|
|
return status;
|
2010-07-23 14:12:06 +04:00
|
|
|
}
|
2007-04-20 06:13:54 +04:00
|
|
|
|
2010-07-23 14:12:06 +04:00
|
|
|
status = _Wait(timeout);
|
|
|
|
if (status != B_OK)
|
2007-04-20 06:13:54 +04:00
|
|
|
return status;
|
2010-07-23 14:12:06 +04:00
|
|
|
|
2007-04-13 04:55:32 +04:00
|
|
|
waited = true;
|
|
|
|
}
|
|
|
|
|
2010-07-23 14:12:06 +04:00
|
|
|
*_buffer = _Dequeue(peek);
|
|
|
|
if (peek && waited) {
|
|
|
|
// There is a new buffer in the list; but since we are only peeking,
|
|
|
|
// notify the next waiting reader.
|
2007-04-13 04:55:32 +04:00
|
|
|
_NotifyOneReader(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*_buffer == NULL)
|
|
|
|
return B_NO_MEMORY;
|
|
|
|
|
|
|
|
return B_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DECL_DATAGRAM_SOCKET(inline void)::Clear()
|
|
|
|
{
|
|
|
|
AutoLocker _(fLock);
|
|
|
|
_Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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);
|
2010-07-23 14:12:06 +04:00
|
|
|
status_t status = SocketStatus(true);
|
2007-04-20 06:13:54 +04:00
|
|
|
if (status < B_OK)
|
|
|
|
return status;
|
|
|
|
|
2007-04-13 04:55:32 +04:00
|
|
|
return fCurrentBytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-03 16:00:06 +04:00
|
|
|
DECL_DATAGRAM_SOCKET(inline void)::WakeAll()
|
|
|
|
{
|
|
|
|
release_sem_etc(fNotify, 0, B_RELEASE_ALL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DECL_DATAGRAM_SOCKET(inline void)::NotifyOne()
|
|
|
|
{
|
|
|
|
release_sem_etc(fNotify, 1, B_RELEASE_IF_WAITING_ONLY
|
|
|
|
| B_DO_NOT_RESCHEDULE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-23 14:12:06 +04:00
|
|
|
DECL_DATAGRAM_SOCKET(inline status_t)::SocketStatus(bool peek) const
|
2007-04-20 06:13:54 +04:00
|
|
|
{
|
2010-07-23 14:12:06 +04:00
|
|
|
if (peek)
|
|
|
|
return fSocket->error;
|
2013-05-03 23:03:54 +04:00
|
|
|
|
2010-07-23 14:12:06 +04:00
|
|
|
status_t status = fSocket->error;
|
|
|
|
fSocket->error = B_OK;
|
|
|
|
|
|
|
|
return status;
|
2007-04-20 06:13:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-03 16:00:06 +04:00
|
|
|
DECL_DATAGRAM_SOCKET(inline status_t)::_Enqueue(net_buffer* buffer)
|
2007-04-13 04:55:32 +04:00
|
|
|
{
|
2010-08-03 16:00:06 +04:00
|
|
|
if (fSocket->receive.buffer_size > 0
|
|
|
|
&& (fCurrentBytes + buffer->size) > fSocket->receive.buffer_size)
|
|
|
|
return ENOBUFS;
|
2007-04-13 04:55:32 +04:00
|
|
|
|
2010-08-03 16:00:06 +04:00
|
|
|
fBuffers.Add(buffer);
|
|
|
|
fCurrentBytes += buffer->size;
|
|
|
|
|
|
|
|
_NotifyOneReader(true);
|
|
|
|
|
|
|
|
return B_OK;
|
2007-04-13 04:55:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-03 16:00:06 +04:00
|
|
|
DECL_DATAGRAM_SOCKET(inline net_buffer*)::_Dequeue(bool peek)
|
2007-04-13 04:55:32 +04:00
|
|
|
{
|
2010-08-03 16:00:06 +04:00
|
|
|
if (fBuffers.IsEmpty())
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (peek)
|
|
|
|
return ModuleBundle::Buffer()->clone(fBuffers.Head(), false);
|
|
|
|
|
|
|
|
net_buffer* buffer = fBuffers.RemoveHead();
|
|
|
|
fCurrentBytes -= buffer->size;
|
|
|
|
|
|
|
|
return buffer;
|
2007-04-13 04:55:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-03 16:00:06 +04:00
|
|
|
DECL_DATAGRAM_SOCKET(inline void)::_Clear()
|
2010-07-23 14:12:06 +04:00
|
|
|
{
|
2010-08-03 16:00:06 +04:00
|
|
|
BufferList::Iterator it = fBuffers.GetIterator();
|
|
|
|
while (it.HasNext())
|
|
|
|
ModuleBundle::Buffer()->free(it.Next());
|
|
|
|
fCurrentBytes = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DECL_DATAGRAM_SOCKET(inline status_t)::_Wait(bigtime_t timeout)
|
|
|
|
{
|
|
|
|
LockingBase::Unlock(&fLock);
|
|
|
|
status_t status = acquire_sem_etc(fNotify, 1, B_CAN_INTERRUPT
|
2013-05-03 23:03:54 +04:00
|
|
|
| (timeout != 0 ? B_ABSOLUTE_TIMEOUT : B_RELATIVE_TIMEOUT), timeout);
|
2010-08-03 16:00:06 +04:00
|
|
|
LockingBase::Lock(&fLock);
|
|
|
|
|
|
|
|
return status;
|
2010-07-23 14:12:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-13 04:55:32 +04:00
|
|
|
DECL_DATAGRAM_SOCKET(inline void)::_NotifyOneReader(bool notifySocket)
|
|
|
|
{
|
|
|
|
release_sem_etc(fNotify, 1, B_RELEASE_IF_WAITING_ONLY
|
|
|
|
| B_DO_NOT_RESCHEDULE);
|
|
|
|
|
2010-07-23 14:12:06 +04:00
|
|
|
if (notifySocket) {
|
2007-04-13 04:55:32 +04:00
|
|
|
ModuleBundle::Stack()->notify_socket(fSocket, B_SELECT_READ,
|
|
|
|
fCurrentBytes);
|
2010-07-23 14:12:06 +04:00
|
|
|
}
|
2007-04-13 04:55:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DECL_DATAGRAM_SOCKET(inline bigtime_t)::_SocketTimeout(uint32 flags) const
|
|
|
|
{
|
2013-05-04 13:25:02 +04:00
|
|
|
if (ModuleBundle::Stack()->is_restarted_syscall())
|
|
|
|
return ModuleBundle::Stack()->restore_syscall_restart_timeout();
|
2007-04-13 04:55:32 +04:00
|
|
|
|
2013-05-04 13:25:02 +04:00
|
|
|
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();
|
|
|
|
|
2013-05-04 13:25:02 +04:00
|
|
|
ModuleBundle::Stack()->store_syscall_restart_timeout(timeout);
|
2007-04-13 04:55:32 +04:00
|
|
|
return timeout;
|
|
|
|
}
|
|
|
|
|
2010-07-21 15:22:54 +04:00
|
|
|
|
2009-06-25 20:01:11 +04:00
|
|
|
#endif // PROTOCOL_UTILITIES_H
|