xref: /haiku/headers/os/net/NetworkAddress.h (revision e7d5c75dce28921de0dc981ed840205a67a0c0e5)
1 /*
2  * Copyright 2010, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef _NETWORK_ADDRESS_H
6 #define _NETWORK_ADDRESS_H
7 
8 
9 #include <net/if_dl.h>
10 #include <netinet/in.h>
11 #include <netinet6/in6.h>
12 #include <sys/socket.h>
13 
14 #include <Archivable.h>
15 #include <String.h>
16 
17 
18 class BNetworkAddress : public BArchivable {
19 public:
20 								BNetworkAddress();
21 								BNetworkAddress(int family,
22 									const char* address, uint16 port = 0);
23 								BNetworkAddress(const char* address,
24 									uint16 port = 0);
25 								BNetworkAddress(const sockaddr& address);
26 								BNetworkAddress(
27 									const sockaddr_storage& address);
28 								BNetworkAddress(const sockaddr_in& address);
29 								BNetworkAddress(const sockaddr_in6& address);
30 								BNetworkAddress(const sockaddr_dl& address);
31 								BNetworkAddress(const in_addr_t address);
32 								BNetworkAddress(const in6_addr* address);
33 								BNetworkAddress(const BNetworkAddress& other);
34 								BNetworkAddress(BMessage* archive);
35 	virtual						~BNetworkAddress();
36 
37 			status_t			InitCheck() const;
38 
39 			void				Unset();
40 
41 			status_t			SetTo(int family, const char* address,
42 									uint16 port = 0);
43 			status_t			SetTo(const char* address, uint16 port = 0);
44 			void				SetTo(const sockaddr& address);
45 			void				SetTo(const sockaddr_storage& address);
46 			void				SetTo(const sockaddr_in& address);
47 			void				SetTo(const sockaddr_in6& address);
48 			void				SetTo(const sockaddr_dl& address);
49 			void				SetTo(const in_addr_t address);
50 			void				SetTo(const in6_addr* address);
51 			void				SetTo(const BNetworkAddress& other);
52 
53 			status_t			SetToBroadcast(int family, uint16 port = 0);
54 			status_t			SetToLocal();
55 			status_t			SetToLoopback();
56 			status_t			SetToMask(int family, uint32 prefixLength);
57 			status_t			SetToWildcard(int family);
58 			void				SetPort(uint16 port);
59 
60 			void				SetToLinkLevel(uint8* address, size_t length);
61 			void				SetToLinkLevel(const char* name);
62 			void				SetToLinkLevel(uint32 index);
63 			void				SetLinkLevelIndex(uint32 index);
64 			void				SetLinkLevelType(uint32 type);
65 			void				SetLinkLevelFrameType(uint32 frameType);
66 
67 			int					Family() const;
68 			uint16				Port() const;
69 			size_t				Length() const;
70 			const sockaddr&		SockAddr() const;
71 
72 			bool				IsEmpty() const;
73 			bool				IsWildcard() const;
74 			bool				IsBroadcast() const;
75 			bool				IsMulticast() const;
76 			bool				IsMulticastGlobal() const;
77 			bool				IsMulticastNodeLocal() const;
78 			bool				IsMulticastLinkLocal() const;
79 			bool				IsMulticastSiteLocal() const;
80 			bool				IsMulticastOrgLocal() const;
81 			bool				IsLinkLocal() const;
82 			bool				IsSiteLocal() const;
83 			bool				IsLocal() const;
84 
85 			uint32				LinkLevelIndex() const;
86 			BString				LinkLevelInterface() const;
87 			uint32				LinkLevelType() const;
88 			uint32				LinkLevelFrameType() const;
89 			uint8*				LinkLevelAddress() const;
90 			size_t				LinkLevelAddressLength() const;
91 
92 			status_t			ResolveForDestination(
93 									const BNetworkAddress& destination);
94 			status_t			ResolveTo(const BNetworkAddress& address);
95 
96 			BString				ToString(bool includePort = true) const;
97 			BString				HostName() const;
98 			BString				PortName() const;
99 
100 	virtual	status_t			Archive(BMessage* into, bool deep = true) const;
101 	static	BArchivable*		Instantiate(BMessage* archive);
102 
103 			bool				Equals(const BNetworkAddress& other,
104 									bool includePort = true) const;
105 
106 			BNetworkAddress&	operator=(const BNetworkAddress& other);
107 
108 			bool				operator==(const BNetworkAddress& other) const;
109 			bool				operator!=(const BNetworkAddress& other) const;
110 			bool				operator<(const BNetworkAddress& other) const;
111 
112 								operator sockaddr*() const;
113 								operator sockaddr&() const;
114 
115 private:
116 			sockaddr_storage	fAddress;
117 			status_t			fStatus;
118 };
119 
120 
121 #endif	// _NETWORK_ADDRESS_H
122