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#ifndef _BOOT_ETHERNET_H
7#define _BOOT_ETHERNET_H
8
9#include <boot/net/NetDefs.h>
10#include <util/Vector.h>
11
12class ChainBuffer;
13class EthernetService;
14
15// EthernetInterface
16class EthernetInterface {
17public:
18	EthernetInterface();
19	virtual ~EthernetInterface();
20
21	ip_addr_t IPAddress() const;
22	void SetIPAddress(ip_addr_t ipAddress);
23
24	virtual mac_addr_t MACAddress() const = 0;
25
26	virtual	void *AllocateSendReceiveBuffer(size_t size) = 0;
27	virtual	void FreeSendReceiveBuffer(void *buffer) = 0;
28
29	virtual ssize_t Send(const void *buffer, size_t size) = 0;
30	virtual ssize_t Receive(void *buffer, size_t size) = 0;
31
32protected:
33	ip_addr_t					fIPAddress;
34};
35
36// EthernetSubService
37class EthernetSubService : public NetService {
38public:
39	EthernetSubService(const char *serviceName);
40	virtual ~EthernetSubService();
41
42	virtual uint16 EthernetProtocol() const = 0;
43
44	virtual void HandleEthernetPacket(EthernetService *ethernet,
45		const mac_addr_t &targetAddress, const void *data, size_t size) = 0;
46};
47
48
49// EthernetService
50class EthernetService : public NetService {
51public:
52	EthernetService();
53	virtual ~EthernetService();
54
55	status_t Init(EthernetInterface *interface);
56
57	mac_addr_t MACAddress() const;
58	ip_addr_t IPAddress() const;
59	void SetIPAddress(ip_addr_t ipAddress);
60
61	status_t Send(const mac_addr_t &destination, uint16 protocol,
62		ChainBuffer *buffer);
63	void ProcessIncomingPackets();
64
65	bool RegisterEthernetSubService(EthernetSubService *service);
66	bool UnregisterEthernetSubService(EthernetSubService *service);
67
68	virtual int CountSubNetServices() const;
69	virtual NetService *SubNetServiceAt(int index) const;
70
71private:
72	enum {
73		SEND_BUFFER_SIZE	= 2048,
74		RECEIVE_BUFFER_SIZE	= SEND_BUFFER_SIZE,
75	};
76
77	EthernetInterface			*fInterface;
78	void						*fSendBuffer;
79	void						*fReceiveBuffer;
80	Vector<EthernetSubService*>	fServices;
81};
82
83
84#endif	// _BOOT_ETHERNET_H
85