1c22d69bfSAxel Dörfler /* 2a3ec278aSAxel Dörfler * Copyright 2006-2010, Haiku, Inc. All Rights Reserved. 3c22d69bfSAxel Dörfler * Distributed under the terms of the MIT License. 4c22d69bfSAxel Dörfler * 5c22d69bfSAxel Dörfler * Authors: 6c22d69bfSAxel Dörfler * Axel Dörfler, axeld@pinc-software.de 7c22d69bfSAxel Dörfler */ 8c22d69bfSAxel Dörfler 9c22d69bfSAxel Dörfler 10c22d69bfSAxel Dörfler #include "domains.h" 11c22d69bfSAxel Dörfler #include "interfaces.h" 12409b1fc0SHugo Santos #include "utility.h" 13fb300cfdSHugo Santos #include "stack_private.h" 14c22d69bfSAxel Dörfler 159206bb37SAxel Dörfler #include <net_device.h> 16c22d69bfSAxel Dörfler 17c22d69bfSAxel Dörfler #include <lock.h> 18c22d69bfSAxel Dörfler #include <util/AutoLock.h> 19c22d69bfSAxel Dörfler 209206bb37SAxel Dörfler #include <KernelExport.h> 219206bb37SAxel Dörfler 229206bb37SAxel Dörfler #include <net/if_media.h> 23c22d69bfSAxel Dörfler #include <new> 24c22d69bfSAxel Dörfler #include <string.h> 25fb300cfdSHugo Santos #include <sys/sockio.h> 26c22d69bfSAxel Dörfler 27c22d69bfSAxel Dörfler 28c22d69bfSAxel Dörfler #define TRACE_DOMAINS 29c22d69bfSAxel Dörfler #ifdef TRACE_DOMAINS 30c22d69bfSAxel Dörfler # define TRACE(x) dprintf x 31c22d69bfSAxel Dörfler #else 32c22d69bfSAxel Dörfler # define TRACE(x) ; 33c22d69bfSAxel Dörfler #endif 34c22d69bfSAxel Dörfler 35a3ec278aSAxel Dörfler #define ENABLE_DEBUGGER_COMMANDS 1 36a3ec278aSAxel Dörfler 37a3ec278aSAxel Dörfler 38a3ec278aSAxel Dörfler typedef DoublyLinkedList<net_domain_private> DomainList; 39a3ec278aSAxel Dörfler 402b07b8e0SIngo Weinhold static mutex sDomainLock; 41a3ec278aSAxel Dörfler static DomainList sDomains; 42c22d69bfSAxel Dörfler 43c22d69bfSAxel Dörfler 4426153d0fSAxel Dörfler /*! Scans the domain list for the specified family. 45c22d69bfSAxel Dörfler You need to hold the sDomainLock when calling this function. 46c22d69bfSAxel Dörfler */ 47c22d69bfSAxel Dörfler static net_domain_private* 48c22d69bfSAxel Dörfler lookup_domain(int family) 49c22d69bfSAxel Dörfler { 50*61729d93SAxel Dörfler ASSERT_LOCKED_MUTEX(&sDomainLock); 51*61729d93SAxel Dörfler 52a3ec278aSAxel Dörfler DomainList::Iterator iterator = sDomains.GetIterator(); 53a3ec278aSAxel Dörfler while (net_domain_private* domain = iterator.Next()) { 54c22d69bfSAxel Dörfler if (domain->family == family) 55c22d69bfSAxel Dörfler return domain; 56c22d69bfSAxel Dörfler } 57c22d69bfSAxel Dörfler 58c22d69bfSAxel Dörfler return NULL; 59c22d69bfSAxel Dörfler } 60c22d69bfSAxel Dörfler 61c22d69bfSAxel Dörfler 62a3ec278aSAxel Dörfler #if ENABLE_DEBUGGER_COMMANDS 63a3ec278aSAxel Dörfler 64a3ec278aSAxel Dörfler 65a3ec278aSAxel Dörfler static int 66a3ec278aSAxel Dörfler dump_domains(int argc, char** argv) 67a3ec278aSAxel Dörfler { 68a3ec278aSAxel Dörfler DomainList::Iterator iterator = sDomains.GetIterator(); 69a3ec278aSAxel Dörfler while (net_domain_private* domain = iterator.Next()) { 70a3ec278aSAxel Dörfler kprintf("domain: %p, %s, %d\n", domain, domain->name, domain->family); 71a3ec278aSAxel Dörfler kprintf(" module: %p\n", domain->module); 72a3ec278aSAxel Dörfler kprintf(" address_module: %p\n", domain->address_module); 73a3ec278aSAxel Dörfler 74a3ec278aSAxel Dörfler if (!domain->routes.IsEmpty()) 75a3ec278aSAxel Dörfler kprintf(" routes:\n"); 76a3ec278aSAxel Dörfler 77a3ec278aSAxel Dörfler RouteList::Iterator routeIterator = domain->routes.GetIterator(); 78a3ec278aSAxel Dörfler while (net_route* route = routeIterator.Next()) { 79a3ec278aSAxel Dörfler kprintf(" %p\n", route); 80a3ec278aSAxel Dörfler } 81a3ec278aSAxel Dörfler 82a3ec278aSAxel Dörfler if (!domain->route_infos.IsEmpty()) 83a3ec278aSAxel Dörfler kprintf(" route infos:\n"); 84a3ec278aSAxel Dörfler 85a3ec278aSAxel Dörfler RouteInfoList::Iterator infoIterator = domain->route_infos.GetIterator(); 86a3ec278aSAxel Dörfler while (net_route_info* info = infoIterator.Next()) { 87a3ec278aSAxel Dörfler kprintf(" %p\n", info); 88a3ec278aSAxel Dörfler } 89a3ec278aSAxel Dörfler } 90a3ec278aSAxel Dörfler 91a3ec278aSAxel Dörfler return 0; 92a3ec278aSAxel Dörfler } 93a3ec278aSAxel Dörfler 94a3ec278aSAxel Dörfler 95a3ec278aSAxel Dörfler #endif // ENABLE_DEBUGGER_COMMANDS 96a3ec278aSAxel Dörfler 97a3ec278aSAxel Dörfler 98c22d69bfSAxel Dörfler // #pragma mark - 99c22d69bfSAxel Dörfler 100c22d69bfSAxel Dörfler 10126153d0fSAxel Dörfler /*! Gets the domain of the specified family. 102c22d69bfSAxel Dörfler */ 103c22d69bfSAxel Dörfler net_domain* 104c22d69bfSAxel Dörfler get_domain(int family) 105c22d69bfSAxel Dörfler { 1062b07b8e0SIngo Weinhold MutexLocker locker(sDomainLock); 107c22d69bfSAxel Dörfler return lookup_domain(family); 108c22d69bfSAxel Dörfler } 109c22d69bfSAxel Dörfler 110c22d69bfSAxel Dörfler 111c22d69bfSAxel Dörfler status_t 112c22d69bfSAxel Dörfler register_domain(int family, const char* name, 113c22d69bfSAxel Dörfler struct net_protocol_module_info* module, 114c22d69bfSAxel Dörfler struct net_address_module_info* addressModule, 115c22d69bfSAxel Dörfler net_domain** _domain) 116c22d69bfSAxel Dörfler { 117c22d69bfSAxel Dörfler TRACE(("register_domain(%d, %s)\n", family, name)); 1182b07b8e0SIngo Weinhold MutexLocker locker(sDomainLock); 119c22d69bfSAxel Dörfler 120c22d69bfSAxel Dörfler struct net_domain_private* domain = lookup_domain(family); 121c22d69bfSAxel Dörfler if (domain != NULL) 122c22d69bfSAxel Dörfler return B_NAME_IN_USE; 123c22d69bfSAxel Dörfler 124c22d69bfSAxel Dörfler domain = new(std::nothrow) net_domain_private; 125c22d69bfSAxel Dörfler if (domain == NULL) 126c22d69bfSAxel Dörfler return B_NO_MEMORY; 127c22d69bfSAxel Dörfler 12826153d0fSAxel Dörfler recursive_lock_init(&domain->lock, name); 129c22d69bfSAxel Dörfler 130c22d69bfSAxel Dörfler domain->family = family; 131c22d69bfSAxel Dörfler domain->name = name; 132c22d69bfSAxel Dörfler domain->module = module; 133c22d69bfSAxel Dörfler domain->address_module = addressModule; 134c22d69bfSAxel Dörfler 135a3ec278aSAxel Dörfler sDomains.Add(domain); 136c22d69bfSAxel Dörfler 137c22d69bfSAxel Dörfler *_domain = domain; 138c22d69bfSAxel Dörfler return B_OK; 139c22d69bfSAxel Dörfler } 140c22d69bfSAxel Dörfler 141c22d69bfSAxel Dörfler 142c22d69bfSAxel Dörfler status_t 143c22d69bfSAxel Dörfler unregister_domain(net_domain* _domain) 144c22d69bfSAxel Dörfler { 1453c13a5f5SAxel Dörfler TRACE(("unregister_domain(%p, %d, %s)\n", _domain, _domain->family, 1463c13a5f5SAxel Dörfler _domain->name)); 147c22d69bfSAxel Dörfler 148c22d69bfSAxel Dörfler net_domain_private* domain = (net_domain_private*)_domain; 1492b07b8e0SIngo Weinhold MutexLocker locker(sDomainLock); 150c22d69bfSAxel Dörfler 151a3ec278aSAxel Dörfler sDomains.Remove(domain); 152c22d69bfSAxel Dörfler 15326153d0fSAxel Dörfler recursive_lock_destroy(&domain->lock); 154c22d69bfSAxel Dörfler delete domain; 155c22d69bfSAxel Dörfler return B_OK; 156c22d69bfSAxel Dörfler } 157c22d69bfSAxel Dörfler 158c22d69bfSAxel Dörfler 159c22d69bfSAxel Dörfler status_t 160c22d69bfSAxel Dörfler init_domains() 161c22d69bfSAxel Dörfler { 1622b07b8e0SIngo Weinhold mutex_init(&sDomainLock, "net domains"); 163c22d69bfSAxel Dörfler 164a3ec278aSAxel Dörfler new (&sDomains) DomainList; 165a3ec278aSAxel Dörfler // static C++ objects are not initialized in the module startup 166a3ec278aSAxel Dörfler 167a3ec278aSAxel Dörfler #if ENABLE_DEBUGGER_COMMANDS 168a3ec278aSAxel Dörfler add_debugger_command("net_domains", &dump_domains, 169a3ec278aSAxel Dörfler "Dump network domains"); 170a3ec278aSAxel Dörfler #endif 171c22d69bfSAxel Dörfler return B_OK; 172c22d69bfSAxel Dörfler } 173c22d69bfSAxel Dörfler 174c22d69bfSAxel Dörfler 175c22d69bfSAxel Dörfler status_t 176c22d69bfSAxel Dörfler uninit_domains() 177c22d69bfSAxel Dörfler { 178a3ec278aSAxel Dörfler #if ENABLE_DEBUGGER_COMMANDS 179a3ec278aSAxel Dörfler remove_debugger_command("net_domains", &dump_domains); 180a3ec278aSAxel Dörfler #endif 181a3ec278aSAxel Dörfler 1822b07b8e0SIngo Weinhold mutex_destroy(&sDomainLock); 183c22d69bfSAxel Dörfler return B_OK; 184c22d69bfSAxel Dörfler } 185c22d69bfSAxel Dörfler 186