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 // sNetStack 20 NetStack *NetStack::sNetStack = NULL; 21 22 // constructor 23 NetStack::NetStack() 24 : fEthernetInterface(NULL), 25 fEthernetService(NULL), 26 fARPService(NULL), 27 fIPService(NULL), 28 fUDPService(NULL), 29 fTCPService(NULL) 30 { 31 } 32 33 // destructor 34 NetStack::~NetStack() 35 { 36 delete fTCPService; 37 delete fUDPService; 38 delete fIPService; 39 delete fARPService; 40 delete fEthernetService; 41 delete fEthernetInterface; 42 } 43 44 // Init 45 status_t 46 NetStack::Init() 47 { 48 // create services 49 50 // ethernet service 51 fEthernetService = new(nothrow) EthernetService; 52 if (!fEthernetService) 53 return B_NO_MEMORY; 54 55 // ARP service 56 fARPService = new(nothrow) ARPService(fEthernetService); 57 if (!fARPService) 58 return B_NO_MEMORY; 59 status_t error = fARPService->Init(); 60 if (error != B_OK) 61 return error; 62 63 // IP service 64 fIPService = new(nothrow) IPService(fEthernetService, fARPService); 65 if (!fIPService) 66 return B_NO_MEMORY; 67 error = fIPService->Init(); 68 if (error != B_OK) 69 return error; 70 71 // UDP service 72 fUDPService = new(nothrow) UDPService(fIPService); 73 if (!fUDPService) 74 return B_NO_MEMORY; 75 error = fUDPService->Init(); 76 if (error != B_OK) 77 return error; 78 79 #ifdef __POWERPC__ 80 // TCP service 81 fTCPService = new(nothrow) TCPService(fIPService); 82 if (fTCPService == NULL) 83 return B_NO_MEMORY; 84 error = fTCPService->Init(); 85 if (error != B_OK) 86 return error; 87 #endif 88 89 return B_OK; 90 } 91 92 // CreateDefault 93 status_t 94 NetStack::CreateDefault() 95 { 96 if (sNetStack) 97 return B_OK; 98 99 NetStack *netStack = new(nothrow) NetStack; 100 if (!netStack) 101 return B_NO_MEMORY; 102 103 status_t error = netStack->Init(); 104 if (error != B_OK) { 105 delete netStack; 106 return error; 107 } 108 109 sNetStack = netStack; 110 return B_OK; 111 } 112 113 // Default 114 NetStack * 115 NetStack::Default() 116 { 117 return sNetStack; 118 } 119 120 // AddEthernetInterface 121 status_t 122 NetStack::AddEthernetInterface(EthernetInterface *interface) 123 { 124 if (!interface) 125 return B_BAD_VALUE; 126 127 // we support only one network interface at the moment 128 if (fEthernetInterface) 129 return B_BAD_VALUE; 130 131 if (!fEthernetService) 132 return B_NO_INIT; 133 134 status_t error = fEthernetService->Init(interface); 135 if (error != B_OK) 136 return error; 137 138 fEthernetInterface = interface; 139 return B_OK; 140 } 141 142 143 // #pragma mark - 144 145 status_t 146 net_stack_init() 147 { 148 status_t error = NetStack::CreateDefault(); 149 if (error != B_OK) 150 return error; 151 152 return platform_net_stack_init(); 153 } 154 155