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