1
0
mirror of https://git.libssh.org/projects/libssh.git synced 2025-08-08 19:02:06 +03:00

examples: Reformat sshnetcat.c

Signed-off-by: Jakub Jelen <jjelen@redhat.com>
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
This commit is contained in:
Jakub Jelen
2025-04-24 16:11:52 +02:00
committed by Andreas Schneider
parent 1229ad650b
commit f039edd85d

View File

@@ -50,11 +50,13 @@ char *pcap_file=NULL;
static void usage(void) static void usage(void)
{ {
fprintf(stderr,"Usage : sshnetcat [user@]host forwarded_host forwarded_port\n"); fprintf(stderr,
"Usage : sshnetcat [user@]host forwarded_host forwarded_port\n");
exit(1); exit(1);
} }
static int opts(int argc, char **argv){ static int opts(int argc, char **argv)
{
int i; int i;
while ((i = getopt(argc, argv, "P:")) != -1) { while ((i = getopt(argc, argv, "P:")) != -1) {
switch (i) { switch (i) {
@@ -79,7 +81,8 @@ static int opts(int argc, char **argv){
return 0; return 0;
} }
static void select_loop(ssh_session session,ssh_channel channel){ static void select_loop(ssh_session session, ssh_channel channel)
{
fd_set fds; fd_set fds;
struct timeval timeout; struct timeval timeout;
char buffer[BUF_SIZE]; char buffer[BUF_SIZE];
@@ -104,7 +107,8 @@ static void select_loop(ssh_session session,ssh_channel channel){
fd = ssh_get_fd(session); fd = ssh_get_fd(session);
if (fd == -1) { if (fd == -1) {
fprintf(stderr, "Error getting the session file descriptor: %s\n", fprintf(stderr,
"Error getting the session file descriptor: %s\n",
ssh_get_error(session)); ssh_get_error(session));
return; return;
} }
@@ -131,10 +135,12 @@ static void select_loop(ssh_session session,ssh_channel channel){
channels[0] = NULL; channels[0] = NULL;
} }
if (outchannels[0]) { if (outchannels[0]) {
while(channel && ssh_channel_is_open(channel) && ssh_channel_poll(channel,0)){ while (channel && ssh_channel_is_open(channel) &&
ssh_channel_poll(channel, 0)) {
lus = ssh_channel_read(channel, buffer, sizeof(buffer), 0); lus = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
if (lus == -1) { if (lus == -1) {
fprintf(stderr, "Error reading channel: %s\n", fprintf(stderr,
"Error reading channel: %s\n",
ssh_get_error(session)); ssh_get_error(session));
return; return;
} }
@@ -144,16 +150,19 @@ static void select_loop(ssh_session session,ssh_channel channel){
} else { } else {
ret = write(1, buffer, lus); ret = write(1, buffer, lus);
if (ret < 0) { if (ret < 0) {
fprintf(stderr, "Error writing to stdin: %s", fprintf(stderr,
"Error writing to stdin: %s",
strerror(errno)); strerror(errno));
return; return;
} }
} }
} }
while(channel && ssh_channel_is_open(channel) && ssh_channel_poll(channel,1)){ /* stderr */ while (channel && ssh_channel_is_open(channel) &&
ssh_channel_poll(channel, 1)) { /* stderr */
lus = ssh_channel_read(channel, buffer, sizeof(buffer), 1); lus = ssh_channel_read(channel, buffer, sizeof(buffer), 1);
if (lus == -1) { if (lus == -1) {
fprintf(stderr, "Error reading channel: %s\n", fprintf(stderr,
"Error reading channel: %s\n",
ssh_get_error(session)); ssh_get_error(session));
return; return;
} }
@@ -163,7 +172,8 @@ static void select_loop(ssh_session session,ssh_channel channel){
} else { } else {
ret = write(2, buffer, lus); ret = write(2, buffer, lus);
if (ret < 0) { if (ret < 0) {
fprintf(stderr, "Error writing to stderr: %s", fprintf(stderr,
"Error writing to stderr: %s",
strerror(errno)); strerror(errno));
return; return;
} }
@@ -175,11 +185,11 @@ static void select_loop(ssh_session session,ssh_channel channel){
channel = NULL; channel = NULL;
} }
} while (ret == EINTR || ret == SSH_EINTR); } while (ret == EINTR || ret == SSH_EINTR);
} }
} }
static void forwarding(ssh_session session){ static void forwarding(ssh_session session)
{
ssh_channel channel; ssh_channel channel;
int r; int r;
channel = ssh_channel_new(session); channel = ssh_channel_new(session);
@@ -191,7 +201,8 @@ static void forwarding(ssh_session session){
select_loop(session, channel); select_loop(session, channel);
} }
static int client(ssh_session session){ static int client(ssh_session session)
{
int auth = 0; int auth = 0;
char *banner; char *banner;
int state; int state;
@@ -224,7 +235,8 @@ static int client(ssh_session session){
#ifdef WITH_PCAP #ifdef WITH_PCAP
ssh_pcap_file pcap; ssh_pcap_file pcap;
void set_pcap(ssh_session session); void set_pcap(ssh_session session);
void set_pcap(ssh_session session){ void set_pcap(ssh_session session)
{
if (!pcap_file) if (!pcap_file)
return; return;
pcap = ssh_pcap_file_new(); pcap = ssh_pcap_file_new();
@@ -245,13 +257,15 @@ void cleanup_pcap(void)
} }
#endif #endif
int main(int argc, char **argv){ int main(int argc, char **argv)
{
ssh_session session; ssh_session session;
session = ssh_new(); session = ssh_new();
if (ssh_options_getopt(session, &argc, argv)) { if (ssh_options_getopt(session, &argc, argv)) {
fprintf(stderr, "error parsing command line :%s\n", fprintf(stderr,
"error parsing command line :%s\n",
ssh_get_error(session)); ssh_get_error(session));
usage(); usage();
} }