xref: /haiku/headers/os/net/NetworkAddress.h (revision ed24eb5ff12640d052171c6a7feba37fab8a75d1)
1 /*
2  * Copyright 2010-2011, 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 BFlattenable {
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 	virtual						~BNetworkAddress();
42 
43 			status_t			InitCheck() const;
44 
45 			void				Unset();
46 
47 			status_t			SetTo(const char* address, uint16 port = 0,
48 									uint32 flags = 0);
49 			status_t			SetTo(const char* address, const char* service,
50 									uint32 flags = 0);
51 			status_t			SetTo(int family, const char* address,
52 									uint16 port = 0, uint32 flags = 0);
53 			status_t			SetTo(int family, const char* address,
54 									const char* service, uint32 flags = 0);
55 			void				SetTo(const sockaddr& address);
56 			void				SetTo(const sockaddr& address, size_t length);
57 			void				SetTo(const sockaddr_storage& address);
58 			void				SetTo(const sockaddr_in& address);
59 			void				SetTo(const sockaddr_in6& address);
60 			void				SetTo(const sockaddr_dl& address);
61 			void				SetTo(in_addr_t address, uint16 port = 0);
62 			void				SetTo(const in6_addr& address, uint16 port = 0);
63 			void				SetTo(const BNetworkAddress& other);
64 
65 			status_t			SetToBroadcast(int family, uint16 port = 0);
66 			status_t			SetToLocal(int family = AF_UNSPEC,
67 									uint16 port = 0);
68 			status_t			SetToLoopback(int family = AF_UNSPEC,
69 									uint16 port = 0);
70 			status_t			SetToMask(int family, uint32 prefixLength);
71 			status_t			SetToWildcard(int family, uint16 port = 0);
72 
73 			status_t			SetAddress(in_addr_t address);
74 			status_t			SetAddress(const in6_addr& address);
75 			void				SetPort(uint16 port);
76 			status_t			SetPort(const char* service);
77 
78 			void				SetToLinkLevel(const uint8* address, size_t length);
79 			void				SetToLinkLevel(const char* name);
80 			void				SetToLinkLevel(uint32 index);
81 			void				SetLinkLevelIndex(uint32 index);
82 			void				SetLinkLevelType(uint8 type);
83 			void				SetLinkLevelFrameType(uint16 frameType);
84 
85 			int					Family() const;
86 			uint16				Port() const;
87 			size_t				Length() const;
88 			const sockaddr&		SockAddr() const;
89 			sockaddr&			SockAddr();
90 
91 			bool				IsEmpty() const;
92 			bool				IsWildcard() const;
93 			bool				IsBroadcast() const;
94 			bool				IsMulticast() const;
95 			bool				IsMulticastGlobal() const;
96 			bool				IsMulticastNodeLocal() const;
97 			bool				IsMulticastLinkLocal() const;
98 			bool				IsMulticastSiteLocal() const;
99 			bool				IsMulticastOrgLocal() const;
100 			bool				IsLinkLocal() const;
101 			bool				IsSiteLocal() const;
102 			bool				IsLocal() const;
103 
104 			ssize_t				PrefixLength() const;
105 
106 			uint32				LinkLevelIndex() const;
107 			BString				LinkLevelInterface() const;
108 			uint8				LinkLevelType() const;
109 			uint16				LinkLevelFrameType() const;
110 			uint8*				LinkLevelAddress() const;
111 			size_t				LinkLevelAddressLength() const;
112 
113 			status_t			ResolveForDestination(
114 									const BNetworkAddress& destination);
115 			status_t			ResolveTo(const BNetworkAddress& address);
116 
117 			BString				ToString(bool includePort = true) const;
118 			BString				HostName() const;
119 			BString				ServiceName() const;
120 
121 			bool				Equals(const BNetworkAddress& other,
122 									bool includePort = true) const;
123 
124 	// BFlattenable implementation
125 	virtual	bool				IsFixedSize() const;
126 	virtual	type_code			TypeCode() const;
127 	virtual	ssize_t				FlattenedSize() const;
128 
129 	virtual	status_t			Flatten(void* buffer, ssize_t size) const;
130 	virtual	status_t			Unflatten(type_code code, const void* buffer,
131 									ssize_t size);
132 
133 			BNetworkAddress&	operator=(const BNetworkAddress& other);
134 
135 			bool				operator==(const BNetworkAddress& other) const;
136 			bool				operator!=(const BNetworkAddress& other) const;
137 			bool				operator<(const BNetworkAddress& other) const;
138 
139 								operator const sockaddr*() const;
140 								operator const sockaddr&() const;
141 								operator const sockaddr*();
142 								operator sockaddr*();
143 								operator const sockaddr&();
144 								operator sockaddr&();
145 
146 private:
147 			status_t			_ParseLinkAddress(const char* address);
148 
149 private:
150 			sockaddr_storage	fAddress;
151 			status_t			fStatus;
152 			BString				fHostName;
153 };
154 
155 
156 #endif	// _NETWORK_ADDRESS_H
157