Skip to content

Commit

Permalink
Make all messages sent between the client and server fixed size.
Browse files Browse the repository at this point in the history
This is the first of two changes to make the protocol more resilient and less
sensitive to other changes in the code, particularly with commands. The client
now packs argv into a buffer and sends it to the server for parsing, rather
than doing it itself and sending the parsed command data.

As a side-effect this also removes a lot of now-unused command marshalling
code.

Mixing a server without this change and a client with or vice versa will cause
tmux to hang or crash, please ensure that tmux is entirely killed before
upgrading.
  • Loading branch information
nicm committed Jul 26, 2009
1 parent 2bb89bc commit 34a82e7
Show file tree
Hide file tree
Showing 82 changed files with 253 additions and 780 deletions.
16 changes: 0 additions & 16 deletions client-fn.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,3 @@ client_write_server(
if (buf != NULL && len > 0)
buffer_write(cctx->srv_out, buf, len);
}

void
client_write_server2(struct client_ctx *cctx,
enum hdrtype type, void *buf1, size_t len1, void *buf2, size_t len2)
{
struct hdr hdr;

hdr.type = type;
hdr.size = len1 + len2;
buffer_write(cctx->srv_out, &hdr, sizeof hdr);

if (buf1 != NULL && len1 > 0)
buffer_write(cctx->srv_out, buf1, len1);
if (buf2 != NULL && len2 > 0)
buffer_write(cctx->srv_out, buf2, len2);
}
13 changes: 6 additions & 7 deletions client-msg.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,15 @@ client_msg_dispatch(struct client_ctx *cctx)
int
client_msg_fn_error(struct hdr *hdr, struct client_ctx *cctx)
{
char *errstr;
struct msg_print_data data;

if (hdr->size == SIZE_MAX)
fatalx("bad MSG_ERROR size");
if (hdr->size < sizeof data)
fatalx("bad MSG_PRINT size");
buffer_read(cctx->srv_in, &data, sizeof data);

errstr = xmalloc(hdr->size + 1);
buffer_read(cctx->srv_in, errstr, hdr->size);
errstr[hdr->size] = '\0';
data.msg[(sizeof data.msg) - 1] = '\0';
cctx->errstr = xstrdup(data.msg);

cctx->errstr = errstr;
return (-1);
}

Expand Down
19 changes: 11 additions & 8 deletions client.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ client_init(char *path, struct client_ctx *cctx, int cmdflags, int flags)
struct winsize ws;
size_t size;
int mode;
struct buffer *b;
char *name;
char *name, *term;
char rpathbuf[MAXPATHLEN];

if (realpath(path, rpathbuf) == NULL)
Expand Down Expand Up @@ -103,20 +102,24 @@ client_init(char *path, struct client_ctx *cctx, int cmdflags, int flags)
data.flags = flags;
data.sx = ws.ws_col;
data.sy = ws.ws_row;
*data.tty = '\0';

if (getcwd(data.cwd, sizeof data.cwd) == NULL)
*data.cwd = '\0';

*data.term = '\0';
if ((term = getenv("TERM")) != NULL) {
if (strlcpy(data.term,
term, sizeof data.term) >= sizeof data.term)
*data.term = '\0';
}

*data.tty = '\0';
if ((name = ttyname(STDIN_FILENO)) == NULL)
fatal("ttyname failed");
if (strlcpy(data.tty, name, sizeof data.tty) >= sizeof data.tty)
fatalx("ttyname failed");

b = buffer_create(BUFSIZ);
cmd_send_string(b, getenv("TERM"));
client_write_server2(cctx, MSG_IDENTIFY,
&data, sizeof data, BUFFER_OUT(b), BUFFER_USED(b));
buffer_destroy(b);
client_write_server(cctx, MSG_IDENTIFY, &data, sizeof data);
}

return (0);
Expand Down
2 changes: 0 additions & 2 deletions cmd-attach-session.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ const struct cmd_entry cmd_attach_session_entry = {
cmd_target_init,
cmd_target_parse,
cmd_attach_session_exec,
cmd_target_send,
cmd_target_recv,
cmd_target_free,
cmd_target_print
};
Expand Down
23 changes: 0 additions & 23 deletions cmd-bind-key.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@

int cmd_bind_key_parse(struct cmd *, int, char **, char **);
int cmd_bind_key_exec(struct cmd *, struct cmd_ctx *);
void cmd_bind_key_send(struct cmd *, struct buffer *);
void cmd_bind_key_recv(struct cmd *, struct buffer *);
void cmd_bind_key_free(struct cmd *);
size_t cmd_bind_key_print(struct cmd *, char *, size_t);

Expand All @@ -44,8 +42,6 @@ const struct cmd_entry cmd_bind_key_entry = {
NULL,
cmd_bind_key_parse,
cmd_bind_key_exec,
cmd_bind_key_send,
cmd_bind_key_recv,
cmd_bind_key_free,
cmd_bind_key_print
};
Expand Down Expand Up @@ -113,25 +109,6 @@ cmd_bind_key_exec(struct cmd *self, unused struct cmd_ctx *ctx)
return (0);
}

void
cmd_bind_key_send(struct cmd *self, struct buffer *b)
{
struct cmd_bind_key_data *data = self->data;

buffer_write(b, data, sizeof *data);
cmd_list_send(data->cmdlist, b);
}

void
cmd_bind_key_recv(struct cmd *self, struct buffer *b)
{
struct cmd_bind_key_data *data;

self->data = data = xmalloc(sizeof *data);
buffer_read(b, data, sizeof *data);
data->cmdlist = cmd_list_recv(b);
}

void
cmd_bind_key_free(struct cmd *self)
{
Expand Down
2 changes: 0 additions & 2 deletions cmd-break-pane.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ const struct cmd_entry cmd_break_pane_entry = {
cmd_pane_init,
cmd_pane_parse,
cmd_break_pane_exec,
cmd_pane_send,
cmd_pane_recv,
cmd_pane_free,
cmd_pane_print
};
Expand Down
2 changes: 0 additions & 2 deletions cmd-choose-session.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ const struct cmd_entry cmd_choose_session_entry = {
cmd_target_init,
cmd_target_parse,
cmd_choose_session_exec,
cmd_target_send,
cmd_target_recv,
cmd_target_free,
cmd_target_print
};
Expand Down
2 changes: 0 additions & 2 deletions cmd-choose-window.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ const struct cmd_entry cmd_choose_window_entry = {
cmd_target_init,
cmd_target_parse,
cmd_choose_window_exec,
cmd_target_send,
cmd_target_recv,
cmd_target_free,
cmd_target_print
};
Expand Down
2 changes: 0 additions & 2 deletions cmd-clear-history.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ const struct cmd_entry cmd_clear_history_entry = {
cmd_pane_init,
cmd_pane_parse,
cmd_clear_history_exec,
cmd_pane_send,
cmd_pane_recv,
cmd_pane_free,
cmd_pane_print
};
Expand Down
2 changes: 0 additions & 2 deletions cmd-clock-mode.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ const struct cmd_entry cmd_clock_mode_entry = {
cmd_target_init,
cmd_target_parse,
cmd_clock_mode_exec,
cmd_target_send,
cmd_target_recv,
cmd_target_free,
cmd_target_print
};
Expand Down
2 changes: 0 additions & 2 deletions cmd-command-prompt.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ const struct cmd_entry cmd_command_prompt_entry = {
cmd_command_prompt_init,
cmd_target_parse,
cmd_command_prompt_exec,
cmd_target_send,
cmd_target_recv,
cmd_target_free,
cmd_target_print
};
Expand Down
2 changes: 0 additions & 2 deletions cmd-confirm-before.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ const struct cmd_entry cmd_confirm_before_entry = {
cmd_confirm_before_init,
cmd_target_parse,
cmd_confirm_before_exec,
cmd_target_send,
cmd_target_recv,
cmd_target_free,
cmd_target_print
};
Expand Down
25 changes: 0 additions & 25 deletions cmd-copy-buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@

int cmd_copy_buffer_parse(struct cmd *, int, char **, char **);
int cmd_copy_buffer_exec(struct cmd *, struct cmd_ctx *);
void cmd_copy_buffer_send(struct cmd *, struct buffer *);
void cmd_copy_buffer_recv(struct cmd *, struct buffer *);
void cmd_copy_buffer_free(struct cmd *);
void cmd_copy_buffer_init(struct cmd *, int);
size_t cmd_copy_buffer_print(struct cmd *, char *, size_t);
Expand All @@ -46,8 +44,6 @@ const struct cmd_entry cmd_copy_buffer_entry = {
cmd_copy_buffer_init,
cmd_copy_buffer_parse,
cmd_copy_buffer_exec,
cmd_copy_buffer_send,
cmd_copy_buffer_recv,
cmd_copy_buffer_free,
cmd_copy_buffer_print
};
Expand Down Expand Up @@ -160,27 +156,6 @@ cmd_copy_buffer_exec(struct cmd *self, struct cmd_ctx *ctx)
return (0);
}

void
cmd_copy_buffer_send(struct cmd *self, struct buffer *b)
{
struct cmd_copy_buffer_data *data = self->data;

buffer_write(b, data, sizeof *data);
cmd_send_string(b, data->dst_session);
cmd_send_string(b, data->src_session);
}

void
cmd_copy_buffer_recv(struct cmd *self, struct buffer *b)
{
struct cmd_copy_buffer_data *data;

self->data = data = xmalloc(sizeof *data);
buffer_read(b, data, sizeof *data);
data->dst_session = cmd_recv_string(b);
data->src_session = cmd_recv_string(b);
}

void
cmd_copy_buffer_free(struct cmd *self)
{
Expand Down
2 changes: 0 additions & 2 deletions cmd-copy-mode.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ const struct cmd_entry cmd_copy_mode_entry = {
cmd_target_init,
cmd_target_parse,
cmd_copy_mode_exec,
cmd_target_send,
cmd_target_recv,
cmd_target_free,
NULL
};
Expand Down
2 changes: 0 additions & 2 deletions cmd-delete-buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ const struct cmd_entry cmd_delete_buffer_entry = {
cmd_buffer_init,
cmd_buffer_parse,
cmd_delete_buffer_exec,
cmd_buffer_send,
cmd_buffer_recv,
cmd_buffer_free,
cmd_buffer_print
};
Expand Down
2 changes: 0 additions & 2 deletions cmd-detach-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ const struct cmd_entry cmd_detach_client_entry = {
cmd_target_init,
cmd_target_parse,
cmd_detach_client_exec,
cmd_target_send,
cmd_target_recv,
cmd_target_free,
cmd_target_print
};
Expand Down
2 changes: 0 additions & 2 deletions cmd-display-message.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ const struct cmd_entry cmd_display_message_entry = {
cmd_target_init,
cmd_target_parse,
cmd_display_message_exec,
cmd_target_send,
cmd_target_recv,
cmd_target_free,
cmd_target_print
};
Expand Down
2 changes: 0 additions & 2 deletions cmd-down-pane.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ const struct cmd_entry cmd_down_pane_entry = {
cmd_target_init,
cmd_target_parse,
cmd_down_pane_exec,
cmd_target_send,
cmd_target_recv,
cmd_target_free,
cmd_target_print
};
Expand Down
2 changes: 0 additions & 2 deletions cmd-find-window.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ const struct cmd_entry cmd_find_window_entry = {
cmd_target_init,
cmd_target_parse,
cmd_find_window_exec,
cmd_target_send,
cmd_target_recv,
cmd_target_free,
cmd_target_print
};
Expand Down
Loading

0 comments on commit 34a82e7

Please sign in to comment.