4e36d42007-09-10Henrik Grubbström (Grubba) // // SNMP helper stuff. //
0917d32013-03-04Anders Johansson // $Id$
4e36d42007-09-10Henrik Grubbström (Grubba) // // 2007-08-29 Henrik Grubbström // // Some OIDs //! iso.organizations.dod.internet constant INTERNET_OID = ({ 1, 3, 6, 1 }); //! iso.organizations.dod.internet.private.enterprises.roxenis constant RIS_OID = INTERNET_OID + ({ 4, 1, 8614 }); //! iso.organizations.dod.internet.private.enterprises.roxenis.app.webserver constant RIS_OID_WEBSERVER = RIS_OID + ({ 1, 1 }); class Documentation(string name, string doc) { }
304d302007-09-10Henrik Grubbström (Grubba) class Updateable(function(:mixed) fun) { void update_value() { if (fun) {
a396fb2007-10-25Henrik Grubbström (Grubba)  mixed val = fun();
17cf052014-11-19Martin Karlgren  if (undefinedp (val)) {
18a0742016-05-11Henrik Grubbström (Grubba)  werror("SNMP: Got undefined value from callback %O.\n" "%s\n", fun, describe_backtrace(backtrace()));
a396fb2007-10-25Henrik Grubbström (Grubba)  }
17cf052014-11-19Martin Karlgren  this_object()->init (val);
7f54e72007-09-12Henrik Grubbström (Grubba)  this_object()->der = UNDEFINED;
304d302007-09-10Henrik Grubbström (Grubba)  } } }
c4f2462007-11-03Henrik Grubbström (Grubba) class OwnerInfo { Configuration conf; RoxenModule module; }
4e36d42007-09-10Henrik Grubbström (Grubba) // ASN1 datatypes. class app_integer {
792b832014-06-27Henrik Grubbström (Grubba)  inherit Standards.ASN1.Types.Integer : integer;
4e36d42007-09-10Henrik Grubbström (Grubba)  inherit Documentation : doc;
304d302007-09-10Henrik Grubbström (Grubba)  inherit Updateable : update;
c4f2462007-11-03Henrik Grubbström (Grubba)  inherit OwnerInfo : owner_info;
4e36d42007-09-10Henrik Grubbström (Grubba)  constant type_name = "APPLICATION INTEGER";
6b1d4d2014-06-25Henrik Grubbström (Grubba)  int cls = 1; int tag = 0;
fc40392008-08-15Martin Stjernholm  protected void create(int|function(:int) val, string|void name, string|void doc_string)
4e36d42007-09-10Henrik Grubbström (Grubba)  {
304d302007-09-10Henrik Grubbström (Grubba)  if (intp(val)) { update::create(UNDEFINED); integer::create(val); } else { update::create(val);
ddfdc02007-10-25Henrik Grubbström (Grubba)  integer::create(0);
304d302007-09-10Henrik Grubbström (Grubba)  }
4e36d42007-09-10Henrik Grubbström (Grubba)  doc::create(name, doc_string); }
fc40392008-08-15Martin Stjernholm  protected string _sprintf(int t)
ddfdc02007-10-25Henrik Grubbström (Grubba)  {
a54a1b2007-10-25Henrik Grubbström (Grubba)  switch(t) { case 's': return (string)value; default: return sprintf("%s[%d][%d](%O)", type_name, cls, tag, value); }
ddfdc02007-10-25Henrik Grubbström (Grubba)  }
4e36d42007-09-10Henrik Grubbström (Grubba) } class app_octet_string {
792b832014-06-27Henrik Grubbström (Grubba)  inherit Standards.ASN1.Types.OctetString : octet_string;
4e36d42007-09-10Henrik Grubbström (Grubba)  inherit Documentation : doc;
304d302007-09-10Henrik Grubbström (Grubba)  inherit Updateable : update;
c4f2462007-11-03Henrik Grubbström (Grubba)  inherit OwnerInfo : owner_info;
4e36d42007-09-10Henrik Grubbström (Grubba)  constant type_name = "APPLICATION OCTET_STRING";
6b1d4d2014-06-25Henrik Grubbström (Grubba)  int cls = 1; int tag = 0;
fc40392008-08-15Martin Stjernholm  protected void create(string|function(:string) val, string|void name, string|void doc_string)
4e36d42007-09-10Henrik Grubbström (Grubba)  {
304d302007-09-10Henrik Grubbström (Grubba)  if (stringp(val)) { update::create(UNDEFINED); octet_string::create(val); } else { update::create(val);
ddfdc02007-10-25Henrik Grubbström (Grubba)  octet_string::create("");
304d302007-09-10Henrik Grubbström (Grubba)  }
4e36d42007-09-10Henrik Grubbström (Grubba)  doc::create(name, doc_string); }
fc40392008-08-15Martin Stjernholm  protected string _sprintf(int t)
ddfdc02007-10-25Henrik Grubbström (Grubba)  {
a54a1b2007-10-25Henrik Grubbström (Grubba)  switch(t) { case 's': return (string)value; default: return sprintf("%s[%d][%d](%O)", type_name, cls, tag, value); }
ddfdc02007-10-25Henrik Grubbström (Grubba)  }
4e36d42007-09-10Henrik Grubbström (Grubba) } class OID {
792b832014-06-27Henrik Grubbström (Grubba)  inherit Standards.ASN1.Types.Identifier : identifier;
4e36d42007-09-10Henrik Grubbström (Grubba)  inherit Documentation : doc;
c4f2462007-11-03Henrik Grubbström (Grubba)  inherit OwnerInfo : owner_info;
4e36d42007-09-10Henrik Grubbström (Grubba)  constant type_name = "OID";
fc40392008-08-15Martin Stjernholm  protected void create(array(int) oid, string|void name, string|void doc_string)
4e36d42007-09-10Henrik Grubbström (Grubba)  { identifier::create(@oid); doc::create(name, doc_string); }
fc40392008-08-15Martin Stjernholm  protected string _sprintf(int t)
ddfdc02007-10-25Henrik Grubbström (Grubba)  {
a54a1b2007-10-25Henrik Grubbström (Grubba)  switch(t) { case 's': return ((array(string))id) * "."; default: return sprintf("%s[%d][%d](%O)", type_name, cls, tag, ((array(string))id) * ".");
ddfdc02007-10-25Henrik Grubbström (Grubba)  } }
4e36d42007-09-10Henrik Grubbström (Grubba) } class Integer {
792b832014-06-27Henrik Grubbström (Grubba)  inherit Standards.ASN1.Types.Integer : integer;
4e36d42007-09-10Henrik Grubbström (Grubba)  inherit Documentation : doc;
304d302007-09-10Henrik Grubbström (Grubba)  inherit Updateable : update;
c4f2462007-11-03Henrik Grubbström (Grubba)  inherit OwnerInfo : owner_info;
4e36d42007-09-10Henrik Grubbström (Grubba)  constant type_name = "INTEGER";
fc40392008-08-15Martin Stjernholm  protected void create(int|function(:int) val, string|void name, string|void doc_string)
4e36d42007-09-10Henrik Grubbström (Grubba)  {
304d302007-09-10Henrik Grubbström (Grubba)  if (intp(val)) { update::create(UNDEFINED); integer::create(val); } else { update::create(val);
ddfdc02007-10-25Henrik Grubbström (Grubba)  integer::create(0);
304d302007-09-10Henrik Grubbström (Grubba)  }
4e36d42007-09-10Henrik Grubbström (Grubba)  doc::create(name, doc_string); }
fc40392008-08-15Martin Stjernholm  protected string _sprintf(int t)
ddfdc02007-10-25Henrik Grubbström (Grubba)  {
a54a1b2007-10-25Henrik Grubbström (Grubba)  switch(t) { case 'd': case 's': return (string)value; default: return sprintf("%s[%d][%d](%O)", type_name, cls, tag, value);
ddfdc02007-10-25Henrik Grubbström (Grubba)  } }
4e36d42007-09-10Henrik Grubbström (Grubba) } class String {
792b832014-06-27Henrik Grubbström (Grubba)  inherit Standards.ASN1.Types.OctetString : octet_string;
4e36d42007-09-10Henrik Grubbström (Grubba)  inherit Documentation : doc;
304d302007-09-10Henrik Grubbström (Grubba)  inherit Updateable : update;
c4f2462007-11-03Henrik Grubbström (Grubba)  inherit OwnerInfo : owner_info;
4e36d42007-09-10Henrik Grubbström (Grubba)  constant type_name = "STRING";
fc40392008-08-15Martin Stjernholm  protected void create(string|function(:string) val, string|void name, string|void doc_string)
4e36d42007-09-10Henrik Grubbström (Grubba)  {
304d302007-09-10Henrik Grubbström (Grubba)  if (stringp(val)) { update::create(UNDEFINED); octet_string::create(val); } else { update::create(val);
ddfdc02007-10-25Henrik Grubbström (Grubba)  octet_string::create("");
304d302007-09-10Henrik Grubbström (Grubba)  }
4e36d42007-09-10Henrik Grubbström (Grubba)  doc::create(name, doc_string); }
fc40392008-08-15Martin Stjernholm  protected string _sprintf(int t)
ddfdc02007-10-25Henrik Grubbström (Grubba)  {
a54a1b2007-10-25Henrik Grubbström (Grubba)  switch(t) { case 's': return (string)value; default: return sprintf("%s[%d][%d](%O)", type_name, cls, tag, (string)value);
ddfdc02007-10-25Henrik Grubbström (Grubba)  } }
4e36d42007-09-10Henrik Grubbström (Grubba) } class Counter { inherit app_integer;
6b1d4d2014-06-25Henrik Grubbström (Grubba)  int tag = 1;
4e36d42007-09-10Henrik Grubbström (Grubba)  constant type_name = "COUNTER"; } class Gauge { inherit app_integer;
6b1d4d2014-06-25Henrik Grubbström (Grubba)  int tag = 2;
4e36d42007-09-10Henrik Grubbström (Grubba)  constant type_name = "GAUGE"; }
ddfdc02007-10-25Henrik Grubbström (Grubba) //! One tick is 1/100 seconds.
4e36d42007-09-10Henrik Grubbström (Grubba) class Tick { inherit app_integer;
6b1d4d2014-06-25Henrik Grubbström (Grubba)  int tag = 3;
4e36d42007-09-10Henrik Grubbström (Grubba)  constant type_name = "TICK";
fc40392008-08-15Martin Stjernholm  protected string _sprintf(int t)
ddfdc02007-10-25Henrik Grubbström (Grubba)  { if (t == 's') {
39fa2e2010-11-25Henrik Grubbström (Grubba)  return Roxen.short_date((int)(time(1) - value/100));
ddfdc02007-10-25Henrik Grubbström (Grubba)  } return ::_sprintf(t); }
4e36d42007-09-10Henrik Grubbström (Grubba) } class Opaque { inherit app_octet_string;
6b1d4d2014-06-25Henrik Grubbström (Grubba)  int tag = 4;
4e36d42007-09-10Henrik Grubbström (Grubba)  constant type_name = "OPAQUE";
fc40392008-08-15Martin Stjernholm  protected string _sprintf(int t)
ddfdc02007-10-25Henrik Grubbström (Grubba)  { if (t == 's') return ""; return ::_sprintf(t); }
4e36d42007-09-10Henrik Grubbström (Grubba) } class Counter64 { inherit app_integer;
6b1d4d2014-06-25Henrik Grubbström (Grubba)  int tag = 6;
4e36d42007-09-10Henrik Grubbström (Grubba)  constant type_name = "COUNTER64"; }
87b18e2014-06-25Henrik Grubbström (Grubba) class ContextOctetString { inherit Standards.ASN1.Types.OctetString; int cls = 2; constant type_name = "CONTEXT OCTET STRING"; protected void create(int tag, string(8bit) val) { this_program::tag = tag; ::create(val); } } class ContextSequence { inherit Standards.ASN1.Types.Sequence; int cls = 2; constant type_name = "CONTEXT SEQUENCE"; protected void create(int tag, array arg) { this_program::tag = tag; ::create(arg); } }
4e36d42007-09-10Henrik Grubbström (Grubba) //! No such object marker.
87b18e2014-06-25Henrik Grubbström (Grubba) ContextOctetString NO_SUCH_OBJECT = ContextOctetString(0, "");
4e36d42007-09-10Henrik Grubbström (Grubba)  //! No such instance marker.
87b18e2014-06-25Henrik Grubbström (Grubba) ContextOctetString NO_SUCH_INSTANCE = ContextOctetString(1, "");
4e36d42007-09-10Henrik Grubbström (Grubba)  //! End of MIB marker.
87b18e2014-06-25Henrik Grubbström (Grubba) ContextOctetString END_OF_MIB = ContextOctetString(2, "");
4e36d42007-09-10Henrik Grubbström (Grubba)  //! The NULL counter. Counter NULL_COUNTER = Counter(0); class SimpleMIB { inherit ADT.Trie;
bc077a2010-05-04Henrik Grubbström (Grubba)  protected void init(array(int) oid, array(int) oid_suffix, array(Standards.ASN1.Types.Object| function|array|mapping)| mapping(int:Standards.ASN1.Types.Object| function|array|mapping) values)
4e36d42007-09-10Henrik Grubbström (Grubba)  {
bc077a2010-05-04Henrik Grubbström (Grubba)  foreach(values; int i; function|Standards.ASN1.Types.Object|array|mapping val) { if (arrayp(val) || mappingp(val)) {
b02fc82010-05-04Henrik Grubbström (Grubba)  init(oid + ({ i }), oid_suffix, val);
bc077a2010-05-04Henrik Grubbström (Grubba)  } else if (!zero_type(val)) {
747c742007-09-14Henrik Grubbström (Grubba)  insert(oid + ({ i }) + oid_suffix + ({ 0 }), val);
4e36d42007-09-10Henrik Grubbström (Grubba)  } } }
bc077a2010-05-04Henrik Grubbström (Grubba)  protected void create(array(int) oid, array(int) oid_suffix, array(Standards.ASN1.Types.Object| function|array|mapping)| mapping(int:Standards.ASN1.Types.Object| function|array|mapping) values) { ::create(oid); init(oid, oid_suffix, values); }
4e36d42007-09-10Henrik Grubbström (Grubba)  Standards.ASN1.Types.Object lookup(array(int) key) { function|Standards.ASN1.Types.Object res = ::lookup(key); if (zero_type(res)) return UNDEFINED; if (functionp(res)) return res(); return res; } }
ddfdc02007-10-25Henrik Grubbström (Grubba) 
c4f2462007-11-03Henrik Grubbström (Grubba) void set_owner(ADT.Trie mib, Configuration conf, RoxenModule|void module) { array(int) oid = mib->first(); while (oid) { Standards.ASN1.Types.Object o = mib->lookup(oid); catch { o->conf = conf; if (module) { o->module = module; } }; oid = mib->next(oid); } } void remove_owned(ADT.Trie mib, Configuration conf, RoxenModule|void module) { array(int) oid = mib->first(); while(oid) { Standards.ASN1.Types.Object o = mib->lookup(oid); if (objectp(o) && (o->conf == conf) && (!module || (o->module == module))) { mib->remove(oid); } oid = mib->next(oid); } }
ddfdc02007-10-25Henrik Grubbström (Grubba) #if 0 // Not ready for production yet. class Describer(string symbol) { } class IndexDescriber { inherit Describer; constant is_index = "int"; } class StringIndexDescriber { inherit Describer; constant is_index = "string"; } class IndexedDescriber { inherit Describer; constant index = "int"; } class StringIndexedDescriber { inherit Describer; constant index = "string"; } ADT.Trie OID_ParseInfo = ADT.Trie(); void add_oid_path(array(int) oid, string symbolic_oid) { int i; foreach(symbolic_oid/".", string symbol) { if (i >= sizeof(oid)) return; if (sizeof(symbol)) { if (symbol[0] == '"') { OID_ParseInfo->insert(oid[..i], StringDescriber(symbol)); i += oid[i]; } else { OID_ParseInfo->insert(oid[..i], Describer(symbol)); } } i++; } } string format_oid(array(int) oid) { ADT.Trie parse_info = OID_ParseInfo; int i; array(IndexDescriber) indexers = ({}); array(string) res = ({}); for (i=0; i < sizeof(oid); i++) { int j = i; while(i < parse_info->offset) { if (oid[i] != parse_info->path[i]) { i = j; break; } i++; } if (i < parse_info->offset) break; Describer desc = parse_info->value; switch(desc && desc->is_index) { case "string": if (i + oid[i] < sizeof(oid)) { res += ({ sprintf("%O", (string)oid[i+1..i+oid[i]]) }); i += oid[i]; break; } res += ({ (string)oid[i] }); break; case 0: if (desc->symbol) { res += desc->symbol; break; } case "int": default: res += ({ (string)oid[i] }); } if (desc && desc->index) { indexers += ({ desc }); } while (parse_info && parse_info->offset < i) { parse_info = parse_info->trie[oid[parse_info->offset]]; } if (parse_info) break; } return res * "."; }
fc40392008-08-15Martin Stjernholm protected void create()
ddfdc02007-10-25Henrik Grubbström (Grubba) { add_oid_path(RIS_OID_WEBSERVER, "iso.organizations.dod.internet.private." "enterprises.roxenis.app.webserver"); } #endif /* 0 */