Success.  
The described mechanism below works perfectly (external_readfd). Attached is
the stub code to implement the ability to request the snmpd daemon to send a
trap using mib2c compiled in hooks.  Sweet.

Code is not robust yet, but it works.

The client side
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <unistd.h>


#define MY_TRAP_LISTENER_PORT 32567

int main(int argc, char *argv[])
{
        int    my_trap_socket;
        struct sockaddr_in sa;
        int    bytes_sent;
        int    msg_len;
        char   msg[200];
        int        flags = 0;

        int trap_num;

        if (argc != 2)
        {
                printf("First param must be the traps number\n");
                return 1;
        }

        trap_num = atoi(argv[1]);

        printf("Trap: Sending %d to port %d\n", trap_num,
MY_TRAP_LISTENER_PORT);

        msg[0]  = trap_num;
        msg_len = 1;

        my_trap_socket = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
        if (-1 == my_trap_socket)//if socket failed to initialize, exit
        {
                printf("Error Creating Socket");
                return 0;
        }

        sa.sin_family      = AF_INET;
        sa.sin_addr.s_addr = htonl(0x7F000001);
        sa.sin_port        = htons(MY_TRAP_LISTENER_PORT);

        bytes_sent = sendto(my_trap_socket, msg, msg_len, flags,(struct
sockaddr*) &sa, sizeof(struct sockaddr_in) );
        if (bytes_sent < 0)
                printf("Error sending packet: %s\n", strerror(errno) );

        close(my_trap_socket);//close the socket
        return 0;
} 


Code in snmpd main()
        setup_trap_listener();  



//This is called when a message comes in
//read from socket - already open and msg is waiting, data is NULL
void func_handler(int my_trap_socket, void *data)
{
#define BUFFER_SIZE 100

        int rc;
        char msg[BUFFER_SIZE];
        struct sockaddr from;
        u_int from_size = sizeof(from);
        int flags = 0;

        //define msg structure to use

        rc = recvfrom(my_trap_socket, msg, BUFFER_SIZE, flags, &from,
&from_size);

        snmp_log(LOG_DEBUG, "Trap Listener got %d, rc = %d\n", (int)msg[0],
rc);

        switch (msg[0])
        {
                case 1:
                        send_hAudNotificationLogFull_trap();
                        break;

                default:
                        break;

        }

}

#define MY_TRAP_LISTENER_PORT 32567
static void setup_trap_listener(void)
{
        int rc;
        int my_trap_socket;             
        struct sockaddr_in sock;

        snmp_log(LOG_DEBUG, "setup_trap_listener(port %d)\n",
MY_TRAP_LISTENER_PORT);

        my_trap_socket = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
        netsnmp_assert(my_trap_socket != -1)

        sock.sin_port        = htons(MY_TRAP_LISTENER_PORT); 
        sock.sin_addr.s_addr = INADDR_ANY;

        rc = bind(my_trap_socket, (struct sockaddr *)&sock, sizeof(sock));
        netsnmp_assert(rc != -1);

        //this is the only magic
        rc = register_readfd(my_trap_socket, func_handler, "");

        //socket will be polled in receive() via external_readfd[]
        netsnmp_assert(rc == FD_REGISTERED_OK);
}





-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Adam
Bell
Sent: Wednesday, March 28, 2007 11:25 AM
To: [email protected]
Subject: FW: Writing/building my trap send code

All,

I have been digging into the agent code for socket listening and a mechanism
to request traps from outside the agent (not using agentx).  I found some
public variables that FD operations touch in the receive() function.

They are
        external_readfd
        external_writefd
        external_exceptfd

It appears that one can setup any number of sockets to be listened on within
this main loop (up to 32) by
        1. creating the socket and binding it
        2. call register_readfd(socket, func, NULL);
        3. Implement func to do something. (like send a trap)

Now the questions
        1. There are no references to register_readfd() anywhere in the code
- so is this the intended usage?
        2. I assume the socket is my own, and there need not be any SNMP
PDU.  I plan on sending messages into      snmpd to request a trap send.

I have version 5.2.3

Any advise is appreciated,
Adam





-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Adam Bell
Sent: Thursday, March 08, 2007 10:48 AM
To: [email protected]
Subject: Writing/building my trap send code

All,

I have read through the trap send related FAQs but I have a question on
programmatic trap sending.

I see that the 'trapsend' program takes command line arguments and sends a
trap.  I have an Event system and I plan on sending traps from that rather
than the command line.

I used mib2c to generate the PDU build code for my traps.  

The question is, am I on my own to code/compile my the generated code with
the trapssend code and compile it into my app?  

I ask because the snmpd agent can take my MIB code and compile it into the
agent quite easily.  I was wondering if there is something similar for
traps.

Thanks,
Adam

The information contained in this electronic mail transmission may be
privileged and confidential, and therefore, protected from disclosure. If
you have received this communication in error, please notify us immediately
by replying to this message and deleting it from your computer without
copying or disclosing it.

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT Join SourceForge.net's
Techsay panel and you'll get the chance to share your opinions on IT &
business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
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

The information contained in this electronic mail transmission may be
privileged and confidential, and therefore, protected from disclosure. If
you have received this communication in error, please notify us immediately
by replying to this message and deleting it from your computer without
copying or disclosing it.

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT Join SourceForge.net's
Techsay panel and you'll get the chance to share your opinions on IT &
business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Net-snmp-coders mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/net-snmp-coders

The information contained in this electronic mail transmission may be 
privileged and confidential, and therefore, protected from disclosure. If you 
have received this communication in error, please notify us immediately by 
replying to this message and deleting it from your computer without copying or 
disclosing it.

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
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

Reply via email to