?? itgsend.cpp
字號(hào):
MUTEX_THREAD_LOCK(mutexLog);
if (((logging) && (isFirstThread == 1)) || ((meter == METER_RTTM) && (isFirstThreadRttm == 1))) {
#ifdef DEBUG
printf("Log! \n");
#endif
isFirstThread = 0;
isFirstThreadRttm = 0;
if (!logremoto) {
out.open(logFile, ios::out | ios::binary | ios::trunc);
if (!out) {
printf("** WARNING ** Flow %d. Cannot create local logfile\n", id);
logging = 0;
}
}
else
createRemoteLogFile(logHost, protoTxLog);
}
MUTEX_THREAD_UNLOCK(mutexLog);
if (connect(sock, DestHost->ai_addr, DestHost->ai_addrlen) < 0) {
perror("flowSender");
printf("Error in connect(). Flow ID: %d\n", id);
msg.code = MSG_SM_ERRFLOW;
if (sendPipeMsg(signalChannels[sigChanId].pipe, &msg) < 0) {
perror("flowSender sending msg");
exitThread();
}
exitThread();
}
#ifdef DEBUG
printf("DS byte %d\n",DSByte);
#endif
if (DSByte && setsockopt(sock, IPPROTO_IP, IP_TOS, (char *) &DSByte, sizeof(DSByte)) < 0)
printf("** WARNING ** Flow %d. Could not set DS byte to: %d\n", id, DSByte);
#ifdef DEBUG
printf("TTL %d\n", TTL);
#endif
if (TTL >0 && (setsockopt(sock, IPPROTO_IP, IP_TTL, (char *) &TTL, sizeof(TTL)) < 0))
printf("** WARNING ** Flow %d. Could not set TTL to: %d\n", id, TTL);
#ifdef LINUX_OS
getsockname(sock, SrcHost->ai_addr, &SrcHost->ai_addrlen);
#endif
#ifdef WIN32
getsockname(sock, SrcHost->ai_addr, (int *) &SrcHost->ai_addrlen);
#endif
getInfo(SrcHost, tmpPort_SrcPort, HelpSrcAddress);
getInfo(DestHost, tmpPort_DstPort, HelpDstAddress);
if (l4Proto == L4_PROTO_TCP)
ptrSize = ptrTimeUsec + sizeof(int);
printf("Starting sending packets of flow ID: %d\n", id);
Ticker.count = 0.0;
memcpy(payload, &id, sizeof(unsigned int));
TSTART(_tstart, secondi, microsecondi, first, meter, 0);
GET_TIME_OF_DAY(&start_time, _tend, _tstart, secondi, microsecondi, meter, 0);
end_time.tv_sec = start_time.tv_sec + Duration / 1000 + (end_time.tv_usec =
start_time.tv_usec + (Duration % 1000) * 1000) / 1000000;
end_time.tv_usec %= 1000000;
GET_TIME_OF_DAY(&Ticker.lastTime, _tend, _tstart, secondi, microsecondi, meter, 0);
if (meter == METER_RTTM) {
start_time.tv_sec = 0;
start_time.tv_usec = 0;
FD_ZERO(&active_set);
FD_SET(sock, &active_set);
}
#ifdef WIN32
if (flows[id].serial != INVALID_HANDLE_VALUE){
RTS_Enable(flows[id].serial);
RTS_Disable(flows[id].serial);
DTR_Enable(flows[id].serial);
}
#endif
#ifdef LINUX_OS
if (flows[id].serial != INVALID_HANDLE_VALUE){
RTS_Disable(flows[id].serial);
}
#endif
do {
if (l4Proto == L4_PROTO_TCP)
((size = (int) PktSize->Next()) >= MAX_PAYLOAD_SIZE) ? size =
MAX_PAYLOAD_SIZE : (size < (MinPayloadSize + (int) sizeof(int))) ? size =
(MinPayloadSize + (int) sizeof(int)) : 1;
else
((size = (int) PktSize->Next()) >= MAX_PAYLOAD_SIZE) ? size =
MAX_PAYLOAD_SIZE : (size < MinPayloadSize) ? size = MinPayloadSize : 1;
time = Ticker.lastTime.tv_sec % 86400L;
memcpy(ptrSeqNum, &seqNum, sizeof(unsigned int));
memcpy(ptrTimeSec, &(time), sizeof(long int));
memcpy(ptrTimeUsec, &(Ticker.lastTime.tv_usec), sizeof(long int));
if (l4Proto == L4_PROTO_TCP)
memcpy(ptrSize, &size, sizeof(int));
if ((l4Proto == L4_PROTO_ICMP) && (meter == METER_OWDM)) {
if (DestHost->ai_family == AF_INET) {
icmppkt pkt;
pkt.icmp_buf.icmp_type = icmptype;
pkt.icmp_buf.icmp_code = 0;
pkt.icmp_buf.icmp_cksum = 0;
memcpy(&pkt.packet, payload, size);
pkt.icmp_buf.icmp_cksum =
checksum((unsigned short *) &pkt, size + sizeof(icmp));
sockchk =
sendto(sock, (char *) &pkt, size + sizeof(icmp), 0, DestHost->ai_addr,
DestHost->ai_addrlen);
} else if (DestHost->ai_family == AF_INET6) {
#ifdef LINUX_OS
icmppktv6 pkt;
pkt.icmp_buf.icmp_type = icmptype;
pkt.icmp_buf.icmp_code = 0;
pkt.icmp_buf.icmp_cksum = 0;
memcpy(&pkt.packet, payload, size);
pkt.icmp_buf.icmp_cksum =
checksum((unsigned short *) &pkt, size + sizeof(icmpv6));
struct sockaddr_in6 from;
from.sin6_family=AF_INET6;
from.sin6_port=0;
from.sin6_addr=((struct sockaddr_in6*)(DestHost->ai_addr))->sin6_addr;
sockchk =
sendto(sock, (char *) &pkt, size + sizeof(icmpv6), 0, (sockaddr *)&from,
sizeof(from));
#endif
}
} else {
sockchk = send(sock, (char *) payload, size, 0);
}
if (flows[id].serial != INVALID_HANDLE_VALUE){
DTR_Disable(flows[id].serial);
DTR_Enable(flows[id].serial);
}
if (sockchk < 0)
printf("** WARNING ** flow: %d Packet: %d not sent\n", id, seqNum);
else {
if (meter == METER_RTTM) {
read_set = active_set;
if (select(FD_SETSIZE, &read_set, NULL, NULL, &start_time) < 0) {
perror("flowSender");
printf("invalid fd or operation interrupted by signal\n");
msg.code = MSG_SM_ERRFLOW;
if (sendPipeMsg(signalChannels[sigChanId].pipe, &msg) < 0) {
perror("flowSender sending msg");
exitThread();
}
exitThread();
}
if (FD_ISSET(sock, &read_set)) {
GET_TIME_OF_DAY(&RcvTime, _tend, _tstart, secondi, microsecondi, meter, 0);
if (l4Proto == L4_PROTO_TCP)
size_r=TCPrecvPacket((unsigned char*)payload,sock);
else
size_r = recv(sock, (char *) payload, MAX_PAYLOAD_SIZE, 0);
time1 = (*(long int *) ptrTimeSec);
time2 = RcvTime.tv_sec % 86400L;
if (((logging) && (size_r > 0)) || (meter == METER_RTTM)) {
writeInBuffer(&infos[count],*(unsigned int *) payload,*(unsigned int *) ptrSeqNum,
HelpSrcAddress, HelpDstAddress, tmpPort_SrcPort, tmpPort_DstPort, time1,time2,*(long int *)
ptrTimeUsec,RcvTime.tv_usec,size);
count++;
if (count == DIM) {
flushBuffer(infos, count);
count = 0;
}
}
}
}
if ((logging) && (meter == METER_OWDM)) {
if (l7Proto == L7_PROTO_TELNET)
size = size - 20;
writeInBuffer(&infos[count],*(unsigned int *) payload,*(unsigned int *) ptrSeqNum,
HelpSrcAddress, HelpDstAddress, tmpPort_SrcPort, tmpPort_DstPort,time,time,Ticker.lastTime.tv_usec,Ticker.lastTime.tv_usec,size);
count++;
if (count == DIM) {
flushBuffer(infos, count);
count = 0;
}
}
}
wait = IntArriv->Next();
UPDATE_TICKER(&Ticker, _tend, _tprec, _tstart, first_update);
if (wait <= Ticker.count)
Ticker.count -= wait;
else {
while (wait > Ticker.count) {
wait -= Ticker.count;
Ticker.count = 0.0;
timeout.tv_sec = (long) wait / 1000;
timeout.tv_usec = (long) (wait - (timeout.tv_sec * 1000)) * 1000;
select(1, NULL, NULL, NULL, &timeout);
UPDATE_TICKER(&Ticker, _tend, _tprec, _tstart, first_update);
}
Ticker.count -= wait;
}
#ifdef WIN32
GET_TIME_OF_DAY(&Ticker.lastTime, _tend, _tstart, secondi, microsecondi, meter, 0);
#endif
seqNum++;
} while (Ticker.lastTime.tv_sec < end_time.tv_sec
|| (Ticker.lastTime.tv_sec == end_time.tv_sec && Ticker.lastTime.tv_usec < end_time.tv_usec));
if (flows[id].serial != INVALID_HANDLE_VALUE)
RTS_Enable(flows[id].serial);
if (meter == METER_RTTM) {
bool flag = true;
while (flag) {
start_time.tv_sec = rttmDelay;
read_set = active_set;
if (select(FD_SETSIZE, &read_set, NULL, NULL, &start_time) < 0) {
perror("send_packrttm");
flag = true;
} else {
if (FD_ISSET(sock, &read_set)) {
GET_TIME_OF_DAY(&RcvTime, _tend, _tstart, secondi, microsecondi,
meter,0);
if (l4Proto == L4_PROTO_TCP)
TCPrecvPacket((unsigned char*)payload,sock);
else
size_r = recv(sock, (char *) payload, MAX_PAYLOAD_SIZE, 0);
time1 = *(long int *) ptrTimeSec;
time2 = RcvTime.tv_sec % 86400L;
if (((logging) && (size_r > 0)) || (meter == METER_RTTM)) {
writeInBuffer(&infos[count],*(unsigned int *) payload,*(unsigned int *) ptrSeqNum,
HelpSrcAddress, HelpDstAddress, tmpPort_SrcPort, tmpPort_DstPort, time1,time2,
*(long int*)ptrTimeSec,RcvTime.tv_usec,size);
count++;
if (count == DIM) {
flushBuffer(infos, count);
count = 0;
}
}
} else {
flag = false;
}
}
}
}
printf("Finished sending packets of flow ID: %d\n", id);
if ((logging) || (meter == METER_RTTM)) {
flushBuffer(infos, count);
count = 0;
}
free(infos);
closeSock(sock);
msg.code = MSG_SM_ENDFLOW;
if (sendPipeMsg(signalChannels[sigChanId].pipe, &msg) < 0) {
perror("flowSender sending msg");
exitThread();
}
}
void Terminate(int sig)
{
if (logremoto) {
signaling signaling_log;
signaling_log.stop = true;
sendto(logSockSignaling, (char *) &signaling_log, sizeof(signaling_log), 0,
logHost->ai_addr, logHost->ai_addrlen);
if (closeSock(logSock) < 0)
printf("Error into close log sock\n");
if (closeSock(logSockSignaling) < 0)
printf("Error into close log sock signaling\n");
} else if (logging) {
out.close();
}
memClean();
#ifdef LINUX_OS
exit(1);
#endif
#ifdef WIN32
WSACleanup();
ExitProcess(0);
#endif
}
void printHelp()
{
cout << " Name " << endl;
cout << " ITGSend - Sender componet of D-ITG platform" << endl << endl;
cout << " Synopsis" << endl<<endl;
cout << " - In case of using a script file to generate multiple flows:" << endl;
cout << " ./ITGSend <script_file> [-l [<logfile>]] [-L [<log_server_addr>]" <<endl;
cout << " [<protocol_type>]][-X [<log_server_addr>] [<protocol_type>]]" <<endl;
cout << " [-x [<receiver_logfile>]] "<<endl<<endl;
cout << " - If you want to remotely control the sender, launch it in daemon mode:" <<
endl;
cout << " ./ITGSend -Q [-l [<logfile>]] [-L [<log_server_addr>] [<protocol_type>]]" <<endl;
cout <<
" [-x [<receiver_logfile>]] [-X [<log_server_addr>] [<protocol_type>]]"
<< endl << endl;
cout << " - Otherwise:" << endl;
cout << " ./ITGSend [-m <msr_type>][-a <destination_address>][-rp <destination_port>]" <<endl;
cout << " [-sk <sender_serial_iface>] [-rk <receiver_serial_iface>]" << endl;
cout << " [-b <DS byte>] [-l [<logfile>]] [-L [<log_server_addr>]" <<
endl;
cout << " [<protocol _type>]] [-X [<log_server_addr>] [<protocol_type>]]" << endl;
cout << " [-x [<logfile_receiver>]] [-t <duration>] [-d <gen_delay>]" << endl;
cout << " [-T <protocol_type>] [-s <seed>] [-D] [-f <TTL>] ";
#ifdef WIN32
cout << " [-P] ";
#endif
cout << endl;
cout << " [-sp <source_port>]" << endl;
cout << " [[ -C <pkts_per_s> | -U <min_pkts_per_s max_pkts_per_s> | " <<
endl;
cout << " -E <average_pkts_per_s> | -V <shape> <scale> | -Y <shap> <scale> |" <<
endl;
cout << " -N <mean> <std_dev> | -O <average_pkts_per_s> | -G <shape scale>] " <<
endl;
cout << " [ -u <min_pkt_size> <max_pkt_size> | -e <average_pkt_size> |" << endl;
cout << " -v <shape> <scale> | -y <shape> <scale> | -n <mean> <std_dev> | " << endl;
cout << " -o <average_pkt_size> | -g <shape> <scale> | -c <pkt_size> ]] | " << endl;
cout << " [[Telnet] [DNS] [[VoIP] [-x <codec_type>] [-h <protocol_type>] " << endl;
cout << " [-VAD]]]" << endl;
cout << endl << " Options" << endl << endl;
cout <<
" -m <msr_type>\t\t\tSet the type of meter.\n\t\t\t\t\tValues: owdm (one way delay meter)\n\t\t\t\t\trttm (round trip time meter)\n\t\t\t\t\t(Default: owdm)"
<< endl;
cout << " -a <destination_address>\t\tSet the destination address\n\t\t\t\t\t\(Default: " << DefaultDestIP << ")" << endl;
cout << " -rp <destination_port>\t\tSet the destination port \n\t\t\t\t\t\(Default: " << DEFAULT_PORT << ") " << endl;
cout << " -sp <source_port>\t\t\tSet the source port \n\t\t\t\t\t\(Default: Set by O.S.)" << endl;
cout << " -sk <sender_serial_iface>\t\tSender serial interface"<<endl;
cout << " -rk <receiver_serial_iface>\t\tReceiver serial interface"<<endl;
cout << " -s <seed>\t\t\t\tSet the seed \n\t\t\t\t\t\(Default: Random)" << endl;
cout << " -t <duration>\t\t\tSet the generation duration\n\t\t\t\t\t(Default: " <<
DefaultDuration << " msecs)" << endl;
cout << " -d <generation_delay>\t\t\tSet the generation delay\n\t\t\t\t\t\(Default: " << DefaultDelay << " msecs)" << endl;
cout << " -D \t\t\t\t\tNagle Algorithm Disabled" << endl;
#ifdef WIN32
cout << " -P \t\t\t\t\tEnable thread high priority " << endl;
#endif
cout << " -l <logfile>\t\t\t\tGenerate log file\n\t\t\t\t\t(Default: " << DefaultLogFile <<
")" << endl;;
cout << " -x <receiver_logfile>\t\tGenerate log file at the receiver side \n\t\t\t\t\t(Default: " << DefaultRecvLogFile<<
")" << endl;;
cout << " -L <log_server_address> \t\tRemote log file \n\t<protocol_type> \t\t(Default: <" <<
DEFAULT_LOG_IP << "> <" << invFindL4Proto(DEFAULT_PROTOCOL_TX_LOG) << ">)"<< endl;;
cout << " -Z <log_server_address> \t\tReceiver remote log file\n\t<protocol_type>\t\t\t(Default: <"
<< DEFAULT_LOG_IP << "> <" <<invFindL4Proto(DEFAULT_PROTOCOL_TX_LOG) << ">)" << endl;
cout << " -T <protocol_type>\t\t\t(Default: " << invFindL4Proto(L4_PROTO_UDP) <<
") NOTE: Protocol\n\t\t\t\t\tTypes are: UDP,TCP, ICMP" << endl;
cout << " -b <DS_byte>\t\t\t\tSet the DS byte for QoS tests\n\t\t\t\t\t(Default: " << DefaultDSByte << ")" << endl;
cout << " -f <TTL byte>\t\t\t\tSet the time to live (TTL) \n\t\t\t\t\t(Default: 64)" << endl;
cout <<
" -C <pkts_per_s>\t\t\t[DEFAULT] constant inter-arrival\n\t\t\t\t\t(Default: " <<
DefaultPktPerSec << " pkts/s)" << endl;
cout <<
" -U <min_pkts_per_s> <max_pkts_per_s>\tuniformly distributed\n\t\t\t\t\tinter-arrival"
<< endl;
cout <<
" -E <average_pkts_per_s>\t\texponentially distributed\n\t\t\t\t\tinter-arrival"
<< endl;
cout << " -V <shape> <scale>\t\t\tpareto distributed\n\t\t\t\t\tinter-arrival" << endl;
cout << " -Y <shape> <scale>\t\t\tcauchy distributed\n\t\t\t\t\tinter-arrival" << endl;
cout << " -N <mean> <std_dev>\t\t\tnormal distributed\n\t\t\t\t\tinter-arrival" <<
endl;
cout << " -O <mean>\t\t\t\tpoisson distributed\n\t\t\t\t\tinter-arrival" << endl;
cout << " -G <shape> <scale>\t\t\tgamma distributed\n\t\t\t\t\tinter-arrival" << endl;
cout << " -c <pkt_size>\t\t\t\t[DEFAULT] constant payload size\n\t\t\t\t\t(Default: "
<< DefaultPktSize << " bytes)" << endl;
cout <<
" -u <min_pkt_size> <max_pkt_size>\tuniformly distributed\n\t\t\t\t\tpayload size"
<< endl;
cout << " -e <average_pkt_size>\t\t\texponentially distributed\n\t\t\t\t\tpayload size"
<< endl;
cout << " -v <shape> <scale>\t\t\tpareto distrib
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -