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

version» Context lines:

pike.git/lib/modules/Protocols.pmod/SNMP.pmod/protocol.pike:10:   //! 1155-7 : v1   //!   //! 1901-4 : v2/community (Bulk ops aren't implemented!)   //!   //! planned:   //! 2742 : agentX   //!   //! 2570 : v3 description   //!    - // $Id: protocol.pike,v 1.11 2003/04/23 14:01:57 nilsson Exp $ + // $Id: protocol.pike,v 1.12 2003/09/01 16:35:09 nilsson 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:234:    DWRITE(sprintf("protocol.create: local adress:port bound: [%s:%d].\n", local_host, lport));      }         //! return the whole SNMP message in raw format   mapping readmsg() {    mapping rv;       rv = read(); -  return(rv); +  return rv;   }      //! decode ASN1 data, if garbaged ignore it   mapping decode_asn1_msg(mapping rawd) {       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])); } ) +  "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])); +  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); +  return msg;   }         //! read decoded message from pool   private mapping readmsg_from_pool(int msgid) {    //: read SNMP response PDU from PDU pool       mapping rv = from_pool((string)msgid);    if(rv) -  return(rv); +  return rv;      #define HACK 1   #ifdef HACK    mapping tmpm = readmsg();    to_pool(tmpm);   #endif    -  return(from_pool((string)msgid)); +  return from_pool((string)msgid);   }            private int writemsg(string rem_addr, int rem_port, object pdu) {    //: send SNMP encoded message and return status    //: OK, in most cases :)       object msg;    string rawd;
pike.git/lib/modules/Protocols.pmod/SNMP.pmod/protocol.pike:316:    msg = Standards.ASN1.Types.asn1_sequence(({    Standards.ASN1.Types.asn1_integer(snmp_version-1),    Standards.ASN1.Types.asn1_octet_string(snmp_community),    pdu}));       rawd = msg->get_der();       DWRITE(sprintf("protocol.writemsg: %O\n", rawd));       msize = send(rem_addr, rem_port, rawd); -  return(msize = sizeof(rawd) ? SNMP_SUCCESS : SNMP_SEND_ERROR); +  return (msize = sizeof(rawd) ? SNMP_SUCCESS : SNMP_SEND_ERROR);   }         private int get_req_id() {    //: returns unicate id       //LOCK    int rv = request_id++;    //UNLOCK -  return(rv); +  return rv;   }      //:   //: read_response   //:   array(mapping)|int read_response(int msgid) {    mapping rawpdu = readmsg_from_pool(msgid);       -  return(({rawpdu})); +  return ({rawpdu});   }      //!   //! 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)
pike.git/lib/modules/Protocols.pmod/SNMP.pmod/protocol.pike:358:   //! request ID   int get_request(array(string) varlist, string|void rem_addr,    int|void rem_port) {    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);})), +  @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)})    );   
pike.git/lib/modules/Protocols.pmod/SNMP.pmod/protocol.pike:384:   }      object mk_asn1_val(string type, int|string val) {   // returns appropriate ASN.1 value       object rv;       switch(type) {    case "oid": // OID    rv = Standards.ASN1.Types.asn1_identifier( -  @Array.map(val/".", lambda(string el){ return((int)el);})); +  @Array.map(val/".", lambda(string el){ return (int)el;}));    break;       case "int": // INTEGER    rv = Standards.ASN1.Types.asn1_integer(val);    break;       case "str": // STRING    rv = Standards.ASN1.Types.asn1_octet_string(val);    break;   
pike.git/lib/modules/Protocols.pmod/SNMP.pmod/protocol.pike:473:   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( -  @Array.map(varname/".", lambda(string el){ return((int)el);})), +  @Array.map(varname/".", lambda(string el){ return (int)el;})),    mk_asn1_val(varlist[varname][0], varlist[varname][1])})    )});    }       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)})    );
pike.git/lib/modules/Protocols.pmod/SNMP.pmod/protocol.pike:513:   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( -  @Array.map(varname/".", lambda(string el){ return((int)el);})), +  @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(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)})    );   
pike.git/lib/modules/Protocols.pmod/SNMP.pmod/protocol.pike:582:   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( -  @Array.map(varname/".", lambda(string el){ return((int)el);})), +  @Array.map(varname/".", lambda(string el){ return (int)el; })),    mk_asn1_val(varlist[varname][0], varlist[varname][1])})    )});       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)})    );   
pike.git/lib/modules/Protocols.pmod/SNMP.pmod/protocol.pike:663:    if (has_value(locip,":")) // FIXME: Can't handle IPv6    locip = "0.0.0.0";    if (sizeof(locip/".") != 4)    locip = "0.0.0.0"; //FIXME: what for hell I want to do with such ugly value?    //sscanf(locip, "%d.%d.%d.%d", @lip);    sscanf(locip, "%d.%d.%d.%d", lip[0], lip[1], lip[2], lip[3]);       foreach(indices(varlist), string varname)    vararr += ({Standards.ASN1.Types.asn1_sequence(    ({Standards.ASN1.Types.asn1_identifier( -  @Array.map(varname/".", lambda(string el){ return((int)el);})), +  @Array.map(varname/".", lambda(string el){ return (int)el; })),    mk_asn1_val(varlist[varname][0], varlist[varname][1])})    )});    pdu = Protocols.LDAP.ldap_privates.asn1_context_sequence(4,    ({    mk_asn1_val("oid", oid), // enterprise OID    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 || remote_port || SNMP_DEFAULT_TRAPPORT, pdu);       return id;   }