pike.git / lib / modules / Protocols.pmod / SNMP.pmod / protocol.pike

version» Context lines:

pike.git/lib/modules/Protocols.pmod/SNMP.pmod/protocol.pike:6:   //:   //: RFC:   //: implemented (yet):   //: 1155-7 : v1   //: 1901-4 : v2/community (Bulk ops aren't implemented!)   //: planned:   // 2742 : agentX   //: 2570 : v3 description   //:    - // $Id: protocol.pike,v 1.4 2002/03/20 16:39:59 nilsson Exp $ + // $Id: protocol.pike,v 1.5 2002/11/30 21:25:12 bill Exp $         #include "snmp_globals.h"   #include "snmp_errors.h"      #if 1   // --- ASN.1 hack   class asn1_application_octet_string   {    inherit Standards.ASN1.Types.asn1_octet_string;
pike.git/lib/modules/Protocols.pmod/SNMP.pmod/protocol.pike:158:   inherit Stdio.UDP : snmp;      //:   //: public vars   //:         //:   //: private variables   //: - int port; // = SNMP_DEFAULT_PORT; + int remote_port; // = SNMP_DEFAULT_PORT;   string local_host = SNMP_DEFAULT_HOST;   string remote_host;   int request_id = 1;   int next_id = 1;   int snmp_version = SNMP_DEFAULT_VERSION;   string snmp_community = SNMP_DEFAULT_COMMUNITY;   int snmp_errno = SNMP_SUCCESS;   int ok;      //:   //: msg pool   //:   mapping msgpool = ([]);      //:   //: callback support   //:   function con_ok, con_fail;   array extra_args;    - //: - //: create - //: - //: *_port: local and remote UDP port - //: *_addr: local and remote address - //: + //! create a new SNMP protocol object + //! + //! @param rem_port + //! @param rem_addr + //! remote address and UDP port (optional) + //! @param loc_port + //! @param loc_addr + //! local address and UDB port (optional) + //!   void create(int|void rem_port, string|void rem_addr, int|void loc_port, string|void loc_addr) {       int lport = loc_port;       local_host = (!loc_addr || !sizeof(loc_addr)) ? SNMP_DEFAULT_LOCHOST : loc_addr;    if(stringp(rem_addr) && sizeof(rem_addr)) remote_host = rem_addr; -  +  if(intp(rem_port) && sizeof(rem_port)) remote_port = rem_port;       if (!snmp::bind(lport, local_host)) {    //# error ...    DWRITE("protocol.create: can't bind to the socket.\n");    ok = 0;    if(con_fail)    con_fail(this_object(), @extra_args);    }       if(snmp_errno)    THROW(({"Failed to bind to SNMP port.\n", backtrace()}));    DWRITE("protocol.bind: success!\n");    -  DWRITE(sprintf("protocol.create: local adress:port binded: [%s:%d].\n", local_host, lport)); +  DWRITE(sprintf("protocol.create: local adress:port bound: [%s:%d].\n", local_host, lport));      }       -  + //! return the whole SNMP message in raw format   mapping readmsg() { -  //: returns the whole SNMP message in raw format +     mapping rv;       rv = read();    return(rv);   }    -  + //! decode ASN1 data, if garbaged ignore it   mapping decode_asn1_msg(mapping rawd) { -  //: decode ASN1 data, if garbaged ignore it +        object xdec = snmp_der_decode(rawd->data);    string msgid = (string)xdec->elements[2]->elements[0]->value;    int errno = xdec->elements[2]->elements[1]->value;    mapping msg = ([ "ip":rawd->ip,    "port":rawd->port,    "error-status":errno,    "error-string":snmp_errlist[errno],    "error-index":xdec->elements[2]->elements[2]->value,    "version":xdec->elements[0]->value,    "community":xdec->elements[1]->value,    "op":xdec->elements[2]->get_tag(),    "attribute":Array.map(xdec->elements[2]->elements[3]->elements, lambda(object duo) { return(([(array(string))(duo->elements[0]->id)*".":duo->elements[1]->value])); } )    ]);       return(([msgid:msg]));   }       -  + //! decode raw pdu message and place in message pool   void to_pool(mapping rawd) {    //: put decoded msg to the pool       msgpool += decode_asn1_msg(rawd);      }      mapping|int from_pool(string msgid) {    //: get data from poll    mapping msg;       if(zero_type(msgpool[msgid]))    return 0;       msg = msgpool[msgid];    msgpool -= ([msgid:msg]);    return(msg);   }       -  + //! read decoded message from pool   mapping readmsg_from_pool(int msgid) {    //: read SNMP response PDU from PDU pool       mapping rv = from_pool((string)msgid);    if(rv)    return(rv);      #define HACK 1   #ifdef HACK    mapping tmpm = readmsg();
pike.git/lib/modules/Protocols.pmod/SNMP.pmod/protocol.pike:311:    //LOCK    int rv = request_id++;    //UNLOCK    return(rv);   }      //:   //: read_response   //:   array(mapping)|int read_response(int msgid) { -  //: GetResponse-PDU low call +     mapping rawpdu = readmsg_from_pool(msgid);          return(({rawpdu}));   }    - //: - //: get_request - //: + //! + //! GetRequest-PDU call + //! + //! @param varlist + //! an array of OIDs to GET + //! @param rem_addr + //! @param rem_port + //! remote address an UDP port to send request to (optional) + //! @returns + //! request ID   int get_request(array(string) varlist, string|void rem_addr,    int|void rem_port) { -  //: GetRequest-PDU low call +     object pdu;    int id = get_req_id(), flg;    array vararr = ({});       foreach(varlist, string varname)    vararr += ({Standards.ASN1.Types.asn1_sequence(    ({Standards.ASN1.Types.asn1_identifier(    @Array.map(varname/".", lambda(string el){ return((int)el);})),    Standards.ASN1.Types.asn1_integer(1)}) //doesn't sense but req    )});       pdu = Protocols.LDAP.ldap_privates.asn1_context_sequence(0,    ({Standards.ASN1.Types.asn1_integer(id), // request-id    Standards.ASN1.Types.asn1_integer(0), // error-status    Standards.ASN1.Types.asn1_integer(0), // error-index    Standards.ASN1.Types.asn1_sequence(vararr)})    );       // now we have PDU ... -  flg = writemsg(rem_addr||remote_host, rem_port || port || SNMP_DEFAULT_PORT, pdu); +  flg = writemsg(rem_addr||remote_host, rem_port || remote_port || SNMP_DEFAULT_PORT, pdu);       return id;      }      object mk_asn1_val(string type, int|string val) {   // returns appropriate ASN.1 value       object rv;   
pike.git/lib/modules/Protocols.pmod/SNMP.pmod/protocol.pike:399:    rv = asn1_application_integer(6,val);    break;       default: // bad type!    return 0;    }       return rv;   }    - //: - //: get_response - //: + //! + //! GetResponse-PDU call + //! + //! @param varlist + //! a mapping containing data to return + //! @mapping + //! @member array oid1 + //! @array + //! @elem string type + //! data type such as tick, oid, gauge, etc + //! @elem mixed data + //! data to return for oid1 + //! @endarray + //! @member array oid2 + //! @array + //! @elem string type + //! data type such as tick, oid, gauge, etc + //! @elem mixed data + //! data to return for oid2 + //! @endarray + //! @member array oidn + //! @array + //! @elem string type + //! data type such as tick, oid, gauge, etc + //! @elem mixed data + //! data to return for oidn + //! @endarray + //! @endmapping + //! @param origdata + //! original received decoded pdu that this response corresponds to + //! @param errcode + //! error code + //! @param erridx + //! error index + //! @returns + //! request ID   int get_response(mapping varlist, mapping origdata, int|void errcode, int|void erridx) {    //: GetResponse-PDU low call    object pdu;    int id = indices(origdata)[0], flg;    array vararr = ({});       foreach(indices(varlist), string varname)    if(arrayp(varlist[varname]) || sizeof(varlist[varname]) > 1) {    vararr += ({Standards.ASN1.Types.asn1_sequence(    ({Standards.ASN1.Types.asn1_identifier(
pike.git/lib/modules/Protocols.pmod/SNMP.pmod/protocol.pike:425:    }       pdu = Protocols.LDAP.ldap_privates.asn1_context_sequence(2,    ({Standards.ASN1.Types.asn1_integer(id), // request-id    Standards.ASN1.Types.asn1_integer(errcode), // error-status    Standards.ASN1.Types.asn1_integer(erridx), // error-index    Standards.ASN1.Types.asn1_sequence(vararr)})    );       // now we have PDU ... -  flg = writemsg(origdata[id]->ip||remote_host, origdata[id]->port || port || SNMP_DEFAULT_PORT, pdu); +  flg = writemsg(origdata[id]->ip||remote_host, origdata[id]->port || remote_port || SNMP_DEFAULT_PORT, pdu);       return id;      }       - //: - //: get_nextrequest - //: + //! + //! GetNextRequest-PDU call + //! + //! @param varlist + //! an array of OIDs to GET + //! @param rem_addr + //! @param rem_port + //! remote address an UDP port to send request to (optional) + //! @returns + //! request ID   int get_nextrequest(array(string) varlist, string|void rem_addr,    int|void rem_port) {    //: GetNextRequest-PDU low call    object pdu;    int id = get_req_id(), flg;    array vararr = ({});       foreach(varlist, string varname)    vararr += ({Standards.ASN1.Types.asn1_sequence(    ({Standards.ASN1.Types.asn1_identifier(
pike.git/lib/modules/Protocols.pmod/SNMP.pmod/protocol.pike:457:    )});       pdu = Protocols.LDAP.ldap_privates.asn1_context_sequence(1,    ({Standards.ASN1.Types.asn1_integer(id), // request-id    Standards.ASN1.Types.asn1_integer(0), // error-status    Standards.ASN1.Types.asn1_integer(0), // error-index    Standards.ASN1.Types.asn1_sequence(vararr)})    );       // now we have PDU ... -  flg = writemsg(rem_addr||remote_host, rem_port || port || SNMP_DEFAULT_PORT, pdu); +  flg = writemsg(rem_addr||remote_host, rem_port || remote_port || SNMP_DEFAULT_PORT, pdu);       return id;      }    - //: - //: set_request - //: + //! + //! SetRequest-PDU call + //! + //! @param varlist + //! a mapping of OIDs to SET + //! @mapping + //! @member array oid1 + //! @array + //! @elem string type + //! data type such as tick, oid, gauge, etc + //! @elem mixed data + //! data to return for oid1 + //! @endarray + //! @member array oid2 + //! @array + //! @elem string type + //! data type such as tick, oid, gauge, etc + //! @elem mixed data + //! data to return for oid2 + //! @endarray + //! @member array oidn + //! @array + //! @elem string type + //! data type such as tick, oid, gauge, etc + //! @elem mixed data + //! data to return for oidn + //! @endarray + //! @endmapping + //! @param rem_addr + //! @param rem_port + //! remote address an UDP port to send request to (optional) + //! @returns + //! request ID   int set_request(mapping varlist, string|void rem_addr,    int|void rem_port) {    //: SetRequest-PDU low call    object pdu;    int id = get_req_id(), flg;    array vararr = ({});       foreach(indices(varlist), string varname)    vararr += ({Standards.ASN1.Types.asn1_sequence(    ({Standards.ASN1.Types.asn1_identifier(
pike.git/lib/modules/Protocols.pmod/SNMP.pmod/protocol.pike:488:    )});       pdu = Protocols.LDAP.ldap_privates.asn1_context_sequence(3,    ({Standards.ASN1.Types.asn1_integer(id), // request-id    Standards.ASN1.Types.asn1_integer(0), // error-status    Standards.ASN1.Types.asn1_integer(0), // error-index    Standards.ASN1.Types.asn1_sequence(vararr)})    );       // now we have PDU ... -  flg = writemsg(rem_addr||remote_host, rem_port || port || SNMP_DEFAULT_PORT, pdu); +  flg = writemsg(rem_addr||remote_host, rem_port || remote_port || SNMP_DEFAULT_PORT, pdu);       return id;   }    - //: - //: trap - //: +  + //! send an SNMP-v1 trap + //! + //! @param varlist + //! a mapping of OIDs to include in trap + //! @mapping + //! @member array oid1 + //! @array + //! @elem string type + //! data type such as tick, oid, gauge, etc + //! @elem mixed data + //! data to return for oid1 + //! @endarray + //! @member array oid2 + //! @array + //! @elem string type + //! data type such as tick, oid, gauge, etc + //! @elem mixed data + //! data to return for oid2 + //! @endarray + //! @member array oidn + //! @array + //! @elem string type + //! data type such as tick, oid, gauge, etc + //! @elem mixed data + //! data to return for oidn + //! @endarray + //! @endmapping + //! @param oid + //! @param type + //! generic trap-type + //! @param spectype + //! specific trap-type + //! @param ticks + //! uptime + //! @param locip + //! originating ip address of the trap + //! @param remaddr + //! @param remport + //! address and UDP to send trap to + //! @returns + //! request id   int trap(mapping varlist, string oid, int type, int spectype, int ticks,    string|void locip, string|void remaddr, int|void remport) {    //: Trap-PDU low call    object pdu;    int id = get_req_id(), flg;    array vararr = ({});    string lip = "1234";      // DWRITE(sprintf("protocols.trap: varlist: %O, oid: %O, type: %O, spectype: %O,"   // " ticks: %O, locip: %O, remaddr: %O, remport: %O\n",
pike.git/lib/modules/Protocols.pmod/SNMP.pmod/protocol.pike:532:    mk_asn1_val("ipaddr", lip), // ip address (UGLY!)    mk_asn1_val("int", type), // type (0 = coldstart, ...)    mk_asn1_val("int", spectype), // enterprise type    mk_asn1_val("tick", ticks), // uptime    Standards.ASN1.Types.asn1_sequence(vararr) // optional vars    })       );       // now we have PDU ... -  flg = writemsg(remaddr||remote_host, remport || port || SNMP_DEFAULT_TRAPPORT, pdu); +  flg = writemsg(remaddr||remote_host, remport || remote_port || SNMP_DEFAULT_TRAPPORT, pdu);       return id;   }