-
Notifications
You must be signed in to change notification settings - Fork 69
/
snmp_lib.c
1210 lines (1143 loc) · 36.3 KB
/
snmp_lib.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
*
***** BEGIN LICENSE BLOCK *****
Copyright (C) 2022 Olof Hagsand and Kristofer Hallin
Sponsored by Siklu Communications LTD
This file is part of CLIXON.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Alternatively, the contents of this file may be used under the terms of
the GNU General Public License Version 3 or later (the "GPL"),
in which case the provisions of the GPL are applicable instead
of those above. If you wish to allow use of your version of this file only
under the terms of the GPL, and not to allow others to
use your version of this file under the terms of Apache License version 2,
indicate your decision by deleting the provisions above and replace them with
the notice and other provisions required by the GPL. If you do not delete
the provisions above, a recipient may use your version of this file under
the terms of any one of the Apache License version 2 or the GPL.
***** END LICENSE BLOCK *****
* See RFC 6643
* Extensions are grouped in some categories, the one I have seen are, example:
* 1. leaf
* smiv2:max-access "read-write";
* smiv2:oid "1.3.6.1.4.1.8072.2.1.1";
* smiv2:defval "42"; (not always)
* 2. container, list
* smiv2:oid "1.3.6.1.4.1.8072.2.1";
* 3. module level
* smiv2:alias "netSnmpExamples" {
* smiv2:oid "1.3.6.1.4.1.8072.2";
*
*/
#ifdef HAVE_CONFIG_H
#include "clixon_config.h" /* generated by config & autoconf */
#endif
#include <stdio.h>
#include <unistd.h>
#include <pwd.h>
#include <syslog.h>
#include <errno.h>
#include <signal.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h> /* inet_addr */
#include <arpa/inet.h>
#include <netinet/ether.h> /* ether_aton */
/* net-snmp */
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>
/* cligen */
#include <cligen/cligen.h>
/* clicon */
#include <clixon/clixon.h>
#include "snmp_lib.h"
/*
* Local variables
*/
/* Mapping between yang keyword string <--> clixon constants
* Here is also the place where doc on some types store variables (cv)
*/
/* Mapping between smiv2 yang extension access string string <--> netsnmp handler codes (agent_handler.h)
* Here is also the place where doc on some types store variables (cv)
* see netsnmp_handler_registration_create()
*/
static const map_str2int snmp_access_map[] = {
{"read-only", HANDLER_CAN_RONLY}, /* HANDLER_CAN_GETANDGETNEXT */
{"read-write", HANDLER_CAN_RWRITE}, /* HANDLER_CAN_GETANDGETNEXT | HANDLER_CAN_SET */
{"not-accessible", 0}, // XXX
{"accessible-for-notify", 0}, // XXX
{NULL, -1}
};
/* Map between clixon and ASN.1 types.
* @see net-snmp/library/asn1.h
* @see union netsnmp_vardata in net-snmp/types.h
*/
static const map_str2int snmp_type_map[] = {
{"int32", ASN_INTEGER}, // 2
{"string", ASN_OCTET_STR}, // 4
{"enumeration", ASN_INTEGER}, // 2 special case
{"uint32", ASN_GAUGE}, // 0x42 / 66
{"uint32", ASN_COUNTER}, // 0x41 / 65
{"uint32", ASN_TIMETICKS}, // 0x43 / 67
{"uint64", ASN_COUNTER64}, // 0x46 / 70
{"boolean", ASN_INTEGER}, // 2 special case -> enumeration
{"string", ASN_IPADDRESS}, // 64
{NULL, -1}
};
/* Map between clixon "orig" resolved type and ASN.1 types.
*/
static const map_str2int snmp_orig_map[] = {
{"counter32", ASN_COUNTER}, // 0x41 / 65
{"object-identifier-128", ASN_OBJECT_ID}, // 6
{"AutonomousType", ASN_OBJECT_ID}, // 6
{"DateAndTime", ASN_OCTET_STR}, // 4
{"UUIDorZero", ASN_OCTET_STR}, // 4
{"binary", ASN_OCTET_STR}, // 4
{"timeticks", ASN_TIMETICKS}, // 0x43 / 67
{"timestamp", ASN_TIMETICKS}, // 0x43 / 67
{"InetAddress", ASN_IPADDRESS}, // 0x40 / 64 (Dont see this being used)
{"ipv4-address", ASN_IPADDRESS}, // 0x40 / 64 (This is used instead)
{"RowStatus", CLIXON_ASN_ROWSTATUS}, // 0x40 / 64 (This is used instead)
{"phys-address", CLIXON_ASN_PHYS_ADDR}, /* Clixon extended string type */
{NULL, -1}
};
/* Map between SNMP message / mode str and int form
*/
static const map_str2int snmp_msg_map[] = {
{"MODE_SET_RESERVE1", MODE_SET_RESERVE1}, // 0
{"MODE_SET_RESERVE2", MODE_SET_RESERVE2}, // 1
{"MODE_SET_ACTION", MODE_SET_ACTION}, // 2
{"MODE_SET_COMMIT", MODE_SET_COMMIT}, // 3
{"MODE_SET_FREE", MODE_SET_FREE}, // 4
{"MODE_GET", MODE_GET}, // 160
{"MODE_GETNEXT", MODE_GETNEXT}, // 161
{NULL, -1}
};
/*! Translate from snmp string to int representation
* @note Internal snmpd, maybe find something in netsnmpd?
*/
int
snmp_access_str2int(char *modes_str)
{
return clicon_str2int(snmp_access_map, modes_str);
}
const char *
snmp_msg_int2str(int msg)
{
return clicon_int2str(snmp_msg_map, msg);
}
/*! Check equality of two OIDs
*
* @param[in] objid0 First OID vector
* @param[in] objid0len Length of first OID vector
* @param[in] objid1 Second OID vector
* @param[in] objid1len Length of second OID vector
* @retval 0 Equal
* @retval !=0 Not equal, see man memcmp
* Should really be netsnmp lib function, but cant find any?
*/
int
oid_eq(const oid *objid0,
size_t objid0len,
const oid *objid1,
size_t objid1len)
{
size_t min;
int ret;
if (objid0len < objid1len)
min = objid0len;
else
min = objid1len;
/* First compare common prefix */
ret = memcmp(objid0, objid1, min*sizeof(*objid0));
if (ret != 0)
return ret;
/* If equal, check lengths */
if (objid0len < objid1len)
return -1;
else if (objid0len > objid1len)
return 1;
else
return 0;
}
/*! Append a second OID to a first
* @param[in,out] objid0 First OID vector
* @param[in,out] objid0len Length of first OID vector
* @param[in] objid1 Second OID vector
* @param[in] objid1len Length of second OID vector
* @retval 0 OK
* @retval -1 Error
* Assume objid0 is allocated with MAX_OID_LEN > oid0len+oid1len
*/
int
oid_append(const oid *objid0,
size_t *objid0len,
const oid *objid1,
size_t objid1len)
{
void *dst;
dst = (void*)objid0;
dst += (*objid0len)*sizeof(*objid0);
if (memcpy(dst, objid1, objid1len*sizeof(*objid0)) < 0){
clicon_err(OE_UNIX, errno, "memcpy");
return -1;
}
*objid0len += objid1len;
return 0;
}
/*! Print objid to file
* @see fprint_objid but prints symbolic
*/
int
oid_cbuf(cbuf *cb,
const oid *objid,
size_t objidlen)
{
size_t i;
for (i=0; i<objidlen; i++)
cprintf(cb, ".%lu", objid[i]);
return 0;
}
int
oid_print(FILE *f,
const oid *objid,
size_t objidlen)
{
int retval = -1;
cbuf *cb;
if ((cb = cbuf_new()) == NULL){
clicon_err(OE_UNIX, errno, "cbuf_new");
goto done;
}
oid_cbuf(cb, objid, objidlen);
fprintf(stderr, "%s", cbuf_get(cb));
fprintf(stderr, "\n");
retval = 0;
done:
if (cb)
cbuf_free(cb);
return retval;
}
/*! Variant of yang_type_get that follows leafrefs
*/
int
snmp_yang_type_get(yang_stmt *ys,
yang_stmt **yrefp,
char **origtypep,
yang_stmt **yrestypep,
char **restypep)
{
int retval = -1;
yang_stmt *yrestype; /* resolved type */
char *restype; /* resolved type */
char *origtype = NULL; /* original type */
yang_stmt *ypath;
yang_stmt *yref = NULL;
/* Get yang type of leaf and trasnslate to ASN.1 */
if (yang_type_get(ys, &origtype, &yrestype, NULL, NULL, NULL, NULL, NULL) < 0)
goto done;
restype = yrestype?yang_argument_get(yrestype):NULL;
if (strcmp(restype, "leafref")==0){
if ((ypath = yang_find(yrestype, Y_PATH, NULL)) == NULL){
clicon_err(OE_YANG, 0, "No path in leafref");
goto done;
}
if (yang_path_arg(ys, yang_argument_get(ypath), &yref) < 0)
goto done;
if (yref == NULL){
clicon_err(OE_YANG, 0, "No referred YANG node found for leafref path %s", yang_argument_get(ypath));
goto done;
}
if (origtype){
free(origtype);
origtype = NULL;
}
if (yang_type_get(yref, &origtype, &yrestype, NULL, NULL, NULL, NULL, NULL) < 0)
goto done;
restype = yrestype?yang_argument_get(yrestype):NULL;
}
if (yrefp){
if (yref)
*yrefp = yref;
else
*yrefp = ys;
}
if (origtypep){
*origtypep = origtype;
origtype = NULL;
}
if (yrestypep)
*yrestypep = yrestype;
if (restypep)
*restypep = restype;
retval = 0;
done:
if (origtype)
free(origtype);
return retval;
}
/*! Given a YANG node, return SMIv2 oid extension as OID
* @param[in] yn Yang node
* @param[out] objid OID vector, assume allocated with MAX_OID_LEN > oidlen
* @param[out] objidlen Length of OID vector on return
* @param[out] objidstrp Pointer to string (direct not malloced) optional
* @retval 1 OK
* @retval 0 Invalid, not found
* @retval -1 Error
*/
int
yangext_oid_get(yang_stmt *yn,
oid *objid,
size_t *objidlen,
char **objidstrp)
{
int retval = -1;
int exist = 0;
char *oidstr = NULL;
yang_stmt *yref = NULL;
if (yang_keyword_get(yn) == Y_LEAF){
if (snmp_yang_type_get(yn, &yref, NULL, NULL, NULL) < 0)
goto done;
}
else
yref = yn;
/* Get OID from table /list */
if (yang_extension_value(yref, "oid", IETF_YANG_SMIV2_NS, &exist, &oidstr) < 0)
goto done;
if (exist == 0 || oidstr == NULL){
clicon_debug(1, "OID not found as SMIv2 yang extension of %s", yang_argument_get(yref));
goto fail;
}
if (snmp_parse_oid(oidstr, objid, objidlen) == NULL){
clicon_err(OE_XML, errno, "snmp_parse_oid");
goto done;
}
if (objidstrp)
*objidstrp = oidstr;
retval = 1;
done:
return retval;
fail:
retval = 0;
goto done;
}
/*! Duplicate clixon snmp handler struct
* Use signature of libnetsnmp data_clone field of netsnmp_mib_handler in agent_handler.h
* @param[in] arg
*/
void*
snmp_handle_clone(void *arg)
{
clixon_snmp_handle *sh0 = (clixon_snmp_handle *)arg;
clixon_snmp_handle *sh1 = NULL;
if (sh0 == NULL)
return NULL;
if ((sh1 = malloc(sizeof(*sh1))) == NULL){
clicon_err(OE_UNIX, errno, "malloc");
return NULL;
}
memset(sh1, 0, sizeof(*sh1));
if (sh0->sh_cvk_orig &&
(sh1->sh_cvk_orig = cvec_dup(sh0->sh_cvk_orig)) == NULL){
clicon_err(OE_UNIX, errno, "cvec_dup");
return NULL;
}
return (void*)sh1;
}
/*! Free clixon snmp handler struct
* Use signature of libnetsnmp data_free field of netsnmp_mib_handler in agent_handler.h
* @param[in] arg
*/
void
snmp_handle_free(void *arg)
{
clixon_snmp_handle *sh = (clixon_snmp_handle *)arg;
if (sh != NULL){
if (sh->sh_cvk_orig)
cvec_free(sh->sh_cvk_orig);
if (sh->sh_table_info){
if (sh->sh_table_info->indexes){
snmp_free_varbind(sh->sh_table_info->indexes);
}
free(sh->sh_table_info);
}
free(sh);
}
}
/*! Translate from YANG to SNMP asn1.1 type ids (not value)
*
* @param[in] ys YANG leaf node
* @param[out] asn1_type ASN.1 type id
* @param[in] extended Special case clixon extended types used in xml<->asn1 data conversions
* @retval 0 OK
* @retval -1 Error
* @see type_yang2snmp, yang only
* @note there are some special cases where extended clixon asn1-types are used to convey info
* to type_snmpstr2val, these types are prefixed with CLIXON_ASN_
*/
int
type_yang2asn1(yang_stmt *ys,
int *asn1_type,
int extended)
{
int retval = -1;
char *restype; /* resolved type */
char *origtype = NULL; /* original type */
int at;
yang_stmt *yrestype = NULL;
/* Get yang type of leaf and translate to ASN.1 */
if (snmp_yang_type_get(ys, NULL, &origtype, &yrestype, &restype) < 0)
goto done;
/* Translate to asn.1
* First try original type, first type
*/
if ((at = clicon_str2int(snmp_orig_map, origtype)) >= 0 &&
(extended || (at < CLIXON_ASN_EXTRAS))){
;
}
/* Then try fully resolved type */
else if ((at = clicon_str2int(snmp_type_map, restype)) < 0){
clicon_err(OE_YANG, 0, "No snmp translation for YANG %s type:%s",
yang_argument_get(ys), restype);
goto done;
}
if (extended && at == ASN_OCTET_STR && yrestype){
yang_stmt *yrp;
char *display_hint = NULL;
yrp = yang_parent_get(yrestype);
if (yang_extension_value(yrp, "display-hint", IETF_YANG_SMIV2_NS, NULL, &display_hint) < 0)
goto done;
/* RFC2578/2579 but maybe all strings with display-hint should use this, eg exist>0? */
if (display_hint &&
(strcmp(display_hint, "255a")==0 ||
strcmp(display_hint, "255t")==0))
at = CLIXON_ASN_FIXED_STRING;
}
if (asn1_type)
*asn1_type = at;
retval = 0;
done:
if (origtype)
free(origtype);
return retval;
}
/*! Translate from yang/xml/clixon to SNMP/ASN.1
*
* @param[in] snmpval Malloc:ed snmp type
* @param[in] snmplen Length of snmp type
* @param[in] reqinfo Agent transaction request structure
* @param[in] request The netsnmp request info structure.
* @param[out] valstr Clixon/yang/xml string value, free after use)
* @retval 1 OK, and valstr set
* @retval 0 Invalid value or type
* @retval -1 Error
* @see type_xml2snmp for snmpget
*/
int
type_snmp2xml(yang_stmt *ys,
int *asn1type,
netsnmp_variable_list *requestvb,
netsnmp_agent_request_info *reqinfo,
netsnmp_request_info *request,
char **valstr)
{
int retval = -1;
char *cvstr;
enum cv_type cvtype;
cg_var *cv = NULL;
char *restype = NULL; /* resolved type */
char *origtype = NULL; /* original type */
yang_stmt *yrestype = NULL;
int ret;
clicon_debug(1, "%s", __FUNCTION__);
if (valstr == NULL){
clicon_err(OE_UNIX, EINVAL, "valstr is NULL");
goto done;
}
if ((cvstr = (char*)clicon_int2str(snmp_type_map, requestvb->type)) == NULL){
clicon_err(OE_XML, 0, "No mapping for snmp type %d", requestvb->type);
goto done;
}
/* Get yang type of leaf and trasnslate to ASN.1 */
if (snmp_yang_type_get(ys, NULL, &origtype, &yrestype, &restype) < 0)
goto done;
/* special case for enum */
if (strcmp(cvstr, "int32")==0 && strcmp(restype, "enumeration") == 0)
cvstr = "string";
else if (strcmp(cvstr, "int32")==0 && strcmp(restype, "boolean") == 0)
cvstr = "string";
cvtype = cv_str2type(cvstr);
if ((cv = cv_new(cvtype)) == NULL){
clicon_err(OE_UNIX, errno, "cv_new");
goto done;
}
switch (*asn1type){
case CLIXON_ASN_ROWSTATUS:
*asn1type = ASN_INTEGER;
/* fall through */
case ASN_TIMETICKS: // 67
case ASN_INTEGER: // 2
if (cvtype == CGV_STRING){ /* special case for enum */
char *xmlstr;
cbuf *cb = NULL;
if (strcmp(restype, "enumeration") == 0){
if ((cb = cbuf_new()) == NULL){
clicon_err(OE_UNIX, errno, "cbuf_new");
goto done;
}
cprintf(cb, "%ld", *requestvb->val.integer);
if (yang_valstr2enum(yrestype, cbuf_get(cb), &xmlstr) < 0)
goto done;
cbuf_free(cb);
}
else if (strcmp(restype, "boolean") == 0){
if (*requestvb->val.integer == 1)
xmlstr = "true";
else
xmlstr = "false";
}
cv_string_set(cv, xmlstr);
}
else
cv_int32_set(cv, *requestvb->val.integer);
break;
case ASN_GAUGE: // 0x42
cv_uint32_set(cv, *requestvb->val.integer);
break;
case ASN_IPADDRESS:{
struct in_addr addr;
memcpy(&addr.s_addr, requestvb->val.string, 4);
cv_string_set(cv, inet_ntoa(addr));
break;
}
case CLIXON_ASN_FIXED_STRING:
cv_string_set(cv, (char*)requestvb->val.string);
*asn1type = ASN_OCTET_STR;
break;
case CLIXON_ASN_PHYS_ADDR:
cv_string_set(cv, ether_ntoa((const struct ether_addr *)requestvb->val.string));
*asn1type = ASN_OCTET_STR;
break;
case ASN_OCTET_STR: // 4
cv_string_set(cv, (char*)requestvb->val.string);
break;
case ASN_COUNTER64:{ // 0x46 / 70
uint64_t u64;
struct counter64 *c64;
c64 = requestvb->val.counter64;
u64 = c64->low;
u64 += c64->high*0x100000000;
cv_uint64_set(cv, u64);
break;
}
default:
assert(0); // XXX
clicon_debug(1, "%s %s not supported", __FUNCTION__, cv_type2str(cvtype));
if ((ret = netsnmp_request_set_error(request, SNMP_ERR_WRONGTYPE)) != SNMPERR_SUCCESS){
clicon_err(OE_SNMP, ret, "netsnmp_request_set_error");
goto done;
}
goto fail;
break;
}
if ((*valstr = cv2str_dup(cv)) == NULL){
clicon_err(OE_UNIX, errno, "cv2str_dup");
goto done;
}
retval = 1;
done:
clicon_debug(2, "%s %d", __FUNCTION__, retval);
if (origtype)
free(origtype);
if (cv)
cv_free(cv);
return retval;
fail:
retval = 0;
goto done;
}
/*! Given xml value and YANG,m return corresponding malloced snmp string
*
* For special cases to prepare for proper xml2snmp translation. This includes translating
* from string values to numeric values for enumeration and boolean.
* @param[in] xmlstr0 XML string pre
* @param[in] ys Yang node
* @param[out] xmlstr1 XML string ready for translation
* @retval 1 OK
* @retval 0 Invalid type
* @retval -1 Error
* @see type_snmp2xml for snmpset
*/
int
type_xml2snmp_pre(char *xmlstr0,
yang_stmt *ys,
char **xmlstr1)
{
int retval = -1;
yang_stmt *yrestype; /* resolved type */
char *restype = NULL; /* resolved type */
char *str = NULL;
int ret;
if (xmlstr0 == NULL || xmlstr1 == NULL){
clicon_err(OE_UNIX, EINVAL, "xmlstr0/1 is NULL");
goto done;
}
/* Get yang type of leaf and trasnslate to ASN.1 */
if (snmp_yang_type_get(ys, NULL, NULL, &yrestype, &restype) < 0) // XXX yrestype
goto done;
if (strcmp(restype, "enumeration") == 0){ /* special case for enum */
if ((ret = yang_enum2valstr(yrestype, xmlstr0, &str)) < 0)
goto done;
if (ret == 0){
clicon_debug(1, "Invalid enum valstr %s", xmlstr0);
goto fail;
}
}
/* special case for bool: although smidump translates TruthValue to boolean
* and there is an ASN_BOOLEAN constant:
* 1) there is no code for ASN_BOOLEAN and
* 2) Truthvalue actually translates to enum true(1)/false(0)
*/
else if (strcmp(restype, "boolean") == 0){
if (strcmp(xmlstr0, "false")==0)
str = "0";
else
str = "1";
}
else{
str = xmlstr0;
}
if ((*xmlstr1 = strdup(str)) == NULL){
clicon_err(OE_UNIX, errno, "strdup");
goto done;
}
retval = 1;
done:
clicon_debug(2, "%s %d", __FUNCTION__, retval);
return retval;
fail:
retval = 0;
goto done;
}
/*! Given snmp string value (as translated frm XML) parse into snmp value
*
* @param[in] snmpstr SNMP type string
* @param[in,out] asn1type ASN.1 type id
* @param[out] snmpval Malloc:ed snmp type
* @param[out] snmplen Length of snmp type
* @param[out] reason Error reason if retval is 0
* @retval 1 OK
* @retval 0 Invalid
* @retval -1 Error
* @note asn1type can be rewritten from CLIXON_ASN_ to ASN_
* @see type_xml2snmp_pre for some pre-condition XML special cases (eg enums and bool)
*/
int
type_xml2snmp(char *snmpstr,
int *asn1type,
u_char **snmpval,
size_t *snmplen,
char **reason)
{
int retval = -1;
int ret;
if (snmpval == NULL || snmplen == NULL){
clicon_err(OE_UNIX, EINVAL, "snmpval or snmplen is NULL");
goto done;
}
switch (*asn1type){
case CLIXON_ASN_ROWSTATUS:
*asn1type = ASN_INTEGER;
/* fall through */
case ASN_INTEGER: // 2
*snmplen = 4;
if ((*snmpval = malloc(*snmplen)) == NULL){
clicon_err(OE_UNIX, errno, "malloc");
goto done;
}
if ((ret = parse_int32(snmpstr, (int32_t*)*snmpval, reason)) < 0)
goto done;
if (ret == 0)
goto fail;
break;
case ASN_TIMETICKS:
case ASN_COUNTER: // 0x41
case ASN_GAUGE: // 0x42
*snmplen = 4;
if ((*snmpval = malloc(*snmplen)) == NULL){
clicon_err(OE_UNIX, errno, "malloc");
goto done;
}
if ((ret = parse_uint32(snmpstr, (uint32_t*)*snmpval, reason)) < 0)
goto done;
if (ret == 0)
goto fail;
break;
case ASN_OBJECT_ID:{ // 6
oid oid1[MAX_OID_LEN] = {0,};
size_t sz1 = MAX_OID_LEN;
if (snmp_parse_oid(snmpstr, oid1, &sz1) == NULL){
clicon_debug(1, "Failed to parse OID %s", snmpstr);
goto fail;
}
*snmplen = sizeof(oid)*sz1;
if ((*snmpval = malloc(*snmplen)) == NULL){
clicon_err(OE_UNIX, errno, "malloc");
goto done;
}
memcpy(*snmpval, oid1, *snmplen);
break;
}
case ASN_OCTET_STR: // 4
*snmplen = strlen(snmpstr)+1;
if ((*snmpval = (u_char*)strdup((snmpstr))) == NULL){
clicon_err(OE_UNIX, errno, "strdup");
goto done;
}
break;
case ASN_COUNTER64:{ // 0x46 / 70
uint64_t u64;
struct counter64 *c64;
*snmplen = sizeof(struct counter64); // 16!
if ((*snmpval = malloc(*snmplen)) == NULL){
clicon_err(OE_UNIX, errno, "malloc");
goto done;
}
memset(*snmpval, 0, *snmplen);
if ((ret = parse_uint64(snmpstr, &u64, reason)) < 0)
goto done;
c64 = (struct counter64 *)*snmpval;
c64->low = u64&0xffffffff;
c64->high = u64/0x100000000;
if (ret == 0)
goto fail;
}
break;
case ASN_IPADDRESS:{
in_addr_t saddr;
*snmplen = 4;
if ((*snmpval = malloc(*snmplen)) == NULL){
clicon_err(OE_UNIX, errno, "malloc");
goto done;
}
saddr = (int32_t)inet_addr(snmpstr);
memcpy(*snmpval, &saddr, 4);
break;
}
case CLIXON_ASN_PHYS_ADDR:{
struct ether_addr *eaddr;
*snmplen = sizeof(*eaddr);
if ((*snmpval = malloc(*snmplen + 1)) == NULL){
clicon_err(OE_UNIX, errno, "malloc");
goto done;
}
memset(*snmpval, 0, *snmplen + 1);
if ((eaddr = ether_aton(snmpstr)) == NULL){
clicon_debug(1, "ether_aton(%s)", snmpstr);
goto fail;
}
memcpy(*snmpval, eaddr, sizeof(*eaddr));
*asn1type = ASN_OCTET_STR;
break;
}
case CLIXON_ASN_FIXED_STRING: /* OCTET-STRING with decrement length */
*snmplen = strlen(snmpstr);
if ((*snmpval = (u_char*)strdup((snmpstr))) == NULL){
clicon_err(OE_UNIX, errno, "strdup");
goto done;
}
*asn1type = ASN_OCTET_STR;
break;
default:
assert(0);
}
retval = 1;
done:
clicon_debug(2, "%s %d", __FUNCTION__, retval);
return retval;
fail:
retval = 0;
goto done;
}
/*! Construct an xpath from yang statement, internal fn using cb
* Recursively construct it to the top.
* @param[in] ys Yang statement
* @param[in] keyvec Cvec of key values
* @param[out] cb xpath as cbuf
* @retval 0 OK
* @retval -1 Error
* @see yang2xpath
*/
static int
snmp_yang2xpath_cb(yang_stmt *ys,
cvec *keyvec,
cbuf *cb)
{
yang_stmt *yp; /* parent */
int i;
cvec *cvk = NULL; /* vector of index keys */
int retval = -1;
char *prefix = NULL;
if ((yp = yang_parent_get(ys)) == NULL){
clicon_err(OE_YANG, EINVAL, "yang expected parent %s", yang_argument_get(ys));
goto done;
}
if (yp != NULL && /* XXX rm */
yang_keyword_get(yp) != Y_MODULE &&
yang_keyword_get(yp) != Y_SUBMODULE){
if (snmp_yang2xpath_cb(yp, keyvec, cb) < 0) /* recursive call */
goto done;
if (yang_keyword_get(yp) != Y_CHOICE && yang_keyword_get(yp) != Y_CASE){
cprintf(cb, "/");
}
}
prefix = yang_find_myprefix(ys);
if (yang_keyword_get(ys) != Y_CHOICE && yang_keyword_get(ys) != Y_CASE){
if (prefix)
cprintf(cb, "%s:", prefix);
cprintf(cb, "%s", yang_argument_get(ys));
}
switch (yang_keyword_get(ys)){
case Y_LIST:
cvk = yang_cvec_get(ys); /* Use Y_LIST cache, see ys_populate_list() */
/* Iterate over individual keys */
assert(keyvec && cvec_len(cvk) == cvec_len(keyvec));
for (i=0; i<cvec_len(cvk); i++){
cprintf(cb, "[");
if (prefix)
cprintf(cb, "%s:", prefix);
cprintf(cb, "%s='%s']",
cv_string_get(cvec_i(cvk, i)),
cv_string_get(cvec_i(keyvec, i)));
}
break;
case Y_LEAF_LIST:
assert(0); // NYI
break;
default:
break;
} /* switch */
retval = 0;
done:
return retval;
}
/*! Construct an xpath from yang statement, limited to SNMP table translations
* Recursively construct it to the top.
* @param[in] ys Yang statement
* @param[in] keyvec Cvec of key values
* @param[out] xpath Malloced xpath string, use free() after use
* @retval 0 OK
* @retval -1 Error
* @note
* 1. This should really be in a core .c file, like clixon_yang, BUT
* 2. It is far from complete so maybe keep it here as a special case
*/
int
snmp_yang2xpath(yang_stmt *ys,
cvec *keyvec,
char **xpath)
{
int retval = -1;
cbuf *cb = NULL;
if ((cb = cbuf_new()) == NULL){
clicon_err(OE_UNIX, errno, "cbuf_new");
goto done;
}
if (snmp_yang2xpath_cb(ys, keyvec, cb) < 0)
goto done;
if (xpath && (*xpath = strdup(cbuf_get(cb))) == NULL){
clicon_err(OE_UNIX, errno, "strdup");
goto done;
}
retval = 0;
done:
if (cb)
cbuf_free(cb);
return retval;
}
/*! Translate from xml body string to SMI OID representation
* For ints this is one to one, eg 42 -> 42
* But for eg strings this is more complex, eg foo -> 3.6.22.22 (or something,...)
* @param[in] str XML body string
* @param[in] yi Yang statement
* @param[out] objid OID vector
* @param[out] objidlen Length of OID vector
*/
int
snmp_str2oid(char *str,
yang_stmt *yi,
oid *objid,
size_t *objidlen)
{
int retval = -1;
int asn1_type;
int i;
int j = 0;
if (type_yang2asn1(yi, &asn1_type, 0) < 0)
goto done;
switch (asn1_type){
case ASN_INTEGER:
case ASN_GAUGE:
case ASN_TIMETICKS:
case ASN_COUNTER64:
case ASN_COUNTER:
case ASN_IPADDRESS:
objid[j++] = atoi(str);
break;
case ASN_OCTET_STR:{ /* encode to N.c.c.c.c */
objid[j++] = strlen(str);
for (i=0; i<strlen(str); i++)
objid[j++] = str[i]&0xff;
break;
}
default:
break;
}
*objidlen = j;
// ok:
retval = 0;
done:
return retval;
}
/*! Translate from SMI OID representation to name
* For ints this is one to one, eg 42 -> 42
* But for eg strings this is more complex, eg foo -> 3.6.22.22 (or something,...)
* @param[in,out] oidi ObjID vector
* @param[in,out] oidilen Length of ObjID vector
* @param[in] yk Yang statement of key
* @param[out] cv CLIgen variable string notation as "x.y.z"
* @see rfc2578 Section 7.7
*/
int
snmp_oid2str(oid **oidi,
size_t *oidilen,
yang_stmt *yk,
cg_var *cv)
{
int retval = -1;
int asn1_type;
int i = 0;
cbuf *enc = NULL;
size_t len;
if (type_yang2asn1(yk, &asn1_type, 1) < 0)
goto done;
if ((enc = cbuf_new()) == NULL){
clicon_err(OE_UNIX, errno, "cbuf_new");
goto done;
}
switch (asn1_type){
case ASN_INTEGER:
case ASN_GAUGE:
case ASN_TIMETICKS:
case ASN_COUNTER64:
case ASN_COUNTER:
case ASN_IPADDRESS: