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> 130e30c21cSHugo Santos #include <util/OpenHashTable.h> 1446527f68SHugo Santos 158aa4c7e3SHugo Santos #include <net_datalink.h> 168aa4c7e3SHugo Santos 176a606180SHugo Santos #include <netinet/in.h> 186c501a40SHugo Santos 198aa4c7e3SHugo Santos #include <utility> 208aa4c7e3SHugo Santos 216a606180SHugo Santos struct net_buffer; 226a606180SHugo Santos struct net_protocol; 236a606180SHugo Santos 246a606180SHugo Santos // This code is template'ized as it is reusable for IPv6 256a606180SHugo Santos 266a606180SHugo Santos template<typename Addressing> class MulticastFilter; 278aa4c7e3SHugo Santos template<typename Addressing> class MulticastGroupInterface; 286a606180SHugo Santos 296a606180SHugo Santos // TODO move this elsewhere... 306a606180SHugo Santos struct IPv4Multicast { 316a606180SHugo Santos typedef struct in_addr AddressType; 320e30c21cSHugo Santos typedef struct ipv4_protocol ProtocolType; 338aa4c7e3SHugo Santos typedef MulticastGroupInterface<IPv4Multicast> GroupInterface; 346a606180SHugo Santos 358aa4c7e3SHugo Santos static status_t JoinGroup(GroupInterface *); 368aa4c7e3SHugo Santos static status_t LeaveGroup(GroupInterface *); 376a606180SHugo Santos 380e30c21cSHugo Santos static const in_addr &AddressFromSockAddr(const sockaddr *sockaddr) 390e30c21cSHugo Santos { return ((const sockaddr_in *)sockaddr)->sin_addr; } 400e30c21cSHugo Santos static size_t HashAddress(const in_addr &address) 410e30c21cSHugo Santos { return address.s_addr; } 426a606180SHugo Santos }; 4346527f68SHugo Santos 4446527f68SHugo Santos template<typename AddressType> 458aa4c7e3SHugo Santos class AddressSet { 468aa4c7e3SHugo Santos struct ContainedAddress : DoublyLinkedListLinkImpl<ContainedAddress> { 4746527f68SHugo Santos AddressType address; 4846527f68SHugo Santos }; 4946527f68SHugo Santos 50*ab7e05a3SHugo Santos typedef DoublyLinkedList<ContainedAddress> AddressList; 51*ab7e05a3SHugo Santos 52*ab7e05a3SHugo Santos public: 53*ab7e05a3SHugo Santos AddressSet() 54*ab7e05a3SHugo Santos : fCount(0) {} 55*ab7e05a3SHugo Santos 568aa4c7e3SHugo Santos ~AddressSet() { Clear(); } 5746527f68SHugo Santos 588aa4c7e3SHugo Santos status_t Add(const AddressType &address) 598aa4c7e3SHugo Santos { 608aa4c7e3SHugo Santos if (Has(address)) 618aa4c7e3SHugo Santos return B_OK; 6246527f68SHugo Santos 638aa4c7e3SHugo Santos ContainedAddress *container = new ContainedAddress(); 648aa4c7e3SHugo Santos if (container == NULL) 658aa4c7e3SHugo Santos return B_NO_MEMORY; 6646527f68SHugo Santos 678aa4c7e3SHugo Santos container->address = address; 688aa4c7e3SHugo Santos fAddresses.Add(container); 698aa4c7e3SHugo Santos 708aa4c7e3SHugo Santos return B_OK; 718aa4c7e3SHugo Santos } 728aa4c7e3SHugo Santos 738aa4c7e3SHugo Santos void Remove(const AddressType &address) 748aa4c7e3SHugo Santos { 758aa4c7e3SHugo Santos ContainedAddress *container = _Get(address); 768aa4c7e3SHugo Santos if (container == NULL) 778aa4c7e3SHugo Santos return; 788aa4c7e3SHugo Santos 798aa4c7e3SHugo Santos fAddresses.Remove(container); 808aa4c7e3SHugo Santos delete container; 818aa4c7e3SHugo Santos } 828aa4c7e3SHugo Santos 838aa4c7e3SHugo Santos bool Has(const AddressType &address) const 848aa4c7e3SHugo Santos { 858aa4c7e3SHugo Santos return _Get(address) != NULL; 868aa4c7e3SHugo Santos } 878aa4c7e3SHugo Santos 888aa4c7e3SHugo Santos bool IsEmpty() const { return fAddresses.IsEmpty(); } 898aa4c7e3SHugo Santos 908aa4c7e3SHugo Santos void Clear() 918aa4c7e3SHugo Santos { 928aa4c7e3SHugo Santos while (!fAddresses.IsEmpty()) 938aa4c7e3SHugo Santos Remove(fAddresses.Head()->address); 948aa4c7e3SHugo Santos } 956a606180SHugo Santos 96*ab7e05a3SHugo Santos class Iterator { 97*ab7e05a3SHugo Santos public: 98*ab7e05a3SHugo Santos Iterator(const AddressList &addresses) 99*ab7e05a3SHugo Santos : fBaseIterator(addresses.GetIterator()) {} 10046527f68SHugo Santos 101*ab7e05a3SHugo Santos bool HasNext() const { return fBaseIterator.HasNext(); } 102*ab7e05a3SHugo Santos AddressType &Next() { return fBaseIterator.Next()->address; } 103*ab7e05a3SHugo Santos 104*ab7e05a3SHugo Santos private: 105*ab7e05a3SHugo Santos typename AddressList::ConstIterator fBaseIterator; 106*ab7e05a3SHugo Santos }; 107*ab7e05a3SHugo Santos 108*ab7e05a3SHugo Santos Iterator GetIterator() const { return Iterator(fAddresses); } 109*ab7e05a3SHugo Santos 110*ab7e05a3SHugo Santos private: 1118aa4c7e3SHugo Santos ContainedAddress *_Get(const AddressType &address) const 1128aa4c7e3SHugo Santos { 1138465a069SHugo Santos typename AddressList::ConstIterator it = fAddresses.GetIterator(); 1148aa4c7e3SHugo Santos while (it.HasNext()) { 1158aa4c7e3SHugo Santos ContainedAddress *container = it.Next(); 1168aa4c7e3SHugo Santos if (container->address == address) 1178aa4c7e3SHugo Santos return container; 1188aa4c7e3SHugo Santos } 1198aa4c7e3SHugo Santos return NULL; 1208aa4c7e3SHugo Santos } 12146527f68SHugo Santos 1228aa4c7e3SHugo Santos AddressList fAddresses; 123*ab7e05a3SHugo Santos int fCount; 12446527f68SHugo Santos }; 12546527f68SHugo Santos 1268aa4c7e3SHugo Santos 1276a606180SHugo Santos template<typename Addressing> 1288aa4c7e3SHugo Santos class MulticastGroupInterface 1298aa4c7e3SHugo Santos : public HashTableLink< MulticastGroupInterface<Addressing> > { 13046527f68SHugo Santos public: 1318aa4c7e3SHugo Santos typedef MulticastGroupInterface<Addressing> ThisType; 1320e30c21cSHugo Santos typedef HashTableLink<ThisType> HashLink; 1336a606180SHugo Santos typedef typename Addressing::AddressType AddressType; 1348aa4c7e3SHugo Santos typedef MulticastFilter<Addressing> Filter; 135*ab7e05a3SHugo Santos typedef AddressSet<AddressType> AddressSet; 13646527f68SHugo Santos 13746527f68SHugo Santos enum FilterMode { 13846527f68SHugo Santos kInclude, 13946527f68SHugo Santos kExclude 14046527f68SHugo Santos }; 14146527f68SHugo Santos 1428aa4c7e3SHugo Santos MulticastGroupInterface(Filter *parent, const AddressType &address, 1438aa4c7e3SHugo Santos net_interface *interface); 1448aa4c7e3SHugo Santos ~MulticastGroupInterface(); 1458aa4c7e3SHugo Santos 1468aa4c7e3SHugo Santos Filter *Parent() const { return fParent; } 1478aa4c7e3SHugo Santos 1488aa4c7e3SHugo Santos const AddressType &Address() const { return fMulticastAddress; } 1498aa4c7e3SHugo Santos net_interface *Interface() const { return fInterface; } 1508aa4c7e3SHugo Santos 1518aa4c7e3SHugo Santos status_t Add(); 1528aa4c7e3SHugo Santos status_t Drop(); 1538aa4c7e3SHugo Santos status_t BlockSource(const AddressType &sourceAddress); 1548aa4c7e3SHugo Santos status_t UnblockSource(const AddressType &sourceAddress); 1558aa4c7e3SHugo Santos status_t AddSSM(const AddressType &sourceAddress); 1568aa4c7e3SHugo Santos status_t DropSSM(const AddressType &sourceAddress); 1578aa4c7e3SHugo Santos 1588aa4c7e3SHugo Santos bool IsEmpty() const; 1598aa4c7e3SHugo Santos void Clear(); 1608aa4c7e3SHugo Santos 161*ab7e05a3SHugo Santos FilterMode Mode() const { return fFilterMode; } 162*ab7e05a3SHugo Santos const AddressSet &Sources() const { return fAddresses; } 163*ab7e05a3SHugo Santos 1648aa4c7e3SHugo Santos bool FilterAccepts(net_buffer *buffer) const; 1658aa4c7e3SHugo Santos 1668aa4c7e3SHugo Santos struct HashDefinition { 1678aa4c7e3SHugo Santos typedef void ParentType; 1688aa4c7e3SHugo Santos typedef std::pair<const AddressType *, uint32> KeyType; 1698aa4c7e3SHugo Santos typedef ThisType ValueType; 1708aa4c7e3SHugo Santos 1718aa4c7e3SHugo Santos size_t HashKey(const KeyType &key) const 1728aa4c7e3SHugo Santos { return Addressing::HashAddress(*key.first) ^ key.second; } 1738aa4c7e3SHugo Santos size_t Hash(ValueType *value) const 1748aa4c7e3SHugo Santos { return HashKey(std::make_pair(&value->Address(), 1758aa4c7e3SHugo Santos value->Interface()->index)); } 1768aa4c7e3SHugo Santos bool Compare(const KeyType &key, ValueType *value) const 1778aa4c7e3SHugo Santos { return value->Interface()->index == key.second 1788aa4c7e3SHugo Santos && value->Address().s_addr == key.first->s_addr; } 1798aa4c7e3SHugo Santos HashLink *GetLink(ValueType *value) const { return &value->fLink; } 1808aa4c7e3SHugo Santos }; 1818aa4c7e3SHugo Santos 1828aa4c7e3SHugo Santos private: 1838aa4c7e3SHugo Santos // for g++ 2.95 1848aa4c7e3SHugo Santos friend class HashDefinition; 1858aa4c7e3SHugo Santos 1868aa4c7e3SHugo Santos Filter *fParent; 18746527f68SHugo Santos AddressType fMulticastAddress; 1888aa4c7e3SHugo Santos net_interface *fInterface; 18946527f68SHugo Santos FilterMode fFilterMode; 190*ab7e05a3SHugo Santos AddressSet fAddresses; 1918aa4c7e3SHugo Santos HashLink fLink; 19246527f68SHugo Santos }; 19346527f68SHugo Santos 1946a606180SHugo Santos template<typename Addressing> 19546527f68SHugo Santos class MulticastFilter { 19646527f68SHugo Santos public: 1976a606180SHugo Santos typedef typename Addressing::AddressType AddressType; 1980e30c21cSHugo Santos typedef typename Addressing::ProtocolType ProtocolType; 1998aa4c7e3SHugo Santos typedef MulticastGroupInterface<Addressing> GroupInterface; 20046527f68SHugo Santos 2010e30c21cSHugo Santos MulticastFilter(ProtocolType *parent); 20246527f68SHugo Santos ~MulticastFilter(); 20346527f68SHugo Santos 2048aa4c7e3SHugo Santos ProtocolType *Socket() const { return fParent; } 2056c501a40SHugo Santos 2068aa4c7e3SHugo Santos status_t GetState(const AddressType &groupAddress, 2078aa4c7e3SHugo Santos net_interface *interface, GroupInterface* &state, bool create); 2088aa4c7e3SHugo Santos void ReturnState(GroupInterface *state); 20946527f68SHugo Santos 21046527f68SHugo Santos private: 2118aa4c7e3SHugo Santos typedef typename GroupInterface::HashDefinition HashDefinition; 2128aa4c7e3SHugo Santos typedef OpenHashTable<HashDefinition> States; 21346527f68SHugo Santos 2148aa4c7e3SHugo Santos void _ReturnState(GroupInterface *state); 21557967505SHugo Santos 2160e30c21cSHugo Santos ProtocolType *fParent; 21746527f68SHugo Santos States fStates; 21846527f68SHugo Santos }; 21946527f68SHugo Santos 22046527f68SHugo Santos #endif 221