static void ssh_loop_read(ssh_channel channel, FILE* fp, const guint32 count) { char line[SSH_READ_BLOCK_SIZE]; char chr; unsigned offset = 0; unsigned packet_size = 0; guint8* packet; time_t curtime = time(NULL); int err; guint64 bytes_written; long unsigned packets = 0; int status = CISCODUMP_PARSER_STARTING; /* This is big enough to put on the heap */ packet = (guint8*)g_malloc(PACKET_MAX_SIZE); do { if (ssh_channel_read_timeout(channel, &chr, 1, FALSE, SSH_READ_TIMEOUT) == SSH_ERROR) { g_warning("Error reading from channel"); g_free(packet); return; } if (chr != '\n') { line[offset] = chr; offset++; } else { /* Parse the current line */ line[offset] = '\0'; status = parse_line(packet, &packet_size, line, status); if (status == CISCODUMP_PARSER_END_PACKET) { /* dump the packet to the pcap file */ if (!libpcap_write_packet(fp, curtime, (guint32)(curtime / 1000), packet_size, packet_size, packet, &bytes_written, &err)) { g_debug("Error in libpcap_write_packet(): %s", g_strerror(err)); break; } g_debug("Dumped packet %lu size: %u", packets, packet_size); packet_size = 0; status = CISCODUMP_PARSER_STARTING; packets++; } offset = 0; } } while(packets < count); g_free(packet); }
/* Read bytes from the channel. If bytes == -1, read all data (until timeout). If outbuf != NULL, data are stored there */ static int read_output_bytes(ssh_channel channel, int bytes, char* outbuf) { char chr; int total; int bytes_read; total = (bytes > 0 ? bytes : G_MAXINT); bytes_read = 0; while(ssh_channel_read_timeout(channel, &chr, 1, 0, 2000) > 0 && bytes_read < total) { g_debug("%c", chr); if (chr == '^') return EXIT_FAILURE; if (outbuf) outbuf[bytes_read] = chr; bytes_read++; } return EXIT_SUCCESS; }
static void *nogvl_read(void *ptr) { struct nogvl_read_args *args = ptr; args->rc = ssh_channel_read_timeout(args->channel, args->buf, args->count, args->is_stderr, args->timeout); return NULL; }