diff --git a/src/command.c b/src/command.c index 93b992a..a9503c7 100644 --- a/src/command.c +++ b/src/command.c @@ -102,18 +102,12 @@ static const char *cmd_lookup(const char *cmd) static const char *cmderr2str(int err) { switch (err) { - case RTTY_CMD_ERR_PERMIT: - return "operation not permitted"; - case RTTY_CMD_ERR_NOT_FOUND: - return "not found"; - case RTTY_CMD_ERR_NOMEM: - return "no mem"; - case RTTY_CMD_ERR_SYSERR: - return "sys error"; - case RTTY_CMD_ERR_RESP_TOOBIG: - return "stdout+stderr is too big"; - default: - return ""; + case RTTY_CMD_ERR_PERMIT: return "operation not permitted"; + case RTTY_CMD_ERR_NOT_FOUND: return "not found"; + case RTTY_CMD_ERR_NOMEM: return "no mem"; + case RTTY_CMD_ERR_SYSERR: return "sys error"; + case RTTY_CMD_ERR_RESP_TOOBIG: return "stdout+stderr is too big"; + default: return ""; } } @@ -151,7 +145,7 @@ static void cmd_err_reply(struct rtty *rtty, const char *token, int err) char str[256] = ""; snprintf(str, sizeof(str) - 1, "{\"token\":\"%s\"," - "\"attrs\":{\"err\":%d,\"msg\":\"%s\"}}", token, err, cmderr2str(err)); + "\"attrs\":{\"err\":%d,\"msg\":\"%s\"}}", token, err, cmderr2str(err)); rtty_send_msg(rtty, MSG_TYPE_CMD, str, strlen(str)); } @@ -174,7 +168,7 @@ static void cmd_reply(struct task *t, int code) pos = str; ret = snprintf(pos, len, "{\"token\":\"%s\"," - "\"attrs\":{\"code\":%d,\"stdout\":\"", t->token, code); + "\"attrs\":{\"code\":%d,\"stdout\":\"", t->token, code); len -= ret; pos += ret; @@ -259,13 +253,11 @@ static void run_task(struct task *t) } pid = fork(); - switch (pid) { - case -1: + if (pid < 0) { log_err("fork: %s\n", strerror(errno)); err = RTTY_CMD_ERR_SYSERR; goto ERR; - - case 0: { + } else if (pid == 0) { int arglen = 2 + t->nparams; char **args; int i; @@ -290,8 +282,7 @@ static void run_task(struct task *t) args[i + 1] = t->params[i]; execv(t->cmd, args); - } - default: + } else { /* Close unused write end */ close(opipe[1]); close(epipe[1]); diff --git a/src/command.h b/src/command.h index d0cb012..50cba94 100644 --- a/src/command.h +++ b/src/command.h @@ -31,11 +31,11 @@ #define RTTY_CMD_EXEC_TIMEOUT 30 enum { - RTTY_CMD_ERR_PERMIT = 1, - RTTY_CMD_ERR_NOT_FOUND, - RTTY_CMD_ERR_NOMEM, - RTTY_CMD_ERR_SYSERR, - RTTY_CMD_ERR_RESP_TOOBIG + RTTY_CMD_ERR_PERMIT = 1, + RTTY_CMD_ERR_NOT_FOUND, + RTTY_CMD_ERR_NOMEM, + RTTY_CMD_ERR_SYSERR, + RTTY_CMD_ERR_RESP_TOOBIG }; struct task { diff --git a/src/config.h.in b/src/config.h.in index aa70606..46cafb9 100644 --- a/src/config.h.in +++ b/src/config.h.in @@ -25,15 +25,15 @@ #ifndef _RTTY_CONFIG_H #define _RTTY_CONFIG_H -#define RTTY_VERSION_MAJOR @RTTY_VERSION_MAJOR@ -#define RTTY_VERSION_MINOR @RTTY_VERSION_MINOR@ -#define RTTY_VERSION_PATCH @RTTY_VERSION_PATCH@ +#define RTTY_VERSION_MAJOR @RTTY_VERSION_MAJOR@ +#define RTTY_VERSION_MINOR @RTTY_VERSION_MINOR@ +#define RTTY_VERSION_PATCH @RTTY_VERSION_PATCH@ #define RTTY_VERSION_STRING "@RTTY_VERSION_MAJOR@.@RTTY_VERSION_MINOR@.@RTTY_VERSION_PATCH@" -#define RTTY_SSL_SUPPORT @RTTY_SSL_SUPPORT_CONFIG@ +#define RTTY_SSL_SUPPORT @RTTY_SSL_SUPPORT_CONFIG@ -#define RTTY_HAVE_OPENSSL @RTTY_HAVE_OPENSSL_CONFIG@ -#define RTTY_HAVE_WOLFSSL @RTTY_HAVE_WOLFSSL_CONFIG@ -#define RTTY_HAVE_MBEDTLS @RTTY_HAVE_MBEDTLS_CONFIG@ +#define RTTY_HAVE_OPENSSL @RTTY_HAVE_OPENSSL_CONFIG@ +#define RTTY_HAVE_WOLFSSL @RTTY_HAVE_WOLFSSL_CONFIG@ +#define RTTY_HAVE_MBEDTLS @RTTY_HAVE_MBEDTLS_CONFIG@ #endif diff --git a/src/downfile.c b/src/downfile.c index 0d90bde..b4b75c2 100644 --- a/src/downfile.c +++ b/src/downfile.c @@ -44,15 +44,15 @@ static void on_socket_read(struct ev_loop *loop, struct ev_io *w, int revents) switch (type) { case RTTY_FILE_MSG_REQUEST_ACCEPT: - buffer_put_u8 (&b, RTTY_FILE_MSG_SAVE_PATH); - buffer_put_string (&b, abspath); - buffer_put_zero (&b, 1); - buffer_pull_to_fd (&b, w->fd, -1); + buffer_put_u8(&b, RTTY_FILE_MSG_SAVE_PATH); + buffer_put_string(&b, abspath); + buffer_put_zero(&b, 1); + buffer_pull_to_fd(&b, w->fd, -1); break; case RTTY_FILE_MSG_INFO: - printf ("Transferring '%s'...\n", (char *)buffer_data (&b)); + printf("Transferring '%s'...\n", (char *)buffer_data(&b)); buffer_pull(&b, NULL, buffer_length(&b)); - start_time = ev_now (loop); + start_time = ev_now(loop); break; case RTTY_FILE_MSG_PROGRESS: update_progress(loop, start_time, &b); @@ -86,7 +86,7 @@ void download_file() detect_sid('d'); ev_io_init(&ior, on_socket_read, sock, EV_READ); - ev_io_start (loop, &ior); + ev_io_start(loop, &ior); ev_signal_init(&sw, signal_cb, SIGINT); ev_signal_start(loop, &sw); diff --git a/src/file.c b/src/file.c index 2c2da71..29c82b6 100644 --- a/src/file.c +++ b/src/file.c @@ -42,29 +42,29 @@ static struct buffer b; static void notify_progress(struct file_context *ctx) { - struct rtty *rtty = container_of (ctx, struct rtty, file_context); + struct rtty *rtty = container_of(ctx, struct rtty, file_context); ev_tstamp now = ev_now(rtty->loop); if (ctx->remain_size > 0 && now - ctx->last_notify_progress < 0.1) return; ctx->last_notify_progress = now; - buffer_truncate (&b, 0); + buffer_truncate(&b, 0); buffer_put_u8(&b, RTTY_FILE_MSG_PROGRESS); buffer_put_u32(&b, ctx->remain_size); buffer_put_u32(&b, ctx->total_size); sendto(ctx->sock, buffer_data(&b), buffer_length(&b), 0, - (struct sockaddr *)&ctx->peer_sun, sizeof(struct sockaddr_un)); + (struct sockaddr *)&ctx->peer_sun, sizeof(struct sockaddr_un)); } static void on_file_read(struct ev_loop *loop, struct ev_io *w, int revents) { - struct file_context *ctx = container_of (w, struct file_context, iof); - struct rtty *rtty = container_of (ctx, struct rtty, file_context); + struct file_context *ctx = container_of(w, struct file_context, iof); + struct rtty *rtty = container_of(ctx, struct rtty, file_context); uint8_t buf[4096]; int ret; - if (buffer_length (&rtty->wb) > 4096) + if (buffer_length(&rtty->wb) > 4096) return; ret = read(w->fd, buf, sizeof(buf)); @@ -88,7 +88,7 @@ static void on_file_read(struct ev_loop *loop, struct ev_io *w, int revents) static void start_upload_file(struct file_context *ctx, struct buffer *info) { - struct rtty *rtty = container_of (ctx, struct rtty, file_context); + struct rtty *rtty = container_of(ctx, struct rtty, file_context); uint32_t size = buffer_pull_u32(info); const char *path = buffer_data(info); const char *name = basename(path); @@ -96,13 +96,13 @@ static void start_upload_file(struct file_context *ctx, struct buffer *info) fd = open(path, O_RDONLY | O_NONBLOCK); if (fd < 0) { - log_err ("open '%s' fail\n", path, strerror (errno)); + log_err("open '%s' fail\n", path, strerror(errno)); return; } ctx->busy = true; - buffer_put_u8 (&rtty->wb, MSG_TYPE_FILE); + buffer_put_u8(&rtty->wb, MSG_TYPE_FILE); buffer_put_u16be(&rtty->wb, 2 + strlen(name)); buffer_put_u8(&rtty->wb, ctx->sid); buffer_put_u8(&rtty->wb, RTTY_FILE_MSG_INFO); @@ -110,7 +110,7 @@ static void start_upload_file(struct file_context *ctx, struct buffer *info) ev_io_start(rtty->loop, &rtty->iow); ev_io_init(&ctx->iof, on_file_read, fd, EV_READ); - ev_io_start (rtty->loop, &ctx->iof); + ev_io_start(rtty->loop, &ctx->iof); ctx->total_size = size; ctx->remain_size = size; @@ -123,29 +123,29 @@ static void start_download_file(struct file_context *ctx, struct buffer *info, i char *name; int fd; - ctx->total_size = ctx->remain_size = buffer_pull_u32be (info); - name = strndup (buffer_data (info), len - 4); - buffer_pull (info, NULL, len - 4); + ctx->total_size = ctx->remain_size = buffer_pull_u32be(info); + name = strndup(buffer_data(info), len - 4); + buffer_pull(info, NULL, len - 4); strcat(abspath, name); - fd = open (abspath, O_WRONLY | O_TRUNC | O_CREAT, 0644); + fd = open(abspath, O_WRONLY | O_TRUNC | O_CREAT, 0644); if (fd < 0) { - log_err ("create file '%s' fail: %s\n", name, strerror (errno)); + log_err("create file '%s' fail: %s\n", name, strerror(errno)); return; } ctx->fd = fd; ctx->busy = true; - log_info ("download file: %s, size: %u\n", abspath, ctx->total_size); + log_info("download file: %s, size: %u\n", abspath, ctx->total_size); - buffer_truncate (&b, 0); - buffer_put_u8 (&b, RTTY_FILE_MSG_INFO); + buffer_truncate(&b, 0); + buffer_put_u8(&b, RTTY_FILE_MSG_INFO); buffer_put_string(&b, name); - buffer_put_zero (&b, 1); + buffer_put_zero(&b, 1); sendto(ctx->sock, buffer_data(&b), buffer_length(&b), 0, - (struct sockaddr *)&ctx->peer_sun, sizeof(struct sockaddr_un)); + (struct sockaddr *)&ctx->peer_sun, sizeof(struct sockaddr_un)); free(name); } @@ -154,14 +154,14 @@ static void notify_busy(struct file_context *ctx) int type = RTTY_FILE_MSG_BUSY; log_err("upload file is busy\n"); - sendto (ctx->sock, &type, 1, 0, - (struct sockaddr *)&ctx->peer_sun, sizeof(struct sockaddr_un)); + sendto(ctx->sock, &type, 1, 0, + (struct sockaddr *)&ctx->peer_sun, sizeof(struct sockaddr_un)); } static void on_socket_read(struct ev_loop *loop, struct ev_io *w, int revents) { - struct file_context *ctx = container_of (w, struct file_context, ios); - struct rtty *rtty = container_of (ctx, struct rtty, file_context); + struct file_context *ctx = container_of(w, struct file_context, ios); + struct rtty *rtty = container_of(ctx, struct rtty, file_context); int type = read_file_msg(w->fd, &b); switch (type) { @@ -169,18 +169,18 @@ static void on_socket_read(struct ev_loop *loop, struct ev_io *w, int revents) start_upload_file(ctx, &b); break; case RTTY_FILE_MSG_CANCELED: - ev_io_stop (loop, &ctx->iof); + ev_io_stop(loop, &ctx->iof); close(ctx->iof.fd); ctx->busy = false; - buffer_put_u8 (&rtty->wb, MSG_TYPE_FILE); + buffer_put_u8(&rtty->wb, MSG_TYPE_FILE); buffer_put_u16be(&rtty->wb, 2); buffer_put_u8(&rtty->wb, ctx->sid); buffer_put_u8(&rtty->wb, RTTY_FILE_MSG_CANCELED); ev_io_start(rtty->loop, &rtty->iow); break; case RTTY_FILE_MSG_SAVE_PATH: - strcpy(abspath, buffer_data (&b)); + strcpy(abspath, buffer_data(&b)); strcat(abspath, "/"); break; default: @@ -190,7 +190,7 @@ static void on_socket_read(struct ev_loop *loop, struct ev_io *w, int revents) int start_file_service(struct file_context *ctx) { - struct rtty *rtty = container_of (ctx, struct rtty, file_context); + struct rtty *rtty = container_of(ctx, struct rtty, file_context); struct sockaddr_un sun = { .sun_family = AF_UNIX }; @@ -217,7 +217,7 @@ int start_file_service(struct file_context *ctx) } ev_io_init(&ctx->ios, on_socket_read, sock, EV_READ); - ev_io_start (rtty->loop, &ctx->ios); + ev_io_start(rtty->loop, &ctx->ios); ctx->sock = sock; ctx->peer_sun.sun_family = AF_UNIX; @@ -227,7 +227,7 @@ int start_file_service(struct file_context *ctx) err: if (sock > -1) - close (sock); + close(sock); return -1; } @@ -237,8 +237,8 @@ static void accept_file_request(struct file_context *ctx) ctx->fd = -1; ctx->last_notify_progress = 0; - sendto (ctx->sock, &type, 1, 0, - (struct sockaddr *)&ctx->peer_sun, sizeof(struct sockaddr_un)); + sendto(ctx->sock, &type, 1, 0, + (struct sockaddr *)&ctx->peer_sun, sizeof(struct sockaddr_un)); } bool detect_file_operation(uint8_t *buf, int len, int sid, int *type, struct file_context *ctx) @@ -246,7 +246,7 @@ bool detect_file_operation(uint8_t *buf, int len, int sid, int *type, struct fil if (len != 4) return false; - if (memcmp (buf, RTTY_FILE_MAGIC, sizeof (RTTY_FILE_MAGIC))) + if (memcmp(buf, RTTY_FILE_MAGIC, sizeof(RTTY_FILE_MAGIC))) return false; if (buf[3] != 'u' && buf[3] != 'd') @@ -292,7 +292,7 @@ void parse_file_msg(struct file_context *ctx, int type, struct buffer *data, int close(ctx->fd); ctx->fd = -1; ctx->busy = false; - sendto (ctx->sock, &type, 1, 0, (struct sockaddr *)&ctx->peer_sun, sizeof(struct sockaddr_un)); + sendto(ctx->sock, &type, 1, 0, (struct sockaddr *) &ctx->peer_sun, sizeof(struct sockaddr_un)); break; default: break; @@ -301,9 +301,7 @@ void parse_file_msg(struct file_context *ctx, int type, struct buffer *data, int int connect_rtty_file_service() { - struct sockaddr_un sun = { - .sun_family = AF_UNIX - }; + struct sockaddr_un sun = { .sun_family = AF_UNIX }; int sock = -1; sock = socket(AF_UNIX, SOCK_DGRAM | SOCK_NONBLOCK, 0); @@ -323,8 +321,8 @@ int connect_rtty_file_service() strcpy(sun.sun_path, RTTY_FILE_UNIX_SOCKET_S); - if (connect(sock, (struct sockaddr *)&sun, sizeof(sun)) < 0) { - fprintf(stderr,"connect to rtty fail: %s\n", strerror(errno)); + if (connect(sock, (struct sockaddr *) &sun, sizeof(sun)) < 0) { + fprintf(stderr, "connect to rtty fail: %s\n", strerror(errno)); goto err; } @@ -345,7 +343,7 @@ void detect_sid(char type) fwrite(buf, 4, 1, stdout); fflush(stdout); - usleep (10000); + usleep(10000); } void update_progress(struct ev_loop *loop, ev_tstamp start_time, struct buffer *info) @@ -355,11 +353,11 @@ void update_progress(struct ev_loop *loop, ev_tstamp start_time, struct buffer * printf("%100c\r", ' '); printf(" %lu%% %s %.3lfs\r", (total - remain) * 100UL / total, - format_size(total - remain), ev_now(loop) - start_time); - fflush (stdout); + format_size(total - remain), ev_now(loop) - start_time); + fflush(stdout); if (remain == 0) { - ev_break (loop, EVBREAK_ALL); + ev_break(loop, EVBREAK_ALL); puts(""); } } @@ -376,7 +374,7 @@ int read_file_msg(int sock, struct buffer *out) { uint8_t buf[1024]; int ret = read(sock, buf, sizeof(buf)); - buffer_truncate (out, 0); + buffer_truncate(out, 0); buffer_put_data(out, buf, ret); return buffer_pull_u8(out); } diff --git a/src/file.h b/src/file.h index 3b9a1ce..6c10aea 100644 --- a/src/file.h +++ b/src/file.h @@ -55,12 +55,19 @@ struct file_context { }; int start_file_service(struct file_context *ctx); + bool detect_file_operation(uint8_t *buf, int len, int sid, int *type, struct file_context *ctx); + void parse_file_msg(struct file_context *ctx, int type, struct buffer *data, int len); + void update_progress(struct ev_loop *loop, ev_tstamp start_time, struct buffer *info); + void cancel_file_operation(struct ev_loop *loop, int sock); + int read_file_msg(int sock, struct buffer *out); + int connect_rtty_file_service(); + void detect_sid(char type); #endif diff --git a/src/log.h b/src/log.h index f4ca1df..ed12390 100644 --- a/src/log.h +++ b/src/log.h @@ -21,7 +21,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ - + #ifndef _LOG_H #define _LOG_H @@ -29,6 +29,7 @@ #include void set_log_threshold(int threshold); + void log_close(); #define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__) @@ -39,6 +40,6 @@ void log_close(); #define log_info(fmt...) log(LOG_INFO, fmt) #define log_err(fmt...) log(LOG_ERR, fmt) -void __ilog(const char *filename, int line, int priority, const char *fmt, ...); +void __ilog(const char *filename, int line, int priority, const char *fmt, ...); #endif diff --git a/src/main.c b/src/main.c index 2437cc0..614d1dc 100644 --- a/src/main.c +++ b/src/main.c @@ -46,14 +46,14 @@ static void signal_cb(struct ev_loop *loop, ev_signal *w, int revents) } static struct option long_options[] = { - {"id", required_argument, NULL, 'I'}, - {"host", required_argument, NULL, 'h'}, - {"port", required_argument, NULL, 'p'}, + {"id", required_argument, NULL, 'I'}, + {"host", required_argument, NULL, 'h'}, + {"port", required_argument, NULL, 'p'}, {"description", required_argument, NULL, 'd'}, - {"token", required_argument, NULL, 't'}, - {"verbose", no_argument, NULL, 'v'}, - {"version", no_argument, NULL, 'V'}, - {"help", no_argument, NULL, LONG_OPT_HELP}, + {"token", required_argument, NULL, 't'}, + {"verbose", no_argument, NULL, 'v'}, + {"version", no_argument, NULL, 'V'}, + {"help", no_argument, NULL, LONG_OPT_HELP}, {0, 0, 0, 0} }; diff --git a/src/net.c b/src/net.c index 1e9bac0..139d1b2 100644 --- a/src/net.c +++ b/src/net.c @@ -37,6 +37,7 @@ struct net_context { struct ev_io iow; int sock; void *arg; + void (*on_connected)(int sock, void *arg); }; @@ -94,7 +95,7 @@ static void timer_cb(struct ev_loop *loop, struct ev_timer *w, int revents) } static void wait_connect(struct ev_loop *loop, int sock, int timeout, - void (*on_connected)(int sock, void *arg), void *arg) + void (*on_connected)(int sock, void *arg), void *arg) { static struct net_context ctx; @@ -107,11 +108,11 @@ static void wait_connect(struct ev_loop *loop, int sock, int timeout, ev_io_init(&ctx.iow, sock_write_cb, sock, EV_WRITE); ctx.iow.data = &ctx; - ev_io_start (loop, &ctx.iow); + ev_io_start(loop, &ctx.iow); } int tcp_connect(struct ev_loop *loop, const char *host, int port, - void (*on_connected)(int sock, void *arg), void *arg) + void (*on_connected)(int sock, void *arg), void *arg) { struct sockaddr *addr = NULL; struct addrinfo *result, *rp; diff --git a/src/net.h b/src/net.h index 815a54a..3c41a79 100644 --- a/src/net.h +++ b/src/net.h @@ -28,6 +28,6 @@ #include int tcp_connect(struct ev_loop *loop, const char *host, int port, - void (*on_connected)(int sock, void *arg), void *arg); + void (*on_connected)(int sock, void *arg), void *arg); #endif diff --git a/src/rtty.c b/src/rtty.c index 1c79c53..9502680 100644 --- a/src/rtty.c +++ b/src/rtty.c @@ -38,7 +38,8 @@ static char login_path[128]; /* /bin/login */ -static void del_tty(struct tty *tty) { +static void del_tty(struct tty *tty) +{ struct rtty *rtty = tty->rtty; struct ev_loop *loop = rtty->loop; @@ -58,19 +59,22 @@ static void del_tty(struct tty *tty) { free(tty); } -static inline struct tty *find_tty(struct rtty *rtty, int sid) { +static inline struct tty *find_tty(struct rtty *rtty, int sid) +{ if (sid > RTTY_MAX_TTY - 1) return NULL; return rtty->ttys[sid]; } -static inline void tty_logout(struct rtty *rtty, int sid) { - struct tty *tty = find_tty (rtty, sid); +static inline void tty_logout(struct rtty *rtty, int sid) +{ + struct tty *tty = find_tty(rtty, sid); if (tty) - del_tty (tty); + del_tty(tty); } -static void pty_on_read(struct ev_loop *loop, struct ev_io *w, int revents) { +static void pty_on_read(struct ev_loop *loop, struct ev_io *w, int revents) +{ struct tty *tty = container_of(w, struct tty, ior); struct rtty *rtty = tty->rtty; struct buffer *wb = &rtty->wb; @@ -95,21 +99,22 @@ static void pty_on_read(struct ev_loop *loop, struct ev_io *w, int revents) { return; } - if (!detect_file_operation (buf, len, tty->sid, &type, &rtty->file_context)) { - buffer_put_u8 (wb, MSG_TYPE_TERMDATA); - buffer_put_u16be (wb, len + 1); - buffer_put_u8 (wb, tty->sid); - buffer_put_data (wb, buf, len); + if (!detect_file_operation(buf, len, tty->sid, &type, &rtty->file_context)) { + buffer_put_u8(wb, MSG_TYPE_TERMDATA); + buffer_put_u16be(wb, len + 1); + buffer_put_u8(wb, tty->sid); + buffer_put_data(wb, buf, len); } else if (type > -1) { - buffer_put_u8 (wb, MSG_TYPE_FILE); - buffer_put_u16be (wb, 2); - buffer_put_u8 (wb, tty->sid); - buffer_put_u8 (wb, type); + buffer_put_u8(wb, MSG_TYPE_FILE); + buffer_put_u16be(wb, 2); + buffer_put_u8(wb, tty->sid); + buffer_put_u8(wb, type); } ev_io_start(loop, &rtty->iow); } -static void pty_on_write(struct ev_loop *loop, struct ev_io *w, int revents) { +static void pty_on_write(struct ev_loop *loop, struct ev_io *w, int revents) +{ struct tty *tty = container_of(w, struct tty, iow); struct buffer *wb = &tty->wb; int ret; @@ -124,16 +129,17 @@ static void pty_on_write(struct ev_loop *loop, struct ev_io *w, int revents) { ev_io_stop(loop, w); } -static void pty_on_exit(struct ev_loop *loop, struct ev_child *w, int revents) { +static void pty_on_exit(struct ev_loop *loop, struct ev_child *w, int revents) +{ struct tty *tty = container_of(w, struct tty, cw); struct rtty *rtty = tty->rtty; struct buffer *wb = &rtty->wb; - del_tty (tty); + del_tty(tty); - buffer_put_u8 (wb, MSG_TYPE_LOGOUT); - buffer_put_u16be (wb, 1); - buffer_put_u8 (wb, tty->sid); + buffer_put_u8(wb, MSG_TYPE_LOGOUT); + buffer_put_u16be(wb, 1); + buffer_put_u8(wb, tty->sid); ev_io_start(loop, &rtty->iow); } @@ -144,7 +150,7 @@ static void tty_login(struct rtty *rtty) int pty; int sid; - buffer_put_u8 (&rtty->wb, MSG_TYPE_LOGIN); + buffer_put_u8(&rtty->wb, MSG_TYPE_LOGIN); for (sid = 0; sid < RTTY_MAX_TTY; sid++) { if (!rtty->ttys[sid]) @@ -152,10 +158,10 @@ static void tty_login(struct rtty *rtty) } if (sid == RTTY_MAX_TTY) { - log_info ("tty login fail, device busy\n"); - buffer_put_u16be (&rtty->wb, 1); - buffer_put_u8 (&rtty->wb, 1); - ev_io_start (rtty->loop, &rtty->iow); + log_info("tty login fail, device busy\n"); + buffer_put_u16be(&rtty->wb, 1); + buffer_put_u8(&rtty->wb, 1); + ev_io_start(rtty->loop, &rtty->iow); return; } @@ -182,30 +188,30 @@ static void tty_login(struct rtty *rtty) rtty->ttys[sid] = tty; - buffer_put_u16be (&rtty->wb, 2); - buffer_put_u8 (&rtty->wb, 0); - buffer_put_u8 (&rtty->wb, sid); - ev_io_start (rtty->loop, &rtty->iow); + buffer_put_u16be(&rtty->wb, 2); + buffer_put_u8(&rtty->wb, 0); + buffer_put_u8(&rtty->wb, sid); + ev_io_start(rtty->loop, &rtty->iow); log_info("new tty: %d\n", sid); } static void write_data_to_tty(struct rtty *rtty, int sid, int len) { - struct tty *tty = find_tty (rtty, sid); + struct tty *tty = find_tty(rtty, sid); if (!tty) { log_err("non-existent sid: %d\n", sid); return; } - buffer_put_data (&tty->wb, buffer_data (&rtty->rb), len); + buffer_put_data(&tty->wb, buffer_data(&rtty->rb), len); buffer_pull(&rtty->rb, NULL, len); ev_io_start(rtty->loop, &tty->iow); } static void set_tty_winsize(struct rtty *rtty, int sid) { - struct tty *tty = find_tty (rtty, sid); + struct tty *tty = find_tty(rtty, sid); struct winsize size; if (!tty) { @@ -226,20 +232,20 @@ static void rtty_exit(struct rtty *rtty) rtty->sock = -1; if (rtty->sock_file > -1) { - close (rtty->sock_file); + close(rtty->sock_file); rtty->sock_file = -1; } - ev_io_stop (rtty->loop, &rtty->ior); - ev_io_stop (rtty->loop, &rtty->iow); - ev_io_stop (rtty->loop, &rtty->iof); + ev_io_stop(rtty->loop, &rtty->ior); + ev_io_stop(rtty->loop, &rtty->iow); + ev_io_stop(rtty->loop, &rtty->iof); for (int i = 0; i < RTTY_MAX_TTY; i++) if (rtty->ttys[i]) - del_tty (rtty->ttys[i]); + del_tty(rtty->ttys[i]); if (!rtty->reconnect) - ev_break (rtty->loop, EVBREAK_ALL); + ev_break(rtty->loop, EVBREAK_ALL); #if RTTY_SSL_SUPPORT rtty_ssl_free(rtty->ssl); @@ -281,48 +287,48 @@ static void parse_msg(struct rtty *rtty) int msglen; while (true) { - if (buffer_length (rb) < 3) + if (buffer_length(rb) < 3) return; msglen = buffer_get_u16be(rb, 1); - if (buffer_length (rb) < msglen + 3) + if (buffer_length(rb) < msglen + 3) return; - msgtype = buffer_pull_u8 (rb); - buffer_pull_u16 (rb); + msgtype = buffer_pull_u8(rb); + buffer_pull_u16(rb); switch (msgtype) { case MSG_TYPE_REGISTER: - if (buffer_pull_u8 (rb)) { + if (buffer_pull_u8(rb)) { char errs[128] = ""; - buffer_pull (rb, errs, msglen - 1); - rtty_exit (rtty); + buffer_pull(rb, errs, msglen - 1); + rtty_exit(rtty); log_err("register fail: %s\n", errs); return; } - buffer_pull (rb, NULL, msglen - 1); - log_info ("register success\n"); + buffer_pull(rb, NULL, msglen - 1); + log_info("register success\n"); break; case MSG_TYPE_LOGIN: - tty_login (rtty); + tty_login(rtty); break; case MSG_TYPE_LOGOUT: - tty_logout (rtty, buffer_pull_u8 (rb)); + tty_logout(rtty, buffer_pull_u8(rb)); break; case MSG_TYPE_TERMDATA: - write_data_to_tty (rtty, buffer_pull_u8 (rb), msglen - 1); + write_data_to_tty(rtty, buffer_pull_u8(rb), msglen - 1); break; case MSG_TYPE_WINSIZE: - set_tty_winsize (rtty, buffer_pull_u8 (rb)); + set_tty_winsize(rtty, buffer_pull_u8(rb)); break; case MSG_TYPE_CMD: - run_command (rtty, buffer_data (rb)); - buffer_pull (rb, NULL, msglen); + run_command(rtty, buffer_data(rb)); + buffer_pull(rb, NULL, msglen); break; case MSG_TYPE_HEARTBEAT: @@ -333,8 +339,8 @@ static void parse_msg(struct rtty *rtty) break; default: - log_err ("invalid message type: %d\n", msgtype); - rtty_exit (rtty); + log_err("invalid message type: %d\n", msgtype); + rtty_exit(rtty); return; } } @@ -342,7 +348,7 @@ static void parse_msg(struct rtty *rtty) static void on_net_read(struct ev_loop *loop, struct ev_io *w, int revents) { - struct rtty *rtty = container_of (w, struct rtty, ior); + struct rtty *rtty = container_of(w, struct rtty, ior); bool eof; int ret; @@ -353,7 +359,7 @@ static void on_net_read(struct ev_loop *loop, struct ev_io *w, int revents) #endif ret = buffer_put_fd(&rtty->rb, w->fd, 4096, &eof); if (ret < 0) { - log_err("socket read error: %s\n", strerror (errno)); + log_err("socket read error: %s\n", strerror(errno)); return; } @@ -362,15 +368,15 @@ static void on_net_read(struct ev_loop *loop, struct ev_io *w, int revents) parse_msg(rtty); if (eof) { - log_info ("socket closed by server\n"); - rtty_exit (rtty); + log_info("socket closed by server\n"); + rtty_exit(rtty); return; } } static void on_net_write(struct ev_loop *loop, struct ev_io *w, int revents) { - struct rtty *rtty = container_of (w, struct rtty, iow); + struct rtty *rtty = container_of(w, struct rtty, iow); int ret; #if RTTY_SSL_SUPPORT @@ -389,18 +395,18 @@ static void on_net_write(struct ev_loop *loop, struct ev_io *w, int revents) } if (rtty->ssl) - ret = buffer_pull_to_fd_ex (&rtty->wb, w->fd, -1, rtty_ssl_write, rtty->ssl); + ret = buffer_pull_to_fd_ex(&rtty->wb, w->fd, -1, rtty_ssl_write, rtty->ssl); else #endif - ret = buffer_pull_to_fd (&rtty->wb, w->fd, -1); + ret = buffer_pull_to_fd(&rtty->wb, w->fd, -1); if (ret < 0) { - log_err ("socket write error: %s\n", strerror(errno)); + log_err("socket write error: %s\n", strerror(errno)); rtty_exit(rtty); return; } - if (buffer_length (&rtty->wb) < 1) - ev_io_stop (loop, w); + if (buffer_length(&rtty->wb) < 1) + ev_io_stop(loop, w); } static void on_net_connected(int sock, void *arg) @@ -409,16 +415,16 @@ static void on_net_connected(int sock, void *arg) if (sock < 0) { if (!rtty->reconnect) - ev_break (rtty->loop, EVBREAK_ALL); + ev_break(rtty->loop, EVBREAK_ALL); return; } - log_info ("connected to server\n"); + log_info("connected to server\n"); rtty->sock = sock; ev_io_init(&rtty->ior, on_net_read, sock, EV_READ); - ev_io_start (rtty->loop, &rtty->ior); + ev_io_start(rtty->loop, &rtty->ior); ev_io_init(&rtty->iow, on_net_write, sock, EV_WRITE); @@ -426,7 +432,7 @@ static void on_net_connected(int sock, void *arg) if (rtty->ssl_on) { #if (RTTY_SSL_SUPPORT) - rtty_ssl_init((struct rtty_ssl_ctx **)&rtty->ssl, sock, rtty->host); + rtty_ssl_init((struct rtty_ssl_ctx **) &rtty->ssl, sock, rtty->host); #endif } @@ -435,14 +441,14 @@ static void on_net_connected(int sock, void *arg) static void rtty_timer_cb(struct ev_loop *loop, struct ev_timer *w, int revents) { - struct rtty *rtty = container_of (w, struct rtty, tmr); + struct rtty *rtty = container_of(w, struct rtty, tmr); ev_tstamp now = ev_now(loop); if (rtty->sock < 0) { if (now - rtty->active < 5) return; rtty->active = now; - log_err ("rtty reconnecting...\n"); + log_err("rtty reconnecting...\n"); tcp_connect(rtty->loop, rtty->host, rtty->port, on_net_connected, rtty); return; } @@ -498,8 +504,8 @@ int rtty_start(struct rtty *rtty) void rtty_send_msg(struct rtty *rtty, int type, void *data, int len) { struct buffer *wb = &rtty->wb; - buffer_put_u8 (wb, type); - buffer_put_u16be (wb, len); + buffer_put_u8(wb, type); + buffer_put_u16be(wb, len); buffer_put_data(wb, data, len); - ev_io_start (rtty->loop, &rtty->iow); + ev_io_start(rtty->loop, &rtty->iow); } diff --git a/src/ssl.c b/src/ssl.c index 395bc2c..a68697d 100644 --- a/src/ssl.c +++ b/src/ssl.c @@ -50,8 +50,10 @@ struct rtty_ssl_ctx { #else #if RTTY_HAVE_OPENSSL + #include #include + #elif RTTY_HAVE_WOLFSSL #define WC_NO_HARDEN #include diff --git a/src/ssl.h b/src/ssl.h index a8ca3b1..972968b 100644 --- a/src/ssl.h +++ b/src/ssl.h @@ -35,10 +35,13 @@ struct rtty_ssl_ctx; int rtty_ssl_init(struct rtty_ssl_ctx **ctx, int sock, const char *host); + int rtty_ssl_handshake(struct rtty_ssl_ctx *ctx); + void rtty_ssl_free(struct rtty_ssl_ctx *ctx); int rtty_ssl_read(int fd, void *buf, size_t count, void *arg); + int rtty_ssl_write(int fd, void *buf, size_t count, void *arg); #endif diff --git a/src/upfile.c b/src/upfile.c index fec3d6e..cecc9c7 100644 --- a/src/upfile.c +++ b/src/upfile.c @@ -51,7 +51,7 @@ static void on_socket_read(struct ev_loop *loop, struct ev_io *w, int revents) buffer_put_string(&b, abspath); buffer_put_zero(&b, 1); buffer_pull_to_fd(&b, w->fd, -1); - start_time = ev_now (loop); + start_time = ev_now(loop); break; case RTTY_FILE_MSG_PROGRESS: update_progress(loop, start_time, &b); @@ -75,17 +75,17 @@ void upload_file(const char *path) fd = open(path, O_RDONLY); if (fd < 0) { - fprintf (stderr, "open '%s' failed: ", path); + fprintf(stderr, "open '%s' failed: ", path); if (errno == ENOENT) - fprintf(stderr,"No such file\n"); + fprintf(stderr, "No such file\n"); else - fprintf(stderr,"%s\n", strerror (errno)); + fprintf(stderr, "%s\n", strerror(errno)); return; } fstat(fd, &st); if (!(st.st_mode & S_IFREG)) { - fprintf(stderr,"'%s' is not a regular file\n", path); + fprintf(stderr, "'%s' is not a regular file\n", path); close(fd); return; } @@ -103,7 +103,7 @@ void upload_file(const char *path) detect_sid('u'); ev_io_init(&ior, on_socket_read, sock, EV_READ); - ev_io_start (loop, &ior); + ev_io_start(loop, &ior); ev_signal_init(&sw, signal_cb, SIGINT); ev_signal_start(loop, &sw); diff --git a/src/utils.c b/src/utils.c index 77375ba..4d72f0d 100644 --- a/src/utils.c +++ b/src/utils.c @@ -66,39 +66,39 @@ int b64_encode(const void *src, size_t srclen, void *dest, size_t destsize) char *output = dest; while (srclen > 0) { - int skip = 1; - int i0 = input[0] >> 2; - int i1 = (input[0] & 0x3) << 4; - int i2 = 64; - int i3 = 64; + int skip = 1; + int i0 = input[0] >> 2; + int i1 = (input[0] & 0x3) << 4; + int i2 = 64; + int i3 = 64; - if (destsize < 5) - return -1; + if (destsize < 5) + return -1; - if (srclen > 1) { - skip++; - i1 += input[1] >> 4; - i2 = (input[1] & 0xF) << 2; + if (srclen > 1) { + skip++; + i1 += input[1] >> 4; + i2 = (input[1] & 0xF) << 2; - if (srclen > 2) { - i2 += input[2] >> 6; - i3 = input[2] & 0x3F; - skip++; - } - } - - *output++ = Base64[i0]; - *output++ = Base64[i1]; - *output++ = Base64[i2]; - *output++ = Base64[i3]; - - input += skip; - srclen -= skip; - destsize -= 4; + if (srclen > 2) { + i2 += input[2] >> 6; + i3 = input[2] & 0x3F; + skip++; + } } + *output++ = Base64[i0]; + *output++ = Base64[i1]; + *output++ = Base64[i2]; + *output++ = Base64[i3]; + + input += skip; + srclen -= skip; + destsize -= 4; + } + *output++ = 0; - return output - (char *)dest - 1; + return output - (char *) dest - 1; } const char *format_size(size_t size) @@ -106,11 +106,11 @@ const char *format_size(size_t size) static char str[64]; if (size < 1024) - sprintf(str,"%zu B", size); + sprintf(str, "%zu B", size); else if (size < 1024 * 1024) - sprintf(str,"%.2f KB", size / 1024.0); + sprintf(str, "%.2f KB", size / 1024.0); else - sprintf(str,"%.2f MB", size / 1024.0 / 1024.0); + sprintf(str, "%.2f MB", size / 1024.0 / 1024.0); return str; }