00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #include <oasys/util/StringBuffer.h>
00040
00041 #include "RouteCommand.h"
00042 #include "CompletionNotifier.h"
00043
00044 #include "contacts/Link.h"
00045 #include "contacts/ContactManager.h"
00046
00047 #include "bundling/BundleEvent.h"
00048 #include "bundling/BundleDaemon.h"
00049
00050 #include "routing/BundleRouter.h"
00051 #include "routing/RouteEntry.h"
00052
00053 namespace dtn {
00054
00055 RouteCommand::RouteCommand()
00056 : TclCommand("route")
00057 {
00058 bind_s("type", &BundleRouter::Config.type_, "static",
00059 "Which routing algorithm to use.");
00060
00061 bind_b("add_nexthop_routes",
00062 &BundleRouter::Config.add_nexthop_routes_, true,
00063 "Whether or not to automatically add routes for next hop links");
00064
00065 bind_i("default_priority",
00066 &BundleRouter::Config.default_priority_, 0,
00067 "Default priority for new routes (initially zero)");
00068
00069 add_to_help("add <dest> <link/endpoint> [opts]", "add a route");
00070 add_to_help("del <dest> <link/endpoint>", "delete a route");
00071 add_to_help("dump", "dump all of the static routes");
00072 }
00073
00074 int
00075 RouteCommand::exec(int argc, const char** argv, Tcl_Interp* interp)
00076 {
00077 (void)interp;
00078
00079 if (argc < 2) {
00080 resultf("need a route subcommand");
00081 return TCL_ERROR;
00082 }
00083
00084 const char* cmd = argv[1];
00085
00086 if (strcmp(cmd, "add") == 0) {
00087
00088 if (argc < 4) {
00089 wrong_num_args(argc, argv, 2, 4, INT_MAX);
00090 return TCL_ERROR;
00091 }
00092
00093 const char* dest_str = argv[2];
00094
00095 EndpointIDPattern dest(dest_str);
00096 if (!dest.valid()) {
00097 resultf("invalid destination eid %s", dest_str);
00098 return TCL_ERROR;
00099 }
00100 const char* name = argv[3];
00101 Link* link = NULL;
00102
00103 link = BundleDaemon::instance()->contactmgr()->find_link(name);
00104
00105 if (link == NULL) {
00106 resultf("no such link %s", name);
00107 return TCL_ERROR;
00108 }
00109
00110 RouteEntry* entry = new RouteEntry(dest, link);
00111
00112
00113
00114 argc -= 4;
00115 argv += 4;
00116 if (argc != 0 && (entry->parse_options(argc, argv) != argc))
00117 {
00118 resultf("invalid argument '%s'", argv[0]);
00119 return TCL_ERROR;
00120 }
00121
00122
00123
00124
00125
00126
00127
00128 if (BundleDaemon::instance()->started()) {
00129 BundleDaemon::post_and_wait(new RouteAddEvent(entry),
00130 CompletionNotifier::notifier());
00131 } else {
00132 BundleDaemon::post(new RouteAddEvent(entry));
00133 }
00134
00135 return TCL_OK;
00136 }
00137
00138 else if (strcmp(cmd, "del") == 0) {
00139
00140 if (argc != 3) {
00141 wrong_num_args(argc, argv, 2, 3, 3);
00142 return TCL_ERROR;
00143 }
00144
00145 EndpointIDPattern pat(argv[2]);
00146 if (!pat.valid()) {
00147 resultf("invalid endpoint id pattern '%s'", argv[2]);
00148 return TCL_ERROR;
00149 }
00150
00151 if (BundleDaemon::instance()->started()) {
00152 BundleDaemon::post_and_wait(new RouteDelEvent(pat),
00153 CompletionNotifier::notifier());
00154 } else {
00155 BundleDaemon::post(new RouteDelEvent(pat));
00156 }
00157
00158 return TCL_OK;
00159 }
00160
00161 else if (strcmp(cmd, "dump") == 0) {
00162 oasys::StringBuffer buf;
00163 BundleDaemon::instance()->get_routing_state(&buf);
00164 set_result(buf.c_str());
00165 return TCL_OK;
00166 }
00167
00168 else if (strcmp(cmd, "local_eid") == 0) {
00169 if (argc == 2) {
00170
00171 set_result(BundleDaemon::instance()->local_eid().c_str());
00172 return TCL_OK;
00173
00174 } else if (argc == 3) {
00175
00176 BundleDaemon::instance()->set_local_eid(argv[2]);
00177 if (! BundleDaemon::instance()->local_eid().valid()) {
00178 resultf("invalid eid '%s'", argv[2]);
00179 return TCL_ERROR;
00180 }
00181 if (! BundleDaemon::instance()->local_eid().known_scheme()) {
00182 resultf("local eid '%s' has unknown scheme", argv[2]);
00183 return TCL_ERROR;
00184 }
00185 } else {
00186 wrong_num_args(argc, argv, 2, 2, 3);
00187 return TCL_ERROR;
00188 }
00189 }
00190
00191 else {
00192 resultf("unimplemented route subcommand %s", cmd);
00193 return TCL_ERROR;
00194 }
00195
00196 return TCL_OK;
00197 }
00198
00199 }