Kaynağa Gözat

vccd: check if the user is logged in

intirain 4 yıl önce
ebeveyn
işleme
46a4829a2b
7 değiştirilmiş dosya ile 39 ekleme ve 11 silme
  1. 1 1
      Makefile
  2. 11 0
      include/klist.h
  3. 2 2
      include/vcc/version.h
  4. 1 1
      main.c
  5. 5 6
      session.c
  6. 5 1
      users.c
  7. 14 0
      vcc.c

+ 1 - 1
Makefile

@@ -1,5 +1,5 @@
 VCCD_MAJOR 		= 1
-VCCD_MINOR 		= 18
+VCCD_MINOR 		= 19
 VCCD_PATCH 		= 0
 VCCD_STR 		= -rc 
 

+ 11 - 0
include/klist.h

@@ -29,8 +29,19 @@
  * is silly, but now i realize that, i'm the stupid one, there are 
  * too many things in the structure. */
 
+#define STAT_UNLOGGED 			0
+#define STAT_LOGGED 			1
+
+#define check_logged(n) \
+	if (find_connection(n)->stat != STAT_LOGGED) { \
+		vcc_log("%d requested before logging in. \n", fd); \
+		vcc_info("WUNLOG %d\n", fd); \
+		return 1; \
+	}
+
 struct klist_node {
 	long 				value;
+	int 				stat;
 
 	union {
 		void 			*data;

+ 2 - 2
include/vcc/version.h

@@ -1,2 +1,2 @@
-#define VCCD_VERSION "vccd 1.18.0 Sat Jun 18 03:54:07 PM CST 2022 root"
-#define VCCD_SEMVER "vccd 1.18.0-rc "
+#define VCCD_VERSION "vccd 1.19.0 Sun Jun 19 07:04:37 PM CST 2022 root"
+#define VCCD_SEMVER "vccd 1.19.0-rc "

+ 1 - 1
main.c

@@ -209,7 +209,7 @@ int main(int argc, const char **argv) {
 	if (unlikely(argc != 1)) 
 		return usage();
 
-	becomes_daemon();
+//	becomes_daemon();
 	init_socket();
 	users_init();
 

+ 5 - 6
session.c

@@ -202,8 +202,8 @@ int session_broadcast(int sid, char *msg, char *usrname, int except, int flags)
 
 
 int session_name(int fd, struct vcc_request *req) {
-	struct klist_node *n;
 	struct vcc_request *out;
+	char *p;
 
 	if (unlikely(!(out = amalloc(sizeof(struct vcc_request))))) {
 		vcc_log("*** malloc() failed\n");
@@ -215,9 +215,9 @@ int session_name(int fd, struct vcc_request *req) {
 	out->reqtype = htonl(REQ_CTL_SENAME);
 
 	if (req->session == 0) 
-		strcpy(out->msg, "broadcast");
+		p = "broadcast";
 
-	else if (unlikely(!(n = find_session(ntohl(req->session))))) {
+	else if (unlikely(!(p = find_session(ntohl(req->session))->usrname))) {
 		out->session = htonl(-1);
 		vcc_send_req(find_connection(fd), out);
 
@@ -226,13 +226,12 @@ int session_name(int fd, struct vcc_request *req) {
 		return 1;
 	}
 
-	else 
-		strcpy(out->msg, n->usrname);
+	strcpy(out->msg, p);
 
 	out->session = req->session;
 	vcc_send_req(find_connection(fd), out);
 
-	vcc_log("sename: %s sent to %d\n", n->usrname, fd);
+	vcc_log("sename: %s sent to %d\n", p, fd);
 
 	return 0;
 }

+ 5 - 1
users.c

@@ -228,6 +228,7 @@ int do_ia_login(int fd, struct vcc_request *req) {
 
 int init_user_info(int fd, char *usrname, struct user *u) {
 	struct klist_node 	*n;
+	struct klist_node 	*p;
 	static const char 	*join_msg = "%s logged in. now there are %d users. \n";
 	char 			*t;
 	struct vcc_request 	*out;
@@ -277,11 +278,14 @@ int init_user_info(int fd, char *usrname, struct user *u) {
 	n->data = u;
 
 	klist_add(&logged_users, n);
+	p = find_connection(fd);
+
+	p->stat = STAT_LOGGED;
 
 	out->magic = htonl(VCC_MAGIC);
 	out->reqtype = htonl(REQ_CTL_LOGIN);
 	out->uid = htonl(1);
-	vcc_send_req(find_connection(fd), out);
+	vcc_send_req(p, out);
 
 	/* nusers already inc'ed when it connects */
 

+ 14 - 0
vcc.c

@@ -58,6 +58,7 @@ int vcc_new_connection(int fd, struct in_addr *addr) {
 	klist_init(n);
 	n->data = addr;
 	n->value = fd;
+	n->stat = STAT_UNLOGGED;
 
 	if (unlikely(!(n->sending = amalloc(sizeof(struct klist_node))))) {
 		vcc_log("*** malloc() failed\n");
@@ -185,6 +186,9 @@ int vcc_message_broadcast(char *msg, char *usrname, int except_fd, int flags) {
 		if (n->value == except_fd) 
 			continue;
 
+		if (n->stat == STAT_UNLOGGED) 
+			continue;
+
 		if (unlikely(!(req = amalloc(sizeof(struct vcc_request))))) {
 			vcc_log("*** malloc() failed\n");
 
@@ -298,6 +302,7 @@ int vcc_new_msg(int fd, struct vcc_request *req) {
 		return 0;
 
 	case REQ_MSG_SEND:
+		check_logged(fd);
 		if (((unsigned char *) msg)[0] == 0xffu || !*msg) 
 			return 0;
 
@@ -314,6 +319,7 @@ int vcc_new_msg(int fd, struct vcc_request *req) {
 		break;
 
 	case REQ_CTL_USRS:
+		check_logged(fd);
 		send_users(fd);
 
 		break;
@@ -325,42 +331,50 @@ int vcc_new_msg(int fd, struct vcc_request *req) {
 		break;
 
 	case REQ_CTL_IALOG:
+		check_logged(fd);
 		do_ia_login(fd, req);
 		vcc_update_usrname(fd, req->usrname);
 
 		break;
 
 	case REQ_CTL_NEWSE:
+		check_logged(fd);
 		create_session(fd, req->usrname);
 
 		break;
 
 	case REQ_CTL_SESS:
+		check_logged(fd);
 		send_sessions(fd);
 
 		break;
 
 	case REQ_CTL_JOINS:
+		check_logged(fd);
 		join_session(ntohl(req->session), fd);
 
 		break;
 
 	case REQ_CTL_UINFO:
+		check_logged(fd);
 		get_user_info(fd, req);
 
 		break;
 
 	case REQ_SYS_SCRINC:
+		check_logged(fd);
 		incr_score(fd, req);
 
 		break;
 
 	case REQ_SYS_INFO:
+		check_logged(fd);
 		monitor_info(fd, req);
 
 		break;
 
 	case REQ_CTL_SENAME:
+		check_logged(fd);
 		session_name(fd, req);
 
 		break;