xref: /haiku/headers/private/kernel/boot/net/UDP.h (revision 427edfcf0ddc74fc461c9355484d33fd4b027b70)
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_UDP_H
7 #define _BOOT_UDP_H
8 
9 #include <boot/net/IP.h>
10 
11 // UDPPacket
12 class UDPPacket {
13 public:
14 	UDPPacket();
15 	~UDPPacket();
16 
17 	status_t SetTo(const void *data, size_t size, ip_addr_t sourceAddress,
18 		uint16 sourcePort, ip_addr_t destinationAddress,
19 		uint16 destinationPort);
20 
21 	UDPPacket *Next() const;
22 	void SetNext(UDPPacket *next);
23 
24 	const void *Data() const;
25 	size_t DataSize() const;
26 
27 	ip_addr_t SourceAddress() const;
28 	uint16 SourcePort() const;
29 	ip_addr_t DestinationAddress() const;
30 	uint16 DestinationPort() const;
31 
32 private:
33 	UDPPacket	*fNext;
34 	void		*fData;
35 	size_t		fSize;
36 	ip_addr_t	fSourceAddress;
37 	ip_addr_t	fDestinationAddress;
38 	uint16		fSourcePort;
39 	uint16		fDestinationPort;
40 };
41 
42 
43 class UDPService;
44 
45 // UDPSocket
46 class UDPSocket {
47 public:
48 	UDPSocket();
49 	~UDPSocket();
50 
51 	ip_addr_t Address() const	{ return fAddress; }
52 	uint16 Port() const			{ return fPort; }
53 
54 	status_t Bind(ip_addr_t address, uint16 port);
55 	void Detach();
56 
57 	status_t Send(ip_addr_t destinationAddress, uint16 destinationPort,
58 		ChainBuffer *buffer);
59 	status_t Send(ip_addr_t destinationAddress, uint16 destinationPort,
60 		const void *data, size_t size);
61 	status_t Receive(UDPPacket **packet, bigtime_t timeout = 0);
62 
63 	void PushPacket(UDPPacket *packet);
64 	UDPPacket *PopPacket();
65 
66 private:
67 	UDPService	*fUDPService;
68 	UDPPacket	*fFirstPacket;
69 	UDPPacket	*fLastPacket;
70 	ip_addr_t	fAddress;
71 	uint16		fPort;
72 };
73 
74 
75 // UDPService
76 class UDPService : public IPSubService {
77 public:
78 	UDPService(IPService *ipService);
79 	virtual ~UDPService();
80 
81 	status_t Init();
82 
83 	virtual uint8 IPProtocol() const;
84 
85 	virtual void HandleIPPacket(IPService *ipService, ip_addr_t sourceIP,
86 		ip_addr_t destinationIP, const void *data, size_t size);
87 
88 	status_t Send(uint16 sourcePort, ip_addr_t destinationAddress,
89 		uint16 destinationPort, ChainBuffer *buffer);
90 
91 	void ProcessIncomingPackets();
92 
93 	status_t BindSocket(UDPSocket *socket, ip_addr_t address, uint16 port);
94 	void UnbindSocket(UDPSocket *socket);
95 
96 private:
97 	uint16 _ChecksumBuffer(ChainBuffer *buffer, ip_addr_t source,
98 		ip_addr_t destination, uint16 length);
99 	uint16 _ChecksumData(const void *data, uint16 length, ip_addr_t source,
100 		ip_addr_t destination);
101 
102 	UDPSocket *_FindSocket(ip_addr_t address, uint16 port);
103 
104 	IPService			*fIPService;
105 	Vector<UDPSocket*>	fSockets;
106 };
107 
108 #endif	// _BOOT_UDP_H
109