fixed multiple TCP issues.
- Moved FIN handling to after Segment Text handling, it matches the RFC and fixes a possible issue with a user missing some final segment data when receiving a FIN. - Fixed the FIN handling to better terminate connections (no more senseless chatter in the end with RSTs etc). - When Bind()ing, set the socket address so a call to getsockname() gets the allocated port correctly. - After reaching the ESTABLISHED state, try to wake all pending threads (still needs work). - Corrected the maximum amount of time we wait in SendData() to respect socket->send.timeout. - Now ReadData() behaves correctly in all possible states. - There was a missing handling of MSG_DONTWAIT, fixed. - Better internal handling of PSH. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@20624 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
@@ -355,7 +355,10 @@ BufferQueue::Available(tcp_sequence sequence) const
|
|||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
BufferQueue::SetPushPointer(tcp_sequence sequence)
|
BufferQueue::SetPushPointer()
|
||||||
{
|
{
|
||||||
fPushPointer = sequence;
|
if (fList.IsEmpty())
|
||||||
|
fPushPointer = 0;
|
||||||
|
else
|
||||||
|
fPushPointer = fList.Tail()->sequence + fList.Tail()->size;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,8 +33,15 @@ class BufferQueue {
|
|||||||
size_t Available() const { return fContiguousBytes; }
|
size_t Available() const { return fContiguousBytes; }
|
||||||
size_t Available(tcp_sequence sequence) const;
|
size_t Available(tcp_sequence sequence) const;
|
||||||
|
|
||||||
size_t PushedData() const { return fPushPointer > fFirstSequence ? fPushPointer - fFirstSequence : 0; }
|
size_t PushedData() const
|
||||||
void SetPushPointer(tcp_sequence sequence);
|
{
|
||||||
|
// we must check if fPushPointer is not 0 here due to
|
||||||
|
// `tcp_sequence's special handling of >
|
||||||
|
return fPushPointer != 0 && fPushPointer > fFirstSequence ?
|
||||||
|
fPushPointer - fFirstSequence : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SetPushPointer();
|
||||||
|
|
||||||
size_t Used() const { return fNumBytes; }
|
size_t Used() const { return fNumBytes; }
|
||||||
size_t Free() const { return fMaxBytes - fNumBytes; }
|
size_t Free() const { return fMaxBytes - fNumBytes; }
|
||||||
|
|||||||
@@ -132,6 +132,8 @@ status_t
|
|||||||
EndpointManager::SetConnection(TCPEndpoint *endpoint,
|
EndpointManager::SetConnection(TCPEndpoint *endpoint,
|
||||||
const sockaddr *local, const sockaddr *peer, const sockaddr *interfaceLocal)
|
const sockaddr *local, const sockaddr *peer, const sockaddr *interfaceLocal)
|
||||||
{
|
{
|
||||||
|
TRACE(("EndpointManager::SetConnection(%p)\n", endpoint));
|
||||||
|
|
||||||
RecursiveLocker locker(&fLock);
|
RecursiveLocker locker(&fLock);
|
||||||
sockaddr localBuffer;
|
sockaddr localBuffer;
|
||||||
|
|
||||||
@@ -212,6 +214,8 @@ EndpointManager::_LookupEndpoint(uint16 port)
|
|||||||
status_t
|
status_t
|
||||||
EndpointManager::Bind(TCPEndpoint *endpoint, sockaddr *address)
|
EndpointManager::Bind(TCPEndpoint *endpoint, sockaddr *address)
|
||||||
{
|
{
|
||||||
|
TRACE(("EndpointManager::Bind(%p, %s)\n", endpoint, AddressString(gDomain, address, true).Data()));
|
||||||
|
|
||||||
if (gAddressModule->is_empty_address(address, true))
|
if (gAddressModule->is_empty_address(address, true))
|
||||||
return B_BAD_VALUE;
|
return B_BAD_VALUE;
|
||||||
|
|
||||||
@@ -255,6 +259,8 @@ EndpointManager::Bind(TCPEndpoint *endpoint, sockaddr *address)
|
|||||||
} else
|
} else
|
||||||
hash_insert(fEndpointHash, endpoint);
|
hash_insert(fEndpointHash, endpoint);
|
||||||
|
|
||||||
|
gAddressModule->set_to((sockaddr *)&endpoint->socket->address, address);
|
||||||
|
|
||||||
endpoint->fEndpointNextWithSamePort = NULL;
|
endpoint->fEndpointNextWithSamePort = NULL;
|
||||||
hash_insert(fConnectionHash, endpoint);
|
hash_insert(fConnectionHash, endpoint);
|
||||||
|
|
||||||
@@ -265,6 +271,8 @@ EndpointManager::Bind(TCPEndpoint *endpoint, sockaddr *address)
|
|||||||
status_t
|
status_t
|
||||||
EndpointManager::BindToEphemeral(TCPEndpoint *endpoint, sockaddr *address)
|
EndpointManager::BindToEphemeral(TCPEndpoint *endpoint, sockaddr *address)
|
||||||
{
|
{
|
||||||
|
TRACE(("EndpointManager::BindToEphemeral(%p)\n", endpoint));
|
||||||
|
|
||||||
RecursiveLocker locker(&fLock);
|
RecursiveLocker locker(&fLock);
|
||||||
|
|
||||||
uint32 max = kFirstEphemeralPort + 65536;
|
uint32 max = kFirstEphemeralPort + 65536;
|
||||||
@@ -284,7 +292,10 @@ EndpointManager::BindToEphemeral(TCPEndpoint *endpoint, sockaddr *address)
|
|||||||
TCPEndpoint *other = _LookupEndpoint(port);
|
TCPEndpoint *other = _LookupEndpoint(port);
|
||||||
if (other == NULL) {
|
if (other == NULL) {
|
||||||
// found a port
|
// found a port
|
||||||
|
gAddressModule->set_to_empty_address((sockaddr *)&endpoint->socket->address);
|
||||||
gAddressModule->set_port((sockaddr *)&endpoint->socket->address, port);
|
gAddressModule->set_port((sockaddr *)&endpoint->socket->address, port);
|
||||||
|
TRACE((" EndpointManager::BindToEphemeral(%p) -> %s\n", endpoint,
|
||||||
|
AddressString(gDomain, (sockaddr *)&endpoint->socket->address, true).Data()));
|
||||||
endpoint->fEndpointNextWithSamePort = NULL;
|
endpoint->fEndpointNextWithSamePort = NULL;
|
||||||
hash_insert(fEndpointHash, endpoint);
|
hash_insert(fEndpointHash, endpoint);
|
||||||
hash_insert(fConnectionHash, endpoint);
|
hash_insert(fConnectionHash, endpoint);
|
||||||
@@ -308,9 +319,6 @@ EndpointManager::Unbind(TCPEndpoint *endpoint)
|
|||||||
|
|
||||||
RecursiveLocker locker(&fLock);
|
RecursiveLocker locker(&fLock);
|
||||||
|
|
||||||
if (!endpoint->IsBound())
|
|
||||||
return B_BAD_VALUE;
|
|
||||||
|
|
||||||
TCPEndpoint *other = _LookupEndpoint(gAddressModule->get_port((sockaddr *)&endpoint->socket->address));
|
TCPEndpoint *other = _LookupEndpoint(gAddressModule->get_port((sockaddr *)&endpoint->socket->address));
|
||||||
if (other != endpoint) {
|
if (other != endpoint) {
|
||||||
// remove endpoint from the list of endpoints with the same port
|
// remove endpoint from the list of endpoints with the same port
|
||||||
|
|||||||
@@ -59,10 +59,30 @@ enum {
|
|||||||
FLAG_OPTION_WINDOW_SHIFT = 0x01,
|
FLAG_OPTION_WINDOW_SHIFT = 0x01,
|
||||||
FLAG_OPTION_TIMESTAMP = 0x02,
|
FLAG_OPTION_TIMESTAMP = 0x02,
|
||||||
FLAG_NO_RECEIVE = 0x04,
|
FLAG_NO_RECEIVE = 0x04,
|
||||||
FLAG_NO_SEND = 0x08,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
static status_t
|
||||||
|
wait_on_locker(RecursiveLocker &locker, sem_id sem, bigtime_t timeout)
|
||||||
|
{
|
||||||
|
locker.Unlock();
|
||||||
|
status_t status = acquire_sem_etc(sem, 1, B_ABSOLUTE_TIMEOUT |
|
||||||
|
B_CAN_INTERRUPT, timeout);
|
||||||
|
locker.Lock();
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static inline bigtime_t
|
||||||
|
absolute_timeout(bigtime_t timeout)
|
||||||
|
{
|
||||||
|
if (timeout == 0 || timeout == B_INFINITE_TIMEOUT)
|
||||||
|
return timeout;
|
||||||
|
|
||||||
|
return timeout + system_time();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
TCPEndpoint::TCPEndpoint(net_socket *socket)
|
TCPEndpoint::TCPEndpoint(net_socket *socket)
|
||||||
:
|
:
|
||||||
fOptions(0),
|
fOptions(0),
|
||||||
@@ -167,27 +187,23 @@ TCPEndpoint::Close()
|
|||||||
if (status != B_OK)
|
if (status != B_OK)
|
||||||
return status;
|
return status;
|
||||||
|
|
||||||
TRACE("Close(): Entering state %d", fState);
|
TRACE("Close(): Entering state %s", name_for_state(fState));
|
||||||
|
|
||||||
if (socket->options & SO_LINGER) {
|
if (socket->options & SO_LINGER) {
|
||||||
TRACE("Close(): Lingering for %i secs", socket->linger);
|
TRACE("Close(): Lingering for %i secs", socket->linger);
|
||||||
|
|
||||||
bigtime_t maximum = system_time() + socket->linger * 1000000LL;
|
bigtime_t maximum = absolute_timeout(socket->linger * 1000000LL);
|
||||||
|
|
||||||
while (fSendQueue.Used() > 0) {
|
while (fSendQueue.Used() > 0) {
|
||||||
lock.Unlock();
|
status = wait_on_locker(lock, fSendLock, maximum);
|
||||||
status = acquire_sem_etc(fSendLock, 1, B_CAN_INTERRUPT
|
if (status == B_TIMED_OUT)
|
||||||
| B_ABSOLUTE_TIMEOUT, maximum);
|
break;
|
||||||
if (status == B_TIMED_OUT) {
|
else if (status < B_OK)
|
||||||
TRACE("Close(): Lingering made me wait, but not all data was sent.");
|
|
||||||
return B_OK;
|
|
||||||
} else if (status < B_OK)
|
|
||||||
return status;
|
return status;
|
||||||
|
|
||||||
lock.Lock();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TRACE("Close(): Waited for all data to be sent with success");
|
TRACE("Close(): after waiting, the SendQ was left with %lu bytes.",
|
||||||
|
fSendQueue.Used());
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: do i need to wait until fState returns to CLOSED?
|
// TODO: do i need to wait until fState returns to CLOSED?
|
||||||
@@ -225,7 +241,7 @@ TCPEndpoint::Connect(const struct sockaddr *address)
|
|||||||
|
|
||||||
RecursiveLocker locker(&fLock);
|
RecursiveLocker locker(&fLock);
|
||||||
|
|
||||||
TRACE(" Connect(): in state %d", fState);
|
TRACE(" Connect(): in state %s", name_for_state(fState));
|
||||||
|
|
||||||
// Can only call connect() from CLOSED or LISTEN states
|
// Can only call connect() from CLOSED or LISTEN states
|
||||||
// otherwise endpoint is considered already connected
|
// otherwise endpoint is considered already connected
|
||||||
@@ -319,6 +335,9 @@ TCPEndpoint::Accept(struct net_socket **_acceptedSocket)
|
|||||||
status_t
|
status_t
|
||||||
TCPEndpoint::Bind(sockaddr *address)
|
TCPEndpoint::Bind(sockaddr *address)
|
||||||
{
|
{
|
||||||
|
if (address == NULL)
|
||||||
|
return B_BAD_VALUE;
|
||||||
|
|
||||||
TRACE("Bind() on address %s",
|
TRACE("Bind() on address %s",
|
||||||
AddressString(gDomain, address, true).Data());
|
AddressString(gDomain, address, true).Data());
|
||||||
|
|
||||||
@@ -376,9 +395,8 @@ TCPEndpoint::Shutdown(int direction)
|
|||||||
|
|
||||||
RecursiveLocker lock(fLock);
|
RecursiveLocker lock(fLock);
|
||||||
|
|
||||||
if (direction == SHUT_RD || direction == SHUT_RDWR) {
|
if (direction == SHUT_RD || direction == SHUT_RDWR)
|
||||||
fFlags |= FLAG_NO_RECEIVE;
|
fFlags |= FLAG_NO_RECEIVE;
|
||||||
}
|
|
||||||
|
|
||||||
if (direction == SHUT_WR || direction == SHUT_RDWR)
|
if (direction == SHUT_WR || direction == SHUT_RDWR)
|
||||||
_ShutdownEgress(false);
|
_ShutdownEgress(false);
|
||||||
@@ -398,14 +416,25 @@ TCPEndpoint::SendData(net_buffer *buffer)
|
|||||||
|
|
||||||
RecursiveLocker lock(fLock);
|
RecursiveLocker lock(fLock);
|
||||||
|
|
||||||
if (fFlags & FLAG_NO_SEND) {
|
if (fState == CLOSED)
|
||||||
|
return ENOTCONN;
|
||||||
|
else if (fState == LISTEN) {
|
||||||
|
// TODO change socket from passive to active.
|
||||||
|
return EOPNOTSUPP;
|
||||||
|
} else if (fState == FINISH_SENT || fState == FINISH_ACKNOWLEDGED
|
||||||
|
|| fState == CLOSING || fState == WAIT_FOR_FINISH_ACKNOWLEDGE
|
||||||
|
|| fState == TIME_WAIT) {
|
||||||
// TODO: send SIGPIPE signal to app?
|
// TODO: send SIGPIPE signal to app?
|
||||||
return EPIPE;
|
return EPIPE;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t bytesLeft = buffer->size;
|
size_t bytesLeft = buffer->size;
|
||||||
|
|
||||||
|
bigtime_t timeout = absolute_timeout(socket->send.timeout);
|
||||||
|
|
||||||
do {
|
do {
|
||||||
|
// TODO we should not segment here, but on TX.
|
||||||
|
|
||||||
net_buffer *chunk;
|
net_buffer *chunk;
|
||||||
if (bytesLeft > socket->send.buffer_size) {
|
if (bytesLeft > socket->send.buffer_size) {
|
||||||
// divide the buffer in multiple of the maximum segment size
|
// divide the buffer in multiple of the maximum segment size
|
||||||
@@ -421,14 +450,9 @@ TCPEndpoint::SendData(net_buffer *buffer)
|
|||||||
chunk = buffer;
|
chunk = buffer;
|
||||||
|
|
||||||
while (fSendQueue.Free() < chunk->size) {
|
while (fSendQueue.Free() < chunk->size) {
|
||||||
lock.Unlock();
|
status_t status = wait_on_locker(lock, fSendLock, timeout);
|
||||||
|
|
||||||
status_t status = acquire_sem_etc(fSendLock, 1,
|
|
||||||
B_RELATIVE_TIMEOUT | B_CAN_INTERRUPT, socket->send.timeout);
|
|
||||||
if (status < B_OK)
|
if (status < B_OK)
|
||||||
return status;
|
return status;
|
||||||
|
|
||||||
lock.Lock();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: check state!
|
// TODO: check state!
|
||||||
@@ -441,7 +465,10 @@ TCPEndpoint::SendData(net_buffer *buffer)
|
|||||||
size_t chunkSize = chunk->size;
|
size_t chunkSize = chunk->size;
|
||||||
fSendQueue.Add(chunk);
|
fSendQueue.Add(chunk);
|
||||||
|
|
||||||
status_t status = _SendQueued();
|
status_t status = B_OK;
|
||||||
|
|
||||||
|
if (fState == ESTABLISHED || fState == FINISH_RECEIVED)
|
||||||
|
status = _SendQueued();
|
||||||
|
|
||||||
if (buffer != chunk) {
|
if (buffer != chunk) {
|
||||||
// as long as we didn't eat the buffer, we can still return an error code
|
// as long as we didn't eat the buffer, we can still return an error code
|
||||||
@@ -474,31 +501,25 @@ TCPEndpoint::ReadData(size_t numBytes, uint32 flags, net_buffer** _buffer)
|
|||||||
|
|
||||||
RecursiveLocker locker(fLock);
|
RecursiveLocker locker(fLock);
|
||||||
|
|
||||||
|
*_buffer = NULL;
|
||||||
|
|
||||||
|
if (fState == CLOSED)
|
||||||
|
return ENOTCONN;
|
||||||
|
|
||||||
|
bigtime_t timeout = absolute_timeout(socket->receive.timeout);
|
||||||
|
|
||||||
if (fState == SYNCHRONIZE_SENT || fState == SYNCHRONIZE_RECEIVED) {
|
if (fState == SYNCHRONIZE_SENT || fState == SYNCHRONIZE_RECEIVED) {
|
||||||
// we need to wait until the connection becomes established
|
|
||||||
if (flags & MSG_DONTWAIT)
|
if (flags & MSG_DONTWAIT)
|
||||||
return B_WOULD_BLOCK;
|
return B_WOULD_BLOCK;
|
||||||
|
|
||||||
locker.Unlock();
|
while (fState != ESTABLISHED) {
|
||||||
|
// we need to wait until the connection becomes established
|
||||||
status_t status = acquire_sem_etc(fSendLock, 1,
|
status_t status = wait_on_locker(locker, fSendLock, timeout);
|
||||||
B_RELATIVE_TIMEOUT | B_CAN_INTERRUPT, socket->receive.timeout);
|
if (status < B_OK)
|
||||||
if (status < B_OK)
|
return status;
|
||||||
return status;
|
}
|
||||||
|
|
||||||
locker.Lock();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((fFlags & FLAG_NO_RECEIVE) != 0 && fReceiveQueue.Available() == 0) {
|
|
||||||
// there is no data left in the queue, and we can't receive anything,
|
|
||||||
// anymore
|
|
||||||
*_buffer = NULL;
|
|
||||||
return B_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
// read data out of buffer
|
|
||||||
// TODO: add support for urgent data (MSG_OOB)
|
|
||||||
|
|
||||||
size_t dataNeeded = socket->receive.low_water_mark;
|
size_t dataNeeded = socket->receive.low_water_mark;
|
||||||
|
|
||||||
// When MSG_WAITALL is set then the function should block
|
// When MSG_WAITALL is set then the function should block
|
||||||
@@ -506,49 +527,65 @@ TCPEndpoint::ReadData(size_t numBytes, uint32 flags, net_buffer** _buffer)
|
|||||||
if (flags & MSG_WAITALL)
|
if (flags & MSG_WAITALL)
|
||||||
dataNeeded = numBytes;
|
dataNeeded = numBytes;
|
||||||
|
|
||||||
bigtime_t timeout = socket->receive.timeout;
|
// TODO: add support for urgent data (MSG_OOB)
|
||||||
if (timeout != B_INFINITE_TIMEOUT)
|
|
||||||
timeout += system_time();
|
|
||||||
|
|
||||||
while ((fFlags & FLAG_NO_RECEIVE) == 0
|
while (true) {
|
||||||
&& fReceiveQueue.Available() < dataNeeded
|
if (fState == CLOSING || fState == WAIT_FOR_FINISH_ACKNOWLEDGE
|
||||||
&& (fReceiveQueue.PushedData() == 0
|
|| fState == TIME_WAIT) {
|
||||||
|| fReceiveQueue.Available() < fReceiveQueue.PushedData())) {
|
// ``Connection closing''.
|
||||||
locker.Unlock();
|
return B_OK;
|
||||||
|
}
|
||||||
|
|
||||||
status_t status = acquire_sem_etc(fReceiveLock, 1,
|
if (fReceiveQueue.Available() > dataNeeded ||
|
||||||
B_ABSOLUTE_TIMEOUT | B_CAN_INTERRUPT, timeout);
|
(fReceiveQueue.PushedData() > 0
|
||||||
|
&& (fReceiveQueue.PushedData() >= fReceiveQueue.Available())))
|
||||||
|
break;
|
||||||
|
|
||||||
locker.Lock();
|
if (fState == FINISH_RECEIVED) {
|
||||||
|
// ``If no text is awaiting delivery, the RECEIVE will
|
||||||
|
// get a Connection closing''.
|
||||||
|
return B_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (flags & MSG_DONTWAIT)
|
||||||
|
return B_WOULD_BLOCK;
|
||||||
|
|
||||||
|
status_t status = wait_on_locker(locker, fReceiveLock, timeout);
|
||||||
if (status < B_OK) {
|
if (status < B_OK) {
|
||||||
// The Open Group base specification mentions that EINTR should be
|
// The Open Group base specification mentions that EINTR should be
|
||||||
// returned if the recv() is interrupted before _any data_ is
|
// returned if the recv() is interrupted before _any data_ is
|
||||||
// available. So we actually check if there is data, and if so,
|
// available. So we actually check if there is data, and if so,
|
||||||
// push it to the user.
|
// push it to the user.
|
||||||
if ((status == B_TIMED_OUT || status == B_INTERRUPTED)
|
if ((status == B_TIMED_OUT || status == B_INTERRUPTED)
|
||||||
&& fReceiveQueue.Available() > 0)
|
&& fReceiveQueue.Available() > 0)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TRACE("ReadData(): read %lu bytes, %lu are available (flags 0x%x)",
|
TRACE(" ReadData(): read %lu bytes, %lu are available.",
|
||||||
numBytes, fReceiveQueue.Available(), (unsigned int)fFlags);
|
numBytes, fReceiveQueue.Available());
|
||||||
|
|
||||||
if (numBytes < fReceiveQueue.Available())
|
if (numBytes < fReceiveQueue.Available())
|
||||||
release_sem_etc(fReceiveLock, 1, B_DO_NOT_RESCHEDULE);
|
release_sem_etc(fReceiveLock, 1, B_DO_NOT_RESCHEDULE);
|
||||||
|
|
||||||
return fReceiveQueue.Get(numBytes, (flags & MSG_PEEK) == 0, _buffer);
|
ssize_t receivedBytes = fReceiveQueue.Get(numBytes,
|
||||||
|
(flags & MSG_PEEK) == 0, _buffer);
|
||||||
|
|
||||||
|
TRACE(" ReadData(): %lu bytes kept.", fReceiveQueue.Available());
|
||||||
|
|
||||||
|
return receivedBytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
ssize_t
|
ssize_t
|
||||||
TCPEndpoint::ReadAvailable()
|
TCPEndpoint::ReadAvailable()
|
||||||
{
|
{
|
||||||
TRACE("ReadAvailable()");
|
|
||||||
|
|
||||||
RecursiveLocker locker(fLock);
|
RecursiveLocker locker(fLock);
|
||||||
|
|
||||||
|
TRACE("ReadAvailable(): %li", _AvailableData());
|
||||||
|
|
||||||
return _AvailableData();
|
return _AvailableData();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -743,9 +780,7 @@ TCPEndpoint::SynchronizeSentReceive(tcp_segment_header &segment, net_buffer *buf
|
|||||||
release_sem_etc(fAcceptSemaphore, 1, B_DO_NOT_RESCHEDULE);
|
release_sem_etc(fAcceptSemaphore, 1, B_DO_NOT_RESCHEDULE);
|
||||||
}
|
}
|
||||||
|
|
||||||
release_sem_etc(fSendLock, 1, B_DO_NOT_RESCHEDULE);
|
release_sem_etc(fSendLock, 0, B_DO_NOT_RESCHEDULE | B_RELEASE_ALL);
|
||||||
release_sem_etc(fReceiveLock, 1, B_DO_NOT_RESCHEDULE);
|
|
||||||
// TODO: this is not enough - we need to use B_RELEASE_ALL
|
|
||||||
_NotifyReader();
|
_NotifyReader();
|
||||||
} else {
|
} else {
|
||||||
// simultaneous open
|
// simultaneous open
|
||||||
@@ -762,8 +797,8 @@ TCPEndpoint::SynchronizeSentReceive(tcp_segment_header &segment, net_buffer *buf
|
|||||||
int32
|
int32
|
||||||
TCPEndpoint::Receive(tcp_segment_header &segment, net_buffer *buffer)
|
TCPEndpoint::Receive(tcp_segment_header &segment, net_buffer *buffer)
|
||||||
{
|
{
|
||||||
TRACE("Receive(): Connection in state %d received packet %p (%lu bytes) with flags 0x%x, seq %lu, ack %lu!",
|
TRACE("Receive(): Connection in state %s received packet %p (%lu bytes) with flags 0x%x, seq %lu, ack %lu!",
|
||||||
fState, buffer, buffer->size, segment.flags, segment.sequence, segment.acknowledge);
|
name_for_state(fState), buffer, buffer->size, segment.flags, segment.sequence, segment.acknowledge);
|
||||||
|
|
||||||
// TODO: rethink locking!
|
// TODO: rethink locking!
|
||||||
|
|
||||||
@@ -804,25 +839,21 @@ TCPEndpoint::Receive(tcp_segment_header &segment, net_buffer *buffer)
|
|||||||
}
|
}
|
||||||
} else if (segment.acknowledge == fSendUnacknowledged
|
} else if (segment.acknowledge == fSendUnacknowledged
|
||||||
&& fReceiveQueue.IsContiguous()
|
&& fReceiveQueue.IsContiguous()
|
||||||
&& fReceiveQueue.Free() >= buffer->size) {
|
&& fReceiveQueue.Free() >= buffer->size
|
||||||
|
&& !(fFlags & FLAG_NO_RECEIVE)) {
|
||||||
TRACE("Receive(): header prediction receive!");
|
TRACE("Receive(): header prediction receive!");
|
||||||
// we're on the receiving end of the connection, and this segment
|
// we're on the receiving end of the connection, and this segment
|
||||||
// is the one we were expecting, in-sequence
|
// is the one we were expecting, in-sequence
|
||||||
if (fFlags & FLAG_NO_RECEIVE) {
|
fReceiveNext += buffer->size;
|
||||||
return DROP;
|
TRACE("Receive(): receive next = %lu", (uint32)fReceiveNext);
|
||||||
} else {
|
fReceiveQueue.Add(buffer, segment.sequence);
|
||||||
fReceiveNext += buffer->size;
|
if (segment.flags & TCP_FLAG_PUSH)
|
||||||
TRACE("Receive(): receive next = %lu", (uint32)fReceiveNext);
|
fReceiveQueue.SetPushPointer();
|
||||||
fReceiveQueue.Add(buffer, segment.sequence);
|
|
||||||
if (segment.flags & TCP_FLAG_PUSH)
|
|
||||||
fReceiveQueue.SetPushPointer(fReceiveNext);
|
|
||||||
|
|
||||||
release_sem_etc(fReceiveLock, 1, B_DO_NOT_RESCHEDULE);
|
_NotifyReader();
|
||||||
// TODO: real conditional locking needed!
|
|
||||||
_NotifyReader();
|
|
||||||
|
|
||||||
return KEEP | ACKNOWLEDGE;
|
return KEEP | (segment.flags & TCP_FLAG_PUSH ?
|
||||||
}
|
IMMEDIATE_ACKNOWLEDGE : ACKNOWLEDGE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -845,8 +876,6 @@ TCPEndpoint::Receive(tcp_segment_header &segment, net_buffer *buffer)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (fState != TIME_WAIT && fReceiveQueue.Available() > 0) {
|
if (fState != TIME_WAIT && fReceiveQueue.Available() > 0) {
|
||||||
release_sem_etc(fReceiveLock, 1, B_DO_NOT_RESCHEDULE);
|
|
||||||
// TODO: real conditional locking needed!
|
|
||||||
_NotifyReader();
|
_NotifyReader();
|
||||||
} else {
|
} else {
|
||||||
return DELETE | DROP;
|
return DELETE | DROP;
|
||||||
@@ -927,9 +956,7 @@ TCPEndpoint::Receive(tcp_segment_header &segment, net_buffer *buffer)
|
|||||||
|
|
||||||
fState = ESTABLISHED;
|
fState = ESTABLISHED;
|
||||||
|
|
||||||
release_sem_etc(fSendLock, 1, B_DO_NOT_RESCHEDULE);
|
release_sem_etc(fSendLock, 0, B_DO_NOT_RESCHEDULE | B_RELEASE_ALL);
|
||||||
release_sem_etc(fReceiveLock, 1, B_DO_NOT_RESCHEDULE);
|
|
||||||
// TODO: real conditional locking needed!
|
|
||||||
_NotifyReader();
|
_NotifyReader();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1009,64 +1036,60 @@ TCPEndpoint::Receive(tcp_segment_header &segment, net_buffer *buffer)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: ignore data *after* FIN
|
bool notify = false;
|
||||||
|
|
||||||
|
if (buffer->size > 0 && _ShouldReceive()) {
|
||||||
|
fReceiveQueue.Add(buffer, segment.sequence);
|
||||||
|
fReceiveNext += buffer->size;
|
||||||
|
notify = true;
|
||||||
|
TRACE("Receive(): adding data, receive next = %lu. Now have %lu bytes.",
|
||||||
|
(uint32)fReceiveNext, fReceiveQueue.Available());
|
||||||
|
|
||||||
|
if (segment.flags & TCP_FLAG_PUSH)
|
||||||
|
fReceiveQueue.SetPushPointer();
|
||||||
|
} else {
|
||||||
|
action = (action & ~KEEP) | DROP;
|
||||||
|
}
|
||||||
|
|
||||||
if (segment.flags & TCP_FLAG_FINISH) {
|
if (segment.flags & TCP_FLAG_FINISH) {
|
||||||
TRACE("Receive(): peer is finishing connection!");
|
if (fState != CLOSED && fState != LISTEN && fState != SYNCHRONIZE_SENT) {
|
||||||
fReceiveNext++;
|
TRACE("Receive(): peer is finishing connection!");
|
||||||
|
fReceiveNext++;
|
||||||
|
notify = true;
|
||||||
|
|
||||||
release_sem_etc(fReceiveLock, 1, B_DO_NOT_RESCHEDULE);
|
// FIN implies PSH
|
||||||
// TODO: real conditional locking needed!
|
fReceiveQueue.SetPushPointer();
|
||||||
_NotifyReader();
|
|
||||||
|
|
||||||
// other side is closing connection; change states
|
// RFC 793 states that we must send an ACK to FIN
|
||||||
switch (fState) {
|
action |= IMMEDIATE_ACKNOWLEDGE;
|
||||||
case ESTABLISHED:
|
|
||||||
case SYNCHRONIZE_RECEIVED:
|
|
||||||
fState = FINISH_RECEIVED;
|
|
||||||
action |= IMMEDIATE_ACKNOWLEDGE;
|
|
||||||
break;
|
|
||||||
case FINISH_ACKNOWLEDGED:
|
|
||||||
fState = TIME_WAIT;
|
|
||||||
_EnterTimeWait();
|
|
||||||
break;
|
|
||||||
case FINISH_RECEIVED:
|
|
||||||
// a second FIN?
|
|
||||||
break;
|
|
||||||
case FINISH_SENT:
|
|
||||||
// simultaneous close
|
|
||||||
fState = CLOSING;
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
// other side is closing connection; change states
|
||||||
break;
|
switch (fState) {
|
||||||
|
case ESTABLISHED:
|
||||||
|
case SYNCHRONIZE_RECEIVED:
|
||||||
|
fState = FINISH_RECEIVED;
|
||||||
|
break;
|
||||||
|
case FINISH_SENT:
|
||||||
|
// simultaneous close
|
||||||
|
fState = CLOSING;
|
||||||
|
break;
|
||||||
|
case FINISH_ACKNOWLEDGED:
|
||||||
|
fState = TIME_WAIT;
|
||||||
|
_EnterTimeWait();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (buffer->size > 0 || (segment.flags & (TCP_FLAG_SYNCHRONIZE | TCP_FLAG_FINISH)) != 0)
|
if (notify)
|
||||||
|
_NotifyReader();
|
||||||
|
|
||||||
|
if (buffer->size > 0 || (segment.flags & TCP_FLAG_SYNCHRONIZE) != 0)
|
||||||
action |= ACKNOWLEDGE;
|
action |= ACKNOWLEDGE;
|
||||||
|
|
||||||
TRACE("Receive():Entering state %d, segment action %ld", fState, action);
|
TRACE("Receive():Entering state %s, segment action %ld", name_for_state(fState), action);
|
||||||
|
|
||||||
// state machine is done switching states and the data is good.
|
|
||||||
// put it in the receive buffer
|
|
||||||
|
|
||||||
if (buffer->size > 0 && (fFlags & FLAG_NO_RECEIVE) == 0) {
|
|
||||||
fReceiveQueue.Add(buffer, segment.sequence);
|
|
||||||
fReceiveNext += buffer->size;
|
|
||||||
TRACE("Receive(): adding data, receive next = %lu!", (uint32)fReceiveNext);
|
|
||||||
|
|
||||||
release_sem_etc(fReceiveLock, 1, B_DO_NOT_RESCHEDULE);
|
|
||||||
// TODO: real conditional locking needed!
|
|
||||||
_NotifyReader();
|
|
||||||
} else
|
|
||||||
gBufferModule->free(buffer);
|
|
||||||
|
|
||||||
if (segment.flags & TCP_FLAG_PUSH)
|
|
||||||
fReceiveQueue.SetPushPointer(fReceiveQueue.LastSequence());
|
|
||||||
|
|
||||||
if (segment.flags & TCP_FLAG_FINISH)
|
|
||||||
fFlags |= FLAG_NO_RECEIVE;
|
|
||||||
|
|
||||||
return action;
|
return action;
|
||||||
}
|
}
|
||||||
@@ -1316,8 +1339,6 @@ TCPEndpoint::_ShutdownEgress(bool closing)
|
|||||||
fState = FINISH_SENT;
|
fState = FINISH_SENT;
|
||||||
else if (fState == FINISH_RECEIVED)
|
else if (fState == FINISH_RECEIVED)
|
||||||
fState = WAIT_FOR_FINISH_ACKNOWLEDGE;
|
fState = WAIT_FOR_FINISH_ACKNOWLEDGE;
|
||||||
else if (closing)
|
|
||||||
fState = CLOSED;
|
|
||||||
else
|
else
|
||||||
return B_OK;
|
return B_OK;
|
||||||
|
|
||||||
@@ -1327,8 +1348,6 @@ TCPEndpoint::_ShutdownEgress(bool closing)
|
|||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
fFlags |= FLAG_NO_SEND;
|
|
||||||
|
|
||||||
return B_OK;
|
return B_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1338,9 +1357,7 @@ TCPEndpoint::_AvailableData() const
|
|||||||
{
|
{
|
||||||
ssize_t availableData = fReceiveQueue.Available();
|
ssize_t availableData = fReceiveQueue.Available();
|
||||||
|
|
||||||
// FLAG_NO_RECEIVE is set whenever the socket is in a state
|
if (availableData == 0 && !_ShouldReceive())
|
||||||
// where read() would be non-blocking and return 0.
|
|
||||||
if ((fFlags & FLAG_NO_RECEIVE) && availableData == 0)
|
|
||||||
return ENOTCONN;
|
return ENOTCONN;
|
||||||
|
|
||||||
return availableData;
|
return availableData;
|
||||||
@@ -1350,10 +1367,24 @@ TCPEndpoint::_AvailableData() const
|
|||||||
void
|
void
|
||||||
TCPEndpoint::_NotifyReader()
|
TCPEndpoint::_NotifyReader()
|
||||||
{
|
{
|
||||||
|
// TODO maintain a waiting receiver count so we know whether
|
||||||
|
// we should release or not.
|
||||||
|
release_sem_etc(fReceiveLock, 1, B_DO_NOT_RESCHEDULE);
|
||||||
gSocketModule->notify(socket, B_SELECT_READ, _AvailableData());
|
gSocketModule->notify(socket, B_SELECT_READ, _AvailableData());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool
|
||||||
|
TCPEndpoint::_ShouldReceive() const
|
||||||
|
{
|
||||||
|
if (fFlags & FLAG_NO_RECEIVE)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return fState == ESTABLISHED || fState == FINISH_SENT
|
||||||
|
|| fState == FINISH_ACKNOWLEDGED;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// #pragma mark - timer
|
// #pragma mark - timer
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -69,6 +69,7 @@ class TCPEndpoint : public net_protocol {
|
|||||||
status_t _ShutdownEgress(bool closing);
|
status_t _ShutdownEgress(bool closing);
|
||||||
ssize_t _AvailableData() const;
|
ssize_t _AvailableData() const;
|
||||||
void _NotifyReader();
|
void _NotifyReader();
|
||||||
|
bool _ShouldReceive() const;
|
||||||
|
|
||||||
static void _TimeWaitTimer(net_timer *timer, void *data);
|
static void _TimeWaitTimer(net_timer *timer, void *data);
|
||||||
static void _RetransmitTimer(net_timer *timer, void *data);
|
static void _RetransmitTimer(net_timer *timer, void *data);
|
||||||
|
|||||||
@@ -260,7 +260,7 @@ reply_with_reset(tcp_segment_header &segment, net_buffer *buffer)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static const char *
|
const char *
|
||||||
name_for_state(tcp_state state)
|
name_for_state(tcp_state state)
|
||||||
{
|
{
|
||||||
switch (state) {
|
switch (state) {
|
||||||
|
|||||||
@@ -168,4 +168,6 @@ extern EndpointManager *gEndpointManager;
|
|||||||
|
|
||||||
status_t add_tcp_header(tcp_segment_header &segment, net_buffer *buffer);
|
status_t add_tcp_header(tcp_segment_header &segment, net_buffer *buffer);
|
||||||
|
|
||||||
|
const char *name_for_state(tcp_state state);
|
||||||
|
|
||||||
#endif // TCP_H
|
#endif // TCP_H
|
||||||
|
|||||||
Reference in New Issue
Block a user