#include <NetworkInfo.h>
Protected Member Functions | |
virtual void | initialize () |
virtual void | handleMessage (cMessage *msg) |
virtual void | processCommand (const cXMLElement &node) |
virtual void | dumpRoutingInfo (cModule *target, const char *filename, bool append, bool compat) |
void NetworkInfo::initialize | ( | ) | [protected, virtual] |
void NetworkInfo::handleMessage | ( | cMessage * | msg | ) | [protected, virtual] |
void NetworkInfo::processCommand | ( | const cXMLElement & | node | ) | [protected, virtual] |
Called by ScenarioManager whenever a script command needs to be carried out by the module.
The command is represented by the XML element or element tree. The command name can be obtained as:
const char *command = node->getTagName()
Parameters are XML attributes, e.g. a "neighbour" parameter can be retrieved as:
const char *attr = node->getAttribute("neighbour")
More complex input can be passed in child elements.
Implements IScriptable.
00040 { 00041 cModule *target = simulation.getModuleByPath(node.getAttribute("target")); 00042 00043 if (!strcmp(node.getTagName(), "routing")) 00044 { 00045 const char *filename = node.getAttribute("file"); 00046 ASSERT(filename); 00047 const char *mode = node.getAttribute("mode"); 00048 const char *compat = node.getAttribute("compat"); 00049 00050 dumpRoutingInfo(target, filename, (mode && !strcmp(mode, "a")), (compat && !strcmp(compat, "linux"))); 00051 } 00052 else 00053 ASSERT(false); 00054 }
void NetworkInfo::dumpRoutingInfo | ( | cModule * | target, | |
const char * | filename, | |||
bool | append, | |||
bool | compat | |||
) | [protected, virtual] |
Referenced by processCommand().
00057 { 00058 std::ofstream s; 00059 s.open(filename, append?(std::ios::app):(std::ios::out)); 00060 if (s.fail()) 00061 error("cannot open `%s' for write", filename); 00062 00063 if (compat) s << "Kernel IP routing table" << endl; 00064 s << "Destination Gateway Genmask "; 00065 if (compat) s << "Flags "; 00066 s << "Metric "; 00067 if (compat) s << "Ref Use "; 00068 s << "Iface" << endl; 00069 00070 cModule *rtmod = target->getSubmodule("routingTable"); 00071 if (rtmod) 00072 { 00073 std::vector<std::string> lines; 00074 00075 IRoutingTable *rt = check_and_cast<IRoutingTable *>(rtmod); 00076 for (int i = 0; i < rt->getNumRoutes(); i++) 00077 { 00078 IPAddress host = rt->getRoute(i)->getHost(); 00079 00080 if (host.isMulticast()) 00081 continue; 00082 00083 if (rt->getRoute(i)->getInterface()->isLoopback()) 00084 continue; 00085 00086 IPAddress netmask = rt->getRoute(i)->getNetmask(); 00087 IPAddress gateway = rt->getRoute(i)->getGateway(); 00088 int metric = rt->getRoute(i)->getMetric(); 00089 00090 std::ostringstream line; 00091 00092 line << std::left; 00093 IPAddress dest = compat ? host.doAnd(netmask) : host; 00094 line.width(16); 00095 if (dest.isUnspecified()) line << "0.0.0.0"; else line << dest; 00096 00097 line.width(16); 00098 if (gateway.isUnspecified()) line << "0.0.0.0"; else line << gateway; 00099 00100 line.width(16); 00101 if (netmask.isUnspecified()) line << "0.0.0.0"; else line << netmask; 00102 00103 if (compat) 00104 { 00105 int pad = 3; 00106 line << "U"; // routes in INET are always up 00107 if (!gateway.isUnspecified()) line << "G"; else ++pad; 00108 if (netmask.equals(IPAddress::ALLONES_ADDRESS)) line << "H"; else ++pad; 00109 line.width(pad); 00110 line << " "; 00111 } 00112 00113 line.width(7); 00114 if (compat && rt->getRoute(i)->getSource() == IPRoute::IFACENETMASK) 00115 metric = 0; 00116 line << metric; 00117 00118 if (compat) line << "0 0 "; 00119 00120 line << rt->getRoute(i)->getInterfaceName() << endl; 00121 00122 if (compat) 00123 lines.push_back(line.str()); 00124 else 00125 s << line.str(); 00126 } 00127 00128 if (compat) 00129 { 00130 // sort to avoid random order 00131 // typically routing tables are sorted by netmask prefix length (descending) 00132 // sorting by reversed natural order looks weired, but allows easy comparison 00133 // with `route -n | sort -r` output by means of `diff` command... 00134 std::sort(lines.begin(), lines.end()); 00135 for (std::vector<std::string>::reverse_iterator it = lines.rbegin(); it != lines.rend(); it++) 00136 s << *it; 00137 } 00138 } 00139 s << endl; 00140 s.close(); 00141 }