forked from alliedtelesis/apteryx-netconf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
netconf.c
2724 lines (2464 loc) · 86 KB
/
netconf.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
/**
* @file netconf.c
* libnetconf2 to Apteryx glue
*
* Copyright 2019, Allied Telesis Labs New Zealand, Ltd
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>
*/
#include "internal.h"
#define __USE_GNU
#include <sys/socket.h>
#include <pwd.h>
#define APTERYX_XML_LIBXML2
#include <apteryx-xml.h>
#include <libxml/xpath.h>
#include <libxml/xpathInternals.h>
#include <libxml/debugXML.h>
#define DEFAULT_LANG "en"
#define RECV_TIMEOUT_SEC 60
static sch_instance *g_schema = NULL;
struct netconf_session
{
int fd;
uint32_t id;
char *username;
gchar *rem_addr;
gchar *rem_port;
gchar *login_time;
bool running;
session_counters_t counters;
};
static struct _running_ds_lock_t
{
struct netconf_session nc_sess;
gboolean locked;
} running_ds_lock;
#define NETCONF_BASE_1_0_END "]]>]]>"
#define NETCONF_BASE_1_1_END "\n##\n"
#define NETCONF_HELLO_END "hello>]]>]]>"
#define NETCONF_HELLO_END_LEN 12
#define HELLO_RX_SIZE 1024
#define MAX_HELLO_RX_SIZE 16384
#define MAX_REQUEST_MESSAGE_SIZE 32768
#define NETCONF_STATE_SESSIONS_PATH "/netconf-state/sessions/session"
#define NETCONF_STATE_STATISTICS_PATH "/netconf-state/statistics"
#define NETCONF_SESSION_STATUS "/netconf-state/sessions/session/*/status"
#define NETCONF_CONFIG_MAX_SESSIONS "/netconf/config/max-sessions"
#define NETCONF_STATE "/netconf/state"
/* Defines for the max-sessions variable - the maximum number of sessions allowed */
#define NETCONF_MAX_SESSIONS_MIN 1
#define NETCONF_MAX_SESSIONS_MAX 10
#define NETCONF_MAX_SESSIONS_DEF 4
static uint32_t netconf_session_id = 1;
static uint32_t netconf_max_sessions = NETCONF_MAX_SESSIONS_DEF;
static uint32_t netconf_num_sessions = 0;
/* Maintain a list of open sessions */
static GList *open_sessions_list = NULL;
GMutex session_lock;
/* Global statistics */
global_statistics_t netconf_global_stats;
static void
_set_xmlns (xmlNode* node)
{
xmlNs *ns = xmlNewNs (node, BAD_CAST "urn:ietf:params:xml:ns:netconf:base:1.0", BAD_CAST "nc");
xmlSetNs (node, ns);
}
/* Error handling helper routines */
static const char*
rpc_error_tag_to_msg (NC_ERR_TAG err_tag)
{
switch (err_tag)
{
case NC_ERR_TAG_IN_USE:
return "Resource is already in use";
case NC_ERR_TAG_INVALID_VAL:
return "Unacceptable value for one or more parameters";
case NC_ERR_TAG_TOO_BIG:
return "The request is too large to be handled";
case NC_ERR_TAG_MISSING_ATTR:
return "An expected attribute is missing";
case NC_ERR_TAG_BAD_ATTR:
return "An attribute value is not correct";
case NC_ERR_TAG_UNKNOWN_ATTR:
return "An unexpected attribute is present";
case NC_ERR_TAG_MISSING_ELEM:
return "An expected element is missing";
case NC_ERR_TAG_BAD_ELEM:
return "An element value is not correct";
case NC_ERR_TAG_UNKNOWN_ELEM:
return "An unexpected element is present";
case NC_ERR_TAG_UNKNOWN_NS:
return "An unexpected namespace is present";
case NC_ERR_TAG_ACCESS_DENIED:
return "Access to the requested resource is denied due to authorization failure";
case NC_ERR_TAG_LOCK_DENIED:
return "Access to the requested lock is denied because the lock is currently held by another entity";
case NC_ERR_TAG_RESOURCE_DENIED:
return "Request could not be completed because of insufficient resources";
case NC_ERR_TAG_DATA_EXISTS:
return "Requested data model content already exists";
case NC_ERR_TAG_DATA_MISSING:
return "Requested data model content does not exist";
case NC_ERR_TAG_OPR_NOT_SUPPORTED:
return "Requested operation is not supported by this implementation";
case NC_ERR_TAG_OPR_FAILED:
return "Requested operation failed due to some reason";
case NC_ERR_TAG_MALFORMED_MSG:
return "Failed to parse XML message";
default:
return NULL;
}
}
static const char*
rpc_error_tag_to_string (NC_ERR_TAG err_tag)
{
switch (err_tag)
{
case NC_ERR_TAG_IN_USE:
return "in-use";
case NC_ERR_TAG_INVALID_VAL:
return "invalid-value";
case NC_ERR_TAG_TOO_BIG:
return "too-big";
case NC_ERR_TAG_MISSING_ATTR:
return "missing-attribute";
case NC_ERR_TAG_BAD_ATTR:
return "bad-attribute";
case NC_ERR_TAG_UNKNOWN_ATTR:
return "unknown-attribute";
case NC_ERR_TAG_MISSING_ELEM:
return "missing-element";
case NC_ERR_TAG_BAD_ELEM:
return "bad-element";
case NC_ERR_TAG_UNKNOWN_ELEM:
return "unknown-element";
case NC_ERR_TAG_UNKNOWN_NS:
return "unknown-namespace";
case NC_ERR_TAG_ACCESS_DENIED:
return "access-denied";
case NC_ERR_TAG_LOCK_DENIED:
return "lock-denied";
case NC_ERR_TAG_RESOURCE_DENIED:
return "resource-denied";
case NC_ERR_TAG_DATA_EXISTS:
return "data-exists";
case NC_ERR_TAG_DATA_MISSING:
return "data-missing";
case NC_ERR_TAG_OPR_NOT_SUPPORTED:
return "operation-not-supported";
case NC_ERR_TAG_OPR_FAILED:
return "operation-failed";
case NC_ERR_TAG_MALFORMED_MSG:
return "malformed-message";
default:
return NULL;
}
}
static const char*
rpc_error_type_to_string (NC_ERR_TYPE err_type)
{
switch (err_type)
{
case NC_ERR_TYPE_TRANSPORT:
return "transport";
case NC_ERR_TYPE_RPC:
return "rpc";
case NC_ERR_TYPE_PROTOCOL:
return "protocol";
case NC_ERR_TYPE_APP:
return "application";
default:
return NULL;
}
}
static xmlNode *
_create_error_info_xml (nc_error_parms parms)
{
xmlNode *xml_err_info = xmlNewNode (NULL, BAD_CAST "error-info");
_set_xmlns(xml_err_info);
if (parms.tag == NC_ERR_TAG_UNKNOWN_NS)
{
char *bad_ns = NULL;
char *bad_elem = NULL;
bad_ns = g_hash_table_lookup (parms.info, "bad-namespace");
bad_elem = g_hash_table_lookup (parms.info, "bad-element");
if (bad_ns && bad_elem)
{
xmlNewChild (xml_err_info, NULL, BAD_CAST "bad-namespace", BAD_CAST bad_ns);
xmlNewChild (xml_err_info, NULL, BAD_CAST "bad-element", BAD_CAST bad_elem);
}
}
else if (parms.tag == NC_ERR_TAG_IN_USE || parms.tag == NC_ERR_TAG_LOCK_DENIED)
{
char *session_id = NULL;
session_id = g_hash_table_lookup (parms.info, "session-id");
if (session_id)
{
xmlNewChild (xml_err_info, NULL, BAD_CAST "session-id", BAD_CAST session_id);
}
}
else if (parms.tag == NC_ERR_TAG_MISSING_ATTR || parms.tag == NC_ERR_TAG_BAD_ATTR ||
parms.tag == NC_ERR_TAG_UNKNOWN_ATTR)
{
char *bad_attr = NULL;
char *bad_elem = NULL;
bad_attr = g_hash_table_lookup (parms.info, "bad-attribute");
bad_elem = g_hash_table_lookup (parms.info, "bad-element");
if (bad_elem && bad_attr)
{
xmlNewChild (xml_err_info, NULL, BAD_CAST "bad-attribute", BAD_CAST bad_attr);
xmlNewChild (xml_err_info, NULL, BAD_CAST "bad-element", BAD_CAST bad_elem);
}
}
else if (parms.tag == NC_ERR_TAG_MISSING_ELEM || parms.tag == NC_ERR_TAG_BAD_ELEM ||
parms.tag == NC_ERR_TAG_UNKNOWN_ELEM)
{
char *bad_elem = NULL;
bad_elem = g_hash_table_lookup (parms.info, "bad-element");
if (bad_elem)
{
xmlNewChild (xml_err_info, NULL, BAD_CAST "bad-element", BAD_CAST bad_elem);
}
}
return xml_err_info;
}
static void
_free_error_parms (nc_error_parms error_parms)
{
error_parms.tag = 0;
error_parms.type = 0;
g_string_free (error_parms.msg, TRUE);
g_hash_table_destroy (error_parms.info);
}
/* Close open sessions */
void
netconf_close_open_sessions (void)
{
if (open_sessions_list)
{
for (GList *iter = open_sessions_list; iter; iter = g_list_next (iter))
{
struct netconf_session *nc_session = iter->data;
if (nc_session && nc_session->fd >= 0)
{
close (nc_session->fd);
nc_session->fd = -1;
}
}
}
}
/**
* Remove specified netconf session from open_sessions_list. Can't guarantee
* that the passed in session is the one on the list, hence the search by ID.
*/
static void
remove_netconf_session (struct netconf_session *session)
{
if (!session || !open_sessions_list)
{
return;
}
g_mutex_lock (&session_lock);
for (GList *iter = open_sessions_list; iter; iter = g_list_next (iter))
{
struct netconf_session *nc_session = iter->data;
if (nc_session && session->id == nc_session->id)
{
open_sessions_list = g_list_remove (open_sessions_list, nc_session);
netconf_num_sessions--;
break;
}
}
g_mutex_unlock (&session_lock);
}
/* Find open netconf session details by ID */
static struct netconf_session *
find_netconf_session_by_id (uint32_t session_id)
{
struct netconf_session *ret = NULL;
g_mutex_lock (&session_lock);
for (GList *iter = open_sessions_list; iter; iter = g_list_next (iter))
{
struct netconf_session *nc_session = iter->data;
if (nc_session && session_id == nc_session->id)
{
ret = nc_session;
break;
}
}
g_mutex_unlock (&session_lock);
return ret;
}
static xmlDoc*
create_rpc (xmlChar *type, xmlChar *msg_id)
{
xmlDoc *doc = xmlNewDoc (BAD_CAST "1.0");
xmlNode *root = xmlNewNode (NULL, type);
xmlNs *ns = xmlNewNs (root, BAD_CAST "urn:ietf:params:xml:ns:netconf:base:1.0", BAD_CAST "nc");
xmlSetNs (root, ns);
if (msg_id)
{
xmlSetProp (root, BAD_CAST "message-id", msg_id);
free (msg_id);
}
xmlDocSetRootElement (doc, root);
return doc;
}
static bool
send_rpc_ok (struct netconf_session *session, xmlNode * rpc, bool closing)
{
xmlDoc *doc;
xmlChar *xmlbuff = NULL;
char *header = NULL;
int len;
bool ret = true;
/* Generate reply */
doc = create_rpc (BAD_CAST "rpc-reply", xmlGetProp (rpc, BAD_CAST "message-id"));
xmlNewChild (xmlDocGetRootElement (doc), NULL, BAD_CAST "ok", NULL);
xmlDocDumpMemoryEnc (doc, &xmlbuff, &len, "UTF-8");
header = g_strdup_printf ("\n#%d\n", len);
/* Send reply */
if (write (session->fd, header, strlen (header)) != strlen (header))
{
if (!closing)
{
ERROR ("TX failed: Sending %ld bytes of header\n", strlen (header));
}
ret = false;
goto cleanup;
}
VERBOSE ("TX(%ld):\n%s", strlen (header), header);
if (write (session->fd, xmlbuff, len) != len)
{
if (!closing)
{
ERROR ("TX failed: Sending %d bytes of hello\n", len);
}
ret = false;
goto cleanup;
}
VERBOSE ("TX(%d):\n%.*s", len, len, (char *) xmlbuff);
if (write (session->fd, NETCONF_BASE_1_1_END, strlen (NETCONF_BASE_1_1_END)) !=
strlen (NETCONF_BASE_1_1_END))
{
if (!closing)
{
ERROR ("TX failed: Sending %ld bytes of trailer\n",
strlen (NETCONF_BASE_1_1_END));
}
ret = false;
goto cleanup;
}
VERBOSE ("TX(%ld):\n%s\n", strlen (NETCONF_BASE_1_1_END), NETCONF_BASE_1_1_END);
cleanup:
g_free (header);
xmlFree (xmlbuff);
xmlFreeDoc (doc);
return ret;
}
/**
* Actually send the RPC error message - all information is contained in the nc_error_parms structure.
*/
static bool
_send_rpc_error (struct netconf_session *session, xmlNode * rpc, nc_error_parms error_parms)
{
xmlDoc *doc;
xmlNode *child;
xmlNode *error_msg = NULL;
xmlNode *error_info = NULL;
xmlChar *xmlbuff = NULL;
char *header = NULL;
int len;
bool ret = true;
/* Generate reply */
if (rpc)
doc = create_rpc (BAD_CAST "rpc-reply", xmlGetProp (rpc, BAD_CAST "message-id"));
else
doc = create_rpc (BAD_CAST "rpc-reply", NULL);
child = xmlNewChild (xmlDocGetRootElement (doc), NULL, BAD_CAST "rpc-error", NULL);
xmlNewChild (child, NULL, BAD_CAST "error-tag",
BAD_CAST rpc_error_tag_to_string (error_parms.tag));
xmlNewChild (child, NULL, BAD_CAST "error-type",
BAD_CAST rpc_error_type_to_string (error_parms.type));
xmlNewChild (child, NULL, BAD_CAST "error-severity", BAD_CAST "error");
if (!error_parms.msg || g_strcmp0 (error_parms.msg->str, "") == 0)
{
g_string_printf (error_parms.msg, "%s", rpc_error_tag_to_msg (error_parms.tag));
}
error_msg = xmlNewNode (child->ns, BAD_CAST "error-message");
xmlSetProp (error_msg, (const xmlChar *) "xml:lang", (const xmlChar *) DEFAULT_LANG);
xmlNodeSetContent (error_msg, BAD_CAST error_parms.msg->str);
xmlAddChild (child, error_msg);
if (error_parms.info != NULL && g_hash_table_size (error_parms.info) > 0)
{
error_info = _create_error_info_xml (error_parms);
xmlAddChild (child, error_info);
}
xmlDocDumpMemoryEnc (doc, &xmlbuff, &len, "UTF-8");
header = g_strdup_printf ("\n#%d\n", len);
/* Send reply */
if (write (session->fd, header, strlen (header)) != strlen (header))
{
ERROR ("TX failed: Sending %ld bytes of header\n", strlen (header));
ret = false;
goto cleanup;
}
VERBOSE ("TX(%ld):\n%s", strlen (header), header);
if (write (session->fd, xmlbuff, len) != len)
{
ERROR ("TX failed: Sending %d bytes of hello\n", len);
ret = false;
goto cleanup;
}
VERBOSE ("TX(%d):\n%.*s", len, len, (char *) xmlbuff);
if (write (session->fd, NETCONF_BASE_1_1_END, strlen (NETCONF_BASE_1_1_END)) !=
strlen (NETCONF_BASE_1_1_END))
{
ERROR ("TX failed: Sending %ld bytes of trailer\n", strlen (NETCONF_BASE_1_1_END));
ret = false;
goto cleanup;
}
VERBOSE ("TX(%ld):\n%s\n", strlen (NETCONF_BASE_1_1_END), NETCONF_BASE_1_1_END);
session->counters.out_rpc_errors++;
netconf_global_stats.session_totals.out_rpc_errors++;
cleanup:
g_free (header);
xmlFree (xmlbuff);
xmlFreeDoc (doc);
return ret;
}
/**
* Fully parameterised send_rpc_error. This can be used to send a variety of RPC error types, depending
* on what is passed in. Parameters session, rpc, err_tag, err_type and error_msg are mandatory, the rest are
* optional. Valid combinations of optional parameters are:
* session error - no optional parameters set, no_info flag false
* element error - set bad_elem only, no_info flag false (actually don't care)
* attribute error - set bad_elem and bad_attr only, no_info flag false (actually don't care)
* no_info error - no optional parameters set, no_info flag true
*/
static bool
send_rpc_error_full (struct netconf_session *session, xmlNode *rpc, NC_ERR_TAG err_tag, NC_ERR_TYPE err_type,
gchar *error_msg, char *bad_elem, char *bad_attr, bool no_info)
{
nc_error_parms error_parms = NC_ERROR_PARMS_INIT;
bool ret = false;
error_parms.tag = err_tag;
error_parms.type = err_type;
if (!bad_elem && !no_info)
{
gchar *sess_id_str = g_strdup_printf ("%u", running_ds_lock.nc_sess.id);
g_hash_table_insert (error_parms.info, "session-id", sess_id_str);
/* No need to free, hash table cleanup will do that */
}
if (error_msg)
{
g_string_printf (error_parms.msg, "%s", error_msg);
ERROR ("%s\n", error_msg);
}
if (bad_elem)
{
g_hash_table_insert (error_parms.info, "bad_element", g_strdup (bad_elem));
/* No need to free, hash table cleanup will do that */
}
if (bad_attr)
{
g_hash_table_insert (error_parms.info, "bad-attribute", g_strdup (bad_attr));
/* No need to free, hash table cleanup will do that */
}
ret = _send_rpc_error (session, rpc, error_parms);
_free_error_parms (error_parms);
return ret;
}
static bool
send_rpc_data (struct netconf_session *session, xmlNode * rpc, GList *xml_list)
{
xmlDoc *doc;
xmlNode * data;
xmlNode *child;
xmlChar *xmlbuff;
GList *list;
char *header = NULL;
int len;
bool ret = true;
/* Generate reply */
doc = create_rpc ( BAD_CAST "rpc-reply", xmlGetProp (rpc, BAD_CAST "message-id"));
child = xmlNewChild (xmlDocGetRootElement (doc), NULL, BAD_CAST "data", NULL);
if (!xml_list)
{
xmlAddChildList (child, NULL);
}
else
{
for (list = g_list_first (xml_list); list; list = g_list_next (list))
{
data = list->data;
xmlAddChildList (child, data);
}
}
xmlDocDumpMemoryEnc (doc, &xmlbuff, &len, "UTF-8");
header = g_strdup_printf ("\n#%d\n", len);
/* Send reply */
if (write (session->fd, header, strlen (header)) != strlen (header))
{
ERROR ("TX failed: Sending %ld bytes of header\n", strlen (header));
ret = false;
goto cleanup;
}
VERBOSE ("TX(%ld):\n%s", strlen (header), header);
if (write (session->fd, xmlbuff, len) != len)
{
ERROR ("TX failed: Sending %d bytes of hello\n", len);
ret = false;
goto cleanup;
}
VERBOSE ("TX(%d):\n%.*s", len, len, (char *) xmlbuff);
if (write (session->fd, NETCONF_BASE_1_1_END, strlen (NETCONF_BASE_1_1_END)) !=
strlen (NETCONF_BASE_1_1_END))
{
ERROR ("TX failed: Sending %ld bytes of trailer\n", strlen (NETCONF_BASE_1_1_END));
ret = false;
goto cleanup;
}
VERBOSE ("TX(%ld):\n%s\n", strlen (NETCONF_BASE_1_1_END), NETCONF_BASE_1_1_END);
cleanup:
g_free (header);
xmlFree (xmlbuff);
xmlFreeDoc (doc);
if (xml_list)
g_list_free (xml_list);
return ret;
}
static void
schema_set_model_information (xmlNode * cap)
{
xmlNode *xml_child;
sch_loaded_model *loaded;
GList *list;
char *capability;
GList *loaded_models = sch_get_loaded_models (g_schema);
for (list = g_list_first (loaded_models); list; list = g_list_next (list))
{
loaded = list->data;
if (loaded->organization && loaded->version && loaded->model &&
strlen (loaded->organization) && strlen (loaded->version) &&
strlen (loaded->model))
{
char *old;
xml_child = xmlNewChild (cap, NULL, BAD_CAST "capability", NULL);
capability = g_strdup_printf ("%s?module=%s&revision=%s",
loaded->ns_href, loaded->model, loaded->version);
if (loaded->features)
{
old = capability;
capability = g_strdup_printf ("%s&features=%s", capability, loaded->features);
g_free (old);
}
if (loaded->deviations)
{
old = capability;
capability = g_strdup_printf ("%s&deviations=%s", capability, loaded->deviations);
g_free (old);
}
xmlNodeSetContent (xml_child, BAD_CAST capability);
g_free (capability);
}
}
}
static bool
validate_hello (char *buffer, int buf_len)
{
xmlDoc *doc = NULL;
xmlNode *root;
xmlNode *node;
xmlNode *cap_node;
xmlChar *cap;
bool found_base11 = false;
doc = xmlParseMemory (buffer, buf_len);
if (!doc)
{
ERROR ("XML: Invalid hello message\n");
return false;
}
root = xmlDocGetRootElement (doc);
if (!root || g_strcmp0 ((char *) root->name, "hello") != 0)
{
ERROR ("XML: No root HELLO element\n");
xmlFreeDoc (doc);
return false;
}
node = xmlFirstElementChild (root);
if (!node || g_strcmp0 ((char *) node->name, "capabilities") != 0)
{
ERROR ("XML: No capabilities element in HELLO\n");
xmlFreeDoc (doc);
return false;
}
/* Check capabilities - we want to see base:1.1 */
for (cap_node = xmlFirstElementChild (node); cap_node; cap_node = xmlNextElementSibling (cap_node))
{
if (g_strcmp0 ((char *) cap_node->name, "capability") == 0)
{
cap = xmlNodeGetContent (cap_node);
if (cap)
{
if (g_strcmp0 ((char *) cap, "urn:ietf:params:netconf:base:1.1") == 0)
{
found_base11 = true;
}
xmlFree (cap);
if (found_base11)
{
break;
}
}
}
}
if (found_base11)
{
VERBOSE ("Received valid hello message\n");
}
else
{
ERROR ("NETCONF: No compatible base version found\n");
}
xmlFreeDoc (doc);
return found_base11;
}
static bool
handle_hello (struct netconf_session *session)
{
bool ret = true;
char buf[HELLO_RX_SIZE];
char *pt;
char *buffer = NULL;
char *endpt = NULL;
int len;
int recv_len = HELLO_RX_SIZE - NETCONF_HELLO_END_LEN;
int total_len = 0;
int offset = 0;
/* Allow MSG_PEEK to read sequentially through the buffer */
setsockopt (session->fd, SOL_SOCKET, SO_PEEK_OFF, &offset, sizeof (offset));
/* Reserve NETCONF_HELLO_END_LEN bytes at the front of the buffer */
pt = buf + NETCONF_HELLO_END_LEN;
memset (buf, ' ', NETCONF_HELLO_END_LEN);
while (g_main_loop_is_running (g_loop))
{
len = recv (session->fd, pt, recv_len, MSG_PEEK);
if (len > 0)
{
char *match = g_strstr_len (buf, len + NETCONF_HELLO_END_LEN, NETCONF_HELLO_END);
if (match)
{
total_len += match - buf;
break;
}
else if (len < recv_len)
{
/* Reached the end of the message with no hello end */
return false;
}
if (len == recv_len)
{
/* Copy NETCONF_HELLO_END_LEN bytes from the end of the buffer to the front of the
receive buffer so that we can guard against the end string being split over two recveives */
total_len += recv_len;
if (total_len >= MAX_HELLO_RX_SIZE)
{
return false;
}
memcpy (buf, pt + recv_len - NETCONF_HELLO_END_LEN, NETCONF_HELLO_END_LEN);
}
}
else
{
return false;
}
}
buffer = g_malloc0 (total_len);
if (!buffer)
{
return false;
}
len = recv (session->fd, buffer, total_len, 0);
if (len <= 0)
{
g_free (buffer);
return false;
}
VERBOSE ("RX(%d):\n%.*s", len, (int) len, buffer);
/* Find trailer */
endpt = g_strstr_len (buffer, len, NETCONF_BASE_1_0_END);
if (!endpt)
{
ERROR ("XML: Invalid hello message (no 1.0 trailer)\n");
g_free (buffer);
return false;
}
/* Validate hello */
if (!validate_hello (buffer, (endpt - buffer)))
{
ret = false;
}
g_free (buffer);
return ret;
}
static bool
send_hello (struct netconf_session *session)
{
bool ret = true;
xmlDoc *doc = NULL;
xmlNode *root, *node, *child;
xmlChar *hello_resp = NULL;
char session_id_str[32];
int hello_resp_len = 0;
doc = create_rpc (BAD_CAST "hello", NULL);
root = xmlDocGetRootElement (doc);
node = xmlNewChild (root, NULL, BAD_CAST "capabilities", NULL);
child = xmlNewChild (node, NULL, BAD_CAST "capability", NULL);
xmlNodeSetContent (child, BAD_CAST "urn:ietf:params:netconf:base:1.1");
child = xmlNewChild (node, NULL, BAD_CAST "capability", NULL);
xmlNodeSetContent (child, BAD_CAST "urn:ietf:params:netconf:capability:xpath:1.0");
child = xmlNewChild (node, NULL, BAD_CAST "capability", NULL);
xmlNodeSetContent (child,
BAD_CAST "urn:ietf:params:netconf:capability:writable-running:1.0");
child = xmlNewChild (node, NULL, BAD_CAST "capability", NULL);
xmlNodeSetContent (child,
BAD_CAST "urn:ietf:params:netconf:capability:with-defaults:1.0?basic-mode=explicit&also-supported=report-all,trim");
/* Find all models in the entire tree */
schema_set_model_information (node);
snprintf (session_id_str, sizeof (session_id_str), "%u", session->id);
node = xmlNewChild (root, NULL, BAD_CAST "session-id", NULL);
xmlNodeSetContent (node, BAD_CAST session_id_str);
xmlDocDumpMemoryEnc (doc, &hello_resp, &hello_resp_len, "UTF-8");
xmlFreeDoc (doc);
/* Send reply */
if (write (session->fd, hello_resp, hello_resp_len) != hello_resp_len)
{
ERROR ("TX failed: Sending %d bytes of hello\n", hello_resp_len);
ret = false;
goto cleanup;
}
VERBOSE ("TX(%d):\n%.*s", hello_resp_len, hello_resp_len, (char *) hello_resp);
if (write (session->fd, NETCONF_BASE_1_0_END, strlen (NETCONF_BASE_1_0_END)) !=
strlen (NETCONF_BASE_1_0_END))
{
ERROR ("TX failed: Sending %ld bytes of hello trailer\n",
strlen (NETCONF_BASE_1_0_END));
ret = false;
goto cleanup;
}
VERBOSE ("TX(%ld):\n%s\n", strlen (NETCONF_BASE_1_0_END), NETCONF_BASE_1_0_END);
cleanup:
xmlFree (hello_resp);
return ret;
}
static GNode *
get_full_tree ()
{
GNode *tree = APTERYX_NODE (NULL, g_strdup_printf ("/"));
GList *children, *iter;
/* Search root and then get tree for each root entry */
children = apteryx_search ("/");
for (iter = children; iter; iter = g_list_next (iter))
{
const char *path = (const char *) iter->data;
GNode *subtree = apteryx_get_tree (path);
if (subtree)
{
g_free (subtree->data);
subtree->data = g_strdup (path + 1);
g_node_append (tree, subtree);
}
}
g_list_free_full (children, free);
return tree;
}
static gboolean
remove_null_data (GNode *node, gpointer data)
{
if (node->data == NULL)
{
g_node_unlink (node);
g_node_destroy (node);
}
return false;
}
static void
cleanup_empty_branches (xmlNode *node, int depth, int max_depth, bool *root_deleted)
{
xmlNode *cur_node = NULL;
xmlNode *next_node = NULL;
if (depth < max_depth - 1)
{
for (cur_node = node; cur_node; cur_node = next_node)
{
next_node = cur_node->next;
if (cur_node->type == XML_ELEMENT_NODE)
{
if (!cur_node->children)
{
xmlUnlinkNode (cur_node);
xmlFreeNode (cur_node);
if (depth == 0 && cur_node == node)
*root_deleted = true;
continue;
}
}
cleanup_empty_branches (cur_node->children, depth + 1, max_depth, root_deleted);
}
}
}
static void
cleanup_xpath_tree (GHashTable *node_table, xmlNode *node, int depth, int *max_depth, bool *root_deleted)
{
xmlNode *cur_node = NULL;
xmlNode *next_node = NULL;
for (cur_node = node; cur_node; cur_node = next_node)
{
next_node = cur_node->next;
if (cur_node->type == XML_ELEMENT_NODE) {
if (!g_hash_table_lookup (node_table, cur_node))
{
xmlUnlinkNode (cur_node);
xmlFreeNode (cur_node);
if (depth == 0 && cur_node == node)
*root_deleted = true;
continue;
}
}
cleanup_xpath_tree (node_table, cur_node->children, depth + 1, max_depth, root_deleted);
}
if (depth > *max_depth)
*max_depth = depth;
if (depth == 0)
cleanup_empty_branches (node, depth, *max_depth, root_deleted);
}
static bool
_xpath_mark_list_nodes (sch_node *schema, xmlNode *node, int flags, int depth, GHashTable *node_table)
{
sch_node *s_node;
sch_node *child;
const char *target_name;
char *name;
char *colon;
bool rc = true;
for (xmlNode *cur_node = node; cur_node; cur_node = cur_node->next)
{
if (g_hash_table_lookup (node_table, cur_node))
{
target_name = (const char *) cur_node->name;
for (s_node = schema; s_node; s_node = sch_node_next_sibling (s_node))
{
name = sch_name (s_node);
if (depth == 0)
{
colon = strchr (name, ':');
if (colon)
{
char *tmp = name;
name = g_strdup (colon + 1);
g_free (tmp);
}
}
if (g_strcmp0 (name, target_name) == 0)
{
g_free (name);
break;
}
g_free (name);
}
if (s_node)
{
if (sch_is_list (s_node))
{
xmlNode *first_child = cur_node->children;
if (first_child && !g_hash_table_lookup (node_table, first_child))
g_hash_table_insert (node_table, first_child, first_child);
}
child = sch_node_child_first (s_node);
if (cur_node->children && child)
{
_xpath_mark_list_nodes (child, cur_node->children, flags, depth + 1, node_table);
}
}
}
}
return rc;
}
bool
xpath_mark_list_nodes (xmlNode * xml, int flags, GHashTable *node_table)
{
sch_node *schema;
bool rc = false;
schema = sch_get_root_schema (g_schema);
schema = sch_node_namespace_child (schema, (char *) xml->ns->href, (char *) xml->name);
if (g_hash_table_lookup (node_table, xml))
rc = _xpath_mark_list_nodes (schema, xml, flags, 0, node_table);
return rc;
}
void
xpath_tree_add (GHashTable *node_table, xmlNode *node)
{