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 56 status_t Send(ip_addr_t destinationAddress, uint16 destinationPort, 57 ChainBuffer *buffer); 58 status_t Send(ip_addr_t destinationAddress, uint16 destinationPort, 59 const void *data, size_t size); 60 status_t Receive(UDPPacket **packet, bigtime_t timeout = 0); 61 62 void PushPacket(UDPPacket *packet); 63 UDPPacket *PopPacket(); 64 65 private: 66 UDPService *fUDPService; 67 UDPPacket *fFirstPacket; 68 UDPPacket *fLastPacket; 69 ip_addr_t fAddress; 70 uint16 fPort; 71 }; 72 73 74 // UDPService 75 class UDPService : public IPSubService { 76 public: 77 UDPService(IPService *ipService); 78 virtual ~UDPService(); 79 80 status_t Init(); 81 82 virtual uint8 IPProtocol() const; 83 84 virtual void HandleIPPacket(IPService *ipService, ip_addr_t sourceIP, 85 ip_addr_t destinationIP, const void *data, size_t size); 86 87 status_t Send(uint16 sourcePort, ip_addr_t destinationAddress, 88 uint16 destinationPort, ChainBuffer *buffer); 89 90 void ProcessIncomingPackets(); 91 92 status_t BindSocket(UDPSocket *socket, ip_addr_t address, uint16 port); 93 void UnbindSocket(UDPSocket *socket); 94 95 private: 96 uint16 _ChecksumBuffer(ChainBuffer *buffer, ip_addr_t source, 97 ip_addr_t destination, uint16 length); 98 uint16 _ChecksumData(const void *data, uint16 length, ip_addr_t source, 99 ip_addr_t destination); 100 101 UDPSocket *_FindSocket(ip_addr_t address, uint16 port); 102 103 IPService *fIPService; 104 Vector<UDPSocket*> fSockets; 105 }; 106 107 #endif // _BOOT_UDP_H 108