Can't send SNMPv3 Traps from C Program

[email protected]
Newsgroups gmane.network.net-snmp.user
Message-ID <10556.212.162.211.211.1186478034.QFIUQm5cTkF4Qh0R.squirrel@212.162.211.211>
Please help, I am trying to send a SNMPv3 trap from within a C program.

I am using the add_trap_session function to add my trap sink sessions and
then send_v2trap to send the trap.

The program works with the authentication turned off (securityLevel = 
SNMP_SEC_LEVEL_NOAUTH) but I can not get it to work when I set the
securityLevel to use encryption (i.e. SNMP_SEC_LEVEL_AUTH).
I can successfully send traps using the snmptrap tool so I know the
trapsink is working.

A simplified version of my code is shown below (and is also contained in
the attached file)...

Thanks Darryl

int main() {
	init();
	send_trap();

}

void init() {
	For each trap sink {
		create_v3_trap_session(...);
	}
}

void send_trap() {
	netsnmp_variable_list *notification_vars;

	Setup notification_vars...

	send_v2trap(notification_vars);
}


int create_v3_trap_session(...) {
    netsnmp_session session, *sesp;
    memset(&session, 0, sizeof(netsnmp_session));
    snmp_sess_init( &session );  // Set up defaults


    session.version == SNMP_VERSION_3;
    session.peername = strdup("192.168.144.214:162");
    session.callback       = snmp_inform_callback; // Function to
interpret incoming data
    session.callback_magic = NULL;                 // Pointer to data for
callback

    // SNMP v2
    session.community = NULL;
    session.community_len = 0;

    // SNMP v3
    session.securityName = strdup("usera");         // The user name
    session.securityNameLen = strlen("usera");      // Length of securityName

    session.securityAuthKeyLen = USM_AUTH_KU_LEN;   //Length of Ku for
auth protocol
    session.securityAuthProto =
snmp_duplicate_objid(usmHMACMD5AuthProtocol, USM_AUTH_PROTO_MD5_LEN);
    session.securityAuthProtoLen = USM_AUTH_PROTO_MD5_LEN;

    if (generate_Ku(session.securityAuthProto,
     	              session.securityAuthProtoLen,
       	            (u_char *) "mypassword", "mypassword",
         	          session.securityAuthKey,
           	        &session.securityAuthKeyLen) != SNMPERR_SUCCESS) {
    	return (-1);
    }

    session.securityPrivKeyLen = USM_PRIV_KU_LEN; //  Length of Ku for
priv protocol
    session.securityPrivProto  = snmp_duplicate_objid(usmDESPrivProtocol,
USM_PRIV_PROTO_DES_LEN);
    session.securityPrivProtoLen = USM_PRIV_PROTO_DES_LEN;

    if (generate_Ku(session.securityAuthProto,
                    session.securityAuthProtoLen,
                    (u_char *) "mypassword", strlen("mypassword"),
                    session.securityPrivKey,
                    &session.securityPrivKeyLen) != SNMPERR_SUCCESS) {
    	return (-1);
    }

    session.securityModel = SNMP_DEFAULT_SECMODEL; // snmp security model,
v1, v2c, usm */
    session.securityLevel = SNMP_SEC_LEVEL_AUTHNOPRIV;

    // The following session variables were not set
    //session.securityAuthLocalKey    =  //Kul for auth protocol
    //session.securityAuthLocalKeyLen =  // Length of Kul for auth
protocol XXX
    //session.remote_port             =  // UDP port number of peer. (NO
LONGER USED - USE peername INSTEAD)
    //session.local_port              =  // My UDP port number, 0 for
default, picked randomly
    //session.isAuthoritative         =  // Are we the authoritative
engine? */
    //session.contextName             =  // authoritative contextName
    //session.contextNameLen          =  // Length of contextName
    //session.securityEngineID        =  // Authoritative snmpEngineID
(See snmp_hex_to_binary)
    //session.securityEngineIDLen     =  // Length of contextEngineID
    //session.paramName               =  // target param name
    //session.securityInfo            =  // security module specific
    //session.localname               =
    //session.securityEngineIDLen     =
    //session.securityEngineID        =
    //session.engineBoots             =  // Initial engineBoots for remote
engine
    //session.engineTime              =  // Initial engineTime for remote
engine



    sesp = snmp_open(&session);
    if (sesp) {
    	add_trap_session(sesp, SNMP_MSG_INFORM, TRUE, SNMP_VERSION_3);
    }
}

-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >>  http://get.splunk.com/

_______________________________________________
Net-snmp-users mailing list
[email protected]
Please see the following page to unsubscribe or change other options:
https://lists.sourceforge.net/lists/listinfo/net-snmp-users
test_code.c (text/plain, 3.4 KB)
int main() {
	init();
	send_trap();

}

void init() {
	For each trap sink {
		create_v3_trap_session(...);
	}
}

void send_trap() {
	netsnmp_variable_list *notification_vars;

	Setup notification_vars...

	send_v2trap(notification_vars);
}


int create_v3_trap_session(...) {
    netsnmp_session session, *sesp;
    memset(&session, 0, sizeof(netsnmp_session));
    snmp_sess_init( &session );  // Set up defaults


    session.version == SNMP_VERSION_3;
    session.peername = strdup("192.168.144.214:162");
    session.callback       = snmp_inform_callback; // Function to interpret incoming data
    session.callback_magic = NULL;                 // Pointer to data for callback

    // SNMP v2
    session.community = NULL;
    session.community_len = 0;

    // SNMP v3
    session.securityName = strdup("usera");         // The user name
    session.securityNameLen = strlen("usera");      // Length of securityName

    session.securityAuthKeyLen = USM_AUTH_KU_LEN;   //Length of Ku for auth protocol
    session.securityAuthProto = snmp_duplicate_objid(usmHMACMD5AuthProtocol, USM_AUTH_PROTO_MD5_LEN);
    session.securityAuthProtoLen = USM_AUTH_PROTO_MD5_LEN;

    if (generate_Ku(session.securityAuthProto,
     	              session.securityAuthProtoLen,
       	            (u_char *) "mypassword", "mypassword",
         	          session.securityAuthKey,
           	        &session.securityAuthKeyLen) != SNMPERR_SUCCESS) {
    	return (-1);
    }

    session.securityPrivKeyLen = USM_PRIV_KU_LEN; //  Length of Ku for priv protocol
    session.securityPrivProto  = snmp_duplicate_objid(usmDESPrivProtocol, USM_PRIV_PROTO_DES_LEN);
    session.securityPrivProtoLen = USM_PRIV_PROTO_DES_LEN;

    if (generate_Ku(session.securityAuthProto,
                    session.securityAuthProtoLen,
                    (u_char *) "mypassword", strlen("mypassword"),
                    session.securityPrivKey,
                    &session.securityPrivKeyLen) != SNMPERR_SUCCESS) {
    	return (-1);
    }

    session.securityModel = SNMP_DEFAULT_SECMODEL; // snmp security model, v1, v2c, usm */
    session.securityLevel = SNMP_SEC_LEVEL_AUTHNOPRIV;

    // The following session variables were not set
    //session.securityAuthLocalKey    =  //Kul for auth protocol
    //session.securityAuthLocalKeyLen =  // Length of Kul for auth protocol XXX
    //session.remote_port             =  // UDP port number of peer. (NO LONGER USED - USE peername INSTEAD)
    //session.local_port              =  // My UDP port number, 0 for default, picked randomly
    //session.isAuthoritative         =  // Are we the authoritative engine? */
    //session.contextName             =  // authoritative contextName
    //session.contextNameLen          =  // Length of contextName
    //session.securityEngineID        =  // Authoritative snmpEngineID (See snmp_hex_to_binary)
    //session.securityEngineIDLen     =  // Length of contextEngineID
    //session.paramName               =  // target param name
    //session.securityInfo            =  // security module specific
    //session.localname               =
    //session.securityEngineIDLen     =
    //session.securityEngineID        =
    //session.engineBoots             =  // Initial engineBoots for remote engine
    //session.engineTime              =  // Initial engineTime for remote engine



    sesp = snmp_open(&session);
    if (sesp) {
    	add_trap_session(sesp, SNMP_MSG_INFORM, TRUE, SNMP_VERSION_3);
    }
}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.