]> git.gsnw.org Git - ftraceroute.git/commitdiff
New parameter interval with time period for checking the next hop
authorGerman Service Network <support@gsnw.de>
Fri, 27 Feb 2026 16:45:12 +0000 (17:45 +0100)
committerGerman Service Network <support@gsnw.de>
Fri, 27 Feb 2026 16:45:12 +0000 (17:45 +0100)
src/ftraceroute.c
src/ftraceroute.h

index 3d086309774d431735b194eca3962974f3f743c9..5ccd71b3667c92d6ac9e2e1661dfd42798d62d2f 100644 (file)
@@ -47,8 +47,9 @@ int main(int argc, char **argv) {
   int max_hops = DEFAULT_MAX_HOPS;
   int probes = DEFAULT_PROBES;
   int timeout_ms = DEFAULT_TIMEOUT_MS;
+  int interval_ms = DEFAULT_INTERVAL_MS;
 
-  while ((opt = getopt(argc, argv, "hv46m:c:t:")) != -1) {
+  while ((opt = getopt(argc, argv, "hv46m:c:t:i:")) != -1) {
     switch (opt) {
       case 'h':
         usage(argv[0]);
@@ -79,8 +80,11 @@ int main(int argc, char **argv) {
       case 't':
         timeout_ms = atoi(optarg);
         break;
+      case 'i':
+        interval_ms = atoi(optarg);
+        break;
       case '?':
-        if(optopt == 'm' || optopt == 'c') {
+        if(optopt == 'm' || optopt == 'c' || optopt == 'i') {
           fprintf(stderr, "Option -%c requires an argument.\n", optopt);
         } else {
           fprintf(stderr, "Unknown option '-%c'\n", optopt);
@@ -104,18 +108,20 @@ int main(int argc, char **argv) {
   printf("Max hops: %d\n", max_hops);
   printf("Probes: %d\n", probes);
   printf("Timeout: %d ms\n", timeout_ms);
+  printf("Interval: %d ms\n", interval_ms);
   printf("--------------\n");
 #endif /* DEBUG || _DEBUG */
 
   if (max_hops <= 0) max_hops = DEFAULT_MAX_HOPS;
   if (probes   <= 0) probes   = DEFAULT_PROBES;
+  if (interval_ms < 0) interval_ms = DEFAULT_INTERVAL_MS;
 
   int num_hosts = argc - optind;
   trace_session_t *sessions = calloc(num_hosts, sizeof(trace_session_t));
 
   // Initialization of all sessions
   for (int i = 0; i < num_hosts; i++) {
-    session_init(&sessions[i], argv[optind + i], max_hops, probes, timeout_ms, i);
+    session_init(&sessions[i], argv[optind + i], max_hops, probes, timeout_ms, interval_ms, i);
   }
 
   // Main loop (event loop)
@@ -160,6 +166,11 @@ int main(int argc, char **argv) {
         process_timeout_check(s);
       }
 
+      // B2) Interval wait between probes
+      if (s->state == STATE_INTERVAL_WAIT) {
+        process_interval_check(s);
+      }
+
       // C) Preparing a new hop
       if (s->state == STATE_PREPARE_HOP) {
         s->current_ttl++;
@@ -201,11 +212,12 @@ int main(int argc, char **argv) {
  * Functions
  */
 
-void session_init(trace_session_t *s, char *host, int mh, int pp, int tm, int idx) {
+void session_init(trace_session_t *s, char *host, int mh, int pp, int tm, int im, int idx) {
   s->host_arg = host;
   s->max_hops = mh;
   s->probes_per_hop = pp;
   s->timeout_ms = tm;
+  s->interval_ms = im;
   s->current_ttl = 0;
   s->reached_dest = false;
   s->ident = (getpid() & 0xFFFF) + idx; // Unique ID per session
@@ -308,6 +320,20 @@ void process_timeout_check(trace_session_t *s) {
   if (elapsed > s->timeout_ms) {
     s->line_off += snprintf(s->line_buf + s->line_off, sizeof(s->line_buf) - s->line_off, "* ");
     s->current_probe++;
+    if (s->interval_ms > 0) {
+      gettimeofday(&s->t_interval, NULL);
+      s->state = STATE_INTERVAL_WAIT;
+    } else {
+      s->state = STATE_SEND_PROBE;
+    }
+  }
+}
+
+void process_interval_check(trace_session_t *s) {
+  struct timeval now;
+  gettimeofday(&now, NULL);
+  double elapsed = ms_between(s->t_interval, now);
+  if (elapsed >= s->interval_ms) {
     s->state = STATE_SEND_PROBE;
   }
 }
@@ -390,7 +416,12 @@ void process_read(trace_session_t *s) {
     
     // Test successful -> Next test
     s->current_probe++;
-    s->state = STATE_SEND_PROBE;
+    if (s->interval_ms > 0) {
+      gettimeofday(&s->t_interval, NULL);
+      s->state = STATE_INTERVAL_WAIT;
+    } else {
+      s->state = STATE_SEND_PROBE;
+    }
   }
 }
 
@@ -472,4 +503,5 @@ void usage(const char *progname) {
   printf("  -m <value>  Set max hops\n");
   printf("  -c <value>  Set probe count\n");
   printf("  -t <value>  Set timeout in ms\n");
+  printf("  -i <value>  Set interval between probes in ms\n");
 }
\ No newline at end of file
index b8b7dc537c0b0e657a92f899b0119c4af84e41c0..abd426586c329ef550bdd91e75ccb3cda97f8291 100644 (file)
 #include <sys/select.h>
 #include <netinet/in.h>
 
+#define DEFAULT_INTERVAL_MS 0
+
+
+
 /* State definition for a session */
 typedef enum {
   STATE_PREPARE_HOP,  // Ready for next hop (increase TTL, initialize buffer)
   STATE_SEND_PROBE,   // Ready to send a sample
   STATE_AWAIT_REPLY,  // Waiting for response (select)
   STATE_FINISHED,     // Goal achieved or Max Hops through
+  STATE_INTERVAL_WAIT,
   STATE_ERROR         // Error case (socket defective, etc.)
 } session_state_t;
 
@@ -36,6 +41,8 @@ typedef struct {
     int max_hops;
     int probes_per_hop;
     int timeout_ms;
+    int interval_ms;
+    struct timeval t_interval;
     
     // Current progress
     int current_ttl;
@@ -52,10 +59,11 @@ typedef struct {
     bool printed_addr;            // Has the address for this hop already been buffered?
 } trace_session_t;
 
-void session_init(trace_session_t *s, char *host, int mh, int pp, int tm, int idx);
+void session_init(trace_session_t *s, char *host, int mh, int pp, int tm, int im, int idx);
 void session_close(trace_session_t *s);
 void process_send(trace_session_t *s);
 void process_timeout_check(trace_session_t *s);
+void process_interval_check(trace_session_t *s);
 void process_read(trace_session_t *s);
 void flush_line(trace_session_t *s);
 unsigned short checksum(void *b, int len);