* Major rewrite of event streams
* If you used old event callbacks API, you need to switch to the streams API
* See clixon_stream.[ch]
* Old streams API which needs to be removed include:
* clicon_log_register_callback()
* subscription_add() --> stream_register()
* backend_notify() and backend_notify_xml() - use stream_notify() instead
* Example uses "NETCONF" stream instead of "ROUTING"
* Added timeout option -t for clixon_netconf - quit after max time.
This commit is contained in:
parent
d7fbe75c9e
commit
98f3cd0e32
31 changed files with 597 additions and 635 deletions
|
|
@ -69,59 +69,6 @@
|
|||
#include "backend_client.h"
|
||||
#include "backend_handle.h"
|
||||
|
||||
/*! Add client notification subscription. Ie send notify to this client when event occurs
|
||||
* @param[in] ce Client entry struct
|
||||
* @param[in] stream Notification stream name
|
||||
* @param[in] format How to display event (see enum format_enum)
|
||||
* @param[in] filter Filter, what to display, eg xpath for format=xml, fnmatch
|
||||
*
|
||||
* @see backend_notify - where subscription is made and notify call is made
|
||||
*/
|
||||
static struct client_subscription *
|
||||
client_subscription_add(struct client_entry *ce,
|
||||
char *stream,
|
||||
enum format_enum format,
|
||||
char *filter)
|
||||
{
|
||||
struct client_subscription *su = NULL;
|
||||
|
||||
if ((su = malloc(sizeof(*su))) == NULL){
|
||||
clicon_err(OE_PLUGIN, errno, "malloc");
|
||||
goto done;
|
||||
}
|
||||
memset(su, 0, sizeof(*su));
|
||||
su->su_stream = strdup(stream);
|
||||
su->su_format = format;
|
||||
su->su_filter = filter?strdup(filter):strdup("");
|
||||
su->su_next = ce->ce_subscription;
|
||||
ce->ce_subscription = su;
|
||||
done:
|
||||
return su;
|
||||
}
|
||||
|
||||
/*! Delete stream subscription from client subscription list */
|
||||
static int
|
||||
client_subscription_delete(struct client_entry *ce,
|
||||
struct client_subscription *su0)
|
||||
{
|
||||
struct client_subscription *su;
|
||||
struct client_subscription **su_prev;
|
||||
|
||||
su_prev = &ce->ce_subscription; /* this points to stack and is not real backpointer */
|
||||
for (su = *su_prev; su; su = su->su_next){
|
||||
if (su == su0){
|
||||
*su_prev = su->su_next;
|
||||
free(su->su_stream);
|
||||
if (su->su_filter)
|
||||
free(su->su_filter);
|
||||
free(su);
|
||||
break;
|
||||
}
|
||||
su_prev = &su->su_next;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct client_entry *
|
||||
ce_find_bypid(struct client_entry *ce_list,
|
||||
int pid)
|
||||
|
|
@ -134,6 +81,31 @@ ce_find_bypid(struct client_entry *ce_list,
|
|||
return NULL;
|
||||
}
|
||||
|
||||
static int
|
||||
ce_event_cb(clicon_handle h,
|
||||
void *event,
|
||||
void *arg)
|
||||
{
|
||||
struct client_entry *ce = (struct client_entry *)arg;
|
||||
|
||||
if (send_msg_notify_xml(ce->ce_s, (cxobj*)event) < 0){
|
||||
if (errno == ECONNRESET || errno == EPIPE){
|
||||
clicon_log(LOG_WARNING, "client %d reset", ce->ce_nr);
|
||||
#if 0
|
||||
/* We should remove here but removal is not possible
|
||||
from a client since backend_client is not linked.
|
||||
Maybe we should add it to the plugin, but it feels
|
||||
"safe" that you cant remove a client.
|
||||
Instead, the client is (hopefully) removed elsewhere?
|
||||
*/
|
||||
backend_client_rm(h, ce);
|
||||
#endif
|
||||
}
|
||||
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*! Remove client entry state
|
||||
* Close down everything wrt clients (eg sockets, subscriptions)
|
||||
* Finally actually remove client struct in handle
|
||||
|
|
@ -148,7 +120,6 @@ backend_client_rm(clicon_handle h,
|
|||
struct client_entry *c;
|
||||
struct client_entry *c0;
|
||||
struct client_entry **ce_prev;
|
||||
struct client_subscription *su;
|
||||
|
||||
c0 = backend_client_list(h);
|
||||
ce_prev = &c0; /* this points to stack and is not real backpointer */
|
||||
|
|
@ -159,8 +130,8 @@ backend_client_rm(clicon_handle h,
|
|||
close(ce->ce_s);
|
||||
ce->ce_s = 0;
|
||||
}
|
||||
while ((su = ce->ce_subscription) != NULL)
|
||||
client_subscription_delete(ce, su);
|
||||
/* for all streams */
|
||||
stream_cb_delete(h, NULL, ce_event_cb, (void*)ce);
|
||||
break;
|
||||
}
|
||||
ce_prev = &c->ce_next;
|
||||
|
|
@ -900,6 +871,7 @@ from_client_delete_config(clicon_handle h,
|
|||
return retval;
|
||||
}
|
||||
|
||||
|
||||
/*! Internal message: Create subscription for notifications see RFC 5277
|
||||
* @param[in] h Clicon handle
|
||||
* @param[in] xe Netconf request xml tree
|
||||
|
|
@ -910,7 +882,7 @@ from_client_delete_config(clicon_handle h,
|
|||
* @example:
|
||||
* <create-subscription>
|
||||
* <stream>RESULT</stream> # If not present, events in the default NETCONF stream will be sent.
|
||||
* <filter>XPATH-EXPR<(filter>
|
||||
* <filter type="xpath" select="XPATH-EXPR"/>
|
||||
* <startTime/> # only for replay (NYI)
|
||||
* <stopTime/> # only for replay (NYI)
|
||||
* </create-subscription>
|
||||
|
|
@ -922,24 +894,27 @@ from_client_create_subscription(clicon_handle h,
|
|||
cbuf *cbret)
|
||||
{
|
||||
char *stream = "NETCONF";
|
||||
char *filter = NULL;
|
||||
int retval = -1;
|
||||
cxobj *x; /* Genereic xml tree */
|
||||
cxobj *x; /* Generic xml tree */
|
||||
cxobj *xfilter; /* Filter xml tree */
|
||||
char *ftype;
|
||||
char *selector = NULL;
|
||||
|
||||
if ((x = xpath_first(xe, "//stream")) != NULL)
|
||||
stream = xml_find_value(x, "body");
|
||||
if ((x = xpath_first(xe, "//filter")) != NULL){
|
||||
if ((ftype = xml_find_value(x, "type")) != NULL){
|
||||
if ((xfilter = xpath_first(xe, "//filter")) != NULL){
|
||||
if ((ftype = xml_find_value(xfilter, "type")) != NULL){
|
||||
/* Only accept xpath as filter type */
|
||||
if (strcmp(ftype, "xpath") != 0){
|
||||
if (netconf_operation_failed(cbret, "application", "Only xpath filter type supported")< 0)
|
||||
goto done;
|
||||
goto ok;
|
||||
}
|
||||
if ((selector = xml_find_value(xfilter, "select")) == NULL)
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
if (client_subscription_add(ce, stream, FORMAT_XML, filter) == NULL)
|
||||
if (stream_cb_add(h, stream, selector, ce_event_cb, (void*)ce) < 0)
|
||||
goto done;
|
||||
cprintf(cbret, "<rpc-reply><ok/></rpc-reply>");
|
||||
ok:
|
||||
|
|
@ -1334,6 +1309,7 @@ from_client_msg(clicon_handle h,
|
|||
}
|
||||
else if (strcmp(name, "close-session") == 0){
|
||||
xmldb_unlock_all(h, pid);
|
||||
stream_cb_delete(h, NULL, ce_event_cb, (void*)ce);
|
||||
cprintf(cbret, "<rpc-reply><ok/></rpc-reply>");
|
||||
}
|
||||
else if (strcmp(name, "kill-session") == 0){
|
||||
|
|
|
|||
|
|
@ -52,19 +52,8 @@ struct client_entry{
|
|||
int ce_pid; /* Process id */
|
||||
int ce_uid; /* User id of calling process */
|
||||
clicon_handle ce_handle; /* clicon config handle (all clients have same?) */
|
||||
struct client_subscription *ce_subscription; /* notification subscriptions */
|
||||
};
|
||||
|
||||
/* Notification subscription info
|
||||
* @see subscription in config_handle.c
|
||||
*/
|
||||
struct client_subscription{
|
||||
struct client_subscription *su_next;
|
||||
int su_s; /* stream socket */
|
||||
enum format_enum su_format; /* format of notification stream */
|
||||
char *su_stream;
|
||||
char *su_filter;
|
||||
};
|
||||
|
||||
/*
|
||||
* Prototypes
|
||||
|
|
|
|||
|
|
@ -98,7 +98,6 @@ backend_terminate(clicon_handle h)
|
|||
xmldb_plugin_unload(h); /* unload storage plugin */
|
||||
backend_handle_exit(h); /* Cannot use h after this */
|
||||
event_exit();
|
||||
clicon_log_register_callback(NULL, NULL);
|
||||
clicon_debug(1, "%s done", __FUNCTION__);
|
||||
clicon_log_exit();
|
||||
return 0;
|
||||
|
|
@ -191,7 +190,6 @@ db_merge(clicon_handle h,
|
|||
return retval;
|
||||
}
|
||||
|
||||
|
||||
/*! Create backend server socket and register callback
|
||||
*/
|
||||
static int
|
||||
|
|
@ -211,45 +209,6 @@ server_socket(clicon_handle h)
|
|||
return ss;
|
||||
}
|
||||
|
||||
/*! Callback for CLICON log events
|
||||
* If you make a subscription to CLICON stream, this function is called for every
|
||||
* log event.
|
||||
*/
|
||||
static int
|
||||
backend_log_cb(int level,
|
||||
char *msg,
|
||||
void *arg)
|
||||
{
|
||||
int retval = -1;
|
||||
size_t n;
|
||||
char *ptr;
|
||||
char *nptr;
|
||||
char *newmsg = NULL;
|
||||
|
||||
/* backend_notify() will go through all clients and see if any has
|
||||
registered "CLICON", and if so make a clicon_proto notify message to
|
||||
those clients.
|
||||
Sanitize '%' into "%%" to prevent segvfaults in vsnprintf later.
|
||||
At this stage all formatting is already done */
|
||||
n = 0;
|
||||
for(ptr=msg; *ptr; ptr++)
|
||||
if (*ptr == '%')
|
||||
n++;
|
||||
if ((newmsg = malloc(strlen(msg) + n + 1)) == NULL) {
|
||||
clicon_err(OE_UNIX, errno, "malloc");
|
||||
return -1;
|
||||
}
|
||||
for(ptr=msg, nptr=newmsg; *ptr; ptr++) {
|
||||
*nptr++ = *ptr;
|
||||
if (*ptr == '%')
|
||||
*nptr++ = '%';
|
||||
}
|
||||
retval = backend_notify(arg, "CLICON", level, newmsg);
|
||||
free(newmsg);
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
/*! Call plugin_start with -- user options */
|
||||
static int
|
||||
plugin_start_useroptions(clicon_handle h,
|
||||
|
|
@ -758,8 +717,6 @@ main(int argc,
|
|||
|
||||
if (stream_register(h, "NETCONF", "default NETCONF event stream") < 0)
|
||||
goto done;
|
||||
if (stream_register(h, "CLICON", "Clicon logs") < 0)
|
||||
goto done;
|
||||
|
||||
if ((xmldb_plugin = clicon_xmldb_plugin(h)) == NULL){
|
||||
clicon_log(LOG_ERR, "No xmldb plugin given (specify option CLICON_XMLDB_PLUGIN).\n");
|
||||
|
|
@ -850,9 +807,6 @@ main(int argc,
|
|||
if ((pid = pidfile_write(pidfile)) < 0)
|
||||
goto done;
|
||||
|
||||
/* Register log notifications */
|
||||
if (clicon_log_register_callback(backend_log_cb, h) < 0)
|
||||
goto done;
|
||||
clicon_log(LOG_NOTICE, "%s: %u Started", __PROGRAM__, getpid());
|
||||
if (set_signal(SIGTERM, backend_sig_term, NULL) < 0){
|
||||
clicon_err(OE_DEMON, errno, "Setting signal");
|
||||
|
|
|
|||
|
|
@ -91,7 +91,6 @@ struct backend_handle {
|
|||
/* ------ end of common handle ------ */
|
||||
struct client_entry *bh_ce_list; /* The client list */
|
||||
int bh_ce_nr; /* Number of clients, just increment */
|
||||
struct handle_subscription *bh_subscription; /* Event subscription list */
|
||||
cxobj *bh_nacm; /* NACM external struct */
|
||||
};
|
||||
|
||||
|
|
@ -121,155 +120,6 @@ backend_handle_exit(clicon_handle h)
|
|||
return 0;
|
||||
}
|
||||
|
||||
/*! Notify event and distribute to all registered clients
|
||||
*
|
||||
* @param[in] h Clicon handle
|
||||
* @param[in] stream Name of event stream. CLICON is predefined as LOG stream
|
||||
* @param[in] level Event level (not used yet)
|
||||
* @param[in] event Actual message as text format
|
||||
*
|
||||
* Stream is a string used to qualify the event-stream. Distribute the
|
||||
* event to all clients registered to this backend.
|
||||
* XXX: event-log NYI.
|
||||
* @see also subscription_add()
|
||||
* @see also backend_notify_xml()
|
||||
*/
|
||||
int
|
||||
backend_notify(clicon_handle h,
|
||||
char *stream,
|
||||
int level,
|
||||
char *event)
|
||||
{
|
||||
struct client_entry *ce;
|
||||
struct client_entry *ce_next;
|
||||
struct client_subscription *su;
|
||||
struct handle_subscription *hs;
|
||||
int retval = -1;
|
||||
|
||||
clicon_debug(2, "%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;
|
||||
for (su = ce->ce_subscription; su; su = su->su_next)
|
||||
if (strcmp(su->su_stream, stream) == 0){
|
||||
if (strlen(su->su_filter)==0 || fnmatch(su->su_filter, event, 0) == 0){
|
||||
if (send_msg_notify(ce->ce_s, level, event) < 0){
|
||||
if (errno == ECONNRESET || errno == EPIPE){
|
||||
clicon_log(LOG_WARNING, "client %d reset", ce->ce_nr);
|
||||
#if 0
|
||||
/* We should remove here but removal is not possible
|
||||
from a client since backend_client is not linked.
|
||||
Maybe we should add it to the plugin, but it feels
|
||||
"safe" that you cant remove a client.
|
||||
Instead, the client is (hopefully) removed elsewhere?
|
||||
*/
|
||||
backend_client_rm(h, ce);
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/* Then go thru all global (handle) subscriptions and find matches */
|
||||
hs = NULL;
|
||||
while ((hs = subscription_each(h, hs)) != NULL){
|
||||
if (hs->hs_format != FORMAT_TEXT)
|
||||
continue;
|
||||
if (strcmp(hs->hs_stream, stream))
|
||||
continue;
|
||||
if (hs->hs_filter==NULL ||
|
||||
strlen(hs->hs_filter)==0 ||
|
||||
fnmatch(hs->hs_filter, event, 0) == 0)
|
||||
if ((*hs->hs_fn)(h, event, hs->hs_arg) < 0)
|
||||
goto done;
|
||||
}
|
||||
retval = 0;
|
||||
done:
|
||||
return retval;
|
||||
}
|
||||
|
||||
/*! Notify event and distribute to all registered clients
|
||||
*
|
||||
* @param[in] h Clicon handle
|
||||
* @param[in] stream Name of event stream. CLICON is predefined as LOG stream
|
||||
* @param[in] level Event level (not used yet)
|
||||
* @param[in] x Actual message as xml tree
|
||||
*
|
||||
* Stream is a string used to qualify the event-stream. Distribute the
|
||||
* event to all clients registered to this backend.
|
||||
* XXX: event-log NYI.
|
||||
* @see also subscription_add()
|
||||
* @see also backend_notify()
|
||||
*/
|
||||
int
|
||||
backend_notify_xml(clicon_handle h,
|
||||
char *stream,
|
||||
int level,
|
||||
cxobj *x)
|
||||
{
|
||||
struct client_entry *ce;
|
||||
struct client_entry *ce_next;
|
||||
struct client_subscription *su;
|
||||
int retval = -1;
|
||||
cbuf *cb = NULL;
|
||||
struct handle_subscription *hs;
|
||||
|
||||
clicon_debug(1, "%s %s", __FUNCTION__, stream);
|
||||
/* Now go 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;
|
||||
for (su = ce->ce_subscription; su; su = su->su_next)
|
||||
if (strcmp(su->su_stream, stream) == 0){
|
||||
if (strlen(su->su_filter)==0 || xpath_first(x, "%s", su->su_filter) != NULL){
|
||||
if (cb==NULL){
|
||||
if ((cb = cbuf_new()) == NULL){
|
||||
clicon_err(OE_PLUGIN, errno, "cbuf_new");
|
||||
goto done;
|
||||
}
|
||||
if (clicon_xml2cbuf(cb, x, 0, 0) < 0)
|
||||
goto done;
|
||||
}
|
||||
if (send_msg_notify(ce->ce_s, level, cbuf_get(cb)) < 0){
|
||||
if (errno == ECONNRESET || errno == EPIPE){
|
||||
clicon_log(LOG_WARNING, "client %d reset", ce->ce_nr);
|
||||
#if 0
|
||||
/* We should remove here but removal is not possible
|
||||
from a client since backend_client is not linked.
|
||||
Maybe we should add it to the plugin, but it feels
|
||||
"safe" that you cant remove a client.
|
||||
Instead, the client is (hopefully) removed elsewhere?
|
||||
*/
|
||||
backend_client_rm(h, ce);
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/* Then go thru all global (handle) subscriptions and find matches */
|
||||
hs = NULL;
|
||||
while ((hs = subscription_each(h, hs)) != NULL){
|
||||
if (hs->hs_format != FORMAT_XML)
|
||||
continue;
|
||||
if (strcmp(hs->hs_stream, stream))
|
||||
continue;
|
||||
if (strlen(hs->hs_filter)==0 || xpath_first(x, "%s", hs->hs_filter) != NULL){
|
||||
if ((*hs->hs_fn)(h, x, hs->hs_arg) < 0)
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
retval = 0;
|
||||
done:
|
||||
if (cb)
|
||||
cbuf_free(cb);
|
||||
return retval;
|
||||
|
||||
}
|
||||
|
||||
/*! Add new client, typically frontend such as cli, netconf, restconf
|
||||
* @param[in] h Clicon handle
|
||||
* @param[in] addr Address of client
|
||||
|
|
@ -332,111 +182,6 @@ backend_client_delete(clicon_handle h,
|
|||
return 0;
|
||||
}
|
||||
|
||||
/*! Add subscription given stream name, callback and argument
|
||||
* @param[in] h Clicon handle
|
||||
* @param[in] stream Name of event stream
|
||||
* @param[in] format Expected format of event, eg text or xml
|
||||
* @param[in] filter Filter to match event, depends on format, eg xpath for xml
|
||||
* @param[in] fn Callback when event occurs
|
||||
* @param[in] arg Argument to use with callback. Also handle when deleting
|
||||
* Note that arg is not a real handle.
|
||||
* @see subscription_delete
|
||||
* @see subscription_each
|
||||
*/
|
||||
struct handle_subscription *
|
||||
subscription_add(clicon_handle h,
|
||||
char *stream,
|
||||
enum format_enum format,
|
||||
char *filter,
|
||||
subscription_fn_t fn,
|
||||
void *arg)
|
||||
{
|
||||
struct backend_handle *bh = handle(h);
|
||||
struct handle_subscription *hs = NULL;
|
||||
|
||||
if ((hs = malloc(sizeof(*hs))) == NULL){
|
||||
clicon_err(OE_PLUGIN, errno, "malloc");
|
||||
goto done;
|
||||
}
|
||||
memset(hs, 0, sizeof(*hs));
|
||||
hs->hs_stream = strdup(stream);
|
||||
hs->hs_format = format;
|
||||
hs->hs_filter = filter?strdup(filter):NULL;
|
||||
hs->hs_next = bh->bh_subscription;
|
||||
hs->hs_fn = fn;
|
||||
hs->hs_arg = arg;
|
||||
bh->bh_subscription = hs;
|
||||
done:
|
||||
return hs;
|
||||
}
|
||||
|
||||
/*! Delete subscription given stream name, callback and argument
|
||||
* @param[in] h Clicon handle
|
||||
* @param[in] stream Name of event stream
|
||||
* @param[in] fn Callback when event occurs
|
||||
* @param[in] arg Argument to use with callback and handle
|
||||
* Note that arg is not a real handle.
|
||||
* @see subscription_add
|
||||
* @see subscription_each
|
||||
*/
|
||||
int
|
||||
subscription_delete(clicon_handle h,
|
||||
char *stream,
|
||||
subscription_fn_t fn,
|
||||
void *arg)
|
||||
{
|
||||
struct backend_handle *bh = handle(h);
|
||||
struct handle_subscription *hs;
|
||||
struct handle_subscription **hs_prev;
|
||||
|
||||
hs_prev = &bh->bh_subscription; /* this points to stack and is not real backpointer */
|
||||
for (hs = *hs_prev; hs; hs = hs->hs_next){
|
||||
/* XXX arg == hs->hs_arg */
|
||||
if (strcmp(hs->hs_stream, stream)==0 && hs->hs_fn == fn){
|
||||
*hs_prev = hs->hs_next;
|
||||
free(hs->hs_stream);
|
||||
if (hs->hs_filter)
|
||||
free(hs->hs_filter);
|
||||
if (hs->hs_arg)
|
||||
free(hs->hs_arg);
|
||||
free(hs);
|
||||
break;
|
||||
}
|
||||
hs_prev = &hs->hs_next;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*! Iterator over subscriptions
|
||||
*
|
||||
* NOTE: Never manipulate the child-list during operation or using the
|
||||
* same object recursively, the function uses an internal field to remember the
|
||||
* index used. It works as long as the same object is not iterated concurrently.
|
||||
*
|
||||
* @param[in] h clicon handle
|
||||
* @param[in] hprev iterator, initialize with NULL
|
||||
* @code
|
||||
* clicon_handle h;
|
||||
* struct handle_subscription *hs = NULL;
|
||||
* while ((hs = subscription_each(h, hs)) != NULL) {
|
||||
* ...
|
||||
* }
|
||||
* @endcode
|
||||
*/
|
||||
struct handle_subscription *
|
||||
subscription_each(clicon_handle h,
|
||||
struct handle_subscription *hprev)
|
||||
{
|
||||
struct backend_handle *bh = handle(h);
|
||||
struct handle_subscription *hs = NULL;
|
||||
|
||||
if (hprev)
|
||||
hs = hprev->hs_next;
|
||||
else
|
||||
hs = bh->bh_subscription;
|
||||
return hs;
|
||||
}
|
||||
|
||||
int
|
||||
backend_nacm_list_set(clicon_handle h,
|
||||
cxobj *xnacm)
|
||||
|
|
|
|||
|
|
@ -43,34 +43,9 @@
|
|||
/*
|
||||
* Types
|
||||
*/
|
||||
/* Notification subscription info
|
||||
* @see client_subscription in config_client.h
|
||||
*/
|
||||
struct handle_subscription{
|
||||
struct handle_subscription *hs_next;
|
||||
enum format_enum hs_format; /* format (enum format_enum) XXX not needed? */
|
||||
char *hs_stream; /* name of notify stream */
|
||||
char *hs_filter; /* filter, if format=xml: xpath, if text: fnmatch */
|
||||
subscription_fn_t hs_fn; /* Callback when event occurs */
|
||||
void *hs_arg; /* Callback argument */
|
||||
};
|
||||
|
||||
/*
|
||||
* Prototypes
|
||||
*/
|
||||
/* Log for netconf notify function (config_client.c) */
|
||||
int backend_notify(clicon_handle h, char *stream, int level, char *txt);
|
||||
int backend_notify_xml(clicon_handle h, char *stream, int level, cxobj *x);
|
||||
|
||||
|
||||
struct handle_subscription *subscription_add(clicon_handle h, char *stream,
|
||||
enum format_enum format, char *filter,
|
||||
subscription_fn_t fn, void *arg);
|
||||
|
||||
int subscription_delete(clicon_handle h, char *stream,
|
||||
subscription_fn_t fn, void *arg);
|
||||
|
||||
struct handle_subscription *subscription_each(clicon_handle h,
|
||||
struct handle_subscription *hprev);
|
||||
|
||||
#endif /* _CLIXON_BACKEND_HANDLE_H_ */
|
||||
|
|
|
|||
|
|
@ -71,7 +71,7 @@
|
|||
#include "netconf_rpc.h"
|
||||
|
||||
/* Command line options to be passed to getopt(3) */
|
||||
#define NETCONF_OPTS "hDf:l:qa:u:d:y:U:"
|
||||
#define NETCONF_OPTS "hD:f:l:qa:u:d:y:U:t:"
|
||||
|
||||
#define NETCONF_LOGFILE "/tmp/clixon_netconf.log"
|
||||
|
||||
|
|
@ -284,6 +284,13 @@ netconf_terminate(clicon_handle h)
|
|||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
timeout_fn(int s,
|
||||
void *arg)
|
||||
{
|
||||
clicon_err(OE_EVENTS, ETIME, "User request timeout");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*! Usage help routine
|
||||
* @param[in] h Clicon handle
|
||||
|
|
@ -305,7 +312,8 @@ usage(clicon_handle h,
|
|||
"\t-d <dir>\tSpecify netconf plugin directory dir (default: %s)\n"
|
||||
|
||||
"\t-y <file>\tOverride yang spec file (dont include .yang suffix)\n"
|
||||
"\t-U <user>\tOver-ride unix user with a pseudo user for NACM.\n",
|
||||
"\t-U <user>\tOver-ride unix user with a pseudo user for NACM.\n"
|
||||
"\t-t <sec>\tTimeout in seconds. Quit after this time.\n",
|
||||
argv0,
|
||||
clicon_netconf_dir(h)
|
||||
);
|
||||
|
|
@ -324,12 +332,13 @@ main(int argc,
|
|||
char *dir;
|
||||
int logdst = CLICON_LOG_STDERR;
|
||||
struct passwd *pw;
|
||||
|
||||
/* In the startup, logs to stderr & debug flag set later */
|
||||
clicon_log_init(__PROGRAM__, LOG_INFO, logdst);
|
||||
struct timeval tv = {0,}; /* timeout */
|
||||
|
||||
/* Create handle */
|
||||
if ((h = clicon_handle_init()) == NULL)
|
||||
return -1;
|
||||
/* In the startup, logs to stderr & debug flag set later */
|
||||
clicon_log_init(__PROGRAM__, LOG_INFO, logdst);
|
||||
|
||||
/* Set username to clicon handle. Use in all communication to backend */
|
||||
if ((pw = getpwuid(getuid())) == NULL){
|
||||
|
|
@ -338,7 +347,6 @@ main(int argc,
|
|||
}
|
||||
if (clicon_username_set(h, pw->pw_name) < 0)
|
||||
goto done;
|
||||
|
||||
while ((c = getopt(argc, argv, NETCONF_OPTS)) != -1)
|
||||
switch (c) {
|
||||
case 'h' : /* help */
|
||||
|
|
@ -362,6 +370,7 @@ main(int argc,
|
|||
goto done;
|
||||
break;
|
||||
}
|
||||
|
||||
/*
|
||||
* Logs, error and debug to stderr or syslog, set debug level
|
||||
*/
|
||||
|
|
@ -408,6 +417,10 @@ main(int argc,
|
|||
if (clicon_username_set(h, optarg) < 0)
|
||||
goto done;
|
||||
break;
|
||||
case 't': /* timeout in seconds */
|
||||
tv.tv_sec = atoi(optarg);
|
||||
break;
|
||||
|
||||
default:
|
||||
usage(h, argv[0]);
|
||||
break;
|
||||
|
|
@ -440,6 +453,13 @@ main(int argc,
|
|||
goto done;
|
||||
if (debug)
|
||||
clicon_option_dump(h, debug);
|
||||
if (tv.tv_sec || tv.tv_usec){
|
||||
struct timeval t;
|
||||
gettimeofday(&t, NULL);
|
||||
timeradd(&t, &tv, &t);
|
||||
if (event_reg_timeout(t, timeout_fn, NULL, "timeout") < 0)
|
||||
goto done;
|
||||
}
|
||||
if (event_loop() < 0)
|
||||
goto done;
|
||||
done:
|
||||
|
|
|
|||
|
|
@ -733,20 +733,14 @@ static int
|
|||
netconf_notification_cb(int s,
|
||||
void *arg)
|
||||
{
|
||||
cxobj *xfilter = (cxobj *)arg;
|
||||
char *selector;
|
||||
struct clicon_msg *reply = NULL;
|
||||
int eof;
|
||||
char *event = NULL;
|
||||
int retval = -1;
|
||||
cbuf *cb;
|
||||
cxobj *xe = NULL; /* event xml */
|
||||
cxobj *xn = NULL; /* event xml */
|
||||
cxobj *xt = NULL; /* top xml */
|
||||
|
||||
if (0){
|
||||
fprintf(stderr, "%s\n", __FUNCTION__); /* debug */
|
||||
xml_print(stderr, xfilter); /* debug */
|
||||
}
|
||||
clicon_debug(1, "%s", __FUNCTION__);
|
||||
/* get msg (this is the reason this function is called) */
|
||||
if (clicon_msg_rcv(s, &reply, &eof) < 0)
|
||||
goto done;
|
||||
|
|
@ -756,31 +750,20 @@ netconf_notification_cb(int s,
|
|||
close(s);
|
||||
errno = ESHUTDOWN;
|
||||
event_unreg_fd(s, netconf_notification_cb);
|
||||
if (xfilter)
|
||||
xml_free(xfilter);
|
||||
goto done;
|
||||
}
|
||||
if (clicon_msg_decode(reply, &xt) < 0)
|
||||
goto done;
|
||||
if ((xe = xpath_first(xt, "//event")) != NULL)
|
||||
event = xml_body(xe);
|
||||
|
||||
/* parse event */
|
||||
if (0){ /* XXX CLICON events are not xml */
|
||||
/* find and apply filter */
|
||||
if ((selector = xml_find_value(xfilter, "select")) == NULL)
|
||||
goto done;
|
||||
if (xpath_first(xe, "%s", selector) == NULL) {
|
||||
fprintf(stderr, "%s no match\n", __FUNCTION__); /* debug */
|
||||
}
|
||||
}
|
||||
if ((xn = xpath_first(xt, "notification")) == NULL)
|
||||
goto ok;
|
||||
/* create netconf message */
|
||||
if ((cb = cbuf_new()) == NULL){
|
||||
clicon_err(OE_PLUGIN, errno, "cbuf_new");
|
||||
goto done;
|
||||
}
|
||||
add_preamble(cb); /* Make it well-formed netconf xml */
|
||||
cprintf(cb, "<notification><event>%s</event></notification>", event);
|
||||
if (clicon_xml2cbuf(cb, xn, 0, 0) < 0)
|
||||
goto done;
|
||||
add_postamble(cb);
|
||||
/* Send it to listening client on stdout */
|
||||
if (netconf_output(1, cb, "notification") < 0){
|
||||
|
|
@ -789,6 +772,7 @@ netconf_notification_cb(int s,
|
|||
}
|
||||
fflush(stdout);
|
||||
cbuf_free(cb);
|
||||
ok:
|
||||
retval = 0;
|
||||
done:
|
||||
if (xt != NULL)
|
||||
|
|
@ -802,7 +786,7 @@ netconf_notification_cb(int s,
|
|||
|
||||
<create-subscription>
|
||||
<stream>RESULT</stream> # If not present, events in the default NETCONF stream will be sent.
|
||||
<filter>XPATH-EXPR<(filter>
|
||||
<filter type="xpath" select="XPATHEXPR"/>
|
||||
<startTime/> # only for replay (NYI)
|
||||
<stopTime/> # only for replay (NYI)
|
||||
</create-subscription>
|
||||
|
|
@ -810,7 +794,7 @@ netconf_notification_cb(int s,
|
|||
* @param[in] h clicon handle
|
||||
* @param[in] xn Sub-tree (under xorig) at <rpc>...</rpc> level.
|
||||
* @param[out] xret Return XML, error or OK
|
||||
|
||||
* @see netconf_notification_cb for asynchronous stream notifications
|
||||
*/
|
||||
static int
|
||||
netconf_create_subscription(clicon_handle h,
|
||||
|
|
@ -840,7 +824,7 @@ netconf_create_subscription(clicon_handle h,
|
|||
goto done;
|
||||
if (event_reg_fd(s,
|
||||
netconf_notification_cb,
|
||||
xfilter?xml_dup(xfilter):NULL,
|
||||
NULL,
|
||||
"notification socket") < 0)
|
||||
goto done;
|
||||
ok:
|
||||
|
|
|
|||
|
|
@ -13,10 +13,15 @@ Define nginx config file: /etc/nginx/sites-available/default
|
|||
```
|
||||
server {
|
||||
...
|
||||
location / {
|
||||
root /usr/share/nginx/html/restconf;
|
||||
fastcgi_pass unix:/www-data/fastcgi_restconf.sock;
|
||||
include fastcgi_params;
|
||||
location /restconf {
|
||||
fastcgi_pass unix:/www-data/fastcgi_restconf.sock;
|
||||
include fastcgi_params;
|
||||
}
|
||||
location /stream { # for restconf notifications
|
||||
fastcgi_pass unix:/www-data/fastcgi_restconf.sock;
|
||||
include fastcgi_params;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Connection "";
|
||||
}
|
||||
}
|
||||
```
|
||||
|
|
|
|||
|
|
@ -36,6 +36,12 @@
|
|||
#ifndef _RESTCONF_LIB_H_
|
||||
#define _RESTCONF_LIB_H_
|
||||
|
||||
/*
|
||||
* Constants
|
||||
*/
|
||||
#define RESTCONF_API "restconf"
|
||||
#define RESTCONF_STREAM "stream"
|
||||
|
||||
/*
|
||||
* Prototypes (also in clixon_restconf.h)
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -89,12 +89,6 @@
|
|||
*/
|
||||
#define RESTCONF_WELL_KNOWN "/.well-known/host-meta"
|
||||
|
||||
#define RESTCONF_API "restconf"
|
||||
#define RESTCONF_API_ROOT "/restconf"
|
||||
#define RESTCONF_STREAM_ROOT "/stream"
|
||||
|
||||
#define RESTCONF_LOGFILE "/www-data/clixon_restconf.log"
|
||||
|
||||
/*! Generic REST method, GET, PUT, DELETE, etc
|
||||
* @param[in] h CLIXON handle
|
||||
* @param[in] r Fastcgi request handle
|
||||
|
|
@ -531,9 +525,11 @@ main(int argc,
|
|||
|
||||
/* In the startup, logs to stderr & debug flag set later */
|
||||
clicon_log_init(__PROGRAM__, LOG_INFO, logdst);
|
||||
|
||||
/* Create handle */
|
||||
if ((h = clicon_handle_init()) == NULL)
|
||||
goto done;
|
||||
|
||||
_CLICON_HANDLE = h; /* for termination handling */
|
||||
while ((c = getopt(argc, argv, RESTCONF_OPTS)) != -1)
|
||||
switch (c) {
|
||||
|
|
@ -634,6 +630,8 @@ main(int argc,
|
|||
yang_spec_append(h, CLIXON_DATADIR, "ietf-yang-library", NULL)< 0)
|
||||
goto done;
|
||||
|
||||
if (stream_register(h, "NETCONF", "default NETCONF event stream") < 0)
|
||||
goto done;
|
||||
/* Call start function in all plugins before we go interactive
|
||||
Pass all args after the standard options to plugin_start
|
||||
*/
|
||||
|
|
@ -667,9 +665,9 @@ main(int argc,
|
|||
clicon_debug(1, "------------");
|
||||
if ((path = FCGX_GetParam("REQUEST_URI", r->envp)) != NULL){
|
||||
clicon_debug(1, "path: %s", path);
|
||||
if (strncmp(path, RESTCONF_API_ROOT, strlen(RESTCONF_API_ROOT)) == 0)
|
||||
if (strncmp(path, "/" RESTCONF_API, strlen("/" RESTCONF_API)) == 0)
|
||||
api_restconf(h, r); /* This is the function */
|
||||
else if (strncmp(path, RESTCONF_STREAM_ROOT, strlen(RESTCONF_STREAM_ROOT)) == 0) {
|
||||
else if (strncmp(path, "/" RESTCONF_STREAM, strlen("/" RESTCONF_STREAM)) == 0) {
|
||||
api_stream(h, r);
|
||||
}
|
||||
else if (strncmp(path, RESTCONF_WELL_KNOWN, strlen(RESTCONF_WELL_KNOWN)) == 0) {
|
||||
|
|
|
|||
|
|
@ -64,6 +64,32 @@
|
|||
|
||||
#include <fcgi_stdio.h> /* Need to be after clixon_xml-h due to attribute format */
|
||||
|
||||
static int
|
||||
restconf_stream(clicon_handle h,
|
||||
FCGX_Request *r,
|
||||
event_stream_t *es)
|
||||
{
|
||||
int retval = -1;
|
||||
|
||||
clicon_debug(1, "%s", __FUNCTION__);
|
||||
|
||||
FCGX_SetExitStatus(201, r->out); /* Created */
|
||||
FCGX_FPrintF(r->out, "Content-Type: text/event-stream\r\n");
|
||||
FCGX_FPrintF(r->out, "Cache-Control: no-cache\r\n");
|
||||
FCGX_FPrintF(r->out, "Connection: keep-alive\r\n");
|
||||
FCGX_FPrintF(r->out, "X-Accel-Buffering: no\r\n");
|
||||
FCGX_FPrintF(r->out, "\r\n");
|
||||
FCGX_FPrintF(r->out, "Here is output\r\n");
|
||||
FCGX_FPrintF(r->out, "\r\n");
|
||||
FCGX_FFlush(r->out);
|
||||
sync();
|
||||
sleep(1);
|
||||
FCGX_FPrintF(r->out, "Here is output 2\r\n");
|
||||
retval = 0;
|
||||
// done:
|
||||
return retval;
|
||||
}
|
||||
|
||||
/* restconf */
|
||||
#include "restconf_lib.h"
|
||||
#include "restconf_stream.h"
|
||||
|
|
@ -73,13 +99,122 @@
|
|||
*/
|
||||
int
|
||||
api_stream(clicon_handle h,
|
||||
FCGX_Request *r)
|
||||
FCGX_Request *r)
|
||||
{
|
||||
int retval = -1;
|
||||
char *path;
|
||||
char *query;
|
||||
char *method;
|
||||
char **pvec = NULL;
|
||||
int pn;
|
||||
cvec *qvec = NULL;
|
||||
cvec *dvec = NULL;
|
||||
cvec *pcvec = NULL; /* for rest api */
|
||||
cbuf *cb = NULL;
|
||||
char *data;
|
||||
int authenticated = 0;
|
||||
char *media_accept;
|
||||
char *media_content_type;
|
||||
int pretty;
|
||||
int parse_xml = 0; /* By default expect and parse JSON */
|
||||
int use_xml = 0; /* By default use JSON */
|
||||
cbuf *cbret = NULL;
|
||||
cxobj *xret = NULL;
|
||||
cxobj *xerr;
|
||||
event_stream_t *es;
|
||||
|
||||
clicon_debug(1, "%s", __FUNCTION__);
|
||||
path = FCGX_GetParam("REQUEST_URI", r->envp);
|
||||
query = FCGX_GetParam("QUERY_STRING", r->envp);
|
||||
pretty = clicon_option_bool(h, "CLICON_RESTCONF_PRETTY");
|
||||
/* get xml/json in put and output */
|
||||
media_accept = FCGX_GetParam("HTTP_ACCEPT", r->envp);
|
||||
if (media_accept && strcmp(media_accept, "application/yang-data+xml")==0)
|
||||
use_xml++;
|
||||
media_content_type = FCGX_GetParam("HTTP_CONTENT_TYPE", r->envp);
|
||||
if (media_content_type &&
|
||||
strcmp(media_content_type, "application/yang-data+xml")==0)
|
||||
parse_xml++;
|
||||
if ((pvec = clicon_strsep(path, "/", &pn)) == NULL)
|
||||
goto done;
|
||||
/* Sanity check of path. Should be /stream/<name> */
|
||||
if (pn != 3){
|
||||
notfound(r);
|
||||
goto ok;
|
||||
}
|
||||
if (strlen(pvec[0]) != 0){
|
||||
retval = notfound(r);
|
||||
goto done;
|
||||
}
|
||||
if (strcmp(pvec[1], RESTCONF_STREAM)){
|
||||
retval = notfound(r);
|
||||
goto done;
|
||||
}
|
||||
test(r, 1);
|
||||
if ((method = pvec[2]) == NULL){
|
||||
retval = notfound(r);
|
||||
goto done;
|
||||
}
|
||||
clicon_debug(1, "%s: method=%s", __FUNCTION__, method);
|
||||
if (str2cvec(query, '&', '=', &qvec) < 0)
|
||||
goto done;
|
||||
if (str2cvec(path, '/', '=', &pcvec) < 0) /* rest url eg /album=ricky/foo */
|
||||
goto done;
|
||||
/* data */
|
||||
if ((cb = readdata(r)) == NULL)
|
||||
goto done;
|
||||
data = cbuf_get(cb);
|
||||
clicon_debug(1, "%s DATA=%s", __FUNCTION__, data);
|
||||
if (str2cvec(data, '&', '=', &dvec) < 0)
|
||||
goto done;
|
||||
|
||||
/* If present, check credentials. See "plugin_credentials" in plugin
|
||||
* See RFC 8040 section 2.5
|
||||
*/
|
||||
if ((authenticated = clixon_plugin_auth(h, r)) < 0)
|
||||
goto done;
|
||||
clicon_debug(1, "%s auth:%d %s", __FUNCTION__, authenticated, clicon_username_get(h));
|
||||
|
||||
/* If set but no user, we set a dummy user */
|
||||
if (authenticated){
|
||||
if (clicon_username_get(h) == NULL)
|
||||
clicon_username_set(h, "none");
|
||||
}
|
||||
else{
|
||||
if (netconf_access_denied_xml(&xret, "protocol", "The requested URL was unauthorized") < 0)
|
||||
goto done;
|
||||
if ((xerr = xpath_first(xret, "//rpc-error")) != NULL){
|
||||
if (api_return_err(h, r, xerr, pretty, use_xml) < 0)
|
||||
goto done;
|
||||
goto ok;
|
||||
}
|
||||
goto ok;
|
||||
}
|
||||
clicon_debug(1, "%s auth2:%d %s", __FUNCTION__, authenticated, clicon_username_get(h));
|
||||
if ((es = stream_find(h, method)) == NULL){
|
||||
retval = notfound(r);
|
||||
goto done;
|
||||
}
|
||||
if (restconf_stream(h, r, es) < 0)
|
||||
goto done;
|
||||
ok:
|
||||
retval = 0;
|
||||
// done:
|
||||
done:
|
||||
clicon_debug(1, "%s retval:%d", __FUNCTION__, retval);
|
||||
if (pvec)
|
||||
free(pvec);
|
||||
if (dvec)
|
||||
cvec_free(dvec);
|
||||
if (qvec)
|
||||
cvec_free(qvec);
|
||||
if (pcvec)
|
||||
cvec_free(pcvec);
|
||||
if (cb)
|
||||
cbuf_free(cb);
|
||||
if (cbret)
|
||||
cbuf_free(cbret);
|
||||
if (xret)
|
||||
xml_free(xret);
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue