BPicture: Fix archive constructor.
[haiku.git] / src / add-ons / kernel / network / stack / domains.cpp
blob0c581e9143b11f11627a2e2309e92a9c2de70770
1 /*
2 * Copyright 2006-2010, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Axel Dörfler, axeld@pinc-software.de
7 */
10 #include "domains.h"
11 #include "interfaces.h"
12 #include "utility.h"
13 #include "stack_private.h"
15 #include <net_device.h>
16 #include <NetUtilities.h>
18 #include <lock.h>
19 #include <util/AutoLock.h>
21 #include <KernelExport.h>
23 #include <net/if_media.h>
24 #include <new>
25 #include <string.h>
26 #include <sys/sockio.h>
29 #define TRACE_DOMAINS
30 #ifdef TRACE_DOMAINS
31 # define TRACE(x) dprintf x
32 #else
33 # define TRACE(x) ;
34 #endif
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)
56 return domain;
59 return NULL;
63 #if ENABLE_DEBUGGER_COMMANDS
66 static int
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);
98 return 0;
102 #endif // ENABLE_DEBUGGER_COMMANDS
105 // #pragma mark -
108 /*! Gets the domain of the specified family.
110 net_domain*
111 get_domain(int family)
113 MutexLocker locker(sDomainLock);
114 return lookup_domain(family);
118 status_t
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);
128 if (domain != NULL)
129 return B_NAME_IN_USE;
131 domain = new(std::nothrow) net_domain_private;
132 if (domain == NULL)
133 return B_NO_MEMORY;
135 recursive_lock_init(&domain->lock, name);
137 domain->family = family;
138 domain->name = name;
139 domain->module = module;
140 domain->address_module = addressModule;
142 sDomains.Add(domain);
144 *_domain = domain;
145 return B_OK;
149 status_t
150 unregister_domain(net_domain* _domain)
152 TRACE(("unregister_domain(%p, %d, %s)\n", _domain, _domain->family,
153 _domain->name));
155 net_domain_private* domain = (net_domain_private*)_domain;
156 MutexLocker locker(sDomainLock);
158 sDomains.Remove(domain);
160 recursive_lock_destroy(&domain->lock);
161 delete domain;
162 return B_OK;
166 status_t
167 init_domains()
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");
177 #endif
178 return B_OK;
182 status_t
183 uninit_domains()
185 #if ENABLE_DEBUGGER_COMMANDS
186 remove_debugger_command("net_domains", &dump_domains);
187 #endif
189 mutex_destroy(&sDomainLock);
190 return B_OK;