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
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 #include "tpm.h"
00056
00057
00058 TPMTimer::TPMTimer(TPM* t) {
00059 tpm = t;
00060 };
00061
00062 void TPMTimer::expire(Event *e) {
00063
00064
00065
00066 tpm->run();
00067 }
00068
00069
00070 static class TPMClass : public TclClass {
00071 public:
00072 TPMClass() : TclClass("TPM") {}
00073 TclObject* create(int, const char*const*) {
00074 return (new TPM());
00075 }
00076 } class_tpm;
00077
00078 TPM::TPM() {
00079 tp_default_ = tp_num_ = 0;
00080 tp = NULL;
00081
00082
00083 next_p.time = 0;
00084 next_p.saddr = 0;
00085 next_p.sport = 0;
00086 next_p.daddr = 0;
00087 next_p.dport = 0;
00088 next_p.len = 0;
00089
00090
00091 timer = new TPMTimer(this);
00092 start_t = 0;
00093 }
00094
00095 TPM::~TPM() {
00096 if (fp)
00097 fclose(fp);
00098 if (timer)
00099 delete timer;
00100 }
00101
00102 int TPM::loadLog(const char* filename) {
00103 fp = fopen(filename, "r");
00104 if (fp == 0)
00105 return(0);
00106
00107 return(1);
00108 }
00109
00110 int TPM::start() {
00111 start_t = Scheduler::instance().clock();
00112 processLog();
00113 return(1);
00114 }
00115
00116 int TPM::processLog() {
00117 int time, sport, dport, len;
00118 unsigned int saddr, daddr;
00119
00120 while (!feof(fp)) {
00121 fscanf(fp, "%d %u %d %u %d %d\n",
00122 &time, &saddr, &sport, &daddr, &dport, &len);
00123
00124 if (time > 0) {
00125
00126 next_p.time = time;
00127 next_p.saddr = saddr;
00128 next_p.sport = sport;
00129 next_p.daddr = daddr;
00130 next_p.dport = dport;
00131 next_p.len = len;
00132
00133
00134
00135 double delay = time * 1.0 / 1000000.0;
00136 timer->resched(delay);
00137
00138 return(1);
00139 } else {
00140 genPkt(saddr, sport, daddr, dport, len);
00141 }
00142 }
00143 return(1);
00144 }
00145
00146 int TPM::run() {
00147 genPkt(next_p.saddr, next_p.sport, next_p.daddr, next_p.dport, next_p.len);
00148 processLog();
00149 return(1);
00150 }
00151
00152 int TPM::genPkt(unsigned int saddr, int sport, unsigned int daddr, int dport, int len) {
00153
00154 if (saddr == daddr) {
00155 return(0);
00156 }
00157
00158
00159 unsigned int tp_id = tp_default_;
00160
00161 if (saddr < (unsigned int)tp_num_) {
00162 tp_id = saddr;
00163 }
00164 printf("tpid %d\n", tp_id);
00165 tp[tp_id]->sendto(len, saddr, sport, daddr, dport);
00166
00167 return(1);
00168 }
00169
00170 int TPM::command(int argc, const char*const* argv) {
00171 if (argc == 2) {
00172 if (strcmp(argv[1], "start") == 0) {
00173 if (start())
00174 return(TCL_OK);
00175 else
00176 return(TCL_ERROR);
00177
00178 }
00179
00180 } else if (argc == 3) {
00181 if (strcmp(argv[1], "loadLog") == 0) {
00182 if (loadLog(argv[2]))
00183 return(TCL_OK);
00184 else
00185 return(TCL_ERROR);
00186
00187 } else if (strcmp(argv[1], "tp-num") == 0) {
00188 tp_num_ = atoi(argv[2]);
00189 tp_default_ = tp_num_ - 1;
00190 if (tp_default_ < 0)
00191 tp_default_ = 0;
00192 tp = new TPAgent*[tp_num_];
00193 return(TCL_OK);
00194 } else if (strcmp(argv[1], "tp-default") == 0) {
00195 tp_default_ = atoi(argv[2]);
00196 return(TCL_OK);
00197 }
00198 } else if (argc == 4) {
00199 if (strcmp(argv[1], "tp-reg") == 0) {
00200 int index = atoi(argv[2]);
00201 if (index >= tp_num_)
00202 return(TCL_ERROR);
00203
00204 tp[index] = (TPAgent *) TclObject::lookup(argv[3]);
00205 return(TCL_OK);
00206 }
00207 }
00208
00209 printf("Command specified does not exist\n");
00210 return(TCL_ERROR);
00211 }