forward2.cc
来自「BCAST Implementation for NS2」· CC 代码 · 共 611 行 · 第 1/2 页
CC
611 行
// -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-// Copyright (c) 2001-2003 International Computer Science Institute//// 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 XORP 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 XORP LICENSE file; the license in that file is// legally binding.#ident "$XORP: xorp/fea/click_elements/forward2.cc,v 1.2 2003/03/10 23:20:18 hodson Exp $"#ifdef HAVE_CONFIG_H# include <config.h>#endif// ALWAYS INCLUDE click/config.h#include <click/config.h>// ALWAYS INCLUDE click/package.hh#include <click/package.hh>#include <click/error.hh>#include <click/confparse.hh>#include "fea/fti.hh"#include "forward2.hh"#include "rtable2.hh"Forward2::Forward2(){ // CONSTRUCTOR MUST MOD_INC_USE_COUNT MOD_INC_USE_COUNT; route_swap(); _intransaction = false; _transactionID = 0; add_input();}Forward2::~Forward2(){ // DESTRUCTOR MUST MOD_DEC_USE_COUNT MOD_DEC_USE_COUNT;}int Forward2::configure(const Vector<String> &conf, ErrorHandler *errh){ int max_port = -1; for(int i = 0; i < conf.size(); i++) {// click_chatter((String)conf[i]); unsigned int dst, mask; unsigned int gw = 0; int port; Vector<String> words; cp_spacevec(conf[i], words); /* ** The options are: ** address/mask [gw optional] port ** address/prefix [gw optional] port */ if(!((2 == words.size()) || (3 == words.size()))) { String tmp = conf[i]; errh->warning("bad argument line %d: %s", i + 1, tmp.cc()); return -1; } /* ** Pick out the destination */ if(!cp_ip_prefix(words[0], (unsigned char *)&dst, (unsigned char *)&mask, true, this)) { errh->warning("bad argument line %d: %s should be addr/mask", i + 1, words[0].cc()); return -1; } /* ** If there are three words pick out the optional gateway. */ if((3 == words.size())) { if(!cp_ip_address(words[1], (unsigned char *)&gw, this)) { errh->warning("bad argument line %d: %s should be addr", i + 1, words[1].cc()); return -1; } } /* ** port */ if(!cp_integer(words.back(), &port)) { errh->warning( "bad argument line %d: %s should be port number", i + 1, words.back().cc()); return -1; } _rt_current->add(dst, mask, gw, port); max_port = port > max_port ? port : max_port; } set_noutputs(max_port + 1); return 0;}intForward2::initialize(ErrorHandler *){// errh->message("Successfully linked with package!"); // Rtable2 rt; _rt_current->test(); return 0;}voidForward2::push(int, Packet *p){ IPV4address a = p->dst_ip_anno().addr(); IPV4address gw; int oport; if(!_rt_current->lookup(a, gw, oport)) { click_chatter("Forward2::push: No route for %s", (const char *)a.string()); p->kill(); return; }// click_chatter("Forward2::push: %s", (const char *)a.string()); output(oport).push(p); return;}voidForward2::add_handlers(){// click_chatter("Forward2::add_handlers()"); /* ** Handlers to communicate with the fea */ add_write_handler("start_transaction", write_handler_start_transaction, (void *)this); add_read_handler("start_transaction", read_handler_start_transaction, (void *)this); add_write_handler("commit_transaction", write_handler_commit_transaction, (void *)this); add_write_handler("abort_transaction", write_handler_abort_transaction, (void *)this); add_write_handler("break_transaction", write_handler_break_transaction, (void *)this); add_write_handler("delete_all_entries", write_handler_delete_all_entries, (void *)this); add_write_handler("delete_entry", write_handler_delete_entry, (void *)this); add_write_handler("add_entry", write_handler_add_entry, (void *)this); add_read_handler("table", read_handler_table, (void *)this); add_write_handler("lookup_route", write_handler_lookup_route, (void *)this); add_read_handler("lookup_route", read_handler_lookup_route, (void *)this); add_write_handler("lookup_entry", write_handler_lookup_entry, (void *)this); add_read_handler("lookup_entry", read_handler_lookup_entry, (void *)this);}/*** XXX - This function should be moved into a utility file somewhere.** Used to split a string on newlines into a vector of strings.*/static void split_on_newline(const String &str, Vector<String> &conf){ for(int i = 0, pos = 0, newpos;; i++, pos = newpos + 1) {// click_chatter("%d index %d\n", i, str.find_left('\n', pos)); if(-1 == (newpos = str.find_left('\n', pos))) break; conf.push_back(str.substring(pos, newpos - pos));// click_chatter("conf[%d] = <%s>\n", i, conf[i].cc()); }}/*** Start a transaction.** In order the start a transaction write the transaction type into** the "start_transaction".** ** Then read back from the interface to get the transaction ID.*/intForward2::write_handler_start_transaction(const String &addr, Element *, void *thunk, ErrorHandler *errh){ Forward2 *me = (Forward2 *)thunk; if(me->_intransaction) return errh->error("In transaction"); me->_transactionID++; click_chatter("Setting transaction ID to %d\n", me->_transactionID); me->_intransaction = true; /* ** Take a copy of the current routing table and place it into the ** shadow copy. */ me->route_sync(); return 0;}StringForward2::read_handler_start_transaction(Element *, void *thunk){ Forward2 *me = (Forward2 *)thunk; return String(me->_transactionID) + String("\n");}/*** Commit transaction.** Perform a write with the correct transactionID*/intForward2::write_handler_commit_transaction(const String &addr, Element *, void *thunk, ErrorHandler *errh){ Forward2 *me = (Forward2 *)thunk; int tid; if(!me->_intransaction) return errh->error("Not in transaction"); if(!cp_integer(cp_uncomment(addr), (int *)&tid)) return errh->error("transaction ID required <%s>", String(addr).cc()); if(tid != me->_transactionID) return errh->error("transaction ID supplied <%s> does not match <%d>", String(addr).cc(), me->_transactionID); me->_intransaction = false; /* ** Make the shadow table that we have been operating on the ** current routing table. */ me->route_swap(); return 0;}/*** Abort transaction.** Perform a write with the correct transactionID*/intForward2::write_handler_abort_transaction(const String &addr, Element *, void *thunk, ErrorHandler *errh){ Forward2 *me = (Forward2 *)thunk; int tid; if(!me->_intransaction) return errh->error("Not in transaction"); if(!cp_integer(cp_uncomment(addr), (int *)&tid)) return errh->error("transaction ID required <%s>", String(addr).cc()); if(tid != me->_transactionID) return errh->error("transaction ID supplied <%s> does not match <%d>", String(addr).cc(), me->_transactionID); me->_intransaction = false; return 0;}/*** Break transaction.** Perform a write with the correct transactionID*/intForward2::write_handler_break_transaction(const String &addr, Element *, void *thunk, ErrorHandler *errh){
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?