diff -crB internet-stack-old/ipv4-l3-protocol.cc internet-stack/ipv4-l3-protocol.cc *** internet-stack-old/ipv4-l3-protocol.cc Tue Apr 27 10:06:58 2010 --- internet-stack/ipv4-l3-protocol.cc Tue Apr 27 10:07:23 2010 *************** *** 40,45 **** --- 40,47 ---- #include "icmpv4-l4-protocol.h" #include "ipv4-interface.h" #include "ipv4-raw-socket-impl.h" + #include "tcp-header.h" + #include "udp-header.h" NS_LOG_COMPONENT_DEFINE ("Ipv4L3Protocol"); *************** *** 82,88 **** } Ipv4L3Protocol::Ipv4L3Protocol() ! : m_identification (0) { NS_LOG_FUNCTION_NOARGS (); } --- 84,90 ---- } Ipv4L3Protocol::Ipv4L3Protocol() ! : m_identification (0), m_markingactive(false) { NS_LOG_FUNCTION_NOARGS (); } *************** *** 483,488 **** --- 485,535 ---- socket->ForwardUp (packet, ipHeader, device); } + if (m_markingactive) + { + uint16_t srcport = 0; + uint16_t dstport = 0; + uint8_t protocol = ipHeader.GetProtocol(); + TcpHeader tcpheader; + UdpHeader udpheader; + switch (protocol) + { + case 6: + p->PeekHeader(tcpheader); + srcport = tcpheader.GetSourcePort(); + dstport = tcpheader.GetDestinationPort(); + break; + case 17: + p->PeekHeader(udpheader); + srcport = udpheader.GetSourcePort(); + dstport = udpheader.GetDestinationPort(); + break; + } + for (std::list ::iterator i = m_trafficDefinitionList.begin(); i != m_trafficDefinitionList.end(); i++) + { + if ((i->device) && (i->device == device)) + { + continue; + } + if ((ipHeader.GetSource().CombineMask(i->prefix.GetMask())) == (i->prefix.GetLocal())) + { + if (i->protocol == 0) // Protocol doesn't matter + { + ipHeader.SetTos(i->mark); + break; + } + if (i->protocol == protocol) + { // Protocol matters + if (((srcport == i->src_port) || (i->src_port == 0)) && ((dstport == i->dst_port) || (i->dst_port == 0))) + { + ipHeader.SetTos(i->mark); + break; + } + } + } + } + } + NS_ASSERT_MSG (m_routingProtocol != 0, "Need a routing protocol object to process packets"); m_routingProtocol->RouteInput (packet, ipHeader, device, MakeCallback (&Ipv4L3Protocol::IpForward, this), *************** *** 1055,1058 **** --- 1102,1137 ---- m_dropTrace (ipHeader, p, DROP_ROUTE_ERROR, m_node->GetObject (), 0); } + void + Ipv4L3Protocol::InsertMarkingRule (uint8_t mark, Ipv4Address net, Ipv4Mask mask, uint8_t proto, uint16_t src_port, uint16_t dst_port, Ptr device) + { + TrafficDefinition rule; + Ipv4InterfaceAddress prefix (net, mask); + rule.prefix = prefix; + rule.protocol = proto; + rule.device = device; + rule.mark = mark; + rule.src_port = src_port; + rule.dst_port = dst_port; + m_trafficDefinitionList.push_back(rule); + } + + void + Ipv4L3Protocol::ClearMarkingRules() + { + m_trafficDefinitionList.clear(); + } + + void + Ipv4L3Protocol::EnableMarking() + { + m_markingactive = true; + } + + void + Ipv4L3Protocol::DisableMarking() + { + m_markingactive = false; + } + }//namespace ns3 diff -crB internet-stack-old/ipv4-l3-protocol.h internet-stack/ipv4-l3-protocol.h *** internet-stack-old/ipv4-l3-protocol.h Tue Apr 27 10:06:58 2010 --- internet-stack/ipv4-l3-protocol.h Tue Apr 27 10:07:25 2010 *************** *** 188,193 **** --- 188,197 ---- void SetDown (uint32_t i); bool IsForwarding (uint32_t i) const; void SetForwarding (uint32_t i, bool val); + void InsertMarkingRule (uint8_t mark, Ipv4Address net, Ipv4Mask mask, uint8_t proto = 0, uint16_t src_port = 0, uint16_t dst_port = 0, Ptr device = 0); + void ClearMarkingRules(); + void EnableMarking(); + void DisableMarking(); Ptr GetNetDevice (uint32_t i); *************** *** 266,271 **** --- 270,288 ---- Ptr m_routingProtocol; SocketList m_sockets; + + bool m_markingactive; + struct TrafficDefinition + { + Ipv4InterfaceAddress prefix; + uint8_t mark; + uint8_t protocol; + Ptr device; + uint16_t src_port; + uint16_t dst_port; + }; + std::list m_trafficDefinitionList; + }; } // Namespace ns3 diff -crB nodeold/ipv4.h node/ipv4.h *** nodeold/ipv4.h Tue Apr 27 10:07:06 2010 --- node/ipv4.h Tue Apr 27 10:07:26 2010 *************** *** 313,318 **** --- 313,346 ---- static const uint32_t IF_ANY = 0xffffffff; + /** + * \param mark Byte value for TOS. Note that this is all 8 bits, not just the 6 lower ones reserved for Diffserf codepoints. ECN bits can be changed as well. + * \param net Network address for packets to be marked. + * \param mask Network mask for packets to be marked. Can also be 255.255.255.255 for single host. + * \param proto Protocol to be marked. Zero is default and matches all protocols. UDP is 17, TCP is 6, ICMP is 1. + * \param src_port In case of TCP or UDP, source port to be matched. Zero is default and matches any port. + * \param dst_port In case of TCP or UDP, destination port to be matched. Zero is default and matches any port. + * \param device Pointer to netdevice where packets are to arrive. Zero is default and matches any interface. + * + * You can insert packet matching rules for marking. The rules are processed in order, thus if you intend to have a default rule + * (e.g. matching 0.0.0.0/0), insert it last. Remember to EnableMarking() to enable the rules. + */ + virtual void InsertMarkingRule (uint8_t mark, Ipv4Address net, Ipv4Mask mask, uint8_t proto = 0, uint16_t src_port = 0, uint16_t dst_port = 0, Ptr device = 0) = 0; + /** + * Clears all marking rules set to the node via InsertMarkingRule. + * + */ + virtual void ClearMarkingRules() = 0; + /** + * Enables marking according to inserted rules on a node. + */ + virtual void EnableMarking() = 0; + /** + * Disables marking according to inserted rules on a node. + */ + virtual void DisableMarking() = 0; + + private: // Indirect the Ipv4 attributes through private pure virtual methods virtual void SetIpForward (bool forward) = 0;