jpopelka / rpms / net-tools

Forked from rpms/net-tools 4 years ago
Clone
612949
diff -up net-tools-2.0/netstat.c.dup-tcp net-tools-2.0/netstat.c
612949
--- net-tools-2.0/netstat.c.dup-tcp	2012-10-04 11:32:01.437729086 +0200
612949
+++ net-tools-2.0/netstat.c	2012-10-04 11:32:01.441729032 +0200
612949
@@ -502,6 +502,121 @@ static void prg_cache_load(void)
612949
 			 " will not be shown, you would have to be root to see it all.)\n"));
612949
 }
612949
 
612949
+#define TCP_HASH_SIZE 1009
612949
+
612949
+static struct tcp_node {
612949
+  struct tcp_node *next;
612949
+  char            *socket_pair;
612949
+} *tcp_node_hash[TCP_HASH_SIZE];
612949
+
612949
+static unsigned int tcp_node_compute_string_hash(const char *p)
612949
+{
612949
+  unsigned int h = *p;
612949
+
612949
+  if (h)
612949
+    for (p += 1; *p != '\0'; p++)
612949
+      h = (h << 5) - h + *p;
612949
+
612949
+  return h;
612949
+}
612949
+
612949
+#define TCP_NODE_HASH_STRING(x) \
612949
+  (tcp_node_compute_string_hash(x) % TCP_HASH_SIZE)
612949
+
612949
+static void tcp_node_hash_clear(void)
612949
+{
612949
+  int i;
612949
+  struct tcp_node *next_node;
612949
+  struct tcp_node *tmp_node;
612949
+  for (i=0; i < TCP_HASH_SIZE; i++) {
612949
+    if (tcp_node_hash[i]) {
612949
+      /* free the children of this hash bucket */
612949
+      next_node = tcp_node_hash[i]->next;
612949
+      while (next_node) {
612949
+	tmp_node = next_node;
612949
+	next_node = next_node->next;
612949
+	free(tmp_node->socket_pair);
612949
+	free(tmp_node);
612949
+      }
612949
+
612949
+      /* free the bucket itself */
612949
+      free(tcp_node_hash[i]->socket_pair);
612949
+      free(tcp_node_hash[i]);
612949
+      tcp_node_hash[i] = NULL;
612949
+    }
612949
+  }
612949
+}
612949
+
612949
+/* This function takes a socket pair string.  If it already exists in
612949
+   the hash it returns -1, otherwise it returns 0. */
612949
+
612949
+static int tcp_node_hash_check_and_append(const char *local_addr,
612949
+					  int local_port,
612949
+					  const char *rem_addr,
612949
+					  int rem_port)
612949
+{
612949
+  unsigned int hash_val;
612949
+  struct tcp_node *tmp_node;
612949
+  int   tmp_string_len;
612949
+  char *tmp_string;;
612949
+
612949
+  /* Size of the string is the size of the two lengths of the address
612949
+     strings plus enough sizes for the colons and the ports. */
612949
+  tmp_string_len = strlen(local_addr) + strlen(rem_addr) + 32;
612949
+  tmp_string = malloc(tmp_string_len);
612949
+  if (!tmp_string)
612949
+    return 0;
612949
+
612949
+  if (snprintf(tmp_string, tmp_string_len - 1, "%s:%d:%s:%d",
612949
+	       local_addr, local_port, rem_addr, rem_port) < 0) {
612949
+    free(tmp_string);
612949
+    return 0;
612949
+  }
612949
+
612949
+  hash_val = TCP_NODE_HASH_STRING(tmp_string);
612949
+
612949
+  /* See if we have to allocate this node */
612949
+  if (!tcp_node_hash[hash_val]) {
612949
+    tcp_node_hash[hash_val] = malloc(sizeof(struct tcp_node));
612949
+    if (!tcp_node_hash[hash_val]) {
612949
+      free(tmp_string);
612949
+      return 0;
612949
+    }
612949
+
612949
+    memset(tcp_node_hash[hash_val], 0, sizeof(struct tcp_node));
612949
+
612949
+    /* Stuff this new value into the hash bucket and return early */
612949
+    tcp_node_hash[hash_val]->socket_pair = tmp_string;
612949
+    return 0;
612949
+  }
612949
+
612949
+  /* Try to find the value in the hash bucket. */
612949
+  tmp_node = tcp_node_hash[hash_val];
612949
+  while (tmp_node) {
612949
+    if (!strcmp(tmp_node->socket_pair, tmp_string)) {
612949
+      free(tmp_string);
612949
+      return -1;
612949
+    }
612949
+    tmp_node = tmp_node->next;
612949
+  }
612949
+
612949
+  /* If we got this far it means that it isn't in the hash bucket.
612949
+     Add it to the front since it's faster that way. */
612949
+  tmp_node = tcp_node_hash[hash_val];
612949
+
612949
+  tcp_node_hash[hash_val] = malloc(sizeof(struct tcp_node));
612949
+  if (!tcp_node_hash[hash_val]) {
612949
+    free(tmp_string);
612949
+    tcp_node_hash[hash_val] = tmp_node;
612949
+    return 0;
612949
+  }
612949
+
612949
+  tcp_node_hash[hash_val]->socket_pair = tmp_string;
612949
+  tcp_node_hash[hash_val]->next = tmp_node;
612949
+
612949
+  return 0;
612949
+}
612949
+
612949
 #if HAVE_AFNETROM
612949
 static const char *netrom_state[] =
612949
 {
612949
@@ -1018,6 +1133,12 @@ static void tcp_do_one(int lnr, const ch
612949
 	return;
612949
     }
612949
 
612949
+    /* make sure that we haven't seen this socket pair before */
612949
+    if (tcp_node_hash_check_and_append(local_addr, local_port, rem_addr, rem_port) < 0) {
612949
+	/*  fprintf(stderr, _("warning, got duplicate tcp line.\n")); */
612949
+	return;
612949
+    }
612949
+
612949
 	addr_do_one(local_addr, sizeof(local_addr), 22, ap, &localaddr, local_port, "tcp");
612949
 	addr_do_one(rem_addr, sizeof(rem_addr), 22, ap, &remaddr, rem_port, "tcp");
612949
 
612949
@@ -2355,6 +2476,7 @@ int main
612949
 	    break;
612949
         wait_continous(reptimer);
612949
 	prg_cache_clear();
612949
+	tcp_node_hash_clear();
612949
     }
612949
     return (i);
612949
 }