1 diff -up net-tools-2.0/netstat.c.dup-tcp net-tools-2.0/netstat.c
2 --- net-tools-2.0/netstat.c.dup-tcp 2012-10-04 11:32:01.437729086 +0200
3 +++ net-tools-2.0/netstat.c 2012-10-04 11:32:01.441729032 +0200
4 @@ -502,6 +502,121 @@ static void prg_cache_load(void)
5 " will not be shown, you would have to be root to see it all.)\n"));
8 +#define TCP_HASH_SIZE 1009
10 +static struct tcp_node {
11 + struct tcp_node *next;
13 +} *tcp_node_hash[TCP_HASH_SIZE];
15 +static unsigned int tcp_node_compute_string_hash(const char *p)
17 + unsigned int h = *p;
20 + for (p += 1; *p != '\0'; p++)
21 + h = (h << 5) - h + *p;
26 +#define TCP_NODE_HASH_STRING(x) \
27 + (tcp_node_compute_string_hash(x) % TCP_HASH_SIZE)
29 +static void tcp_node_hash_clear(void)
32 + struct tcp_node *next_node;
33 + struct tcp_node *tmp_node;
34 + for (i=0; i < TCP_HASH_SIZE; i++) {
35 + if (tcp_node_hash[i]) {
36 + /* free the children of this hash bucket */
37 + next_node = tcp_node_hash[i]->next;
39 + tmp_node = next_node;
40 + next_node = next_node->next;
41 + free(tmp_node->socket_pair);
45 + /* free the bucket itself */
46 + free(tcp_node_hash[i]->socket_pair);
47 + free(tcp_node_hash[i]);
48 + tcp_node_hash[i] = NULL;
53 +/* This function takes a socket pair string. If it already exists in
54 + the hash it returns -1, otherwise it returns 0. */
56 +static int tcp_node_hash_check_and_append(const char *local_addr,
58 + const char *rem_addr,
61 + unsigned int hash_val;
62 + struct tcp_node *tmp_node;
66 + /* Size of the string is the size of the two lengths of the address
67 + strings plus enough sizes for the colons and the ports. */
68 + tmp_string_len = strlen(local_addr) + strlen(rem_addr) + 32;
69 + tmp_string = malloc(tmp_string_len);
73 + if (snprintf(tmp_string, tmp_string_len - 1, "%s:%d:%s:%d",
74 + local_addr, local_port, rem_addr, rem_port) < 0) {
79 + hash_val = TCP_NODE_HASH_STRING(tmp_string);
81 + /* See if we have to allocate this node */
82 + if (!tcp_node_hash[hash_val]) {
83 + tcp_node_hash[hash_val] = malloc(sizeof(struct tcp_node));
84 + if (!tcp_node_hash[hash_val]) {
89 + memset(tcp_node_hash[hash_val], 0, sizeof(struct tcp_node));
91 + /* Stuff this new value into the hash bucket and return early */
92 + tcp_node_hash[hash_val]->socket_pair = tmp_string;
96 + /* Try to find the value in the hash bucket. */
97 + tmp_node = tcp_node_hash[hash_val];
99 + if (!strcmp(tmp_node->socket_pair, tmp_string)) {
103 + tmp_node = tmp_node->next;
106 + /* If we got this far it means that it isn't in the hash bucket.
107 + Add it to the front since it's faster that way. */
108 + tmp_node = tcp_node_hash[hash_val];
110 + tcp_node_hash[hash_val] = malloc(sizeof(struct tcp_node));
111 + if (!tcp_node_hash[hash_val]) {
113 + tcp_node_hash[hash_val] = tmp_node;
117 + tcp_node_hash[hash_val]->socket_pair = tmp_string;
118 + tcp_node_hash[hash_val]->next = tmp_node;
124 static const char *netrom_state[] =
126 @@ -1018,6 +1133,12 @@ static void tcp_do_one(int lnr, const ch
130 + /* make sure that we haven't seen this socket pair before */
131 + if (tcp_node_hash_check_and_append(local_addr, local_port, rem_addr, rem_port) < 0) {
132 + /* fprintf(stderr, _("warning, got duplicate tcp line.\n")); */
136 addr_do_one(local_addr, sizeof(local_addr), 22, ap, &localaddr, local_port, "tcp");
137 addr_do_one(rem_addr, sizeof(rem_addr), 22, ap, &remaddr, rem_port, "tcp");
139 @@ -2355,6 +2476,7 @@ int main
141 wait_continous(reptimer);
143 + tcp_node_hash_clear();