2 * Copyright 2006-2010, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
6 * Axel Dörfler, axeld@pinc-software.de
11 #include "interfaces.h"
13 #include "stack_private.h"
15 #include <net_device.h>
16 #include <NetUtilities.h>
19 #include <util/AutoLock.h>
21 #include <KernelExport.h>
23 #include <net/if_media.h>
26 #include <sys/sockio.h>
31 # define TRACE(x) dprintf x
36 #define ENABLE_DEBUGGER_COMMANDS 1
39 typedef DoublyLinkedList
<net_domain_private
> DomainList
;
41 static mutex sDomainLock
;
42 static DomainList sDomains
;
45 /*! Scans the domain list for the specified family.
46 You need to hold the sDomainLock when calling this function.
48 static net_domain_private
*
49 lookup_domain(int family
)
51 ASSERT_LOCKED_MUTEX(&sDomainLock
);
53 DomainList::Iterator iterator
= sDomains
.GetIterator();
54 while (net_domain_private
* domain
= iterator
.Next()) {
55 if (domain
->family
== family
)
63 #if ENABLE_DEBUGGER_COMMANDS
67 dump_domains(int argc
, char** argv
)
69 DomainList::Iterator iterator
= sDomains
.GetIterator();
70 while (net_domain_private
* domain
= iterator
.Next()) {
71 kprintf("domain: %p, %s, %d\n", domain
, domain
->name
, domain
->family
);
72 kprintf(" module: %p\n", domain
->module
);
73 kprintf(" address_module: %p\n", domain
->address_module
);
75 if (!domain
->routes
.IsEmpty())
76 kprintf(" routes:\n");
78 RouteList::Iterator routeIterator
= domain
->routes
.GetIterator();
79 while (net_route_private
* route
= routeIterator
.Next()) {
80 kprintf(" %p: dest %s, mask %s, gw %s, flags %" B_PRIx32
", "
81 "address %p\n", route
, AddressString(domain
, route
->destination
82 ? route
->destination
: NULL
).Data(),
83 AddressString(domain
, route
->mask
? route
->mask
: NULL
).Data(),
84 AddressString(domain
, route
->gateway
85 ? route
->gateway
: NULL
).Data(),
86 route
->flags
, route
->interface_address
);
89 if (!domain
->route_infos
.IsEmpty())
90 kprintf(" route infos:\n");
92 RouteInfoList::Iterator infoIterator
= domain
->route_infos
.GetIterator();
93 while (net_route_info
* info
= infoIterator
.Next()) {
94 kprintf(" %p\n", info
);
102 #endif // ENABLE_DEBUGGER_COMMANDS
108 /*! Gets the domain of the specified family.
111 get_domain(int family
)
113 MutexLocker
locker(sDomainLock
);
114 return lookup_domain(family
);
119 register_domain(int family
, const char* name
,
120 struct net_protocol_module_info
* module
,
121 struct net_address_module_info
* addressModule
,
122 net_domain
** _domain
)
124 TRACE(("register_domain(%d, %s)\n", family
, name
));
125 MutexLocker
locker(sDomainLock
);
127 struct net_domain_private
* domain
= lookup_domain(family
);
129 return B_NAME_IN_USE
;
131 domain
= new(std::nothrow
) net_domain_private
;
135 recursive_lock_init(&domain
->lock
, name
);
137 domain
->family
= family
;
139 domain
->module
= module
;
140 domain
->address_module
= addressModule
;
142 sDomains
.Add(domain
);
150 unregister_domain(net_domain
* _domain
)
152 TRACE(("unregister_domain(%p, %d, %s)\n", _domain
, _domain
->family
,
155 net_domain_private
* domain
= (net_domain_private
*)_domain
;
156 MutexLocker
locker(sDomainLock
);
158 sDomains
.Remove(domain
);
160 recursive_lock_destroy(&domain
->lock
);
169 mutex_init(&sDomainLock
, "net domains");
171 new (&sDomains
) DomainList
;
172 // static C++ objects are not initialized in the module startup
174 #if ENABLE_DEBUGGER_COMMANDS
175 add_debugger_command("net_domains", &dump_domains
,
176 "Dump network domains");
185 #if ENABLE_DEBUGGER_COMMANDS
186 remove_debugger_command("net_domains", &dump_domains
);
189 mutex_destroy(&sDomainLock
);