Change internal protocol from clicon_proto.h to netconf.

This commit is contained in:
Olof hagsand 2017-03-25 11:10:50 +01:00
parent 2e09f54d12
commit 2fcefda831
66 changed files with 3012 additions and 5141 deletions

View file

@ -40,7 +40,7 @@ LIBS = @LIBS@
SHELL = /bin/sh
SUBDIRS = cli backend dbctrl netconf xmldb restconf
SUBDIRS = cli backend dbctrl netconf restconf
.PHONY: all clean depend install $(SUBDIRS)

File diff suppressed because it is too large Load diff

View file

@ -70,8 +70,6 @@ struct client_subscription{
* Prototypes
*/
int backend_client_rm(clicon_handle h, struct client_entry *ce);
int config_snapshot(clicon_handle h, char *dbname, char *dir);
int from_client(int fd, void *arg);
#endif /* _BACKEND_CLIENT_H_ */

View file

@ -126,6 +126,10 @@ generic_validate(yang_spec *yspec,
}
/*! Common code of candidate_validate and candidate_commit
* @param[in] h Clicon handle
* @param[in] candidate The candidate database. The wanted backend state
* @retval 0 OK
* @retval -1 Fatal error or netconf error XXX Differentiate
*/
static int
validate_common(clicon_handle h,
@ -137,7 +141,6 @@ validate_common(clicon_handle h,
int i;
cxobj *xn;
if ((yspec = clicon_dbspec_yang(h)) == NULL){
clicon_err(OE_FATAL, 0, "No DB_SPEC");
goto done;
@ -253,126 +256,117 @@ candidate_commit(clicon_handle h,
return retval;
}
/*! Do a diff between candidate and running, then start a validate transaction
*
* @param[in] h Clicon handle
* @param[in] candidate: The candidate database. The wanted backend state
*/
int
candidate_validate(clicon_handle h,
char *candidate)
{
int retval = -1;
transaction_data_t *td = NULL;
/* 1. Start transaction */
if ((td = transaction_new()) == NULL)
goto done;
/* Common steps (with commit) */
if (validate_common(h, candidate, td) < 0)
goto done;
retval = 0;
done:
/* In case of failure, call plugin transaction termination callbacks */
if (retval < 0 && td)
plugin_transaction_abort(h, td);
if (td)
transaction_free(td);
return retval;
}
/*! Handle an incoming commit message from a client.
* XXX: If commit succeeds and snapshot/startup fails, we have strange state:
* the commit has succeeded but an error message is returned.
/*! Discard all changes in candidate / revert to running
* @param[in] h Clicon handle
* @param[out] cbret Return xml value cligen buffer
* @retval 0 OK. This may indicate both ok and err msg back to client
* @retval -1 (Local) Error
*/
int
from_client_commit(clicon_handle h,
int s,
struct clicon_msg *msg,
const char *label)
from_client_commit(clicon_handle h,
cbuf *cbret)
{
int retval = -1;
char *candidate;
char *running;
if (clicon_msg_commit_decode(msg,
&candidate,
&running,
label) < 0)
goto err;
if (strcmp(candidate, "candidate") && strcmp(candidate, "tmp")){
clicon_err(OE_PLUGIN, 0, "candidate is not \"candidate\" or tmp");
goto err;
}
if (strcmp(running, "running")){
clicon_err(OE_PLUGIN, 0, "running db is not \"running\"");
goto err;
}
if (candidate_commit(h, "candidate") < 0){
clicon_debug(1, "Commit %s failed", candidate);
retval = 0; /* We ignore errors from commit, but maybe
we should fail on fatal errors? */
goto err;
clicon_debug(1, "Commit candidate failed");
/* XXX: candidate_validate should have proper error handling */
cprintf(cbret, "<rpc-reply><rpc-error>"
"<error-tag>missing-attribute</error-tag>"
"<error-type>protocol</error-type>"
"<error-severity>error</error-severity>"
"<error-message>%s</error-message>"
"</rpc-error></rpc-reply>",
clicon_err_reason);
goto ok;
}
clicon_debug(1, "Commit %s", candidate);
cprintf(cbret, "<rpc-reply><ok/></rpc-reply>");
ok:
retval = 0;
if (send_msg_ok(s) < 0)
goto done;
goto done;
err:
/* XXX: more elaborate errstring? */
if (send_msg_err(s, clicon_errno, clicon_suberrno, "%s", clicon_err_reason) < 0)
retval = -1;
done:
unchunk_group(__FUNCTION__);
// done:
return retval; /* may be zero if we ignoring errors from commit */
} /* from_client_commit */
/*! Discard all changes in candidate / revert to running
* @param[in] h Clicon handle
* @param[out] cbret Return xml value cligen buffer
* @retval 0 OK. This may indicate both ok and err msg back to client
* @retval -1 (Local) Error
*/
int
from_client_discard_changes(clicon_handle h,
cbuf *cbret)
{
int retval = -1;
if (xmldb_copy(h, "running", "candidate") < 0){
cprintf(cbret, "<rpc-reply><rpc-error>"
"<error-tag>operation-failed</error-tag>"
"<error-type>application</error-type>"
"<error-severity>error</error-severity>"
"<error-info>read-registry</error-info>"
"</rpc-error></rpc-reply>");
goto ok;
}
cprintf(cbret, "<rpc-reply><ok/></rpc-reply>");
ok:
retval = 0;
// done:
return retval; /* may be zero if we ignoring errors from commit */
}
/*! Handle an incoming validate message from a client.
* @param[in] h Clicon handle
* @param[in] db Database name
* @param[out] cbret Return xml value cligen buffer
* @retval 0 OK. This may indicate both ok and err msg back to client (eg invalid)
* @retval -1 (Local) Error
*/
int
from_client_validate(clicon_handle h,
int s,
struct clicon_msg *msg,
const char *label)
from_client_validate(clicon_handle h,
char *db,
cbuf *cbret)
{
int retval = -1;
char *candidate;
int retval = -1;
transaction_data_t *td = NULL;
if (clicon_msg_validate_decode(msg,
&candidate,
label) < 0){
send_msg_err(s, clicon_errno, clicon_suberrno,
clicon_err_reason);
goto err;
if (strcmp(db, "candidate") != 0 && strcmp(db, "tmp") != 0){
cprintf(cbret, "<rpc-reply><rpc-error>"
"<error-tag>invalid-value</error-tag>"
"<error-type>protocol</error-type>"
"<error-severity>error</error-severity>"
"</rpc-error></rpc-reply>");
goto ok;
}
if (strcmp(candidate, "candidate") != 0 && strcmp(candidate, "tmp") != 0){
clicon_err(OE_PLUGIN, 0, "candidate is not \"candidate\" or tmp");
goto err;
}
clicon_debug(1, "Validate %s", candidate);
if (candidate_validate(h, candidate) < 0){
clicon_debug(1, "Validate %s failed", candidate);
retval = 0; /* We ignore errors from commit, but maybe
we should fail on fatal errors? */
goto err;
}
retval = 0;
if (send_msg_ok(s) < 0)
clicon_debug(1, "Validate %s", db);
/* 1. Start transaction */
if ((td = transaction_new()) == NULL)
goto done;
goto done;
err:
/* XXX: more elaborate errstring? */
if (send_msg_err(s, clicon_errno, clicon_suberrno, "%s", clicon_err_reason) < 0)
retval = -1;
done:
unchunk_group(__FUNCTION__);
/* Common steps (with commit) */
if (validate_common(h, db, td) < 0){
clicon_debug(1, "Validate %s failed", db);
/* XXX: candidate_validate should have proper error handling */
cprintf(cbret, "<rpc-reply><rpc-error>"
"<error-tag>missing-attribute</error-tag>"
"<error-type>protocol</error-type>"
"<error-severity>error</error-severity>"
"<error-message>%s</error-message>"
"</rpc-error></rpc-reply>",
clicon_err_reason);
goto ok;
}
cprintf(cbret, "<rpc-reply><ok/></rpc-reply>");
ok:
retval = 0;
done:
if (retval < 0 && td)
plugin_transaction_abort(h, td);
if (td)
transaction_free(td);
return retval;
} /* from_client_validate */

View file

@ -40,8 +40,9 @@
/*
* Prototypes
*/
int from_client_validate(clicon_handle h, int s, struct clicon_msg *msg, const char *label);
int from_client_commit(clicon_handle h, int s, struct clicon_msg *msg, const char *label);
int candidate_commit(clicon_handle h, char *candidate);
int from_client_validate(clicon_handle h, char *db, cbuf *cbret);
int from_client_commit(clicon_handle h, cbuf *cbret);
int from_client_discard_changes(clicon_handle h, cbuf *cbret);
int candidate_commit(clicon_handle h, char *db);
#endif /* _BACKEND_COMMIT_H_ */

View file

@ -37,7 +37,6 @@
#ifndef _BACKEND_HANDLE_H_
#define _BACKEND_HANDLE_H_
/*
* Prototypes
* not exported.

View file

@ -72,7 +72,7 @@
#include "backend_handle.h"
/* Command line options to be passed to getopt(3) */
#define BACKEND_OPTS "hD:f:d:Fzu:P:1IRCc:rg:ptx:"
#define BACKEND_OPTS "hD:f:d:Fzu:P:1IRCc:rg:pt"
/*! Terminate. Cannot use h after this */
static int
@ -141,8 +141,7 @@ usage(char *argv0, clicon_handle h)
" -r\t\tReload running database\n"
" -p \t\tPrint database yang specification\n"
" -t \t\tPrint alternate spec translation (eg if YANG print KEY, if KEY print YANG)\n"
" -g <group>\tClient membership required to this group (default: %s)\n"
" -x <status>\tSet CLICON_XMLDB_RPC to 0 or 1.\n",
" -g <group>\tClient membership required to this group (default: %s)\n",
argv0,
plgdir ? plgdir : "none",
confsock ? confsock : "none",
@ -153,11 +152,12 @@ usage(char *argv0, clicon_handle h)
}
static int
rundb_init(clicon_handle h)
db_reset(clicon_handle h,
char *db)
{
if (xmldb_delete(h, "running") != 0 && errno != ENOENT)
if (xmldb_delete(h, db) != 0 && errno != ENOENT)
return -1;
if (xmldb_init(h, "running") < 0)
if (xmldb_init(h, db) < 0)
return -1;
return 0;
}
@ -192,7 +192,7 @@ rundb_main(clicon_handle h,
if (clicon_xml_parse_file(fd, &xt, "</clicon>") < 0)
goto done;
if ((xn = xml_child_i(xt, 0)) != NULL)
if (xmldb_put(h, "tmp", xn, OP_MERGE) < 0)
if (xmldb_put(h, "tmp", OP_MERGE, NULL, xn) < 0)
goto done;
if (candidate_commit(h, "tmp") < 0)
goto done;
@ -426,14 +426,6 @@ main(int argc, char **argv)
case 't' : /* Print alternative dbspec format (eg if YANG, print KEY) */
printalt++;
break;
case 'x' : /* set xmldb rpc on */
{
int i;
if (sscanf(optarg, "%d", &i) != 1)
usage(argv[0], h);
clicon_option_int_set(h, "CLICON_XMLDB_RPC", i);
}
break;
default:
usage(argv[0], h);
break;
@ -503,7 +495,7 @@ main(int argc, char **argv)
if (yang_spec_main(h, stdout, printspec) < 0)
goto done;
/* First check for starup config
/* First check for startup config
XXX the options below have become out-of-hand.
Too complex, need to simplify*/
if (clicon_option_int(h, "CLICON_USE_STARTUP_CONFIG") > 0){
@ -513,8 +505,12 @@ main(int argc, char **argv)
goto done;
}
else
if (rundb_init(h) < 0)
if (db_reset(h, "running") < 0)
goto done;
if (xmldb_init(h, "candidate") < 0)
goto done;
if (xmldb_copy(h, "running", "candidate") < 0)
goto done;
}
/* If running exists and reload_running set, make a copy to candidate */
if (reload_running){
@ -529,9 +525,17 @@ main(int argc, char **argv)
/* Init running db
* -I or if it isnt there
*/
if (init_rundb || xmldb_exists(h, "running") != 1)
if (rundb_init(h) < 0)
if (init_rundb || xmldb_exists(h, "running") != 1){
if (db_reset(h, "running") < 0)
goto done;
}
/* If candidate does not exist, create it from running */
if (xmldb_exists(h, "candidate") != 1){
if (xmldb_init(h, "candidate") < 0)
goto done;
if (xmldb_copy(h, "running", "candidate") < 0)
goto done;
}
/* Initialize plugins
(also calls plugin_init() and plugin_start(argc,argv) in each plugin */

View file

@ -470,55 +470,6 @@ plugin_finish(clicon_handle h)
return 0;
}
/*! Call from frontend to function 'func' in plugin 'plugin'.
* Plugin function is supposed to populate 'retlen' and 'retarg' where
* 'retarg' is malloc:ed data if non-NULL.
* @param[in] h Clicon handle
* @param[in] req Clicon message containing information about the downcall
* @param[out] retlen Length of return value
* @param[out] ret Return value
* @retval 0 OK
* @retval -1 Error
*/
int
plugin_downcall(clicon_handle h,
struct clicon_msg_call_req *req,
uint16_t *retlen,
void **retarg)
{
int retval = -1;
int i;
downcall_cb funcp;
char name[PATH_MAX];
char *error;
struct plugin *p;
for (i = 0; i < nplugins; i++) {
p = &plugins[i];
strncpy(name, p->p_name, sizeof(name)-1);
if (!strcmp(name+strlen(name)-3, ".so"))
name[strlen(name)-3] = '\0';
/* If no plugin is given or the plugin-name matches */
if (req->cr_plugin == NULL || strlen(req->cr_plugin)==0 ||
strcmp(name, req->cr_plugin) == 0) {
funcp = dlsym(p->p_handle, req->cr_func);
if ((error = (char*)dlerror()) != NULL) {
clicon_err(OE_PROTO, ENOENT,
"Function does not exist: %s()", req->cr_func);
return -1;
}
retval = funcp(h, req->cr_op, req->cr_arglen, req->cr_arg, retlen, retarg);
goto done;
}
}
clicon_err(OE_PROTO, ENOENT,"%s: %s(): Plugin does not exist: %s",
__FUNCTION__, req->cr_func, req->cr_plugin);
return -1;
done:
return retval;
}
/*! Create and initialize transaction */
transaction_data_t *
transaction_new(void)
@ -771,4 +722,3 @@ plugin_transaction_abort(clicon_handle h,
return retval;
}

View file

@ -40,7 +40,6 @@
* Types
*/
/*! Transaction data
* Clicon internal, presented as void* to app's callback in the 'transaction_data'
* type in clicon_backend_api.h
@ -69,8 +68,6 @@ int plugin_finish(clicon_handle h);
int plugin_reset_state(clicon_handle h, char *dbname);
int plugin_start_hooks(clicon_handle h, int argc, char **argv);
int plugin_downcall(clicon_handle h, struct clicon_msg_call_req *req,
uint16_t *retlen, void **retarg);
transaction_data_t * transaction_new(void);
int transaction_free(transaction_data_t *);

View file

@ -265,7 +265,7 @@ config_accept_client(int fd, void *arg)
/*
* Here we register callbacks for actual data socket
*/
if (event_reg_fd(s, from_client, (void*)ce, "client socket") < 0)
if (event_reg_fd(s, from_client, (void*)ce, "local netconf client socket") < 0)
goto done;
retval = 0;
done:

View file

@ -133,6 +133,7 @@ backend_notify(clicon_handle h,
struct handle_subscription *hs;
int retval = -1;
clicon_debug(1, "%s %s", __FUNCTION__, stream);
/* First thru all clients(sessions), and all subscriptions and find matches */
for (ce = backend_client_list(h); ce; ce = ce_next){
ce_next = ce->ce_next;
@ -257,7 +258,8 @@ backend_notify_xml(clicon_handle h,
}
struct client_entry *
backend_client_add(clicon_handle h, struct sockaddr *addr)
backend_client_add(clicon_handle h,
struct sockaddr *addr)
{
struct backend_handle *cb = handle(h);
struct client_entry *ce;
@ -411,3 +413,79 @@ subscription_each(clicon_handle h,
hs = cb->cb_subscription;
return hs;
}
/* Database dependency description */
struct backend_netconf_reg {
qelem_t nr_qelem; /* List header */
backend_netconf_cb_t nr_callback; /* Validation/Commit Callback */
void *nr_arg; /* Application specific argument to cb */
char *nr_tag; /* Xml tag when matched, callback called */
};
typedef struct backend_netconf_reg backend_netconf_reg_t;
static backend_netconf_reg_t *deps = NULL;
/*! Register netconf callback
* Called from plugin to register a callback for a specific netconf XML tag.
*/
int
backend_netconf_register_callback(clicon_handle h,
backend_netconf_cb_t cb, /* Callback called */
void *arg, /* Arg to send to callback */
char *tag) /* Xml tag when callback is made */
{
backend_netconf_reg_t *nr;
if ((nr = malloc(sizeof(backend_netconf_reg_t))) == NULL) {
clicon_err(OE_DB, errno, "malloc: %s", strerror(errno));
goto catch;
}
memset (nr, 0, sizeof (*nr));
nr->nr_callback = cb;
nr->nr_arg = arg;
nr->nr_tag = strdup(tag); /* strdup */
INSQ(nr, deps);
return 0;
catch:
if (nr){
if (nr->nr_tag)
free(nr->nr_tag);
free(nr);
}
return -1;
}
/*! See if there is any callback registered for this tag
*
* @param[in] h clicon handle
* @param[in] xn Sub-tree (under xorig) at child of rpc: <rpc><xn></rpc>.
* @param[out] cb Output xml stream. For reply
* @param[out] cb_err Error xml stream. For error reply
* @param[out] xret Return XML, error or OK
*
* @retval -1 Error
* @retval 0 OK, not found handler.
* @retval 1 OK, handler called
*/
int
backend_netconf_plugin_callbacks(clicon_handle h,
cxobj *xe,
struct client_entry *ce,
cbuf *cbret)
{
backend_netconf_reg_t *nreg;
int retval;
if (deps == NULL)
return 0;
nreg = deps;
do {
if (strcmp(nreg->nr_tag, xml_name(xe)) == 0){
if ((retval = nreg->nr_callback(h, xe, ce, cbret, nreg->nr_arg)) < 0)
return -1;
else
return 1; /* handled */
}
nreg = NEXTQ(backend_netconf_reg_t *, nreg);
} while (nreg != deps);
return 0;
}

View file

@ -43,6 +43,14 @@
/*
* Types
*/
struct client_entry;
typedef int (*backend_netconf_cb_t)(
clicon_handle h,
cxobj *xe, /* Request: <rpc><xn></rpc> */
struct client_entry *ce, /* Client session */
cbuf *cbret, /* Reply eg <rpc-reply>... */
void *arg /* Argument given at register */
);
/*! Generic downcall registration.
* Enables any function to be called from (cli) frontend
@ -81,4 +89,13 @@ int subscription_delete(clicon_handle h, char *stream,
struct handle_subscription *subscription_each(clicon_handle h,
struct handle_subscription *hprev);
int backend_netconf_register_callback(clicon_handle h,
backend_netconf_cb_t cb, /* Callback called */
void *arg, /* Arg to send to callback */
char *tag); /* Xml tag when callback is made */
int backend_netconf_plugin_callbacks(clicon_handle h, cxobj *xe,
struct client_entry *ce, cbuf *cbret);
#endif /* _CLIXON_BACKEND_HANDLE_H_ */

View file

@ -74,36 +74,6 @@
#include "cli_common.h"
/*! Initialize candidate database
* We have implemented these:
* shared - all users share a common candidate db
*/
int
init_candidate_db(clicon_handle h)
{
int retval = -1;
if (xmldb_exists(h, "running") != 1){
clicon_err(OE_FATAL, 0, "Running db does not exist");
goto err;
}
if (xmldb_exists(h, "candidate") != 1)
if (clicon_rpc_copy(h, "running", "candidate") < 0)
goto err;
retval = 0;
err:
return retval;
}
/*! Exit candidate db
* (private canddidates should be removed?)
*/
int
exit_candidate_db(clicon_handle h)
{
return 0;
}
/*! Register log notification stream
* @param[in] h Clicon handle
* @param[in] stream Event stream. CLICON is predefined, others are application-defined
@ -114,13 +84,13 @@ exit_candidate_db(clicon_handle h)
* Note this calls cligen_regfd which may callback on cli command interpretator
*/
int
cli_notification_register(clicon_handle h,
char *stream,
cli_notification_register(clicon_handle h,
char *stream,
enum format_enum format,
char *filter,
int status,
int (*fn)(int, void*),
void *arg)
char *filter,
int status,
int (*fn)(int, void*),
void *arg)
{
int retval = -1;
char *logname;
@ -139,10 +109,10 @@ cli_notification_register(clicon_handle h,
if (status){ /* start */
if (s_exist!=-1){
clicon_err(OE_PLUGIN, 0, "%s: result log socket already exists", __FUNCTION__);
clicon_err(OE_PLUGIN, 0, "Result log socket already exists");
goto done;
}
if (clicon_rpc_subscription(h, status, stream, format, filter, &s) < 0)
if (clicon_rpc_create_subscription(h, stream, filter, &s) < 0)
goto done;
if (cligen_regfd(s, fn, arg) < 0)
goto done;
@ -154,9 +124,10 @@ cli_notification_register(clicon_handle h,
cligen_unregfd(s_exist);
}
hash_del(cdat, logname);
if (clicon_rpc_subscription(h, status, stream, format, filter, NULL) < 0)
#if 0 /* cant turn off */
if (clicon_rpc_create_subscription(h, status, stream, format, filter, NULL) < 0)
goto done;
#endif
}
retval = 0;
done:
@ -214,7 +185,7 @@ cli_signal_flush(clicon_handle h)
* @param[in] cvv Vector of cli string and instantiated variables
* @param[in] argv Vector. First element xml key format string, eg "/aaa/%s"
* @param[in] op Operation to perform on database
* Cvv will contain forst the complete cli string, and then a set of optional
* Cvv will contain first the complete cli string, and then a set of optional
* instantiated variables.
* Example:
* cvv[0] = "set interfaces interface eth0 type bgp"
@ -257,7 +228,7 @@ cli_dbxmlv(clicon_handle h,
if (clicon_rpc_change(h, "candidate", op, xk, str) < 0)
goto done;
if (clicon_autocommit(h)) {
if (clicon_rpc_commit(h, "candidate", "running") < 0)
if (clicon_rpc_commit(h) < 0)
goto done;
}
retval = 0;
@ -465,9 +436,7 @@ cli_commitv(clicon_handle h,
{
int retval = -1;
if ((retval = clicon_rpc_commit(h,
"candidate",
"running")) < 0){ /* startup */
if ((retval = clicon_rpc_commit(h)) < 0){ /* startup */
cli_output(stderr, "Commit failed. Edit and try again or discard changes");
goto done;
}
@ -483,10 +452,12 @@ cli_validatev(clicon_handle h,
cvec *vars,
cvec *argv)
{
int retval = -1;
int retval = -1;
if ((retval = clicon_rpc_validate(h, "candidate")) < 0)
clicon_err(OE_CFG, 0, "Validate failed. Edit and try again or discard changes");
goto done;
retval = 0;
done:
return retval;
}
@ -575,9 +546,9 @@ compare_dbsv(clicon_handle h,
astext = cv_int32_get(cvec_i(argv, 0));
else
astext = 0;
if (xmldb_get(h, "running", "/", &xc1, NULL, NULL) < 0)
if (clicon_rpc_get_config(h, "running", "/", &xc1) < 0)
goto done;
if (xmldb_get(h, "candidate", "/", &xc2, NULL, NULL) < 0)
if (clicon_rpc_get_config(h, "candidate", "/", &xc2) < 0)
goto done;
if (compare_xmls(xc1, xc2, astext) < 0) /* astext? */
goto done;
@ -620,7 +591,6 @@ load_config_filev(clicon_handle h,
char *varstr;
int fd = -1;
cxobj *xt = NULL;
cxobj *xn;
cxobj *x;
cbuf *cbxml;
@ -663,20 +633,25 @@ load_config_filev(clicon_handle h,
}
if (clicon_xml_parse_file(fd, &xt, "</clicon>") < 0)
goto done;
if ((xn = xml_child_i(xt, 0)) != NULL){
if (xt == NULL)
goto done;
/* Ensure top-level is "config", maybe this is too rough? */
xml_name_set(xt, "config");
// if ((xn = xml_child_i(xt, 0)) != NULL){
if ((cbxml = cbuf_new()) == NULL)
goto done;
x = NULL;
while ((x = xml_child_each(xn, x, -1)) != NULL)
while ((x = xml_child_each(xt, x, -1)) != NULL)
if (clicon_xml2cbuf(cbxml, x, 0, 0) < 0)
goto done;
if (clicon_rpc_xmlput(h, "candidate",
replace?OP_REPLACE:OP_MERGE,
"",
cbuf_get(cbxml)) < 0)
if (clicon_rpc_edit_config(h, "candidate",
replace?OP_REPLACE:OP_MERGE,
"",
cbuf_get(cbxml)) < 0)
goto done;
cbuf_free(cbxml);
}
// }
ret = 0;
done:
unchunk_group(__FUNCTION__);
@ -741,13 +716,13 @@ save_config_filev(clicon_handle h,
goto done;
}
filename = vecp[0];
if (xmldb_get(h, dbstr, "/", &xt, NULL, NULL) < 0)
if (clicon_rpc_get_config(h, dbstr,"/", &xt) < 0)
goto done;
if ((f = fopen(filename, "wb")) == NULL){
clicon_err(OE_CFG, errno, "Creating file %s", filename);
goto done;
}
if (xml_print(f, xt) < 0)
if (clicon_xml2file(f, xt, 0, 0) < 0)
goto done;
retval = 0;
/* Fall through */
@ -782,10 +757,8 @@ delete_allv(clicon_handle h,
clicon_err(OE_PLUGIN, 0, "No such db name: %s", dbstr);
goto done;
}
if (clicon_rpc_change(h, "candidate",
OP_REMOVE,
"/", "") < 0)
goto done;
if (clicon_rpc_delete_config(h, dbstr) < 0)
goto done;
retval = 0;
done:
return retval;
@ -798,7 +771,7 @@ discard_changesv(clicon_handle h,
cvec *cvv,
cvec *argv)
{
return clicon_rpc_copy(h, "running", "candidate");
return clicon_rpc_copy_config(h, "running", "candidate");
}
/*! Copy from one database to another, eg running->startup
@ -814,7 +787,7 @@ db_copy(clicon_handle h,
db1 = cv_string_get(cvec_i(argv, 0));
db2 = cv_string_get(cvec_i(argv, 1));
return clicon_rpc_copy(h, db1, db2);
return clicon_rpc_copy_config(h, db1, db2);
}
/* These are strings that can be used as 3rd argument to cli_setlog */
@ -831,18 +804,18 @@ static int
cli_notification_cb(int s,
void *arg)
{
struct clicon_msg *reply;
struct clicon_msg *reply = NULL;
enum clicon_msg_type type;
int eof;
int retval = -1;
char *eventstr = NULL;
int level;
cxobj *xt = NULL;
cxobj *xn;
cxobj *xe;
char *format = (char*)arg;
/* get msg (this is the reason this function is called) */
if (clicon_msg_rcv(s, &reply, &eof, __FUNCTION__) < 0)
if (clicon_msg_rcv(s, &reply, &eof) < 0)
goto done;
if (eof){
clicon_err(OE_PROTO, ESHUTDOWN, "%s: Socket unexpected close", __FUNCTION__);
@ -855,11 +828,13 @@ cli_notification_cb(int s,
goto done;
type = ntohs(reply->op_type);
switch (type){
case CLICON_MSG_NOTIFY:
if (clicon_msg_notify_decode(reply, &level, &eventstr, __FUNCTION__) < 0)
case CLICON_MSG_NETCONF:
if (clicon_msg_netconf_decode(reply, &xt) < 0)
goto done;
if ((xe = xpath_first(xt, "//event")) != NULL)
eventstr = xml_body(xe);
if (strcmp(format, SHOWAS_TXT) == 0){
fprintf(stdout, "%s", eventstr);
fprintf(stdout, "%s\n", eventstr);
}
else
if (strcmp(format, SHOWAS_XML) == 0){
@ -871,25 +846,20 @@ cli_notification_cb(int s,
}
else
if (strcmp(format, SHOWAS_XML2TXT) == 0){
if (clicon_xml_parse_string(&eventstr, &xt) < 0)
goto done;
if ((xn = xml_child_i(xt, 0)) != NULL)
if ((xn = xml_child_i(xe, 0)) != NULL)
if (xml2txt(stdout, xn, 0) < 0)
goto done;
}
else
if (strcmp(format, SHOWAS_XML2JSON) == 0){
if (clicon_xml_parse_string(&eventstr, &xt) < 0)
goto done;
if ((xn = xml_child_i(xt, 0)) != NULL){
if ((xn = xml_child_i(xe, 0)) != NULL){
if (xml2json(stdout, xn, 0) < 0)
goto done;
}
}
break;
default:
clicon_err(OE_PROTO, 0, "%s: unexpected reply: %d",
__FUNCTION__, type);
clicon_err(OE_PROTO, 0, "unexpected reply: %d", type);
goto done;
break;
}
@ -897,15 +867,15 @@ cli_notification_cb(int s,
done:
if (xt)
xml_free(xt);
unchunk_group(__FUNCTION__); /* event allocated by chunk */
if (reply)
free(reply);
return retval;
}
/*! Make a notify subscription to backend and un/register callback for return messages.
*
* @param[in] h Clicon handle
* @param[in] cvv Not used
* @param[in] cvv Not used
* @param[in] arg A string with <log stream name> <stream status> [<format>]
* where <status> is "0" or "1"
* and <format> is XXX
@ -943,7 +913,7 @@ cli_notifyv(clicon_handle h,
"",
status,
cli_notification_cb,
(void*)formatstr) < 0)
(void*)strdup(formatstr)) < 0)
goto done;
retval = 0;
@ -951,6 +921,67 @@ cli_notifyv(clicon_handle h,
return retval;
}
/*! Lock database
*
* @param[in] h Clicon handle
* @param[in] cvv Not used
* @param[in] arg A string with <database>
* @code
* lock("comment"), cli_lock("running");
* @endcode
* XXX: format is a memory leak
*/
int
cli_lock(clicon_handle h,
cvec *cvv,
cvec *argv)
{
char *db;
int retval = -1;
if (cvec_len(argv) != 1){
clicon_err(OE_PLUGIN, 0, "Requires arguments: <db>");
goto done;
}
db = cv_string_get(cvec_i(argv, 0));
if (clicon_rpc_lock(h, db) < 0)
goto done;
retval = 0;
done:
return retval;
}
/*! Unlock database
*
* @param[in] h Clicon handle
* @param[in] cvv Not used
* @param[in] arg A string with <database>
* @code
* lock("comment"), cli_lock("running");
* @endcode
* XXX: format is a memory leak
*/
int
cli_unlock(clicon_handle h,
cvec *cvv,
cvec *argv)
{
char *db;
int retval = -1;
if (cvec_len(argv) != 1){
clicon_err(OE_PLUGIN, 0, "Requires arguments: <db>");
goto done;
}
db = cv_string_get(cvec_i(argv, 0));
if (clicon_rpc_unlock(h, db) < 0)
goto done;
retval = 0;
done:
return retval;
}
/* Here are backward compatible cligen callback functions used when
* the option: CLICON_CLIGEN_CALLBACK_SINGLE_ARG is set.
*/
@ -1076,8 +1107,6 @@ save_config_file(clicon_handle h,
return retval;
}
int
cli_notify(clicon_handle h,
cvec *cvv,
@ -1126,15 +1155,15 @@ cli_notify(clicon_handle h,
return retval;
}
/*
* cli_debug
* set debug level on stderr (not syslog).
/*! set debug level on stderr (not syslog).
* The level is either what is specified in arg as int argument.
* _or_ if a 'level' variable is present in vars use that value instead.
* XXX obsolete. Use cli_debug_cliv or cli_debug_backendv instead
*/
int
cli_debug(clicon_handle h, cvec *vars, cg_var *arg)
cli_debug(clicon_handle h,
cvec *vars,
cg_var *arg)
{
cg_var *cv;
int level;

View file

@ -77,10 +77,10 @@ cli_terminate(clicon_handle h)
{
yang_spec *yspec;
clicon_rpc_close_session(h);
if ((yspec = clicon_dbspec_yang(h)) != NULL)
yspec_free(yspec);
cli_plugin_finish(h);
exit_candidate_db(h);
cli_handle_exit(h);
return 0;
}
@ -359,21 +359,12 @@ main(int argc, char **argv)
goto done;
}
/* A client does not have access to the candidate (and running)
databases if both these conditions are true:
1. clicon_sock_family(h) == AF_INET[6]
*/
if (clicon_sock_family(h) == AF_UNIX)
if (init_candidate_db(h) < 0)
return -1;
if (logclisyntax)
cli_logsyntax_set(h, logclisyntax);
if (debug)
clicon_option_dump(h, debug);
/* Join rest of argv to a single command */
restarg = clicon_strjoin(argc, argv, " ", __FUNCTION__);

View file

@ -142,12 +142,14 @@ expandv_dbvar(void *h,
*/
if (xmlkeyfmt2xpath(xkfmt, cvv, &xkpath) < 0)
goto done;
if (xmldb_get(h, dbstr, xkpath, &xt, &xvec, &xlen) < 0)
goto done;
if (clicon_rpc_get_config(h, dbstr, "/", &xt) < 0)
goto done;
/* One round to detect duplicates
* XXX The code below would benefit from some cleanup
*/
j = 0;
if (xpath_vec(xt, xkpath, &xvec, &xlen) < 0)
goto done;
for (i = 0; i < xlen; i++) {
char *str;
x = xvec[i];
@ -187,7 +189,6 @@ expandv_dbvar(void *h,
}
retval = 0;
done:
unchunk_group(__FUNCTION__);
if (xvec)
free(xvec);
if (xt)
@ -474,7 +475,7 @@ show_confv_as(clicon_handle h,
}
else
cprintf(cbx, "%s", xpath);
if (xmldb_get(h, db, cbuf_get(cbx), xt, NULL, NULL) < 0)
if (clicon_rpc_get_config(h, db, cbuf_get(cbx), xt) < 0)
goto done;
retval = 0;
done:
@ -482,7 +483,6 @@ done:
free(val);
if (cbx)
cbuf_free(cbx);
unchunk_group(__FUNCTION__);
return retval;
}
@ -597,7 +597,6 @@ show_confv_as_text1(clicon_handle h,
done:
if (xt)
xml_free(xt);
unchunk_group(__FUNCTION__);
return retval;
}
@ -732,7 +731,9 @@ show_confv_xpath(clicon_handle h,
}
cv = cvec_find_var(cvv, "xpath");
xpath = cv_string_get(cv);
if (xmldb_get(h, str, xpath, &xt, &xv, &xlen) < 0)
if (clicon_rpc_get_config(h, str, xpath, &xt) < 0)
goto done;
if (xpath_vec(xt, xpath, &xv, &xlen) < 0)
goto done;
for (i=0; i<xlen; i++)
xml_print(stdout, xv[i]);
@ -743,7 +744,6 @@ done:
free(xv);
if (xt)
xml_free(xt);
unchunk_group(__FUNCTION__);
return retval;
}
@ -806,7 +806,9 @@ expand_dbvar(void *h,
*/
if (xmlkeyfmt2xpath(xkfmt, cvv, &xkpath) < 0)
goto done;
if (xmldb_get(h, dbstr, xkpath, &xt, &xvec, &xlen) < 0)
if (clicon_rpc_get_config(h, dbstr, "/", &xt) < 0)
goto done;
if (xpath_vec(xt, xkpath, &xvec, &xlen) < 0)
goto done;
/* One round to detect duplicates
* XXX The code below would benefit from some cleanup
@ -948,7 +950,7 @@ show_conf_as(clicon_handle h,
}
else
cprintf(cbx, "%s", xpath);
if (xmldb_get(h, db, cbuf_get(cbx), xt, NULL, NULL) < 0)
if (clicon_rpc_get_config(h, db, cbuf_get(cbx), xt) < 0)
goto done;
retval = 0;
done:
@ -1160,51 +1162,3 @@ show_conf_as_csv(clicon_handle h, cvec *cvv, cg_var *arg)
return show_conf_as_csv1(h, cvv, arg);
}
/*! Show configuration as text given an xpath
* Utility function used by cligen spec file
* @param[in] h CLICON handle
* @param[in] cvv Vector of variables from CLIgen command-line
* @param[in] arg A string: <dbname> <xpath>
* @note Hardcoded that a variable in cvv is named "xpath"
*/
int
show_conf_xpath(clicon_handle h,
cvec *cvv,
cg_var *arg)
{
int retval = -1;
char *str;
char *xpath;
cg_var *cv;
cxobj *xt = NULL;
cxobj **xv = NULL;
size_t xlen;
int i;
if (arg == NULL || (str = cv_string_get(arg)) == NULL){
clicon_err(OE_PLUGIN, 0, "%s: requires string argument", __FUNCTION__);
goto done;
}
/* Dont get attr here, take it from arg instead */
if (strcmp(str, "running") != 0 &&
strcmp(str, "candidate") != 0 &&
strcmp(str, "startup") != 0){
clicon_err(OE_PLUGIN, 0, "No such db name: %s", str);
goto done;
}
cv = cvec_find_var(cvv, "xpath");
xpath = cv_string_get(cv);
if (xmldb_get(h, str, xpath, &xt, &xv, &xlen) < 0)
goto done;
for (i=0; i<xlen; i++)
xml_print(stdout, xv[i]);
retval = 0;
done:
if (xv)
free(xv);
if (xt)
xml_free(xt);
unchunk_group(__FUNCTION__);
return retval;
}

View file

@ -68,8 +68,6 @@ int cli_handle_exit(clicon_handle h);
cligen_handle cli_cligen(clicon_handle h);
/* cli_common.c */
int init_candidate_db(clicon_handle h);
int exit_candidate_db(clicon_handle h);
int cli_notification_register(clicon_handle h, char *stream, enum format_enum format,
char *filter, int status,
int (*fn)(int, void*), void *arg);
@ -92,6 +90,8 @@ int save_config_filev(clicon_handle h, cvec *vars, cvec *argv);
int delete_allv(clicon_handle h, cvec *vars, cvec *argv);
int discard_changesv(clicon_handle h, cvec *vars, cvec *argv);
int cli_notifyv(clicon_handle h, cvec *cvv, cvec *argv);
int cli_lock(clicon_handle h, cvec *cvv, cvec *argv);
int cli_unlock(clicon_handle h, cvec *cvv, cvec *argv);
/* cli_common.c: CLIgen old single arg callbacks */
int cli_set(clicon_handle h, cvec *vars, cg_var *arg);

View file

@ -209,21 +209,20 @@ main(int argc, char **argv)
}
if (dumpdb){
/* Here db must be local file-path */
if (xmldb_dump_local(stdout, db, matchkey)) {
if (xmldb_dump(stdout, db, matchkey)) {
fprintf(stderr, "Match error\n");
goto done;
}
}
if (addent) /* add entry */
if (xmldb_put_xkey(h, db, addstr, NULL, OP_REPLACE) < 0)
if (xmldb_put_xkey(h, db, OP_REPLACE, NULL, addstr) < 0)
goto done;
if (rment)
if (remove_entry(db, rmkey) < 0)
goto done;
if (zapdb) /* remove databases */
/* XXX This assumes direct access to database */
if (xmldb_delete(h, db) < 0){
clicon_err(OE_FATAL, errno, "xmldb_delete %s", db);
clicon_err(OE_FATAL, errno, "delete %s", db);
goto done;
}
if (initdb)

View file

@ -44,11 +44,9 @@
*/
typedef int (*netconf_cb_t)(
clicon_handle h,
cxobj *xorig, /* Original request. */
cxobj *xn, /* Sub-tree (under xorig) at child: <rpc><xn></rpc> */
cbuf *cb, /* Output xml stream. For reply */
cbuf *cb_err, /* Error xml stream. For error reply */
void *arg /* Argument given at netconf_register_callback() */
cxobj *xn, /* Request: <rpc><xn></rpc> */
cxobj **xret, /* Return xml tree, eg <rpc-reply>... */
void *arg /* Argument given at netconf_register_callback() */
);
/*
@ -57,25 +55,10 @@ typedef int (*netconf_cb_t)(
*/
int netconf_output(int s, cbuf *xf, char *msg);
int netconf_create_rpc_reply(cbuf *cb, /* msg buffer */
cxobj *xr, /* orig request */
char *body,
int ok);
int netconf_register_callback(clicon_handle h,
netconf_cb_t cb, /* Callback called */
void *arg, /* Arg to send to callback */
char *tag); /* Xml tag when callback is made */
int netconf_create_rpc_error(cbuf *xf, /* msg buffer */
cxobj *xr, /* orig request */
char *tag,
char *type,
char *severity,
char *message,
char *info);
void netconf_ok_set(int ok);
int netconf_ok_get(void);
int netconf_xpath(cxobj *xsearch,
cxobj *xfilter,

View file

@ -65,9 +65,7 @@
#include "netconf_lib.h"
#include "netconf_filter.h"
/*
* xml_filter
* xf specifices a filter, and xn is an xml tree.
/* xf specifices a filter, and xn is an xml tree.
* Select the part of xn that matches xf and return it.
* Change xn destructively by removing the parts of the sub-tree that does
* not match.
@ -107,9 +105,9 @@ leafstring(cxobj *x)
* @retval -1 Error
*/
static int
xml_filter2(cxobj *xfilter,
cxobj *xparent,
int *remove_me)
xml_filter_recursive(cxobj *xfilter,
cxobj *xparent,
int *remove_me)
{
cxobj *s;
cxobj *sprev;
@ -176,7 +174,7 @@ xml_filter2(cxobj *xfilter,
}
// XXX: s can be removed itself in the recursive call !
remove_s = 0;
if (xml_filter2(f, s, &remove_s) < 0)
if (xml_filter_recursive(f, s, &remove_s) < 0)
return -1;
if (remove_s){
xml_purge(s);
@ -207,9 +205,9 @@ xml_filter(cxobj *xfilter,
int remove_s;
/* Call recursive variant */
retval = xml_filter2(xfilter,
xconfig,
&remove_s);
retval = xml_filter_recursive(xfilter,
xconfig,
&remove_s);
return retval;
}

View file

@ -68,22 +68,8 @@
/*
* Exported variables
*/
enum transport_type transport = NETCONF_SSH;
int cc_closed = 0;
static int cc_ok = 0;
void
netconf_ok_set(int ok)
{
cc_ok = ok;
}
int
netconf_ok_get(void)
{
return cc_ok;
}
enum transport_type transport = NETCONF_SSH; /* XXX Remove SOAP support */
int cc_closed = 0; /* XXX Please remove (or at least hide in handle) this global variable */
int
add_preamble(cbuf *xf)
@ -191,8 +177,7 @@ detect_endtag(char *tag, char ch, int *state)
* @retval dbname Actual database file name
*/
char *
netconf_get_target(clicon_handle h,
cxobj *xn,
netconf_get_target(cxobj *xn,
char *path)
{
cxobj *x;
@ -218,11 +203,13 @@ netconf_get_target(clicon_handle h,
* @param[in] msg Only for debug
*/
int
netconf_output(int s, cbuf *xf, char *msg)
netconf_output(int s,
cbuf *xf,
char *msg)
{
char *buf = cbuf_get(xf);
int len = cbuf_len(xf);
int retval = -1;
int len = cbuf_len(xf);
int retval = -1;
clicon_debug(1, "SEND %s", msg);
if (debug > 1){ /* XXX: below only works to stderr, clicon_debug may log to syslog */

View file

@ -60,11 +60,6 @@ enum error_option{ /* edit-config */
CONTINUE_ON_ERROR
};
enum filter_option{ /* get-config/filter */
FILTER_SUBTREE,
FILTER_XPATH
};
/*
* Variables
*/
@ -74,14 +69,11 @@ extern int cc_closed;
/*
* Prototypes
*/
void netconf_ok_set(int ok);
int netconf_ok_get(void);
int add_preamble(cbuf *xf);
int add_postamble(cbuf *xf);
int add_error_preamble(cbuf *xf, char *reason);
int detect_endtag(char *tag, char ch, int *state);
char *netconf_get_target(clicon_handle h, cxobj *xn, char *path);
char *netconf_get_target(cxobj *xn, char *path);
int add_error_postamble(cbuf *xf);
int netconf_output(int s, cbuf *xf, char *msg);

View file

@ -82,11 +82,12 @@ process_incoming_packet(clicon_handle h,
{
char *str;
char *str0;
cxobj *xml_req = NULL; /* Request (in) */
cxobj *xreq = NULL; /* Request (in) */
int isrpc = 0; /* either hello or rpc */
cbuf *xf_out;
cbuf *xf_err;
cbuf *xf1;
cbuf *cbret = NULL;
cxobj *xret = NULL; /* Return (out) */
cxobj *xrpc;
cxobj *xc;
clicon_debug(1, "RECV");
clicon_debug(2, "%s: RCV: \"%s\"", __FUNCTION__, cbuf_get(cb));
@ -96,16 +97,15 @@ process_incoming_packet(clicon_handle h,
}
str = str0;
/* Parse incoming XML message */
if (clicon_xml_parse_string(&str, &xml_req) < 0){
if ((cb = cbuf_new()) == NULL){
netconf_create_rpc_error(cb, NULL,
"operation-failed",
"rpc", "error",
NULL,
NULL);
if (clicon_xml_parse_string(&str, &xreq) < 0){
if ((cbret = cbuf_new()) == NULL){
cprintf(cbret, "<rpc-reply><rpc-error>"
"<error-tag>operation-failed</error-tag>"
"<error-type>rpc</error-type>"
"<error-severity>error</error-severity>"
"<error-message>internal error</error-message>"
"</rpc-error></rpc-reply>");
netconf_output(1, cb, "rpc-error");
cbuf_free(cb);
}
else
clicon_log(LOG_ERR, "%s: cbuf_new", __FUNCTION__);
@ -113,72 +113,56 @@ process_incoming_packet(clicon_handle h,
goto done;
}
free(str0);
if (xpath_first(xml_req, "//rpc") != NULL){
if ((xrpc=xpath_first(xreq, "//rpc")) != NULL){
isrpc++;
}
else
if (xpath_first(xml_req, "//hello") != NULL)
if (xpath_first(xreq, "//hello") != NULL)
;
else{
clicon_log(LOG_WARNING, "Invalid netconf msg: neither rpc or hello: dropp\
ed");
goto done;
}
/* Initialize response buffers */
if ((xf_out = cbuf_new()) == NULL){
clicon_log(LOG_ERR, "%s: cbuf_new", __FUNCTION__);
goto done;
if (!isrpc){ /* hello */
if (netconf_hello_dispatch(xreq) < 0)
goto done;
}
else /* rpc */
if (netconf_rpc_dispatch(h, xrpc, &xret) < 0){
goto done;
}
else{ /* there is a return message in xret */
cxobj *xa, *xa2;
assert(xret);
/* Create error buf */
if ((xf_err = cbuf_new()) == NULL){
clicon_log(LOG_ERR, "%s: cbuf_new", __FUNCTION__);
goto done;
}
netconf_ok_set(0);
if (isrpc){
if (netconf_rpc_dispatch(h,
xml_req,
xpath_first(xml_req, "//rpc"),
xf_out, xf_err) < 0){
assert(cbuf_len(xf_err));
clicon_debug(1, "%s", cbuf_get(xf_err));
if (isrpc){
if (netconf_output(1, xf_err, "rpc-error") < 0)
goto done;
if ((cbret = cbuf_new()) != NULL){
if ((xc = xml_child_i(xret,0))!=NULL){
xa=NULL;
while ((xa = xml_child_each(xrpc, xa, CX_ATTR)) != NULL){
if ((xa2 = xml_dup(xa)) ==NULL)
goto done;
if (xml_addsub(xc, xa2) < 0)
goto done;
}
add_preamble(cbret);
clicon_xml2cbuf(cbret, xml_child_i(xret,0), 0, 0);
add_postamble(cbret);
if (netconf_output(1, cbret, "rpc-reply") < 0){
cbuf_free(cbret);
goto done;
}
}
}
}
else{
if ((xf1 = cbuf_new()) != NULL){
if (netconf_create_rpc_reply(xf1, xml_req, cbuf_get(xf_out), netconf_ok_get()) < 0){
cbuf_free(xf_out);
cbuf_free(xf_err);
cbuf_free(xf1);
goto done;
}
if (netconf_output(1, xf1, "rpc-reply") < 0){
cbuf_reset(xf1);
netconf_create_rpc_error(xf1, xml_req, "operation-failed",
"protocol", "error",
NULL, cbuf_get(xf_err));
netconf_output(1, xf1, "rpc-error");
cbuf_free(xf_out);
cbuf_free(xf_err);
cbuf_free(xf1);
goto done;
}
cbuf_free(xf1);
}
}
}
else{
netconf_hello_dispatch(xml_req); /* XXX: return-value */
}
cbuf_free(xf_out);
cbuf_free(xf_err);
done:
if (xml_req)
xml_free(xml_req);
if (xreq)
xml_free(xreq);
if (xret)
xml_free(xret);
if (cbret)
cbuf_free(cbret);
return 0;
}
@ -268,28 +252,12 @@ send_hello(int s)
return retval;
}
/*! Initialize candidate database */
static int
init_candidate_db(clicon_handle h)
{
int retval = -1;
/* init shared candidate */
if (xmldb_exists(h, "candidate") != 1){
if (xmldb_copy(h, "running", "candidate") < 0)
goto done;
}
retval = 0;
done:
unchunk_group(__FUNCTION__);
return retval;
}
static int
terminate(clicon_handle h)
netconf_terminate(clicon_handle h)
{
yang_spec *yspec;
clicon_rpc_close_session(h);
if ((yspec = clicon_dbspec_yang(h)) != NULL)
yspec_free(yspec);
clicon_handle_exit(h);
@ -402,8 +370,6 @@ main(int argc, char **argv)
if (netconf_plugin_load(h) < 0)
return -1;
if (init_candidate_db(h) < 0)
return -1;
/* Call start function is all plugins before we go interactive */
tmp = *(argv-1);
*(argv-1) = argv0;
@ -416,13 +382,11 @@ main(int argc, char **argv)
goto done;
if (debug)
clicon_option_dump(h, debug);
if (event_loop() < 0)
goto done;
done:
netconf_plugin_unload(h);
terminate(h);
netconf_terminate(h);
clicon_log_init(__PROGRAM__, LOG_INFO, 0); /* Log on syslog no stderr */
clicon_log(LOG_NOTICE, "%s: %u Terminated\n", __PROGRAM__, getpid());
return 0;

View file

@ -66,6 +66,15 @@
#include "netconf_lib.h"
#include "netconf_plugin.h"
/* Database dependency description */
struct netconf_reg {
qelem_t nr_qelem; /* List header */
netconf_cb_t nr_callback; /* Validation/Commit Callback */
void *nr_arg; /* Application specific argument to cb */
char *nr_tag; /* Xml tag when matched, callback called */
};
typedef struct netconf_reg netconf_reg_t;
/*! Unload a plugin
*/
static int
@ -245,10 +254,11 @@ catch:
/*! See if there is any callback registered for this tag
*
* @param xn Sub-tree (under xorig) at child of rpc: <rpc><xn></rpc>.
* @param xf Output xml stream. For reply
* @param xf_err Error xml stream. For error reply
* @param xorig Original request.
* @param[in] h clicon handle
* @param[in] xn Sub-tree (under xorig) at child of rpc: <rpc><xn></rpc>.
* @param[out] cb Output xml stream. For reply
* @param[out] cb_err Error xml stream. For error reply
* @param[out] xret Return XML, error or OK
*
* @retval -1 Error
* @retval 0 OK, not found handler.
@ -256,31 +266,24 @@ catch:
*/
int
netconf_plugin_callbacks(clicon_handle h,
cxobj *xn,
cbuf *xf,
cbuf *xf_err,
cxobj *xorig)
cxobj *xn,
cxobj **xret)
{
netconf_reg_t *nr;
netconf_reg_t *nreg;
int retval;
if (deps == NULL)
return 0;
nr = deps;
nreg = deps;
do {
if (strcmp(nr->nr_tag, xml_name(xn)) == 0){
if ((retval = nr->nr_callback(h,
xorig,
xn,
xf,
xf_err,
nr->nr_arg)) < 0)
if (strcmp(nreg->nr_tag, xml_name(xn)) == 0){
if ((retval = nreg->nr_callback(h, xn, xret, nreg->nr_arg)) < 0)
return -1;
else
return 1; /* handled */
}
nr = NEXTQ(netconf_reg_t *, nr);
} while (nr != deps);
nreg = NEXTQ(netconf_reg_t *, nreg);
} while (nreg != deps);
return 0;
}

View file

@ -41,14 +41,6 @@
* Types
*/
/* Database dependency description */
struct netconf_reg {
qelem_t nr_qelem; /* List header */
netconf_cb_t nr_callback; /* Validation/Commit Callback */
void *nr_arg; /* Application specific argument to cb */
char *nr_tag; /* Xml tag when matched, callback called */
};
typedef struct netconf_reg netconf_reg_t;
/*
* Prototypes
@ -59,12 +51,6 @@ int netconf_plugin_start(clicon_handle h, int argc, char **argv);
int netconf_plugin_unload(clicon_handle h);
int netconf_plugin_callbacks(clicon_handle h,
// dbspec_key *dbspec,
cxobj *xn,
cbuf *xf,
cbuf *xf_err,
cxobj *xt);
int netconf_plugin_callbacks(clicon_handle h, cxobj *xn, cxobj **xret);
#endif /* _NETCONF_PLUGIN_H_ */

File diff suppressed because it is too large Load diff

View file

@ -42,20 +42,7 @@
*/
int
netconf_rpc_dispatch(clicon_handle h,
cxobj *xorig,
cxobj *xn,
cbuf *xf,
cbuf *xf_err);
int netconf_create_rpc_reply(cbuf *xf, /* msg buffer */
cxobj *xr, /* orig request */
char *body, int ok);
int netconf_create_rpc_error(cbuf *xf, /* msg buffer */
cxobj *xr, /* orig request */
char *tag,
char *type,
char *severity,
char *message,
char *info);
cxobj *xn,
cxobj **xret);
#endif /* _NETCONF_RPC_H_ */

View file

@ -54,6 +54,9 @@ olof@vandal> curl -G http://127.0.0.1/restconf/data/interfaces/interface/name=et
}
]
curl -sX POST -d '{"clicon":{"interfaces":{"interface":{"name":"eth1","type":"eth","enabled":"true"}}}}' http://localhost/restconf/data
Debugging
---------
Start the restconf programs with debug flag:

View file

@ -141,16 +141,15 @@ api_data_get(clicon_handle h,
int i;
cbuf *path = NULL;
cbuf *path1 = NULL;
cxobj *xt = NULL;
cbuf *cbx = NULL;
cxobj **vec = NULL;
size_t veclen;
yang_spec *yspec;
yang_stmt *y;
yang_stmt *ykey;
char *name;
cvec *cvk = NULL; /* vector of index keys */
cg_var *cvi;
cxobj *xret = NULL;
clicon_debug(1, "%s", __FUNCTION__);
yspec = clicon_dbspec_yang(h);
@ -217,32 +216,34 @@ api_data_get(clicon_handle h,
}
}
clicon_debug(1, "%s path:%s", __FUNCTION__, cbuf_get(path));
if (xmldb_get(h, "running", cbuf_get(path), &xt, &vec, &veclen) < 0)
goto done;
if ((cbx = cbuf_new()) == NULL)
goto done;
if (veclen==0){
if (clicon_rpc_get_config(h, "running", cbuf_get(path), &xret) < 0){
notfound(r);
goto done;
}
if ((cbx = cbuf_new()) == NULL)
goto done;
FCGX_SetExitStatus(200, r->out); /* OK */
FCGX_FPrintF(r->out, "Content-Type: application/yang.data+json\r\n");
FCGX_FPrintF(r->out, "\r\n");
if (xml2json_cbuf_vec(cbx, vec, veclen, 0) < 0)
clicon_debug(1, "%s name:%s child:%d", __FUNCTION__, xml_name(xret), xml_child_nr(xret));
vec = xml_childvec_get(xret);
if (xml2json_cbuf_vec(cbx, vec, xml_child_nr(xret), 0) < 0)
goto done;
clicon_debug(1, "%s cbuf:%s", __FUNCTION__, cbuf_get(cbx));
FCGX_FPrintF(r->out, "%s", cbuf_get(cbx));
FCGX_FPrintF(r->out, "\r\n\r\n");
retval = 0;
done:
clicon_debug(1, "%s retval:%d", __FUNCTION__, retval);
if (cbx)
cbuf_free(cbx);
if (xt)
xml_free(xt);
if (path)
cbuf_free(path);
if (path1)
cbuf_free(path1);
if (xret)
xml_free(xret);
return retval;
}
@ -266,14 +267,14 @@ api_data_delete(clicon_handle h,
clicon_debug(1, "%s api_path:%s", __FUNCTION__, api_path);
for (i=0; i<pi; i++)
api_path = index(api_path+1, '/');
/* Parse input data as json into xml */
if (clicon_rpc_xmlput(h, "candidate",
OP_REMOVE,
api_path,
"") < 0)
if (clicon_rpc_edit_config(h, "candidate",
OP_REMOVE,
api_path,
"<config/>") < 0){
notfound(r);
goto done;
if (clicon_rpc_commit(h, "candidate", "running") < 0)
}
if (clicon_rpc_commit(h) < 0)
goto done;
FCGX_SetExitStatus(201, r->out);
FCGX_FPrintF(r->out, "Content-Type: text/plain\r\n");
@ -338,16 +339,23 @@ api_data_put(clicon_handle h,
}
if ((cbx = cbuf_new()) == NULL)
goto done;
cprintf(cbx, "<config>");
x = NULL;
while ((x = xml_child_each(xdata, x, -1)) != NULL)
while ((x = xml_child_each(xdata, x, -1)) != NULL) {
if (clicon_xml2cbuf(cbx, x, 0, 0) < 0)
goto done;
if (clicon_rpc_xmlput(h, "candidate",
OP_MERGE,
api_path,
cbuf_get(cbx)) < 0)
}
cprintf(cbx, "</config>");
clicon_debug(1, "%s cbx: %s api_path:%s",__FUNCTION__, cbuf_get(cbx), api_path);
if (clicon_rpc_edit_config(h, "candidate",
OP_MERGE,
api_path,
cbuf_get(cbx)) < 0){
notfound(r);
goto done;
if (clicon_rpc_commit(h, "candidate", "running") < 0)
}
if (clicon_rpc_commit(h) < 0)
goto done;
FCGX_SetExitStatus(201, r->out); /* Created */
FCGX_FPrintF(r->out, "Content-Type: text/plain\r\n");
@ -437,7 +445,12 @@ request_process(clicon_handle h,
clicon_debug(1, "DATA=%s", data);
if (str2cvec(data, '&', '=', &dvec) < 0)
goto done;
method = pvec[2];
if ((method = pvec[2]) == NULL){
retval = notfound(r);
goto done;
}
retval = 0;
test(r, 1);
/* If present, check credentials */
@ -448,6 +461,7 @@ request_process(clicon_handle h,
if (auth == 0)
goto done;
clicon_debug(1, "%s credentials ok 2", __FUNCTION__);
clicon_debug(1, "%s credentials ok 3", __FUNCTION__);
if (strcmp(method, "data") == 0) /* restconf, skip /api/data */
retval = api_data(h, r, path, pcvec, 2, qvec, data);
@ -469,6 +483,18 @@ request_process(clicon_handle h,
return retval;
}
static int
restconf_terminate(clicon_handle h)
{
yang_spec *yspec;
clicon_rpc_close_session(h);
if ((yspec = clicon_dbspec_yang(h)) != NULL)
yspec_free(yspec);
clicon_handle_exit(h);
return 0;
}
/*! Usage help routine
* @param[in] argv0 command line
* @param[in] h Clicon handle
@ -592,5 +618,6 @@ main(int argc,
retval = 0;
done:
restconf_plugin_unload(h);
restconf_terminate(h);
return retval;
}

View file

@ -1,108 +0,0 @@
#
# ***** BEGIN LICENSE BLOCK *****
#
# Copyright (C) 2009-2017 Olof Hagsand and Benny Holmgren
#
# 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 *****
#
VPATH = @srcdir@
srcdir = @srcdir@
top_srcdir = @top_srcdir@
CC = @CC@
CFLAGS = @CFLAGS@
LDFLAGS = @LDFLAGS@
prefix = @prefix@
exec_prefix = @exec_prefix@
bindir = @bindir@
libexecdir = @libexecdir@
localstatedir = @localstatedir@
sysconfdir = @sysconfdir@
SH_SUFFIX = @SH_SUFFIX@
CLIXON_MAJOR = @CLIXON_VERSION_MAJOR@
CLIXON_MINOR = @CLIXON_VERSION_MINOR@
# Use this clixon lib for linking
CLIXON_LIB = libclixon.so.$(CLIXON_MAJOR).$(CLIXON_MINOR)
# For dependency
LIBDEPS = $(top_srcdir)/lib/src/$(CLIXON_LIB)
LIBS = -L$(top_srcdir)/lib/src @LIBS@ -l:$(CLIXON_LIB)
CPPFLAGS = @CPPFLAGS@
INCLUDES = -I. -I$(top_srcdir)/lib/src -I$(top_srcdir)/lib -I$(top_srcdir)/include -I$(top_srcdir) @INCLUDES@
SRC =
OBJS = $(SRC:.c=.o)
APPSRC = xmldb_main.c
APPOBJ = $(APPSRC:.c=.o)
APPL = clixon_xmldb
all: $(APPL)
clean:
rm -f $(OBJS) *.core $(APPL) $(APPOBJ)
distclean: clean
rm -f Makefile *~ .depend
# Put demon in bin
# Put other executables in libexec/
# Also create a libexec/ directory for writeable/temporary files.
# Put config file in etc/
install: $(APPL)
install -d $(DESTDIR)$(bindir)
install $(APPL) $(DESTDIR)$(bindir)
install-include:
uninstall:
rm -f $(bindir)/$(APPL)
.SUFFIXES:
.SUFFIXES: .c .o
.c.o:
$(CC) $(INCLUDES) -D__PROGRAM__=\"$(APPL)\" $(CPPFLAGS) $(CFLAGS) -c $<
$(APPL) : $(APPOBJ) $(OBJS) $(LIBDEPS)
$(CC) $(LDFLAGS) $(APPOBJ) $(OBJS) $(LIBS) -o $@
TAGS:
find . -name '*.[chyl]' -print | etags -
depend:
$(CC) $(DEPENDFLAGS) @DEFS@ $(INCLUDES) $(CFLAGS) -MM $(SRC) $(APPSRC) > .depend
#include .depend

View file

@ -1,940 +0,0 @@
/*
*
***** BEGIN LICENSE BLOCK *****
Copyright (C) 2009-2017 Olof Hagsand and Benny Holmgren
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 *****
*/
#ifdef HAVE_CONFIG_H
#include "clixon_config.h" /* generated by config & autoconf */
#endif
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdarg.h>
#include <errno.h>
#include <signal.h>
#include <fcntl.h>
#include <time.h>
#include <syslog.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/un.h>
/* cligen */
#include <cligen/cligen.h>
/* clicon */
#include <clixon/clixon.h>
/* Command line options to be passed to getopt(3) */
#define XMLDB_OPTS "hDSf:a:p:y:m:r:"
#define DEFAULT_PORT 7878
#define DEFAULT_ADDR "127.0.0.1"
static int
xmldb_send_error(int s,
char *reason)
{
int retval = -1;
cbuf *cb = NULL; /* Outgoing return message */
clicon_log(LOG_NOTICE, "%s", reason);
if ((cb = cbuf_new()) == NULL){
clicon_err(OE_UNIX, errno, "cbuf_new");
goto done;
}
cprintf(cb, "<error>%s</error>", reason);
if (write(s, cbuf_get(cb), cbuf_len(cb)+1) < 0){
clicon_err(OE_UNIX, errno, "write");
goto done;
}
retval = 0;
done:
if (cb)
cbuf_free(cb);
return retval;
}
/*! Process incoming xmldb get message
* @param[in] s Stream socket
* @param[in] cb Packet buffer
* @param[in] xr XML request node with root in "get"
* example
* <rpc>
* <get>
* <source><candidate/></source>
* <xpath>/</xpath>
* <vector/> # If set send back list of xpath hits not single tree
* </get>
* </rpc>
* @note restrictions on using only databases called candidate and running
*
*/
static int
xmldb_from_get(clicon_handle h,
int s,
cxobj *xr)
{
int retval = -1;
cxobj *x;
cbuf *cb = NULL; /* Outgoing return message */
char *db;
char *xpath = "/";
cxobj *xt = NULL; /* Top of return tree */
cxobj *xc; /* Child */
cxobj **xvec = NULL;
size_t xlen = 0;
int i;
if (xpath_first(xr, "source/candidate") != NULL)
db = "candidate";
else if (xpath_first(xr, "source/running") != NULL)
db = "running";
else {
xmldb_send_error(s, "Get request: Expected candidate or running as source");
goto drop;
}
if ((x = xpath_first(xr, "xpath")) != NULL)
xpath = xml_body(x);
/* Actual get call */
if (xmldb_get(h, db, xpath, &xt, &xvec, &xlen) < 0)
goto done;
xml_name_set(xt, "config");
if ((cb = cbuf_new()) == NULL){
clicon_err(OE_UNIX, errno, "cbuf_new");
goto done;
}
if (xvec){
for (i=0; i<xlen; i++){
xc = xvec[i];
if (clicon_xml2cbuf(cb, xc, 0, 0) < 0)
goto done;
}
}
else{
if (clicon_xml2cbuf(cb, xt, 0, 0) < 0)
goto done;
}
if (debug)
fprintf(stderr, "%s: \"%s\" len:%d\n",
__FUNCTION__, cbuf_get(cb), cbuf_len(cb)+1);
if (write(s, cbuf_get(cb), cbuf_len(cb)+1) < 0){
clicon_err(OE_UNIX, errno, "write");
goto done;
}
drop:
retval = 0;
done:
if (xt)
xml_free(xt);
if (xvec)
free(xvec);
if (cb)
cbuf_free(cb);
return retval;
}
/*! Process incoming xmldb put message
* @param[in] s Stream socket
* @param[in] cb Packet buffer
* @param[in] xr XML request node with root in "put"
* example
* <rpc>
* <put>
* <target><candidate/></target>
* <default-operation>merge|none|replace</default-operation>
* <config>
* ...
* </config>
* </put>
* </rpc>
* @note restrictions on using only databases called candidate and running
* @note key,val see xmldb_put_xkey
*/
static int
xmldb_from_put(clicon_handle h,
int s,
cxobj *xr)
{
int retval = -1;
cxobj *x;
char *db;
cxobj *xc; /* Child */
char *opstr;
enum operation_type op = OP_REPLACE;
if (xpath_first(xr, "target/candidate") != NULL)
db = "candidate";
else if (xpath_first(xr, "target/running") != NULL)
db = "running";
else {
xmldb_send_error(s, "Put request: Expected candidate or running as source");
goto drop;
}
if ((x = xpath_first(xr, "default-operation")) != NULL)
if ((opstr = xml_body(x)) != NULL){
if (strcmp(opstr, "replace") == 0)
op = OP_REPLACE;
else
if (strcmp(opstr, "merge") == 0)
op = OP_MERGE;
else
if (strcmp(opstr, "none") == 0)
op = OP_NONE;
else{
xmldb_send_error(s, "Put request: unrecognized default-operation");
goto drop;
}
}
if ((xc = xpath_first(xr, "config")) != NULL){
/* Actual put call */
if (xmldb_put(h, db, xc, op) < 0)
goto done;
}
if (write(s, "<ok/>", strlen("<ok/>")+1) < 0){
clicon_err(OE_UNIX, errno, "write");
goto done;
}
drop:
retval = 0;
done:
return retval;
}
static int
xmldb_from_put_xkey(clicon_handle h,
int s,
cxobj *xr)
{
int retval = -1;
cxobj *x;
char *db;
char *xkey;
char *val;
char *opstr;
enum operation_type op = OP_REPLACE;
if (xpath_first(xr, "target/candidate") != NULL)
db = "candidate";
else if (xpath_first(xr, "target/running") != NULL)
db = "running";
else {
xmldb_send_error(s, "Put request: Expected candidate or running as source");
goto drop;
}
if ((x = xpath_first(xr, "default-operation")) != NULL)
if ((opstr = xml_body(x)) != NULL){
if (strcmp(opstr, "replace") == 0)
op = OP_REPLACE;
else
if (strcmp(opstr, "merge") == 0)
op = OP_MERGE;
else
if (strcmp(opstr, "none") == 0)
op = OP_NONE;
else{
xmldb_send_error(s, "Put xkey request: unrecognized default-operation");
goto drop;
}
}
if ((x = xpath_first(xr, "xkey")) == NULL){
xmldb_send_error(s, "Put xkey request: no xkey");
goto drop;
}
xkey = xml_body(x);
if ((x = xpath_first(xr, "value")) == NULL){
xmldb_send_error(s, "Put xkey request: no value");
goto drop;
}
val = xml_body(x);
if (xmldb_put_xkey(h, db, xkey, val, op) < 0)
goto done;
if (write(s, "<ok/>", strlen("<ok/>")+1) < 0){
clicon_err(OE_UNIX, errno, "write");
goto done;
}
drop:
retval = 0;
done:
return retval;
}
/*! Process incoming copy message
* @param[in] s Stream socket
* @param[in] cb Packet buffer
* @param[in] xr XML request node with root in "exists"
* example
* <rpc>
* <copy>
* <source><candidate/></source>
* <target><running/></target>
* </copy>
* </rpc>
* @note restrictions on using only databases called candidate and running
*/
static int
xmldb_from_copy(clicon_handle h,
int s,
cxobj *xr)
{
int retval = -1;
char *source;
char *target;
if (xpath_first(xr, "source/candidate") != NULL)
source = "candidate";
else if (xpath_first(xr, "source/running") != NULL)
source = "running";
else {
xmldb_send_error(s, "Copy request: Expected candidate or running as source");
goto drop;
}
if (xpath_first(xr, "target/candidate") != NULL)
target = "candidate";
else if (xpath_first(xr, "target/running") != NULL)
target = "running";
else {
xmldb_send_error(s, "Copy request: Expected candidate or running as target");
goto drop;
}
if (xmldb_copy(h, source, target) < 0)
goto done;
if (write(s, "<ok/>", strlen("<ok/>")+1) < 0){
clicon_err(OE_UNIX, errno, "write");
goto done;
}
drop:
retval = 0;
done:
return retval;
}
/*! Process incoming lock message
* @param[in] s Stream socket
* @param[in] cb Packet buffer
* @param[in] xr XML request node with root in "exists"
* example
* <rpc>
* <exists>
* <target><candidate/></target>
* <id>43</id>
* </exists>
* </rpc>
* @note restrictions on using only databases called candidate and running
*/
static int
xmldb_from_lock(clicon_handle h,
int s,
cxobj *xr)
{
int retval = -1;
char *target;
char *idstr = NULL;
cxobj *x;
if (xpath_first(xr, "target/candidate") != NULL)
target = "candidate";
else if (xpath_first(xr, "target/running") != NULL)
target = "running";
else {
xmldb_send_error(s, "Lock request: Expected candidate or running as target");
goto drop;
}
if ((x = xpath_first(xr, "id")) != NULL){
xmldb_send_error(s, "Lock request: mandatory id not found");
goto drop;
}
idstr = xml_body(x);
if (xmldb_lock(h, target, atoi(idstr)) < 0)
goto done;
drop:
retval = 0;
done:
return retval;
}
/*! Process incoming unlock message
* @param[in] s Stream socket
* @param[in] cb Packet buffer
* @param[in] xr XML request node with root in "exists"
* example
* <rpc>
* <unlock>
* <target><candidate/></target>
* <id>43</id>
* </unlock>
* </rpc>
* @note restrictions on using only databases called candidate and running
*/
static int
xmldb_from_unlock(clicon_handle h,
int s,
cxobj *xr)
{
int retval = -1;
char *target;
char *idstr = NULL;
cxobj *x;
if (xpath_first(xr, "target/candidate") != NULL)
target = "candidate";
else if (xpath_first(xr, "target/running") != NULL)
target = "running";
else {
xmldb_send_error(s, "Unlock request: Expected candidate or running as target");
goto drop;
}
if ((x = xpath_first(xr, "id")) != NULL){
xmldb_send_error(s, "Unlock request: mandatory id not found");
goto drop;
}
idstr = xml_body(x);
if (xmldb_unlock(h, target, atoi(idstr)) < 0)
goto done;
drop:
retval = 0;
done:
return retval;
}
/*! Process incoming islocked message
* @param[in] s Stream socket
* @param[in] cb Packet buffer
* @param[in] xr XML request node with root in "exists"
* example
* <rpc>
* <islocked>
* <target><candidate/></target>
* </islocked>
* </rpc>
* @note restrictions on using only databases called candidate and running
*/
static int
xmldb_from_islocked(clicon_handle h,
int s,
cxobj *xr)
{
int retval = -1;
char *db;
int ret;
cbuf *cb = NULL;
if (xpath_first(xr, "target/candidate") != NULL)
db = "candidate";
else if (xpath_first(xr, "target/running") != NULL)
db = "running";
else {
xmldb_send_error(s, "Islocked request: Expected candidate or running as source");
goto drop;
}
if ((ret = xmldb_islocked(h, db)) < 0)
goto done;
if (ret > 0){
if ((cb = cbuf_new()) == NULL){
clicon_err(OE_UNIX, errno, "cbuf_new");
goto done;
}
cprintf(cb, "<locked>%u</locked>", ret);
if (write(s, cbuf_get(cb), cbuf_len(cb)+1) < 0){
clicon_err(OE_UNIX, errno, "write");
goto done;
}
}
else{
if (write(s, "<unlocked/>", strlen("<unlocked/>")+1) < 0){
clicon_err(OE_UNIX, errno, "write");
goto done;
}
}
drop:
retval = 0;
done:
if (cb)
cbuf_free(cb);
return retval;
}
/*! Process incoming exists? message
* @param[in] s Stream socket
* @param[in] cb Packet buffer
* @param[in] xr XML request node with root in "exists"
* example
* <rpc>
* <exists>
* <target><candidate/></target>
* </exists>
* </rpc>
* @note restrictions on using only databases called candidate and running
*/
static int
xmldb_from_exists(clicon_handle h,
int s,
cxobj *xr)
{
int retval = -1;
char *db;
if (xpath_first(xr, "target/candidate") != NULL)
db = "candidate";
else if (xpath_first(xr, "target/running") != NULL)
db = "running";
else {
xmldb_send_error(s, "Exists request: Expected candidate or running as source");
goto drop;
}
/* XXX error and non-exist treated same */
if (xmldb_exists(h, db) == 1){
if (write(s, "<ok/>", strlen("<ok/>")+1) < 0){
clicon_err(OE_UNIX, errno, "write");
goto done;
}
}
else{
xmldb_send_error(s, "DB does not exist");
goto drop;
}
drop:
retval = 0;
done:
return retval;
}
/*! Process incoming xmldb delete message
* @param[in] s Stream socket
* @param[in] cb Packet buffer
* @param[in] xr XML request node with root in "delete"
* example
* <rpc>
* <delete>
* <target><candidate/></target>
* </delete>
* </rpc>
* @note restrictions on using only databases called candidate and running
*/
static int
xmldb_from_delete(clicon_handle h,
int s,
cxobj *xr)
{
int retval = -1;
char *db;
if (xpath_first(xr, "target/candidate") != NULL)
db = "candidate";
else if (xpath_first(xr, "target/running") != NULL)
db = "running";
else {
xmldb_send_error(s, "Delete request: Expected candidate or running as source");
goto drop;
}
if (xmldb_delete(h, db) < 0)
; /* ignore */
if (write(s, "<ok/>", strlen("<ok/>")+1) < 0){
clicon_err(OE_UNIX, errno, "write");
goto done;
}
drop:
retval = 0;
done:
return retval;
}
/*! Process incoming xmldb init message
* @param[in] s Stream socket
* @param[in] cb Packet buffer
* @param[in] xr XML request node with root in "init"
* example
* <rpc>
* <init>
* <target><candidate/></target>
* </init>
* </rpc>
* @note restrictions on using only databases called candidate and running
*/
static int
xmldb_from_init(clicon_handle h,
int s,
cxobj *xr)
{
int retval = -1;
char *db;
if (xpath_first(xr, "target/candidate") != NULL)
db = "candidate";
else if (xpath_first(xr, "target/running") != NULL)
db = "running";
else {
xmldb_send_error(s, "Init request: Expected candidate or running as source");
goto drop;
}
if (xmldb_init(h, db) < 0)
goto done;
if (write(s, "<ok/>", strlen("<ok/>")+1) < 0){
clicon_err(OE_UNIX, errno, "write");
goto done;
}
drop:
retval = 0;
done:
return retval;
}
/*! Process incoming xmldb packet
* @param[in] h Clicon handle
* @param[in] s Stream socket
* @param[in] cbin Incoming packet buffer
* example: <rpc><get></get></rpc>]]>]]>
*/
static int
xmldb_from_client(clicon_handle h,
int s,
cbuf *cbin)
{
int retval = -1;
char *str;
cxobj *xrq = NULL; /* Request (in) */
cxobj *xr;
cxobj *x;
cxobj *xt = NULL;
clicon_debug(1, "xmldb message: \"%s\"", cbuf_get(cbin));
str = cbuf_get(cbin);
str[strlen(str)-strlen("]]>]]>")] = '\0';
/* Parse incoming XML message */
if (clicon_xml_parse_string(&str, &xrq) < 0)
goto done;
if (debug)
xml_print(stderr, xrq);
if ((xr = xpath_first(xrq, "rpc")) != NULL){
if ((x = xpath_first(xr, "get")) != NULL){
if (xmldb_from_get(h, s, x) < 0)
goto done;
}
else if ((x = xpath_first(xr, "put")) != NULL){
if (xmldb_from_put(h, s, x) < 0)
goto done;
}
else if ((x = xpath_first(xr, "put-xkey")) != NULL){
if (xmldb_from_put_xkey(h, s, x) < 0)
goto done;
}
else if ((x = xpath_first(xr, "copy")) != NULL){
if (xmldb_from_copy(h, s, x) < 0)
goto done;
}
else if ((x = xpath_first(xr, "lock")) != NULL){
if (xmldb_from_lock(h, s, x) < 0)
goto done;
}
else if ((x = xpath_first(xr, "unlock")) != NULL){
if (xmldb_from_unlock(h, s, x) < 0)
goto done;
}
else if ((x = xpath_first(xr, "islocked")) != NULL){
if (xmldb_from_islocked(h, s, x) < 0)
goto done;
}
else if ((x = xpath_first(xr, "exists")) != NULL){
if (xmldb_from_exists(h, s, x) < 0)
goto done;
}
else if ((x = xpath_first(xr, "init")) != NULL){
if (xmldb_from_init(h, s, x) < 0)
goto done;
}
else if ((x = xpath_first(xr, "delete")) != NULL){
if (xmldb_from_delete(h, s, x) < 0)
goto done;
}
}
else{
xmldb_send_error(s, "Expected rpc as top xml msg");
goto drop;
}
drop:
retval = 0;
done:
if (xrq)
xml_free(xrq);
if (xt)
xml_free(xt);
return retval;
}
/*! stolen from netconf_lib.c */
static int
detect_endtag(char *tag, char ch, int *state)
{
int retval = 0;
if (tag[*state] == ch){
(*state)++;
if (*state == strlen(tag)){
*state = 0;
retval = 1;
}
}
else
*state = 0;
return retval;
}
/*! config_accept_client
*/
int
config_accept_client(int fd,
void *arg)
{
int retval = -1;
clicon_handle h = (clicon_handle)arg;
int s = -1;
struct sockaddr_un from;
socklen_t slen;
ssize_t len;
unsigned char buf[BUFSIZ];
int i;
cbuf *cb = NULL;
int xml_state = 0;
clicon_debug(1, "Accepting client request");
if ((cb = cbuf_new()) == NULL){
clicon_err(OE_XML, errno, "cbuf_new");
return retval;
}
len = sizeof(from);
if ((s = accept(fd, (struct sockaddr*)&from, &slen)) < 0){
clicon_err(OE_UNIX, errno, "%s: accept", __FUNCTION__);
goto done;
}
if ((len = read(s, buf, sizeof(buf))) < 0){
clicon_err(OE_UNIX, errno, "read");
goto done;
}
for (i=0; i<len; i++){
if (buf[i] == 0)
continue; /* Skip NULL chars (eg from terminals) */
cprintf(cb, "%c", buf[i]);
if (detect_endtag("]]>]]>",
buf[i],
&xml_state)) {
if (xmldb_from_client(h, s, cb) < 0){
goto done;
}
cbuf_reset(cb);
}
}
retval = 0;
done:
if (cb)
cbuf_free(cb);
if (s != -1)
close(s);
return retval;
}
/*! Create tcp server socket and register callback
*/
static int
server_socket(clicon_handle h,
char *ipv4addr,
uint16_t port)
{
int retval = -1;
int s;
struct sockaddr_in addr;
/* Open control socket */
if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
clicon_err(OE_UNIX, errno, "socket");
goto done;
}
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
if (inet_pton(addr.sin_family, ipv4addr, &addr.sin_addr) != 1){
clicon_err(OE_UNIX, errno, "inet_pton: %s (Expected IPv4 address. Check settings of CLICON_SOCK_FAMILY and CLICON_SOCK)", ipv4addr);
goto done;
}
if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0){
clicon_err(OE_UNIX, errno, "%s: bind", __FUNCTION__);
goto done;
}
clicon_debug(1, "Listen on server socket at %s:%hu", ipv4addr, port);
if (listen(s, 5) < 0){
clicon_err(OE_UNIX, errno, "%s: listen", __FUNCTION__);
goto done;
}
if (event_reg_fd(s, config_accept_client, h, "server socket") < 0) {
close(s);
goto done;
}
retval = 0;
done:
return retval;
}
/*
* usage
*/
static void
usage(char *argv0)
{
fprintf(stderr, "usage:%s\n"
"where options are\n"
"\t-h\t\tHelp\n"
"\t-D\t\tDebug\n"
"\t-S\t\tLog on syslog\n"
"\t-f <file>\tCLICON config file\n"
"\t-a <addr>\tIP address\n"
"\t-p <port>\tTCP port\n"
"\t-y <dir>\tYang dir\n"
"\t-m <mod>\tYang main module name\n"
"\t-r <rev>\tYang module revision\n",
argv0
);
exit(0);
}
int
main(int argc, char **argv)
{
char c;
int use_syslog;
clicon_handle h;
uint16_t port;
char *addr = DEFAULT_ADDR;
/* In the startup, logs to stderr & debug flag set later */
clicon_log_init(__PROGRAM__, LOG_INFO, CLICON_LOG_STDERR);
/* Defaults */
use_syslog = 0;
port = DEFAULT_PORT;
if ((h = clicon_handle_init()) == NULL)
goto done;
/* getopt in two steps, first find config-file before over-riding options. */
while ((c = getopt(argc, argv, XMLDB_OPTS)) != -1)
switch (c) {
case '?' :
case 'h' : /* help */
usage(argv[0]);
break;
case 'D' : /* debug */
debug = 1;
break;
case 'f': /* config file */
if (!strlen(optarg))
usage(argv[0]);
clicon_option_str_set(h, "CLICON_CONFIGFILE", optarg);
break;
case 'S': /* Log on syslog */
use_syslog = 1;
break;
}
/*
* Logs, error and debug to stderr or syslog, set debug level
*/
clicon_log_init(__PROGRAM__, debug?LOG_DEBUG:LOG_INFO,
use_syslog?CLICON_LOG_SYSLOG:CLICON_LOG_STDERR);
clicon_debug_init(debug, NULL);
/* Find and read configfile */
if (clicon_options_main(h) < 0){
usage(argv[0]);
goto done;
}
/* Now rest of options */
optind = 1;
while ((c = getopt(argc, argv, XMLDB_OPTS)) != -1)
switch (c) {
case 'D': /* Processed earlier, ignore now. */
case 'S':
case 'f':
break;
case 'a': /* address */
clicon_option_str_set(h, "CLICON_XMLDB_ADDR", optarg);
break;
case 'p': /* port */
clicon_option_str_set(h, "CLICON_XMLDB_PORT", optarg);
break;
case 'y': /* yang dir */
clicon_option_str_set(h, "CLICON_YANG_DIR", optarg);
break;
case 'm': /* yang module */
clicon_option_str_set(h, "CLICON_YANG_MODULE_MAIN", optarg);
break;
case 'r': /* yang revision */
clicon_option_str_set(h, "CLICON_YANG_MODULE_REVISION", optarg);
break;
default:
usage(argv[0]);
break;
}
argc -= optind;
argv += optind;
clicon_option_str_set(h, "CLICON_XMLDB_RPC", "0");
if (clicon_yang_dir(h) == NULL){
clicon_err(OE_UNIX, errno, "yang dir not set");
goto done;
}
if (clicon_yang_module_main(h) == NULL){
clicon_err(OE_UNIX, errno, "yang main module not set");
goto done;
}
if (yang_spec_main(h, NULL, 0) < 0)
goto done;
addr = clicon_xmldb_addr(h);
port = clicon_xmldb_port(h);
if (server_socket(h, addr, port) < 0)
goto done;
if (event_loop() < 0)
goto done;
done:
return 0;
}