287e357
--- net-tools-1.60/netstat.c.foo	Mon Apr 22 14:25:20 2002
287e357
+++ net-tools-1.60/netstat.c	Mon Apr 22 14:25:22 2002
287e357
@@ -435,6 +435,162 @@
287e357
 			 " will not be shown, you would have to be root to see it all.)\n"));
287e357
 }
287e357
 
287e357
+#define TCP_HASH_SIZE 1009
287e357
+
287e357
+static struct tcp_node {
287e357
+  struct tcp_node *next;
287e357
+  char            *socket_pair;
287e357
+} *tcp_node_hash[TCP_HASH_SIZE];
287e357
+
287e357
+static unsigned int tcp_node_compute_string_hash(const char *p)
287e357
+{
287e357
+  unsigned int h = *p;
287e357
+
287e357
+  if (h)
287e357
+    for (p += 1; *p != '\0'; p++)
287e357
+      h = (h << 5) - h + *p;
287e357
+
287e357
+  return h;
287e357
+}
287e357
+
287e357
+#define TCP_NODE_HASH_STRING(x) \
287e357
+  (tcp_node_compute_string_hash(x) % TCP_HASH_SIZE)
287e357
+
287e357
+static void tcp_node_hash_clear(void)
287e357
+{
287e357
+  int i;
287e357
+  struct tcp_node *next_node;
287e357
+  struct tcp_node *tmp_node;
287e357
+  for (i=0; i < TCP_HASH_SIZE; i++) {
287e357
+    if (tcp_node_hash[i]) {
287e357
+      /* free the children of this hash bucket */
287e357
+      next_node = tcp_node_hash[i]->next;
287e357
+      while (next_node) {
287e357
+	tmp_node = next_node;
287e357
+	next_node = next_node->next;
287e357
+	free(tmp_node->socket_pair);
287e357
+	free(tmp_node);
287e357
+      }
287e357
+
287e357
+      /* free the bucket itself */
287e357
+      free(tcp_node_hash[i]);
287e357
+      tcp_node_hash[i] = NULL;
287e357
+    }
287e357
+  }
287e357
+}
287e357
+
287e357
+/* This function takes a socket pair string.  If it already exists in
287e357
+   the hash it returns -1, otherwise it returns 0. */
287e357
+
287e357
+static int tcp_node_hash_check_and_append(const char *local_addr,
287e357
+					  int local_port,
287e357
+					  const char *rem_addr,
287e357
+					  int rem_port)
287e357
+{
287e357
+  unsigned int hash_val;
287e357
+  struct tcp_node *tmp_node;
287e357
+  int   tmp_string_len;
287e357
+  char *tmp_string;;
287e357
+
287e357
+  /* Size of the string is the size of the two lengths of the address
287e357
+     strings plus enough sizes for the colons and the ports. */
287e357
+  tmp_string_len = strlen(local_addr) + strlen(rem_addr) + 32;
287e357
+  tmp_string = malloc(tmp_string_len);
287e357
+  if (!tmp_string)
287e357
+    return 0;
287e357
+
287e357
+  if (snprintf(tmp_string, tmp_string_len - 1, "%s:%d:%s:%d",
287e357
+	       local_addr, local_port, rem_addr, rem_port) < 0) {
287e357
+    free(tmp_string);
287e357
+    return 0;
287e357
+  }
287e357
+
287e357
+  hash_val = TCP_NODE_HASH_STRING(tmp_string);
287e357
+
287e357
+  /* See if we have to allocate this node */
287e357
+  if (!tcp_node_hash[hash_val]) {
287e357
+    tcp_node_hash[hash_val] = malloc(sizeof(struct tcp_node));
287e357
+    if (!tcp_node_hash[hash_val]) {
287e357
+      free(tmp_string);
287e357
+      return 0;
287e357
+    }
287e357
+
287e357
+    memset(tcp_node_hash[hash_val], 0, sizeof(struct tcp_node));
287e357
+
287e357
+    /* Stuff this new value into the hash bucket and return early */
287e357
+    tcp_node_hash[hash_val]->socket_pair = tmp_string;
287e357
+    return 0;
287e357
+  }
287e357
+
287e357
+  /* Try to find the value in the hash bucket. */
287e357
+  tmp_node = tcp_node_hash[hash_val];
287e357
+  while (tmp_node) {
287e357
+    if (!strcmp(tmp_node->socket_pair, tmp_string)) {
287e357
+      free(tmp_string);
287e357
+      return -1;
287e357
+    }
287e357
+    tmp_node = tmp_node->next;
287e357
+  }
287e357
+
287e357
+  /* If we got this far it means that it isn't in the hash bucket.
287e357
+     Add it to the front since it's faster that way. */
287e357
+  tmp_node = tcp_node_hash[hash_val];
287e357
+
287e357
+  tcp_node_hash[hash_val] = malloc(sizeof(struct tcp_node));
287e357
+  if (!tcp_node_hash[hash_val]) {
287e357
+    free(tmp_string);
287e357
+    tcp_node_hash[hash_val] = tmp_node;
287e357
+    return 0;
287e357
+  }
287e357
+
287e357
+  tcp_node_hash[hash_val]->socket_pair = tmp_string;
287e357
+  tcp_node_hash[hash_val]->next = tmp_node;
287e357
+
287e357
+  return 0;
287e357
+}
287e357
+
287e357
+#if 0
287e357
+static void tcp_node_hash_report_bucket_size(void)
287e357
+{
287e357
+  int max = 0;
287e357
+  int min = 0;
287e357
+  int num = 0;
287e357
+  int total = 0;
287e357
+  struct tcp_node *tmp_node;
287e357
+  int tmp, i;
287e357
+  float avg;
287e357
+
287e357
+  for (i=0; i < TCP_HASH_SIZE; i++) {
287e357
+    tmp_node = tcp_node_hash[i];
287e357
+    if (!tmp_node)
287e357
+      continue;
287e357
+
287e357
+    tmp = 0;
287e357
+
287e357
+    num++;
287e357
+    tmp = 1;
287e357
+
287e357
+    while (tmp_node) {
287e357
+      tmp++;
287e357
+      tmp_node = tmp_node->next;
287e357
+    }
287e357
+    
287e357
+    total += tmp;
287e357
+    if (tmp > max)
287e357
+      max = tmp;
287e357
+    
287e357
+    if (min == 0 || tmp < min)
287e357
+      min = tmp;
287e357
+  }
287e357
+
287e357
+  avg = (float)total/(float)num;
287e357
+
287e357
+  printf("%d nodes in %d buckets min/max/avg %d/%d/%.2f\n",
287e357
+	 total, num, min, max, avg);
287e357
+
287e357
+}
287e357
+#endif
287e357
+
287e357
 #if HAVE_AFNETROM
287e357
 static const char *netrom_state[] =
287e357
 {
287e357
@@ -752,11 +908,20 @@
287e357
 	fprintf(stderr, _("warning, got bogus tcp line.\n"));
287e357
 	return;
287e357
     }
287e357
+
287e357
     if ((ap = get_afntype(((struct sockaddr *) &localaddr)->sa_family)) == NULL) {
287e357
 	fprintf(stderr, _("netstat: unsupported address family %d !\n"),
287e357
 		((struct sockaddr *) &localaddr)->sa_family);
287e357
 	return;
287e357
     }
287e357
+
287e357
+    /* make sure that we haven't seen this socket pair before */
287e357
+    if (tcp_node_hash_check_and_append(local_addr, local_port,
287e357
+				       rem_addr, rem_port) < 0) {
e9cf15f
+  /*  fprintf(stderr, _("warning, got duplicate tcp line.\n")); */
287e357
+      return;
287e357
+    }
287e357
+
287e357
     if (state == TCP_LISTEN) {
287e357
 	time_len = 0;
287e357
 	retr = 0L;
Zdenek Prikryl 9634070
@@ -1880,6 +2045,7 @@
287e357
 	    break;
Zdenek Prikryl 9634070
 	sleep(reptimer);
287e357
 	prg_cache_clear();
287e357
+	tcp_node_hash_clear();
287e357
     }
287e357
     return (i);
287e357
 }