# BRCM_VERSION=3
[bcm963xx.git] / userapps / opensource / net-snmp / perl / OID / OID.xs
1 /* -*- C -*- */
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5
6 #include <net-snmp/net-snmp-config.h>
7 #include <net-snmp/net-snmp-includes.h>
8
9 /* pulled from Dave's, yet-to-be-used, net-snmp library rewrite.
10    autocompatibility for the future? */
11
12 #define NETSNMP_NAMEBUF_LEN 128
13 typedef struct netsnmp_oid_s {
14     oid                 *name;
15     unsigned int         len;
16     oid                  namebuf[ NETSNMP_NAMEBUF_LEN ];
17 } netsnmp_oid;
18
19 static int
20 not_here(char *s)
21 {
22     croak("%s not implemented on this architecture", s);
23     return -1;
24 }
25
26 static double
27 constant(char *name, int len, int arg)
28 {
29     errno = EINVAL;
30     return 0;
31 }
32
33 netsnmp_oid *
34 nso_newarrayptr(oid *name, size_t name_len) 
35 {
36     netsnmp_oid *RETVAL;
37     RETVAL = SNMP_MALLOC_TYPEDEF(netsnmp_oid);
38     RETVAL->name = RETVAL->namebuf;
39     RETVAL->len = name_len;
40     memcpy(RETVAL->name, name, name_len * sizeof(oid));
41     return RETVAL;
42 }
43
44 MODULE = NetSNMP::OID           PACKAGE = NetSNMP::OID          PREFIX=nso_
45
46 netsnmp_oid *
47 nso_newptr(initstring)
48     char *initstring
49     CODE:
50         RETVAL = SNMP_MALLOC_TYPEDEF(netsnmp_oid);
51         RETVAL->name = RETVAL->namebuf;
52         RETVAL->len = sizeof(RETVAL->namebuf)/sizeof(RETVAL->namebuf[0]);
53         if (!snmp_parse_oid(initstring, (oid *) RETVAL->name, &RETVAL->len)) {
54             fprintf(stderr, "Can't parse: %s\n", initstring);
55             RETVAL->len = 0;
56             RETVAL = NULL;
57         }
58     OUTPUT:
59         RETVAL
60
61 double
62 constant(sv,arg)
63     PREINIT:
64         STRLEN          len;
65     INPUT:
66         SV *            sv
67         char *          s = SvPV(sv, len);
68         int             arg
69     CODE:
70         RETVAL = constant(s,len,arg);
71     OUTPUT:
72         RETVAL
73
74 int
75 _snmp_oid_compare(oid1, oid2)
76     netsnmp_oid *oid1;
77     netsnmp_oid *oid2;
78     CODE:
79         RETVAL = snmp_oid_compare((oid *) oid1->name, oid1->len,
80                                   (oid *) oid2->name, oid2->len);
81     OUTPUT:
82         RETVAL
83
84 MODULE = NetSNMP::OID  PACKAGE = netsnmp_oidPtr  PREFIX = nsop_
85
86 void
87 nsop_DESTROY(oid1)
88         netsnmp_oid *oid1
89     CODE:
90         free(oid1);
91         
92 char *
93 nsop_to_string(oid1)
94         netsnmp_oid *oid1
95     PREINIT:
96         static char mystr[SNMP_MAXBUF];
97     CODE:
98         {
99             if (oid1->len == 0)
100                 snprintf(mystr, sizeof(mystr), "Illegal OID");
101             else
102                 snprint_objid(mystr, sizeof(mystr),
103                               (oid *) oid1->name, oid1->len);
104             RETVAL = mystr;
105         }
106                 
107     OUTPUT:
108         RETVAL
109
110 void
111 nsop_to_array(oid1)
112     netsnmp_oid *oid1;
113     PREINIT:
114         int i;
115
116     PPCODE:
117         EXTEND(SP, oid1->len);
118         for(i=0; i < oid1->len; i++) {
119             PUSHs(sv_2mortal(newSVnv(oid1->name[i])));
120         }
121
122 void
123 nsop_append(oid1, string)
124     netsnmp_oid *oid1;
125     char *string;
126     PREINIT:
127     oid name[128];
128     size_t name_len = 128;
129     int i;
130     CODE: 
131     {
132         if (!snmp_parse_oid(string, (oid *) name, &name_len)) {
133             /* XXX */
134         }
135         if (oid1->len + name_len > 128) {
136             /* XXX: illegal */
137         }
138         for(i = 0; i < name_len; i++) {
139             oid1->name[i+oid1->len] = name[i];
140         }
141         oid1->len += name_len;
142     }
143
144 void
145 nsop_append_oid(oid1, oid2)
146     netsnmp_oid *oid1;
147     netsnmp_oid *oid2;
148     PREINIT:
149     int i;
150     CODE: 
151     {
152         if (oid1->len + oid2->len > 128) {
153             /* XXX: illegal */
154         }
155         for(i = 0; i < oid2->len; i++) {
156             oid1->name[i+oid1->len] = oid2->name[i];
157         }
158         oid1->len += oid2->len;
159     }
160
161 int
162 nsop_length(oid1)
163     netsnmp_oid *oid1;
164     CODE: 
165     {
166         RETVAL = oid1->len;
167     }
168     OUTPUT:
169     RETVAL
170     
171 netsnmp_oid *
172 nsop_clone(oid1)
173     netsnmp_oid *oid1;
174     PREINIT:
175     netsnmp_oid *oid2;
176     CODE:
177     {
178         oid2 = nso_newarrayptr(oid1->name, oid1->len);
179         RETVAL = oid2;
180     }
181 OUTPUT:
182     RETVAL
183