Skip to content

Commit

Permalink
Adapt to new Clixon 6.6 error, log, debug API
Browse files Browse the repository at this point in the history
  • Loading branch information
olofhagsand committed Dec 18, 2023
1 parent 7e400f2 commit d5544da
Show file tree
Hide file tree
Showing 17 changed files with 218 additions and 200 deletions.
41 changes: 21 additions & 20 deletions clixon_restconf_callhome_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,15 +213,15 @@ read_data_file(FILE *fe,
int ret;

if ((buf = malloc(buflen)) == NULL){
clicon_err(OE_UNIX, errno, "malloc");
clixon_err(OE_UNIX, errno, "malloc");
goto done;
}
memset(buf, 0, buflen);
/* Start file form beginning */
rewind(fe);
while (1){
if ((ret = fread(&ch, 1, 1, fe)) < 0){
clicon_err(OE_JSON, errno, "fread");
clixon_err(OE_JSON, errno, "fread");
goto done;
}
if (ret == 0)
Expand Down Expand Up @@ -286,7 +286,7 @@ tls_server_reply_cb(int s,
ssl = sd->sd_ssl;
/* get reply & decrypt */
if ((n = SSL_read(ssl, buf, sizeof(buf))) < 0){
clicon_err(OE_XML, errno, "SSL_read");
clixon_err(OE_XML, errno, "SSL_read");
goto done;
}
clixon_debug(CLIXON_DBG_DEFAULT, "%s n:%d", __FUNCTION__, n);
Expand Down Expand Up @@ -367,7 +367,7 @@ tls_ssl_init_connect(SSL_CTX *ctx,

/* create new SSL connection state */
if ((ssl = SSL_new(ctx)) == NULL){
clicon_err(OE_SSL, 0, "SSL_new.");
clixon_err(OE_SSL, 0, "SSL_new.");
goto done;
}
SSL_set_fd(ssl, s); /* attach the socket descriptor */
Expand All @@ -376,7 +376,7 @@ tls_ssl_init_connect(SSL_CTX *ctx,
protos[0] = 8;
strncpy((char*)&protos[1], "http/1.1", 9);
if ((retval = SSL_set_alpn_protos(ssl, protos, 9)) != 0){
clicon_err(OE_SSL, retval, "SSL_set_alpn_protos.");
clixon_err(OE_SSL, retval, "SSL_set_alpn_protos.");
goto done;
}
#if 0
Expand All @@ -400,7 +400,7 @@ tls_ssl_init_connect(SSL_CTX *ctx,
goto done;
break;
default:
clicon_err(OE_XML, errno, "SSL_connect");
clixon_err(OE_XML, errno, "SSL_connect");
goto done;
break;
}
Expand All @@ -411,7 +411,7 @@ tls_ssl_init_connect(SSL_CTX *ctx,
case X509_V_OK:
break;
default:
clicon_err(OE_SSL, errno, "verify problems: %d", verify);
clixon_err(OE_SSL, errno, "verify problems: %d", verify);
goto done;
}
*sslp = ssl;
Expand Down Expand Up @@ -480,7 +480,7 @@ tls_server_accept_cb(int ss,
goto done;
clixon_debug(CLIXON_DBG_DEFAULT, "connected");
if ((sd = malloc(sizeof(*sd))) == NULL){
clicon_err(OE_UNIX, errno, "malloc");
clixon_err(OE_UNIX, errno, "malloc");
goto done;
}
memset(sd, 0, sizeof(*sd));
Expand Down Expand Up @@ -541,22 +541,22 @@ tls_ctx_init(const char *cert_path,
SSL_CTX *ctx = NULL;

if ((ctx = SSL_CTX_new(TLS_client_method())) == NULL) {
clicon_err(OE_SSL, 0, "SSL_CTX_new");
clixon_err(OE_SSL, 0, "SSL_CTX_new");
goto done;
}
SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, tls_auth_verify_callback);
/* get peer certificate
nc_client_tls_update_opts */
if (SSL_CTX_use_certificate_file(ctx, cert_path, SSL_FILETYPE_PEM) != 1) {
clicon_err(OE_SSL, 0, "SSL_CTX_use_certificate_file");
clixon_err(OE_SSL, 0, "SSL_CTX_use_certificate_file");
goto done;
}
if (SSL_CTX_use_PrivateKey_file(ctx, key_path, SSL_FILETYPE_PEM) != 1) {
clicon_err(OE_SSL, 0, "SSL_CTX_use_PrivateKey_file");
clixon_err(OE_SSL, 0, "SSL_CTX_use_PrivateKey_file");
goto done;
}
if (SSL_CTX_load_verify_locations(ctx, ca_cert_path, NULL) != 1) {
clicon_err(OE_SSL, 0, "SSL_CTX_load_verify_locations");
clixon_err(OE_SSL, 0, "SSL_CTX_load_verify_locations");
goto done;
}
(void)SSL_CTX_set_next_proto_select_cb(ctx, tls_proto_select_cb, NULL);
Expand Down Expand Up @@ -596,7 +596,7 @@ main(int argc,
char **argv)
{
int retval = -1;
clicon_handle h;
clixon_handle h;
int c;
uint16_t port = RESTCONF_CH_TLS;
SSL_CTX *ctx = NULL;
Expand All @@ -615,9 +615,10 @@ main(int argc,
char *family = "inet:ipv4-address";

/* 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)
if ((h = clixon_handle_init()) == NULL)
goto done;
clixon_log_init(h, __FILE__, LOG_INFO, CLIXON_LOG_STDERR);

while ((c = getopt(argc, argv, UTIL_TLS_OPTS)) != -1)
switch (c) {
case 'h':
Expand Down Expand Up @@ -689,11 +690,11 @@ main(int argc,
fprintf(stderr, "-c <cert path> and -k <key path> -C <ca-cert> are mandatory\n");
usage(argv[0]);
}
clixon_debug_init(dbg, NULL);
clixon_debug_init(h, dbg);

if (input_filename){
if ((_input_file = fopen(input_filename, "r")) == NULL){
clicon_err(OE_YANG, errno, "open(%s)", input_filename);
clixon_err(OE_YANG, errno, "open(%s)", input_filename);
goto done;
}
}
Expand All @@ -716,7 +717,7 @@ main(int argc,
goto done;
clixon_debug(CLIXON_DBG_DEFAULT, "callhome_bind %s:%hu", addr, port);
if ((ta = malloc(sizeof(*ta))) == NULL){
clicon_err(OE_UNIX, errno, "malloc");
clixon_err(OE_UNIX, errno, "malloc");
goto done;
}
memset(ta, 0, sizeof(*ta));
Expand All @@ -741,9 +742,9 @@ main(int argc,
close(ss);
if (ctx)
SSL_CTX_free(ctx); /* release context */
clicon_handle_exit(h); /* frees h and options (and streams) */
clixon_handle_exit(h); /* frees h and options (and streams) */
clixon_err_exit();
clixon_debug(CLIXON_DBG_DEFAULT, "clixon_restconf_callhome_client pid:%u done", getpid());
clicon_log_exit(); /* Must be after last clixon_debug */
clixon_log_exit(); /* Must be after last clixon_debug */
return retval;
}
38 changes: 19 additions & 19 deletions clixon_util_datastore.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,12 @@ usage(char *argv0)
}

int
main(int argc, char **argv)
main(int argc,
char **argv)
{
int retval = -1;
int c;
clicon_handle h;
clixon_handle h;
char *argv0;
char *db = "running";
char *cmd = NULL;
Expand All @@ -124,14 +125,13 @@ main(int argc, char **argv)
int dbg = 0;
cxobj *xerr = NULL;
cxobj *xcfg = NULL;

/* In the startup, logs to stderr & debug flag set later */
clicon_log_init(__FILE__, LOG_INFO, CLICON_LOG_STDERR);

if ((h = clixon_handle_init()) == NULL)
goto done;
clixon_log_init(h, __FILE__, LOG_INFO, CLIXON_LOG_STDERR);
argv0 = argv[0];
/* Defaults */
if ((h = clicon_handle_init()) == NULL)
goto done;
if ((xcfg = xml_new("clixon-config", NULL, CX_ELMNT)) == NULL)
goto done;
if (clicon_conf_xml_set(h, xcfg) < 0)
Expand Down Expand Up @@ -180,20 +180,20 @@ main(int argc, char **argv)
/*
* Logs, error and debug to stderr, set debug level
*/
clicon_log_init(__FILE__, dbg?LOG_DEBUG:LOG_INFO, CLICON_LOG_STDERR);
clixon_debug_init(dbg, NULL);
clixon_log_init(h, __FILE__, dbg?LOG_DEBUG:LOG_INFO, CLIXON_LOG_STDERR);
clixon_debug_init(h, dbg);

argc -= optind;
argv += optind;
if (argc < 1)
usage(argv0);
cmd = argv[0];
if (dbdir == NULL){
clicon_err(OE_DB, 0, "Missing dbdir -b option");
clixon_err(OE_DB, 0, "Missing dbdir -b option");
goto done;
}
if (yangfilename == NULL){
clicon_err(OE_YANG, 0, "Missing yang filename -y option");
clixon_err(OE_YANG, 0, "Missing yang filename -y option");
goto done;
}
/* Connect to plugin to get a handle */
Expand Down Expand Up @@ -237,7 +237,7 @@ main(int argc, char **argv)
if (xmldb_get(h, db, NULL, xpath, &xt) < 0)
goto done;
if (xt == NULL){
clicon_err(OE_DB, 0, "xt is NULL");
clixon_err(OE_DB, 0, "xt is NULL");
goto done;
}
if (clixon_xml2file(stdout, xt, 0, 0, NULL, fprintf, 0, 0) < 0)
Expand All @@ -252,22 +252,22 @@ main(int argc, char **argv)
else if (strcmp(cmd, "put")==0){
if (argc == 2){
if (xmlfilename == NULL){
clicon_err(OE_DB, 0, "XML filename expected");
clixon_err(OE_DB, 0, "XML filename expected");
usage(argv0);
}
}
else if (argc != 3){
clicon_err(OE_DB, 0, "Unexpected nr of args: %d", argc);
clixon_err(OE_DB, 0, "Unexpected nr of args: %d", argc);
usage(argv0);
}
if (xml_operation(argv[1], &op) < 0){
clicon_err(OE_DB, 0, "Unrecognized operation: %s", argv[1]);
clixon_err(OE_DB, 0, "Unrecognized operation: %s", argv[1]);
usage(argv0);
}
if (argc == 2){
FILE *fp;
if ((fp = fopen(xmlfilename, "r")) == NULL){
clicon_err(OE_UNIX, errno, "fopen(%s)", xmlfilename);
clixon_err(OE_UNIX, errno, "fopen(%s)", xmlfilename);
goto done;
}
if (clixon_xml_parse_file(fp, YB_MODULE, yspec, &xt, NULL) < 0)
Expand All @@ -285,7 +285,7 @@ main(int argc, char **argv)
if (xml_name_set(xt, NETCONF_INPUT_CONFIG) < 0)
goto done;
if ((cbret = cbuf_new()) == NULL){
clicon_err(OE_UNIX, errno, "cbuf_new");
clixon_err(OE_UNIX, errno, "cbuf_new");
goto done;
}
if ((ret = xmldb_put(h, db, op, xt, NULL, cbret)) < 0)
Expand Down Expand Up @@ -344,7 +344,7 @@ main(int argc, char **argv)
goto done;
}
else{
clicon_err(OE_DB, 0, "Unrecognized command: %s", cmd);
clixon_err(OE_DB, 0, "Unrecognized command: %s", cmd);
usage(argv0);
}
if (xmldb_disconnect(h) < 0)
Expand All @@ -358,7 +358,7 @@ main(int argc, char **argv)
if (xt)
xml_free(xt);
if (h)
clicon_handle_exit(h);
clixon_handle_exit(h);
if (yspec)
ys_free(yspec);
return retval;
Expand Down
11 changes: 7 additions & 4 deletions clixon_util_dispatcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,20 @@ main(int argc,
{
int retval = -1;
char *argv0 = argv[0];
int logdst = CLICON_LOG_STDERR;
int logdst = CLIXON_LOG_STDERR;
int dbg = 0;
int c;
char *arg = NULL;
handler_function fn = cb1;
dispatcher_entry_t *htable = NULL;
int ret;
char *regpath = "/"; /* Register path */
clixon_handle h;

/* In the startup, logs to stderr & debug flag set later */
clicon_log_init("dispatcher", LOG_DEBUG, logdst);
if ((h = clixon_handle_init()) == NULL)
goto done;
clixon_log_init(h, "dispatcher", LOG_DEBUG, logdst);

optind = 1;
opterr = 0;
Expand All @@ -140,8 +143,8 @@ main(int argc,
/*
* Logs, error and debug to stderr or syslog, set debug level
*/
clicon_log_init("xpath", dbg?LOG_DEBUG:LOG_INFO, logdst);
clixon_debug_init(dbg, NULL);
clixon_log_init(h, "xpath", dbg?LOG_DEBUG:LOG_INFO, logdst);
clixon_debug_init(h, dbg);
/* Now rest of options */
opterr = 0;
optind = 1;
Expand Down
Loading

0 comments on commit d5544da

Please sign in to comment.