boot_net: TCP and UDP style cleanup

Remove superfluous comments.
Adjust spacing and initializer indentation.
Make NULL pointer checks explicit.
Don't assign in if-clause.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@38529 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Andreas Färber
2010-09-04 22:14:20 +00:00
parent 267f107882
commit 7d415c01cc
2 changed files with 73 additions and 66 deletions
+1 -1
View File
@@ -775,7 +775,7 @@ TCPService::BindSocket(TCPSocket* socket)
if (socket == NULL) if (socket == NULL)
return B_BAD_VALUE; return B_BAD_VALUE;
if (_FindSocket(socket->Address(), socket->Port())) if (_FindSocket(socket->Address(), socket->Port()) != NULL)
return EADDRINUSE; return EADDRINUSE;
return fSockets.Add(socket); return fSockets.Add(socket);
+72 -65
View File
@@ -3,9 +3,11 @@
* All rights reserved. Distributed under the terms of the MIT License. * All rights reserved. Distributed under the terms of the MIT License.
*/ */
#include <boot/net/UDP.h> #include <boot/net/UDP.h>
#include <stdio.h> #include <stdio.h>
#include <KernelExport.h> #include <KernelExport.h>
#include <boot/net/ChainBuffer.h> #include <boot/net/ChainBuffer.h>
@@ -22,31 +24,32 @@
// #pragma mark - UDPPacket // #pragma mark - UDPPacket
// constructor
UDPPacket::UDPPacket() UDPPacket::UDPPacket()
: fNext(NULL), :
fData(NULL), fNext(NULL),
fSize(0) fData(NULL),
fSize(0)
{ {
} }
// destructor
UDPPacket::~UDPPacket() UDPPacket::~UDPPacket()
{ {
free(fData); free(fData);
} }
// SetTo
status_t status_t
UDPPacket::SetTo(const void *data, size_t size, ip_addr_t sourceAddress, UDPPacket::SetTo(const void *data, size_t size, ip_addr_t sourceAddress,
uint16 sourcePort, ip_addr_t destinationAddress, uint16 destinationPort) uint16 sourcePort, ip_addr_t destinationAddress, uint16 destinationPort)
{ {
if (!data) if (data == NULL)
return B_BAD_VALUE; return B_BAD_VALUE;
// clone the data // clone the data
fData = malloc(size); fData = malloc(size);
if (!fData) if (fData == NULL)
return B_NO_MEMORY; return B_NO_MEMORY;
memcpy(fData, data, size); memcpy(fData, data, size);
@@ -59,56 +62,56 @@ UDPPacket::SetTo(const void *data, size_t size, ip_addr_t sourceAddress,
return B_OK; return B_OK;
} }
// Next
UDPPacket * UDPPacket *
UDPPacket::Next() const UDPPacket::Next() const
{ {
return fNext; return fNext;
} }
// SetNext
void void
UDPPacket::SetNext(UDPPacket *next) UDPPacket::SetNext(UDPPacket *next)
{ {
fNext = next; fNext = next;
} }
// Data
const void * const void *
UDPPacket::Data() const UDPPacket::Data() const
{ {
return fData; return fData;
} }
// DataSize
size_t size_t
UDPPacket::DataSize() const UDPPacket::DataSize() const
{ {
return fSize; return fSize;
} }
// SourceAddress
ip_addr_t ip_addr_t
UDPPacket::SourceAddress() const UDPPacket::SourceAddress() const
{ {
return fSourceAddress; return fSourceAddress;
} }
// SourcePort
uint16 uint16
UDPPacket::SourcePort() const UDPPacket::SourcePort() const
{ {
return fSourcePort; return fSourcePort;
} }
// DestinationAddress
ip_addr_t ip_addr_t
UDPPacket::DestinationAddress() const UDPPacket::DestinationAddress() const
{ {
return fDestinationAddress; return fDestinationAddress;
} }
// DestinationPort
uint16 uint16
UDPPacket::DestinationPort() const UDPPacket::DestinationPort() const
{ {
@@ -118,28 +121,29 @@ UDPPacket::DestinationPort() const
// #pragma mark - UDPSocket // #pragma mark - UDPSocket
// constructor
UDPSocket::UDPSocket() UDPSocket::UDPSocket()
: fUDPService(NetStack::Default()->GetUDPService()), :
fFirstPacket(NULL), fUDPService(NetStack::Default()->GetUDPService()),
fLastPacket(NULL), fFirstPacket(NULL),
fAddress(INADDR_ANY), fLastPacket(NULL),
fPort(0) fAddress(INADDR_ANY),
fPort(0)
{ {
} }
// destructor
UDPSocket::~UDPSocket() UDPSocket::~UDPSocket()
{ {
if (fPort != 0 && fUDPService) if (fPort != 0 && fUDPService != NULL)
fUDPService->UnbindSocket(this); fUDPService->UnbindSocket(this);
} }
// Bind
status_t status_t
UDPSocket::Bind(ip_addr_t address, uint16 port) UDPSocket::Bind(ip_addr_t address, uint16 port)
{ {
if (!fUDPService) { if (fUDPService == NULL) {
printf("UDPSocket::Bind(): no UDP service\n"); printf("UDPSocket::Bind(): no UDP service\n");
return B_NO_INIT; return B_NO_INIT;
} }
@@ -151,7 +155,8 @@ UDPSocket::Bind(ip_addr_t address, uint16 port)
if (fPort != 0) { if (fPort != 0) {
printf("UDPSocket::Bind(): already bound\n"); printf("UDPSocket::Bind(): already bound\n");
return EALREADY; // correct code? return EALREADY;
// correct code?
} }
status_t error = fUDPService->BindSocket(this, address, port); status_t error = fUDPService->BindSocket(this, address, port);
@@ -166,44 +171,45 @@ UDPSocket::Bind(ip_addr_t address, uint16 port)
return B_OK; return B_OK;
} }
// Send
status_t status_t
UDPSocket::Send(ip_addr_t destinationAddress, uint16 destinationPort, UDPSocket::Send(ip_addr_t destinationAddress, uint16 destinationPort,
ChainBuffer *buffer) ChainBuffer *buffer)
{ {
if (!fUDPService) if (fUDPService == NULL)
return B_NO_INIT; return B_NO_INIT;
return fUDPService->Send(fPort, destinationAddress, destinationPort, return fUDPService->Send(fPort, destinationAddress, destinationPort,
buffer); buffer);
} }
// Send
status_t status_t
UDPSocket::Send(ip_addr_t destinationAddress, uint16 destinationPort, UDPSocket::Send(ip_addr_t destinationAddress, uint16 destinationPort,
const void *data, size_t size) const void *data, size_t size)
{ {
if (!data) if (data == NULL)
return B_BAD_VALUE; return B_BAD_VALUE;
ChainBuffer buffer((void*)data, size); ChainBuffer buffer((void*)data, size);
return Send(destinationAddress, destinationPort, &buffer); return Send(destinationAddress, destinationPort, &buffer);
} }
// Receive
status_t status_t
UDPSocket::Receive(UDPPacket **_packet, bigtime_t timeout) UDPSocket::Receive(UDPPacket **_packet, bigtime_t timeout)
{ {
if (!fUDPService) if (fUDPService == NULL)
return B_NO_INIT; return B_NO_INIT;
if (!_packet) if (_packet == NULL)
return B_BAD_VALUE; return B_BAD_VALUE;
bigtime_t startTime = system_time(); bigtime_t startTime = system_time();
for (;;) { for (;;) {
fUDPService->ProcessIncomingPackets(); fUDPService->ProcessIncomingPackets();
if ((*_packet = PopPacket())) *_packet = PopPacket();
if (*_packet != NULL)
return B_OK; return B_OK;
if (system_time() - startTime > timeout) if (system_time() - startTime > timeout)
@@ -211,11 +217,11 @@ UDPSocket::Receive(UDPPacket **_packet, bigtime_t timeout)
} }
} }
// PushPacket
void void
UDPSocket::PushPacket(UDPPacket *packet) UDPSocket::PushPacket(UDPPacket *packet)
{ {
if (fLastPacket) if (fLastPacket != NULL)
fLastPacket->SetNext(packet); fLastPacket->SetNext(packet);
else else
fFirstPacket = packet; fFirstPacket = packet;
@@ -224,17 +230,17 @@ UDPSocket::PushPacket(UDPPacket *packet)
packet->SetNext(NULL); packet->SetNext(NULL);
} }
// PopPacket
UDPPacket * UDPPacket *
UDPSocket::PopPacket() UDPSocket::PopPacket()
{ {
if (!fFirstPacket) if (fFirstPacket == NULL)
return NULL; return NULL;
UDPPacket *packet = fFirstPacket; UDPPacket *packet = fFirstPacket;
fFirstPacket = packet->Next(); fFirstPacket = packet->Next();
if (!fFirstPacket) if (fFirstPacket == NULL)
fLastPacket = NULL; fLastPacket = NULL;
packet->SetNext(NULL); packet->SetNext(NULL);
@@ -244,39 +250,40 @@ UDPSocket::PopPacket()
// #pragma mark - UDPService // #pragma mark - UDPService
// constructor
UDPService::UDPService(IPService *ipService) UDPService::UDPService(IPService *ipService)
: IPSubService(kUDPServiceName), :
fIPService(ipService) IPSubService(kUDPServiceName),
fIPService(ipService)
{ {
} }
// destructor
UDPService::~UDPService() UDPService::~UDPService()
{ {
if (fIPService) if (fIPService != NULL)
fIPService->UnregisterIPSubService(this); fIPService->UnregisterIPSubService(this);
} }
// Init
status_t status_t
UDPService::Init() UDPService::Init()
{ {
if (!fIPService) if (fIPService == NULL)
return B_BAD_VALUE; return B_BAD_VALUE;
if (!fIPService->RegisterIPSubService(this)) if (!fIPService->RegisterIPSubService(this))
return B_NO_MEMORY; return B_NO_MEMORY;
return B_OK; return B_OK;
} }
// IPProtocol
uint8 uint8
UDPService::IPProtocol() const UDPService::IPProtocol() const
{ {
return IPPROTO_UDP; return IPPROTO_UDP;
} }
// HandleIPPacket
void void
UDPService::HandleIPPacket(IPService *ipService, ip_addr_t sourceIP, UDPService::HandleIPPacket(IPService *ipService, ip_addr_t sourceIP,
ip_addr_t destinationIP, const void *data, size_t size) ip_addr_t destinationIP, const void *data, size_t size)
@@ -285,7 +292,7 @@ UDPService::HandleIPPacket(IPService *ipService, ip_addr_t sourceIP,
"%lu - %lu bytes\n", sourceIP, destinationIP, size, "%lu - %lu bytes\n", sourceIP, destinationIP, size,
sizeof(udp_header))); sizeof(udp_header)));
if (!data || size < sizeof(udp_header)) if (data == NULL || size < sizeof(udp_header))
return; return;
const udp_header *header = (const udp_header*)data; const udp_header *header = (const udp_header*)data;
@@ -304,12 +311,12 @@ UDPService::HandleIPPacket(IPService *ipService, ip_addr_t sourceIP,
// find the target socket // find the target socket
UDPSocket *socket = _FindSocket(destinationIP, destination); UDPSocket *socket = _FindSocket(destinationIP, destination);
if (!socket) if (socket == NULL)
return; return;
// create a UDPPacket and queue it in the socket // create a UDPPacket and queue it in the socket
UDPPacket *packet = new(nothrow) UDPPacket; UDPPacket *packet = new(nothrow) UDPPacket;
if (!packet) if (packet == NULL)
return; return;
status_t error = packet->SetTo((uint8*)data + sizeof(udp_header), status_t error = packet->SetTo((uint8*)data + sizeof(udp_header),
length - sizeof(udp_header), sourceIP, source, destinationIP, length - sizeof(udp_header), sourceIP, source, destinationIP,
@@ -320,19 +327,19 @@ UDPService::HandleIPPacket(IPService *ipService, ip_addr_t sourceIP,
delete packet; delete packet;
} }
// Send
status_t status_t
UDPService::Send(uint16 sourcePort, ip_addr_t destinationAddress, UDPService::Send(uint16 sourcePort, ip_addr_t destinationAddress,
uint16 destinationPort, ChainBuffer *buffer) uint16 destinationPort, ChainBuffer *buffer)
{ {
TRACE(("UDPService::Send(source port: %hu, to: %08lx:%hu, %lu bytes)\n", TRACE(("UDPService::Send(source port: %hu, to: %08lx:%hu, %lu bytes)\n",
sourcePort, destinationAddress, destinationPort, sourcePort, destinationAddress, destinationPort,
(buffer ? buffer->TotalSize() : 0))); (buffer != NULL ? buffer->TotalSize() : 0)));
if (!fIPService) if (fIPService == NULL)
return B_NO_INIT; return B_NO_INIT;
if (!buffer) if (buffer == NULL)
return B_BAD_VALUE; return B_BAD_VALUE;
// prepend the UDP header // prepend the UDP header
@@ -354,22 +361,22 @@ UDPService::Send(uint16 sourcePort, ip_addr_t destinationAddress,
return fIPService->Send(destinationAddress, IPPROTO_UDP, &headerBuffer); return fIPService->Send(destinationAddress, IPPROTO_UDP, &headerBuffer);
} }
// ProcessIncomingPackets
void void
UDPService::ProcessIncomingPackets() UDPService::ProcessIncomingPackets()
{ {
if (fIPService) if (fIPService != NULL)
fIPService->ProcessIncomingPackets(); fIPService->ProcessIncomingPackets();
} }
// BindSocket
status_t status_t
UDPService::BindSocket(UDPSocket *socket, ip_addr_t address, uint16 port) UDPService::BindSocket(UDPSocket *socket, ip_addr_t address, uint16 port)
{ {
if (!socket) if (socket == NULL)
return B_BAD_VALUE; return B_BAD_VALUE;
if (_FindSocket(address, port)) { if (_FindSocket(address, port) != NULL) {
printf("UDPService::BindSocket(): address in use\n"); printf("UDPService::BindSocket(): address in use\n");
return EADDRINUSE; return EADDRINUSE;
} }
@@ -377,14 +384,14 @@ UDPService::BindSocket(UDPSocket *socket, ip_addr_t address, uint16 port)
return fSockets.Add(socket); return fSockets.Add(socket);
} }
// UnbindSocket
void void
UDPService::UnbindSocket(UDPSocket *socket) UDPService::UnbindSocket(UDPSocket *socket)
{ {
fSockets.Remove(socket); fSockets.Remove(socket);
} }
// _ChecksumBuffer
uint16 uint16
UDPService::_ChecksumBuffer(ChainBuffer *buffer, ip_addr_t source, UDPService::_ChecksumBuffer(ChainBuffer *buffer, ip_addr_t source,
ip_addr_t destination, uint16 length) ip_addr_t destination, uint16 length)
@@ -412,7 +419,7 @@ UDPService::_ChecksumBuffer(ChainBuffer *buffer, ip_addr_t source,
return checksum; return checksum;
} }
// _ChecksumData
uint16 uint16
UDPService::_ChecksumData(const void *data, uint16 length, ip_addr_t source, UDPService::_ChecksumData(const void *data, uint16 length, ip_addr_t source,
ip_addr_t destination) ip_addr_t destination)
@@ -421,7 +428,7 @@ UDPService::_ChecksumData(const void *data, uint16 length, ip_addr_t source,
return _ChecksumBuffer(&buffer, source, destination, length); return _ChecksumBuffer(&buffer, source, destination, length);
} }
// _FindSocket
UDPSocket * UDPSocket *
UDPService::_FindSocket(ip_addr_t address, uint16 port) UDPService::_FindSocket(ip_addr_t address, uint16 port)
{ {