📄 xorer.hh
字号:
#ifndef XORER_HH#define XORER_HHCLICK_DECLS#include <elements/wifi/sr/srpacket.hh>#include <elements/wifi/sr/path.hh>#include <clicknet/ether.h>#include <clicknet/ip.h>#include <click/etheraddress.hh>#include <click/packet.hh>#include "xorheader.hh"class EtherAddress;class Xorer {public: inline bool xor_worthy(Packet *, uint32_t myip, uint32_t &, uint32_t &); inline bool xor_candidate(Packet *, uint32_t myip, uint32_t, uint32_t); inline WritablePacket *xor_encode(Packet *, Packet *); inline WritablePacket *xor_decode(Packet *, Packet *, const EtherAddress &, int);};inline boolXorer::xor_worthy(Packet *p, uint32_t myip, uint32_t &prev_hop, uint32_t &next_hop){ unsigned int min_len = sizeof(click_ether) + sizeof(struct srpacket) + sizeof(struct click_xor_header); prev_hop = next_hop = 0; if (p->length() <= min_len) { click_chatter("Xorer: xor_worthy rejected p because length %u < min %u", p->length(), min_len); return false; } click_ether *eth = (click_ether *)p->data(); struct srpacket *srh = (struct srpacket *)(eth+1); srh->find_surrounding_hops(prev_hop, next_hop, myip); if ((!prev_hop) || (!next_hop)) { click_chatter("Xorer: xor_worthy rejected p because some hop is 0"); return false; } return true;}boolXorer::xor_candidate(Packet *q, uint32_t myip, uint32_t prev_hop, uint32_t next_hop){ unsigned int min_len = sizeof(click_ether) + sizeof(struct srpacket) + sizeof(struct click_xor_header); if (q->length() <= min_len) { click_chatter("Xorer: find_xor_candidate candidate packet rejected - too short"); return false; } click_ether *eth = (click_ether *)q->data(); struct srpacket *srh = (struct srpacket *)(eth+1); uint32_t prev_hop_q, next_hop_q; srh->find_surrounding_hops(prev_hop_q, next_hop_q, myip); click_chatter("Xorer: find_xor_candidate candidate packet has prev, next = (%u, %u)", prev_hop_q, next_hop_q); if ((!prev_hop_q) || (!next_hop_q)) { click_chatter("Xorer: find_xor_candidate packet rejected - no prev/next"); return false; } if ((prev_hop_q == next_hop) && (next_hop_q == prev_hop)) { click_chatter("Xorer: find_xor_candidate packet matched hops"); return true; } click_chatter("Xorer: find_xor_candidate packet rejected - hops did not match"); return false;}WritablePacket *Xorer::xor_encode(Packet *p1, Packet *p2){ /* uint16_t p1len = p1->length() - 4 - sizeof(struct click_ether); uint16_t p2len = p2->length() - 4 - sizeof(struct click_ether); */ uint16_t p1len = p1->length() - sizeof(struct click_ether); uint16_t p2len = p2->length() - sizeof(struct click_ether); Packet *spkt, *lpkt; uint16_t llen, slen; if (p1len < p2len) { lpkt = p2; spkt = p1; llen = p2len; slen = p1len; } else { lpkt = p1; spkt = p2; llen = p1len; slen = p2len; } WritablePacket *newp = lpkt->uniqueify(); newp->pull(sizeof(struct click_ether)); unsigned char *newp_data = newp->data(); const unsigned char *data = (const unsigned char *)((click_ether *)spkt->data() + 1); // xor with spkt for (int i = 0; i < slen; i++) { newp_data[i] ^= data[i]; } spkt->kill(); click_chatter("Xorer: xor_encode created new packet with length %u (no xor/ether hdr)", newp->length()); return newp;}WritablePacket *Xorer::xor_decode(Packet *sent, Packet *encoded, const EtherAddress &myea, int idx){ WritablePacket *wsent = sent->uniqueify(); click_ip *iph = wsent->ip_header(); iph->ip_ttl--; unsigned long sum = (~ntohs(iph->ip_sum) & 0xFFFF) + 0xFEFF; iph->ip_sum = ~htons(sum + (sum >> 16)); struct click_ether *eh = (struct click_ether *)wsent->data(); struct srpacket *srh = (struct srpacket *)(eh+1); srh->set_next(srh->next()+1); sum = (~ntohs(srh->_cksum) & 0xFFFF) - 0xFFFE; srh->_cksum = ~htons(sum + (sum >> 16)); /* // also update the SR header new_hw = old_hw = (srh->_nlinks << 4) & 0xff00; old_hw |= (srh->next() & 0xff); srh->set_next(srh->next()+1); new_hw |= (srh->next() & 0xff); click_update_in_cksum(&(srh->_cksum), old_hw, new_hw); */ WritablePacket *wenc = encoded->uniqueify(); click_ether *eth_enc = (struct click_ether *)(wenc->data()); EtherAddress src = EtherAddress(eth_enc->ether_shost); struct click_xor_header *xorh = (struct click_xor_header *)(eth_enc + 1); uint16_t et = xorh->_ether_type[idx]; // int enc_plen = wenc->length() - sizeof(struct click_ether) - sizeof(struct click_xor_header) - 4; // int sent_plen = wsent->length() - sizeof(struct click_ether) - 4; int sent_plen = wsent->length() - sizeof(struct click_ether); int recd_plen = ntohs(xorh->_len[idx]); int min_len = ((sent_plen < recd_plen) ? sent_plen : recd_plen); wenc->pull(sizeof(struct click_ether)); wenc->pull(sizeof(struct click_xor_header)); const unsigned char *sent_data = (const unsigned char *)((struct click_ether *)(wsent->data()) + 1); for (int i = 0; i < min_len; i++) { (wenc->data())[i] ^= sent_data[i]; } /* if (recd_plen < sent_plen) { wenc->take(sent_plen-recd_plen); } else { wenc->take(8); // voodoo } */ if (recd_plen < sent_plen) { wenc->take(sent_plen - recd_plen); } click_chatter("Xorer: xor_decode() sent has len %u, enc has len %u, result ethertype is %#x", sent_plen, recd_plen, ntohs(et)); wenc->push(sizeof(struct click_ether)); eh = (struct click_ether *)wenc->data(); memcpy(eh->ether_shost, src.data(), 6); memcpy(eh->ether_dhost, myea.data(), 6); eh->ether_type = et; wsent->kill(); return wenc;}CLICK_ENDDECLS#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -