Author: axeld Date: 2010-08-06 10:08:02 +0200 (Fri, 06 Aug 2010) New Revision: 37936 Changeset: http://dev.haiku-os.org/changeset/37936 Added: haiku/trunk/headers/os/net/NetworkAddress.h haiku/trunk/headers/os/net/NetworkInterface.h haiku/trunk/headers/os/net/NetworkRoster.h Log: * Introduced first draft of a C++ network API. Comments welcome (I haven't started implementing it yet, anyway). * Note that BNetworkAddress is supposed to replace BNetAddress -- the latter does not provide enough space for a struct sockaddr_storage, and has a very IPv4 specific and incapable API, anyway. Added: haiku/trunk/headers/os/net/NetworkAddress.h =================================================================== --- haiku/trunk/headers/os/net/NetworkAddress.h (rev 0) +++ haiku/trunk/headers/os/net/NetworkAddress.h 2010-08-06 08:08:02 UTC (rev 37936) @@ -0,0 +1,113 @@ +/* + * Copyright 2010, Haiku, Inc. All Rights Reserved. + * Distributed under the terms of the MIT License. + */ +#ifndef _NETWORK_ADDRESS_H +#define _NETWORK_ADDRESS_H + + +#include <net/if_dl.h> +#include <netinet/in.h> +#include <netinet6/in6.h> +#include <sys/socket.h> + +#include <Archivable.h> + + +class BNetworkAddress : public BArchivable { +public: + BNetworkAddress(); + BNetworkAddress(int family, + const char* address, uint16 port = 0); + BNetworkAddress(const char* address, + uint16 port = 0); + BNetworkAddress(const sockaddr* address); + BNetworkAddress(const sockaddr_in* address); + BNetworkAddress(const sockaddr_in6* address); + BNetworkAddress(const sockaddr_dl* address); + BNetworkAddress(const in_addr_t address); + BNetworkAddress(const in6_addr* address); + BNetworkAddress(const BNetworkAddress& other); + BNetworkAddress(BMessage* archive); + virtual ~BNetworkAddress(); + + status_t InitCheck() const; + + void Unset(); + + status_t SetTo(int family, const char* address, + uint16 port = 0); + status_t SetTo(const char* address, uint16 port = 0); + void SetTo(const sockaddr* address); + void SetTo(const sockaddr_in* address); + void SetTo(const sockaddr_in6* address); + void SetTo(const sockaddr_dl* address); + void SetTo(const in_addr_t address); + void SetTo(const in6_addr* address); + void SetTo(const BNetworkAddress& other); + + status_t SetToBroadcast(int family, uint16 port = 0); + status_t SetToLocal(); + status_t SetToLoopback(); + status_t SetToMask(int family, uint32 prefixLength); + status_t SetToWildcard(int family); + void SetPort(uint16 port); + + void SetToLinkLevel(uint8* address, size_t length); + void SetToLinkLevel(const char* name); + void SetToLinkLevel(uint32 index); + void SetLinkLevelIndex(uint32 index); + void SetLinkLevelType(uint32 type); + void SetLinkLevelFrameType(uint32 frameType); + + int Family() const; + uint16 Port() const; + size_t Length() const; + + bool IsEmpty() const; + bool IsWildcard() const; + bool IsBroadcast() const; + bool IsMulticast() const; + bool IsMulticastGlobal() const; + bool IsMulticastNodeLocal() const; + bool IsMulticastLinkLocal() const; + bool IsMulticastSiteLocal() const; + bool IsMulticastOrgLocal() const; + bool IsLinkLocal() const; + bool IsSiteLocal() const; + bool IsLocal() const; + + uint32 LinkLevelIndex() const; + BString LinkLevelInterface() const; + uint32 LinkLevelType() const; + uint32 LinkLevelFrameType() const; + uint8* LinkLevelAddress() const; + size_t LinkLevelAddressLength() const; + + BNetworkAddress ResolvedForDestination( + const BNetworkAddress& destination) const; + void ResolveTo(const BNetworkAddress& address); + + BString ToString() const; + BString HostName() const; + BString PortName() const; + + virtual status_t Archive(BMessage* into, bool deep = true) const; + static BArchivable* Instantiate(BMessage* archive); + + BNetworkAddress& operator=(const BNetworkAddress& other); + + bool operator==(const BNetworkAddress& other) const; + bool operator!=(const BNetworkAddress& other) const; + bool operator<(const BNetworkAddress& other) const; + + operator sockaddr*() const; + operator sockaddr&() const; + +private: + sockaddr_storage fAddress; + status_t fStatus; +}; + + +#endif // _NETWORK_ADDRESS_H Added: haiku/trunk/headers/os/net/NetworkInterface.h =================================================================== --- haiku/trunk/headers/os/net/NetworkInterface.h (rev 0) +++ haiku/trunk/headers/os/net/NetworkInterface.h 2010-08-06 08:08:02 UTC (rev 37936) @@ -0,0 +1,86 @@ +/* + * Copyright 2010, Haiku, Inc. All Rights Reserved. + * Distributed under the terms of the MIT License. + */ +#ifndef _NETWORK_INTERFACE_H +#define _NETWORK_INTERFACE_H + + +#include <net/if.h> +#include <net/if_types.h> + +#include <List.h> +#include <NetworkAddress.h> + + +class BNetworkInterface; + + +class BNetworkInterfaceAddress { +public: + BNetworkInterfaceAddress(); + ~BNetworkInterfaceAddress(); + + void SetAddress(BNetworkAddress& address); + void SetMask(BNetworkAddress& mask); + void SetBroadcast(BNetworkAddress& mask); + + BNetworkAddress& Address() { return fAddress; } + BNetworkAddress& Mask() { return fMask; } + BNetworkAddress& Broadcast() { return fBroadcast; } + + const BNetworkAddress& Address() const { return fAddress; } + const BNetworkAddress& Mask() const { return fMask; } + const BNetworkAddress& Broadcast() const { return fBroadcast; } + + void SetFlags(uint32 flags); + uint32 Flags() const { return fFlags; } + +private: + BNetworkInterface* fInterface; + uint32 fIndex; + BNetworkAddress fAddress; + BNetworkAddress fMask; + BNetworkAddress fBroadcast; + uint32 fFlags; +}; + + +class BNetworkInterface { +public: + BNetworkInterface(); + BNetworkInterface(const char* name); + BNetworkInterface(uint32 index); + ~BNetworkInterface(); + + bool Exists() const; + + const char* Name() const; + uint32 Flags() const; + uint32 MTU() const; + uint32 Type() const; + status_t GetStats(ifreq_stats& stats); + bool HasLink() const; + + status_t SetFlags(uint32 flags); + status_t SetMTU(uint32 mtu); + + uint32 CountAddresses() const; + BNetworkInterfaceAddress* AddressAt(uint32 index); + + status_t AddAddress( + const BNetworkInterfaceAddress& address); + status_t RemoveAddress( + const BNetworkInterfaceAddress& address); + status_t RemoveAddressAt(uint32 index); + + status_t GetHardwareAddress(BNetworkAddress& address); + +private: + char fName[IF_NAMESIZE]; + uint32 fIndex; + BList fAddresses; +}; + + +#endif // _NETWORK_INTERFACE_H Added: haiku/trunk/headers/os/net/NetworkRoster.h =================================================================== --- haiku/trunk/headers/os/net/NetworkRoster.h (rev 0) +++ haiku/trunk/headers/os/net/NetworkRoster.h 2010-08-06 08:08:02 UTC (rev 37936) @@ -0,0 +1,39 @@ +/* + * Copyright 2010, Haiku, Inc. All Rights Reserved. + * Distributed under the terms of the MIT License. + */ +#ifndef _NETWORK_ROSTER_H +#define _NETWORK_ROSTER_H + + +#include <SupportDefs.h> + + +class BMessenger; +class BNetworkInterface; + + +class BNetworkRoster { +public: + BNetworkRoster& Default(); + + uint32 CountInterfaces(); + BNetworkInterface* InterfaceAt(uint32 index); + + status_t AddInterface( + const BNetworkInterface& interface); + status_t RemoveInterface( + const BNetworkInterface& interface); + status_t RemoveInterfaceAt(uint32 index); + + status_t StartWatching(const BMessenger& target, + uint32 eventMask); + void StopWatching(const BMessenger& target); + +private: + BNetworkRoster(); + ~BNetworkRoster(); +}; + + +#endif // _NETWORK_ROSTER_H