📄 protocol.c
字号:
DEBUGMSGTL(("agentx","padding %.2x %.2x)\n", *(bufp+2), *(bufp+3))); bufp += 4; *length -= 4; DEBUGMSGTL(("agentx","\t ")); bufp = agentx_dump_oid( bufp, length, network_byte_order ); if ( bufp == NULL ) return NULL;DEBUGMSGTL(("agentx","\t value = ")); switch ( type ) { case ASN_INTEGER: case ASN_COUNTER: case ASN_GAUGE: case ASN_TIMETICKS: agentx_dump_int( bufp, network_byte_order ); bufp += 4; *length -= 4; break; case ASN_OCTET_STR: case ASN_IPADDRESS: case ASN_OPAQUE: bufp = agentx_dump_string( bufp, length, network_byte_order ); break; case ASN_OBJECT_ID: bufp = agentx_dump_oid( bufp, length, network_byte_order ); break; case ASN_COUNTER64: if ( network_byte_order ) { DEBUGMSGTL(("agentx","\t High ")); agentx_dump_int( bufp, network_byte_order ); DEBUGMSGTL(("agentx","\t Low ")); agentx_dump_int( bufp+4, network_byte_order ); } else { DEBUGMSGTL(("agentx","\t Low ")); agentx_dump_int( bufp, network_byte_order ); DEBUGMSGTL(("agentx","\t High ")); agentx_dump_int( bufp+4, network_byte_order ); } bufp += 8; *length -= 8; break; case ASN_NULL: case SNMP_NOSUCHOBJECT: case SNMP_NOSUCHINSTANCE: case SNMP_ENDOFMIBVIEW: /* No data associated with these types */DEBUGMSGTL(("agentx","null data \n")); break; default:DEBUGMSGTL(("agentx","unrecognised \n")); return NULL; } return bufp;}u_char *agentx_dump_header(struct snmp_pdu *pdu, u_char *data, size_t *length){ register u_char *bufp = data; size_t payload; if ( *length < 20 ) { /* Incomplete header */ return NULL; }DEBUGMSGTL(("agentx","HEADER version = %d, command = %d, flags = %.2x, (reserved %d)\n", *bufp, *(bufp+1), *(bufp+2), *(bufp+3))); pdu->version = AGENTX_VERSION_BASE | *bufp; bufp++; pdu->command = *bufp; bufp++; pdu->flags |= *bufp; bufp++; bufp++;DEBUGMSGTL(("agentx","\t Session ID = ")); agentx_dump_int( bufp, pdu->flags & AGENTX_FLAGS_NETWORK_BYTE_ORDER ); bufp += 4;DEBUGMSGTL(("agentx","\t Request ID = ")); agentx_dump_int( bufp, pdu->flags & AGENTX_FLAGS_NETWORK_BYTE_ORDER ); bufp += 4;DEBUGMSGTL(("agentx","\t Message ID = ")); agentx_dump_int( bufp, pdu->flags & AGENTX_FLAGS_NETWORK_BYTE_ORDER ); bufp += 4;DEBUGMSGTL(("agentx","\t Payload Length = ")); payload = agentx_dump_int( bufp, pdu->flags & AGENTX_FLAGS_NETWORK_BYTE_ORDER ); bufp += 4; *length -= 20; if ( *length != payload ) { /* Short payload */ return NULL; } return bufp;}intagentx_dump(struct snmp_session *session, struct snmp_pdu *pdu, u_char *data, size_t len){ register u_char *bufp = data; u_char buffer[BUFSIZ]; u_char *prefix_ptr; int range_subid; int inc; /* Inclusive SearchRange flag */ int type; /* VarBind data type */ size_t *length = &len; if (!IS_AGENTX_VERSION( session->version )) return SNMPERR_BAD_VERSION; /* * Handle (common) header .... */ bufp = agentx_dump_header( pdu, bufp, length ); if ( bufp == NULL ) return SNMPERR_INCOMPLETE_PACKET; /* i.e. wait for the rest */ /* * ... and (not-un-common) context */ if ( pdu->flags & AGENTX_MSG_FLAG_NON_DEFAULT_CONTEXT ) {DEBUGMSGTL(("agentx","CONTEXT ")); bufp = agentx_dump_string( bufp, length, pdu->flags & AGENTX_FLAGS_NETWORK_BYTE_ORDER ); if ( bufp == NULL ) return SNMPERR_ASN_PARSE_ERR; } switch ( pdu->command ) { case AGENTX_MSG_OPEN:DEBUGMSGTL(("agentx","OPEN T/Out = %d, (reserved %d %d %d)\n", *bufp, *(bufp+1), *(bufp+2), *(bufp+3))); bufp += 4; *length -= 4; bufp = agentx_dump_oid( bufp, length, pdu->flags & AGENTX_FLAGS_NETWORK_BYTE_ORDER ); if ( bufp == NULL ) return SNMPERR_ASN_PARSE_ERR; bufp = agentx_dump_string( bufp, length, pdu->flags & AGENTX_FLAGS_NETWORK_BYTE_ORDER ); if ( bufp == NULL ) return SNMPERR_ASN_PARSE_ERR; break; case AGENTX_MSG_CLOSE:DEBUGMSGTL(("agentx","CLOSE reason = %d, (reserved %d %d %d)\n", *bufp, *(bufp+1), *(bufp+2), *(bufp+3))); bufp += 4; *length -= 4; break; case AGENTX_MSG_REGISTER: case AGENTX_MSG_UNREGISTER:DEBUGMSGTL(("agentx","(Un)REGISTER T/Out = %d, priority = %d, range = %d, (reserved %d)\n", *bufp, *(bufp+1), *(bufp+2), *(bufp+3))); range_subid = *(bufp+2); bufp += 4; *length -= 4; prefix_ptr = bufp+1; bufp = agentx_dump_oid( bufp, length, pdu->flags & AGENTX_FLAGS_NETWORK_BYTE_ORDER ); if ( bufp == NULL ) return SNMPERR_ASN_PARSE_ERR; if ( range_subid ) {DEBUGMSGTL(("agentx","\t range bound = ")); agentx_dump_int( bufp, pdu->flags & AGENTX_FLAGS_NETWORK_BYTE_ORDER ); bufp += 4; *length -= 4; } break; case AGENTX_MSG_GETBULK:DEBUGMSGTL(("agentx","GETBULK non-rep = ")); agentx_dump_short( bufp, pdu->flags & AGENTX_FLAGS_NETWORK_BYTE_ORDER );DEBUGMSGTL(("agentx","\t max-rep = ")); agentx_dump_short( bufp+2, pdu->flags & AGENTX_FLAGS_NETWORK_BYTE_ORDER ); bufp += 4; *length -= 4; /* Fallthrough - SearchRange handling is the same */ case AGENTX_MSG_GET: case AGENTX_MSG_GETNEXT: /* * SearchRange List * Keep going while we have data left */ while ( *length > 0 ) {DEBUGMSGTL(("agentx","Search List \n")); bufp = agentx_dump_oid( bufp, length, pdu->flags & AGENTX_FLAGS_NETWORK_BYTE_ORDER ); if ( bufp == NULL ) return SNMPERR_ASN_PARSE_ERR; bufp = agentx_dump_oid( bufp, length, pdu->flags & AGENTX_FLAGS_NETWORK_BYTE_ORDER ); if ( bufp == NULL ) return SNMPERR_ASN_PARSE_ERR; } break; case AGENTX_MSG_RESPONSE: /* sysUpTime */DEBUGMSGTL(("agentx","RESPONSE T/Out = ")); agentx_dump_int( bufp, pdu->flags & AGENTX_FLAGS_NETWORK_BYTE_ORDER ); bufp += 4; *length -= 4; DEBUGMSGTL(("agentx","\t Error status = ")); pdu->errstat = agentx_dump_short( bufp, pdu->flags & AGENTX_FLAGS_NETWORK_BYTE_ORDER );DEBUGMSGTL(("agentx","\t Error index = ")); pdu->errindex = agentx_dump_short( bufp+2, pdu->flags & AGENTX_FLAGS_NETWORK_BYTE_ORDER ); bufp += 4; *length -= 4; /* Fallthrough - VarBind handling is the same */ case AGENTX_MSG_INDEX_ALLOCATE: case AGENTX_MSG_INDEX_DEALLOCATE: case AGENTX_MSG_NOTIFY: case AGENTX_MSG_TESTSET: /* * VarBind List * Keep going while we have data left */ while ( *length > 0 ) { bufp = agentx_dump_varbind( bufp, length, pdu->flags & AGENTX_FLAGS_NETWORK_BYTE_ORDER ); if ( bufp == NULL ) return SNMPERR_ASN_PARSE_ERR; } break; case AGENTX_MSG_COMMITSET: case AGENTX_MSG_UNDOSET: case AGENTX_MSG_CLEANUPSET: case AGENTX_MSG_PING:DEBUGMSGTL(("agentx","Empty packet\n")); /* "Empty" packet */ break; case AGENTX_MSG_ADD_AGENT_CAPS:DEBUGMSGTL(("agentx","ADD AGENT CAPS \n")); bufp = agentx_dump_oid( bufp, length, pdu->flags & AGENTX_FLAGS_NETWORK_BYTE_ORDER ); if ( bufp == NULL ) return SNMPERR_ASN_PARSE_ERR; bufp = agentx_dump_string( bufp, length, pdu->flags & AGENTX_FLAGS_NETWORK_BYTE_ORDER ); if ( bufp == NULL ) return SNMPERR_ASN_PARSE_ERR; break; case AGENTX_MSG_REMOVE_AGENT_CAPS:DEBUGMSGTL(("agentx","REMOVE AGENT CAPS \n")); bufp = agentx_dump_oid( bufp, length, pdu->flags & AGENTX_FLAGS_NETWORK_BYTE_ORDER ); if ( bufp == NULL ) return SNMPERR_ASN_PARSE_ERR; break; default: DEBUGMSGTL(("agentx","Unrecognised PDU type")); return SNMPERR_UNKNOWN_PDU; } return SNMP_ERR_NOERROR;}#ifdef TESTINGtestit( struct snmp_pdu *pdu1){ char packet1[BUFSIZ]; char packet2[BUFSIZ]; int len1, len2; struct snmp_pdu pdu2; struct snmp_session sess; memset( &pdu2, 0, sizeof(struct snmp_pdu)); memset( packet1, 0, BUFSIZ ); memset( packet2, 0, BUFSIZ ); /* Encode this into a "packet" */ len1 = BUFSIZ; if ( agentx_build( &sess, pdu1, packet1, &len1 ) < 0 ) { DEBUGMSGTL(("agentx","First build failed")); exit(1); } DEBUGMSGTL(("agentx","First build succeeded:\n")); xdump( packet1, len1, "Ax1> "); /* Unpack this into a PDU */ len2 = len1; if ( agentx_parse( &pdu2, packet1, &len2, (u_char **)NULL ) < 0 ) { DEBUGMSGTL(("agentx","First parse failed\n")); exit(1); } DEBUGMSGTL(("agentx","First parse succeeded:\n")); if ( len2 != 0 ) DEBUGMSGTL(("agentx","Warning - parsed packet has %d bytes left\n", len2)); /* Encode this into another "packet" */ len2 = BUFSIZ; if ( agentx_build( &sess, &pdu2, packet2, &len2 ) < 0 ) { DEBUGMSGTL(("agentx","Second build failed\n")); exit(1); } DEBUGMSGTL(("agentx","Second build succeeded:\n")); xdump( packet2, len2, "Ax2> "); /* Compare the results */ if ( len1 != len2 ) { DEBUGMSGTL(("agentx","Error: first build (%d) is different to second (%d)\n", len1, len2)); exit(1); } if (memcmp( packet1, packet2, len1 ) != 0 ) { DEBUGMSGTL(("agentx","Error: first build data is different to second\n")); exit(1); } DEBUGMSGTL(("agentx","OK\n"));}main (){ struct snmp_pdu pdu1; oid oid_buf[] = { 1, 3, 6, 1, 2, 1, 10 }; oid oid_buf2[] = { 1, 3, 6, 1, 2, 1, 20 }; oid null_oid[] = { 0, 0 }; char *string = "Example string"; char *context = "LUCS"; /* Create an example AgentX pdu structure */ memset( &pdu1, 0, sizeof(struct snmp_pdu)); pdu1.command = AGENTX_MSG_TESTSET; pdu1.flags = 0; pdu1.sessid = 16; pdu1.transid = 24; pdu1.reqid = 132; pdu1.time = 10; pdu1.non_repeaters = 3; pdu1.max_repetitions = 32; pdu1.priority = 5; pdu1.range_subid = 0; snmp_pdu_add_variable( &pdu1, oid_buf, sizeof(oid_buf)/sizeof(oid), ASN_OBJECT_ID, (char *)oid_buf2, sizeof(oid_buf2)); snmp_pdu_add_variable( &pdu1, oid_buf, sizeof(oid_buf)/sizeof(oid), ASN_INTEGER, (char *)&pdu1.reqid, sizeof(pdu1.reqid)); snmp_pdu_add_variable( &pdu1, oid_buf, sizeof(oid_buf)/sizeof(oid), ASN_OCTET_STR, (char *)string, strlen(string)); printf("Test with non-network order.....\n"); testit( &pdu1 ); printf("\nTest with network order.....\n"); pdu1.flags |= AGENTX_FLAGS_NETWORK_BYTE_ORDER; testit( &pdu1 ); pdu1.community = context; pdu1.community_len = strlen(context); pdu1.flags |= AGENTX_FLAGS_NON_DEFAULT_CONTEXT; printf("Test with non-default context.....\n"); testit( &pdu1 ); }#endif/* returns the proper length of an incoming agentx packet. *//* * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * | h.version | h.type | h.flags | <reserved> | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * | h.sessionID | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * | h.transactionID | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * | h.packetID | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * | h.payload_length | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * 20 bytes in header */intagentx_check_packet(u_char *packet, size_t packet_len) { if (packet_len < 20) return 0; /* minimum header length == 20 */ return agentx_parse_int(packet+16, *(packet+2) & AGENTX_FLAGS_NETWORK_BYTE_ORDER) + 20;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -