📄 mip-reg.cc
字号:
// Copyright (c) 2000 by the University of Southern California// All rights reserved.//// Permission to use, copy, modify, and distribute this software and its// documentation in source and binary forms for non-commercial purposes// and without fee is hereby granted, provided that the above copyright// notice appear in all copies and that both the copyright notice and// this permission notice appear in supporting documentation. and that// any documentation, advertising materials, and other materials related// to such distribution and use acknowledge that the software was// developed by the University of Southern California, Information// Sciences Institute. The name of the University may not be used to// endorse or promote products derived from this software without// specific prior written permission.//// THE UNIVERSITY OF SOUTHERN CALIFORNIA makes no representations about// the suitability of this software for any purpose. THIS SOFTWARE IS// PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,// INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.//// Other copyrights might apply to parts of this software and are so// noted when applicable.//// $Header: /nfs/jade/vint/CVSROOT/ns-2/mip-reg.cc,v 1.7 2000/09/01 03:04:06 haoboy Exp $/* * Copyright (c) Sun Microsystems, Inc. 1998 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 Sun Microsystems, Inc. * * 4. The name of the Sun Microsystems, Inc nor may not be used to endorse or * promote products derived from this software without specific prior * written permission. * * SUN MICROSYSTEMS MERCHANTABILITY OF THIS SOFTWARE OR THE SUITABILITY OF THIS * SOFTWARE FOR ANY PARTICULAR PURPOSE. The software is provided "as is" * without express or implied warranty of any kind. * * These notices must be retained in any copies of any part of this software. */// #ident "@(#)mip-reg.cc 1.4 98/08/30 SMI"/* modified by Thomas Schwabe and Joerg Widmer (ICSI) Sep. 2000 */#include <template.h>#include <mip.h>#include <random.h>#include <address.h>#include <mobilenode.h>#include <float.h>#define AGENT_ADS_SIZE 48#define REG_REQUEST_SIZE 52#define HANDOFF_RATIO 0.2 /* TS_JCW */#define OPTIMIZED_HANDOFF /* TS_JCW *///#define MIP_QUIETint hdr_mip::offset_;static class MIPHeaderClass : public PacketHeaderClass {public: MIPHeaderClass() : PacketHeaderClass("PacketHeader/MIP", sizeof(hdr_mip)) { bind_offset(&hdr_mip::offset_); }} class_miphdr;static class MIPBSAgentClass : public TclClass {public: MIPBSAgentClass() : TclClass("Agent/MIPBS") {} TclObject* create(int, const char*const*) { return (new MIPBSAgent()); }} class_mipbsagent;MIPBSAgent::MIPBSAgent() : Agent(PT_UDP), beacon_(1.0), bcast_target_(0), ragent_(0), timer_(this), seqno_(-1), priority_(0), adlftm_(~0){ bind("adSize_", &size_); bind("ad_lifetime_", &adlftm_); size_ = AGENT_ADS_SIZE;}void MIPBSAgent::recv(Packet* p, Handler *){ Tcl& tcl = Tcl::instance(); char *objname = NULL; NsObject *obj = NULL; hdr_mip *miph = hdr_mip::access(p); hdr_ip *iph = hdr_ip::access(p); hdr_cmn *ch = hdr_cmn::access(p); int nodeaddr = Address::instance().get_nodeaddr(addr()); switch (miph->type_) { case MIPT_REG_REQUEST: if (miph->ha_ == (Address::instance().get_nodeaddr(addr()))){ if (miph->ha_ == miph->coa_) { // back home tcl.evalf("%s clear-reg %d", name_, miph->haddr_);#ifndef MIP_QUIET printf("%f HA %s updated, MH back home, packet %i\n", Scheduler::instance().clock(), Address::instance().print_nodeaddr(addr()), ch->uid());#endif } else { tcl.evalf("%s encap-route %d %d %lf", name_, miph->haddr_, miph->coa_, miph->lifetime_);#ifndef MIP_QUIET printf("%f HA %s updated (FA %s), packet %i\n", Scheduler::instance().clock(), Address::instance().print_nodeaddr(addr()), Address::instance().print_nodeaddr(miph->coa_), ch->uid());#endif } iph->dst() = iph->src(); miph->type_ = MIPT_REG_REPLY; miph->priority_ = priority_; // write priority_ of HA } else { iph->daddr() = miph->ha_; iph->dport() = 0;#ifndef MIP_QUIET printf("%f BS %s forwarding reg-request from %s to HA %s, packet %i\n", Scheduler::instance().clock(), Address::instance().print_nodeaddr(addr()), Address::instance().print_nodeaddr(iph->saddr()), Address::instance().print_nodeaddr(miph->ha_), ch->uid());#endif } iph->saddr() = addr(); iph->sport() = port(); // by now should be back to normal route // if dst is the mobile // also initialise forward counter to 0. otherwise routing // agent is going to think pkt is looping and drop it!! ch->num_forwards() = 0; send(p, 0); break; case MIPT_REG_REPLY:#ifndef MIP_QUIET printf("%f BS %s received reply from BS %s, packet %i\n", Scheduler::instance().clock(), Address::instance().print_nodeaddr(addr()), Address::instance().print_nodeaddr(iph->saddr()), ch->uid()); // JCW#endif assert(miph->coa_ == nodeaddr); tcl.evalf("%s get-link %d %d", name_, nodeaddr, miph->haddr_); // // XXX hacking mobileip. all this should go away // when mobileIP for sun-wired model is no longer reqd. // obj = (NsObject*)tcl.lookup(objname = tcl.result()); if (strlen(objname) == 0) objname = "XXX"; tcl.evalf("%s decap-route %d %s %lf", name_, miph->haddr_, objname, miph->lifetime_); iph->src() = iph->dst(); iph->daddr() = miph->haddr_; iph->dport() = 0; miph->priority_ = priority_; // write priority_ of FA (overwriting HA) if (obj == NULL) obj = ragent_; obj->recv(p, (Handler*)0); break; case MIPT_SOL: tcl.evalf("%s get-link %d %d",name_,nodeaddr,miph->haddr_); send_ads(miph->haddr_, (NsObject*)tcl.lookup(tcl.result())); Packet::free(p); break; default: Packet::free(p); break; }}void MIPBSAgent::timeout(int ){ send_ads(); timer_.resched(beacon_);}int MIPBSAgent::command(int argc, const char*const* argv){ if (argc == 3) { if (strcmp(argv[1], "beacon-period") == 0) { beacon_ = atof(argv[2]); timer_.resched(Random::uniform(0, beacon_)); return TCL_OK; } if (strcmp(argv[1], "priority") == 0) { // JCW priority_ = atoi(argv[2]); return TCL_OK; } if (strcmp(argv[1], "bcast-target") == 0) { bcast_target_ = (NsObject *)TclObject::lookup(argv[2]); return TCL_OK; } if (strcmp(argv[1], "ragent") == 0) { ragent_ = (NsObject *)TclObject::lookup(argv[2]); return TCL_OK; } } return (Agent::command(argc, argv));}void MIPBSAgent::send_ads(int dst, NsObject *target){ printf("%f BS send ADS %s\n", Scheduler::instance().clock(), Address::instance().print_nodeaddr(addr())); Packet *p = allocpkt(); hdr_mip *miph = hdr_mip::access(p); hdr_ip *iph = hdr_ip::access(p); miph->haddr_ = miph->ha_ = -1; miph->coa_ = Address::instance().get_nodeaddr(addr()); miph->type_ = MIPT_ADS; miph->lifetime_ = adlftm_; miph->seqno_ = ++seqno_; miph->priority_ = priority_; // JCW if (dst != -1) { iph->daddr() = dst; iph->dport() = 0; } else { hdr_ip *iph = hdr_ip::access(p); hdr_cmn *ch = hdr_cmn::access(p); ch->next_hop_ = IP_BROADCAST; ch->addr_type_ = NS_AF_INET; iph->daddr() = IP_BROADCAST; iph->dport() = 0; } if (target == NULL) { if (bcast_target_) bcast_target_->recv(p, (Handler*) 0); else if (target_) target_->recv(p, (Handler*) 0); else Packet::free(p); // drop; may log in future code } else target->recv(p, (Handler*)0);}void AgtListTimer::expire(Event *) { a_->timeout(MIP_TIMER_AGTLIST);}static class MIPMHAgentClass : public TclClass {public: MIPMHAgentClass() : TclClass("Agent/MIPMH") {} TclObject* create(int, const char*const*) { return (new MIPMHAgent()); }} class_mipmhagent;MIPMHAgent::MIPMHAgent() : Agent(PT_UDP), ha_(-1), coa_(-1), cur_prio_(0), ad_nr(0), beacon_(1.0), force_handoff_(0), bcast_target_(0), agts_(0), rtx_timer_(this), agtlist_timer_(this), seqno_(-1), reglftm_(~0), adlftm_(0.0), node_ (0){ bind("home_agent_", &ha_); bind("rreqSize_", &size_); bind("reg_rtx_", ®_rtx_); bind("reg_lifetime_", ®lftm_); size_ = REG_REQUEST_SIZE; for(int i = 0; i < AD_CACHE_SIZE; ++i) ad_cache[i] = -1;}double MIPMHAgent::distance(MobileNode* t){ double rX, rY, rZ; // loc of receiver double tX, tY, tZ; // loc of xmitter node_->getLoc(&rX, &rY, &rZ); t->getLoc(&tX, &tY, &tZ); return sqrt((rX - tX) * (rX - tX) + (rY - tY) * (rY - tY));}void MIPMHAgent::recv(Packet* p, Handler *){ Tcl& tcl = Tcl::instance(); hdr_mip *miph = hdr_mip::access(p); //printf("%f MH receive %c\n", Scheduler::instance().clock(), miph->type_); switch (miph->type_) { case MIPT_REG_REPLY: {#ifndef MIP_QUIET hdr_cmn *ch = hdr_cmn::access(p); printf("%f MH %s received reply from BS %s [COA: %s], packet %i\n", Scheduler::instance().clock(), Address::instance().print_nodeaddr(addr()), Address::instance().print_nodeaddr(miph->coa_), Address::instance().print_nodeaddr(coa_), ch->uid()); // JCW#endif if (miph->coa_ != coa_) { int valid = 0; for(int i = 0; i < AD_CACHE_SIZE; ++i) { if (miph->coa_ == ad_cache[i]) { valid = 1; break; } } if (!valid) break; // don't register, coa_ was not found in ad_cache
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -