📄 classifier.cc
字号:
/* -*- Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- *//* * Copyright (c) 1996 Regents of the University of California. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the MASH Research * Group at the University of California Berkeley. * 4. Neither the name of the University nor of the Research Group may be * used to endorse or promote products derived from this software without * specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */#ifndef lintstatic const char rcsid[] = "@(#) $Header: /usr/src/mash/repository/vint/ns-2/classifier.cc,v 1.33 1999/09/20 01:55:02 heideman Exp $";#endif#include <stdlib.h>#include "config.h"#include "classifier.h"#include "packet.h"#include "ip.h"#include "address.h"#include "mip.h"//SHK#include "tcp.h"static class ClassifierClass : public TclClass {public: ClassifierClass() : TclClass("Classifier") {} TclObject* create(int, const char*const*) { return (new Classifier()); }} class_classifier;Classifier::Classifier() : slot_(0), nslot_(0), maxslot_(-1) , shift_(0), mask_(0xffffffff){ default_target_ = 0; bind("offset_", &offset_); bind("shift_", &shift_); bind("mask_", &mask_); bind("top_", &top_); //Wan bind("foreignBS_", &foreignBS_); bind("off_ipinip_", &off_ipinip_); cq = NULL;}int Classifier::classify(Packet *p){ return (mshift(*((int*) p->access(offset_))));}Classifier::~Classifier(){ delete [] slot_;}void Classifier::alloc(int slot){ NsObject** old = slot_; int n = nslot_; if (old == 0) nslot_ = 32; while (nslot_ <= slot) nslot_ <<= 1; slot_ = new NsObject*[nslot_]; memset(slot_, 0, nslot_ * sizeof(NsObject*)); for (int i = 0; i < n; ++i) slot_[i] = old[i]; delete [] old;}void Classifier::install(int slot, NsObject* p){ if (slot >= nslot_) alloc(slot); slot_[slot] = p; if (slot >= maxslot_) maxslot_ = slot;}void Classifier::clear(int slot){ slot_[slot] = 0; if (slot == maxslot_) { while (--maxslot_ >= 0 && slot_[maxslot_] == 0) ; }}int Classifier::getnxt(NsObject *nullagent){ int i; for (i=0; i < nslot_; i++) if (slot_[i]==0 || slot_[i]==nullagent) return i; i=nslot_; alloc(nslot_); return i;}void Classifier::dump() { printf("ok start dumpping\n"); Packet *old_pkt; int success; double elapsed, timestamp; while(!cq->IsEmpty()) { cq->Remove(×tamp, &old_pkt, &success); if(success == True) printf("%s recovered the packet successfully\n", name());#ifndef TIMER elapsed = Scheduler::instance().clock() - timestamp; printf("now: %f timestamp: %f\n", Scheduler::instance().clock(), timestamp); printf("elapsed time: %f timelimit: %f\n", elapsed, cq->TimeLimit()); if(elapsed > cq->TimeLimit()) { Packet::free(old_pkt); printf("but it was discarded\n"); continue; }#endif#ifdef TCPPKT hdr_cmn *th = hdr_cmn::access(old_pkt); hdr_tcp *tcph = hdr_tcp::access(old_pkt); hdr_ip *iph = hdr_ip::access(old_pkt); printf("**%s: TCP Seq. Number %d UID %d Dumped\n", name(), tcph->seqno(), th->uid());#endif NsObject* node = find(old_pkt); if (node == NULL) { Packet::free(old_pkt); return; } node->recv(old_pkt, (Handler*)0); //give some delay //how? }}//Wan:int not_Hawaii();int is_HFA();/* * objects only ever see "packet" events, which come either * from an incoming link or a local agent (i.e., packet source). */void Classifier::recv(Packet* p, Handler*h){ char* nextHop; hdr_ip* iph = hdr_ip::access(p); if (is_HFA()) { if(foreignBS_ == 1){ hdr_ipinip **ppinhdr = (hdr_ipinip **)p->access(off_ipinip_); hdr_ip *pouthdr = (hdr_ip *)p->access(off_ip_); assert(ppinhdr); hdr_ip *pinhdr = &(*ppinhdr)->hdr_; Tcl::instance().evalf("%s Lookup_RT %d", name(), iph->daddr()); nextHop = Tcl::instance().result(); if (!strcmp(nextHop, "-1")) { } else if (!strcmp(nextHop, "1")) { } else if (!strcmp(nextHop, "2")) { Packet::free(p); return; } } } if (!not_Hawaii()) { if(foreignBS_ == 1){ Tcl::instance().evalf("%s Lookup_Hawaii_RT %d", name(), iph->daddr()); nextHop = Tcl::instance().result(); if (!strcmp(nextHop, "-1")) { } else if (!strcmp(nextHop, "1")) { if(cq != NULL) { //buffer exist#ifdef TCPPKT hdr_cmn *th = hdr_cmn::access(p); hdr_tcp *tcph = hdr_tcp::access(p); hdr_ip *iph = hdr_ip::access(p); printf("**%s: TCP Seq. Number %d UID %d\n",name(), tcph->seqno(), th->uid());#endif int success; Packet *pkt, *old_pkt; double now, timestamp; now = Scheduler::instance().clock(); if(cq->IsFull() == False) { pkt = p->copy(); cq->Insert(now, pkt, &success); if(success != True) printf("ERROR: %s can not store the packet\n", name()); } else { // Buffer is full. Remove old packet first! cq->Remove(×tamp, &old_pkt, &success); if(success != True) { printf("ERROR: %s can not delete old packet\n", name()); free(old_pkt); } pkt = p->copy(); cq->Insert(now, pkt, &success); if(success != True) printf("ERROR: %s can not store the packet\n", name()); } } NsObject* link = RAfind(p); link->recv(p,h); return; } else if (!strcmp(nextHop, "2")) { if(cq != NULL) { //buffer exist#ifdef TCPPKT hdr_cmn *th = hdr_cmn::access(p); hdr_tcp *tcph = hdr_tcp::access(p); hdr_ip *iph = hdr_ip::access(p); printf("**%s: TCP Seq. Number %d UID %d\n",name(), tcph->seqno(), th->uid());#endif int success; Packet *pkt, *old_pkt; double now, timestamp; now = Scheduler::instance().clock(); if(cq->IsFull() == False) { pkt = p->copy();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -