diff --git a/CHANGELOG.md b/CHANGELOG.md index d92916e9..f8baf5c7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ * Fixed multi-namespace for augmented state which was not covered in 4.2.0. ### API changes on existing features (you may need to change your code) +* C-API: Added clicon_handle as parameter to all `clicon_connect_` functions to get better error message * Yang files reorganized into three classes: clixon, mandatory, optional (previous "standard" split into mandatory and optional). * Clixon and mandatory yang spec are always installed * Optional yang files are loaded only if configured with `--enable-optyangs` (flipped logic and changed from `disable-stdyangs`). NOTE: you must do this to run examples and tests. diff --git a/lib/clixon/clixon_data.h b/lib/clixon/clixon_data.h index 3f173e04..42981a3b 100644 --- a/lib/clixon/clixon_data.h +++ b/lib/clixon/clixon_data.h @@ -40,12 +40,6 @@ #ifndef _CLIXON_DATA_H_ #define _CLIXON_DATA_H_ -/* - * Constants - */ -/* default group membership to access config unix socket */ -#define CLICON_SOCK_GROUP "clicon" - /* * Types */ diff --git a/lib/clixon/clixon_options.h b/lib/clixon/clixon_options.h index fdca3460..6177ee71 100644 --- a/lib/clixon/clixon_options.h +++ b/lib/clixon/clixon_options.h @@ -38,12 +38,6 @@ #ifndef _CLIXON_OPTIONS_H_ #define _CLIXON_OPTIONS_H_ -/* - * Constants - */ -/* default group membership to access config unix socket */ -#define CLICON_SOCK_GROUP "clicon" - /* * Types */ diff --git a/lib/clixon/clixon_proto.h b/lib/clixon/clixon_proto.h index 5e77fcb2..97f005e4 100644 --- a/lib/clixon/clixon_proto.h +++ b/lib/clixon/clixon_proto.h @@ -69,14 +69,17 @@ struct clicon_msg *clicon_msg_encode(uint32_t id, char *format, ...); #endif int clicon_msg_decode(struct clicon_msg *msg, yang_stmt *yspec, uint32_t *id, cxobj **xml); -int clicon_connect_unix(char *sockpath); +int clicon_connect_unix(clicon_handle h, char *sockpath); -int clicon_rpc_connect_unix(struct clicon_msg *msg, + +int clicon_rpc_connect_unix(clicon_handle h, + struct clicon_msg *msg, char *sockpath, char **ret, int *sock0); -int clicon_rpc_connect_inet(struct clicon_msg *msg, +int clicon_rpc_connect_inet(clicon_handle h, + struct clicon_msg *msg, char *dst, uint16_t port, char **ret, diff --git a/lib/src/clixon_proto.c b/lib/src/clixon_proto.c index 5c7d98f7..6c1dbd04 100644 --- a/lib/src/clixon_proto.c +++ b/lib/src/clixon_proto.c @@ -71,6 +71,7 @@ #include "clixon_yang.h" #include "clixon_sig.h" #include "clixon_xml.h" +#include "clixon_options.h" #include "clixon_proto.h" static int _atomicio_sig = 0; @@ -191,12 +192,14 @@ clicon_msg_decode(struct clicon_msg *msg, } /*! Open local connection using unix domain sockets + * @param[in] h Clicon handle * @param[in] sockpath Unix domain file path - * @retval s socket - * @retval -1 error + * @retval s socket + * @retval -1 error */ int -clicon_connect_unix(char *sockpath) +clicon_connect_unix(clicon_handle h, + char *sockpath) { struct sockaddr_un addr; int retval = -1; @@ -213,9 +216,9 @@ clicon_connect_unix(char *sockpath) clicon_debug(2, "%s: connecting to %s", __FUNCTION__, addr.sun_path); if (connect(s, (struct sockaddr *)&addr, SUN_LEN(&addr)) < 0){ if (errno == EACCES) - clicon_err(OE_CFG, errno, "connecting unix socket: %s." - "Client should be member of group $CLICON_SOCK_GROUP: ", - sockpath); + clicon_err(OE_CFG, errno, "connecting unix socket: %s. " + "Is user not member of group: \"%s\"?", + sockpath, clicon_sock_group(h)); else clicon_err(OE_CFG, errno, "connecting unix socket: %s", sockpath); close(s); @@ -394,6 +397,7 @@ clicon_msg_rcv(int s, /*! Connect to server, send a clicon_msg message and wait for result using unix socket * + * @param[in] h Clicon handle * @param[in] msg CLICON msg data structure. It has fixed header and variable body. * @param[in] sockpath Unix domain file path * @param[out] retdata Returned data as string netconf xml tree. @@ -403,7 +407,8 @@ clicon_msg_rcv(int s, * @see clicon_rpc But this is one-shot rpc: open, send, get reply and close. */ int -clicon_rpc_connect_unix(struct clicon_msg *msg, +clicon_rpc_connect_unix(clicon_handle h, + struct clicon_msg *msg, char *sockpath, char **retdata, int *sock0) @@ -422,7 +427,7 @@ clicon_rpc_connect_unix(struct clicon_msg *msg, clicon_err(OE_PROTO, EIO, "%s: Not unix socket", sockpath); goto done; } - if ((s = clicon_connect_unix(sockpath)) < 0) + if ((s = clicon_connect_unix(h, sockpath)) < 0) goto done; if (clicon_rpc(s, msg, retdata) < 0) goto done; @@ -436,7 +441,8 @@ clicon_rpc_connect_unix(struct clicon_msg *msg, } /*! Connect to server, send a clicon_msg message and wait for result using an inet socket - * This uses unix domain socket communication + * + * @param[in] h Clicon handle * @param[in] msg CLICON msg data structure. It has fixed header and variable body. * @param[in] dst IPv4 address * @param[in] port TCP port @@ -447,7 +453,8 @@ clicon_rpc_connect_unix(struct clicon_msg *msg, * @see clicon_rpc But this is one-shot rpc: open, send, get reply and close. */ int -clicon_rpc_connect_inet(struct clicon_msg *msg, +clicon_rpc_connect_inet(clicon_handle h, + struct clicon_msg *msg, char *dst, uint16_t port, char **retdata, @@ -597,9 +604,9 @@ send_msg_notify(int s, /*! Send a clicon_msg NOTIFY message asynchronously to client * + * @param[in] h Clicon handle * @param[in] s Socket to communicate with client - * @param[in] level - * @param[in] xml Event as XML + * @param[in] xev Event as XML * @retval 0 OK * @retval -1 Error * @see send_msg_notify XXX beauty contest diff --git a/lib/src/clixon_proto_client.c b/lib/src/clixon_proto_client.c index a1de8f4e..5a3e7bd6 100644 --- a/lib/src/clixon_proto_client.c +++ b/lib/src/clixon_proto_client.c @@ -108,7 +108,7 @@ clicon_rpc_msg(clicon_handle h, /* What to do if inet socket? */ switch (clicon_sock_family(h)){ case AF_UNIX: - if (clicon_rpc_connect_unix(msg, sock, &retdata, sock0) < 0){ + if (clicon_rpc_connect_unix(h, msg, sock, &retdata, sock0) < 0){ #if 0 if (errno == ESHUTDOWN) /* Maybe could reconnect on a higher layer, but lets fail @@ -127,7 +127,7 @@ clicon_rpc_msg(clicon_handle h, clicon_err(OE_FATAL, 0, "CLICON_SOCK_PORT not set"); goto done; } - if (clicon_rpc_connect_inet(msg, sock, port, &retdata, sock0) < 0) + if (clicon_rpc_connect_inet(h, msg, sock, port, &retdata, sock0) < 0) goto done; break; } diff --git a/util/clixon_util_socket.c b/util/clixon_util_socket.c index 787b20ad..ed2c66c2 100644 --- a/util/clixon_util_socket.c +++ b/util/clixon_util_socket.c @@ -95,10 +95,14 @@ main(int argc, char *family = "UNIX"; int ret; cbuf *cb = cbuf_new(); - + clicon_handle h; + /* In the startup, logs to stderr & debug flag set later */ clicon_log_init(__FILE__, LOG_INFO, CLICON_LOG_STDERR); + if ((h = clicon_handle_init()) == NULL) + goto done; + optind = 1; opterr = 0; while ((c = getopt(argc, argv, "hD:s:f:Ja:")) != -1) @@ -161,11 +165,11 @@ main(int argc, if ((msg = clicon_msg_encode(getpid(), "%s", cbuf_get(cb))) < 0) goto done; if (strcmp(family, "UNIX")==0){ - if (clicon_rpc_connect_unix(msg, sockpath, &retdata, NULL) < 0) + if (clicon_rpc_connect_unix(h, msg, sockpath, &retdata, NULL) < 0) goto done; } else - if (clicon_rpc_connect_inet(msg, sockpath, 4535, &retdata, NULL) < 0) + if (clicon_rpc_connect_inet(h, msg, sockpath, 4535, &retdata, NULL) < 0) goto done; fprintf(stdout, "%s\n", retdata); retval = 0;