This repository was archived by the owner on Mar 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsmi.c
711 lines (580 loc) · 19.1 KB
/
smi.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
/*
* ProFTPD - mod_snmp SMI routines
* Copyright (c) 2008-2012 TJ Saunders
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
*
* As a special exemption, TJ Saunders and other respective copyright holders
* give permission to link this program with OpenSSL, and distribute the
* resulting executable, without including the source code for OpenSSL in the
* source distribution.
*
* $Id$
*/
#include "mod_snmp.h"
#include "asn1.h"
#include "smi.h"
#include "mib.h"
#include "msg.h"
#include "stacktrace.h"
static const char *trace_channel = "snmp.smi";
const char *snmp_smi_get_varstr(pool *p, unsigned char var_type) {
const char *varstr = "unknown";
switch (var_type) {
case SNMP_SMI_INTEGER:
varstr = "INTEGER";
break;
case SNMP_SMI_STRING:
varstr = "STRING";
break;
case SNMP_SMI_OID:
varstr = "OID";
break;
case SNMP_SMI_NULL:
varstr = "NULL";
break;
case SNMP_SMI_IPADDR:
varstr = "IPADDR";
break;
case SNMP_SMI_COUNTER32:
varstr = "COUNTER32";
break;
case SNMP_SMI_GAUGE32:
varstr = "GAUGE32";
break;
case SNMP_SMI_TIMETICKS:
varstr = "TIMETICKS";
break;
case SNMP_SMI_OPAQUE:
varstr = "OPAQUE";
break;
case SNMP_SMI_COUNTER64:
varstr = "COUNTER64";
break;
case SNMP_SMI_NO_SUCH_OBJECT:
varstr = "NO_SUCH_OBJECT";
break;
case SNMP_SMI_NO_SUCH_INSTANCE:
varstr = "NO_SUCH_INSTANCE";
break;
case SNMP_SMI_END_OF_MIB_VIEW:
varstr = "END_OF_MIB_VIEW";
break;
}
return varstr;
}
struct snmp_var *snmp_smi_alloc_var(pool *p, oid_t *name,
unsigned int namelen) {
pool *sub_pool;
struct snmp_var *var;
sub_pool = pr_pool_create_sz(p, 64);
var = pcalloc(sub_pool, sizeof(struct snmp_var));
var->pool = sub_pool;
var->next = NULL;
/* Default type for newly-allocated variables. */
var->smi_type = SNMP_SMI_NULL;
var->namelen = namelen;
if (var->namelen == 0) {
/* Not sure why a caller would do this, but... */
return var;
}
/* Even though the name argument may be NULL, we still allocate the space.
* Why? Because when reading off variables from a message, we may not
* know the name when we are allocating the struct, but we will know at
* some point after that.
*/
var->name = pcalloc(var->pool, sizeof(oid_t) * var->namelen);
if (name != NULL) {
memmove(var->name, name, sizeof(oid_t) * var->namelen);
}
return var;
}
struct snmp_var *snmp_smi_create_var(pool *p, oid_t *name, unsigned int namelen,
unsigned char smi_type, int32_t int_value, char *str_value,
size_t str_valuelen) {
struct snmp_var *var = NULL;
switch (smi_type) {
case SNMP_SMI_INTEGER:
case SNMP_SMI_COUNTER32:
case SNMP_SMI_GAUGE32:
case SNMP_SMI_TIMETICKS:
var = snmp_smi_create_int(p, name, namelen, smi_type, int_value);
break;
case SNMP_SMI_STRING:
case SNMP_SMI_IPADDR:
var = snmp_smi_create_string(p, name, namelen, smi_type, str_value,
str_valuelen);
break;
default:
pr_trace_msg(trace_channel, 16,
"unable to create variable for SMI type %s",
snmp_smi_get_varstr(p, smi_type));
errno = ENOENT;
break;
}
return var;
}
struct snmp_var *snmp_smi_create_int(pool *p, oid_t *name, unsigned int namelen,
unsigned char smi_type, int32_t value) {
struct snmp_var *var;
var = snmp_smi_alloc_var(p, name, namelen);
var->valuelen = sizeof(value);
var->value.integer = palloc(var->pool, var->valuelen);
*(var->value.integer) = value;
var->smi_type = smi_type;
pr_trace_msg(trace_channel, 19,
"created SMI variable %s, value %d", snmp_smi_get_varstr(p, smi_type),
value);
return var;
}
struct snmp_var *snmp_smi_create_string(pool *p, oid_t *name,
unsigned int namelen, unsigned char smi_type, char *value,
size_t valuelen) {
struct snmp_var *var;
if (value == NULL) {
errno = EINVAL;
return NULL;
}
var = snmp_smi_alloc_var(p, name, namelen);
var->valuelen = valuelen;
var->value.string = pstrndup(var->pool, value, var->valuelen);
var->smi_type = smi_type;
pr_trace_msg(trace_channel, 19,
"created SMI variable %s, value '%s'", snmp_smi_get_varstr(p, smi_type),
value);
return var;
}
struct snmp_var *snmp_smi_create_oid(pool *p, oid_t *name,
unsigned int namelen, unsigned char smi_type, oid_t *value,
unsigned int valuelen) {
struct snmp_var *var;
if (value == NULL) {
errno = EINVAL;
return NULL;
}
if (smi_type != SNMP_SMI_OID) {
errno = EINVAL;
return NULL;
}
var = snmp_smi_alloc_var(p, name, namelen);
/* The valuelen argument is the number of sub-ids, NOT the number of bytes,
* for an OID.
*/
var->valuelen = valuelen;
var->value.oid = palloc(var->pool, sizeof(oid_t) * var->valuelen);
memmove(var->value.oid, value, sizeof(oid_t) * var->valuelen);
var->smi_type = smi_type;
pr_trace_msg(trace_channel, 19,
"created SMI variable %s, value %s", snmp_smi_get_varstr(p, smi_type),
snmp_asn1_get_oidstr(p, value, valuelen));
return var;
}
struct snmp_var *snmp_smi_create_exception(pool *p, oid_t *name,
unsigned int namelen, unsigned char smi_type) {
struct snmp_var *var;
/* Check that the SMI type is one of the allowed "exceptions"
* (terminology from RFC 1905).
*/
switch (smi_type) {
case SNMP_SMI_NO_SUCH_OBJECT:
case SNMP_SMI_NO_SUCH_INSTANCE:
case SNMP_SMI_END_OF_MIB_VIEW:
break;
default:
errno = EINVAL;
return NULL;
}
var = snmp_smi_alloc_var(p, name, namelen);
var->valuelen = 0;
var->smi_type = smi_type;
pr_trace_msg(trace_channel, 19,
"created SMI variable %s", snmp_smi_get_varstr(p, smi_type));
return var;
}
/* Note: This will duplicate the entire varlist represented by the head
* variable.
*/
struct snmp_var *snmp_smi_dup_var(pool *p, struct snmp_var *src_var) {
struct snmp_var *head_var = NULL, *iter_var = NULL, *tail_var = NULL;
unsigned int var_count = 0;
for (iter_var = src_var; iter_var; iter_var = iter_var->next) {
struct snmp_var *var;
pr_signals_handle();
var = snmp_smi_alloc_var(p, iter_var->name, iter_var->namelen);
var->smi_type = iter_var->smi_type;
var->valuelen = iter_var->valuelen;
if (var->valuelen > 0) {
switch (var->smi_type) {
case SNMP_SMI_INTEGER:
var->value.integer = palloc(var->pool, var->valuelen);
memmove(var->value.integer, iter_var->value.integer, var->valuelen);
break;
case SNMP_SMI_STRING:
var->value.string = pcalloc(var->pool, var->valuelen);
memmove(var->value.string, iter_var->value.string, var->valuelen);
break;
case SNMP_SMI_OID:
var->value.oid = palloc(var->pool, var->valuelen);
memmove(var->value.oid, iter_var->value.oid, var->valuelen);
break;
default:
pr_trace_msg(trace_channel, 1,
"unable to dup variable '%s': unsupported",
snmp_asn1_get_tagstr(p, var->smi_type));
/* XXX Destroy the entire chain? */
destroy_pool(var->pool);
snmp_stacktrace_log();
errno = EINVAL;
return NULL;
}
}
if (head_var == NULL) {
head_var = var;
}
if (tail_var != NULL) {
tail_var->next = var;
}
tail_var = var;
var_count++;
pr_trace_msg(trace_channel, 19,
"cloned SMI variable %s", snmp_smi_get_varstr(p, iter_var->smi_type));
}
pr_trace_msg(trace_channel, 19, "cloned %u SMI %s", var_count,
var_count != 1 ? "variables" : "variable");
return head_var;
}
/* Decode a list of SNMPv2 variable bindings. */
int snmp_smi_read_vars(pool *p, unsigned char **buf, size_t *buflen,
struct snmp_var **varlist, int snmp_version) {
struct snmp_var *var = NULL, *head = NULL, *tail = NULL;
unsigned char asn1_type;
unsigned int total_varlen = 0;
int res, var_count = 0;
res = snmp_asn1_read_header(p, buf, buflen, &asn1_type, &total_varlen, 0);
if (res < 0) {
return -1;
}
/* If this isn't a constructed sequence, error out. */
if (asn1_type != (SNMP_ASN1_TYPE_SEQUENCE|SNMP_ASN1_CONSTRUCT)) {
pr_trace_msg(trace_channel, 1,
"unable to parse tag (%s) as list of variables",
snmp_asn1_get_tagstr(p, asn1_type));
snmp_stacktrace_log();
errno = EINVAL;
return -1;
}
pr_trace_msg(trace_channel, 17, "reading %s variables (%u bytes)",
snmp_msg_get_versionstr(snmp_version), total_varlen);
while (*buflen > 0) {
unsigned int varlen;
unsigned char *hdr_start = NULL, *hdr_end = NULL, *obj_start = NULL;
size_t obj_startlen = 0;
pr_signals_handle();
/* Keep track of the buffer, so that we know where the start/end of the
* header is. Which then lets us know how many bytes the header is,
* and how that relates to the total varlist length.
*/
hdr_start = *buf;
res = snmp_asn1_read_header(p, buf, buflen, &asn1_type, &varlen, 0);
if (res < 0) {
return -1;
}
hdr_end = *buf;
/* If this isn't a constructed sequence, error out. */
if (asn1_type != (SNMP_ASN1_TYPE_SEQUENCE|SNMP_ASN1_CONSTRUCT)) {
pr_trace_msg(trace_channel, 1,
"unable to parse tag (%s) as variable binding",
snmp_asn1_get_tagstr(p, asn1_type));
snmp_stacktrace_log();
errno = EINVAL;
return -1;
}
/* We don't know the name of this variable yet. */
var = snmp_smi_alloc_var(p, NULL, SNMP_SMI_MAX_NAMELEN);
/* Read the variable name/OID. */
res = snmp_asn1_read_oid(p, buf, buflen, &asn1_type, var->name,
&(var->namelen));
if (res < 0) {
destroy_pool(var->pool);
return -1;
}
if (asn1_type != (SNMP_ASN1_CLASS_UNIVERSAL|SNMP_ASN1_PRIMITIVE|SNMP_ASN1_TYPE_OID)) {
pr_trace_msg(trace_channel, 1,
"expected OID tag, read tag (%s) from variable list",
snmp_asn1_get_tagstr(p, asn1_type));
destroy_pool(var->pool);
snmp_stacktrace_log();
errno = EINVAL;
return -1;
}
if (pr_trace_get_level(trace_channel) >= 19) {
struct snmp_mib *mib;
int lacks_instance_id = FALSE;
mib = snmp_mib_get_by_oid(var->name, var->namelen, &lacks_instance_id);
if (mib != NULL) {
pr_trace_msg(trace_channel, 19,
"read variable OID %s (%u sub-ids, name %s)",
snmp_asn1_get_oidstr(p, var->name, var->namelen), var->namelen,
mib->instance_name);
} else {
pr_trace_msg(trace_channel, 19,
"read variable OID %s (%u sub-ids)",
snmp_asn1_get_oidstr(p, var->name, var->namelen), var->namelen);
}
}
obj_start = *buf;
obj_startlen = *buflen;
/* Now that we know the name/OID of the variable, let's find out what
* type of variable it is.
*
* This is effectively a peek, since the following reader functions
* will also want to read the tag/length header values.
*/
res = snmp_asn1_read_header(p, &obj_start, &obj_startlen, &(var->smi_type),
&(var->valuelen), 0);
if (res < 0) {
destroy_pool(var->pool);
return -1;
}
pr_trace_msg(trace_channel, 19,
"read SMI variable %s, data len %u bytes",
snmp_smi_get_varstr(p, var->smi_type), var->valuelen);
/* Now read in the value */
switch (var->smi_type) {
case SNMP_SMI_INTEGER:
res = snmp_asn1_read_int(p, buf, buflen,
&(var->smi_type), var->value.integer, 0);
if (res == 0) {
pr_trace_msg(trace_channel, 19,
"read INTEGER variable (value %d)", *((int *) var->value.integer));
}
break;
case SNMP_SMI_COUNTER32:
case SNMP_SMI_GAUGE32:
case SNMP_SMI_TIMETICKS:
res = snmp_asn1_read_uint(p, buf, buflen,
&(var->smi_type), (unsigned long *) var->value.integer);
if (res == 0) {
pr_trace_msg(trace_channel, 19,
"read %s variable (value %u)",
snmp_smi_get_varstr(p, var->smi_type),
*((unsigned int *) var->value.integer));
}
break;
case SNMP_SMI_STRING:
case SNMP_SMI_IPADDR:
case SNMP_SMI_OPAQUE:
res = snmp_asn1_read_string(p, buf, buflen,
&(var->smi_type), &(var->value.string), &(var->valuelen));
if (res == 0) {
pr_trace_msg(trace_channel, 19,
"read %s variable (value '%.*s')",
snmp_smi_get_varstr(p, var->smi_type),
var->valuelen, var->value.string);
}
break;
case SNMP_SMI_OID:
res = snmp_asn1_read_oid(p, buf, buflen,
&(var->smi_type), var->value.oid, &(var->valuelen));
if (res == 0) {
pr_trace_msg(trace_channel, 19,
"read %s variable (%u sub-ids, value %s)",
snmp_smi_get_varstr(p, var->smi_type), var->valuelen,
snmp_asn1_get_oidstr(p, var->value.oid, var->valuelen));
}
break;
case SNMP_SMI_NULL:
res = snmp_asn1_read_null(p, buf, buflen, &(var->smi_type));
if (res == 0) {
pr_trace_msg(trace_channel, 19, "read %s variable",
snmp_smi_get_varstr(p, var->smi_type));
}
break;
case SNMP_SMI_NO_SUCH_OBJECT:
case SNMP_SMI_NO_SUCH_INSTANCE:
case SNMP_SMI_END_OF_MIB_VIEW:
pr_trace_msg(trace_channel, 19, "read %s variable",
snmp_smi_get_varstr(p, var->smi_type));
break;
case SNMP_SMI_COUNTER64:
pr_trace_msg(trace_channel, 1,
"unable to handle COUNTER64 variable (%x)", var->smi_type);
/* fallthrough */
default:
pr_trace_msg(trace_channel, 1,
"unable to read variable type %x", var->smi_type);
destroy_pool(var->pool);
snmp_stacktrace_log();
errno = EINVAL;
return -1;
}
if (res < 0) {
return -1;
}
/* Add the variable to the end of the list. */
if (tail != NULL) {
tail->next = var;
tail = var;
} else {
head = tail = var;
}
var_count++;
}
*varlist = head;
return var_count;
}
/* Encode an SNMPv2 variable binding.
*
* As per RFC 1905 Protocol Operations for SNMPv2:
*
* VarBind ::=
* SEQUENCE {
* name ObjectName
* CHOICE {
* value ObjectSyntax
* unSpecified NULL
* noSuchObject[0] NULL
* noSuchInstance[1] NULL
* endOfMibView[2] NULL
* }
* }
*/
int snmp_smi_write_vars(pool *p, unsigned char **buf, size_t *buflen,
struct snmp_var *varlist, int snmp_version) {
struct snmp_var *iter;
unsigned char asn1_type, *list_hdr_start, *list_hdr_end;
size_t list_hdr_startlen;
unsigned int asn1_len;
int res;
/* Write the header for the varlist. */
asn1_type = (SNMP_ASN1_TYPE_SEQUENCE|SNMP_ASN1_CONSTRUCT);
asn1_len = 0;
list_hdr_start = *buf;
list_hdr_startlen = *buflen;
res = snmp_asn1_write_header(p, buf, buflen, asn1_type, asn1_len, 0);
if (res < 0) {
return -1;
}
list_hdr_end = *buf;
for (iter = varlist; iter; iter = iter->next) {
unsigned char *var_hdr_start = NULL, *var_hdr_end = NULL;
size_t var_hdr_startlen;
pr_signals_handle();
/* Write the header for this varible. */
asn1_type = (SNMP_ASN1_TYPE_SEQUENCE|SNMP_ASN1_CONSTRUCT);
asn1_len = 0;
var_hdr_start = *buf;
var_hdr_startlen = *buflen;
res = snmp_asn1_write_header(p, buf, buflen, asn1_type, asn1_len, 0);
if (res < 0) {
return -1;
}
var_hdr_end = *buf;
asn1_type = (SNMP_ASN1_CLASS_UNIVERSAL|SNMP_ASN1_PRIMITIVE|SNMP_ASN1_TYPE_OID);
res = snmp_asn1_write_oid(p, buf, buflen, asn1_type, iter->name,
iter->namelen);
if (res < 0) {
return -1;
}
switch (iter->smi_type) {
case SNMP_SMI_INTEGER:
res = snmp_asn1_write_int(p, buf, buflen, iter->smi_type,
*((long *) iter->value.integer), 0);
break;
case SNMP_SMI_COUNTER32:
case SNMP_SMI_GAUGE32:
case SNMP_SMI_TIMETICKS:
res = snmp_asn1_write_uint(p, buf, buflen, iter->smi_type,
*((unsigned long *) iter->value.integer));
break;
case SNMP_SMI_STRING:
case SNMP_SMI_IPADDR:
case SNMP_SMI_OPAQUE:
res = snmp_asn1_write_string(p, buf, buflen, iter->smi_type,
iter->value.string, iter->valuelen);
break;
case SNMP_SMI_OID:
res = snmp_asn1_write_oid(p, buf, buflen, iter->smi_type,
iter->value.oid, iter->valuelen);
break;
case SNMP_SMI_NO_SUCH_OBJECT:
case SNMP_SMI_NO_SUCH_INSTANCE:
case SNMP_SMI_END_OF_MIB_VIEW:
if (snmp_version == SNMP_PROTOCOL_VERSION_1) {
/* SNMPv1 does not support the other error codes. */
res = snmp_asn1_write_null(p, buf, buflen, SNMP_SMI_NO_SUCH_OBJECT);
} else {
res = snmp_asn1_write_exception(p, buf, buflen, iter->smi_type, 0);
}
break;
case SNMP_SMI_NULL:
res = snmp_asn1_write_null(p, buf, buflen, iter->smi_type);
break;
case SNMP_SMI_COUNTER64:
pr_trace_msg(trace_channel, 1, "%s",
"unable to encode COUNTER64 SMI variable");
/* fall through */
default:
/* Unsupported type */
pr_trace_msg(trace_channel, 1, "%s",
"unable to encode unsupported SMI variable type");
snmp_stacktrace_log();
errno = ENOSYS;
return -1;
}
if (res < 0) {
return -1;
}
/* Rewrite the header, this time with the appropriate length. */
asn1_type = (SNMP_ASN1_TYPE_SEQUENCE|SNMP_ASN1_CONSTRUCT);
asn1_len = (*buf - var_hdr_end);
pr_trace_msg(trace_channel, 18,
"updating variable header to have length %u", asn1_len);
res = snmp_asn1_write_header(p, &var_hdr_start, &var_hdr_startlen,
asn1_type, asn1_len, 0);
if (res < 0) {
return -1;
}
}
/* Rewrite the varlist header, this time with the length of all of the
* variables.
*/
asn1_type = (SNMP_ASN1_TYPE_SEQUENCE|SNMP_ASN1_CONSTRUCT);
asn1_len = (*buf - list_hdr_end);
pr_trace_msg(trace_channel, 18,
"updating variable bindings list header to have length %u", asn1_len);
res = snmp_asn1_write_header(p, &list_hdr_start, &list_hdr_startlen,
asn1_type, asn1_len, 0);
if (res < 0) {
return -1;
}
return 0;
}
unsigned int snmp_smi_util_add_list_var(struct snmp_var **head,
struct snmp_var **tail, struct snmp_var *var) {
unsigned int count = 0;
struct snmp_var *iter_var;
if (*head == NULL) {
*head = var;
}
if (*tail != NULL) {
(*tail)->next = var;
}
(*tail) = var;
for (iter_var = *head; iter_var; iter_var = iter_var->next) {
count++;
}
return count;
}