aboutsummaryrefslogtreecommitdiff
path: root/command.c
diff options
context:
space:
mode:
Diffstat (limited to 'command.c')
-rw-r--r--command.c24
1 files changed, 24 insertions, 0 deletions
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;