ソースを参照

vccd: relay messages without invisible users. after that i'll release 2.0.0

intirain 4 年 前
親
コミット
32028418ee
7 ファイル変更、181 行追加、22 行削除
  1. 4 4
      Makefile
  2. 21 1
      include/vcc/vcc.h
  3. 4 0
      include/vcc/vccd.h
  4. 2 2
      include/vcc/version.h
  5. 36 6
      main.c
  6. 111 0
      relay.c
  7. 3 9
      vcc.c

+ 4 - 4
Makefile

@@ -1,14 +1,14 @@
 VCCD_MAJOR 		= 1
-VCCD_MINOR 		= 9
-VCCD_PATCH 		= 4
+VCCD_MINOR 		= 10
+VCCD_PATCH 		= 0
 VCCD_STR 		= -rc 
 
 VCCD_COMPILE_TIME 	= $(shell date)
 VCCD_COMPILE_BY 	= $(shell whoami)
 VCCD_SEMVER 		= vccd $(VCCD_MAJOR).$(VCCD_MINOR).$(VCCD_PATCH)$(VCCD_STR)
 
-srcs 			= main.c vcc.c klist.c users.c monitor.c session.c 
-objs 			= main.o vcc.o klist.o users.o monitor.o session.o 
+srcs 			= main.c vcc.c klist.c users.c monitor.c session.c relay.c 
+objs 			= main.o vcc.o klist.o users.o monitor.o session.o relay.o 
 target 			= vccd
 CFLAGS 			= -std=gnu11 -Wall -Wextra -Werror -Iinclude -g 
 MAKEFLAGS 		= --no-print-directory

+ 21 - 1
include/vcc/vcc.h

@@ -24,7 +24,7 @@
 
 /* magic number in vcc_request to check that it's really a vcc package */
 #define VCC_MAGIC 	0x01328e22
-#define VCC_MAGIC_CR 	0x01328e36
+#define VCC_MAGIC_RL 	0x01328e36
 
 /* the port that vccd is listening on */
 #define VCC_PORT 	46
@@ -58,6 +58,13 @@
 /* client -> server. increase one's score */
 #define REQ_SYS_SCRINC 	9
 
+/* client -> server. send new message. */
+#define REQ_REL_MSG 	10
+
+/* server -> client. new message */
+#define REQ_REL_NEW 	11
+#define REQ_MAX 	12
+
 #define REQ_SIZE 	512
 #define USRNAME_SIZE 	32
 
@@ -80,6 +87,19 @@ struct vcc_request {
 	char 		msg[MSG_SIZE];
 };
 
+
+struct vcc_relay_header {
+	int 		magic;
+	int 		reqtype;
+	int 		size;
+
+	int 		uid;
+	int 		session;
+
+	char 		usrname[USRNAME_SIZE];
+	char 		visible[USRNAME_SIZE];
+};
+
 #define to_boolean(c) 	(!!(c))
 #define likely(c) 	__builtin_expect(to_boolean(c), 1)
 #define unlikely(c) 	__builtin_expect(to_boolean(c), 0)

+ 4 - 0
include/vcc/vccd.h

@@ -57,6 +57,10 @@ int join_session(int sid, int fd);
 int get_user_info(int fd, struct vcc_request *);
 int incr_score(int fd, struct vcc_request *);
 
+int relay_new_msg(int fd, void *);
+int relay_send_to(const char *, int expect, struct vcc_relay_header *);
+int read_until(int fd, void *, int);
+
 struct klist_node *find_connection(int fd);
 
 #define LJEST_LEVEL 	16

+ 2 - 2
include/vcc/version.h

@@ -1,2 +1,2 @@
-#define VCCD_VERSION "vccd 1.9.4 Sun May 15 12:39:17 PM CST 2022 root"
-#define VCCD_SEMVER "vccd 1.9.4-rc "
+#define VCCD_VERSION "vccd 1.10.0 Sat May 21 09:52:25 PM CST 2022 root"
+#define VCCD_SEMVER "vccd 1.10.0-rc "

+ 36 - 6
main.c

@@ -168,12 +168,34 @@ static int setup_signals(void) {
 }
 
 
+/* a bad programme can joke us by a wrong size member in the struct 
+ * vcc_relay_header. he makes it large, but never writes after. then 
+ * we'll wait in the loop. 
+ *
+ * we can use a 'sending' member in the struct connection and that was 
+ * what we tried to do. but againly, do that later. :-( */
+
+int read_until(int fd, void *p, int until) {
+	int did_read = 0;
+	int n;
+
+	while ((did_read += n = read(fd, p, until)) < until) {
+		if (!n) 
+			return 0;
+
+		p += n;
+	}
+	
+	return until;
+}
+
+
 int main(int argc, const char **argv) {
-	__uint32_t 	size;
-	int 		usr_fd, nfds_total, nfds_ready, i;
-	void 		*buf;
+	__uint32_t 		size;
+	int 			usr_fd, nfds_total, nfds_ready, i;
+	void 			*buf;
 
-	struct sockaddr_in usr_addr;
+	struct sockaddr_in 	usr_addr;
 
 	(void) argv;
 
@@ -234,7 +256,7 @@ int main(int argc, const char **argv) {
 				continue;
 
 			if (fds[i].revents & POLLIN) {
-				size = read(fds[i].fd, buf, REQ_SIZE);
+				size = read_until(fds[i].fd, buf, sizeof(struct vcc_relay_header));
 				
 				if (!size) {
 					vcc_close_connection(fds[i].fd);
@@ -245,7 +267,15 @@ int main(int argc, const char **argv) {
 					continue;
 				}
 
-				vcc_new_msg(fds[i].fd, buf);
+				if (ntohl(((struct vcc_relay_header *) buf)->magic) == VCC_MAGIC) {
+					read_until(fds[i].fd, buf + sizeof(struct vcc_relay_header), 
+							REQ_SIZE - sizeof(struct vcc_relay_header));
+
+					vcc_new_msg(fds[i].fd, buf);
+				}
+
+				else 
+					relay_new_msg(fds[i].fd, buf);
 
 				fds[i].events = POLLOUT;
 			}

+ 111 - 0
relay.c

@@ -0,0 +1,111 @@
+/* vccd/relay.c 
+ * 
+ * This file is part of vccd. 
+ * 
+ * vccd is free software: you can redistribute it and/or modify 
+ * it under the terms of the GNU General Public License as published by 
+ * the Free Software Foundation, either version 3 of the License, or 
+ * (at your option) any later version. 
+ * 
+ * vccd is distributed in the hope that it will be useful, 
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of 
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
+ * GNU General Public License for more details. 
+ * 
+ * You should have received a copy of the GNU General Public License 
+ * along with vccd. If not, see <https://www.gnu.org/licenses/>
+ */ 
+
+
+
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <string.h>
+
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <poll.h>
+
+#include <vcc/vcc.h>
+#include <vcc/vccd.h>
+#include <vcc/version.h>
+#include <klist.h>
+
+
+extern struct klist_node connections;
+
+int relay_new_msg(int fd, void *t) {
+	struct vcc_relay_header 	*req;
+	char 				*msg;
+	int 				len;
+
+	req = t;
+	len = ntohl(req->size) - sizeof(struct vcc_relay_header);
+
+	if (unlikely(!(msg = amalloc(len)))) {
+		fprintf(stderr, "*** malloc() failed\n");
+
+		return 1;
+	}
+
+	read_until(fd, msg, len);
+	relay_send_to(msg, 0, req);
+
+	return 0;
+}
+
+
+int relay_send_to(const char *msg, int expect_fd, struct vcc_relay_header *hdr) {
+	struct vcc_relay_header *req;
+	struct klist_node 	*n;
+	int 			i, len;
+
+	/* make sure about that the write() bottom will execute after these */
+	vcc_write_all();
+
+	i = 0;
+	len = strlen(msg);
+
+	for (n = connections.next; n != &connections; n = n->next) {
+		if (n->value == expect_fd) 
+			continue;
+
+		if (unlikely(!(req = amalloc(sizeof(struct vcc_relay_header))))) {
+			vcc_log("*** malloc() failed\n");
+
+			return 1;
+		}
+
+		memset(req, 0, sizeof(struct vcc_relay_header));
+
+		req->magic = htonl(VCC_MAGIC_RL);
+		req->reqtype = htonl(REQ_REL_NEW);
+		req->uid = 0;
+		req->session = 0;
+		req->size = htonl(sizeof(struct vcc_relay_header) + len);
+
+		strncpy(req->usrname, hdr->usrname, USRNAME_SIZE);
+
+		/* direct writes here 
+		 * vcc_send_req(n, req); 
+		 * */
+
+		write(n->value, req, sizeof(struct vcc_relay_header));
+		write(n->value, msg, len);
+
+		i++;
+	}
+
+	vcc_log("Relay message sent to %d users. \n", i);
+
+	return 0;
+}
+
+

+ 3 - 9
vcc.c

@@ -266,20 +266,14 @@ int send_users(int fd) {
 int vcc_new_msg(int fd, struct vcc_request *req) {
 	struct klist_node 	*n = find_connection(fd);
 	char 			*msg;
-	int 			sid, magic;
+	int 			sid;
 
 	if (unlikely(!n)) 
 		return 1;
 
 	msg = req->msg;
-	magic = ntohl(req->magic);
-
-	if (unlikely(magic != VCC_MAGIC && magic != VCC_MAGIC_CR)) {
-		vcc_log("--- bad magic number from %d\n", fd);
-
-		return 0;
-	}
 
+	/* it's not so beautiful... but arguments of each function are not common. */
 	switch (htonl(req->reqtype)) {
 	case REQ_MSG_NEW:
 		/* POLLOUT will be set, just because they sent us a message */
@@ -340,7 +334,7 @@ int vcc_new_msg(int fd, struct vcc_request *req) {
 
 	default:
 		vcc_log("--- bad request type from %d\n", fd);
-
+	
 		break;
 	}