]>
Commit | Line | Data |
---|---|---|
67d25278 | 1 | --- net-snmp-5.4.2.1/apps/snmpnetstat/inet.c 2006-04-21 15:30:47.000000000 +0300 |
85c5e829 ER |
2 | +++ net-snmp-5.4.2.1-dev/apps/snmpnetstat/inet.c 2009-01-22 18:26:23.264301468 +0200 |
3 | @@ -106,20 +106,65 @@ | |
4 | }; | |
5 | #define TCP_NSTATES 11 | |
6 | ||
7 | -void | |
8 | -tcpprotopr(const char *name) | |
9 | +static void | |
10 | +tcpprotoprint_line(const char *name, netsnmp_variable_list *vp, int *first) | |
67d25278 | 11 | { |
85c5e829 | 12 | - netsnmp_variable_list *var, *vp; |
67d25278 ER |
13 | - oid tcpConnState_oid[] = { 1,3,6,1,2,1,6,13,1,1 }; |
14 | - size_t tcpConnState_len = OID_LENGTH( tcpConnState_oid ); | |
67d25278 | 15 | int state, width; |
85c5e829 | 16 | + char *cp; |
67d25278 ER |
17 | union { |
18 | struct in_addr addr; | |
85c5e829 ER |
19 | char data[4]; |
20 | } tmpAddr; | |
21 | oid localPort, remotePort; | |
67d25278 | 22 | struct in_addr localAddr, remoteAddr; |
85c5e829 ER |
23 | - char *cp; |
24 | + | |
25 | + state = *vp->val.integer; | |
26 | + if (!aflag && state == MIB_TCPCONNSTATE_LISTEN) { | |
27 | + return; | |
28 | + } | |
67d25278 | 29 | + |
85c5e829 ER |
30 | + if (*first) { |
31 | + printf("Active Internet (%s) Connections", name); | |
32 | + if (aflag) | |
33 | + printf(" (including servers)"); | |
34 | + putchar('\n'); | |
35 | + width = Aflag ? 18 : 22; | |
36 | + printf("%-5.5s %*.*s %*.*s %s\n", | |
37 | + "Proto", -width, width, "Local Address", | |
38 | + -width, width, "Remote Address", "(state)"); | |
39 | + *first = 0; | |
40 | + } | |
41 | + | |
42 | + /* Extract the local/remote information from the index values */ | |
43 | + cp = tmpAddr.data; | |
44 | + cp[0] = vp->name[ 10 ] & 0xff; | |
45 | + cp[1] = vp->name[ 11 ] & 0xff; | |
46 | + cp[2] = vp->name[ 12 ] & 0xff; | |
47 | + cp[3] = vp->name[ 13 ] & 0xff; | |
48 | + localAddr.s_addr = tmpAddr.addr.s_addr; | |
49 | + localPort = ntohs(vp->name[ 14 ]); | |
50 | + cp = tmpAddr.data; | |
51 | + cp[0] = vp->name[ 15 ] & 0xff; | |
52 | + cp[1] = vp->name[ 16 ] & 0xff; | |
53 | + cp[2] = vp->name[ 17 ] & 0xff; | |
54 | + cp[3] = vp->name[ 18 ] & 0xff; | |
55 | + remoteAddr.s_addr = tmpAddr.addr.s_addr; | |
56 | + remotePort = ntohs(vp->name[ 19 ]); | |
67d25278 | 57 | + |
85c5e829 ER |
58 | + printf("%-5.5s", name); |
59 | + inetprint(&localAddr, localPort, name, 1); | |
60 | + inetprint(&remoteAddr, remotePort, name, 0); | |
61 | + if (state < 1 || state > TCP_NSTATES) { | |
62 | + printf("%d\n", state ); | |
63 | + } else { | |
64 | + printf("%s\n", tcpstates[state]); | |
65 | + } | |
66 | +} | |
67 | + | |
68 | +static void | |
69 | +tcpprotopr_get(const char *name, oid *root, size_t root_len) | |
70 | +{ | |
71 | + netsnmp_variable_list *var, *vp; | |
72 | int first = 1; | |
67d25278 ER |
73 | |
74 | /* | |
85c5e829 | 75 | @@ -127,7 +172,7 @@ |
67d25278 ER |
76 | * the necessary information. |
77 | */ | |
85c5e829 | 78 | var = NULL; |
67d25278 | 79 | - snmp_varlist_add_variable( &var, tcpConnState_oid, tcpConnState_len, |
85c5e829 ER |
80 | + snmp_varlist_add_variable( &var, root, root_len, |
81 | ASN_NULL, NULL, 0); | |
82 | if (!var) | |
83 | return; | |
84 | @@ -135,47 +180,113 @@ | |
85 | return; | |
86 | ||
87 | for (vp = var; vp ; vp=vp->next_variable) { | |
67d25278 ER |
88 | - state = *vp->val.integer; |
89 | - if (!aflag && state == MIB_TCPCONNSTATE_LISTEN) | |
90 | - continue; | |
85c5e829 ER |
91 | + tcpprotoprint_line(name, vp, &first); |
92 | + } | |
93 | + snmp_free_varbind( var ); | |
94 | +} | |
67d25278 ER |
95 | |
96 | - if (first) { | |
97 | - printf("Active Internet (%s) Connections", name); | |
98 | - if (aflag) | |
99 | - printf(" (including servers)"); | |
100 | - putchar('\n'); | |
101 | - width = Aflag ? 18 : 22; | |
102 | - printf("%-5.5s %*.*s %*.*s %s\n", | |
103 | - "Proto", -width, width, "Local Address", | |
104 | - -width, width, "Remote Address", "(state)"); | |
105 | - first=0; | |
85c5e829 ER |
106 | +void |
107 | +tcpprotopr_bulkget(const char *name, oid *root, size_t root_len) | |
108 | +{ | |
109 | + netsnmp_variable_list *vp; | |
110 | + netsnmp_pdu *pdu, *response; | |
111 | + oid tcpConnState_oid[MAX_OID_LEN]; | |
112 | + size_t tcpConnState_len; | |
113 | + int first = 1; | |
114 | + int running = 1; | |
115 | + int status; | |
116 | + | |
117 | + /* | |
118 | + * setup initial object name | |
119 | + */ | |
120 | + memmove(tcpConnState_oid, root, sizeof(root) * root_len); | |
121 | + tcpConnState_len = root_len; | |
122 | + | |
123 | + /* | |
124 | + * Walking the tcpConnState column will provide all | |
125 | + * the necessary information. | |
126 | + */ | |
127 | + while (running) { | |
128 | + /* | |
129 | + * create PDU for GETBULK request and add object name to request | |
130 | + */ | |
131 | + pdu = snmp_pdu_create(SNMP_MSG_GETBULK); | |
132 | + pdu->non_repeaters = 0; | |
133 | + pdu->max_repetitions = 1024; /* fill the packet */ | |
134 | + snmp_add_null_var(pdu, tcpConnState_oid, tcpConnState_len); | |
135 | + | |
67d25278 ER |
136 | + /* |
137 | + * do the request | |
138 | + */ | |
139 | + status = snmp_synch_response(ss, pdu, &response); | |
140 | + if (status == STAT_SUCCESS) { | |
141 | + if (response->errstat == SNMP_ERR_NOERROR) { | |
142 | + for (vp = response->variables; vp ; vp=vp->next_variable) { | |
85c5e829 ER |
143 | + if ((vp->name_length < root_len) || |
144 | + (memcmp(root, vp->name, sizeof(oid) * root_len) != 0)) { | |
67d25278 ER |
145 | + /* |
146 | + * not part of this subtree | |
147 | + */ | |
148 | + running = 0; | |
149 | + continue; | |
150 | + } | |
151 | + | |
85c5e829 | 152 | + tcpprotoprint_line(name, vp, &first); |
67d25278 ER |
153 | + |
154 | + if ((vp->type != SNMP_ENDOFMIBVIEW) && | |
155 | + (vp->type != SNMP_NOSUCHOBJECT) && | |
156 | + (vp->type != SNMP_NOSUCHINSTANCE)) { | |
157 | + /* | |
158 | + * Check if last variable, and if so, save for next request. | |
159 | + */ | |
160 | + if (vp->next_variable == NULL) { | |
67d25278 ER |
161 | + memmove(tcpConnState_oid, vp->name, |
162 | + vp->name_length * sizeof(oid)); | |
163 | + tcpConnState_len = vp->name_length; | |
67d25278 ER |
164 | + } |
165 | + } else { | |
166 | + /* | |
167 | + * an exception value, so stop | |
168 | + */ | |
169 | + running = 0; | |
170 | + } | |
171 | + } | |
172 | + } else { | |
173 | + /* | |
174 | + * error in response, print it | |
175 | + */ | |
176 | + running = 0; | |
177 | + } | |
178 | + } else if (status == STAT_TIMEOUT) { | |
179 | + running = 0; | |
180 | + } else { /* status == STAT_ERROR */ | |
181 | + running = 0; | |
182 | } | |
183 | - | |
184 | - /* Extract the local/remote information from the index values */ | |
185 | - cp = tmpAddr.data; | |
186 | - cp[0] = vp->name[ 10 ] & 0xff; | |
187 | - cp[1] = vp->name[ 11 ] & 0xff; | |
188 | - cp[2] = vp->name[ 12 ] & 0xff; | |
189 | - cp[3] = vp->name[ 13 ] & 0xff; | |
190 | - localAddr.s_addr = tmpAddr.addr.s_addr; | |
191 | - localPort = ntohs(vp->name[ 14 ]); | |
192 | - cp = tmpAddr.data; | |
193 | - cp[0] = vp->name[ 15 ] & 0xff; | |
194 | - cp[1] = vp->name[ 16 ] & 0xff; | |
195 | - cp[2] = vp->name[ 17 ] & 0xff; | |
196 | - cp[3] = vp->name[ 18 ] & 0xff; | |
197 | - remoteAddr.s_addr = tmpAddr.addr.s_addr; | |
198 | - remotePort = ntohs(vp->name[ 19 ]); | |
199 | ||
200 | - printf("%-5.5s", name); | |
201 | - inetprint(&localAddr, localPort, name, 1); | |
202 | - inetprint(&remoteAddr, remotePort, name, 0); | |
203 | - if ( state < 1 || state > TCP_NSTATES ) | |
204 | - printf("%d\n", state ); | |
205 | - else | |
206 | - printf("%s\n", tcpstates[ state ]); | |
207 | - } | |
208 | - snmp_free_varbind( var ); | |
209 | + if (response) { | |
210 | + snmp_free_pdu(response); | |
211 | + } | |
85c5e829 ER |
212 | + } |
213 | +} | |
214 | + | |
215 | +void | |
216 | +tcpprotopr(const char *name) | |
217 | +{ | |
218 | + oid tcpConnState_oid[] = { 1,3,6,1,2,1,6,13,1,1 }; | |
219 | + size_t tcpConnState_len = OID_LENGTH( tcpConnState_oid ); | |
220 | + int use_getbulk = 1; | |
221 | + | |
222 | +#ifndef NETSNMP_DISABLE_SNMPV1 | |
223 | + if (ss->version == SNMP_VERSION_1) { | |
224 | + use_getbulk = 0; | |
225 | + } | |
226 | +#endif | |
227 | + | |
228 | + if (use_getbulk) { | |
229 | + tcpprotopr_bulkget(name, tcpConnState_oid, tcpConnState_len); | |
230 | + } else { | |
231 | + tcpprotopr_get(name, tcpConnState_oid, tcpConnState_len); | |
67d25278 ER |
232 | + } |
233 | } | |
234 | ||
235 | /* |