Branch: Tag:

2005-11-26

2005-11-26 03:58:55 by Martin Nilsson <mani@lysator.liu.se>

Slight cleanup

Rev: lib/modules/Protocols.pmod/SNMP.pmod/protocol.pike:1.14
Rev: lib/modules/Protocols.pmod/SNMP.pmod/snmp_globals.h:1.4

17:   //! 2570 : v3 description   //!    - // $Id: protocol.pike,v 1.13 2004/01/11 00:46:54 nilsson Exp $ + // $Id: protocol.pike,v 1.14 2005/11/26 03:58:55 nilsson Exp $         #include "snmp_globals.h"
38: Inside #if 1
   ::value = arg;    tagx = tagid;    } -  +    }      class asn1_application_integer
54: Inside #if 1
   ::init(arg);    tagx = tagid;    } -  +    }      object|mapping der_decode(object data, mapping types)
64: Inside #if 1
   string contents;       if ( (raw_tag & 0x1f) == 0x1f) -  error("ASN1.Decode: High tag numbers is not supported\n"); +  error("ASN1.Decode: High tag numbers is not supported.\n");       len = data->get_uint(1);    if (len & 0x80)
73: Inside #if 1
   contents = data->get_fix_string(len);       int tag = raw_tag & 0xdf; // Class and tag bits - //werror(sprintf("DEBx: tag=%O\n",tag)); +        program p = types[tag];       if (raw_tag & 0x20)    { -  /* Constructed encoding */ +  // Constructed encoding       array elements = ({ });    object struct = ADT.struct(contents);
101: Inside #if 1
      int i;    -  /* Ask object which types it expects for field i, decode it, and -  * record the decoded object */ +  // Ask object which types it expects for field i, decode it, and +  // record the decoded object.    for(i = 0; !struct->is_empty(); i++)    {    res->decode_constructed_element
113: Inside #if 1
   }    else    { -  /* Primitive encoding */ +  // Primitive encoding    return p ? p()->decode_primitive(contents)    : Standards.ASN1.Decode.primitive(tag, contents);    }
213:   //!   void create(int|void rem_port, string|void rem_addr, int|void loc_port, string|void loc_addr) {    +  werror("%O\n", wait); +     int lport = loc_port;       local_host = (!loc_addr || !sizeof(loc_addr)) ? SNMP_DEFAULT_LOCHOST : loc_addr;
228:    }       if(snmp_errno) -  THROW(({"Failed to bind to SNMP port.\n", backtrace()})); +  ERROR("Failed to bind to SNMP port.\n");    DWRITE("protocol.bind: success!\n");    -  DWRITE(sprintf("protocol.create: local adress:port bound: [%s:%d].\n", local_host, lport)); +  DWRITE("protocol.create: local adress:port bound: [%s:%d].\n", +  local_host, lport);      }         //! return the whole SNMP message in raw format - mapping readmsg() { + mapping readmsg(int|float|void timeout) {    mapping rv;       rv = read();
258:    "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":map(xdec->elements[2]->elements[3]->elements, +  lambda(object duo) { +  return ([ +  (array(string))(duo->elements[0]->id)*".":duo->elements[1]->value    ]); -  +  } ) +  ]);       return ([msgid:msg]);   }
320:       rawd = msg->get_der();    -  DWRITE(sprintf("protocol.writemsg: %O\n", rawd)); +  DWRITE("protocol.writemsg: %O\n", rawd);       msize = send(rem_addr, rem_port, rawd);    return (msize = sizeof(rawd) ? SNMP_SUCCESS : SNMP_SEND_ERROR);
365:    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(int))(varname/".")),    Standards.ASN1.Types.asn1_integer(1)}) //doesn't sense but req    )});   
377:    );       // now we have PDU ... -  flg = writemsg(rem_addr||remote_host, rem_port || remote_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) {
390:       switch(type) {    case "oid": // OID -  rv = Standards.ASN1.Types.asn1_identifier( -  @Array.map(val/".", lambda(string el){ return (int)el;})); +  rv = Standards.ASN1.Types.asn1_identifier( @(array(int))(val/".") );    break;       case "int": // INTEGER
403:    break;       case "ipaddr": // ipAddress -  rv = asn1_application_octet_string(0,val[..3]); +  rv = asn1_application_octet_string(0, val[..3]);    break;       case "count": // COUNTER -  rv = asn1_application_integer(1,val); +  rv = asn1_application_integer(1, val);    break;       case "gauge": // GAUGE -  rv = asn1_application_integer(2,val); +  rv = asn1_application_integer(2, val);    break;       case "tick": // TICK -  rv = asn1_application_integer(3,val); +  rv = asn1_application_integer(3, val);    break;       case "opaque": // OPAQUE -  rv = asn1_application_octet_string(4,val); +  rv = asn1_application_octet_string(4, val);    break;       case "count64": // COUNTER64 - v2 object -  rv = asn1_application_integer(6,val); +  rv = asn1_application_integer(6, val);    break;       default: // bad type! -  error("Unknown SNMP data type " + type + "."); -  return 0; +  error("Unknown SNMP data type %O.", type);    }       return rv;
470:   //! error index   //! @returns   //! request ID - int get_response(mapping varlist, mapping origdata, int|void errcode, int|void erridx) { + 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;
480:    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(int))(varname/".")),    mk_asn1_val(varlist[varname][0], varlist[varname][1])})    )});    }
493:    );       // now we have PDU ... -  flg = writemsg(origdata[id]->ip||remote_host, origdata[id]->port || remote_port || SNMP_DEFAULT_PORT, pdu); +  flg = writemsg(origdata[id]->ip||remote_host, +  origdata[id]->port || remote_port || SNMP_DEFAULT_PORT, pdu);       return id; -  +    }      
520:    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(int))(varname/".")),    Standards.ASN1.Types.asn1_integer(1)}) //doesn't sense but req    )});   
532:    );       // now we have PDU ... -  flg = writemsg(rem_addr||remote_host, rem_port || remote_port || SNMP_DEFAULT_PORT, pdu); +  flg = writemsg(rem_addr||remote_host, +  rem_port || remote_port || SNMP_DEFAULT_PORT, pdu);       return id; -  +    }      //!
589:    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(int))(varname/".")),    mk_asn1_val(varlist[varname][0], varlist[varname][1])})    )});   
601:    );       // now we have PDU ... -  flg = writemsg(rem_addr||remote_host, rem_port || remote_port || SNMP_DEFAULT_PORT, pdu); +  flg = writemsg(rem_addr||remote_host, +  rem_port || remote_port || SNMP_DEFAULT_PORT, pdu);       return id;   }
656:    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", - // varlist, oid, type, spectype, ticks, locip, remaddr, remport)); +     locip = locip || "0.0.0.0";    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(int))(varname/".")),    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 +  // enterprise OID +  mk_asn1_val("oid", oid), +  +  // ip address (UGLY!) +  mk_asn1_val("ipaddr", lip), +  +  // type (0 = coldstart, ...) +  mk_asn1_val("int", type), +  +  // enterprise type +  mk_asn1_val("int", spectype), +  +  // uptime +  mk_asn1_val("tick", ticks), +  +  // optional vars +  Standards.ASN1.Types.asn1_sequence(vararr)    })       );       // now we have PDU ... -  flg = writemsg(remaddr||remote_host, remport || remote_port || SNMP_DEFAULT_TRAPPORT, pdu); +  flg = writemsg(remaddr||remote_host, +  remport || remote_port || SNMP_DEFAULT_TRAPPORT, pdu);       return id;   }