📄 udpipencaps.cc
字号:
/* * udpipencap.{cc,hh} -- element encapsulates packet in UDP/IP header * Benjie Chen, Eddie Kohler * * Copyright (c) 1999-2000 Massachusetts Institute of Technology * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, subject to the conditions * listed in the Click LICENSE file. These conditions include: you must * preserve this copyright notice, and you cannot mention the copyright * holders in advertising related to the Software without their permission. * The Software is provided WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED. This * notice is a summary of the Click LICENSE file; the license in that file is * legally binding. */#include <click/config.h>#include <clicknet/ip.h>#include "udpipencaps.hh"#include <click/confparse.hh>#include <click/error.hh>#include <click/glue.hh>#include <click/straccum.hh>#include <click/string.hh>#include <click/standard/alignmentinfo.hh>#ifdef CLICK_LINUXMODULE# include <net/checksum.h>#endifCLICK_DECLSUDPIPEncaps::UDPIPEncaps() : Element(1, 1){}UDPIPEncaps::~UDPIPEncaps(){}intUDPIPEncaps::configure(Vector<String> &conf, ErrorHandler *errh){ bool do_cksum = true; if (cp_va_parse(conf, this, errh, cpOptional, cpBool, "do UDP checksum?", &do_cksum, cpEnd) < 0) return -1; _id = 0; _cksum = do_cksum;#if HAVE_FAST_CHECKSUM && FAST_CHECKSUM_ALIGNED // check alignment { int ans, c, o; ans = AlignmentInfo::query(this, 0, c, o); _aligned = (ans && c == 4 && o == 0); if (!_aligned) errh->warning("IP header unaligned, cannot use fast IP checksum"); if (!ans) errh->message("(Try passing the configuration through `click-align'.)"); }#endif _nflows = 0; _flowidx = -1; return 0;}Packet *UDPIPEncaps::simple_action(Packet *p_in){ WritablePacket *p = p_in->push(sizeof(click_udp) + sizeof(click_ip)); click_ip *ip = reinterpret_cast<click_ip *>(p->data()); click_udp *udp = reinterpret_cast<click_udp *>(ip + 1); // set up IP header ip->ip_v = 4; ip->ip_hl = sizeof(click_ip) >> 2; ip->ip_len = htons(p->length()); ip->ip_id = htons(_id.read_and_add(1)); ip->ip_p = IP_PROTO_UDP; ip->ip_src = _flows[_flowidx]->_src.in_addr(); ip->ip_dst = _flows[_flowidx]->_dst.in_addr(); ip->ip_tos = 0; ip->ip_off = 0; ip->ip_ttl = 250; ip->ip_sum = 0;#if HAVE_FAST_CHECKSUM && FAST_CHECKSUM_ALIGNED if (_aligned) ip->ip_sum = ip_fast_csum((unsigned char *)ip, sizeof(click_ip) >> 2); else ip->ip_sum = click_in_cksum((unsigned char *)ip, sizeof(click_ip));#elif HAVE_FAST_CHECKSUM ip->ip_sum = ip_fast_csum((unsigned char *)ip, sizeof(click_ip) >> 2);#else ip->ip_sum = click_in_cksum((unsigned char *)ip, sizeof(click_ip));#endif p->set_dst_ip_anno(_flows[_flowidx]->_dst); p->set_ip_header(ip, sizeof(click_ip)); // set up UDP header udp->uh_sport = htons((short)_flows[_flowidx]->_sport); udp->uh_dport = htons((short)_flows[_flowidx]->_dport); uint16_t len = p->length() - sizeof(click_ip); udp->uh_ulen = htons(len); udp->uh_sum = 0; if (_cksum) { unsigned csum = click_in_cksum((unsigned char *)udp, len); udp->uh_sum = click_in_cksum_pseudohdr(csum, ip, len); } StringAccum sa; sa << "Src: " << IPAddress(ip->ip_src) << " Dst: " << IPAddress(ip->ip_dst) << " sp: " << ntohs(udp->uh_sport) << " dp: " << ntohs(udp->uh_dport); click_chatter("forwarding a packet %s", sa.c_str()); if (_flowidx == (_nflows-1)) { _flowidx = 0; } else { _flowidx++; } return p;}intUDPIPEncaps::add_flow(const String &arg, Element *e, void *, ErrorHandler *errh){ Vector<String> args; UDPIPEncaps *me = static_cast<UDPIPEncaps *>(e); cp_spacevec(arg, args); UDPFlow *newflow = new UDPFlow(); me->_flows.push_back(newflow); if (!cp_ip_address(args[0], &me->_flows[me->_nflows]->_src)) { return errh->error("Couldn't read arg %d to src IP address: %s", 0, args[0].cc()); } if (!cp_unsigned(args[1], &me->_flows[me->_nflows]->_sport)) { return errh->error("Couldn't read arg %d to src port: %s", 1, args[1].cc()); } if (!cp_ip_address(args[2], &me->_flows[me->_nflows]->_dst)) { return errh->error("Couldn't read arg %d to dst IP address: %s", 2, args[2].cc()); } if (!cp_unsigned(args[3], &me->_flows[me->_nflows]->_dport)) { return errh->error("Couldn't read arg %d to dst port: %s", 3, args[3].cc()); } StringAccum sa; sa << "Src: " << me->_flows[me->_nflows]->_src << " sport: " << me->_flows[me->_nflows]->_sport << " Dst: " << me->_flows[me->_nflows]->_dst << " dport: " << me->_flows[me->_nflows]->_dport; click_chatter("%s",sa.c_str()); me->_nflows++; if (me->_flowidx == -1) { me->_flowidx = 0; } return 1;}voidUDPIPEncaps::add_handlers(){ add_write_handler("flow", add_flow, (void*)0);}CLICK_ENDDECLSEXPORT_ELEMENT(UDPIPEncaps)ELEMENT_MT_SAFE(UDPIPEncaps)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -