From 241b048033e34efa942d1f43f8d199d086b07743 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Tue, 29 Aug 2006 00:19:23 +0000 Subject: [PATCH] * Fixed build of libnetapi.so - it probably won't work for now, though, have a look at the comment in r5_compatibility.h. * Intentionally broke source compatibility and removed all that outdated Nettle stuff. * Also, I took the liberty of making m_init private and rename it to fInit - again, this will only affect source compatibility. * Rewrote NetEndpoint.h * Fixed quite a few small bugs around the code that I touched, for example in NetAddress, SetTo() never set fInit, and therefore could be wrong. * Some cleanup. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@18680 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- headers/os/net/NetAddress.h | 128 ++--- headers/os/net/NetBuffer.h | 174 +++---- headers/os/net/NetEndpoint.h | 321 +++--------- headers/private/net/r5_compatibility.h | 15 + src/kits/network/libnetapi/Jamfile | 2 +- src/kits/network/libnetapi/NetAddress.cpp | 510 +++++++------------ src/kits/network/libnetapi/NetBuffer.cpp | 77 ++- src/kits/network/libnetapi/NetEndpoint.cpp | 552 +++++++++++---------- 8 files changed, 706 insertions(+), 1073 deletions(-) create mode 100644 headers/private/net/r5_compatibility.h diff --git a/headers/os/net/NetAddress.h b/headers/os/net/NetAddress.h index 34c6344fa7..403e8277ce 100644 --- a/headers/os/net/NetAddress.h +++ b/headers/os/net/NetAddress.h @@ -1,105 +1,61 @@ -/*=--------------------------------------------------------------------------=* - * NetAddress.h -- Interface definition for the BNetAddress class. - * - * Written by S.T. Mansfield (thephantom@mac.com) - *=--------------------------------------------------------------------------=* - * Copyright (c) 2002, The OpenBeOS project. - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - *=--------------------------------------------------------------------------=* +/* + * Copyright 2002-2006, Haiku, Inc. All Rights Reserved. + * Distributed under the terms of the MIT License. */ +#ifndef H_NETADDRESS +#define H_NETADDRESS -#ifndef _NETADDRESS_H -#define _NETADDRESS_H #include #include #include -#include -/* - * Empty forward declaration to satisfy the GetImpl method. - */ -class NLAddress; +#include +#include -class BNetAddress : public BArchivable -{ -public: - BNetAddress( BMessage* archive ); - virtual ~BNetAddress(); - virtual status_t Archive( BMessage* into, bool deep = true ) const; - static BArchivable* Instantiate( BMessage* archive ); +class BNetAddress : public BArchivable { + public: + BNetAddress(BMessage* archive); + virtual ~BNetAddress(); - BNetAddress( const char* hostname = 0, unsigned short port = 0 ); - BNetAddress( const struct sockaddr_in& sa ); - BNetAddress( in_addr addr, int port = 0 ); - BNetAddress( uint32 addr, int port = 0 ); - BNetAddress( const BNetAddress& refparam ); - BNetAddress( const char* hostname, const char* protocol, const char* service ); + virtual status_t Archive(BMessage* into, bool deep = true) const; + static BArchivable* Instantiate(BMessage* archive); - BNetAddress& operator=( const BNetAddress& ); + BNetAddress(const char* hostname = 0, unsigned short port = 0); + BNetAddress(const struct sockaddr_in& addr); + BNetAddress(in_addr addr, int port = 0); + BNetAddress(uint32 addr, int port = 0); + BNetAddress(const BNetAddress& other); + BNetAddress(const char* hostname, const char* protocol, const char* service); - status_t InitCheck(); + BNetAddress& operator=(const BNetAddress&); - status_t SetTo( const char* hostname, const char* protocol, const char* service ); - status_t SetTo( const char* hostname = NULL, unsigned short port = 0 ); - status_t SetTo( const struct sockaddr_in& sa ); - status_t SetTo( in_addr addr, int port = 0 ); - status_t SetTo( uint32 addr = INADDR_ANY, int port = 0 ); + status_t InitCheck(); - status_t GetAddr( char* hostname = NULL, unsigned short* port = NULL ) const; - status_t GetAddr( struct sockaddr_in& sa ) const; - status_t GetAddr( in_addr& addr, unsigned short* port = NULL ) const; + status_t SetTo(const char* hostname, const char* protocol, const char* service); + status_t SetTo(const char* hostname = NULL, unsigned short port = 0); + status_t SetTo(const struct sockaddr_in& addr); + status_t SetTo(in_addr addr, int port = 0); + status_t SetTo(uint32 addr = INADDR_ANY, int port = 0); - inline NLAddress *GetImpl() const; + status_t GetAddr(char* hostname = NULL, unsigned short* port = NULL) const; + status_t GetAddr(struct sockaddr_in& addr) const; + status_t GetAddr(in_addr& addr, unsigned short* port = NULL) const; -protected: - status_t m_init; + private: + virtual void _ReservedBNetAddressFBCCruft1(); + virtual void _ReservedBNetAddressFBCCruft2(); + virtual void _ReservedBNetAddressFBCCruft3(); + virtual void _ReservedBNetAddressFBCCruft4(); + virtual void _ReservedBNetAddressFBCCruft5(); + virtual void _ReservedBNetAddressFBCCruft6(); -private: - virtual void _ReservedBNetAddressFBCCruft1(); - virtual void _ReservedBNetAddressFBCCruft2(); - virtual void _ReservedBNetAddressFBCCruft3(); - virtual void _ReservedBNetAddressFBCCruft4(); - virtual void _ReservedBNetAddressFBCCruft5(); - virtual void _ReservedBNetAddressFBCCruft6(); - -// Private copy helper. - status_t clone( const BNetAddress& RefParam ); - - int32 fFamily; // Encapsulation of sockaddr::sin_family - int32 fPort; // Encapsulation of sockaddr::sin_port - int32 fAddress; // Encapsulation of sockaddr::sin_addr.s_addr - -// Not used, here to maintain R5 binary compatiblilty. - int32 fPrivateData[6]; + status_t fInit; + int32 fFamily; + int32 fPort; + int32 fAddress; + int32 fPrivateData[6]; }; - -inline NLAddress* BNetAddress::GetImpl( void ) const -{ - return NULL; // No Nettle backward compatibility -} - -#endif // <-- #ifndef _NETADDRESS_H - - -/*=------------------------------------------------------------------- End -=*/ - +#endif // H_NETADDRESS diff --git a/headers/os/net/NetBuffer.h b/headers/os/net/NetBuffer.h index 27a5418245..a954c25da8 100644 --- a/headers/os/net/NetBuffer.h +++ b/headers/os/net/NetBuffer.h @@ -1,128 +1,86 @@ -/*=--------------------------------------------------------------------------=* - * NetBuffer.h -- Interface definition for the BNetBuffer class. - * - * Written by S.T. Mansfield (thephantom@mac.com) - *=--------------------------------------------------------------------------=* - * Copyright (c) 2002, The OpenBeOS project. - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - *=--------------------------------------------------------------------------=* +/* + * Copyright 2002-2006, Haiku, Inc. All Rights Reserved. + * Distributed under the terms of the MIT License. */ - #ifndef _NETBUFFER_H #define _NETBUFFER_H + #include #include #include -#include -/* - * Empty forward declaration to satisfy the GetImpl method. - */ -class NLPacket; +#include -class BNetBuffer : public BArchivable -{ -public: - BNetBuffer(size_t size = 0); - virtual ~BNetBuffer(); - status_t InitCheck(); +class BNetBuffer : public BArchivable { + public: + BNetBuffer(size_t size = 0); + virtual ~BNetBuffer(); - BNetBuffer(BMessage *archive); - virtual status_t Archive(BMessage *into, bool deep = true) const; - static BArchivable *Instantiate(BMessage *archive); + status_t InitCheck(); - BNetBuffer(const BNetBuffer &); - BNetBuffer& operator=(const BNetBuffer &); + BNetBuffer(BMessage* archive); + virtual status_t Archive(BMessage* into, bool deep = true) const; + static BArchivable* Instantiate(BMessage* archive); -// Mutators. - status_t AppendInt8(int8); - status_t AppendUint8(uint8); - status_t AppendInt16(int16); - status_t AppendUint16(uint16); - status_t AppendInt32(int32); - status_t AppendUint32(uint32); - status_t AppendFloat(float); - status_t AppendDouble(double); - status_t AppendString(const char *); - status_t AppendData(const void *, size_t); - status_t AppendMessage(const BMessage &); - status_t AppendInt64(int64); - status_t AppendUint64(uint64); + BNetBuffer(const BNetBuffer &); + BNetBuffer& operator=(const BNetBuffer &); - status_t RemoveInt8(int8 &); - status_t RemoveUint8(uint8 &); - status_t RemoveInt16(int16 &); - status_t RemoveUint16(uint16 &); - status_t RemoveInt32(int32 &); - status_t RemoveUint32(uint32 &); - status_t RemoveFloat(float &); - status_t RemoveDouble(double &); - status_t RemoveString(char *, size_t); - status_t RemoveData(void *, size_t); - status_t RemoveMessage(BMessage &); - status_t RemoveInt64(int64 &); - status_t RemoveUint64(uint64 &); + // Mutators. + status_t AppendInt8(int8); + status_t AppendUint8(uint8); + status_t AppendInt16(int16); + status_t AppendUint16(uint16); + status_t AppendInt32(int32); + status_t AppendUint32(uint32); + status_t AppendFloat(float); + status_t AppendDouble(double); + status_t AppendString(const char *); + status_t AppendData(const void *, size_t); + status_t AppendMessage(const BMessage &); + status_t AppendInt64(int64); + status_t AppendUint64(uint64); -// Accessors. - unsigned char *Data() const; - size_t Size() const; - size_t BytesRemaining() const; + status_t RemoveInt8(int8 &); + status_t RemoveUint8(uint8 &); + status_t RemoveInt16(int16 &); + status_t RemoveUint16(uint16 &); + status_t RemoveInt32(int32 &); + status_t RemoveUint32(uint32 &); + status_t RemoveFloat(float &); + status_t RemoveDouble(double &); + status_t RemoveString(char *, size_t); + status_t RemoveData(void *, size_t); + status_t RemoveMessage(BMessage &); + status_t RemoveInt64(int64 &); + status_t RemoveUint64(uint64 &); - inline NLPacket *GetImpl() const; + // Accessors. + unsigned char* Data() const; + size_t Size() const; + size_t BytesRemaining() const; -protected: -// Attributes. - status_t m_init; - -private: - virtual void _ReservedBNetBufferFBCCruft1(); - virtual void _ReservedBNetBufferFBCCruft2(); - virtual void _ReservedBNetBufferFBCCruft3(); - virtual void _ReservedBNetBufferFBCCruft4(); - virtual void _ReservedBNetBufferFBCCruft5(); - virtual void _ReservedBNetBufferFBCCruft6(); + private: + virtual void _ReservedBNetBufferFBCCruft1(); + virtual void _ReservedBNetBufferFBCCruft2(); + virtual void _ReservedBNetBufferFBCCruft3(); + virtual void _ReservedBNetBufferFBCCruft4(); + virtual void _ReservedBNetBufferFBCCruft5(); + virtual void _ReservedBNetBufferFBCCruft6(); - unsigned char* fData; - int32 fDataSize; - int32 fStackSize; - int32 fCapacity; - -// Not used, here to maintain R5 binary compatiblilty. - int32 fPrivateData[5]; - -// Private class helpers. - status_t clone( const BNetBuffer& ); - status_t dpop( int32, int32, void* ); - status_t dpush( int32, int32, const void* ); - status_t resize( int32 NewSize, bool RegenStack = false ); + // Private class helpers. + status_t clone(const BNetBuffer&); + status_t dpop(int32, int32, void*); + status_t dpush(int32, int32, const void*); + status_t resize(int32 newSize, bool regenStack = false); + + status_t fInit; + uint8* fData; + int32 fDataSize; + int32 fStackSize; + int32 fCapacity; + int32 _reserved[5]; }; - -inline NLPacket* BNetBuffer::GetImpl( void ) const -{ - return NULL; // No Nettle backward compatibility -} - -#endif // <-- #ifndef _NETBUFFER_H - -/*=------------------------------------------------------------------- End -=*/ - +#endif // _NETBUFFER_H diff --git a/headers/os/net/NetEndpoint.h b/headers/os/net/NetEndpoint.h index d12da0404a..d2c83f96e4 100644 --- a/headers/os/net/NetEndpoint.h +++ b/headers/os/net/NetEndpoint.h @@ -1,283 +1,92 @@ -//------------------------------------------------------------------------------ -// Copyright (c) 2001-2004, Haiku -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the "Software"), -// to deal in the Software without restriction, including without limitation -// the rights to use, copy, modify, merge, publish, distribute, sublicense, -// and/or sell copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -// DEALINGS IN THE SOFTWARE. -//------------------------------------------------------------------------------ +/* + * Copyright 2006, Haiku, Inc. All Rights Reserved. + * Distributed under the terms of the MIT License. + */ +#ifndef H_NETENDPOINT +#define H_NETENDPOINT -#ifndef _NETENDPOINT_H -#define _NETENDPOINT_H #include -#include #include +#include +#include +#include #include -#include -#include -/* - * BNetEndpoint - * - * BNetEndpoint is an object that implements a networking endpoint abstraction. - * Think of it as an object-oriented socket. BNetEndpoint provides - * various ways to send and receive data, establish network connections, bind - * to local addresses, and listen for and accept new connections. - * - */ +class BNetEndpoint : public BArchivable { + public: + BNetEndpoint(int type = SOCK_STREAM); + BNetEndpoint(int family, int type, int protocol); + BNetEndpoint(const BNetEndpoint& other); + BNetEndpoint(BMessage* archive); + virtual ~BNetEndpoint(); -// Nettle forward declarations -class NLEndpoint; + BNetEndpoint& operator=(const BNetEndpoint& other); + status_t InitCheck(); -class BNetEndpoint : public BArchivable -{ -public: + virtual status_t Archive(BMessage* into, bool deep = true) const; + static BArchivable* Instantiate(BMessage* archive); - BNetEndpoint(int type = SOCK_STREAM); - BNetEndpoint(int family, int type, int protocol); - BNetEndpoint(const BNetEndpoint &); - BNetEndpoint(BMessage *archive); + status_t SetProtocol(int protocol); + int SetOption(int32 option, int32 level, const void* data, unsigned int dataSize); + int SetNonBlocking(bool on = true); + int SetReuseAddr(bool on = true); - BNetEndpoint & operator=(const BNetEndpoint &); + const BNetAddress& LocalAddr(); + const BNetAddress& RemoteAddr(); - virtual ~BNetEndpoint(); + int Socket() const; - /* - * It is important to call InitCheck() after creating an instance of this class. - */ - status_t InitCheck(); + virtual void Close(); + virtual status_t Bind(const BNetAddress& addr); + virtual status_t Bind(int port = 0); + virtual status_t Connect(const BNetAddress& addr); + virtual status_t Connect(const char* addr, int port); + virtual status_t Listen(int backlog = 5); + virtual BNetEndpoint* Accept(int32 timeout = -1); - /* - * When a BNetEndpoint is archived, it saves various state information - * which will allow reinstantiation later. For example, if a BNetEndpoint - * that is connected to a remote FTP server is archived, when the archive - * is later instantiated the connection to the FTP server will be reopened - * automatically. - */ - virtual status_t Archive(BMessage *into, bool deep = true) const; - static BArchivable * Instantiate(BMessage *archive); - + int Error() const; + char* ErrorStr() const; - /* - * BNetEndpoint::SetProtocol() - * - * allows the protocol type to be changed from stream - * to datagram and vice-versa. The current connection (if any) is closed and - * the BNetEndpoint's state is reset. - * - * Returns B_OK if successful, B_ERROR otherwise. - */ - - status_t SetProtocol(int proto); + virtual int32 Send(const void* buffer, size_t size, int flags = 0); + virtual int32 Send(BNetBuffer& buffer, int flags = 0); - /* - * These functions allow various options to be set for data communications. - * The first allows any option to be set; the latter two turn nonblocking - * I/O on or off, and toggle reuseaddr respectively. The default is for - * *blocking* I/O and for reuseaddr to be OFF. - * - * Returns 0 if successful, -1 otherwise. - */ - int SetOption(int32 opt, int32 lev, const void *data, unsigned int datasize); - int SetNonBlocking(bool on = true); - int SetReuseAddr(bool on = true); + virtual int32 SendTo(const void* buffer, size_t size, + const BNetAddress& to, int flags = 0); + virtual int32 SendTo(BNetBuffer& buffer, const BNetAddress& to, + int flags = 0); - /* - * LocalAddr() returns a BNetAddress corresponding to the local address used by this - * BNetEndpoint. RemoteAddr() returns a BNetAddress corresponding to the address of - * the remote peer we are connected to, if using a connected stream protocol. - */ - const BNetAddress & LocalAddr(); - const BNetAddress & RemoteAddr(); - - /* - * BNetEndpoint::Socket() returns the actual socket used for data communications. - */ - int Socket() const; - - /* - * BNetEndpoint::Close() - * - * Close the current BNetEndpoint, terminating any existing connection (if any) - * and discarding unread data. - */ + void SetTimeout(bigtime_t usec); - virtual void Close(); - - /* - * BNetEndpoint::Bind() - * - * Bind this BNetEndpoint to a local address. Calling bind() without specifying - * a BNetAddress or port number binds to a random local port. The actual port - * bound to can be determined by a subsequent call to BNetEndpoint::LocalAddr(). - * - * Returns B_OK if successful, B_ERROR otherwise. - */ - virtual status_t Bind(const BNetAddress &addr); - virtual status_t Bind(int port = 0); + virtual int32 Receive(void* buffer, size_t size, int flags = 0); + virtual int32 Receive(BNetBuffer& buffer, size_t size, int flags = 0); + virtual int32 ReceiveFrom(void* buffer, size_t size, BNetAddress& from, + int flags = 0); + virtual int32 ReceiveFrom(BNetBuffer& buffer, size_t size, + BNetAddress& from, int flags = 0); - /* - * BNetEndpoint::Connect() - * - * Connect this BNetEndpoint to a remote address. The first version takes - * a BNetAddress already set to the remote address as an argument; the other - * one internally constructs a BNetAddress from the passed-in hostname and port - * number. - * - * Returns B_OK if successful, B_ERROR otherwise. - */ - virtual status_t Connect(const BNetAddress &addr); - virtual status_t Connect(const char *addr, int port); - - /* - * BNetEndpoint::Listen() - * - * Specify the number of pending incoming connection attemps to queue. - * This is the number of connection requests that will be allowed in between - * calls to BNetEndpoint::accept(). When this number is exceeded, new - * connection attempts are refused until BNetEndpoint::accept() is called. - * - * Returns B_OK if successful, B_ERROR otherwise. - */ - - virtual status_t Listen(int backlog = 5); - - /* - * BNetEndpoint::Accept() - * - * Accepts new connections to a bound stream protocol BNetEndpoint. - * Blocks for timeout milleseconds (defaults to forever) waiting for - * new connections. Returns a BNetEndpoint with the same characteristics - * as the one calling BNetEndpoint::Accept(), the new one representing the new - * connection. This returned BNetEndpoint is ready for communication and - * must be deleted at a later time using delete. - */ - - virtual BNetEndpoint *Accept(int32 timeout = -1); + virtual bool IsDataPending(bigtime_t timeout = 0); - /* - * BNetEndpoint::Error() will return the integer error code (set by the OS) - * for the last send/recv error. Likewise, BNetEndpoint::ErrorStr() returns - * a string describing the error. - */ - int Error() const; - char *ErrorStr() const; + private: + virtual void _ReservedBNetEndpointFBCCruft1(); + virtual void _ReservedBNetEndpointFBCCruft2(); + virtual void _ReservedBNetEndpointFBCCruft3(); + virtual void _ReservedBNetEndpointFBCCruft4(); + virtual void _ReservedBNetEndpointFBCCruft5(); + virtual void _ReservedBNetEndpointFBCCruft6(); - /* - * BNetEndpoint::Send() - * - * Send data to the remote end of the connection. If not connected, fails. - * If using a datagram protocol, fails unless BNetEndpoint::Connect() - * has been called, which caches the address we are sending to. The first - * version sends an arbitrary buffer of size size; the second sends the - * contents of a BNetBuffer. Both return the number of bytes actually - * sent, or -1 on failure. - */ - virtual int32 Send(const void *buf, size_t size, int flags = 0); - virtual int32 Send(BNetBuffer &pack, int flags = 0); + status_t fInit; + int fSocket; + bigtime_t fTimeout; + int fLastError; + BNetAddress fAddr; + BNetAddress fPeer; - /* - * BNetEndpoint::SendTo() - * - * Send data to the address specified by the passed-in BNetAddress object. - * Fails if not using a datagram protocol. The first version sends - * an arbitrary buffer of size size; the second sends the contents of a - * BNetBuffer. Both return the number of bytes actually sent, - * and return -1 on failure. - */ - - virtual int32 SendTo(const void *buf, size_t size, const BNetAddress &to, int flags = 0); - virtual int32 SendTo(BNetBuffer &pack, const BNetAddress &to, int flags = 0); - - /* - * BNetEndpoint::SetTimeout() - * - * Sets an optional timeout (in microseconds) for calls to BNetEndpoint::Receive() - * and BNetEndpoint::ReceiveFrom(). Causes these calls to time out after the specified - * number of microseconds when in blocking mode. - */ - void SetTimeout(bigtime_t usec); - - /* - * BNetEndpoint::Receive() - * - * Receive data pending on the BNetEndpoint's connection. If in the default - * blocking mode, blocks until data is available or a timeout (set by - * BNetEndpoint::SetTimeout). The first call receives into a preallocated buffer - * of the specified size; the second call receives (at most) size bytes into - * an BNetBuffer. The data is appended to the end of the BNetBuffer; this allows - * a BNetBuffer to be used in a loop to buffer incoming data read in chunks - * until a certain size is read. Both return the number of bytes actually - * received, and return -1 on error. - */ - virtual int32 Receive(void *buf, size_t size, int flags = 0); - virtual int32 Receive(BNetBuffer &pack, size_t size, int flags = 0); - - /* - * BNetEndpoint::ReceiveFrom() - * - * Receive datagrams from an arbitrary source and fill in the passed-in BNetAddress - * with the address from whence the datagram came. As usual, both return the - * actual number of bytes read. In the BNetBuffer version, the data is appended - * to the end of the BNetBuffer; this allows a BNetBuffer to be used in a loop to - * buffer incoming data read in chunks until a certain amount is read. - */ - virtual int32 ReceiveFrom(void *buf, size_t size, BNetAddress &from, int flags = 0); - virtual int32 ReceiveFrom(BNetBuffer &pack, size_t size, BNetAddress &from, int flags = 0); - - /* - * BNetEndpoint::IsDataPending() - * - * Returns true if there is data waiting to be received on this BNetEndpoint. - * Will block for usec_timeout microseconds if specified, waiting for data. - */ - virtual bool IsDataPending(bigtime_t usec_timeout = 0); - - inline NLEndpoint *GetImpl() const; - -protected: - status_t m_init; - -private: - virtual void _ReservedBNetEndpointFBCCruft1(); - virtual void _ReservedBNetEndpointFBCCruft2(); - virtual void _ReservedBNetEndpointFBCCruft3(); - virtual void _ReservedBNetEndpointFBCCruft4(); - virtual void _ReservedBNetEndpointFBCCruft5(); - virtual void _ReservedBNetEndpointFBCCruft6(); - - int m_socket; - bigtime_t m_timeout; - int m_last_error; - BNetAddress m_addr; - BNetAddress m_peer; - BNetAddress m_conaddr; - - int32 __ReservedBNetEndpointFBCCruftData[6]; + int32 _reserved[18]; }; - -inline NLEndpoint *BNetEndpoint::GetImpl() const -{ - return NULL; // No Nettle backward compatibility -} - -#endif // _NETENDPOINT_H +#endif // H_NETENDPOINT diff --git a/headers/private/net/r5_compatibility.h b/headers/private/net/r5_compatibility.h new file mode 100644 index 0000000000..c005ddf194 --- /dev/null +++ b/headers/private/net/r5_compatibility.h @@ -0,0 +1,15 @@ +/* + * Copyright 2006, Haiku, Inc. All Rights Reserved. + * Distributed under the terms of the MIT License. + */ +#ifndef NET_R5_COMPATIBILITY_H +#define NET_R5_COMPATIBILITY_H + +// TODO: judging from R5 headers, this is all wrong, though (this has been taken from +// our old socket header) - we'll need to test this later on! +// Also, more problematic is that IPPROTO_* constants also don't seem to be compatible, too +#define R5_SOCK_DGRAM 10 +#define R5_SOCK_STREAM 11 +#define R5_SOCK_RAW 12 + +#endif // NET_R5_COMPATIBILITY_H diff --git a/src/kits/network/libnetapi/Jamfile b/src/kits/network/libnetapi/Jamfile index 7c9ca7ceb8..845a71703e 100644 --- a/src/kits/network/libnetapi/Jamfile +++ b/src/kits/network/libnetapi/Jamfile @@ -2,7 +2,7 @@ SubDir HAIKU_TOP src kits network libnetapi ; SetSubDirSupportedPlatformsBeOSCompatible ; -#UsePrivateHeaders net ; +UsePrivateHeaders net ; #UseHeaders [ FDirName $(HAIKU_TOP) headers legacy network ] : true ; if $(TARGET_PLATFORM) != haiku { diff --git a/src/kits/network/libnetapi/NetAddress.cpp b/src/kits/network/libnetapi/NetAddress.cpp index 7caf1f8aad..87d3f9ada7 100644 --- a/src/kits/network/libnetapi/NetAddress.cpp +++ b/src/kits/network/libnetapi/NetAddress.cpp @@ -1,183 +1,116 @@ -/*=--------------------------------------------------------------------------=* - * NetAddress.cpp -- Implementation of the BNetAddress class. +/* + * Copyright 2002-2006, Haiku, Inc. All Rights Reserved. + * Distributed under the terms of the MIT License. * - * Written by S.T. Mansfield (thephantom@mac.com) - * - * Remarks: - * * In all accessors, address and port are converted from network to - * host byte order. - * * In all mutators, address and port are converted from host to - * network byte order. - * * No trouts were harmed during the development of this class. - *=--------------------------------------------------------------------------=* - * Copyright (c) 2002, The OpenBeOS project. - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - *=--------------------------------------------------------------------------=* + * Authors: + * Scott T. Mansfield, thephantom@mac.com */ - -#include -#include -#include -#include -#include +/*! + NetAddress.cpp -- Implementation of the BNetAddress class. + Remarks: + * In all accessors, address and port are converted from network to + host byte order. + * In all mutators, address and port are converted from host to + network byte order. + * No trouts were harmed during the development of this class. +*/ #include #include +#include -/* - * AF_INET is 2 or 0 depending on whether or not you use the POSIX - * headers. Never never never compare by value, always use the - * #defined token. - */ -#define CTOR_INIT_LIST \ - BArchivable( ), \ - m_init( B_NO_INIT ), \ - fFamily( AF_INET ), \ - fPort( 0 ), \ - fAddress( INADDR_ANY ) +#include +#include +#include +#include +#include -/* BNetAddress - *=--------------------------------------------------------------------------=* - * Purpose: - * Class constructors that have a direct compliment with one of the SetTo - * methods. - * - * Input parameters: - * Varies, see the method signature description for the corresponding - * SetTo() method descriptor. - */ - -// This bad boy doubles up as the defalt ctor. -BNetAddress::BNetAddress( const char* hostname, unsigned short port ) - : CTOR_INIT_LIST +BNetAddress::BNetAddress(const char* hostname, unsigned short port) + : + fInit(B_NO_INIT) { - m_init = SetTo( hostname, port ); -} - -BNetAddress::BNetAddress( const struct sockaddr_in& sa ) - : CTOR_INIT_LIST -{ - m_init = SetTo( sa ); -} - -BNetAddress::BNetAddress( in_addr addr, int port ) - : CTOR_INIT_LIST -{ - m_init = SetTo( addr, port ); -} - -BNetAddress::BNetAddress( uint32 addr, int port ) - : CTOR_INIT_LIST -{ - m_init = SetTo( addr, port ); -} - -BNetAddress::BNetAddress( const char* hostname, const char* protocol, - const char* service ) - : CTOR_INIT_LIST -{ - m_init = SetTo( hostname, protocol, service ); + SetTo(hostname, port); } -/* BNetAddress - *=--------------------------------------------------------------------------=* - * Purpose: - * Class copy ctor. - * - * Input parameter: - * refparam: Instance to copy. - */ -BNetAddress::BNetAddress( const BNetAddress& refparam ) - : CTOR_INIT_LIST +BNetAddress::BNetAddress(const struct sockaddr_in& addr) + : + fInit(B_NO_INIT) { - m_init = clone( refparam ); + SetTo(addr); } -/* BNetAddress - *=--------------------------------------------------------------------------=* - * Purpose: - * Ctor to instantiate from serialized data (BMessage). - * - * Input parameter: - * archive : Serialized object to instantiate from. - */ -BNetAddress::BNetAddress( BMessage* archive ) - : CTOR_INIT_LIST +BNetAddress::BNetAddress(in_addr addr, int port) + : + fInit(B_NO_INIT) { - int8 int8_val; - int32 int32_val; + SetTo(addr, port); +} - if ( archive->FindInt8( "bnaddr_family", &int8_val ) != B_OK ) - { + +BNetAddress::BNetAddress(uint32 addr, int port) + : + fInit(B_NO_INIT) +{ + SetTo(addr, port); +} + + +BNetAddress::BNetAddress(const char* hostname, const char* protocol, + const char* service) + : + fInit(B_NO_INIT) +{ + SetTo(hostname, protocol, service); +} + + +BNetAddress::BNetAddress(const BNetAddress& other) +{ + *this = other; +} + + +BNetAddress::BNetAddress(BMessage* archive) +{ + int8 int8value; + if (archive->FindInt8("bnaddr_family", &int8value) != B_OK) return; - } - fFamily = int8_val; - if ( archive->FindInt8( "bnaddr_port", &int8_val ) != B_OK ) - { + fFamily = int8value; + + if (archive->FindInt8("bnaddr_port", &int8value) != B_OK) return; - } - fPort = int8_val; - if ( archive->FindInt32( "bnaddr_addr", &int32_val ) != B_OK ) - { + fPort = int8value; + + if (archive->FindInt32("bnaddr_addr", &fAddress) != B_OK) return; - } - fAddress = int32_val; - m_init = B_OK; + fInit = B_OK; } -/* operator= - *=--------------------------------------------------------------------------=* - * Purpose: - * Class' assignment operator. - * - * Input parameter: - * refparam : Instance to assign from. - */ -BNetAddress & BNetAddress::operator=( const BNetAddress& refparam ) +BNetAddress::~BNetAddress() { - clone(refparam); - // TODO: what do return on clone() failure!? +} + + +BNetAddress& +BNetAddress::operator=( const BNetAddress& other) +{ + fInit = other.fInit; + fFamily = other.fFamily; + fPort = other.fPort; + fAddress = other.fAddress; + return *this; } -/* ~BNetAddress - *=--------------------------------------------------------------------------=* - * Purpose: - * Class dtor. - */ -BNetAddress::~BNetAddress( void ) -{ - fFamily = fPort = fAddress = 0; - m_init = B_NO_INIT; -} - - /* GetAddr *=--------------------------------------------------------------------------=* * Purpose: @@ -208,30 +141,22 @@ BNetAddress::~BNetAddress( void ) * You can generally be safe using the MAXHOSTNAMELEN define, which * defaults to 64 bytes--don't forget to leave room for the NULL! */ -status_t BNetAddress::GetAddr( char* hostname, unsigned short* port ) const +status_t +BNetAddress::GetAddr(char* hostname, unsigned short* port) const { - if ( m_init != B_OK ) - { + if (fInit != B_OK) return B_NO_INIT; - } - char* hnBuff; - struct in_addr ia; + if (port != NULL) + *port = ntohs(fPort); - ia.s_addr = fAddress; + if (hostname != NULL) { + struct in_addr addr; + addr.s_addr = fAddress; - if ( port != NULL ) - { - *port = ( unsigned short )ntohs( fPort ); - } - - if ( hostname != NULL ) - { - hnBuff = inet_ntoa( ia ); - if ( hnBuff != NULL ) - { - strcpy( hostname, hnBuff ); - } + char* text = inet_ntoa(addr); + if (text != NULL) + strcpy(hostname, text); } return B_OK; @@ -257,7 +182,7 @@ status_t BNetAddress::GetAddr( char* hostname, unsigned short* port ) const */ status_t BNetAddress::GetAddr( struct sockaddr_in& sa ) const { - if ( m_init != B_OK ) + if ( fInit != B_OK ) { return B_NO_INIT; } @@ -288,7 +213,7 @@ status_t BNetAddress::GetAddr( struct sockaddr_in& sa ) const */ status_t BNetAddress::GetAddr( in_addr& addr, unsigned short* port ) const { - if ( m_init != B_OK ) + if ( fInit != B_OK ) { return B_NO_INIT; } @@ -314,7 +239,7 @@ status_t BNetAddress::GetAddr( in_addr& addr, unsigned short* port ) const */ status_t BNetAddress::InitCheck( void ) { - return ( m_init == B_OK ) ? B_OK : B_ERROR; + return ( fInit == B_OK ) ? B_OK : B_ERROR; } @@ -335,7 +260,7 @@ status_t BNetAddress::InitCheck( void ) */ status_t BNetAddress::Archive( BMessage* into, bool deep ) const { - if ( m_init != B_OK ) + if ( fInit != B_OK ) { return B_NO_INIT; } @@ -371,26 +296,22 @@ status_t BNetAddress::Archive( BMessage* into, bool deep ) const * NULL if a BNetAddress instance can not be initialized, otherwise * a new BNetAddress object instantiated from the BMessage parameter. */ -BArchivable* BNetAddress::Instantiate( BMessage* archive ) +BArchivable* +BNetAddress::Instantiate(BMessage* archive) { - if ( !validate_instantiation( archive, "BNetAddress" ) ) - { + if (!validate_instantiation(archive, "BNetAddress")) + return NULL; + + BNetAddress* address = new (std::nothrow) BNetAddress(archive); + if (address == NULL) + return NULL; + + if (address->InitCheck() != B_OK) { + delete address; return NULL; } - BNetAddress* bna = new BNetAddress( archive ); - if ( bna == NULL ) - { - return NULL; - } - - if ( bna->InitCheck( ) != B_OK ) - { - delete bna; - return NULL; - } - - return bna; + return address; } @@ -410,103 +331,87 @@ BArchivable* BNetAddress::Instantiate( BMessage* archive ) * Returns: * B_OK/B_ERROR for success/failure. */ -status_t BNetAddress::SetTo( const char* hostname, unsigned short port ) +status_t +BNetAddress::SetTo(const char* hostname, unsigned short port) { - struct hostent* HostEnt; - in_addr_t ia; + if (hostname == NULL) + return B_ERROR; - ia = INADDR_ANY; + in_addr_t addr = INADDR_ANY; - // Try like all git-out to set the address from the given hostname. - if ( hostname != NULL ) - { - // See if the string is an ASCII-fied IP address. - ia = inet_addr( hostname ); - if ( ia == INADDR_ANY || ia == ( unsigned long )-1 ) - { - // See if we can resolve the hostname to an IP address. - HostEnt = gethostbyname( hostname ); - if ( HostEnt != NULL ) - { - ia = *( int * )HostEnt->h_addr_list[0]; - } - else - { - return B_ERROR; - } - } - } + // Try like all git-out to set the address from the given hostname. - fFamily = AF_INET; - fPort = htons( port ); - fAddress = htonl( ia ); + // See if the string is an ASCII-fied IP address. + addr = inet_addr(hostname); + if (addr == INADDR_ANY || addr == (unsigned long)-1) { + // See if we can resolve the hostname to an IP address. + struct hostent* host = gethostbyname(hostname); + if (host != NULL) + addr = *(int*)host->h_addr_list[0]; + else + return B_ERROR; + } - return B_OK; + fFamily = AF_INET; + fPort = htons(port); + fAddress = htonl(addr); + + return fInit = B_OK; } -/* SetTo - *=--------------------------------------------------------------------------=* - * Purpose: - * Set from passed in socket/address info, our preferred mutator. - * - * Input parameter: - * sa : Data specifying the host/client connection. - * - * Returns: - * B_OK. - */ -status_t BNetAddress::SetTo( const struct sockaddr_in& sa ) -{ - fFamily = sa.sin_family; - fPort = htons( sa.sin_port ); - fAddress = htonl( sa.sin_addr.s_addr ); +/*! + Set from passed in sockaddr_in address. - return B_OK; + \param addr address + \return B_OK. +*/ +status_t +BNetAddress::SetTo(const struct sockaddr_in& addr) +{ + fFamily = addr.sin_family; + fPort = htons(addr.sin_port); + fAddress = htonl(addr.sin_addr.s_addr); + + return fInit = B_OK; } -/* SetTo - *=--------------------------------------------------------------------------=* - * Purpose: - * Set from passed in address and port. - * - * Input parameters: - * addr : IP address in network form. - * port : Optional port number. - * - * Returns: - * B_OK. - */ -status_t BNetAddress::SetTo( in_addr addr, int port ) -{ - fFamily = AF_INET; - fPort = htons( port ); - fAddress = htonl( addr.s_addr ); +/*! + Set from passed in address and port. - return B_OK; + \param addr IP address in network form. + \param port Optional port number. + + \return B_OK. +*/ +status_t +BNetAddress::SetTo(in_addr addr, int port) +{ + fFamily = AF_INET; + fPort = htons(port); + fAddress = htonl(addr.s_addr); + + return fInit = B_OK; } -/* SetTo - *=--------------------------------------------------------------------------=* - * Purpose: - * Set from passed in address and port. - * - * Input parameters: - * addr : Optional IP address in network form. - * port : Optional port number. - * - * Returns: - * B_OK. - */ -status_t BNetAddress::SetTo( uint32 addr, int port ) -{ - fFamily = AF_INET; - fPort = htons( port ); - fAddress = htonl( addr ); +/*! + Set from passed in address and port. - return B_OK; + \param addr IP address in network form. + \param port Optional port number. + + \return B_OK. +*/ +status_t +BNetAddress::SetTo(uint32 addr, int port) +{ + fFamily = AF_INET; + fPort = htons(port); + fAddress = htonl(addr); + + return fInit = B_OK; } @@ -536,71 +441,24 @@ status_t BNetAddress::SetTo( uint32 addr, int port ) * determine the port number (see getservbyname(3)). This method will * fail if the aforementioned precondition is not met. */ -status_t BNetAddress::SetTo( const char* hostname, const char* protocol, - const char* service ) +status_t +BNetAddress::SetTo(const char* hostname, const char* protocol, + const char* service) { - struct servent* ServiceEntry; - - ServiceEntry = getservbyname( service, protocol ); - if ( ServiceEntry == NULL ) - { + struct servent* serviceEntry = getservbyname(service, protocol); + if (serviceEntry == NULL) return B_ERROR; - } - // endservent( ); ??? - return SetTo( hostname, ServiceEntry->s_port ); + return SetTo(hostname, serviceEntry->s_port); } -/* clone - *=--------------------------------------------------------------------------=* - * Purpose: - * Private copy helper method. - * - * Input parameter: - * RefParam: Instance to clone. - * - * Returns: - * B_OK for success, B_NO_INIT if RefParam was not properly constructed. - */ -status_t BNetAddress::clone( const BNetAddress& RefParam ) -{ - if ( !RefParam.m_init ) - { - return B_NO_INIT; - } +// #pragma mark - FBC - fFamily = RefParam.fFamily; - fPort = RefParam.fPort; - fAddress = RefParam.fAddress; - return B_OK; -} - -/* Reserved methods, for future evolution */ - -void BNetAddress::_ReservedBNetAddressFBCCruft1() -{ -} - -void BNetAddress::_ReservedBNetAddressFBCCruft2() -{ -} - -void BNetAddress::_ReservedBNetAddressFBCCruft3() -{ -} - -void BNetAddress::_ReservedBNetAddressFBCCruft4() -{ -} - -void BNetAddress::_ReservedBNetAddressFBCCruft5() -{ -} - -void BNetAddress::_ReservedBNetAddressFBCCruft6() -{ -} - -/*=------------------------------------------------------------------- End -=*/ +void BNetAddress::_ReservedBNetAddressFBCCruft1() {} +void BNetAddress::_ReservedBNetAddressFBCCruft2() {} +void BNetAddress::_ReservedBNetAddressFBCCruft3() {} +void BNetAddress::_ReservedBNetAddressFBCCruft4() {} +void BNetAddress::_ReservedBNetAddressFBCCruft5() {} +void BNetAddress::_ReservedBNetAddressFBCCruft6() {} diff --git a/src/kits/network/libnetapi/NetBuffer.cpp b/src/kits/network/libnetapi/NetBuffer.cpp index 21c6cb7eea..003a2e1dfe 100644 --- a/src/kits/network/libnetapi/NetBuffer.cpp +++ b/src/kits/network/libnetapi/NetBuffer.cpp @@ -1,53 +1,36 @@ -/*=--------------------------------------------------------------------------=* - * NetBuffer.cpp -- Implementation of the BNetBuffer class. +/* + * Copyright 2002-2006, Haiku, Inc. All Rights Reserved. + * Distributed under the terms of the MIT License. * - * Written by S.T. Mansfield (thephantom@mac.com) - * - * Remarks: - * * This is basically a fancy-schmancy FIFO stack manager. Thusly, it is - * considered an error if data is not popped off a particular instance's - * stack in the order it was pushed on. - * * We could possibly implement this class as a thin wrapper around the - * BMessage class, but I'm not sure what kind of payload the latter class - * brings to the party, so we'll do our own stack management. We also - * cannot be sure that BMessage behaves as described above down the road. - * * Never use hot wax to soothe enraged lobsters. - *=--------------------------------------------------------------------------=* - * Copyright (c) 2002, The OpenBeOS project. - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - *=--------------------------------------------------------------------------=* + * Authors: + * Scott T. Mansfield, thephantom@mac.com */ +/*! + Remarks: + * This is basically a fancy-schmancy FIFO stack manager. Thusly, it is + considered an error if data is not popped off a particular instance's + stack in the order it was pushed on. + * We could possibly implement this class as a thin wrapper around the + BMessage class, but I'm not sure what kind of payload the latter class + brings to the party, so we'll do our own stack management. We also + cannot be sure that BMessage behaves as described above down the road. + * Never use hot wax to soothe enraged lobsters. +*/ -#include #include #include - #include +#include + + /* * Parametric ctor initialization list defaults. */ #define CTOR_INIT_LIST \ BArchivable( ), \ - m_init( B_NO_INIT ), \ + fInit( B_NO_INIT ), \ fData( NULL ), \ fDataSize( 0 ), \ fStackSize( 0 ), \ @@ -75,7 +58,7 @@ BNetBuffer::BNetBuffer( size_t size ) : CTOR_INIT_LIST { - m_init = ( resize( size ) == B_OK ) ? B_OK : B_NO_INIT; + fInit = ( resize( size ) == B_OK ) ? B_OK : B_NO_INIT; } @@ -90,7 +73,7 @@ BNetBuffer::BNetBuffer( size_t size ) BNetBuffer::BNetBuffer( const BNetBuffer& refparam ) : CTOR_INIT_LIST { - m_init = ( clone( refparam ) == B_OK ) ? B_OK : B_NO_INIT; + fInit = ( clone( refparam ) == B_OK ) ? B_OK : B_NO_INIT; } @@ -147,7 +130,7 @@ BNetBuffer::BNetBuffer( BMessage* archive ) fStackSize = stackSize; fCapacity = capacity; - m_init = B_OK; + fInit = B_OK; } @@ -166,7 +149,7 @@ BNetBuffer::~BNetBuffer( void ) fDataSize = fStackSize = fCapacity = 0; - m_init = B_NO_INIT; + fInit = B_NO_INIT; } @@ -180,7 +163,7 @@ BNetBuffer::~BNetBuffer( void ) */ BNetBuffer& BNetBuffer::operator=( const BNetBuffer& refparam ) { - m_init = clone( refparam ); + fInit = clone( refparam ); return *this; } @@ -196,7 +179,7 @@ BNetBuffer& BNetBuffer::operator=( const BNetBuffer& refparam ) */ status_t BNetBuffer::InitCheck( void ) { - return ( m_init == B_OK ) ? B_OK : B_ERROR; + return ( fInit == B_OK ) ? B_OK : B_ERROR; } @@ -217,7 +200,7 @@ status_t BNetBuffer::InitCheck( void ) */ status_t BNetBuffer::Archive( BMessage* into, bool deep ) const { - if ( m_init != B_OK ) + if ( fInit != B_OK ) { return B_NO_INIT; } @@ -592,7 +575,7 @@ size_t BNetBuffer::BytesRemaining( void ) const */ status_t BNetBuffer::clone( const BNetBuffer& RefParam ) { - if ( !RefParam.m_init ) + if ( !RefParam.fInit ) { return B_NO_INIT; } @@ -634,7 +617,7 @@ status_t BNetBuffer::clone( const BNetBuffer& RefParam ) */ status_t BNetBuffer::dpop( int32 Type, int32 Length, void* Data ) { - if ( m_init != B_OK ) + if ( fInit != B_OK ) { return B_NO_INIT; } @@ -750,7 +733,7 @@ status_t BNetBuffer::dpop( int32 Type, int32 Length, void* Data ) */ status_t BNetBuffer::dpush( int32 Type, int32 Length, const void* Data ) { - if ( m_init != B_OK ) + if ( fInit != B_OK ) { return B_NO_INIT; } diff --git a/src/kits/network/libnetapi/NetEndpoint.cpp b/src/kits/network/libnetapi/NetEndpoint.cpp index bc28ba07d2..8b8719b575 100644 --- a/src/kits/network/libnetapi/NetEndpoint.cpp +++ b/src/kits/network/libnetapi/NetEndpoint.cpp @@ -1,223 +1,277 @@ -/****************************************************************************** -/ -/ File: NetEndpoint.cpp -/ -/ Description: The Network API. -/ -/ Copyright 2002, OpenBeOS Project, All Rights Reserved. -/ -******************************************************************************/ +/* + * Copyright 2002-2006, Haiku, Inc. All Rights Reserved. + * Distributed under the terms of the MIT License. + */ -#include -#include -#include -#include -#include -#include -#include -#include -#include + +#include #include #include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include -BNetEndpoint::BNetEndpoint(int protocol) - : m_init(B_NO_INIT), m_socket(-1), m_timeout(B_INFINITE_TIMEOUT), m_last_error(0) + +static int +convert_r5_type(int type) { - if ((m_socket = socket(AF_INET, protocol, 0)) < 0) - m_last_error = errno; + // TODO: ideally, we should check if libnet.so has + // been loaded to activate this conversion. + // TODO: this is not enough, as the IPPROTO_* constants need to be translated as well + switch (type) { + case R5_SOCK_DGRAM: + return SOCK_DGRAM; + case R5_SOCK_STREAM: + return SOCK_STREAM; + case R5_SOCK_RAW: + return SOCK_RAW; + } + + return type; +} + + +// #pragma mark - BNetEndpoint + + +BNetEndpoint::BNetEndpoint(int type) + : + fInit(B_NO_INIT), + fSocket(-1), + fTimeout(B_INFINITE_TIMEOUT), + fLastError(0) +{ + // Maintain R5 compatibility + type = convert_r5_type(type); + + if ((fSocket = socket(AF_INET, type, 0)) < 0) + fLastError = errno; else - m_init = B_OK; + fInit = B_OK; } BNetEndpoint::BNetEndpoint(int family, int type, int protocol) - : m_init(B_NO_INIT), m_socket(-1), m_timeout(B_INFINITE_TIMEOUT), m_last_error(0) + : + fInit(B_NO_INIT), + fSocket(-1), + fTimeout(B_INFINITE_TIMEOUT), + fLastError(0) { - if ((m_socket = socket(family, type, protocol)) < 0) - m_last_error = errno; + // Maintain R5 compatibility + type = convert_r5_type(type); + + if ((fSocket = socket(family, type, protocol)) < 0) + fLastError = errno; else - m_init = B_OK; + fInit = B_OK; } -BNetEndpoint::BNetEndpoint(BMessage *archive) - : m_init(B_NO_INIT), - m_socket(-1), - m_timeout(B_INFINITE_TIMEOUT), - m_last_error(0) +BNetEndpoint::BNetEndpoint(BMessage* archive) + : + fInit(B_NO_INIT), + fSocket(-1), + fTimeout(B_INFINITE_TIMEOUT), + fLastError(0) { // TODO if (! archive) return; - + BMessage msg; - if (archive->FindMessage("bnendp_peer", &msg) != B_OK) return; - m_peer = BNetAddress(&msg); + fPeer = BNetAddress(&msg); if (archive->FindMessage("bnendp_addr", &msg) != B_OK) return; - m_addr = BNetAddress(&msg); - - if (archive->FindMessage("bnendp_conaddr", &msg) != B_OK) - return; - m_conaddr = BNetAddress(&msg); + fAddr = BNetAddress(&msg); - m_init = B_OK; + fInit = B_OK; } -BNetEndpoint::BNetEndpoint(const BNetEndpoint & ep) - : m_init(ep.m_init), m_timeout(ep.m_timeout), m_last_error(ep.m_last_error), - m_addr(ep.m_addr), m_peer(ep.m_peer) +BNetEndpoint::BNetEndpoint(const BNetEndpoint& endpoint) + : + fInit(endpoint.fInit), + fTimeout(endpoint.fTimeout), + fLastError(endpoint.fLastError), + fAddr(endpoint.fAddr), + fPeer(endpoint.fPeer) { - m_socket = -1; - if (ep.m_socket >= 0) - m_socket = dup(ep.m_socket); + fSocket = -1; + if (endpoint.fSocket >= 0) { + fSocket = dup(endpoint.fSocket); + if (fSocket < 0) { + fLastError = errno; + fInit = B_NO_INIT; + } + } } -BNetEndpoint & BNetEndpoint::operator=(const BNetEndpoint & ep) +BNetEndpoint& +BNetEndpoint::operator=(const BNetEndpoint& endpoint) { Close(); - m_init = ep.m_init; - m_timeout = ep.m_timeout; - m_last_error = ep.m_last_error; - m_addr = ep.m_addr; - m_peer = ep.m_peer; + fInit = endpoint.fInit; + fTimeout = endpoint.fTimeout; + fLastError = endpoint.fLastError; + fAddr = endpoint.fAddr; + fPeer = endpoint.fPeer; - m_socket = -1; - if (ep.m_socket >= 0) - m_socket = dup(ep.m_socket); - - if (m_socket >= 0) - m_init = B_OK; + fSocket = -1; + if (endpoint.fSocket >= 0) { + fSocket = dup(endpoint.fSocket); + if (fSocket < 0) { + fLastError = errno; + fInit = B_NO_INIT; + } + } return *this; } + BNetEndpoint::~BNetEndpoint() { Close(); } + // #pragma mark - -status_t BNetEndpoint::Archive(BMessage *into, bool deep) const + +status_t +BNetEndpoint::Archive(BMessage* into, bool deep) const { // TODO - - if( into == 0 ) + if (into == 0) return B_ERROR; - if ( m_init != B_OK ) + if (fInit != B_OK) return B_NO_INIT; - + BMessage msg; - - if (m_peer.Archive(&msg) != B_OK) + if (fPeer.Archive(&msg) != B_OK) return B_ERROR; if (into->AddMessage("bnendp_peer", &msg) != B_OK) return B_ERROR; - - if (m_addr.Archive(&msg) != B_OK) + + if (fAddr.Archive(&msg) != B_OK) return B_ERROR; if (into->AddMessage("bnendp_addr", &msg) != B_OK) return B_ERROR; - - if (m_conaddr.Archive(&msg) != B_OK) - return B_ERROR; - if (into->AddMessage("bnendp_conaddr", &msg) != B_OK) - return B_ERROR; - + return B_OK; } -BArchivable *BNetEndpoint::Instantiate(BMessage *archive) +BArchivable* +BNetEndpoint::Instantiate(BMessage* archive) { - if (! archive) + if (!archive) return NULL; - - if (! validate_instantiation(archive, "BNetAddress") ) + + if (!validate_instantiation(archive, "BNetAddress")) return NULL; - - BNetEndpoint *ep = new BNetEndpoint(archive); - if (ep && ep->InitCheck() == B_OK) - return ep; - - delete ep; + + BNetEndpoint* endpoint = new BNetEndpoint(archive); + if (endpoint && endpoint->InitCheck() == B_OK) + return endpoint; + + delete endpoint; return NULL; } + // #pragma mark - -status_t BNetEndpoint::InitCheck() +status_t +BNetEndpoint::InitCheck() { - return m_init; + return fInit; } -int BNetEndpoint::Socket() const +int +BNetEndpoint::Socket() const { - return m_socket; + return fSocket; } -const BNetAddress & BNetEndpoint::LocalAddr() +const BNetAddress& +BNetEndpoint::LocalAddr() { - return m_addr; + return fAddr; } -const BNetAddress & BNetEndpoint::RemoteAddr() +const BNetAddress& +BNetEndpoint::RemoteAddr() { - return m_peer; + return fPeer; } -status_t BNetEndpoint::SetProtocol(int protocol) +status_t +BNetEndpoint::SetProtocol(int protocol) { Close(); - if ((m_socket = socket(AF_INET, protocol, 0)) < 0) { - m_last_error = errno; - return m_last_error; + if ((fSocket = socket(AF_INET, protocol, 0)) < 0) { + fLastError = errno; + return fLastError; } - m_init = B_OK; - return m_init; + fInit = B_OK; + return fInit; } -int BNetEndpoint::SetOption(int32 option, int32 level, - const void * data, unsigned int length) +int +BNetEndpoint::SetOption(int32 option, int32 level, + const void* data, unsigned int length) { - if (m_socket < 0) + if (fSocket < 0) return B_ERROR; - if (setsockopt(m_socket, level, option, data, length) < 0) { - m_last_error = errno; + if (setsockopt(fSocket, level, option, data, length) < 0) { + fLastError = errno; return B_ERROR; } + return B_OK; } -int BNetEndpoint::SetNonBlocking(bool enable) +int +BNetEndpoint::SetNonBlocking(bool enable) { - int flags = fcntl(m_socket, F_GETFL); + int flags = fcntl(fSocket, F_GETFL); + if (flags < 0) { + fLastError = errno; + return B_ERROR; + } if (enable) flags |= O_NONBLOCK; else flags &= ~O_NONBLOCK; - if (fcntl(m_socket, F_SETFL, flags) < 0) { - m_last_error = errno; + if (fcntl(fSocket, F_SETFL, flags) < 0) { + fLastError = errno; return B_ERROR; } @@ -225,201 +279,215 @@ int BNetEndpoint::SetNonBlocking(bool enable) } -int BNetEndpoint::SetReuseAddr(bool enable) +int +BNetEndpoint::SetReuseAddr(bool enable) { int onoff = (int) enable; return SetOption(SO_REUSEADDR, SOL_SOCKET, &onoff, sizeof(onoff)); } -void BNetEndpoint::SetTimeout(bigtime_t timeout) +void +BNetEndpoint::SetTimeout(bigtime_t timeout) { - m_timeout = timeout; + fTimeout = timeout; } -int BNetEndpoint::Error() const +int +BNetEndpoint::Error() const { - return m_last_error; + return fLastError; } -char * BNetEndpoint::ErrorStr() const +char* +BNetEndpoint::ErrorStr() const { - return strerror(m_last_error); + return strerror(fLastError); } // #pragma mark - -void BNetEndpoint::Close() + +void +BNetEndpoint::Close() { - if (m_socket >= 0) - close(m_socket); - m_socket = -1; - m_init = B_NO_INIT; + if (fSocket >= 0) + close(fSocket); + + fSocket = -1; + fInit = B_NO_INIT; } -status_t BNetEndpoint::Bind(const BNetAddress & address) +status_t +BNetEndpoint::Bind(const BNetAddress& address) { struct sockaddr_in addr; - status_t status; - - status = address.GetAddr(addr); + status_t status = address.GetAddr(addr); if (status != B_OK) return status; - - if (bind(m_socket, (struct sockaddr *) &addr, sizeof(addr)) < 0) { - m_last_error = errno; - Close(); - return B_ERROR; - } - - int sz = sizeof(addr); - if (getsockname(m_socket, (struct sockaddr *) &addr, &sz) < 0) { - m_last_error = errno; + if (bind(fSocket, (struct sockaddr *)&addr, sizeof(addr)) < 0) { + fLastError = errno; + Close(); + return B_ERROR; + } + + socklen_t addrSize = sizeof(addr); + if (getsockname(fSocket, (struct sockaddr *)&addr, &addrSize) < 0) { + fLastError = errno; Close(); return B_ERROR; } -#ifdef _NETDB_H_ if (addr.sin_addr.s_addr == 0) { // Grrr, buggy getsockname! char hostname[MAXHOSTNAMELEN]; - struct hostent *he; gethostname(hostname, sizeof(hostname)); - he = gethostbyname(hostname); - if (he) - memcpy(&addr.sin_addr.s_addr, he->h_addr, sizeof(addr.sin_addr.s_addr)); + struct hostent *host = gethostbyname(hostname); + if (host != NULL) + memcpy(&addr.sin_addr.s_addr, host->h_addr, sizeof(addr.sin_addr.s_addr)); } -#endif - m_addr.SetTo(addr); + + fAddr.SetTo(addr); return B_OK; } -status_t BNetEndpoint::Bind(int port) +status_t +BNetEndpoint::Bind(int port) { BNetAddress addr(INADDR_ANY, port); return Bind(addr); } -status_t BNetEndpoint::Connect(const BNetAddress & address) +status_t +BNetEndpoint::Connect(const BNetAddress& address) { sockaddr_in addr; - if (address.GetAddr(addr) != B_OK) return B_ERROR; - - if (connect(m_socket, (sockaddr *) &addr, sizeof(addr)) < 0) { + + if (connect(fSocket, (sockaddr *) &addr, sizeof(addr)) < 0) { Close(); - m_last_error = errno; + fLastError = errno; return B_ERROR; } - int sz = sizeof(addr); - if (getpeername(m_socket, (sockaddr *) &addr, &sz) < 0) { + socklen_t addrSize = sizeof(addr); + if (getpeername(fSocket, (sockaddr *) &addr, &addrSize) < 0) { Close(); return B_ERROR; } - - m_peer.SetTo(addr); + + fPeer.SetTo(addr); return B_OK; } -status_t BNetEndpoint::Connect(const char *hostname, int port) +status_t +BNetEndpoint::Connect(const char *hostname, int port) { BNetAddress addr(hostname, port); return Connect(addr); } -status_t BNetEndpoint::Listen(int backlog) +status_t +BNetEndpoint::Listen(int backlog) { - if (listen(m_socket, backlog) < 0) { + if (listen(fSocket, backlog) < 0) { Close(); - m_last_error = errno; + fLastError = errno; return B_ERROR; } return B_OK; } -BNetEndpoint * BNetEndpoint::Accept(int32 timeout) +BNetEndpoint* +BNetEndpoint::Accept(int32 timeout) { - if (! IsDataPending(timeout < 0 ? B_INFINITE_TIMEOUT : 1000LL * timeout)) + if (!IsDataPending(timeout < 0 ? B_INFINITE_TIMEOUT : 1000LL * timeout)) return NULL; - + struct sockaddr_in addr; - int sz = sizeof(addr); + socklen_t addrSize = sizeof(addr); - int handle = accept(m_socket, (struct sockaddr *) &addr, &sz); - if (handle < 0) { + int socket = accept(fSocket, (struct sockaddr *) &addr, &addrSize); + if (socket < 0) { Close(); - m_last_error = errno; + fLastError = errno; return NULL; } - BNetEndpoint * ep = new BNetEndpoint(*this); - if (! ep) { - close(handle); + BNetEndpoint* endpoint = new (std::nothrow) BNetEndpoint(*this); + if (endpoint == NULL) { + close(socket); + fLastError = B_NO_MEMORY; return NULL; } - - ep->m_socket = handle; - ep->m_peer.SetTo(addr); - if (getsockname(handle, (struct sockaddr *) &addr, &sz) < 0) { - ep->Close(); - delete ep; + + endpoint->fSocket = socket; + endpoint->fPeer.SetTo(addr); + + if (getsockname(socket, (struct sockaddr *)&addr, &addrSize) < 0) { + endpoint->Close(); + delete endpoint; return NULL; } - - ep->m_addr.SetTo(addr); - return ep; + + endpoint->fAddr.SetTo(addr); + return endpoint; } + // #pragma mark - -bool BNetEndpoint::IsDataPending(bigtime_t timeout) + +bool +BNetEndpoint::IsDataPending(bigtime_t timeout) { + struct timeval tv; fd_set fds; - struct timeval tv, *tv_ptr = NULL; - + FD_ZERO(&fds); - FD_SET(m_socket, &fds); - + FD_SET(fSocket, &fds); + if (timeout > 0) { tv.tv_sec = timeout / 1000000; tv.tv_usec = (timeout % 1000000); - tv_ptr = &tv; } - if (select(m_socket + 1, &fds, NULL, NULL, &tv) < 0) { - m_last_error = errno; + if (select(fSocket + 1, &fds, NULL, NULL, timeout > 0 ? &tv : NULL) < 0) { + fLastError = errno; return false; } - - return FD_ISSET(m_socket, &fds); + + return FD_ISSET(fSocket, &fds); } -int32 BNetEndpoint::Receive(void * buffer, size_t length, int flags) +int32 +BNetEndpoint::Receive(void* buffer, size_t length, int flags) { - if (m_timeout >= 0 && IsDataPending(m_timeout) == false) + if (fTimeout >= 0 && IsDataPending(fTimeout) == false) return 0; - - ssize_t sz = recv(m_socket, buffer, length, flags); - if (sz < 0) - m_last_error = errno; - return sz; + + ssize_t bytesReceived = recv(fSocket, buffer, length, flags); + if (bytesReceived < 0) + fLastError = errno; + + return bytesReceived; } -int32 BNetEndpoint::Receive(BNetBuffer & buffer, size_t length, int flags) +int32 +BNetEndpoint::Receive(BNetBuffer& buffer, size_t length, int flags) { BNetBuffer chunk(length); length = Receive(chunk.Data(), length, flags); @@ -428,27 +496,30 @@ int32 BNetEndpoint::Receive(BNetBuffer & buffer, size_t length, int flags) } -int32 BNetEndpoint::ReceiveFrom(void * buffer, size_t length, - BNetAddress & address, int flags) +int32 +BNetEndpoint::ReceiveFrom(void* buffer, size_t length, + BNetAddress& address, int flags) { - if (m_timeout >= 0 && IsDataPending(m_timeout) == false) + if (fTimeout >= 0 && IsDataPending(fTimeout) == false) return 0; - + struct sockaddr_in addr; - int sz = sizeof(addr); - - length = recvfrom(m_socket, buffer, length, flags, - (struct sockaddr *) &addr, &sz); + socklen_t addrSize = sizeof(addr); + + length = recvfrom(fSocket, buffer, length, flags, + (struct sockaddr *)&addr, &addrSize); if (length < 0) - m_last_error = errno; + fLastError = errno; else address.SetTo(addr); + return length; } -int32 BNetEndpoint::ReceiveFrom(BNetBuffer & buffer, size_t length, - BNetAddress & address, int flags) +int32 +BNetEndpoint::ReceiveFrom(BNetBuffer& buffer, size_t length, + BNetAddress& address, int flags) { BNetBuffer chunk(length); length = ReceiveFrom(chunk.Data(), length, address, flags); @@ -457,74 +528,57 @@ int32 BNetEndpoint::ReceiveFrom(BNetBuffer & buffer, size_t length, } -int32 BNetEndpoint::Send(const void * buffer, size_t length, int flags) +int32 +BNetEndpoint::Send(const void* buffer, size_t length, int flags) { - ssize_t sz; + ssize_t bytesSent = send(fSocket, (const char *) buffer, length, flags); + if (bytesSent < 0) + fLastError = errno; - sz = send(m_socket, (const char *) buffer, length, flags); - if (sz < 0) - m_last_error = errno; - return sz; + return bytesSent; } -int32 BNetEndpoint::Send(BNetBuffer & buffer, int flags) +int32 +BNetEndpoint::Send(BNetBuffer& buffer, int flags) { return Send(buffer.Data(), buffer.Size(), flags); } -int32 BNetEndpoint::SendTo(const void * buffer, size_t length, - const BNetAddress & address, int flags) +int32 +BNetEndpoint::SendTo(const void* buffer, size_t length, + const BNetAddress& address, int flags) { - ssize_t sz; struct sockaddr_in addr; - if (address.GetAddr(addr) != B_OK) return B_ERROR; - - sz = sendto(m_socket, buffer, length, flags, - (struct sockaddr *) &addr, sizeof(addr)); - if (sz < 0) - m_last_error = errno; - return sz; + + ssize_t bytesSent = sendto(fSocket, buffer, length, flags, + (struct sockaddr *) &addr, sizeof(addr)); + if (bytesSent < 0) + fLastError = errno; + + return bytesSent; } -int32 BNetEndpoint::SendTo(BNetBuffer & buffer, - const BNetAddress & address, int flags) + +int32 +BNetEndpoint::SendTo(BNetBuffer& buffer, + const BNetAddress& address, int flags) { return SendTo(buffer.Data(), buffer.Size(), address, flags); } + // #pragma mark - + // These are virtuals, implemented for binary compatibility purpose -void BNetEndpoint::_ReservedBNetEndpointFBCCruft1() -{ -} - - -void BNetEndpoint::_ReservedBNetEndpointFBCCruft2() -{ -} - - -void BNetEndpoint::_ReservedBNetEndpointFBCCruft3() -{ -} - - -void BNetEndpoint::_ReservedBNetEndpointFBCCruft4() -{ -} - - -void BNetEndpoint::_ReservedBNetEndpointFBCCruft5() -{ -} - - -void BNetEndpoint::_ReservedBNetEndpointFBCCruft6() -{ -} +void BNetEndpoint::_ReservedBNetEndpointFBCCruft1() {} +void BNetEndpoint::_ReservedBNetEndpointFBCCruft2() {} +void BNetEndpoint::_ReservedBNetEndpointFBCCruft3() {} +void BNetEndpoint::_ReservedBNetEndpointFBCCruft4() {} +void BNetEndpoint::_ReservedBNetEndpointFBCCruft5() {} +void BNetEndpoint::_ReservedBNetEndpointFBCCruft6() {}