Skip to content

Commit

Permalink
Merge branch 'devel'
Browse files Browse the repository at this point in the history
  • Loading branch information
michalvasko committed Sep 19, 2018
2 parents febb6bb + 94244c7 commit e466662
Show file tree
Hide file tree
Showing 13 changed files with 783 additions and 373 deletions.
12 changes: 8 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE debug)
endif()

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -fvisibility=hidden")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -fvisibility=hidden -std=gnu11")
set(CMAKE_C_FLAGS_RELEASE "-O2 -DNDEBUG")
set(CMAKE_C_FLAGS_PACKAGE "-g -O2 -DNDEBUG")
set(CMAKE_C_FLAGS_DEBUG "-g -O0")

# set version
set(LIBNETCONF2_MAJOR_VERSION 0)
set(LIBNETCONF2_MINOR_VERSION 11)
set(LIBNETCONF2_MICRO_VERSION 49)
set(LIBNETCONF2_MINOR_VERSION 12)
set(LIBNETCONF2_MICRO_VERSION 20)
set(LIBNETCONF2_VERSION ${LIBNETCONF2_MAJOR_VERSION}.${LIBNETCONF2_MINOR_VERSION}.${LIBNETCONF2_MICRO_VERSION})
set(LIBNETCONF2_SOVERSION ${LIBNETCONF2_MAJOR_VERSION}.${LIBNETCONF2_MINOR_VERSION})

Expand Down Expand Up @@ -189,7 +189,11 @@ endif()
if(ENABLE_SSH)
find_package(LibSSH 0.7.0 REQUIRED)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DNC_ENABLED_SSH")
target_link_libraries(netconf2 "-L${LIBSSH_LIBRARY_DIR}" -lssh -lssh_threads -lcrypt)
if(LibSSH_VERSION VERSION_LESS 0.8.0)
target_link_libraries(netconf2 "-L${LIBSSH_LIBRARY_DIR}" -lssh -lssh_threads -lcrypt)
else()
target_link_libraries(netconf2 "-L${LIBSSH_LIBRARY_DIR}" -lssh -lcrypt)
endif()
include_directories(${LIBSSH_INCLUDE_DIRS})
endif()

Expand Down
71 changes: 71 additions & 0 deletions python/netconf.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ PyObject *libnetconf2ReplyError;
/* syslog usage flag */
static int syslogEnabled = 0;

/* libyang schema callback */
static PyObject *schemaCallback = NULL;
static void *schemaCallbackData = NULL;

static void
clb_print(NC_VERB_LEVEL level, const char* msg)
{
Expand Down Expand Up @@ -123,6 +127,68 @@ setSearchpath(PyObject *self, PyObject *args, PyObject *keywds)
Py_RETURN_NONE;
}

char *
schemaCallbackWrapper(const char *mod_name, const char *mod_rev, const char *submod_name, const char *sub_rev,
void *user_data, LYS_INFORMAT *format, void (**free_module_data)(void *model_data))
{
PyObject *arglist, *result, *data = NULL;
char *str = NULL;

arglist = Py_BuildValue("(ssssO)", mod_name, mod_rev, submod_name, sub_rev, schemaCallbackData ? schemaCallbackData : Py_None);
if (!arglist) {
PyErr_Print();
return NULL;
}
result = PyObject_CallObject(schemaCallback, arglist);
Py_DECREF(arglist);

if (result) {
if (!PyArg_ParseTuple(result, "iU", format, &data)) {
Py_DECREF(result);
return NULL;
}
Py_DECREF(result);
*free_module_data = free;
str = strdup(PyUnicode_AsUTF8(data));
Py_DECREF(data);
}

return str;
}

static PyObject *
setSchemaCallback(PyObject *self, PyObject *args, PyObject *keywds)
{
PyObject *clb = NULL, *data = NULL;
static char *kwlist[] = {"func", "priv", NULL};

if (!PyArg_ParseTupleAndKeywords(args, keywds, "O|O:setSchemaCallback", kwlist, &clb, &data)) {
return NULL;
}

if (!clb || clb == Py_None) {
Py_XDECREF(schemaCallback);
Py_XDECREF(schemaCallbackData);
data = NULL;
} else if (!PyCallable_Check(clb)) {
PyErr_SetString(PyExc_TypeError, "The callback must be a function.");
return NULL;
} else {
Py_XDECREF(schemaCallback);
Py_XDECREF(schemaCallbackData);

Py_INCREF(clb);
if (data) {
Py_INCREF(data);
}
}
nc_client_set_schema_callback(schemaCallbackWrapper, NULL);
schemaCallback = clb;
schemaCallbackData = data;

Py_RETURN_NONE;
}

static PyMethodDef netconf2Methods[] = {
{"setVerbosity", (PyCFunction)setVerbosity, METH_VARARGS | METH_KEYWORDS,
"setVerbosity(level)\n--\n\n"
Expand Down Expand Up @@ -150,6 +216,11 @@ static PyMethodDef netconf2Methods[] = {
":param path: Search directory.\n"
":type path: string\n"
":returns: None\n"},
{"setSchemaCallback", (PyCFunction)setSchemaCallback, METH_VARARGS | METH_KEYWORDS,
"Set schema search callaback.\n\n"
"setSchemaCallback(func, priv=None)\n"
"with func(str mod_name, str mod_rev, str submod_name, str submod_rev, priv)\n"
"callback returns tuple of format (e.g. LYS_IN_YANG) and string of the schema content.\n"},
{NULL, NULL, 0, NULL}
};

Expand Down
5 changes: 4 additions & 1 deletion python/session.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ auth_interactive_pyclb(const char *auth_name, const char *instruction, const cha
}

return password;

}

char *
Expand Down Expand Up @@ -277,6 +276,10 @@ ncSessionInit(ncSessionObject *self, PyObject *args, PyObject *kwds)
return -1;
}

if (PyErr_Occurred()) {
PyErr_PrintEx(0);
}

/* get the internally created context for this session */
self->ctx = nc_session_get_ctx(session);

Expand Down
10 changes: 8 additions & 2 deletions src/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ nc_read_until(struct nc_session *session, const char *endtag, size_t limit, uint
if ((count + (len - matched)) >= size) {
/* get more memory */
size = size + BUFFERSIZE;
chunk = realloc(chunk, (size + 1) * sizeof *chunk);
chunk = nc_realloc(chunk, (size + 1) * sizeof *chunk);
if (!chunk) {
ERRMEM;
return -1;
Expand Down Expand Up @@ -357,7 +357,7 @@ nc_read_msg_io(struct nc_session *session, int io_timeout, struct lyxml_elem **d
}

/* realloc message buffer, remember to count terminating null byte */
msg = realloc(msg, len + chunk_len + 1);
msg = nc_realloc(msg, len + chunk_len + 1);
if (!msg) {
ERRMEM;
ret = NC_MSG_ERROR;
Expand Down Expand Up @@ -420,6 +420,12 @@ nc_read_msg_io(struct nc_session *session, int io_timeout, struct lyxml_elem **d
/* NETCONF version 1.1 defines sending error reply from the server (RFC 6241 sec. 3) */
reply = nc_server_reply_err(nc_err(NC_ERR_MALFORMED_MSG));

if (io_locked) {
/* nc_write_msg_io locks and unlocks the lock by itself */
nc_session_io_unlock(session, __func__);
io_locked = 0;
}

if (nc_write_msg_io(session, io_timeout, NC_MSG_REPLY, NULL, reply) != NC_MSG_REPLY) {
ERR("Session %u: unable to send a \"Malformed message\" error reply, terminating session.", session->id);
if (session->status != NC_STATUS_INVALID) {
Expand Down
5 changes: 4 additions & 1 deletion src/libnetconf.h
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,9 @@
* So, after starting listening on an endpoint you need to set the server
* certificate (nc_server_tls_endpt_set_server_cert()). Its actual content
* together with the matching private key will be loaded using a callback
* from nc_server_tls_set_server_cert_clb().
* from nc_server_tls_set_server_cert_clb(). Additional certificates needed
* for the client to verify the server's certificate chain can be loaded using
* a callback from nc_server_tls_set_server_cert_chain_clb().
*
* To accept client certificates, they must first be considered trusted,
* which you have three ways of achieving. You can add each of their Certificate Authority
Expand Down Expand Up @@ -428,6 +430,7 @@
* - nc_server_tls_endpt_get_ctn()
*
* - nc_server_tls_set_server_cert_clb()
* - nc_server_tls_set_server_cert_chain_clb()
* - nc_server_tls_set_trusted_cert_list_clb()
*
* FD
Expand Down
1 change: 1 addition & 0 deletions src/messages_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#define NC_MESSAGES_SERVER_H_

#include <stdint.h>
#include <libyang/libyang.h>

#include "netconf.h"
#include "session.h"
Expand Down
9 changes: 5 additions & 4 deletions src/session.c
Original file line number Diff line number Diff line change
Expand Up @@ -933,9 +933,10 @@ nc_server_get_cpblts_version(struct ly_ctx *ctx, LYS_VERSION version)
u = module_set_id = 0;
while ((mod = ly_ctx_get_module_iter(ctx, &u))) {
if (!strcmp(mod->name, "ietf-yang-library")) {
/* ietf-yang-library is always part of the list, but it is specific since it is 1.1 schema */
sprintf(str, "%s?%s%s&module-set-id=%u", mod->ns, mod->rev_size ? "revision=" : "",
mod->rev_size ? mod->rev[0].date : "", ly_ctx_get_module_set_id(ctx));
/* Add the yang-library NETCONF capability as defined in RFC 7950 5.6.4 */
sprintf(str, "urn:ietf:params:netconf:capability:yang-library:1.0?%s%s&module-set-id=%u",
mod->rev_size ? "revision=" : "", mod->rev_size ? mod->rev[0].date : "",
ly_ctx_get_module_set_id(ctx));
add_cpblt(ctx, str, &cpblts, &size, &count);
continue;
} else if (mod->type) {
Expand Down Expand Up @@ -967,7 +968,7 @@ nc_server_get_cpblts_version(struct ly_ctx *ctx, LYS_VERSION version)
ERRINT;
break;
}
if (i) {
if (features_count) {
strcat(str, ",");
++str_len;
}
Expand Down
Loading

0 comments on commit e466662

Please sign in to comment.