diff options
author | Tom Smeding <tom.smeding@gmail.com> | 2020-06-28 22:18:57 +0200 |
---|---|---|
committer | Tom Smeding <tom.smeding@gmail.com> | 2020-06-28 22:18:57 +0200 |
commit | b85c1a60bf7f27e37fe2a86b26aa83ff198e208e (patch) | |
tree | eac45cf0a5c0f24ac04f6d07dd20504370cd0af9 /command.c | |
parent | f2f4f0d94a5280e15d89c25832825bb3ce65d2fd (diff) |
server/protocol: Protocol version negotiation
Diffstat (limited to 'command.c')
-rw-r--r-- | command.c | 24 |
1 files changed, 24 insertions, 0 deletions
@@ -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; |