1 --- net-tools-1.60/netstat.c.foo Mon Apr 22 14:25:20 2002
2 +++ net-tools-1.60/netstat.c Mon Apr 22 14:25:22 2002
4 " will not be shown, you would have to be root to see it all.)\n"));
7 +#define TCP_HASH_SIZE 1009
9 +static struct tcp_node {
10 + struct tcp_node *next;
12 +} *tcp_node_hash[TCP_HASH_SIZE];
14 +static unsigned int tcp_node_compute_string_hash(const char *p)
16 + unsigned int h = *p;
19 + for (p += 1; *p != '\0'; p++)
20 + h = (h << 5) - h + *p;
25 +#define TCP_NODE_HASH_STRING(x) \
26 + (tcp_node_compute_string_hash(x) % TCP_HASH_SIZE)
28 +static void tcp_node_hash_clear(void)
31 + struct tcp_node *next_node;
32 + struct tcp_node *tmp_node;
33 + for (i=0; i < TCP_HASH_SIZE; i++) {
34 + if (tcp_node_hash[i]) {
35 + /* free the children of this hash bucket */
36 + next_node = tcp_node_hash[i]->next;
38 + tmp_node = next_node;
39 + next_node = next_node->next;
40 + free(tmp_node->socket_pair);
44 + /* free the bucket itself */
45 + free(tcp_node_hash[i]);
46 + tcp_node_hash[i] = NULL;
51 +/* This function takes a socket pair string. If it already exists in
52 + the hash it returns -1, otherwise it returns 0. */
54 +static int tcp_node_hash_check_and_append(const char *local_addr,
56 + const char *rem_addr,
59 + unsigned int hash_val;
60 + struct tcp_node *tmp_node;
64 + /* Size of the string is the size of the two lengths of the address
65 + strings plus enough sizes for the colons and the ports. */
66 + tmp_string_len = strlen(local_addr) + strlen(rem_addr) + 32;
67 + tmp_string = malloc(tmp_string_len);
71 + if (snprintf(tmp_string, tmp_string_len - 1, "%s:%d:%s:%d",
72 + local_addr, local_port, rem_addr, rem_port) < 0) {
77 + hash_val = TCP_NODE_HASH_STRING(tmp_string);
79 + /* See if we have to allocate this node */
80 + if (!tcp_node_hash[hash_val]) {
81 + tcp_node_hash[hash_val] = malloc(sizeof(struct tcp_node));
82 + if (!tcp_node_hash[hash_val]) {
87 + memset(tcp_node_hash[hash_val], 0, sizeof(struct tcp_node));
89 + /* Stuff this new value into the hash bucket and return early */
90 + tcp_node_hash[hash_val]->socket_pair = tmp_string;
94 + /* Try to find the value in the hash bucket. */
95 + tmp_node = tcp_node_hash[hash_val];
97 + if (!strcmp(tmp_node->socket_pair, tmp_string)) {
101 + tmp_node = tmp_node->next;
104 + /* If we got this far it means that it isn't in the hash bucket.
105 + Add it to the front since it's faster that way. */
106 + tmp_node = tcp_node_hash[hash_val];
108 + tcp_node_hash[hash_val] = malloc(sizeof(struct tcp_node));
109 + if (!tcp_node_hash[hash_val]) {
111 + tcp_node_hash[hash_val] = tmp_node;
115 + tcp_node_hash[hash_val]->socket_pair = tmp_string;
116 + tcp_node_hash[hash_val]->next = tmp_node;
122 +static void tcp_node_hash_report_bucket_size(void)
128 + struct tcp_node *tmp_node;
132 + for (i=0; i < TCP_HASH_SIZE; i++) {
133 + tmp_node = tcp_node_hash[i];
144 + tmp_node = tmp_node->next;
151 + if (min == 0 || tmp < min)
155 + avg = (float)total/(float)num;
157 + printf("%d nodes in %d buckets min/max/avg %d/%d/%.2f\n",
158 + total, num, min, max, avg);
164 static const char *netrom_state[] =
166 @@ -752,11 +908,20 @@
167 fprintf(stderr, _("warning, got bogus tcp line.\n"));
171 if ((ap = get_afntype(((struct sockaddr *) &localaddr)->sa_family)) == NULL) {
172 fprintf(stderr, _("netstat: unsupported address family %d !\n"),
173 ((struct sockaddr *) &localaddr)->sa_family);
177 + /* make sure that we haven't seen this socket pair before */
178 + if (tcp_node_hash_check_and_append(local_addr, local_port,
179 + rem_addr, rem_port) < 0) {
180 + /* fprintf(stderr, _("warning, got duplicate tcp line.\n")); */
184 if (state == TCP_LISTEN) {
187 @@ -1880,6 +2045,7 @@
191 + tcp_node_hash_clear();