From b85c1a60bf7f27e37fe2a86b26aa83ff198e208e Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Sun, 28 Jun 2020 22:18:57 +0200 Subject: server/protocol: Protocol version negotiation --- command.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'command.c') diff --git a/command.c b/command.c index 5121ddf..6e73ce3 100644 --- a/command.c +++ b/command.c @@ -25,6 +25,22 @@ struct cmd_retval{ #define RET_MEMZERO ((struct cmd_retval){.socket_close=false,.memzero=true}) #define RET_MEMZERO_CLOSE(close_) ((struct cmd_retval){.socket_close=(close_),.memzero=true}) +static struct cmd_retval cmd_version(struct conn_data *data,const char *tag,const char **args){ + i64 version; + if (!parse_i64(args[0], &version) + || version < MIN_SUPPORTED_PROTOCOL_VERSION + || version > PROTOCOL_VERSION) { + data->protversion = -1; + net_send_error(data->fd, tag, "Version not supported"); + return RET_OK; + } + + data->protversion = version; + + net_send_ok(data->fd, tag); + return RET_OK; +} + static struct cmd_retval cmd_register(struct conn_data *data,const char *tag,const char **args){ i64 userid=db_find_user(args[0]); if(userid!=-1){ @@ -375,6 +391,7 @@ struct cmd_info{ }; static const struct cmd_info commands[]={ + {"version",1,false,cmd_version}, {"register",2,true,cmd_register}, {"login",2,true,cmd_login}, {"logout",0,false,cmd_logout}, @@ -423,6 +440,13 @@ bool handle_input_line(struct conn_data *data,char *line,size_t linelen){ return true; } + // Ensure first command is 'version' + if(data->protversion==-1&&strcmp(commands[cmdi].cmdname,"version")!=0){ + debug("Command %s before version negotiation on connection %d", + commands[cmdi].cmdname,data->fd); + return true; + } + int nargs=commands[cmdi].nargs; char *args[nargs]; size_t cursor=cmdlen+1; -- cgit v1.2.3-54-g00ecf