Initial checkin of C++ BeOS Net API include files.

git-svn-id: file:///srv/svn/repos/haiku/trunk/current@918 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Philippe Houdoin 2002-08-28 21:25:08 +00:00
parent c5c7139355
commit 4c44e39a3a
4 changed files with 598 additions and 0 deletions

113
headers/os/net/NetAddress.h Normal file
View File

@ -0,0 +1,113 @@
//------------------------------------------------------------------------------
// Copyright (c) 2001-2002, OpenBeOS
//
// 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.
//------------------------------------------------------------------------------
#ifndef _NETADDRESS_H
#define _NETADDRESS_H
#include <BeBuild.h>
#include <SupportDefs.h>
#include <Archivable.h>
#include <sys/socket.h>
#include <netinet/in.h>
/*
* BNetAddress
*
* This class is used to represent network addresses, and provide access
* to a network address in a variety of formats. BNetAddress provides various ways
* to get and set a network address, converting to or from the chosen representation
* into a generic internal one.
*/
class NLAddress;
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);
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 &);
BNetAddress(const char *hostname, const char *protocol, const char *service);
BNetAddress &operator=(const BNetAddress &);
/*
* It is important to call InitCheck() after creating an instance of this class.
*/
status_t InitCheck();
/*
* BNetAddress::set() sets the internal address representation to refer
* to the internet address specified by the passed-in data.
* Returns true if successful, false otherwise.
*/
status_t SetTo(const char *hostname, const char *protocol, const char *service);
status_t SetTo(const char *hostname = 0, 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);
/*
* BNetAddress::get() converts the internal internet address representation
* into the specified form and returns it by filling in the passed-in parameters.
* Returns true if successful, false otherwise.
*/
status_t GetAddr(char *hostname = 0, unsigned short *port = 0) const;
status_t GetAddr(struct sockaddr_in &sa) const;
status_t GetAddr(in_addr &addr, unsigned short *port = 0) const;
inline NLAddress *GetImpl() 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();
int32 __ReservedBNetAddressFBCCruftData[9];
};
inline NLAddress *BNetAddress::GetImpl() const
{
return NULL; // No Nettle backward compatibility
}
#endif // _NETADDRESS_H

148
headers/os/net/NetBuffer.h Normal file
View File

@ -0,0 +1,148 @@
//------------------------------------------------------------------------------
// Copyright (c) 2001-2002, OpenBeOS
//
// 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.
//------------------------------------------------------------------------------
#ifndef _NETBUFFER_H
#define _NETBUFFER_H
#include <BeBuild.h>
#include <SupportDefs.h>
#include <Archivable.h>
#include <sys/socket.h>
/*
* BNetBuffer
*
* BNetBuffer is a dynamic buffer useful for storing data to be sent
* across the network. Data is inserted into and removed from
* the object using one of the many insert and remove member functions.
* Access to the raw stored data is possible. The BNetEndpoint class has a
* send and recv function for use with BNetBuffer. Network byte order conversion
* is done automatically for all appropriate integral types in the insert and remove
* functions for that type.
*
*/
class NLPacket;
class BNetBuffer : public BArchivable
{
public:
BNetBuffer(size_t size = 0);
virtual ~BNetBuffer();
/*
* It is important to call InitCheck() after creating an instance of this class.
*/
status_t InitCheck();
BNetBuffer(BMessage *archive);
virtual status_t Archive(BMessage *into, bool deep = true) const;
static BArchivable *Instantiate(BMessage *archive);
BNetBuffer(const BNetBuffer &);
BNetBuffer &operator=(const BNetBuffer &);
/*
* Data insertion member functions. Data is inserted at the end of the internal
* dynamic buffer. For the short, int, and long versions (and unsigned of
* each as well) byte order conversion is performed.
*
* The terminating null of all strings are inserted as
* well.
*
* Returns B_OK if successful, B_ERROR otherwise.
*/
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);
/*
* Data extraction member functions. Data is extracted from the start of the
* internal dynamic buffer. For the short, int, and long versions (and
* unsigned of each as well) byte order conversion is performed.
*
* Returns B_OK if successful, B_ERROR otherwise.
*/
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 &);
/*
* GetData() returns a pointer to the internal data buffer. This is useful
* when wanting to pass the data to a function expecting a pointer to data.
* GetSize() returns the size of the BNetPacket's data, in bytes.
* GetBytesRemaining() returns the bytes remaining in the BNetPacket available
* to be extracted via BNetPacket::Remove*().
*/
unsigned char *Data() const;
size_t Size() const;
size_t BytesRemaining() const;
inline NLPacket *GetImpl() const;
protected:
status_t m_init;
private:
virtual void _ReservedBNetBufferFBCCruft1();
virtual void _ReservedBNetBufferFBCCruft2();
virtual void _ReservedBNetBufferFBCCruft3();
virtual void _ReservedBNetBufferFBCCruft4();
virtual void _ReservedBNetBufferFBCCruft5();
virtual void _ReservedBNetBufferFBCCruft6();
int32 __ReservedBNetBufferFBCCruftData[9];
};
inline NLPacket *BNetBuffer::GetImpl() const
{
return NULL; // No Nettle backward compatibility
}
#endif // _NETBUFFER_H

58
headers/os/net/NetDebug.h Normal file
View File

@ -0,0 +1,58 @@
//------------------------------------------------------------------------------
// Copyright (c) 2001-2002, OpenBeOS
//
// 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.
//------------------------------------------------------------------------------
#ifndef _NETDEBUG_H
#define _NETDEBUG_H
/*
* Net debugging class
*
* Use this class to print informative messages and dump data
* to stderr and provide control over whether debug messages
* get printed or not.
*/
class BNetDebug
{
public:
/*
* turn debugging message output on or off
*/
static void Enable(bool);
/*
* test debugging message output state
*/
static bool IsEnabled();
/*
* print a debugging message
*/
static void Print(const char *msg);
/*
* dump raw data in a nice hd-like format
*/
static void Dump(const char *data, size_t size, const char *title);
};
#endif // _NETDEBUG_H

View File

@ -0,0 +1,279 @@
//------------------------------------------------------------------------------
// Copyright (c) 2001-2002, OpenBeOS
//
// 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.
//------------------------------------------------------------------------------
#ifndef _NETENDPOINT_H
#define _NETENDPOINT_H
#include <BeBuild.h>
#include <SupportDefs.h>
#include <Archivable.h>
#include <sys/socket.h>
#include <NetAddress.h>
#include <NetBuffer.h>
/*
* 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.
*
*/
// Nettle forward declarations
class NLEndpoint;
class NLAddress;
class NLPacket;
class BNetEndpoint : public BArchivable
{
public:
BNetEndpoint(int proto = SOCK_STREAM);
virtual ~BNetEndpoint();
/*
* It is important to call InitCheck() after creating an instance of this class.
*/
status_t InitCheck();
BNetEndpoint &operator=(const BNetEndpoint &);
BNetEndpoint(const BNetEndpoint &);
/*
* 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.
*/
BNetEndpoint(BMessage *archive);
virtual status_t Archive(BMessage *into, bool deep = true) const;
static BArchivable *Instantiate(BMessage *archive);
/*
* 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);
/*
* 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);
/*
* 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.
*/
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::getAddr().
*
* Returns B_OK if successful, B_ERROR otherwise.
*/
virtual status_t Bind(const BNetAddress &addr);
virtual status_t Bind(int port = 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);
/*
* 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;
/*
* 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);
/*
* 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();
int32 __ReservedBNetEndpointFBCCruftData[9];
BNetAddress m_peer;
BNetAddress m_addr;
BNetAddress m_conaddr;
};
inline NLEndpoint *BNetEndpoint::GetImpl() const
{
return NULL; // No Nettle backward compatibility
}
#endif // _NETENDPOINT_H