aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortomsmeding <tom.smeding@gmail.com>2017-04-14 20:43:29 +0200
committertomsmeding <tom.smeding@gmail.com>2017-04-14 20:43:29 +0200
commitc02fa99188c868496f864a20b1139ec4e6f93c2a (patch)
treea1c2b5ac84405da437bba639da75286a29961770
parenta1667f92905da03c1c3e26e8311fa7bd2802ca78 (diff)
server: Factor out net related functions from command.c
-rw-r--r--command.c137
-rw-r--r--net.c75
-rw-r--r--net.h10
3 files changed, 119 insertions, 103 deletions
diff --git a/command.c b/command.c
index 4e7180d..92556de 100644
--- a/command.c
+++ b/command.c
@@ -6,6 +6,7 @@
#include <sys/socket.h>
#include "command.h"
#include "db.h"
+#include "net.h"
#include "user_data.h"
@@ -16,90 +17,20 @@ static i64 make_timestamp(void){
}
-static bool send_raw_text(int fd,const char *text,i64 len){
- i64 cursor=0;
- while(cursor<len){
- i64 nwr=send(fd,text+cursor,len-cursor,0);
- if(nwr<0){
- if(errno==EINTR)continue;
- if(errno==ECONNRESET||errno==EPIPE)return true;
- die_perror("send");
- }
- cursor+=nwr;
- }
- return false;
-}
-
-static bool send_ok(int fd,const char *tag){
- char *buf=NULL;
- i64 len=asprintf(&buf,"%s ok\n",tag);
- bool closed=send_raw_text(fd,buf,len);
- free(buf);
- return closed;
-}
-
-static bool send_error(int fd,const char *tag,const char *msg){
- char *buf=NULL;
- i64 len=asprintf(&buf,"%s error %s\n",tag,msg);
- bool closed=send_raw_text(fd,buf,len);
- free(buf);
- return closed;
-}
-
-static bool send_name(int fd,const char *tag,const char *name){
- char *buf=NULL;
- i64 len=asprintf(&buf,"%s name %s\n",tag,name);
- bool closed=send_raw_text(fd,buf,len);
- free(buf);
- return closed;
-}
-
-static bool send_list(int fd,const char *tag,i64 count,const char **list){
- char *buf=NULL;
- i64 len=asprintf(&buf,"%s list %" PRIi64,tag,count);
- bool closed=send_raw_text(fd,buf,len);
- free(buf);
- if(closed)return true;
-
- if(count>0){
- i64 bufsz=64;
- buf=malloc(bufsz,char);
-
- for(i64 i=0;i<count;i++){
- i64 len=strlen(list[i]);
- if(len>=bufsz){
- bufsz=len+512;
- buf=realloc(buf,bufsz,char);
- }
- memcpy(buf+1,list[i],len);
- buf[0]=' ';
- if(send_raw_text(fd,buf,len+1)){
- free(buf);
- return true;
- }
- }
-
- free(buf);
- }
-
- return send_raw_text(fd,"\n",1);
-}
-
-
static bool cmd_register(struct conn_data *data,const char *tag,const char **args){
i64 userid=db_find_user(args[0]);
if(userid!=-1){
- send_error(data->fd,tag,"Username already exists");
+ net_send_error(data->fd,tag,"Username already exists");
return false;
}
db_create_user(args[0],args[1]);
- return send_ok(data->fd,tag);
+ return net_send_ok(data->fd,tag);
}
static bool cmd_login(struct conn_data *data,const char *tag,const char **args){
i64 userid=db_find_user(args[0]);
if(userid==-1){
- send_error(data->fd,tag,"User not found");
+ net_send_error(data->fd,tag,"User not found");
return false;
}
char *pass=db_get_pass(userid);
@@ -111,10 +42,10 @@ static bool cmd_login(struct conn_data *data,const char *tag,const char **args){
if(success){
data->userid=userid;
userdata_register(userid,data->fd);
- send_ok(data->fd,tag);
+ net_send_ok(data->fd,tag);
} else {
data->userid=-1;
- send_error(data->fd,tag,"Incorrect password");
+ net_send_error(data->fd,tag,"Incorrect password");
}
return false;
}
@@ -122,48 +53,48 @@ static bool cmd_login(struct conn_data *data,const char *tag,const char **args){
static bool cmd_list_rooms(struct conn_data *data,const char *tag,const char **args){
(void)args;
if(data->userid==-1){
- send_error(data->fd,tag,"Not logged in");
+ net_send_error(data->fd,tag,"Not logged in");
return false;
}
struct db_room_list rl=db_list_rooms(data->userid);
if(rl.count<=0){
db_nullify_room_list(rl);
- return send_list(data->fd,tag,0,NULL);
+ return net_send_list(data->fd,tag,0,NULL);
}
const char *names[rl.count];
for(i64 i=0;i<rl.count;i++){
names[i]=rl.list[i].name;
}
- bool closed=send_list(data->fd,tag,rl.count,names);
+ bool closed=net_send_list(data->fd,tag,rl.count,names);
db_nullify_room_list(rl);
return closed;
}
static bool cmd_list_members(struct conn_data *data,const char *tag,const char **args){
if(data->userid==-1){
- send_error(data->fd,tag,"Not logged in");
+ net_send_error(data->fd,tag,"Not logged in");
return false;
}
i64 roomid=db_find_room(args[0]);
if(roomid==-1){
- send_error(data->fd,tag,"Room not found");
+ net_send_error(data->fd,tag,"Room not found");
return false;
}
if(!db_is_member(roomid,data->userid)){
- send_error(data->fd,tag,"Not in that room");
+ net_send_error(data->fd,tag,"Not in that room");
return false;
}
struct db_user_list ul=db_list_members(roomid);
if(ul.count<=0){
db_nullify_user_list(ul);
- return send_list(data->fd,tag,0,NULL);
+ return net_send_list(data->fd,tag,0,NULL);
}
const char *names[ul.count];
for(i64 i=0;i<ul.count;i++){
names[i]=ul.list[i].name;
}
- bool closed=send_list(data->fd,tag,ul.count,names);
+ bool closed=net_send_list(data->fd,tag,ul.count,names);
db_nullify_user_list(ul);
return closed;
}
@@ -171,39 +102,39 @@ static bool cmd_list_members(struct conn_data *data,const char *tag,const char *
static bool cmd_create_room(struct conn_data *data,const char *tag,const char **args){
(void)args;
if(data->userid==-1){
- send_error(data->fd,tag,"Not logged in");
+ net_send_error(data->fd,tag,"Not logged in");
return false;
}
struct db_name_id room=db_create_room();
db_add_member(room.id,data->userid);
- bool closed=send_name(data->fd,tag,room.name);
+ bool closed=net_send_name(data->fd,tag,room.name);
db_nullify_name_id(room);
return closed;
}
static bool cmd_invite(struct conn_data *data,const char *tag,const char **args){
if(data->userid==-1){
- send_error(data->fd,tag,"Not logged in");
+ net_send_error(data->fd,tag,"Not logged in");
return false;
}
i64 roomid=db_find_room(args[0]);
if(roomid==-1){
- send_error(data->fd,tag,"Room not found");
+ net_send_error(data->fd,tag,"Room not found");
return false;
}
i64 user2=db_find_user(args[1]);
if(user2==-1){
- send_error(data->fd,tag,"User not found");
+ net_send_error(data->fd,tag,"User not found");
return false;
}
if(!db_is_member(roomid,data->userid)){
- send_error(data->fd,tag,"Not in that room");
+ net_send_error(data->fd,tag,"Not in that room");
return false;
}
if(db_is_member(roomid,user2)){
- send_error(data->fd,tag,"User already in that room");
+ net_send_error(data->fd,tag,"User already in that room");
return false;
}
@@ -223,9 +154,9 @@ static bool cmd_invite(struct conn_data *data,const char *tag,const char **args)
for(i64 j=0;j<nfds;j++){
if(fds[j]!=data->fd){
if(members.list[i].id==user2){
- send_raw_text(fds[j],invitebuf,invitebuflen);
+ net_send_raw_text(fds[j],invitebuf,invitebuflen);
} else {
- send_raw_text(fds[j],joinbuf,joinbuflen);
+ net_send_raw_text(fds[j],joinbuf,joinbuflen);
}
}
}
@@ -235,27 +166,27 @@ static bool cmd_invite(struct conn_data *data,const char *tag,const char **args)
free(joinbuf);
free(invitebuf);
- return send_ok(data->fd,tag);
+ return net_send_ok(data->fd,tag);
}
static bool cmd_send(struct conn_data *data,const char *tag,const char **args){
if(data->userid==-1){
- send_error(data->fd,tag,"Not logged in");
+ net_send_error(data->fd,tag,"Not logged in");
return false;
}
i64 roomid=db_find_room(args[0]);
if(roomid==-1){
- send_error(data->fd,tag,"Room not found");
+ net_send_error(data->fd,tag,"Room not found");
return false;
}
if(!db_is_member(roomid,data->userid)){
- send_error(data->fd,tag,"Not in that room");
+ net_send_error(data->fd,tag,"Not in that room");
return false;
}
i64 timestamp=make_timestamp();
db_create_message(roomid,data->userid,make_timestamp(),args[1]);
- bool closed=send_ok(data->fd,tag);
+ bool closed=net_send_ok(data->fd,tag);
char *pushbuf=NULL;
char *username=db_get_username(data->userid);
@@ -270,7 +201,7 @@ static bool cmd_send(struct conn_data *data,const char *tag,const char **args){
if(nfds<=0)continue;
for(i64 j=0;j<nfds;j++){
if(fds[j]!=data->fd){
- send_raw_text(fds[j],pushbuf,pushbuflen);
+ net_send_raw_text(fds[j],pushbuf,pushbuflen);
}
}
}
@@ -290,23 +221,23 @@ static bool cmd_history(struct conn_data *data,const char *tag,const char **args
}
if(data->userid==-1){
- send_error(data->fd,tag,"Not logged in");
+ net_send_error(data->fd,tag,"Not logged in");
return false;
}
i64 roomid=db_find_room(args[0]);
if(roomid==-1){
- send_error(data->fd,tag,"Room not found");
+ net_send_error(data->fd,tag,"Room not found");
return false;
}
if(!db_is_member(roomid,data->userid)){
- send_error(data->fd,tag,"Not in that room");
+ net_send_error(data->fd,tag,"Not in that room");
return false;
}
struct db_message_list ml=db_get_messages(roomid,nrequested);
char *buf=NULL;
i64 len=asprintf(&buf,"%s history %" PRIi64 "\n",tag,ml.count);
- bool closed=send_raw_text(data->fd,buf,len);
+ bool closed=net_send_raw_text(data->fd,buf,len);
free(buf);
if(closed){
@@ -318,7 +249,7 @@ static bool cmd_history(struct conn_data *data,const char *tag,const char **args
char *username=db_get_username(ml.list[i].userid);
len=asprintf(&buf,"%s history_message %" PRIi64 " %s %s %" PRIi64 " %s\n",
tag,ml.count-1-i,args[0],username,ml.list[i].timestamp,ml.list[i].message);
- closed=send_raw_text(data->fd,buf,len);
+ closed=net_send_raw_text(data->fd,buf,len);
free(buf);
if(closed)break;
}
diff --git a/net.c b/net.c
new file mode 100644
index 0000000..8e6644c
--- /dev/null
+++ b/net.c
@@ -0,0 +1,75 @@
+#include <string.h>
+#include <unistd.h>
+#include <errno.h>
+#include <sys/socket.h>
+#include "net.h"
+
+
+bool net_send_raw_text(int fd,const char *text,i64 len){
+ i64 cursor=0;
+ while(cursor<len){
+ i64 nwr=send(fd,text+cursor,len-cursor,0);
+ if(nwr<0){
+ if(errno==EINTR)continue;
+ if(errno==ECONNRESET||errno==EPIPE)return true;
+ die_perror("send");
+ }
+ cursor+=nwr;
+ }
+ return false;
+}
+
+bool net_send_ok(int fd,const char *tag){
+ char *buf=NULL;
+ i64 len=asprintf(&buf,"%s ok\n",tag);
+ bool closed=net_send_raw_text(fd,buf,len);
+ free(buf);
+ return closed;
+}
+
+bool net_send_error(int fd,const char *tag,const char *msg){
+ char *buf=NULL;
+ i64 len=asprintf(&buf,"%s error %s\n",tag,msg);
+ bool closed=net_send_raw_text(fd,buf,len);
+ free(buf);
+ return closed;
+}
+
+bool net_send_name(int fd,const char *tag,const char *name){
+ char *buf=NULL;
+ i64 len=asprintf(&buf,"%s name %s\n",tag,name);
+ bool closed=net_send_raw_text(fd,buf,len);
+ free(buf);
+ return closed;
+}
+
+bool net_send_list(int fd,const char *tag,i64 count,const char **list){
+ char *buf=NULL;
+ i64 len=asprintf(&buf,"%s list %" PRIi64,tag,count);
+ bool closed=net_send_raw_text(fd,buf,len);
+ free(buf);
+ if(closed)return true;
+
+ if(count>0){
+ i64 bufsz=64;
+ buf=malloc(bufsz,char);
+
+ for(i64 i=0;i<count;i++){
+ i64 len=strlen(list[i]);
+ if(len>=bufsz){
+ bufsz=len+512;
+ buf=realloc(buf,bufsz,char);
+ }
+ memcpy(buf+1,list[i],len);
+ buf[0]=' ';
+ if(net_send_raw_text(fd,buf,len+1)){
+ free(buf);
+ return true;
+ }
+ }
+
+ free(buf);
+ }
+
+ return net_send_raw_text(fd,"\n",1);
+}
diff --git a/net.h b/net.h
new file mode 100644
index 0000000..de78c12
--- /dev/null
+++ b/net.h
@@ -0,0 +1,10 @@
+#pragma once
+
+#include "global.h"
+
+
+bool net_send_raw_text(int fd,const char *text,i64 len);
+bool net_send_ok(int fd,const char *tag);
+bool net_send_error(int fd,const char *tag,const char *msg);
+bool net_send_name(int fd,const char *tag,const char *name);
+bool net_send_list(int fd,const char *tag,i64 count,const char **list);