📄 tools.c
字号:
} /* end hex_to_binary2() *//*******************************************************************-o-****** * dump_chunk * * Parameters: * *title (May be NULL.) * *buf * size */voiddump_chunk(const char *debugtoken, const char *title, const u_char *buf, int size){ u_int printunit = 64; /* XXX Make global. */ char chunk[SNMP_MAXBUF], *s, *sp; if ( title && (*title != '\0') ) { DEBUGMSGTL((debugtoken, "%s\n", title)); } memset(chunk, 0, SNMP_MAXBUF); size = binary_to_hex(buf, size, &s); sp = s; while (size > 0) { if (size > (int)printunit) { strncpy(chunk, sp, printunit); chunk[printunit] = '\0'; DEBUGMSGTL((debugtoken, "\t%s\n", chunk)); } else { DEBUGMSGTL((debugtoken, "\t%s\n", sp)); } sp += printunit; size -= printunit; } SNMP_FREE(s);} /* end dump_chunk() *//*******************************************************************-o-****** * dump_snmpEngineID * * Parameters: * *estring * *estring_len * * Returns: * Allocated memory pointing to a string of buflen char representing * a printf'able form of the snmpEngineID. * * -OR- NULL on error. * * * Translates the snmpEngineID TC into a printable string. From RFC 2271, * Section 5 (pp. 36-37): * * First bit: 0 Bit string structured by means non-SNMPv3. * 1 Structure described by SNMPv3 SnmpEngineID TC. * * Bytes 1-4: Enterprise ID. (High bit of first byte is ignored.) * * Byte 5: 0 (RESERVED by IANA.) * 1 IPv4 address. ( 4 octets) * 2 IPv6 address. ( 16 octets) * 3 MAC address. ( 6 octets) * 4 Locally defined text. (0-27 octets) * 5 Locally defined octets. (0-27 octets) * 6-127 (RESERVED for enterprise.) * * Bytes 6-32: (Determined by byte 5.) * * * Non-printable characters are given in hex. Text is given in quotes. * IP and MAC addresses are given in standard (UN*X) conventions. Sections * are comma separated. * * esp, remaining_len and s trace the state of the constructed buffer. * s will be defined if there is something to return, and it will point * to the end of the constructed buffer. * * * ASSUME "Text" means printable characters. * * XXX Must the snmpEngineID always have a minimum length of 12? * (Cf. part 2 of the TC definition.) * XXX Does not enforce upper-bound of 32 bytes. * XXX Need a switch to decide whether to use DNS name instead of a simple * IP address. * * FIX Use something other than sprint_hexstring which doesn't add * trailing spaces and (sometimes embedded) newlines... */#ifdef CYGPKG_SNMPAGENT_V3_SUPPORT#ifdef SNMP_TESTING_CODEchar *dump_snmpEngineID(const u_char *estring, size_t *estring_len){#define eb(b) ( *(esp+b) & 0xff ) int rval = SNMPERR_SUCCESS, gotviolation = 0, slen = 0; u_int remaining_len; char buf[SNMP_MAXBUF], *s = NULL, *t; const u_char *esp = estring; struct in_addr iaddr; /* * Sanity check. */ if ( !estring || (*estring_len <= 0) ) { QUITFUN(SNMPERR_GENERR, dump_snmpEngineID_quit); } remaining_len = *estring_len; memset(buf, 0, SNMP_MAXBUF); /* * Test first bit. Return immediately with a hex string, or * begin by formatting the enterprise ID. */ if ( !(*esp & 0x80) ) { sprint_hexstring(buf, esp, remaining_len); s = strchr(buf, '\0'); s -= 1; goto dump_snmpEngineID_quit; } s = buf; s += sprintf(s, "enterprise %d, ", ((*(esp+0)&0x7f) << 24) | ((*(esp+1)&0xff) << 16) | ((*(esp+2)&0xff) << 8) | ((*(esp+3)&0xff)) ); /* XXX Ick. */ if (remaining_len < 5) { /* XXX Violating string. */ goto dump_snmpEngineID_quit; } esp += 4; /* Incremented one more in the switch below. */ remaining_len -= 5; /* * Act on the fifth byte. */ switch ((int) *esp++) { case 1: /* IPv4 address. */ if (remaining_len < 4) goto dump_snmpEngineID_violation; memcpy(&iaddr.s_addr, esp, 4); if ( !(t = inet_ntoa(iaddr)) ) goto dump_snmpEngineID_violation; s += sprintf(s, "%s", t); esp += 4; remaining_len -= 4; break; case 2: /* IPv6 address. */ if (remaining_len < 16) goto dump_snmpEngineID_violation; s += sprintf( s, "%02X%02X %02X%02X %02X%02X %02X%02X::" "%02X%02X %02X%02X %02X%02X %02X%02X", eb(0), eb(1), eb(2), eb(3), eb(4), eb(5), eb(6), eb(7), eb(8), eb(9), eb(10), eb(11), eb(12), eb(13), eb(14), eb(15) ); esp += 16; remaining_len -= 16; break; case 3: /* MAC address. */ if (remaining_len < 6) goto dump_snmpEngineID_violation; s += sprintf( s, "%02X:%02X:%02X:%02X:%02X:%02X", eb(0), eb(1), eb(2), eb(3), eb(4), eb(5) ); esp += 6; remaining_len -= 6; break; case 4: /* Text. */ /* Doesn't exist on all (many) architectures */ /* s += snprintf(s, remaining_len+3, "\"%s\"", esp); */ s += sprintf(s, "\"%s\"", esp); goto dump_snmpEngineID_quit; break; /*NOTREACHED*/ case 5: /* Octets. */ sprint_hexstring(s, esp, remaining_len); s = strchr(buf, '\0'); s -= 1; goto dump_snmpEngineID_quit; break; /*NOTREACHED*/dump_snmpEngineID_violation: case 0: /* Violation of RESERVED, * -OR- of expected length. */ gotviolation = 1; s += sprintf(s, "!!! "); default: /* Unknown encoding. */ if ( !gotviolation ) { s += sprintf(s, "??? "); } sprint_hexstring(s, esp, remaining_len); s = strchr(buf, '\0'); s -= 1; goto dump_snmpEngineID_quit; } /* endswitch */ /* * Cases 1-3 (IP and MAC addresses) should not have trailing * octets, but perhaps they do. Throw them in too. XXX */ if (remaining_len > 0) { s += sprintf(s, " (??? "); sprint_hexstring(s, esp, remaining_len); s = strchr(buf, '\0'); s -= 1; s += sprintf(s, ")"); }dump_snmpEngineID_quit: if (s) { slen = s-buf+1; s = calloc(1,slen); memcpy(s, buf, (slen)-1); } memset(buf, 0, SNMP_MAXBUF); /* XXX -- Overkill? XXX: Yes! */ return s;#undef eb} /* end dump_snmpEngineID() */#endif /* SNMP_TESTING_CODE */#endif /* CYGPKG_SNMPAGENT_V3_SUPPORT *//* * create a new time marker. * NOTE: Caller must free time marker when no longer needed. */marker_t atime_newMarker(void){ marker_t pm = (marker_t)calloc(1,sizeof(struct timeval)); gettimeofday((struct timeval *)pm, 0); return pm;}/* * set a time marker. */void atime_setMarker(marker_t pm){ if (! pm) return; gettimeofday((struct timeval *)pm, 0);}/* * Test: Has (marked time plus delta) exceeded current time ? * Returns 0 if test fails or cannot be tested (no marker). */int atime_ready( marker_t pm, int deltaT){ struct timeval txdelta, txnow; if (! pm) return 0; memcpy((void *)&txdelta, pm, sizeof(txdelta)); while (deltaT > 1000) { txdelta.tv_sec ++; deltaT -= 1000; } txdelta.tv_usec = (deltaT * 1000) + txdelta.tv_usec; gettimeofday(&txnow, 0); if (timercmp(&txnow, &txdelta, <)) return 0; return 1;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -