LibSSH.xs

filter: Statementstmt 20.5% 41 / 200 Branchbran 25.4% 26 / 102 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 22.1% 67 / 302 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary blib/lib/Net/LibSSH.pm »
1 #define PERL_NO_GET_CONTEXT
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5 #define NEED_mg_findext
6 #include "ppport.h"
7  
8 #include <libssh/libssh.h>
9 #include <libssh/sftp.h>
10  
11 #define undef &PL_sv_undef
12  
13 /* ====================================================
14 Internal structs (named so Newxz has a type to size)
15 ==================================================== */
16  
17 /* Where the ssh_session is in its one-way life. libssh 0.10.6 cannot
18 reconnect a session it has disconnected -- ssh_connect() sits out the whole
19 timeout -- but measured on the same libssh, disconnect() on a session that
20 was never connected leaves it fully connectable. Only the CONNECTED ->
21 SPENT transition is therefore terminal, which is why this is three states
22 and not a flag. Newxz gives FRESH for free.
23 libssh has nothing to answer this with: ssh_get_status() reports SSH_CLOSED
24 for both a spent session and a never-connected one that saw a disconnect(),
25 and ssh_is_connected() is already 0 by the time connect() would ask. */
26 typedef enum {
27 NLSS_SESSION_FRESH = 0,
28 NLSS_SESSION_CONNECTED,
29 NLSS_SESSION_SPENT
30 } NLSS_SessionState;
31  
32 /* generation / session_generation: ssh_disconnect() frees every ssh_channel
33 the session owns from inside libssh, so a channel or sftp session that
34 outlives a disconnect() holds a non-NULL pointer into freed memory. The
35 session counts its disconnects; each child copies the count it was born
36 under and compares. See nlss_session_stale(). */
37 typedef struct {
38 ssh_session session;
39 unsigned int generation;
40 NLSS_SessionState state;
41 /* The one message this binding raises itself; everything else in error()
42 comes from libssh. Set when connect() refuses a spent session, where
43 ssh_connect() is never called and ssh_get_error() would still hold what
44 stood there before the disconnect (measured: nothing). A string
45 literal, so there is nothing to free, and nothing clears it either --
46 a spent session stays spent, and that is the reason every later call
47 on it fails too. */
48 const char *own_error;
49 /* Mirror of SSH_OPTIONS_STRICTHOSTKEYCHECK, kept here because libssh
50 has no getter for it and connect() has to know. Inverted so that the
51 zero Newxz leaves behind means strict -- the secure default, and
52 libssh's own. Only option('strict_hostkeycheck') writes it. */
53 int hostkey_check_disabled;
54 } NLSS_Session;
55  
56 typedef struct {
57 ssh_channel channel;
58 SV *session_sv;
59 unsigned int session_generation;
60 } NLSS_Channel;
61  
62 typedef struct {
63 sftp_session sftp;
64 SV *session_sv;
65 unsigned int session_generation;
66 } NLSS_SFTP;
67  
68 /* Pointer typedefs with the xsubpp __ → :: naming convention.
69 Using pointer typedefs (not struct typedefs) means XS signatures
70 need no *. The typemap does not derive its vtable names from these:
71 xsubpp maps :: → __ inside ${type} only from 3.60 on, so a generic
72 T_MAGICEXT entry writing &${type}_magic expands to &Net::LibSSH_magic
73 — not valid C — on the older xsubpp this distribution still supports.
74 Every typemap entry therefore spells its vtable out literally. */
75 typedef NLSS_Session *Net__LibSSH;
76 typedef NLSS_Channel *Net__LibSSH__Channel;
77 typedef NLSS_SFTP *Net__LibSSH__SFTP;
78  
79 /* ====================================================
80 Magic vtables — svt_free replaces DESTROY in XS.
81 Perl's GC calls svt_free automatically when the SV
82 is collected; no explicit DESTROY method needed.
83 ==================================================== */
84  
85 static int
86 3 nlss_session_free(pTHX_ SV *sv, MAGIC *mg)
87 {
88 3 NLSS_Session *self = (NLSS_Session *)(void *)mg->mg_ptr;
89 /* No generation bump here: every channel and sftp session holds a
90 reference on this very SV, so this runs only once the last of them is
91 gone. There is nothing left to invalidate. */
92 3 ▶ if (self->session) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->session) { 3 0 3
93 3 ssh_disconnect(self->session);
94 3 ssh_free(self->session);
95 }
96 3 Safefree(self);
97 3 return 0;
98 }
99 static const MGVTBL Net__LibSSH_magic = { .svt_free = nlss_session_free };
100  
101 /* Has libssh already freed the C object this child holds?
102 The child reaches its session through session_sv -- the blessed,
103 magic-bearing SV it took a reference on in channel()/sftp(). Since 0.003
104 that is the referent, SvRV(ST(0)), not the reference scalar, so the magic
105 sits on session_sv itself and mg_findext takes it directly; a further
106 SvRV() here would walk off the object.
107 Missing magic can only mean the session SV is not the one we stored, so it
108 counts as stale: refusing is the safe answer for both the method guard and
109 the free path. */
110 static int
111 0 nlss_session_stale(pTHX_ SV *session_sv, unsigned int generation)
112 {
113 0 ▶ if (!session_sv || !SvMAGICAL(session_sv))
Branch: 2 branches
BranchTrueFalseTotal
if (!session_sv || !SvMAGICAL(session_sv)) 0 0 0
if (!session_sv || !SvMAGICAL(session_sv)) 0 0 0
114 0 return 1;
115 0 MAGIC *mg = mg_findext(session_sv, PERL_MAGIC_ext, &Net__LibSSH_magic);
116 0 ▶ if (!mg)
Branch: 1 branch
BranchTrueFalseTotal
if (!mg) 0 0 0
117 0 return 1;
118 0 return ((NLSS_Session *)(void *)mg->mg_ptr)->generation != generation;
119 }
120  
121 static int
122 0 nlss_channel_free(pTHX_ SV *sv, MAGIC *mg)
123 {
124 0 NLSS_Channel *self = (NLSS_Channel *)(void *)mg->mg_ptr;
125 /* A stale channel was freed inside ssh_disconnect() already; sending eof
126 on it is the crash the caller never asked for. Skip the C teardown
127 only -- the session reference and the struct still have to go, or the
128 crash is merely traded for a leak. The session struct is still there to
129 ask: this child's reference on session_sv is released below, after the
130 question, so nlss_session_free cannot have run yet. */
131 0 ▶ if (self->channel
Branch: 1 branch
BranchTrueFalseTotal
if (self->channel 0 0 0
132 0 ▶ && !nlss_session_stale(aTHX_ self->session_sv, self->session_generation)) {
Branch: 1 branch
BranchTrueFalseTotal
&& !nlss_session_stale(aTHX_ self->session_sv, self->session_generation)) { 0 0 0
133 0 ssh_channel_send_eof(self->channel);
134 0 ssh_channel_close(self->channel);
135 0 ssh_channel_free(self->channel);
136 }
137 0 SvREFCNT_dec(self->session_sv);
138 0 Safefree(self);
139 0 return 0;
140 }
141 static const MGVTBL Net__LibSSH__Channel_magic = { .svt_free = nlss_channel_free };
142  
143 static int
144 0 nlss_sftp_free(pTHX_ SV *sv, MAGIC *mg)
145 {
146 0 NLSS_SFTP *self = (NLSS_SFTP *)(void *)mg->mg_ptr;
147 0 ▶ if (self->sftp) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->sftp) { 0 0 0
148 /* Unlike a channel, an sftp_session is not freed by ssh_disconnect()
149 -- only the channel inside it is. Skipping sftp_free() outright
150 would trade the crash for a leak (measured: ~1.6 kB per
151 disconnected-and-dropped sftp session), so cut the dangling channel
152 loose first and let sftp_free() release everything else. libssh
153 declares struct sftp_session_struct in its public sftp.h, and its
154 ssh_channel_send_eof/close/free are all NULL-tolerant. */
155 0 ▶ if (nlss_session_stale(aTHX_ self->session_sv, self->session_generation))
Branch: 1 branch
BranchTrueFalseTotal
if (nlss_session_stale(aTHX_ self->session_sv, self->session_generation)) 0 0 0
156 0 self->sftp->channel = NULL;
157 0 sftp_free(self->sftp);
158 }
159 0 SvREFCNT_dec(self->session_sv);
160 0 Safefree(self);
161 0 return 0;
162 }
163 static const MGVTBL Net__LibSSH__SFTP_magic = { .svt_free = nlss_sftp_free };
164  
165 /* ====================================================
166 Helper functions
167 ==================================================== */
168  
169 /* The one disconnect path, shared by disconnect() and the host-key refusal
170 in connect(): both have to spend a connected session and bump the
171 generation before libssh frees the channels, and having the invariant in
172 one place is what keeps the two from drifting apart. */
173 static void
174 0 nlss_session_disconnect(NLSS_Session *self)
175 {
176 /* Only a session that actually got connected is spent by this: measured
177 on libssh 0.10.6, disconnect() on a never-connected session leaves it
178 connectable, and marking it would refuse a call that works. */
179 0 ▶ if (self->state == NLSS_SESSION_CONNECTED)
Branch: 1 branch
BranchTrueFalseTotal
if (self->state == NLSS_SESSION_CONNECTED) 0 0 0
180 0 self->state = NLSS_SESSION_SPENT;
181 /* Bump before disconnecting: ssh_disconnect() frees every channel the
182 session owns, so every channel and sftp session opened on it has to be
183 stale by the time that memory goes away. */
184 0 self->generation++;
185 0 ssh_disconnect(self->session);
186 0 }
187  
188 static void
189 1 nlss_croak_error(pTHX_ ssh_session session, const char *prefix)
190 {
191 1 const char *msg = ssh_get_error(session);
192 1 ▶ Perl_croak(aTHX_ "%s: %s", prefix, msg ? msg : "(unknown error)");
Branch: 1 branch
BranchTrueFalseTotal
Perl_croak(aTHX_ "%s: %s", prefix, msg ? msg : "(unknown error)"); 1 0 1
193 }
194  
195 static void
196 0 nlss_channel_check_open(pTHX_ NLSS_Channel *self, const char *prefix)
197 {
198 /* Two distinct states, two distinct messages: the caller closed this
199 channel itself, or the session was disconnected out from under it.
200 Collapsing them would leave a caller unable to tell its own teardown
201 from libssh's. */
202 0 ▶ if (!self->channel)
Branch: 1 branch
BranchTrueFalseTotal
if (!self->channel) 0 0 0
203 0 Perl_croak(aTHX_ "%s: channel is closed", prefix);
204 0 ▶ if (nlss_session_stale(aTHX_ self->session_sv, self->session_generation))
Branch: 1 branch
BranchTrueFalseTotal
if (nlss_session_stale(aTHX_ self->session_sv, self->session_generation)) 0 0 0
205 0 Perl_croak(aTHX_ "%s: session was disconnected", prefix);
206 0 }
207  
208 static void
209 0 nlss_sftp_check_open(pTHX_ NLSS_SFTP *self, const char *prefix)
210 {
211 /* There is no SFTP close(), so self->sftp is set for the object's whole
212 lifetime; disconnect() is the only thing that stops it being usable. */
213 0 ▶ if (nlss_session_stale(aTHX_ self->session_sv, self->session_generation))
Branch: 1 branch
BranchTrueFalseTotal
if (nlss_session_stale(aTHX_ self->session_sv, self->session_generation)) 0 0 0
214 0 Perl_croak(aTHX_ "%s: session was disconnected", prefix);
215 0 }
216  
217 static SV *
218 0 nlss_channel_slurp(pTHX_ ssh_channel ch, int is_stderr)
219 {
220 0 SV *buf = newSVpvs("");
221 char tmp[4096];
222 int n;
223 while (1) {
224 0 n = ssh_channel_read(ch, tmp, sizeof(tmp), is_stderr);
225 0 ▶ if (n <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) 0 0 0
226 0 break;
227 0 sv_catpvn(buf, tmp, n);
228 }
229 0 return buf;
230 }
231  
232 MODULE = Net::LibSSH PACKAGE = Net::LibSSH
233  
234 PROTOTYPES: DISABLE
235  
236 Net::LibSSH
237 new(class)
238 SV *class
239 CODE:
240 3 Newxz(RETVAL, 1, NLSS_Session);
241 3 RETVAL->session = ssh_new();
242 3 ▶ if (!RETVAL->session) {
Branch: 1 branch
BranchTrueFalseTotal
if (!RETVAL->session) { 0 3 3
243 0 Safefree(RETVAL);
244 0 Perl_croak(aTHX_ "Net::LibSSH::new: ssh_new() returned NULL");
245 }
246 OUTPUT:
247 RETVAL
248  
249 void
250 option(self, key, value)
251 Net::LibSSH self
252 const char *key
253 SV *value
254 CODE:
255 12 int rc = SSH_OK;
256 12 ▶ if (strcmp(key, "host") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(key, "host") == 0) { 2 10 12
257 2 rc = ssh_options_set(self->session, SSH_OPTIONS_HOST, SvPV_nolen(value));
258 10 ▶ } else if (strcmp(key, "user") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "user") == 0) { 1 9 10
259 1 rc = ssh_options_set(self->session, SSH_OPTIONS_USER, SvPV_nolen(value));
260 9 ▶ } else if (strcmp(key, "port") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "port") == 0) { 3 6 9
261 3 unsigned int port = (unsigned int) SvUV(value);
262 3 rc = ssh_options_set(self->session, SSH_OPTIONS_PORT, &port);
263 6 ▶ } else if (strcmp(key, "knownhosts") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "knownhosts") == 0) { 1 5 6
264 1 rc = ssh_options_set(self->session, SSH_OPTIONS_KNOWNHOSTS, SvPV_nolen(value));
265 5 ▶ } else if (strcmp(key, "timeout") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "timeout") == 0) { 2 3 5
266 2 long t = (long) SvIV(value);
267 2 rc = ssh_options_set(self->session, SSH_OPTIONS_TIMEOUT, &t);
268 3 ▶ } else if (strcmp(key, "compression") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "compression") == 0) { 1 2 3
269 1 rc = ssh_options_set(self->session, SSH_OPTIONS_COMPRESSION, SvPV_nolen(value));
270 2 ▶ } else if (strcmp(key, "log_verbosity") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "log_verbosity") == 0) { 1 1 2
271 1 int v = SvIV(value);
272 1 rc = ssh_options_set(self->session, SSH_OPTIONS_LOG_VERBOSITY, &v);
273 1 ▶ } else if (strcmp(key, "strict_hostkeycheck") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "strict_hostkeycheck") == 0) { 0 1 1
274 0 int v = SvTRUE(value) ? 1 : 0;
275 0 rc = ssh_options_set(self->session, SSH_OPTIONS_STRICTHOSTKEYCHECK, &v);
276 0 ▶ if (rc == SSH_OK)
Branch: 1 branch
BranchTrueFalseTotal
if (rc == SSH_OK) 0 0 0
277 0 self->hostkey_check_disabled = !v;
278 } else {
279 1 Perl_croak(aTHX_ "Net::LibSSH::option: unknown option '%s'", key);
280 }
281 11 ▶ if (rc != SSH_OK)
Branch: 1 branch
BranchTrueFalseTotal
if (rc != SSH_OK) 1 10 11
282 1 nlss_croak_error(aTHX_ self->session, "Net::LibSSH::option");
283  
284 int
285 connect(self)
286 Net::LibSSH self
287 CODE:
288 1 ▶ if (self->state == NLSS_SESSION_SPENT) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->state == NLSS_SESSION_SPENT) { 0 1 1
289 /* Refuse without calling libssh: ssh_connect() on a disconnected
290 session waits out the full timeout and then reports "Timeout
291 connecting to <host>", which names the wrong problem. Still 0 and
292 still no croak -- connect()'s contract is 1 or 0 with the message
293 on error(), and Rex::LibSSH relies on it. */
294 0 self->own_error = "session was disconnected and cannot be reconnected";
295 0 RETVAL = 0;
296 1 ▶ } else if (ssh_connect(self->session) == SSH_OK) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (ssh_connect(self->session) == SSH_OK) { 0 1 1
297 /* CONNECTED first: ssh_connect() has completed the key exchange, so
298 a refusal below is a disconnect of a connected session and spends
299 it exactly as disconnect() would. */
300 0 self->state = NLSS_SESSION_CONNECTED;
301 0 RETVAL = 1;
302 /* ssh_connect() negotiates the host key but never checks it against
303 known_hosts -- that is a separate call libssh leaves to the
304 application, and without it SSH_OPTIONS_STRICTHOSTKEYCHECK is
305 decoration. strict_hostkeycheck => 0 is documented as disabling
306 verification outright, so it skips the question entirely: no
307 refusal on CHANGED either, and nothing is written to known_hosts. */
308 0 ▶ if (!self->hostkey_check_disabled) {
Branch: 1 branch
BranchTrueFalseTotal
if (!self->hostkey_check_disabled) { 0 0 0
309 0 const char *refusal = NULL;
310 0 switch (ssh_session_is_known_server(self->session)) {
311 0 case SSH_KNOWN_HOSTS_OK:
312 0 break;
313 0 case SSH_KNOWN_HOSTS_CHANGED:
314 0 refusal = "host key has changed from the known_hosts entry"
315 " -- possible man-in-the-middle attack";
316 0 break;
317 0 case SSH_KNOWN_HOSTS_OTHER:
318 0 refusal = "host key type differs from the known_hosts entry"
319 " -- possible man-in-the-middle attack";
320 0 break;
321 0 case SSH_KNOWN_HOSTS_ERROR:
322 0 refusal = "could not verify host key against known_hosts";
323 0 break;
324 0 case SSH_KNOWN_HOSTS_NOT_FOUND:
325 case SSH_KNOWN_HOSTS_UNKNOWN:
326 default:
327 0 refusal = "host key is not in known_hosts"
328 " and strict_hostkeycheck is on";
329 0 break;
330 }
331 0 ▶ if (refusal) {
Branch: 1 branch
BranchTrueFalseTotal
if (refusal) { 0 0 0
332 /* Same 1/0 contract as every other connect() failure. The
333 session is spent from here on, so own_error being sticky
334 is right: the later "cannot be reconnected" overwrites it. */
335 0 nlss_session_disconnect(self);
336 0 self->own_error = refusal;
337 0 RETVAL = 0;
338 }
339 }
340 } else {
341 1 RETVAL = 0;
342 }
343 OUTPUT:
344 RETVAL
345  
346 void
347 disconnect(self)
348 Net::LibSSH self
349 CODE:
350 0 nlss_session_disconnect(self);
351  
352 SV *
353 error(self)
354 Net::LibSSH self
355 CODE:
356 4 const char *msg = self->own_error ? self->own_error
357 2 ▶ : ssh_get_error(self->session);
Branch: 1 branch
BranchTrueFalseTotal
: ssh_get_error(self->session); 0 2 2
358 2 ▶ RETVAL = (msg && *msg) ? newSVpv(msg, 0) : undef;
Branch: 2 branches
BranchTrueFalseTotal
RETVAL = (msg && *msg) ? newSVpv(msg, 0) : undef; 2 0 2
RETVAL = (msg && *msg) ? newSVpv(msg, 0) : undef; 1 1 2
359 OUTPUT:
360 RETVAL
361  
362 int
363 auth_password(self, password)
364 Net::LibSSH self
365 const char *password
366 CODE:
367 0 RETVAL = (ssh_userauth_password(self->session, NULL, password)
368 0 ▶ == SSH_AUTH_SUCCESS) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
== SSH_AUTH_SUCCESS) ? 1 : 0; 0 0 0
369 OUTPUT:
370 RETVAL
371  
372 int
373 auth_agent(self)
374 Net::LibSSH self
375 CODE:
376 0 int rc = ssh_userauth_agent(self->session, NULL);
377 0 ▶ if (rc != SSH_AUTH_SUCCESS)
Branch: 1 branch
BranchTrueFalseTotal
if (rc != SSH_AUTH_SUCCESS) 0 0 0
378 0 rc = ssh_userauth_publickey_auto(self->session, NULL, NULL);
379 0 ▶ RETVAL = (rc == SSH_AUTH_SUCCESS) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (rc == SSH_AUTH_SUCCESS) ? 1 : 0; 0 0 0
380 OUTPUT:
381 RETVAL
382  
383 int
384 auth_publickey(self, privkey_path)
385 Net::LibSSH self
386 const char *privkey_path
387 CODE:
388 0 ssh_key key = NULL;
389 0 int rc = ssh_pki_import_privkey_file(privkey_path, NULL, NULL, NULL, &key);
390 0 ▶ if (rc != SSH_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc != SSH_OK) { 0 0 0
391 0 RETVAL = 0;
392 } else {
393 0 rc = ssh_userauth_publickey(self->session, NULL, key);
394 0 ssh_key_free(key);
395 0 RETVAL = (rc == SSH_AUTH_SUCCESS) ? 1 : 0;
396 }
397 OUTPUT:
398 RETVAL
399  
400 Net::LibSSH::Channel
401 channel(self)
402 Net::LibSSH self
403 CODE:
404 0 ssh_channel ch = ssh_channel_new(self->session);
405 0 ▶ if (!ch)
Branch: 1 branch
BranchTrueFalseTotal
if (!ch) 0 0 0
406 0 XSRETURN_UNDEF;
407 0 ▶ if (ssh_channel_open_session(ch) != SSH_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (ssh_channel_open_session(ch) != SSH_OK) { 0 0 0
408 0 ssh_channel_free(ch);
409 0 XSRETURN_UNDEF;
410 }
411 0 Newxz(RETVAL, 1, NLSS_Channel);
412 0 RETVAL->channel = ch;
413 0 RETVAL->session_sv = SvREFCNT_inc(SvRV(ST(0)));
414 0 RETVAL->session_generation = self->generation;
415 OUTPUT:
416 RETVAL
417  
418 Net::LibSSH::SFTP
419 sftp(self)
420 Net::LibSSH self
421 CODE:
422 0 sftp_session sftp = sftp_new(self->session);
423 0 ▶ if (!sftp)
Branch: 1 branch
BranchTrueFalseTotal
if (!sftp) 0 0 0
424 0 XSRETURN_UNDEF;
425 0 ▶ if (sftp_init(sftp) != SSH_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (sftp_init(sftp) != SSH_OK) { 0 0 0
426 0 sftp_free(sftp);
427 0 XSRETURN_UNDEF;
428 }
429 0 Newxz(RETVAL, 1, NLSS_SFTP);
430 0 RETVAL->sftp = sftp;
431 0 RETVAL->session_sv = SvREFCNT_inc(SvRV(ST(0)));
432 0 RETVAL->session_generation = self->generation;
433 OUTPUT:
434 RETVAL
435  
436  
437 MODULE = Net::LibSSH PACKAGE = Net::LibSSH::Channel
438  
439 int
440 exec(self, cmd)
441 Net::LibSSH::Channel self
442 const char *cmd
443 CODE:
444 0 nlss_channel_check_open(aTHX_ self, "Net::LibSSH::Channel::exec");
445 0 ▶ RETVAL = (ssh_channel_request_exec(self->channel, cmd) == SSH_OK) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (ssh_channel_request_exec(self->channel, cmd) == SSH_OK) ? 1 : 0; 0 0 0
446 OUTPUT:
447 RETVAL
448  
449 SV *
450 read(self, ...)
451 Net::LibSSH::Channel self
452 CODE:
453 0 nlss_channel_check_open(aTHX_ self, "Net::LibSSH::Channel::read");
454 0 int is_stderr = 0;
455 0 int len = -1;
456 0 ▶ if (items >= 2) len = SvIV(ST(1));
Branch: 1 branch
BranchTrueFalseTotal
if (items >= 2) len = SvIV(ST(1)); 0 0 0
457 0 ▶ if (items >= 3) is_stderr = SvTRUE(ST(2));
Branch: 1 branch
BranchTrueFalseTotal
if (items >= 3) is_stderr = SvTRUE(ST(2)); 0 0 0
458 0 ▶ if (len < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (len < 0) { 0 0 0
459 0 RETVAL = nlss_channel_slurp(aTHX_ self->channel, is_stderr);
460 } else {
461 char *buf;
462 int n;
463 0 Newx(buf, len + 1, char);
464 0 n = ssh_channel_read(self->channel, buf, len, is_stderr);
465 0 ▶ if (n <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) { 0 0 0
466 0 Safefree(buf);
467 0 RETVAL = newSVpvs("");
468 } else {
469 0 RETVAL = newSVpvn(buf, n);
470 0 Safefree(buf);
471 }
472 }
473 OUTPUT:
474 RETVAL
475  
476 int
477 write(self, data)
478 Net::LibSSH::Channel self
479 SV *data
480 CODE:
481 0 nlss_channel_check_open(aTHX_ self, "Net::LibSSH::Channel::write");
482 STRLEN len;
483 0 const char *ptr = SvPV(data, len);
484 0 RETVAL = ssh_channel_write(self->channel, ptr, (uint32_t) len);
485 OUTPUT:
486 RETVAL
487  
488 void
489 send_eof(self)
490 Net::LibSSH::Channel self
491 CODE:
492 0 nlss_channel_check_open(aTHX_ self, "Net::LibSSH::Channel::send_eof");
493 0 ssh_channel_send_eof(self->channel);
494  
495 int
496 eof(self)
497 Net::LibSSH::Channel self
498 CODE:
499 0 nlss_channel_check_open(aTHX_ self, "Net::LibSSH::Channel::eof");
500 0 RETVAL = ssh_channel_is_eof(self->channel);
501 OUTPUT:
502 RETVAL
503  
504 int
505 exit_status(self)
506 Net::LibSSH::Channel self
507 CODE:
508 0 nlss_channel_check_open(aTHX_ self, "Net::LibSSH::Channel::exit_status");
509 0 RETVAL = ssh_channel_get_exit_status(self->channel);
510 OUTPUT:
511 RETVAL
512  
513 void
514 close(self)
515 Net::LibSSH::Channel self
516 CODE:
517 /* Free the C channel now; svt_free will later release
518 session_sv and Safefree the struct when the SV is GC'd.
519 Deliberately unguarded, so it stays idempotent for svt_free -- but a
520 channel invalidated by disconnect() is only dropped, never freed twice.
521 No croak either: `$ssh->disconnect; $ch->close` is teardown, and
522 teardown that has already happened is not an error. */
523 0 ▶ if (self->channel) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->channel) { 0 0 0
524 0 ▶ if (!nlss_session_stale(aTHX_ self->session_sv, self->session_generation)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!nlss_session_stale(aTHX_ self->session_sv, self->session_generation)) { 0 0 0
525 0 ssh_channel_send_eof(self->channel);
526 0 ssh_channel_close(self->channel);
527 0 ssh_channel_free(self->channel);
528 }
529 0 self->channel = NULL;
530 }
531  
532  
533 MODULE = Net::LibSSH PACKAGE = Net::LibSSH::SFTP
534  
535 SV *
536 stat(self, path)
537 Net::LibSSH::SFTP self
538 const char *path
539 CODE:
540 0 nlss_sftp_check_open(aTHX_ self, "Net::LibSSH::SFTP::stat");
541 0 sftp_attributes attr = sftp_stat(self->sftp, path);
542 0 ▶ if (!attr)
Branch: 1 branch
BranchTrueFalseTotal
if (!attr) 0 0 0
543 0 XSRETURN_UNDEF;
544 0 HV *h = newHV();
545 0 ▶ hv_stores(h, "name", newSVpv(attr->name ? attr->name : path, 0));
Branch: 1 branch
BranchTrueFalseTotal
hv_stores(h, "name", newSVpv(attr->name ? attr->name : path, 0)); 0 0 0
546 0 hv_stores(h, "size", newSVuv(attr->size));
547 0 hv_stores(h, "uid", newSVuv(attr->uid));
548 0 hv_stores(h, "gid", newSVuv(attr->gid));
549 0 hv_stores(h, "mode", newSVuv(attr->permissions));
550 0 ▶ hv_stores(h, "atime", newSVuv(attr->atime64 ? attr->atime64 : attr->atime));
Branch: 1 branch
BranchTrueFalseTotal
hv_stores(h, "atime", newSVuv(attr->atime64 ? attr->atime64 : attr->atime)); 0 0 0
551 0 ▶ hv_stores(h, "mtime", newSVuv(attr->mtime64 ? attr->mtime64 : attr->mtime));
Branch: 1 branch
BranchTrueFalseTotal
hv_stores(h, "mtime", newSVuv(attr->mtime64 ? attr->mtime64 : attr->mtime)); 0 0 0
552 0 sftp_attributes_free(attr);
553 0 RETVAL = newRV_noinc((SV *) h);
554 OUTPUT:
555 RETVAL
↑ Summary blib/lib/Net/LibSSH.pm »