1 --- net-snmp-5.4.2.1/apps/snmpnetstat/inet.c 2006-04-21 15:30:47.000000000 +0300
2 +++ /home/glen/inet.c 2009-01-22 17:39:57.754383000 +0200
4 tcpprotopr(const char *name)
6 netsnmp_variable_list *var, *vp;
7 - oid tcpConnState_oid[] = { 1,3,6,1,2,1,6,13,1,1 };
8 - size_t tcpConnState_len = OID_LENGTH( tcpConnState_oid );
9 + netsnmp_pdu *pdu, *response;
10 + oid tcpConnState_mib[] = { 1,3,6,1,2,1,6,13,1,1 };
11 + oid tcpConnState_oid[MAX_OID_LEN];
12 + size_t tcpConnState_len;
16 @@ -121,61 +123,126 @@
17 struct in_addr localAddr, remoteAddr;
25 + * setup initial object name
27 + memmove(tcpConnState_oid, tcpConnState_mib, sizeof(tcpConnState_mib));
28 + tcpConnState_len = sizeof(tcpConnState_mib) / sizeof(oid);
31 * Walking the tcpConnState column will provide all
32 * the necessary information.
35 - snmp_varlist_add_variable( &var, tcpConnState_oid, tcpConnState_len,
39 - if (netsnmp_query_walk( var, ss ) != SNMP_ERR_NOERROR)
42 - for (vp = var; vp ; vp=vp->next_variable) {
43 - state = *vp->val.integer;
44 - if (!aflag && state == MIB_TCPCONNSTATE_LISTEN)
48 + * create PDU for GETBULK request and add object name to request
50 + pdu = snmp_pdu_create(SNMP_MSG_GETBULK);
51 + pdu->non_repeaters = 0;
52 + pdu->max_repetitions = 1024; /* fill the packet */
53 + snmp_add_null_var(pdu, tcpConnState_oid, tcpConnState_len);
56 - printf("Active Internet (%s) Connections", name);
58 - printf(" (including servers)");
60 - width = Aflag ? 18 : 22;
61 - printf("%-5.5s %*.*s %*.*s %s\n",
62 - "Proto", -width, width, "Local Address",
63 - -width, width, "Remote Address", "(state)");
68 + status = snmp_synch_response(ss, pdu, &response);
69 + if (status == STAT_SUCCESS) {
70 + if (response->errstat == SNMP_ERR_NOERROR) {
71 + for (vp = response->variables; vp ; vp=vp->next_variable) {
73 + if ((vp->name_length < sizeof(tcpConnState_mib) / sizeof(oid)) ||
74 + (memcmp(tcpConnState_mib, vp->name, sizeof(tcpConnState_mib)) != 0)) {
76 + * not part of this subtree
82 + state = *vp->val.integer;
83 + if (!aflag && state == MIB_TCPCONNSTATE_LISTEN) {
88 + printf("Active Internet (%s) Connections", name);
90 + printf(" (including servers)");
92 + width = Aflag ? 18 : 22;
93 + printf("%-5.5s %*.*s %*.*s %s\n",
94 + "Proto", -width, width, "Local Address",
95 + -width, width, "Remote Address", "(state)");
99 + /* Extract the local/remote information from the index values */
101 + cp[0] = vp->name[ 10 ] & 0xff;
102 + cp[1] = vp->name[ 11 ] & 0xff;
103 + cp[2] = vp->name[ 12 ] & 0xff;
104 + cp[3] = vp->name[ 13 ] & 0xff;
105 + localAddr.s_addr = tmpAddr.addr.s_addr;
106 + localPort = ntohs(vp->name[ 14 ]);
108 + cp[0] = vp->name[ 15 ] & 0xff;
109 + cp[1] = vp->name[ 16 ] & 0xff;
110 + cp[2] = vp->name[ 17 ] & 0xff;
111 + cp[3] = vp->name[ 18 ] & 0xff;
112 + remoteAddr.s_addr = tmpAddr.addr.s_addr;
113 + remotePort = ntohs(vp->name[ 19 ]);
115 + printf("%-5.5s", name);
116 + inetprint(&localAddr, localPort, name, 1);
117 + inetprint(&remoteAddr, remotePort, name, 0);
118 + if (state < 1 || state > TCP_NSTATES) {
119 + printf("%d\n", state );
121 + printf("%s\n", tcpstates[state]);
124 + if ((vp->type != SNMP_ENDOFMIBVIEW) &&
125 + (vp->type != SNMP_NOSUCHOBJECT) &&
126 + (vp->type != SNMP_NOSUCHINSTANCE)) {
128 + * Check if last variable, and if so, save for next request.
130 + if (vp->next_variable == NULL) {
131 +// printf("old: "); fprint_objid(stdout, tcpConnState_oid, tcpConnState_len);
133 + memmove(tcpConnState_oid, vp->name,
134 + vp->name_length * sizeof(oid));
135 + tcpConnState_len = vp->name_length;
137 +// printf("new: "); fprint_objid(stdout, tcpConnState_oid, tcpConnState_len);
141 + * an exception value, so stop
148 + * error in response, print it
152 + } else if (status == STAT_TIMEOUT) {
154 + } else { /* status == STAT_ERROR */
158 - /* Extract the local/remote information from the index values */
160 - cp[0] = vp->name[ 10 ] & 0xff;
161 - cp[1] = vp->name[ 11 ] & 0xff;
162 - cp[2] = vp->name[ 12 ] & 0xff;
163 - cp[3] = vp->name[ 13 ] & 0xff;
164 - localAddr.s_addr = tmpAddr.addr.s_addr;
165 - localPort = ntohs(vp->name[ 14 ]);
167 - cp[0] = vp->name[ 15 ] & 0xff;
168 - cp[1] = vp->name[ 16 ] & 0xff;
169 - cp[2] = vp->name[ 17 ] & 0xff;
170 - cp[3] = vp->name[ 18 ] & 0xff;
171 - remoteAddr.s_addr = tmpAddr.addr.s_addr;
172 - remotePort = ntohs(vp->name[ 19 ]);
174 - printf("%-5.5s", name);
175 - inetprint(&localAddr, localPort, name, 1);
176 - inetprint(&remoteAddr, remotePort, name, 0);
177 - if ( state < 1 || state > TCP_NSTATES )
178 - printf("%d\n", state );
180 - printf("%s\n", tcpstates[ state ]);
182 - snmp_free_varbind( var );
184 + snmp_free_pdu(response);