* rpc msg C API rearranged to separate socket/connect from connect

This commit is contained in:
Olof hagsand 2021-01-12 14:03:07 +01:00
parent 8cde7a4ded
commit 2a392ca0e9
11 changed files with 93 additions and 69 deletions

View file

@ -53,10 +53,10 @@ CLIXON_MINOR = @CLIXON_VERSION_MINOR@
VPATH = @srcdir@
CC = @CC@
LINKAGE = @LINKAGE@
ifeq ($(LINKAGE),dynamic)
CFLAGS = -fPIC @CFLAGS@
else
ifeq ($(LINKAGE),static)
CFLAGS = @CFLAGS@
else
CFLAGS = -fPIC @CFLAGS@
endif
SH_SUFFIX = @SH_SUFFIX@
INSTALL = @INSTALL@
@ -107,10 +107,10 @@ MYLIBSO = lib$(MYNAME)$(SH_SUFFIX).$(CLIXON_MAJOR)
MYLIBLINK = lib$(MYNAME)$(SH_SUFFIX)
MYLIBSTATIC = lib$(MYNAME).a
ifeq ($(LINKAGE),dynamic)
MYLIB = $(MYLIBDYNAMIC)
else
ifeq ($(LINKAGE),static)
MYLIB = $(MYLIBSTATIC)
else
MYLIB = $(MYLIBDYNAMIC)
endif
all: $(MYLIB) $(MYLIBLINK)

View file

@ -315,6 +315,7 @@ clicon_debug_init(int dbglevel,
FILE *f)
{
_clixon_debug = dbglevel; /* Global variable */
_logfile = f;
return 0;
}

View file

@ -562,16 +562,18 @@ clicon_msg_rcv(int s,
*/
int
clicon_rpc_connect_unix(clicon_handle h,
struct clicon_msg *msg,
char *sockpath,
char **retdata,
int *sock0)
{
int retval = -1;
int s = -1;
struct stat sb;
int retval = -1;
int s = -1;
struct stat sb = {0,};
clicon_debug(1, "Send msg on %s", sockpath);
if (sock0 == NULL){
clicon_err(OE_NETCONF, EINVAL, "sock0 expected");
goto done;
}
/* special error handling to get understandable messages (otherwise ENOENT) */
if (stat(sockpath, &sb) < 0){
clicon_err(OE_PROTO, errno, "%s: config daemon not running?", sockpath);
@ -583,14 +585,9 @@ clicon_rpc_connect_unix(clicon_handle h,
}
if ((s = clicon_connect_unix(h, sockpath)) < 0)
goto done;
if (clicon_rpc(s, msg, retdata) < 0)
goto done;
if (sock0 != NULL)
*sock0 = s;
*sock0 = s;
retval = 0;
done:
if (sock0 == NULL && s >= 0)
close(s);
return retval;
}
@ -608,10 +605,8 @@ clicon_rpc_connect_unix(clicon_handle h,
*/
int
clicon_rpc_connect_inet(clicon_handle h,
struct clicon_msg *msg,
char *dst,
uint16_t port,
char **retdata,
int *sock0)
{
int retval = -1;
@ -619,7 +614,10 @@ clicon_rpc_connect_inet(clicon_handle h,
struct sockaddr_in addr;
clicon_debug(1, "Send msg to %s:%hu", dst, port);
if (sock0 == NULL){
clicon_err(OE_NETCONF, EINVAL, "sock0 expected");
goto done;
}
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
@ -636,14 +634,9 @@ clicon_rpc_connect_inet(clicon_handle h,
close(s);
goto done;
}
if (clicon_rpc(s, msg, retdata) < 0)
goto done;
if (sock0 != NULL)
*sock0 = s;
*sock0 = s;
retval = 0;
done:
if (sock0 == NULL && s >= 0)
close(s);
return retval;
}

View file

@ -82,31 +82,16 @@
#include "clixon_netconf_lib.h"
#include "clixon_proto_client.h"
/*! Send internal netconf rpc from client to backend
* @param[in] h CLICON handle
* @param[in] msg Encoded message. Deallocate with free
* @param[out] xret0 Return value from backend as xml tree. Free w xml_free
* @param[inout] sock0 If pointer exists, do not close socket to backend on success
* and return it here. For keeping a notify socket open
* @note sock0 is if connection should be persistent, like a notification/subscribe api
* @note xret is populated with yangspec according to standard handle yangspec
/*! Connect to internal netconf socket
*/
int
clicon_rpc_msg(clicon_handle h,
struct clicon_msg *msg,
cxobj **xret0,
int *sock0)
clicon_rpc_connect(clicon_handle h,
int *sock0)
{
int retval = -1;
char *sock;
int port;
char *retdata = NULL;
cxobj *xret = NULL;
int retval = -1;
char *sock = NULL;
int port;
#ifdef RPC_USERNAME_ASSERT
assert(strstr(msg->op_body, "username")!=NULL); /* XXX */
#endif
clicon_debug(1, "%s request:%s", __FUNCTION__, msg->op_body);
if ((sock = clicon_sock(h)) == NULL){
clicon_err(OE_FATAL, 0, "CLICON_SOCK option not set");
goto done;
@ -114,7 +99,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(h, msg, sock, &retdata, sock0) < 0){
if (clicon_rpc_connect_unix(h, sock, sock0) < 0){
#if 0
if (errno == ESHUTDOWN)
/* Maybe could reconnect on a higher layer, but lets fail
@ -133,10 +118,45 @@ clicon_rpc_msg(clicon_handle h,
clicon_err(OE_FATAL, 0, "CLICON_SOCK_PORT not set");
goto done;
}
if (clicon_rpc_connect_inet(h, msg, sock, port, &retdata, sock0) < 0)
if (clicon_rpc_connect_inet(h, sock, port, sock0) < 0)
goto done;
break;
}
retval = 0;
done:
return retval;
}
/*! Send internal netconf rpc from client to backend
* @param[in] h CLICON handle
* @param[in] msg Encoded message. Deallocate with free
* @param[out] xret0 Return value from backend as xml tree. Free w xml_free
* @param[inout] sock0 If pointer exists, do not close socket to backend on success
* and return it here. For keeping a notify socket open
* @note sock0 is if connection should be persistent, like a notification/subscribe api
* @note xret is populated with yangspec according to standard handle yangspec
*/
int
clicon_rpc_msg(clicon_handle h,
struct clicon_msg *msg,
cxobj **xret0,
int *sock0)
{
int retval = -1;
char *retdata = NULL;
cxobj *xret = NULL;
int s = -1;
#ifdef RPC_USERNAME_ASSERT
assert(strstr(msg->op_body, "username")!=NULL); /* XXX */
#endif
clicon_debug(1, "%s request:%s", __FUNCTION__, msg->op_body);
/* Create a socket and connect to it, either UNIX, IPv4 or IPv6 per config options */
if (clicon_rpc_connect(h, &s) < 0)
goto done;
if (clicon_rpc(s, msg, &retdata) < 0)
goto done;
clicon_debug(1, "%s retdata:%s", __FUNCTION__, retdata);
if (retdata){
@ -150,8 +170,15 @@ clicon_rpc_msg(clicon_handle h,
*xret0 = xret;
xret = NULL;
}
/* If returned, keep socket open, otherwise close it below */
if (sock0){
*sock0 = s;
s = -1;
}
retval = 0;
done:
if (s != -1)
close(s);
if (retdata)
free(retdata);
if (xret)

View file

@ -706,7 +706,7 @@ xpath_first_localonly(cxobj *xcur,
* @retval -1 Error
* @code
* cvec *nsc; // namespace context
* cxobj **vec;
* cxobj **vec = NULL;
* size_t veclen;
* if (xpath_vec(xcur, nsc, "//symbol/foo", &vec, &veclen) < 0)
* err;