static void ssh_loop_read(ssh_channel channel, int fd) { int nbytes; char buffer[SSH_READ_BLOCK_SIZE]; /* read from stdin until data are available */ do { nbytes = ssh_channel_read(channel, buffer, SSH_READ_BLOCK_SIZE, 0); if (ws_write(fd, buffer, nbytes) != nbytes) { errmsg_print("ERROR reading: %s", g_strerror(errno)); return; } } while(nbytes > 0); /* read loop finished... maybe something wrong happened. Read from stderr */ do { nbytes = ssh_channel_read(channel, buffer, SSH_READ_BLOCK_SIZE, 1); if (ws_write(STDERR_FILENO, buffer, nbytes) != nbytes) { return; } } while(nbytes > 0); if (ssh_channel_send_eof(channel) != SSH_OK) return; }
gboolean eo_save_entry(const gchar *save_as_filename, export_object_entry_t *entry, gboolean show_err) { int to_fd; gint64 bytes_left; int bytes_to_write; ssize_t bytes_written; guint8 *ptr; int err; to_fd = ws_open(save_as_filename, O_WRONLY | O_CREAT | O_EXCL | O_BINARY, 0644); if(to_fd == -1) { /* An error occurred */ if (show_err) open_failure_alert_box(save_as_filename, errno, TRUE); return FALSE; } /* * The third argument to _write() on Windows is an unsigned int, * so, on Windows, that's the size of the third argument to * ws_write(). * * The third argument to write() on UN*X is a size_t, although * the return value is an ssize_t, so one probably shouldn't * write more than the max value of an ssize_t. * * In either case, there's no guarantee that a gint64 such as * payload_len can be passed to ws_write(), so we write in * chunks of, at most 2^31 bytes. */ ptr = entry->payload_data; bytes_left = entry->payload_len; while (bytes_left != 0) { if (bytes_left > 0x40000000) bytes_to_write = 0x40000000; else bytes_to_write = (int)bytes_left; bytes_written = ws_write(to_fd, ptr, bytes_to_write); if(bytes_written <= 0) { if (bytes_written < 0) err = errno; else err = WTAP_ERR_SHORT_WRITE; if (show_err) write_failure_alert_box(save_as_filename, err); ws_close(to_fd); return FALSE; } bytes_left -= bytes_written; ptr += bytes_written; } if (ws_close(to_fd) < 0) { if (show_err) write_failure_alert_box(save_as_filename, errno); return FALSE; } return TRUE; }
/* save the SSL Session Keys */ static gboolean savesslkeys_save_clicked_cb(char *file, gchar *keylist) { int fd; fd = ws_open(file, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0666); if (fd == -1) { open_failure_alert_box(file, errno, TRUE); return FALSE; } /* * Thanks, Microsoft, for not using size_t for the third argument to * _write(). Presumably this string will be <= 4GiB long.... */ if (ws_write(fd, keylist, (unsigned int)strlen(keylist)) < 0) { write_failure_alert_box(file, errno); ws_close(fd); return FALSE; } if (ws_close(fd) < 0) { write_failure_alert_box(file, errno); return FALSE; } g_free(keylist); return TRUE; }
static int connect_cb(ws_connect_t *ptr, void *obj, int status) { printf("connect back\n"); ws_read_start(ptr, cb_malloc, cb_read, NULL); uv_buf_t bufs2; bufs2.base = "hello websocket"; bufs2.len = strlen(bufs2.base); ws_write(ptr, OPCODE_TEXT, &bufs2, 1, cb_write, NULL); return 0; }
int main(int argc, char ** argv) { char buf[8000]; websocket_t ws; int fd, plen, len; fd = ws_connect(); ws_init(&ws, fd, 1000, 1); for (int eof = 0; !eof;) { for (len = 0; !eof;) { if ((plen = read(0, buf + len, sizeof(buf) - len)) == -1) exit(1); // check for eof eof = plen == 0; len += plen; // check for "\n.\n" (single period on line) if (len > 2 && buf[len - 1] == '\n' && buf[len - 3] == '\n' && buf[len - 2] == '.') { // remove it len -= 2; break; } } if ((ws_write(&ws, buf, len)) == -1) exit(1); if ((plen = ws_read(&ws, buf, sizeof(buf))) == -1) exit(1); if (!plen) break; write(1, buf, plen); } ws_free(&ws); close(fd); return 0; }
/* download a complete file from the internet */ int download_file(const char *url, const char *filename) { netio_ie5_t * conn; char buf[100]; int chunk_len; int fd; int stream_len; int ret = 0; /* open output file */ fd = ws_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644); if(fd == -1) { g_warning("Couldn't open output file %s!", filename); return -1; } /* connect to url */ conn = netio_ie5_connect (url); if (conn == NULL) { g_warning("Couldn't connect to %s!", url); return -1; } do { /* XXX - maybe add a progress bar here */ /* read some bytes from the url */ chunk_len = netio_ie5_read (conn, buf, sizeof(buf)); /* write bytes to the output file */ stream_len = ws_write( fd, buf, chunk_len); if(stream_len != chunk_len) { g_warning("output failed: stream_len %u != chunk_len %u", stream_len, chunk_len); ret = -1; break; } } while(chunk_len > 0); netio_ie5_disconnect(conn); ws_close(fd); return ret; }
static int con_cb(ws_connect_t *ptr, void *obj, int status) { printf("accept back\n"); ws_read_start(ptr, cb_malloc, cb_read, NULL); uv_buf_t bufs2; bufs2.base = "server send"; bufs2.len = strlen(bufs2.base); ws_write(ptr, OPCODE_TEXT, &bufs2, 1, cb_write, NULL); uv_timer_t *handle = new uv_timer_t; handle->data = ptr; uv_timer_init(uv_default_loop(), handle); uv_timer_start(handle, ping_cb, 100, 100); return 0; }