fix error checks on channel writes in samplesshd-cb example

Signed-off-by: Andrew Wiley <wiley@outlook.com>
Reviewed-by: Jakub Jelen <jjelen@redhat.com>
This commit is contained in:
Andrew Wiley
2021-05-01 12:14:54 -07:00
parent fb8d120bec
commit bd7bef8b50

View File

@@ -25,6 +25,10 @@ clients must be made or how a client should react.
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#ifdef _WIN32
#include <io.h>
#endif
#ifndef KEYS_FOLDER #ifndef KEYS_FOLDER
#ifdef _WIN32 #ifdef _WIN32
#define KEYS_FOLDER #define KEYS_FOLDER
@@ -241,7 +245,7 @@ int main(int argc, char **argv){
.channel_open_request_session_function = new_session_channel .channel_open_request_session_function = new_session_channel
}; };
char buf[2048]; char buf[2049];
int i; int i;
int r; int r;
@@ -297,19 +301,24 @@ int main(int argc, char **argv){
} else } else
printf("Authenticated and got a channel\n"); printf("Authenticated and got a channel\n");
do{ do{
i=ssh_channel_read(chan,buf, 2048, 0); i=ssh_channel_read(chan, buf, sizeof(buf) - 1, 0);
if(i>0) { if(i>0) {
ssh_channel_write(chan, buf, i); if (ssh_channel_write(chan, buf, i) == SSH_ERROR) {
if (write(1,buf,i) < 0) { printf("error writing to channel\n");
printf("error writing to buffer\n");
return 1; return 1;
} }
buf[i] = '\0';
printf("%s", buf);
fflush(stdout);
if (buf[0] == '\x0d') { if (buf[0] == '\x0d') {
if (write(1, "\n", 1) < 0) { if (ssh_channel_write(chan, "\n", 1) == SSH_ERROR) {
printf("error writing to buffer\n"); printf("error writing to channel\n");
return 1; return 1;
} }
ssh_channel_write(chan, "\n", 1);
printf("\n");
} }
} }
} while (i>0); } while (i>0);