148 #if defined(__linux__) || defined(__FreeBSD__) 150 #define __USE_XOPEN // fix of implicit declaration of function 'strptime' 157 #elif defined(WIN32) || defined (WIN64) 158 #include <winsock2.h> 165 #if defined(WIN32) || defined (WIN64) 166 #define snprintf(dst, ...) _snprintf_s((dst), _countof(dst), __VA_ARGS__) 167 #define strtoull _strtoui64 168 #define timegm _mkgmtime 172 "The replay example can replay a data file stored with the \"capture\" example.\n" 173 "The packets will be transmitted according to the TxPort field in the\n" 174 "packet descriptors. The TxPort field is relative to port0 on the adapter\n" 175 "provided the packets and therefore it is needed to select a port0 offset\n" 176 "via the option \"-p\".\n" 177 "The replay rate will be identical to the rate at which the packets were received.\n" 181 "replayGS -f <file> [-p <port offset>] [-g <timestamp>] [-s <starttime>] [-t] [--help] \n" 206 #define OPTION_FILE (1<<1) 207 #define OPTION_PORTOFFSET (1<<2) 208 #define OPTION_GSYNC (1<<3) 209 #define OPTION_START (1<<4) 210 #define OPTION_TIMESTAMP (1<<5) 217 OPT_STRING(
'f',
"file", &
opt_file,
"Specifies the file to replay.", NULL, 0, 0,
"file name"),
219 "should be replayed.", NULL, 0, 0,
"offset"),
221 "Earliest 10-ns ticks time stamp to synchronize with", NULL, 0, 0,
"timestamp"),
223 "Format: YYYY/MM/DD-HH:MM:SS, HH:MM:SS or HH:MM", NULL, 0, 0,
"starttime"),
224 OPT_BOOLEAN(
't', NULL, &
opt_time,
"Find the first time stamp (in 10-ns ticks) in the capture file.", NULL, 0, 0, NULL),
231 #if defined(WIN32) || defined (WIN64) 253 printf(
"\nNapatech Example - %s\n",
progname);
255 for (i = 0; i < 78; ++
i) {
287 tmp_ts = (time_t) (ts / 100000000);
288 loc_time = localtime(&tmp_ts);
289 snprintf(ts_buf,
sizeof(ts_buf),
"%04d/%02d/%02d-%02d:%02d:%02d.%09lu",
290 loc_time->tm_year+1900, loc_time->tm_mon+1, loc_time->tm_mday,
291 loc_time->tm_hour, loc_time->tm_min, loc_time->tm_sec,
292 (
unsigned long) (ts % 100000000 * 10));
293 printf(
"%s", ts_buf);
342 static uint64_t
CalcOffset(uint64_t ts_start, uint64_t ts1, uint64_t ts2)
344 uint64_t offset = 0U;
345 uint64_t ts_min = 0U;
347 ts_min = ts1 <= ts2 ? ts1 : ts2;
348 offset = ts_start - ts_min;
371 if ((status =
NT_ConfigOpen(&hStream,
"Replay")) != NT_SUCCESS) {
373 fprintf(stderr,
"NT_ConfigOpen() failed: %s\n", errorBuffer);
381 if ((status =
NT_ConfigRead(hStream, &configRead)) != NT_SUCCESS) {
383 fprintf(stderr,
"NT_ConfigRead() failed: %s\n", errorBuffer);
392 fprintf(stderr,
"NT_ConfigOpen() failed: %s\n", errorBuffer);
419 if ((status =
NT_ConfigOpen(&hStream,
"Replay")) != NT_SUCCESS) {
421 fprintf(stderr,
"NT_ConfigOpen() failed: %s\n", errorBuffer);
435 if ((status =
NT_ConfigWrite(hStream, &configRead)) != NT_SUCCESS) {
437 fprintf(stderr,
"NT_ConfigWrite() failed: %s\n", errorBuffer);
445 fprintf(stderr,
"NT_ConfigOpen() failed: %s\n", errorBuffer);
473 fprintf(stderr,
"NT_NetFileOpen() failed: %s\n", errorBuffer);
478 if ((status =
NT_NetFileGet(hNetFile, &hNetBufFile)) != NT_SUCCESS) {
479 if (status == NT_STATUS_END_OF_FILE) {
480 fprintf(stderr,
"The file %s has no data\n",
opt_file);
485 fprintf(stderr,
"NT_NetFileGet() failed: %s\n", errorBuffer);
521 memset(&tm, 0,
sizeof(tm));
523 rem_d =
strptime(dts,
"%Y/%m/%d-%H:%M:%S", &tm);
526 rem_d =
strptime(dts,
"%x %X", &tm);
528 memset(&tm, 0,
sizeof(tm));
532 memset(&tm, 0,
sizeof(tm));
534 rem_d =
strptime(dts,
"%H:%M:%S", &tm);
536 memset(&tm, 0,
sizeof(tm));
538 rem_d =
strptime(dts,
"%H:%M", &tm);
549 loc_time = localtime(&now);
550 tm.tm_year = loc_time->tm_year;
551 tm.tm_mon = loc_time->tm_mon;
552 tm.tm_mday = loc_time->tm_mday;
558 if (t == (time_t) -1)
585 fprintf(stderr,
"Reading adapter timestamp failed\n");
588 diff = (((int64_t)((firstTS + offset) - ts) / 100000000) + 1);
590 printf(
"Transmission begins on ");
592 printf(
" (%lld sec) \r", (
unsigned long long int)diff);
598 int main(
int argc,
const char *argv[])
602 uint64_t numPackets=0;
617 i=(int)strlen(&argv[0][0]);
619 for ( ; i != 0; i--) {
621 if ((p[i-1]==
'\\') || (p[i-1]==
'/')) {
623 if ((p[i-1]==
'.') || (p[i-1]==
'\\') || (p[i-1]==
'/')) {
648 fprintf(stderr,
">>> Error: \"-s %s\" time format is invalid\n",
opt_start);
663 fprintf(stderr,
"No file selected.\n");
669 if (
optionMask & ~(OPTION_FILE | OPTION_TIMESTAMP)) {
670 fprintf(stderr,
"Only option -f is allowed with option -t.\n");
677 fprintf(stderr,
"Missing portoffset -p.\n");
683 fprintf(stderr,
"Missing start time -s.\n");
697 fprintf(stderr,
"NT_Init() failed: %s\n", errorBuffer);
712 printf(
"Timestamp = %llu\n", (
long long unsigned int)
gsyncFirstTS);
717 #if defined(WIN32) || defined (WIN64) 720 struct sigaction newaction;
721 memset(&newaction, 0,
sizeof(newaction));
723 if (sigaction(SIGINT, &newaction, NULL) < 0) {
724 fprintf(stderr,
"Failed to register SIGINT sigaction.\n");
734 if ((status =
NT_InfoOpen(&hInfo,
"replay")) != NT_SUCCESS) {
736 fprintf(stderr,
"NT_InfoOpen() failed: %s\n", errorBuffer);
743 if ((status =
NT_InfoRead(hInfo, &infoRead)) != NT_SUCCESS) {
745 fprintf(stderr,
"NT_InfoOpen() failed: %s\n", errorBuffer);
754 fprintf(stderr,
"\"-p %d\" is not a offset to a port0 on a physical adapter.\n", opt_portOffset);
767 fprintf(stderr,
"Global sync is not supported by this adapter no. %d.\n",
gsyncAdapter);
773 fprintf(stderr,
"Global sync cannot be used when running relative TX timing.\n");
779 fprintf(stderr,
"Global sync can only be used when using \"native unix\" timestamps.\n");
785 fprintf(stderr,
"Please use the \"CaptureReplay\" profile.\n");
795 fprintf(stderr,
"Reading adapter timestamp failed\n");
801 fprintf(stderr,
"Error: Start time is in the past.\n");
818 fprintf(stderr,
"Setting Global Sync failed\n");
824 fprintf(stderr,
"Printing start time failed\n");
836 fprintf(stderr,
"NT_NetFileOpen() failed: %s\n", errorBuffer);
844 fprintf(stderr,
"NT_NetTxOpen() failed: %s\n", errorBuffer);
853 if ((status =
NT_NetFileGet(hNetFile, &hNetBufFile)) != NT_SUCCESS) {
854 if (status == NT_STATUS_END_OF_FILE) {
860 fprintf(stderr,
"NT_NetFileGet() failed: %s\n", errorBuffer);
870 fprintf(stderr,
"NtNetFileRead_t() failed: %s\n", errorBuffer);
878 if (status != NT_STATUS_TIMEOUT) {
881 fprintf(stderr,
"NT_NetFileGet() failed: %s\n", errorBuffer);
886 fprintf(stderr,
"Printing start time failed\n");
900 if (firstPacket == 1) {
902 NT_NET_SET_PKT_TXNOW((&pktNetBuf), 0);
903 NT_NET_SET_PKT_TXSETCLOCK((&pktNetBuf), 0);
912 numBytes+=NT_NET_GET_PKT_WIRE_LENGTH((&pktNetBuf));
920 fprintf(stderr,
"NT_NetTxRelease() failed: %s\n", errorBuffer);
928 fprintf(stderr,
"NT_NetFileRelease() failed: %s\n", errorBuffer);
933 if (!(numPackets % 1000)) {
934 printf(
"Packets transmitted so far: %llu \r",
935 (
unsigned long long) numPackets);
963 fprintf(stderr,
"Reading adapter timestamp failed\n");
975 printf(
"Awaiting completion in %lld seconds \r", (
long long int)diff); fflush(stdout);
977 #if defined(__linux__) || defined(__FreeBSD__) 979 #elif defined(WIN32) || defined (WIN64) 998 fprintf(stderr,
"Disabling Global Sync failed\n");
1002 printf(
"Done: %llu packets %llu bytes has been replayed \n", (
unsigned long long int)numPackets, (
unsigned long long int)numBytes);