1 /* 2 * Copyright 2005, Ingo Weinhold <bonefish@cs.tu-berlin.de>. 3 * All rights reserved. Distributed under the terms of the MIT License. 4 */ 5 6 #include <boot/net/NetStack.h> 7 8 #include <new> 9 10 #include <stdio.h> 11 12 #include <boot/net/ARP.h> 13 #include <boot/net/Ethernet.h> 14 #include <boot/net/IP.h> 15 #include <boot/net/UDP.h> 16 #include <boot/net/TCP.h> 17 18 19 using std::nothrow; 20 21 22 // sNetStack 23 NetStack *NetStack::sNetStack = NULL; 24 25 // constructor 26 NetStack::NetStack() 27 : fEthernetInterface(NULL), 28 fEthernetService(NULL), 29 fARPService(NULL), 30 fIPService(NULL), 31 fUDPService(NULL), 32 fTCPService(NULL) 33 { 34 } 35 36 // destructor 37 NetStack::~NetStack() 38 { 39 delete fTCPService; 40 delete fUDPService; 41 delete fIPService; 42 delete fARPService; 43 delete fEthernetService; 44 delete fEthernetInterface; 45 } 46 47 // Init 48 status_t 49 NetStack::Init() 50 { 51 // create services 52 53 // ethernet service 54 fEthernetService = new(nothrow) EthernetService; 55 if (!fEthernetService) 56 return B_NO_MEMORY; 57 58 // ARP service 59 fARPService = new(nothrow) ARPService(fEthernetService); 60 if (!fARPService) 61 return B_NO_MEMORY; 62 status_t error = fARPService->Init(); 63 if (error != B_OK) 64 return error; 65 66 // IP service 67 fIPService = new(nothrow) IPService(fEthernetService, fARPService); 68 if (!fIPService) 69 return B_NO_MEMORY; 70 error = fIPService->Init(); 71 if (error != B_OK) 72 return error; 73 74 // UDP service 75 fUDPService = new(nothrow) UDPService(fIPService); 76 if (!fUDPService) 77 return B_NO_MEMORY; 78 error = fUDPService->Init(); 79 if (error != B_OK) 80 return error; 81 82 #ifdef __POWERPC__ 83 // TCP service 84 fTCPService = new(nothrow) TCPService(fIPService); 85 if (fTCPService == NULL) 86 return B_NO_MEMORY; 87 error = fTCPService->Init(); 88 if (error != B_OK) 89 return error; 90 #endif 91 92 return B_OK; 93 } 94 95 // CreateDefault 96 status_t 97 NetStack::CreateDefault() 98 { 99 if (sNetStack) 100 return B_OK; 101 102 NetStack *netStack = new(nothrow) NetStack; 103 if (!netStack) 104 return B_NO_MEMORY; 105 106 status_t error = netStack->Init(); 107 if (error != B_OK) { 108 delete netStack; 109 return error; 110 } 111 112 sNetStack = netStack; 113 return B_OK; 114 } 115 116 // Default 117 NetStack * 118 NetStack::Default() 119 { 120 return sNetStack; 121 } 122 123 124 status_t 125 NetStack::ShutDown() 126 { 127 if (sNetStack != NULL) { 128 delete sNetStack; 129 sNetStack = NULL; 130 } 131 132 return B_OK; 133 } 134 135 136 // AddEthernetInterface 137 status_t 138 NetStack::AddEthernetInterface(EthernetInterface *interface) 139 { 140 if (!interface) 141 return B_BAD_VALUE; 142 143 // we support only one network interface at the moment 144 if (fEthernetInterface) 145 return B_BAD_VALUE; 146 147 if (!fEthernetService) 148 return B_NO_INIT; 149 150 status_t error = fEthernetService->Init(interface); 151 if (error != B_OK) 152 return error; 153 154 fEthernetInterface = interface; 155 return B_OK; 156 } 157 158 159 // #pragma mark - 160 161 status_t 162 net_stack_init() 163 { 164 status_t error = NetStack::CreateDefault(); 165 if (error != B_OK) 166 return error; 167 168 return platform_net_stack_init(); 169 } 170 171 172 status_t 173 net_stack_cleanup() 174 { 175 return NetStack::ShutDown(); 176 } 177