Hello,
i am developing an application which should receive traps. Then i like to resend the received trap to another destination, different to the peer from where i got the trap.
My problem:
I can receive a trap e.g. on port 162. snmp_read() calls my callback function.
When i now open a second session, clone the received trap-pdu, change the peername
of the session and e.g. the remote_port and send the trap again, the trap is sent, but not
with the right ip address and port.
Instead, the source and destination ip addresses and source and destination ports are changed.
e.g. i receive a trap from 192.168.1.2: 192.168.1.2 port 3333 --> 192.168.1.3 port 162 (thats me)
then i call snmp_send() with the cloned pdu but with a peername 192.168.1.4 192.168.1.3 port 4444 (thats me) --> 192.168.1.4 port 162
But what happens is the following:
192.168.1.2 port 3333 --> 192.168.1.3 port 162 (thats me) OK 192.168.1.3 port 5555 (thats me) --> 192.168.1.2 port 3333 NOT OK
When i instead call snmp_create_pdu(TRAP) and then snmp_send() the outgoing packet contains the correct peername (destination ip).
What is wrong ?
Thanks a lot for any hint. Susanne Goldammer
Below i have listed my little program.
#include <net-snmp/net-snmp-config.h> #include <net-snmp/net-snmp-includes.h>
#include <errno.h>
void
usage(void)
{
fprintf(stderr, "USAGE: snmptest ");
snmp_parse_args_usage(stderr);
fprintf(stderr, "\n\n");
snmp_parse_args_descriptions(stderr);
}/* is called when i receive a trap on port 162 */
int my_asynch_response(int operation, struct snmp_session *sp, int reqid,
struct snmp_pdu *pdu, void *magic)
{
netsnmp_session session, *ss; netsnmp_pdu *response;
int status = 0;
printf("\nGot the message!! Trying to sent it out again\n");
fflush(stdout);
snmp_sess_init(&session);
//change the peername (destination ip)
session.peername = "192.168.1.4";
session.version = SNMP_VERSION_1;
session.remote_port = 162;
//open a new session
ss = snmp_open(&session);
if (ss == NULL)
{ /*
* diagnose snmp_open errors with the input netsnmp_session pointer
*/
snmp_sess_perror("snmp_open", &session);
SOCK_CLEANUP;
exit(1);
}
//clone the pdu and send it
status = snmp_send(ss, snmp_clone_pdu(pdu));
if (!status)
{
snmp_free_pdu(response);
snmp_sess_perror("send_packet", ss);
}
printf("snmp_send status: %d\n", status);
return 0;
}
/* main works fine */
int main(int argc, char *argv[])
{netsnmp_session session, *ss = &session, *rc = NULL;
netsnmp_pdu *pdu = NULL, *response;
netsnmp_transport *transport = NULL;
int status;
int command;
/*
* get the common command line arguments
*/
switch (snmp_parse_args(argc, argv, &session, NULL, NULL))
{
case -2: exit(0);
case -1: usage();
exit(1);
default: break;
} SOCK_STARTUP;
transport = netsnmp_tdomain_transport("udp:162", 1, "udp");
if (transport == NULL) {
snmp_log(LOG_ERR, "couldn't open %s -- errno %d (\"%s\")\n",
"udp:162", errno, strerror(errno));
}
snmp_sess_init(ss);
ss->peername = SNMP_DEFAULT_PEERNAME; //SNMP_DEFAULT_PEERNAME; /* Original code had NULL here */
ss->local_port = 162;
ss->version = SNMP_VERSION_1;//SNMP_DEFAULT_VERSION;
ss->community_len = SNMP_DEFAULT_COMMUNITY_LEN;
ss->retries = SNMP_DEFAULT_RETRIES;
ss->timeout = SNMP_DEFAULT_TIMEOUT;
ss->callback = my_asynch_response;
ss->callback_magic = (void *) transport;
ss = snmp_add(ss, transport, NULL, NULL);
if (ss == NULL)
{
snmp_sess_perror("snmptrapd", ss);
}
printf("\nPort: %d\n", ss->local_port);
fflush(stdout);
int fds = 0, block = 1;
fd_set fdset;
struct timeval timeout;
FD_ZERO(&fdset);
snmp_select_info(&fds, &fdset, &timeout, &block);
fds = select(fds, &fdset, 0, 0, block ? NULL : &timeout);
if (fds < 0)
{
perror("select failed");
exit(1);
}
if (fds)
{
snmp_read(&fdset);
}else
{
printf("\nTimeout\n");
snmp_timeout();
}
}
------------------------------------------------------- SF email is sponsored by - The IT Product Guide Read honest & candid reviews on hundreds of IT Products from real users. Discover which products truly live up to the hype. Start reading now. http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click _______________________________________________ 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
