headers/bsd: Add sys/queue.h.
[haiku.git] / src / system / boot / loader / net / NetStack.cpp
blobf420d94b27da05358d22ebdb0aa49adadffee3db
1 /*
2 * Copyright 2005, Ingo Weinhold <bonefish@cs.tu-berlin.de>.
3 * All rights reserved. Distributed under the terms of the MIT License.
4 */
6 #include <boot/net/NetStack.h>
8 #include <new>
10 #include <stdio.h>
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>
19 using std::nothrow;
22 // sNetStack
23 NetStack *NetStack::sNetStack = NULL;
25 // constructor
26 NetStack::NetStack()
27 : fEthernetInterface(NULL),
28 fEthernetService(NULL),
29 fARPService(NULL),
30 fIPService(NULL),
31 fUDPService(NULL),
32 fTCPService(NULL)
36 // destructor
37 NetStack::~NetStack()
39 delete fTCPService;
40 delete fUDPService;
41 delete fIPService;
42 delete fARPService;
43 delete fEthernetService;
44 delete fEthernetInterface;
47 // Init
48 status_t
49 NetStack::Init()
51 // create services
53 // ethernet service
54 fEthernetService = new(nothrow) EthernetService;
55 if (!fEthernetService)
56 return B_NO_MEMORY;
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;
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;
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;
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
92 return B_OK;
95 // CreateDefault
96 status_t
97 NetStack::CreateDefault()
99 if (sNetStack)
100 return B_OK;
102 NetStack *netStack = new(nothrow) NetStack;
103 if (!netStack)
104 return B_NO_MEMORY;
106 status_t error = netStack->Init();
107 if (error != B_OK) {
108 delete netStack;
109 return error;
112 sNetStack = netStack;
113 return B_OK;
116 // Default
117 NetStack *
118 NetStack::Default()
120 return sNetStack;
124 status_t
125 NetStack::ShutDown()
127 if (sNetStack != NULL) {
128 delete sNetStack;
129 sNetStack = NULL;
132 return B_OK;
136 // AddEthernetInterface
137 status_t
138 NetStack::AddEthernetInterface(EthernetInterface *interface)
140 if (!interface)
141 return B_BAD_VALUE;
143 // we support only one network interface at the moment
144 if (fEthernetInterface)
145 return B_BAD_VALUE;
147 if (!fEthernetService)
148 return B_NO_INIT;
150 status_t error = fEthernetService->Init(interface);
151 if (error != B_OK)
152 return error;
154 fEthernetInterface = interface;
155 return B_OK;
159 // #pragma mark -
161 status_t
162 net_stack_init()
164 status_t error = NetStack::CreateDefault();
165 if (error != B_OK)
166 return error;
168 return platform_net_stack_init();
172 status_t
173 net_stack_cleanup()
175 return NetStack::ShutDown();