📄 iprewriter.hh
字号:
#ifndef CLICK_IPREWRITER_HH#define CLICK_IPREWRITER_HH#include "elements/ip/iprw.hh"#include <click/sync.hh>CLICK_DECLS/*=cIPRewriter(INPUTSPEC1, ..., INPUTSPECn [, I<keywords>])=s TCPrewrites TCP/UDP packets' addresses and ports=dRewrites the source address, source port, destination address, and/ordestination port on TCP and UDP packets, along with their checksums.IPRewriter implements the functionality of a network address/port translatorE<lparen>NAPT). See also IPAddrRewriter and IPAddrPairRewriter, whichimplement Basic NAT, and TCPRewriter, which implements NAPT plus sequencenumber changes for TCP packets.IPRewriter maintains a I<mapping table> that records how packets arerewritten. The mapping table is indexed by I<flow identifier>, the quintupleof source address, source port, destination address, destination port, and IPprotocol (TCP or UDP). Each mapping contains a new flow identifier and anoutput port. Input packets with the indexed flow identifier are rewritten touse the new flow identifier, then emitted on the output port. A mapping iswritten as follows: (SA, SP, DA, DP, PROTO) => (SA', SP', DA', DP') [OUTPUT]When IPRewriter receives a packet, it first looks up that packet in themapping table by flow identifier. If the table contains a mapping for theinput packet, then the packet is rewritten according to the mapping andemitted on the specified output port. If there was no mapping, the packet ishandled by the INPUTSPEC corresponding to the input port on which the packetarrived. (There are as many input ports as INPUTSPECs.) Most INPUTSPECsinstall new mappings, so that future packets from the same TCP or UDP flow arehandled by the mapping table rather than some INPUTSPEC. The six forms ofINPUTSPEC handle input packets as follows:=over 5=item 'drop'Discards input packets.=item 'pass OUTPUT'Sends input packets to output port OUTPUT. No mappings are installed.=item 'keep FOUTPUT ROUTPUT'Installs mappings that preserve the input packet's flow ID. Specifically,given an input packet with flow ID (SA, SP, DA, DP, PROTO), two mappings areinstalled: (SA, SP, DA, DP, PROTO) => (SA, SP, DA, DP) [FOUTPUT] (DA, DP, SA, SP, PROTO) => (DA, DP, SA, SP) [ROUTPUT]Thus, the input packet is emitted on output port FOUTPUT unchanged, andpackets from the reply flow are emitted on output port ROUTPUT unchanged.=item 'pattern SADDR SPORT DADDR DPORT FOUTPUT ROUTPUT'Creates a mapping according to the given pattern, 'SADDR SPORT DADDR DPORT'.Any pattern field may be a dash '-', in which case the packet's correspondingfield is left unchanged. For instance, the pattern '1.0.0.1 20 - -' willrewrite input packets' source address and port, but leave its destinationaddress and port unchanged. SPORT may be a port range 'L-H'; IPRewriter willchoose a source port in that range so that the resulting mappings don'tconflict with any existing mappings. If no source port is available, thepacket is dropped. Normally source ports are chosen randomly within therange. To allocate source ports sequentially (which can make testing easier),append a pound sign to the range, as in '1024-65535#'.Say a packet with flow ID (SA, SP, DA, DP, PROTO) is received, and thecorresponding new flow ID is (SA', SP', DA', DP'). Then two mappings areinstalled: (SA, SP, DA, DP, PROTO) => (SA', SP', DA', DP') [FOUTPUT] (DA', DP', SA', SP', PROTO) => (DA, DP, SA, SP) [ROUTPUT]Thus, the input packet is rewritten and sent to FOUTPUT, and packets from thereply flow are rewritten to look like part of the original flow and sent toROUTPUT.=item 'pattern PATNAME FOUTPUT ROUTPUT'Like 'pattern' above, but refers to named patterns defined by anIPRewriterPatterns element.=item 'ELEMENTNAME'Creates mappings according to instructions from the element ELEMENTNAME. Thiselement must implement the IPMapper interface. One example mapper isRoundRobinIPMapper.=backIPRewriter has no mappings when first initialized.Input packets must have their IP header annotations set. Non-TCP and UDPpackets, and second and subsequent fragments, are dropped unless they arriveon a 'pass' input port. IPRewriter changes IP packet data and, optionally,destination IP address annotations; see the DST_ANNO keyword argument below.Keyword arguments determine how often stale mappings should be removed.=over 5=item TCP_TIMEOUT I<time>Time out TCP connections every I<time> seconds. Default is 24 hours.=item TCP_DONE_TIMEOUT I<time>Time out completed TCP connections every I<time> seconds. Default is 30seconds. FIN and RST flags mark TCP connections as complete.=item UDP_TIMEOUT I<time>Time out UDP connections every I<time> seconds. Default is 1 minute.=item REAP_TCP I<time>Reap timed-out TCP connections every I<time> seconds. If no packetscorresponding to a given mapping have been seen for TCP_TIMEOUT, remove themapping as stale. Default is 1 hour.=item REAP_TCP_DONE I<time>Reap timed-out completed TCP connections every I<time> seconds. Default is 10seconds.=item REAP_UDP I<time>Reap timed-out UDP connections every I<time> seconds. Default is 10 seconds.=item DST_ANNOBoolean. If true, then set the destination IP address annotation on passingpackets to the rewritten destination address. Default is true.=back=h tcp_mappings read-onlyReturns a human-readable description of the IPRewriter's current set ofTCP mappings.=h udp_mappings read-onlyReturns a human-readable description of the IPRewriter's current set ofUDP mappings.=h tcp_done_mappings read-onlyReturns a human-readable description of the IPRewriter's current set ofmappings for completed TCP sessions.=a TCPRewriter, IPAddrRewriter, IPAddrPairRewriter, IPRewriterPatterns,RoundRobinIPMapper, FTPPortMapper, ICMPRewriter, ICMPPingRewriter */#if defined(CLICK_LINUXMODULE) && __MTCLICK__# define IPRW_SPINLOCKS 1# define IPRW_RWLOCKS 0#endifclass IPRewriter : public IPRw { public: IPRewriter(); ~IPRewriter(); const char *class_name() const { return "IPRewriter"; } void *cast(const char *); const char *processing() const { return PUSH; } void notify_noutputs(int); int configure(Vector<String> &, ErrorHandler *); int initialize(ErrorHandler *); void cleanup(CleanupStage); void take_state(Element *, ErrorHandler *); int notify_pattern(Pattern *, ErrorHandler *); Mapping *apply_pattern(Pattern *, int ip_p, const IPFlowID &, int, int); Mapping *get_mapping(int, const IPFlowID &) const; void push(int, Packet *); void add_handlers(); int llrpc(unsigned, void *); private: Map _tcp_map; Map _udp_map; Mapping *_tcp_done; Mapping *_tcp_done_tail; Vector<InputSpec> _input_specs; bool _dst_anno; bool _tcp_done_gc_incr; int _tcp_done_gc_interval; int _tcp_gc_interval; int _udp_gc_interval; Timer _tcp_done_gc_timer; Timer _tcp_gc_timer; Timer _udp_gc_timer; int _udp_timeout_jiffies; int _tcp_timeout_jiffies; int _tcp_done_timeout_jiffies;#if IPRW_SPINLOCKS Spinlock _spinlock;#endif#if IPRW_RWLOCKS ReadWriteLock _rwlock;#endif int _nmapping_failures; static void tcp_gc_hook(Timer *, void *); static void udp_gc_hook(Timer *, void *); static void tcp_done_gc_hook(Timer *, void *); static String dump_mappings_handler(Element *, void *); static String dump_tcp_done_mappings_handler(Element *, void *); static String dump_nmappings_handler(Element *, void *); static String dump_patterns_handler(Element *, void *); };inline IPRw::Mapping *IPRewriter::get_mapping(int ip_p, const IPFlowID &in) const{ if (ip_p == IP_PROTO_TCP) return _tcp_map[in]; else if (ip_p == IP_PROTO_UDP) return _udp_map[in]; else return 0;}CLICK_ENDDECLS#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -