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 // AddEthernetInterface 124 status_t 125 NetStack::AddEthernetInterface(EthernetInterface *interface) 126 { 127 if (!interface) 128 return B_BAD_VALUE; 129 130 // we support only one network interface at the moment 131 if (fEthernetInterface) 132 return B_BAD_VALUE; 133 134 if (!fEthernetService) 135 return B_NO_INIT; 136 137 status_t error = fEthernetService->Init(interface); 138 if (error != B_OK) 139 return error; 140 141 fEthernetInterface = interface; 142 return B_OK; 143 } 144 145 146 // #pragma mark - 147 148 status_t 149 net_stack_init() 150 { 151 status_t error = NetStack::CreateDefault(); 152 if (error != B_OK) 153 return error; 154 155 return platform_net_stack_init(); 156 } 157 158