]> git.ipfire.org Git - people/amarx/ipfire-3.x.git/blob - net-tools/patches/005-net-tools-duplicate-tcp.patch
ntp: Install ntpd with the Base group and enable by default
[people/amarx/ipfire-3.x.git] / net-tools / patches / 005-net-tools-duplicate-tcp.patch
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"));
6 }
7
8 +#define TCP_HASH_SIZE 1009
9 +
10 +static struct tcp_node {
11 + struct tcp_node *next;
12 + char *socket_pair;
13 +} *tcp_node_hash[TCP_HASH_SIZE];
14 +
15 +static unsigned int tcp_node_compute_string_hash(const char *p)
16 +{
17 + unsigned int h = *p;
18 +
19 + if (h)
20 + for (p += 1; *p != '\0'; p++)
21 + h = (h << 5) - h + *p;
22 +
23 + return h;
24 +}
25 +
26 +#define TCP_NODE_HASH_STRING(x) \
27 + (tcp_node_compute_string_hash(x) % TCP_HASH_SIZE)
28 +
29 +static void tcp_node_hash_clear(void)
30 +{
31 + int i;
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;
38 + while (next_node) {
39 + tmp_node = next_node;
40 + next_node = next_node->next;
41 + free(tmp_node->socket_pair);
42 + free(tmp_node);
43 + }
44 +
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;
49 + }
50 + }
51 +}
52 +
53 +/* This function takes a socket pair string. If it already exists in
54 + the hash it returns -1, otherwise it returns 0. */
55 +
56 +static int tcp_node_hash_check_and_append(const char *local_addr,
57 + int local_port,
58 + const char *rem_addr,
59 + int rem_port)
60 +{
61 + unsigned int hash_val;
62 + struct tcp_node *tmp_node;
63 + int tmp_string_len;
64 + char *tmp_string;;
65 +
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);
70 + if (!tmp_string)
71 + return 0;
72 +
73 + if (snprintf(tmp_string, tmp_string_len - 1, "%s:%d:%s:%d",
74 + local_addr, local_port, rem_addr, rem_port) < 0) {
75 + free(tmp_string);
76 + return 0;
77 + }
78 +
79 + hash_val = TCP_NODE_HASH_STRING(tmp_string);
80 +
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]) {
85 + free(tmp_string);
86 + return 0;
87 + }
88 +
89 + memset(tcp_node_hash[hash_val], 0, sizeof(struct tcp_node));
90 +
91 + /* Stuff this new value into the hash bucket and return early */
92 + tcp_node_hash[hash_val]->socket_pair = tmp_string;
93 + return 0;
94 + }
95 +
96 + /* Try to find the value in the hash bucket. */
97 + tmp_node = tcp_node_hash[hash_val];
98 + while (tmp_node) {
99 + if (!strcmp(tmp_node->socket_pair, tmp_string)) {
100 + free(tmp_string);
101 + return -1;
102 + }
103 + tmp_node = tmp_node->next;
104 + }
105 +
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];
109 +
110 + tcp_node_hash[hash_val] = malloc(sizeof(struct tcp_node));
111 + if (!tcp_node_hash[hash_val]) {
112 + free(tmp_string);
113 + tcp_node_hash[hash_val] = tmp_node;
114 + return 0;
115 + }
116 +
117 + tcp_node_hash[hash_val]->socket_pair = tmp_string;
118 + tcp_node_hash[hash_val]->next = tmp_node;
119 +
120 + return 0;
121 +}
122 +
123 #if HAVE_AFNETROM
124 static const char *netrom_state[] =
125 {
126 @@ -1018,6 +1133,12 @@ static void tcp_do_one(int lnr, const ch
127 return;
128 }
129
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")); */
133 + return;
134 + }
135 +
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");
138
139 @@ -2355,6 +2476,7 @@ int main
140 break;
141 wait_continous(reptimer);
142 prg_cache_clear();
143 + tcp_node_hash_clear();
144 }
145 return (i);
146 }