port-map.cc

00001 /*
00002  * Copyright (c) 2007 Regents of the SIGNET lab, University of Padova.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the University of Padova (SIGNET lab) nor the 
00014  *    names of its contributors may be used to endorse or promote products 
00015  *    derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
00018  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 
00019  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
00020  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
00021  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00022  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
00023  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
00024  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
00025  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
00026  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
00027  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00030 #include <ip.h>
00031 
00032 #include "port-map.h"
00033 
00034 #include<iostream>
00035 
00036 static class PortMapClass : public TclClass 
00037 {
00038 public:
00039   PortMapClass() : TclClass("Module/Port/Map") {}
00040   TclObject* create(int, const char*const*) {  return (new PortMap);  }
00041 } class_port_map_module;
00042 
00043 PortMap::PortMap() 
00044   : portcounter(0)
00045 {
00046   bind("debug_",&debug_);
00047 }
00048 
00049 PortMap::~PortMap()
00050 {
00051 }
00052 
00053 int PortMap::command(int argc, const char*const* argv)
00054 {
00055   Tcl& tcl = Tcl::instance();
00056   if(argc == 3)
00057     {
00058       if (strcasecmp(argv[1],"assignPort")==0)
00059         {
00060           Module *m = dynamic_cast<Module*>(tcl.lookup(argv[2]));
00061           if(!m)
00062             return TCL_ERROR;
00063 
00064           int port = assignPort(m);
00065           tcl.resultf("%d", port);
00066           return TCL_OK;
00067           
00068         }
00069 
00070 
00071         
00072     }
00073   return Module::command(argc, argv);
00074 }
00075 
00076 
00077 
00078 void PortMap::recv(Packet *p)
00079 {
00080   fprintf(stderr, "PortMap: a Packet is sent without source module!!\n");
00081   Packet::free(p);
00082 }
00083 
00084 
00085 
00086 
00087 void PortMap::recv(Packet *p, int idSrc)
00088 {
00089   hdr_cmn *ch = HDR_CMN(p);
00090   hdr_ip *iph = HDR_IP(p);
00091 
00092   if(ch->direction() == hdr_cmn::UP)
00093     {
00094    
00095       map<int, int>::const_iterator iter = id_map.find(iph->dport());
00096 
00097       if(iter == id_map.end())
00098         {
00099           // Unknown Port Number
00100           if (debug_) 
00101             std::cerr << "PortMap::recv() (dir:UP) " 
00102                       << " dport=" << iph->dport()
00103                       << " portcounter=" << portcounter
00104                       << std::endl;               
00105           drop(p,1,"UPN");
00106           return;
00107         }
00108       
00109       int id = iter->second;      
00110       if(debug_ > 5)
00111         printf("dest port %d id %d\n", iph->dport(), id);
00112       sendUp(id, p);
00113     }
00114   else
00115     {
00116       // direction DOWN
00117       map<int, int>::const_iterator iter = port_map.find(idSrc);
00118 
00119       if (iter == port_map.end())
00120         {
00121           fprintf(stderr, "PortMap::recv() no port assigned to id %d, dropping packet!!!\n", idSrc);
00122           Packet::free(p);
00123           return;
00124         }
00125       
00126       int sport = iter->second;
00127       assert(sport>0 && sport <= portcounter);
00128       iph->sport() = sport;
00129       sendDown(p);
00130     }
00131 }
00132 
00133 
00134 int PortMap::assignPort(Module* m)
00135 {
00136   int id =  m->getId();
00137 
00138   // Check that the provided module has not been given a port before 
00139   if (port_map.find(id) != port_map.end())
00140     return TCL_ERROR;
00141 
00142   portcounter++;
00143 
00144   int newport = portcounter;
00145 
00146   port_map[id] = newport;
00147   assert(id_map.find(newport) == id_map.end());
00148   id_map[newport] = id;
00149   assert(id_map.find(newport) != id_map.end());
00150 
00151   if (debug_)
00152     std::cerr << "PortMap::assignPort() " 
00153               << " id=" << id
00154               << " port=" << newport
00155               << " portcounter=" << portcounter
00156               << std::endl;              
00157   
00158   return newport;
00159 }

Generated on Wed Nov 26 15:47:28 2008 for NS-MIRACLE library by  doxygen 1.5.2