IGMPTester.cc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. #include <algorithm>
  2. #include <fstream>
  3. #include "inet/common/INETDefs.h"
  4. #include "inet/common/ModuleAccess.h"
  5. #include "inet/common/scenario/IScriptable.h"
  6. #include "inet/networklayer/common/IPSocket.h"
  7. #include "inet/networklayer/contract/IInterfaceTable.h"
  8. #include "inet/networklayer/contract/NetworkProtocolCommand_m.h"
  9. #include "inet/networklayer/contract/ipv4/IPv4Address.h"
  10. #include "inet/networklayer/contract/ipv4/IPv4ControlInfo.h"
  11. #include "inet/networklayer/ipv4/IGMPMessage.h"
  12. #include "inet/networklayer/ipv4/IIPv4RoutingTable.h"
  13. #include "inet/networklayer/ipv4/IPv4InterfaceData.h"
  14. using namespace std;
  15. namespace inet {
  16. class INET_API IGMPTester : public cSimpleModule, public IScriptable
  17. {
  18. private:
  19. IInterfaceTable *ift;
  20. map<IPv4Address, IPv4MulticastSourceList> socketState;
  21. protected:
  22. typedef IPv4InterfaceData::IPv4AddressVector IPv4AddressVector;
  23. virtual int numInitStages() const override { return 2; }
  24. virtual void initialize(int stage) override;
  25. virtual void handleMessage(cMessage *msg) override;
  26. virtual void processCommand(const cXMLElement &node) override;
  27. private:
  28. void processSendCommand(const cXMLElement &node);
  29. void processJoinCommand(IPv4Address group, const IPv4AddressVector &sources, InterfaceEntry* ie);
  30. void processLeaveCommand(IPv4Address group, const IPv4AddressVector &sources, InterfaceEntry* ie);
  31. void processBlockCommand(IPv4Address group, const IPv4AddressVector &sources, InterfaceEntry *ie);
  32. void processAllowCommand(IPv4Address group, const IPv4AddressVector &sources, InterfaceEntry *ie);
  33. void processSetFilterCommand(IPv4Address group, McastSourceFilterMode filterMode, const IPv4AddressVector &sources, InterfaceEntry *ie);
  34. void processDumpCommand(string what, InterfaceEntry *ie);
  35. void parseIPv4AddressVector(const char *str, IPv4AddressVector &result);
  36. void sendIGMP(IGMPMessage *msg, InterfaceEntry *ie, IPv4Address dest);
  37. };
  38. Define_Module(IGMPTester);
  39. static ostream &operator<<(ostream &out, const IPv4AddressVector addresses)
  40. {
  41. for (int i = 0; i < (int)addresses.size(); i++)
  42. out << (i>0?" ":"") << addresses[i];
  43. return out;
  44. }
  45. static ostream &operator<<(ostream &out, IGMPMessage* msg)
  46. {
  47. out << msg->getClassName() << "<";
  48. switch (msg->getType())
  49. {
  50. case IGMP_MEMBERSHIP_QUERY:
  51. {
  52. IGMPQuery *query = check_and_cast<IGMPQuery*>(msg);
  53. out << "group=" << query->getGroupAddress();
  54. if (dynamic_cast<IGMPv3Query*>(msg))
  55. {
  56. IGMPv3Query *v3Query = dynamic_cast<IGMPv3Query*>(msg);
  57. out << ", sourceList=" << v3Query->getSourceList()
  58. << ", maxRespCode=" << (int)v3Query->getMaxRespCode()
  59. << ", suppressRouterProc=" << (int)v3Query->getSuppressRouterProc()
  60. << ", robustnessVariable=" << (int)v3Query->getRobustnessVariable()
  61. << ", queryIntervalCode=" << (int)v3Query->getQueryIntervalCode();
  62. }
  63. else if (dynamic_cast<IGMPv2Query*>(msg))
  64. out << ", maxRespTime=" << (int)dynamic_cast<IGMPv2Query*>(msg)->getMaxRespTime();
  65. break;
  66. }
  67. case IGMPV1_MEMBERSHIP_REPORT:
  68. // TODO
  69. break;
  70. case IGMPV2_MEMBERSHIP_REPORT:
  71. // TODO
  72. break;
  73. case IGMPV2_LEAVE_GROUP:
  74. // TODO
  75. break;
  76. case IGMPV3_MEMBERSHIP_REPORT:
  77. {
  78. IGMPv3Report *report = check_and_cast<IGMPv3Report*>(msg);
  79. for (unsigned int i = 0; i < report->getGroupRecordArraySize(); i++)
  80. {
  81. GroupRecord &record = report->getGroupRecord(i);
  82. out << (i>0?", ":"") << record.groupAddress << "=";
  83. switch (record.recordType)
  84. {
  85. case MODE_IS_INCLUDE: out << "IS_IN" ; break;
  86. case MODE_IS_EXCLUDE: out << "IS_EX" ; break;
  87. case CHANGE_TO_INCLUDE_MODE: out << "TO_IN" ; break;
  88. case CHANGE_TO_EXCLUDE_MODE: out << "TO_EX" ; break;
  89. case ALLOW_NEW_SOURCES: out << "ALLOW" ; break;
  90. case BLOCK_OLD_SOURCE: out << "BLOCK" ; break;
  91. }
  92. if (!record.sourceList.empty())
  93. out << " " << record.sourceList;
  94. }
  95. break;
  96. }
  97. default:
  98. throw cRuntimeError("Unexpected message.");
  99. break;
  100. }
  101. out << ">";
  102. return out;
  103. }
  104. void IGMPTester::initialize(int stage)
  105. {
  106. if (stage == 0)
  107. {
  108. ift = getModuleFromPar<IInterfaceTable>(par("interfaceTableModule"), this);
  109. InterfaceEntry *interfaceEntry = new InterfaceEntry(this);
  110. interfaceEntry->setName("eth0");
  111. MACAddress address("AA:00:00:00:00:01");
  112. interfaceEntry->setMACAddress(address);
  113. interfaceEntry->setInterfaceToken(address.formInterfaceIdentifier());
  114. interfaceEntry->setMtu(par("mtu").longValue());
  115. interfaceEntry->setMulticast(true);
  116. interfaceEntry->setBroadcast(true);
  117. ift->addInterface(interfaceEntry);
  118. }
  119. else if (stage == 2)
  120. {
  121. InterfaceEntry *ie = ift->getInterface(0);
  122. ie->ipv4Data()->setIPAddress(IPv4Address("192.168.1.1"));
  123. ie->ipv4Data()->setNetmask(IPv4Address("255.255.0.0"));
  124. }
  125. }
  126. void IGMPTester::handleMessage(cMessage *msg)
  127. {
  128. if (dynamic_cast<RegisterTransportProtocolCommand*>(msg))
  129. {
  130. delete msg;
  131. return;
  132. }
  133. IGMPMessage *igmpMsg = check_and_cast<IGMPMessage*>(msg);
  134. EV << "IGMPTester: Received: " << igmpMsg << ".\n";
  135. delete msg;
  136. }
  137. void IGMPTester::processCommand(const cXMLElement &node)
  138. {
  139. Enter_Method_Silent();
  140. string tag = node.getTagName();
  141. const char *ifname = node.getAttribute("ifname");
  142. InterfaceEntry *ie = ifname ? ift->getInterfaceByName(ifname) : NULL;
  143. if (tag == "join")
  144. {
  145. const char *group = node.getAttribute("group");
  146. IPv4AddressVector sources;
  147. parseIPv4AddressVector(node.getAttribute("sources"), sources);
  148. processJoinCommand(IPv4Address(group), sources, ie);
  149. }
  150. else if (tag == "leave")
  151. {
  152. const char *group = node.getAttribute("group");
  153. IPv4AddressVector sources;
  154. parseIPv4AddressVector(node.getAttribute("sources"), sources);
  155. processLeaveCommand(IPv4Address(group), sources, ie);
  156. }
  157. else if (tag == "block")
  158. {
  159. const char *group = node.getAttribute("group");
  160. IPv4AddressVector sources;
  161. parseIPv4AddressVector(node.getAttribute("sources"), sources);
  162. processBlockCommand(IPv4Address(group), sources, ie);
  163. }
  164. else if (tag == "allow")
  165. {
  166. const char *group = node.getAttribute("group");
  167. IPv4AddressVector sources;
  168. parseIPv4AddressVector(node.getAttribute("sources"), sources);
  169. processAllowCommand(IPv4Address(group), sources, ie);
  170. }
  171. else if (tag == "set-filter")
  172. {
  173. const char *groupAttr = node.getAttribute("group");
  174. const char *sourcesAttr = node.getAttribute("sources");
  175. ASSERT((sourcesAttr[0] == 'I' || sourcesAttr[0] == 'E') && (sourcesAttr[1] == ' ' || sourcesAttr[1] == '\0'));
  176. McastSourceFilterMode filterMode = sourcesAttr[0] == 'I' ? MCAST_INCLUDE_SOURCES : MCAST_EXCLUDE_SOURCES;
  177. IPv4AddressVector sources;
  178. if (sourcesAttr[1])
  179. parseIPv4AddressVector(sourcesAttr+2, sources);
  180. processSetFilterCommand(IPv4Address(groupAttr), filterMode, sources, ie);
  181. }
  182. else if (tag == "dump")
  183. {
  184. const char *what = node.getAttribute("what");
  185. processDumpCommand(what, ie);
  186. }
  187. else if (tag == "send")
  188. {
  189. processSendCommand(node);
  190. }
  191. }
  192. void IGMPTester::processSendCommand(const cXMLElement &node)
  193. {
  194. const char *ifname = node.getAttribute("ifname");
  195. InterfaceEntry *ie = ifname ? ift->getInterfaceByName(ifname) : ift->getInterface(0);
  196. string type = node.getAttribute("type");
  197. if (type == "IGMPv1Query")
  198. {
  199. // TODO
  200. }
  201. else if (type == "IGMPv2Query")
  202. {
  203. // TODO
  204. }
  205. else if (type == "IGMPv3Query")
  206. {
  207. const char *groupStr = node.getAttribute("group");
  208. const char *maxRespCodeStr = node.getAttribute("maxRespCode");
  209. const char *sourcesStr = node.getAttribute("source");
  210. IPv4Address group = groupStr ? IPv4Address(groupStr) : IPv4Address::UNSPECIFIED_ADDRESS;
  211. int maxRespCode = maxRespCodeStr ? atoi(maxRespCodeStr) : 100 /*10 sec*/;
  212. IPv4AddressVector sources;
  213. parseIPv4AddressVector(sourcesStr, sources);
  214. IGMPv3Query *msg = new IGMPv3Query("IGMPv3 query");
  215. msg->setType(IGMP_MEMBERSHIP_QUERY);
  216. msg->setGroupAddress(group);
  217. msg->setMaxRespCode(maxRespCode);
  218. msg->setSourceList(sources);
  219. msg->setByteLength(12 + (4 * sources.size()));
  220. sendIGMP(msg, ie, group.isUnspecified() ? IPv4Address::ALL_HOSTS_MCAST : group);
  221. }
  222. else if (type == "IGMPv2Report")
  223. {
  224. // TODO
  225. }
  226. else if (type == "IGMPv2Leave")
  227. {
  228. // TODO
  229. }
  230. else if (type == "IGMPv3Report")
  231. {
  232. cXMLElementList records = node.getElementsByTagName("record");
  233. IGMPv3Report *msg = new IGMPv3Report("IGMPv3 report");
  234. msg->setGroupRecordArraySize(records.size());
  235. for (int i = 0; i < (int)records.size(); ++i)
  236. {
  237. cXMLElement *recordNode = records[i];
  238. const char *groupStr = recordNode->getAttribute("group");
  239. string recordTypeStr = recordNode->getAttribute("type");
  240. const char *sourcesStr = recordNode->getAttribute("sources");
  241. ASSERT(groupStr);
  242. GroupRecord &record = msg->getGroupRecord(i);
  243. record.groupAddress = IPv4Address(groupStr);
  244. parseIPv4AddressVector(sourcesStr, record.sourceList);
  245. record.recordType = recordTypeStr == "IS_IN" ? MODE_IS_INCLUDE :
  246. recordTypeStr == "IS_EX" ? MODE_IS_EXCLUDE :
  247. recordTypeStr == "TO_IN" ? CHANGE_TO_INCLUDE_MODE :
  248. recordTypeStr == "TO_EX" ? CHANGE_TO_EXCLUDE_MODE :
  249. recordTypeStr == "ALLOW" ? ALLOW_NEW_SOURCES :
  250. recordTypeStr == "BLOCK" ? BLOCK_OLD_SOURCE : 0;
  251. ASSERT(record.groupAddress.isMulticast());
  252. ASSERT(record.recordType);
  253. }
  254. sendIGMP(msg, ie, IPv4Address::ALL_IGMPV3_ROUTERS_MCAST);
  255. }
  256. }
  257. void IGMPTester::processJoinCommand(IPv4Address group, const IPv4AddressVector &sources, InterfaceEntry *ie)
  258. {
  259. if (sources.empty())
  260. {
  261. ie->ipv4Data()->joinMulticastGroup(group);
  262. socketState[group] = IPv4MulticastSourceList::ALL_SOURCES;
  263. }
  264. else
  265. {
  266. IPv4MulticastSourceList &sourceList = socketState[group];
  267. ASSERT(sourceList.filterMode == MCAST_INCLUDE_SOURCES);
  268. IPv4AddressVector oldSources(sourceList.sources);
  269. for (IPv4AddressVector::const_iterator source = sources.begin(); source != sources.end(); ++source)
  270. sourceList.add(*source);
  271. if (oldSources != sourceList.sources)
  272. ie->ipv4Data()->changeMulticastGroupMembership(group, MCAST_INCLUDE_SOURCES, oldSources, MCAST_INCLUDE_SOURCES, sourceList.sources);
  273. }
  274. }
  275. void IGMPTester::processLeaveCommand(IPv4Address group, const IPv4AddressVector &sources, InterfaceEntry *ie)
  276. {
  277. if (sources.empty())
  278. {
  279. ie->ipv4Data()->leaveMulticastGroup(group);
  280. socketState.erase(group);
  281. }
  282. else
  283. {
  284. IPv4MulticastSourceList &sourceList = socketState[group];
  285. ASSERT(sourceList.filterMode == MCAST_INCLUDE_SOURCES);
  286. IPv4AddressVector oldSources(sourceList.sources);
  287. for (IPv4AddressVector::const_iterator source = sources.begin(); source != sources.end(); ++source)
  288. sourceList.remove(*source);
  289. if (oldSources != sourceList.sources)
  290. ie->ipv4Data()->changeMulticastGroupMembership(group, MCAST_INCLUDE_SOURCES, oldSources, MCAST_INCLUDE_SOURCES, sourceList.sources);
  291. if (sourceList.sources.empty())
  292. socketState.erase(group);
  293. }
  294. }
  295. void IGMPTester::processBlockCommand(IPv4Address group, const IPv4AddressVector &sources, InterfaceEntry *ie)
  296. {
  297. map<IPv4Address, IPv4MulticastSourceList>::iterator it = socketState.find(group);
  298. ASSERT(it != socketState.end());
  299. ASSERT(it->second.filterMode == MCAST_EXCLUDE_SOURCES);
  300. IPv4AddressVector oldSources(it->second.sources);
  301. for (IPv4AddressVector::const_iterator source = sources.begin(); source != sources.end(); ++source)
  302. it->second.add(*source);
  303. if (oldSources != it->second.sources)
  304. ie->ipv4Data()->changeMulticastGroupMembership(group, MCAST_EXCLUDE_SOURCES, oldSources, MCAST_EXCLUDE_SOURCES, it->second.sources);
  305. }
  306. void IGMPTester::processAllowCommand(IPv4Address group, const IPv4AddressVector &sources, InterfaceEntry *ie)
  307. {
  308. map<IPv4Address, IPv4MulticastSourceList>::iterator it = socketState.find(group);
  309. ASSERT(it != socketState.end());
  310. ASSERT(it->second.filterMode == MCAST_EXCLUDE_SOURCES);
  311. IPv4AddressVector oldSources(it->second.sources);
  312. for (IPv4AddressVector::const_iterator source = sources.begin(); source != sources.end(); ++source)
  313. it->second.remove(*source);
  314. if (oldSources != it->second.sources)
  315. ie->ipv4Data()->changeMulticastGroupMembership(group, MCAST_EXCLUDE_SOURCES, oldSources, MCAST_EXCLUDE_SOURCES, it->second.sources);
  316. }
  317. void IGMPTester::processSetFilterCommand(IPv4Address group, McastSourceFilterMode filterMode, const IPv4AddressVector &sources, InterfaceEntry *ie)
  318. {
  319. IPv4MulticastSourceList &sourceList = socketState[group];
  320. McastSourceFilterMode oldFilterMode = sourceList.filterMode;
  321. IPv4AddressVector oldSources(sourceList.sources);
  322. sourceList.filterMode = filterMode;
  323. sourceList.sources = sources;
  324. if (filterMode != oldFilterMode || oldSources != sourceList.sources)
  325. ie->ipv4Data()->changeMulticastGroupMembership(group, oldFilterMode, oldSources, sourceList.filterMode, sourceList.sources);
  326. if (sourceList.filterMode == MCAST_INCLUDE_SOURCES && sourceList.sources.empty())
  327. socketState.erase(group);
  328. }
  329. void IGMPTester::processDumpCommand(string what, InterfaceEntry *ie)
  330. {
  331. EV << "IGMPTester: " << ie->getName() << ": " << what << " = ";
  332. if (what == "groups")
  333. {
  334. for (int i = 0; i < ie->ipv4Data()->getNumOfJoinedMulticastGroups(); i++)
  335. {
  336. IPv4Address group = ie->ipv4Data()->getJoinedMulticastGroup(i);
  337. const IPv4MulticastSourceList &sourceList = ie->ipv4Data()->getJoinedMulticastSources(i);
  338. EV << (i==0?"":", ") << group << " " << sourceList.info();
  339. }
  340. }
  341. else if (what == "listeners")
  342. {
  343. for (int i = 0; i < ie->ipv4Data()->getNumOfReportedMulticastGroups(); i++)
  344. {
  345. IPv4Address group = ie->ipv4Data()->getReportedMulticastGroup(i);
  346. const IPv4MulticastSourceList &sourceList = ie->ipv4Data()->getReportedMulticastSources(i);
  347. EV << (i==0?"":", ") << group << " " << sourceList.info();
  348. }
  349. }
  350. EV << ".\n";
  351. }
  352. void IGMPTester::sendIGMP(IGMPMessage *msg, InterfaceEntry *ie, IPv4Address dest)
  353. {
  354. ASSERT(ie->isMulticast());
  355. IPv4ControlInfo *controlInfo = new IPv4ControlInfo();
  356. controlInfo->setProtocol(IP_PROT_IGMP);
  357. controlInfo->setInterfaceId(ie->getInterfaceId());
  358. controlInfo->setTimeToLive(1);
  359. controlInfo->setDestAddr(dest);
  360. msg->setControlInfo(controlInfo);
  361. EV << "IGMPTester: Sending: " << msg << ".\n";
  362. send(msg, "igmpOut");
  363. }
  364. void IGMPTester::parseIPv4AddressVector(const char *str, IPv4AddressVector &result)
  365. {
  366. if (str)
  367. {
  368. cStringTokenizer tokens(str);
  369. while (tokens.hasMoreTokens())
  370. result.push_back(IPv4Address(tokens.nextToken()));
  371. }
  372. sort(result.begin(), result.end());
  373. }
  374. } // namespace inet