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> 16*8ccc01cfSAxel Dörfler #include <NetUtilities.h> 17c22d69bfSAxel Dörfler 18c22d69bfSAxel Dörfler #include <lock.h> 19c22d69bfSAxel Dörfler #include <util/AutoLock.h> 20c22d69bfSAxel Dörfler 219206bb37SAxel Dörfler #include <KernelExport.h> 229206bb37SAxel Dörfler 239206bb37SAxel Dörfler #include <net/if_media.h> 24c22d69bfSAxel Dörfler #include <new> 25c22d69bfSAxel Dörfler #include <string.h> 26fb300cfdSHugo Santos #include <sys/sockio.h> 27c22d69bfSAxel Dörfler 28c22d69bfSAxel Dörfler 29c22d69bfSAxel Dörfler #define TRACE_DOMAINS 30c22d69bfSAxel Dörfler #ifdef TRACE_DOMAINS 31c22d69bfSAxel Dörfler # define TRACE(x) dprintf x 32c22d69bfSAxel Dörfler #else 33c22d69bfSAxel Dörfler # define TRACE(x) ; 34c22d69bfSAxel Dörfler #endif 35c22d69bfSAxel Dörfler 36a3ec278aSAxel Dörfler #define ENABLE_DEBUGGER_COMMANDS 1 37a3ec278aSAxel Dörfler 38a3ec278aSAxel Dörfler 39a3ec278aSAxel Dörfler typedef DoublyLinkedList<net_domain_private> DomainList; 40a3ec278aSAxel Dörfler 412b07b8e0SIngo Weinhold static mutex sDomainLock; 42a3ec278aSAxel Dörfler static DomainList sDomains; 43c22d69bfSAxel Dörfler 44c22d69bfSAxel Dörfler 4526153d0fSAxel Dörfler /*! Scans the domain list for the specified family. 46c22d69bfSAxel Dörfler You need to hold the sDomainLock when calling this function. 47c22d69bfSAxel Dörfler */ 48c22d69bfSAxel Dörfler static net_domain_private* 49c22d69bfSAxel Dörfler lookup_domain(int family) 50c22d69bfSAxel Dörfler { 5161729d93SAxel Dörfler ASSERT_LOCKED_MUTEX(&sDomainLock); 5261729d93SAxel Dörfler 53a3ec278aSAxel Dörfler DomainList::Iterator iterator = sDomains.GetIterator(); 54a3ec278aSAxel Dörfler while (net_domain_private* domain = iterator.Next()) { 55c22d69bfSAxel Dörfler if (domain->family == family) 56c22d69bfSAxel Dörfler return domain; 57c22d69bfSAxel Dörfler } 58c22d69bfSAxel Dörfler 59c22d69bfSAxel Dörfler return NULL; 60c22d69bfSAxel Dörfler } 61c22d69bfSAxel Dörfler 62c22d69bfSAxel Dörfler 63a3ec278aSAxel Dörfler #if ENABLE_DEBUGGER_COMMANDS 64a3ec278aSAxel Dörfler 65a3ec278aSAxel Dörfler 66a3ec278aSAxel Dörfler static int 67a3ec278aSAxel Dörfler dump_domains(int argc, char** argv) 68a3ec278aSAxel Dörfler { 69a3ec278aSAxel Dörfler DomainList::Iterator iterator = sDomains.GetIterator(); 70a3ec278aSAxel Dörfler while (net_domain_private* domain = iterator.Next()) { 71a3ec278aSAxel Dörfler kprintf("domain: %p, %s, %d\n", domain, domain->name, domain->family); 72a3ec278aSAxel Dörfler kprintf(" module: %p\n", domain->module); 73a3ec278aSAxel Dörfler kprintf(" address_module: %p\n", domain->address_module); 74a3ec278aSAxel Dörfler 75a3ec278aSAxel Dörfler if (!domain->routes.IsEmpty()) 76a3ec278aSAxel Dörfler kprintf(" routes:\n"); 77a3ec278aSAxel Dörfler 78a3ec278aSAxel Dörfler RouteList::Iterator routeIterator = domain->routes.GetIterator(); 79*8ccc01cfSAxel Dörfler while (net_route_private* route = routeIterator.Next()) { 80*8ccc01cfSAxel Dörfler kprintf(" %p: dest %s, mask %s, gw %s, flags %lx, address %p\n", 81*8ccc01cfSAxel Dörfler route, AddressString(domain, route->destination 82*8ccc01cfSAxel Dörfler ? route->destination : NULL).Data(), 83*8ccc01cfSAxel Dörfler AddressString(domain, route->mask ? route->mask : NULL).Data(), 84*8ccc01cfSAxel Dörfler AddressString(domain, route->gateway 85*8ccc01cfSAxel Dörfler ? route->gateway : NULL).Data(), 86*8ccc01cfSAxel Dörfler route->flags, route->interface_address); 87a3ec278aSAxel Dörfler } 88a3ec278aSAxel Dörfler 89a3ec278aSAxel Dörfler if (!domain->route_infos.IsEmpty()) 90a3ec278aSAxel Dörfler kprintf(" route infos:\n"); 91a3ec278aSAxel Dörfler 92a3ec278aSAxel Dörfler RouteInfoList::Iterator infoIterator = domain->route_infos.GetIterator(); 93a3ec278aSAxel Dörfler while (net_route_info* info = infoIterator.Next()) { 94a3ec278aSAxel Dörfler kprintf(" %p\n", info); 95a3ec278aSAxel Dörfler } 96a3ec278aSAxel Dörfler } 97a3ec278aSAxel Dörfler 98a3ec278aSAxel Dörfler return 0; 99a3ec278aSAxel Dörfler } 100a3ec278aSAxel Dörfler 101a3ec278aSAxel Dörfler 102a3ec278aSAxel Dörfler #endif // ENABLE_DEBUGGER_COMMANDS 103a3ec278aSAxel Dörfler 104a3ec278aSAxel Dörfler 105c22d69bfSAxel Dörfler // #pragma mark - 106c22d69bfSAxel Dörfler 107c22d69bfSAxel Dörfler 10826153d0fSAxel Dörfler /*! Gets the domain of the specified family. 109c22d69bfSAxel Dörfler */ 110c22d69bfSAxel Dörfler net_domain* 111c22d69bfSAxel Dörfler get_domain(int family) 112c22d69bfSAxel Dörfler { 1132b07b8e0SIngo Weinhold MutexLocker locker(sDomainLock); 114c22d69bfSAxel Dörfler return lookup_domain(family); 115c22d69bfSAxel Dörfler } 116c22d69bfSAxel Dörfler 117c22d69bfSAxel Dörfler 118c22d69bfSAxel Dörfler status_t 119c22d69bfSAxel Dörfler register_domain(int family, const char* name, 120c22d69bfSAxel Dörfler struct net_protocol_module_info* module, 121c22d69bfSAxel Dörfler struct net_address_module_info* addressModule, 122c22d69bfSAxel Dörfler net_domain** _domain) 123c22d69bfSAxel Dörfler { 124c22d69bfSAxel Dörfler TRACE(("register_domain(%d, %s)\n", family, name)); 1252b07b8e0SIngo Weinhold MutexLocker locker(sDomainLock); 126c22d69bfSAxel Dörfler 127c22d69bfSAxel Dörfler struct net_domain_private* domain = lookup_domain(family); 128c22d69bfSAxel Dörfler if (domain != NULL) 129c22d69bfSAxel Dörfler return B_NAME_IN_USE; 130c22d69bfSAxel Dörfler 131c22d69bfSAxel Dörfler domain = new(std::nothrow) net_domain_private; 132c22d69bfSAxel Dörfler if (domain == NULL) 133c22d69bfSAxel Dörfler return B_NO_MEMORY; 134c22d69bfSAxel Dörfler 13526153d0fSAxel Dörfler recursive_lock_init(&domain->lock, name); 136c22d69bfSAxel Dörfler 137c22d69bfSAxel Dörfler domain->family = family; 138c22d69bfSAxel Dörfler domain->name = name; 139c22d69bfSAxel Dörfler domain->module = module; 140c22d69bfSAxel Dörfler domain->address_module = addressModule; 141c22d69bfSAxel Dörfler 142a3ec278aSAxel Dörfler sDomains.Add(domain); 143c22d69bfSAxel Dörfler 144c22d69bfSAxel Dörfler *_domain = domain; 145c22d69bfSAxel Dörfler return B_OK; 146c22d69bfSAxel Dörfler } 147c22d69bfSAxel Dörfler 148c22d69bfSAxel Dörfler 149c22d69bfSAxel Dörfler status_t 150c22d69bfSAxel Dörfler unregister_domain(net_domain* _domain) 151c22d69bfSAxel Dörfler { 1523c13a5f5SAxel Dörfler TRACE(("unregister_domain(%p, %d, %s)\n", _domain, _domain->family, 1533c13a5f5SAxel Dörfler _domain->name)); 154c22d69bfSAxel Dörfler 155c22d69bfSAxel Dörfler net_domain_private* domain = (net_domain_private*)_domain; 1562b07b8e0SIngo Weinhold MutexLocker locker(sDomainLock); 157c22d69bfSAxel Dörfler 158a3ec278aSAxel Dörfler sDomains.Remove(domain); 159c22d69bfSAxel Dörfler 16026153d0fSAxel Dörfler recursive_lock_destroy(&domain->lock); 161c22d69bfSAxel Dörfler delete domain; 162c22d69bfSAxel Dörfler return B_OK; 163c22d69bfSAxel Dörfler } 164c22d69bfSAxel Dörfler 165c22d69bfSAxel Dörfler 166c22d69bfSAxel Dörfler status_t 167c22d69bfSAxel Dörfler init_domains() 168c22d69bfSAxel Dörfler { 1692b07b8e0SIngo Weinhold mutex_init(&sDomainLock, "net domains"); 170c22d69bfSAxel Dörfler 171a3ec278aSAxel Dörfler new (&sDomains) DomainList; 172a3ec278aSAxel Dörfler // static C++ objects are not initialized in the module startup 173a3ec278aSAxel Dörfler 174a3ec278aSAxel Dörfler #if ENABLE_DEBUGGER_COMMANDS 175a3ec278aSAxel Dörfler add_debugger_command("net_domains", &dump_domains, 176a3ec278aSAxel Dörfler "Dump network domains"); 177a3ec278aSAxel Dörfler #endif 178c22d69bfSAxel Dörfler return B_OK; 179c22d69bfSAxel Dörfler } 180c22d69bfSAxel Dörfler 181c22d69bfSAxel Dörfler 182c22d69bfSAxel Dörfler status_t 183c22d69bfSAxel Dörfler uninit_domains() 184c22d69bfSAxel Dörfler { 185a3ec278aSAxel Dörfler #if ENABLE_DEBUGGER_COMMANDS 186a3ec278aSAxel Dörfler remove_debugger_command("net_domains", &dump_domains); 187a3ec278aSAxel Dörfler #endif 188a3ec278aSAxel Dörfler 1892b07b8e0SIngo Weinhold mutex_destroy(&sDomainLock); 190c22d69bfSAxel Dörfler return B_OK; 191c22d69bfSAxel Dörfler } 192c22d69bfSAxel Dörfler 193