现在的位置: 首页 > 综合 > 正文

addding new fd in select() for recieving request

2013年09月09日 ⁄ 综合 ⁄ 共 4066字 ⁄ 字号 评论关闭

Hello Dev,

This means that in main() of my subagent

main()
{
        init_agent("snmpV3Agent");
        init_currentAlarmTable();
        init_snmp("snmpV3Agent");
         ... .
         ........
        //start listning for client
        listener = socket(AF_INET, SOCK_STREAM, 0);
        bind(listener, (struct sockaddr *)&myaddr, sizeof(myaddr));
        listen(listener, 10)

        while()
        {
            snmp_select_info(&numfds, &read_fds, NULL, 0);
            select(numfds,&read_fds,0,0,NULL)
             if (FD_ISSET(listener , &read_fds))
             {
                 //accept when ready to be read
                 newfd = accept(listener, (struct sockaddr *)&remoteaddr,&addrlen)

                  //myfunction1 --->reads the data add it to queue and forward to manager as a TRAP
                 register_readfd(&numfds,(*myfunction1),NULL);
             }
         }

}

or it is just call snmp_select_info() with readfds containing my fd and just call register_readfd() not
even check for FD_ISSET(). This will automatically accept the connection when the fd is ready to be read
and in the function pointer(call back method) i can write the code to extract the data.

Is it. Please do reply if i am wrong anywhere.

--Thanks a lot

----- Original Message -----
From: Dave Shield <D.T.Shield <at> liverpool.ac.uk>
To: gauravmeh2004 <at> indiatimes.com
Cc: net-snmp-users <at> lists.sourceforge.net
Sent: Mon, 9 Jun 2008 13:40:40 +0530 (IST)
Subject: Re: addding new fd in select() for recieving request

2008/6/9  <gauravmeh2004 <at> indiatimes.com>:
> I have a requirement that i also need to listen for alarms coming
> from another application on socket and forward them to manager
> apart from GET/SET request from Manager

Have a look at the 'register_readfd()' routine
which is designed for exactly this purpose.

> I am thinking of doing it in following manner.
> Please comment on the following if it is wrong

You do not need to change the main() code.
The Net-SNMP toolkit already supports the
functionality you require.

Dave

-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
Net-snmp-users mailing list
Net-snmp-users <at> lists.sourceforge.net
Please see the following page to unsubscribe or change other options:
https://lists.sourceforge.net/lists/listinfo/net-snmp-users

 

2008/6/9  <gauravmeh2004 <at> indiatimes.com>:
> main()
> {
>        //start listning for client
>        listener = socket(AF_INET, SOCK_STREAM, 0);
>        bind(listener, (struct sockaddr *)&myaddr, sizeof(myaddr));
>        listen(listener, 10)

This is also where you would register this external socket:

          register_readfd(  listener,   listener_callback );

>        while()
>        {

Remember to clear the readfd array:

             numfds = 0;
             FD_ZERO(&read_fds);

and you're not passing in the timeout handling structures.

             block = 0;
>            snmp_select_info(&numfds, &read_fds,
                 tvp, &block);

You also need to do the same for the external sockets:

              netsnmp_external_event_info(numfds,&read_fds,NULL,NULL)

You probably do need to pass the 'tvp' parameter to select, as well as taking
note of the return value:

>             count = select(numfds,&read_fds,0,0, tvp)

The next bit isn't needed.  Replace it with:

              netsnmp_dispatch_external_events( &count, &read_fds, NULL, NULL);

That handles checking whether your socket has anything waiting,
and calls the registered callback.

This should then be followed by

              if ( count > 0 )
                  snmp_read( &read_fds );

to handle "normal" SNMP traffic.

>         }

See the main driving loop ("receive()") of snmpd.c for the model
of this code.

Dave

PS: Please do *NOT* repost your query, just because you
  didn't get a response immediately.   Some of us have real
  work to do, and can't spend all our time providing free
  advice to impatient programmers!

-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
Net-snmp-users mailing list
Net-snmp-users <at> lists.sourceforge.net
Please see the following page to unsubscribe or change other options:
https://lists.sourceforge.net/lists/listinfo/net-snmp-users

抱歉!评论已关闭.