2 * Copyright 2006-2011, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
6 * Axel Dörfler, axeld@pinc-software.de
13 #include "stack_private.h"
15 #include <net_datalink.h>
16 #include <net_stack.h>
18 #include <Referenceable.h>
20 #include <util/AutoLock.h>
21 #include <util/DoublyLinkedList.h>
22 #include <util/OpenHashTable.h>
25 struct net_device_interface
;
28 // Additional address flags
29 #define IFAF_DIRECT_ADDRESS 0x1000
32 struct InterfaceAddress
: DoublyLinkedListLinkImpl
<InterfaceAddress
>,
33 net_interface_address
, BReferenceable
{
35 InterfaceAddress(net_interface
* interface
,
37 virtual ~InterfaceAddress();
39 status_t
SetTo(const ifaliasreq
& request
);
41 status_t
SetLocal(const sockaddr
* to
);
42 status_t
SetDestination(const sockaddr
* to
);
43 status_t
SetMask(const sockaddr
* to
);
45 sockaddr
** AddressFor(int32 option
);
47 void AddDefaultRoutes(int32 option
);
48 void RemoveDefaultRoutes(int32 option
);
50 bool LocalIsDefined() const;
52 InterfaceAddress
*& HashTableLink() { return fLink
; }
54 #if ENABLE_DEBUGGER_COMMANDS
55 void Dump(size_t index
= 0,
56 bool hideInterface
= false);
59 static status_t
Set(sockaddr
** _address
, const sockaddr
* to
);
60 static sockaddr
* Prepare(sockaddr
** _address
, size_t length
);
63 void _Init(net_interface
* interface
,
67 InterfaceAddress
* fLink
;
70 typedef DoublyLinkedList
<InterfaceAddress
> AddressList
;
72 struct domain_datalink
{
73 domain_datalink
* hash_link
;
76 struct net_datalink_protocol
* first_protocol
;
77 struct net_datalink_protocol_module_info
* first_info
;
79 // support for binding to an interface
80 net_route_private direct_route
;
81 InterfaceAddress direct_address
;
84 struct DatalinkHashDefinition
{
85 typedef const int KeyType
;
86 typedef domain_datalink ValueType
;
88 DatalinkHashDefinition()
92 size_t HashKey(const KeyType
& key
) const
97 size_t Hash(domain_datalink
* datalink
) const
99 return datalink
->domain
->family
;
102 bool Compare(const KeyType
& key
, domain_datalink
* datalink
) const
104 return datalink
->domain
->family
== key
;
107 domain_datalink
*& GetLink(domain_datalink
* datalink
) const
109 return datalink
->hash_link
;
113 typedef BOpenHashTable
<DatalinkHashDefinition
, true, true> DatalinkTable
;
116 class Interface
: public DoublyLinkedListLinkImpl
<Interface
>,
117 public net_interface
, public BReferenceable
{
119 Interface(const char* name
,
120 net_device_interface
* deviceInterface
);
121 virtual ~Interface();
123 InterfaceAddress
* FirstForFamily(int family
);
124 InterfaceAddress
* FirstUnconfiguredForFamily(int family
);
125 InterfaceAddress
* AddressForDestination(net_domain
* domain
,
126 const sockaddr
* destination
);
127 InterfaceAddress
* AddressForLocal(net_domain
* domain
,
128 const sockaddr
* local
);
130 status_t
AddAddress(InterfaceAddress
* address
);
131 void RemoveAddress(InterfaceAddress
* address
);
132 bool GetNextAddress(InterfaceAddress
** _address
);
133 InterfaceAddress
* AddressAt(size_t index
);
134 int32
IndexOfAddress(InterfaceAddress
* address
);
135 size_t CountAddresses();
136 void RemoveAddresses();
138 status_t
Control(net_domain
* domain
, int32 option
,
139 ifreq
& request
, ifreq
* userRequest
,
145 recursive_lock
& Lock() { return fLock
; }
147 net_device_interface
* DeviceInterface() { return fDeviceInterface
; }
149 status_t
CreateDomainDatalinkIfNeeded(
151 domain_datalink
* DomainDatalink(uint8 family
);
152 domain_datalink
* DomainDatalink(net_domain
* domain
)
153 { return DomainDatalink(domain
->family
); }
155 inline void SetBusy(bool busy
) { atomic_set(&fBusy
, busy
? 1 : 0); };
156 inline bool IsBusy() const { return atomic_get((int32
*)&fBusy
) == 1 ;};
158 #if ENABLE_DEBUGGER_COMMANDS
164 InterfaceAddress
* _FirstForFamily(int family
);
165 status_t
_ChangeAddress(RecursiveLocker
& locker
,
166 InterfaceAddress
* address
, int32 option
,
167 const sockaddr
* oldAddress
,
168 const sockaddr
* newAddress
);
171 recursive_lock fLock
;
173 net_device_interface
* fDeviceInterface
;
174 AddressList fAddresses
;
175 DatalinkTable fDatalinkTable
;
178 typedef DoublyLinkedList
<Interface
> InterfaceList
;
181 status_t
init_interfaces();
182 status_t
uninit_interfaces();
185 status_t
add_interface(const char* name
, net_domain_private
* domain
,
186 const ifaliasreq
& request
, net_device_interface
* deviceInterface
);
187 void remove_interface(Interface
* interface
);
188 void interface_removed_device_interface(net_device_interface
* deviceInterface
);
190 status_t
add_interface_address(Interface
* interface
, net_domain_private
* domain
,
191 const ifaliasreq
& request
);
192 status_t
update_interface_address(InterfaceAddress
* interfaceAddress
,
193 int32 option
, const sockaddr
* oldAddress
, const sockaddr
* newAddress
);
195 Interface
* get_interface(net_domain
* domain
, uint32 index
);
196 Interface
* get_interface(net_domain
* domain
, const char* name
);
197 Interface
* get_interface_for_device(net_domain
* domain
, uint32 index
);
198 Interface
* get_interface_for_link(net_domain
* domain
, const sockaddr
* address
);
199 InterfaceAddress
* get_interface_address(const struct sockaddr
* address
);
200 InterfaceAddress
* get_interface_address_for_destination(net_domain
* domain
,
201 const sockaddr
* destination
);
202 InterfaceAddress
* get_interface_address_for_link(net_domain
* domain
,
203 const sockaddr
* linkAddress
, bool unconfiguredOnly
);
205 uint32
count_interfaces();
206 status_t
list_interfaces(int family
, void* buffer
, size_t* _bufferSize
);
209 #endif // INTERFACES_H