xref: /haiku/src/add-ons/kernel/network/protocols/ipv4/multicast.h (revision 6c501a4085f9f397f2865e80df1242404c8f7066)
146527f68SHugo Santos /*
246527f68SHugo Santos  * Copyright 2007, Haiku, Inc. All Rights Reserved.
346527f68SHugo Santos  * Distributed under the terms of the MIT License.
446527f68SHugo Santos  *
546527f68SHugo Santos  * Authors:
646527f68SHugo Santos  *      Hugo Santos, hugosantos@gmail.com
746527f68SHugo Santos  */
846527f68SHugo Santos 
946527f68SHugo Santos #ifndef _PRIVATE_MULTICAST_H_
1046527f68SHugo Santos #define _PRIVATE_MULTICAST_H_
1146527f68SHugo Santos 
1246527f68SHugo Santos #include <util/DoublyLinkedList.h>
1346527f68SHugo Santos #include <util/list.h>
1446527f68SHugo Santos 
1546527f68SHugo Santos struct net_interface;
16*6c501a40SHugo Santos struct net_socket;
17*6c501a40SHugo Santos 
18*6c501a40SHugo Santos template<typename AddressType> class MulticastFilter;
1946527f68SHugo Santos 
2046527f68SHugo Santos template<typename AddressType>
2146527f68SHugo Santos struct MulticastSource {
2246527f68SHugo Santos 	AddressType address;
2346527f68SHugo Santos 	list_link link;
2446527f68SHugo Santos };
2546527f68SHugo Santos 
2646527f68SHugo Santos template<typename AddressType>
2746527f68SHugo Santos class MulticastGroupInterfaceState {
2846527f68SHugo Santos public:
2946527f68SHugo Santos 	MulticastGroupInterfaceState(net_interface *interface);
3046527f68SHugo Santos 	~MulticastGroupInterfaceState();
3146527f68SHugo Santos 
3246527f68SHugo Santos 	net_interface *Interface() const { return fInterface; }
3346527f68SHugo Santos 
3446527f68SHugo Santos 	status_t Add(const AddressType &address);
3546527f68SHugo Santos 	status_t Remove(const AddressType &address);
3646527f68SHugo Santos 
3746527f68SHugo Santos 	list_link link;
3846527f68SHugo Santos private:
3946527f68SHugo Santos 	typedef MulticastSource<AddressType> Source;
4046527f68SHugo Santos 	typedef DoublyLinkedListCLink<Source> SourceLink;
4146527f68SHugo Santos 	typedef DoublyLinkedList<Source, SourceLink> SourceList;
4246527f68SHugo Santos 
4346527f68SHugo Santos 	Source *_Get(const AddressType &address, bool create);
4446527f68SHugo Santos 	void _Remove(Source *state);
4546527f68SHugo Santos 
4646527f68SHugo Santos 	net_interface *fInterface;
4746527f68SHugo Santos 	// TODO make this an hash table as well
4846527f68SHugo Santos 	SourceList fSources;
4946527f68SHugo Santos };
5046527f68SHugo Santos 
5146527f68SHugo Santos template<typename AddressType>
5246527f68SHugo Santos class MulticastGroupState {
5346527f68SHugo Santos public:
54*6c501a40SHugo Santos 	MulticastGroupState(MulticastFilter<AddressType> *parent,
55*6c501a40SHugo Santos 		const AddressType &address);
5646527f68SHugo Santos 	~MulticastGroupState();
5746527f68SHugo Santos 
5846527f68SHugo Santos 	const AddressType &Address() const { return fMulticastAddress; }
5946527f68SHugo Santos 	bool IsEmpty() const
6046527f68SHugo Santos 		{ return fFilterMode == kInclude && fInterfaces.IsEmpty(); }
6146527f68SHugo Santos 
6246527f68SHugo Santos 	status_t Add(net_interface *interface);
6346527f68SHugo Santos 	status_t Drop(net_interface *interface);
6446527f68SHugo Santos 	status_t BlockSource(net_interface *interface,
6546527f68SHugo Santos 		const AddressType &sourceAddress);
6646527f68SHugo Santos 	status_t UnblockSource(net_interface *interface,
6746527f68SHugo Santos 		const AddressType &sourceAddress);
6846527f68SHugo Santos 	status_t AddSSM(net_interface *interface,
6946527f68SHugo Santos 		const AddressType &sourceAddress);
7046527f68SHugo Santos 	status_t DropSSM(net_interface *interface,
7146527f68SHugo Santos 		const AddressType &sourceAddress);
7246527f68SHugo Santos 
7346527f68SHugo Santos 	list_link link;
7446527f68SHugo Santos private:
7546527f68SHugo Santos 	typedef MulticastGroupInterfaceState<AddressType> InterfaceState;
7646527f68SHugo Santos 	typedef DoublyLinkedListCLink<InterfaceState> InterfaceStateLink;
7746527f68SHugo Santos 	typedef DoublyLinkedList<InterfaceState, InterfaceStateLink> InterfaceList;
7846527f68SHugo Santos 
7946527f68SHugo Santos 	InterfaceState *_GetInterface(net_interface *interface, bool create);
8046527f68SHugo Santos 	void _RemoveInterface(InterfaceState *state);
8146527f68SHugo Santos 
8246527f68SHugo Santos 	enum FilterMode {
8346527f68SHugo Santos 		kInclude,
8446527f68SHugo Santos 		kExclude
8546527f68SHugo Santos 	};
8646527f68SHugo Santos 
87*6c501a40SHugo Santos 	MulticastFilter<AddressType> *fParent;
8846527f68SHugo Santos 	AddressType fMulticastAddress;
8946527f68SHugo Santos 	FilterMode fFilterMode;
9046527f68SHugo Santos 	InterfaceList fInterfaces;
9146527f68SHugo Santos };
9246527f68SHugo Santos 
9346527f68SHugo Santos template<typename AddressType>
9446527f68SHugo Santos class MulticastFilter {
9546527f68SHugo Santos public:
9646527f68SHugo Santos 	typedef MulticastGroupState<AddressType> GroupState;
9746527f68SHugo Santos 
98*6c501a40SHugo Santos 	MulticastFilter(net_socket *parent);
9946527f68SHugo Santos 	~MulticastFilter();
10046527f68SHugo Santos 
101*6c501a40SHugo Santos 	net_socket *Parent() const { return fParent; }
102*6c501a40SHugo Santos 
10346527f68SHugo Santos 	GroupState *GetGroup(const AddressType &groupAddress, bool create);
10446527f68SHugo Santos 	void ReturnGroup(GroupState *group);
10546527f68SHugo Santos 
10646527f68SHugo Santos private:
10746527f68SHugo Santos 	typedef DoublyLinkedListCLink<GroupState> GroupStateLink;
10846527f68SHugo Santos 	typedef DoublyLinkedList<GroupState, GroupStateLink> States;
10946527f68SHugo Santos 
110*6c501a40SHugo Santos 	net_socket *fParent;
111*6c501a40SHugo Santos 
11246527f68SHugo Santos 	// TODO change this into an hash table or tree
11346527f68SHugo Santos 	States fStates;
11446527f68SHugo Santos };
11546527f68SHugo Santos 
11646527f68SHugo Santos #endif
117