diff --git a/headers/os/net/NetworkAddress.h b/headers/os/net/NetworkAddress.h new file mode 100644 index 0000000000..68c8fcc9cc --- /dev/null +++ b/headers/os/net/NetworkAddress.h @@ -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 +#include +#include +#include + +#include + + +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 diff --git a/headers/os/net/NetworkInterface.h b/headers/os/net/NetworkInterface.h new file mode 100644 index 0000000000..715c14c2e9 --- /dev/null +++ b/headers/os/net/NetworkInterface.h @@ -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 +#include + +#include +#include + + +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 diff --git a/headers/os/net/NetworkRoster.h b/headers/os/net/NetworkRoster.h new file mode 100644 index 0000000000..c7ed01612a --- /dev/null +++ b/headers/os/net/NetworkRoster.h @@ -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 + + +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