]> git.ipfire.org Git - people/stevee/ipfire-3.x.git/blob - pkgs/net-tools/patches/net-tools-1.60-duplicate-tcp.patch
Change file layout of the makefiles.
[people/stevee/ipfire-3.x.git] / pkgs / net-tools / patches / net-tools-1.60-duplicate-tcp.patch
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
3 @@ -435,6 +435,162 @@
4 " will not be shown, you would have to be root to see it all.)\n"));
5 }
6
7 +#define TCP_HASH_SIZE 1009
8 +
9 +static struct tcp_node {
10 + struct tcp_node *next;
11 + char *socket_pair;
12 +} *tcp_node_hash[TCP_HASH_SIZE];
13 +
14 +static unsigned int tcp_node_compute_string_hash(const char *p)
15 +{
16 + unsigned int h = *p;
17 +
18 + if (h)
19 + for (p += 1; *p != '\0'; p++)
20 + h = (h << 5) - h + *p;
21 +
22 + return h;
23 +}
24 +
25 +#define TCP_NODE_HASH_STRING(x) \
26 + (tcp_node_compute_string_hash(x) % TCP_HASH_SIZE)
27 +
28 +static void tcp_node_hash_clear(void)
29 +{
30 + int i;
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;
37 + while (next_node) {
38 + tmp_node = next_node;
39 + next_node = next_node->next;
40 + free(tmp_node->socket_pair);
41 + free(tmp_node);
42 + }
43 +
44 + /* free the bucket itself */
45 + free(tcp_node_hash[i]);
46 + tcp_node_hash[i] = NULL;
47 + }
48 + }
49 +}
50 +
51 +/* This function takes a socket pair string. If it already exists in
52 + the hash it returns -1, otherwise it returns 0. */
53 +
54 +static int tcp_node_hash_check_and_append(const char *local_addr,
55 + int local_port,
56 + const char *rem_addr,
57 + int rem_port)
58 +{
59 + unsigned int hash_val;
60 + struct tcp_node *tmp_node;
61 + int tmp_string_len;
62 + char *tmp_string;;
63 +
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);
68 + if (!tmp_string)
69 + return 0;
70 +
71 + if (snprintf(tmp_string, tmp_string_len - 1, "%s:%d:%s:%d",
72 + local_addr, local_port, rem_addr, rem_port) < 0) {
73 + free(tmp_string);
74 + return 0;
75 + }
76 +
77 + hash_val = TCP_NODE_HASH_STRING(tmp_string);
78 +
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]) {
83 + free(tmp_string);
84 + return 0;
85 + }
86 +
87 + memset(tcp_node_hash[hash_val], 0, sizeof(struct tcp_node));
88 +
89 + /* Stuff this new value into the hash bucket and return early */
90 + tcp_node_hash[hash_val]->socket_pair = tmp_string;
91 + return 0;
92 + }
93 +
94 + /* Try to find the value in the hash bucket. */
95 + tmp_node = tcp_node_hash[hash_val];
96 + while (tmp_node) {
97 + if (!strcmp(tmp_node->socket_pair, tmp_string)) {
98 + free(tmp_string);
99 + return -1;
100 + }
101 + tmp_node = tmp_node->next;
102 + }
103 +
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];
107 +
108 + tcp_node_hash[hash_val] = malloc(sizeof(struct tcp_node));
109 + if (!tcp_node_hash[hash_val]) {
110 + free(tmp_string);
111 + tcp_node_hash[hash_val] = tmp_node;
112 + return 0;
113 + }
114 +
115 + tcp_node_hash[hash_val]->socket_pair = tmp_string;
116 + tcp_node_hash[hash_val]->next = tmp_node;
117 +
118 + return 0;
119 +}
120 +
121 +#if 0
122 +static void tcp_node_hash_report_bucket_size(void)
123 +{
124 + int max = 0;
125 + int min = 0;
126 + int num = 0;
127 + int total = 0;
128 + struct tcp_node *tmp_node;
129 + int tmp, i;
130 + float avg;
131 +
132 + for (i=0; i < TCP_HASH_SIZE; i++) {
133 + tmp_node = tcp_node_hash[i];
134 + if (!tmp_node)
135 + continue;
136 +
137 + tmp = 0;
138 +
139 + num++;
140 + tmp = 1;
141 +
142 + while (tmp_node) {
143 + tmp++;
144 + tmp_node = tmp_node->next;
145 + }
146 +
147 + total += tmp;
148 + if (tmp > max)
149 + max = tmp;
150 +
151 + if (min == 0 || tmp < min)
152 + min = tmp;
153 + }
154 +
155 + avg = (float)total/(float)num;
156 +
157 + printf("%d nodes in %d buckets min/max/avg %d/%d/%.2f\n",
158 + total, num, min, max, avg);
159 +
160 +}
161 +#endif
162 +
163 #if HAVE_AFNETROM
164 static const char *netrom_state[] =
165 {
166 @@ -752,11 +908,20 @@
167 fprintf(stderr, _("warning, got bogus tcp line.\n"));
168 return;
169 }
170 +
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);
174 return;
175 }
176 +
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")); */
181 + return;
182 + }
183 +
184 if (state == TCP_LISTEN) {
185 time_len = 0;
186 retr = 0L;
187 @@ -1880,6 +2045,7 @@
188 break;
189 sleep(reptimer);
190 prg_cache_clear();
191 + tcp_node_hash_clear();
192 }
193 return (i);
194 }