Skip to content

Commit c95d9ff

Browse files
committed
pyapi CHANGE support SSH channels
netconf2.Session.newChannel() to connect to the server via another SSH channel.
1 parent 6c0951e commit c95d9ff

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

python/session.c

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
typedef struct {
2727
PyObject_HEAD
2828
struct ly_ctx *ctx;
29+
unsigned int *ctx_counter;
2930
struct nc_session *session;
3031
} ncSessionObject;
3132

@@ -115,7 +116,12 @@ ncSessionFree(ncSessionObject *self)
115116
PyErr_Fetch(&err_type, &err_value, &err_traceback);
116117

117118
nc_session_free(self->session, NULL);
118-
ly_ctx_destroy(self->ctx, NULL);
119+
120+
(*self->ctx_counter)--;
121+
if (!(*self->ctx_counter)) {
122+
ly_ctx_destroy(self->ctx, NULL);
123+
free(self->ctx_counter);
124+
}
119125

120126
/* restore the saved exception state */
121127
PyErr_Restore(err_type, err_value, err_traceback);
@@ -132,13 +138,15 @@ ncSessionNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
132138
if (self != NULL) {
133139
/* NULL initiation */
134140
self->session = NULL;
141+
self->ctx_counter = calloc(1, sizeof *self->ctx_counter);
135142

136143
/* prepare libyang context or use the one already present in the session */
137144
self->ctx = ly_ctx_new(SCHEMAS_DIR);
138145
if (!self->ctx) {
139146
Py_DECREF(self);
140147
return NULL;
141148
}
149+
(*self->ctx_counter)++;
142150
}
143151

144152
return (PyObject *)self;
@@ -213,6 +221,33 @@ ncSessionInit(ncSessionObject *self, PyObject *args, PyObject *kwds)
213221
return 0;
214222
}
215223

224+
static PyObject *
225+
newChannel(PyObject *self)
226+
{
227+
ncSessionObject *new;
228+
229+
if (nc_session_get_ti(((ncSessionObject *)self)->session) != NC_TI_LIBSSH) {
230+
PyErr_SetString(PyExc_TypeError, "The session must be on SSH.");
231+
return NULL;
232+
}
233+
234+
new = (ncSessionObject *)self->ob_type->tp_alloc(self->ob_type, 0);
235+
if (!new) {
236+
return NULL;
237+
}
238+
239+
new->ctx = ((ncSessionObject *)self)->ctx;
240+
new->session = nc_connect_ssh_channel(((ncSessionObject *)self)->session, new->ctx);
241+
if (!new->session) {
242+
Py_DECREF(new);
243+
return NULL;
244+
}
245+
246+
new->ctx_counter = ((ncSessionObject *)self)->ctx_counter;
247+
(*new->ctx_counter)++;
248+
return (PyObject*)new;
249+
}
250+
216251
static PyObject *
217252
ncSessionStr(ncSessionObject *self)
218253
{
@@ -332,6 +367,10 @@ static PyMemberDef ncSessionMembers[] = {
332367
};
333368

334369
static PyMethodDef ncSessionMethods[] = {
370+
{"newChannel", (PyCFunction)newChannel, METH_NOARGS,
371+
"newChannel()\n--\n\n"
372+
"Create another NETCONF session on existing SSH session using separated SSH channel\n\n"
373+
":returns: New netconf2.Session instance.\n"},
335374
{NULL} /* Sentinel */
336375
};
337376

0 commit comments

Comments
 (0)