95ca85a
diff -up net-tools-2.0/netstat.c.dup-tcp net-tools-2.0/netstat.c
95ca85a
--- net-tools-2.0/netstat.c.dup-tcp	2012-10-04 11:32:01.437729086 +0200
95ca85a
+++ net-tools-2.0/netstat.c	2012-10-04 11:32:01.441729032 +0200
95ca85a
@@ -502,6 +502,121 @@ static void prg_cache_load(void)
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 */
3f72065
+      free(tcp_node_hash[i]->socket_pair);
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 HAVE_AFNETROM
287e357
 static const char *netrom_state[] =
287e357
 {
95ca85a
@@ -1018,6 +1133,12 @@ static void tcp_do_one(int lnr, const ch
287e357
 	return;
287e357
     }
626e1f7
 
287e357
+    /* make sure that we haven't seen this socket pair before */
626e1f7
+    if (tcp_node_hash_check_and_append(local_addr, local_port, rem_addr, rem_port) < 0) {
626e1f7
+	/*  fprintf(stderr, _("warning, got duplicate tcp line.\n")); */
626e1f7
+	return;
287e357
+    }
287e357
+
626e1f7
 	addr_do_one(local_addr, sizeof(local_addr), 22, ap, &localaddr, local_port, "tcp");
626e1f7
 	addr_do_one(rem_addr, sizeof(rem_addr), 22, ap, &remaddr, rem_port, "tcp");
626e1f7
 
95ca85a
@@ -2355,6 +2476,7 @@ int main
287e357
 	    break;
626e1f7
         wait_continous(reptimer);
287e357
 	prg_cache_clear();
287e357
+	tcp_node_hash_clear();
287e357
     }
287e357
     return (i);
287e357
 }