xref: /haiku/headers/os/net/NetworkAddress.h (revision 89d652d5e0defd9d095c778709cef82f5f10c357)
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 <NetworkAddressResolver.h>
16 #include <String.h>
17 
18 
19 class BNetworkAddress : public BArchivable {
20 public:
21 								BNetworkAddress();
22 								BNetworkAddress(const char* address,
23 									uint16 port = 0, uint32 flags = 0);
24 								BNetworkAddress(const char* address,
25 									const char* service, uint32 flags = 0);
26 								BNetworkAddress(int family, const char* address,
27 									uint16 port = 0, uint32 flags = 0);
28 								BNetworkAddress(int family, const char* address,
29 									const char* service, uint32 flags = 0);
30 								BNetworkAddress(const sockaddr& address);
31 								BNetworkAddress(
32 									const sockaddr_storage& address);
33 								BNetworkAddress(const sockaddr_in& address);
34 								BNetworkAddress(const sockaddr_in6& address);
35 								BNetworkAddress(const sockaddr_dl& address);
36 								BNetworkAddress(in_addr_t address,
37 									uint16 port = 0);
38 								BNetworkAddress(const in6_addr& address,
39 									uint16 port = 0);
40 								BNetworkAddress(const BNetworkAddress& other);
41 								BNetworkAddress(BMessage* archive);
42 	virtual						~BNetworkAddress();
43 
44 			status_t			InitCheck() const;
45 
46 			void				Unset();
47 
48 			status_t			SetTo(const char* address, uint16 port = 0,
49 									uint32 flags = 0);
50 			status_t			SetTo(const char* address, const char* service,
51 									uint32 flags = 0);
52 			status_t			SetTo(int family, const char* address,
53 									uint16 port = 0, uint32 flags = 0);
54 			status_t			SetTo(int family, const char* address,
55 									const char* service, uint32 flags = 0);
56 			void				SetTo(const sockaddr& address);
57 			void				SetTo(const sockaddr& address, size_t length);
58 			void				SetTo(const sockaddr_storage& address);
59 			void				SetTo(const sockaddr_in& address);
60 			void				SetTo(const sockaddr_in6& address);
61 			void				SetTo(const sockaddr_dl& address);
62 			void				SetTo(in_addr_t address, uint16 port = 0);
63 			void				SetTo(const in6_addr& address, uint16 port = 0);
64 			void				SetTo(const BNetworkAddress& other);
65 
66 			status_t			SetToBroadcast(int family, uint16 port = 0);
67 			status_t			SetToLocal();
68 			status_t			SetToLoopback();
69 			status_t			SetToMask(int family, uint32 prefixLength);
70 			status_t			SetToWildcard(int family, uint16 port = 0);
71 
72 			status_t			SetAddress(in_addr_t address);
73 			status_t			SetAddress(const in6_addr& address);
74 			void				SetPort(uint16 port);
75 			status_t			SetPort(const char* service);
76 
77 			void				SetToLinkLevel(uint8* address, size_t length);
78 			void				SetToLinkLevel(const char* name);
79 			void				SetToLinkLevel(uint32 index);
80 			void				SetLinkLevelIndex(uint32 index);
81 			void				SetLinkLevelType(uint32 type);
82 			void				SetLinkLevelFrameType(uint32 frameType);
83 
84 			int					Family() const;
85 			uint16				Port() const;
86 			size_t				Length() const;
87 			const sockaddr&		SockAddr() const;
88 			sockaddr&			SockAddr();
89 
90 			bool				IsEmpty() const;
91 			bool				IsWildcard() const;
92 			bool				IsBroadcast() const;
93 			bool				IsMulticast() const;
94 			bool				IsMulticastGlobal() const;
95 			bool				IsMulticastNodeLocal() const;
96 			bool				IsMulticastLinkLocal() const;
97 			bool				IsMulticastSiteLocal() const;
98 			bool				IsMulticastOrgLocal() const;
99 			bool				IsLinkLocal() const;
100 			bool				IsSiteLocal() const;
101 			bool				IsLocal() const;
102 
103 			ssize_t				PrefixLength() const;
104 
105 			uint32				LinkLevelIndex() const;
106 			BString				LinkLevelInterface() const;
107 			uint32				LinkLevelType() const;
108 			uint32				LinkLevelFrameType() const;
109 			uint8*				LinkLevelAddress() const;
110 			size_t				LinkLevelAddressLength() const;
111 
112 			status_t			ResolveForDestination(
113 									const BNetworkAddress& destination);
114 			status_t			ResolveTo(const BNetworkAddress& address);
115 
116 			BString				ToString(bool includePort = true) const;
117 			BString				HostName() const;
118 			BString				ServiceName() const;
119 
120 	virtual	status_t			Archive(BMessage* into, bool deep = true) const;
121 	static	BArchivable*		Instantiate(BMessage* archive);
122 
123 			bool				Equals(const BNetworkAddress& other,
124 									bool includePort = true) const;
125 
126 			BNetworkAddress&	operator=(const BNetworkAddress& other);
127 
128 			bool				operator==(const BNetworkAddress& other) const;
129 			bool				operator!=(const BNetworkAddress& other) const;
130 			bool				operator<(const BNetworkAddress& other) const;
131 
132 								operator const sockaddr*() const;
133 								operator const sockaddr&() const;
134 								operator const sockaddr*();
135 								operator sockaddr*();
136 								operator const sockaddr&();
137 								operator sockaddr&();
138 
139 private:
140 			sockaddr_storage	fAddress;
141 			status_t			fStatus;
142 };
143 
144 
145 #endif	// _NETWORK_ADDRESS_H
146