Parcourir la source

vccd: using N-string-functions instead

intirain il y a 4 ans
Parent
commit
879352912c
6 fichiers modifiés avec 31 ajouts et 21 suppressions
  1. 4 1
      include/vcc/vcc.h
  2. 2 2
      include/vcc/vccd.h
  3. 1 1
      include/vcc/version.h
  4. 6 5
      session.c
  5. 6 6
      users.c
  6. 12 6
      vcc.c

+ 4 - 1
include/vcc/vcc.h

@@ -63,7 +63,9 @@
 
 /* we'll not warn you anyway.  */
 #define PASSWD_SIZE 	64
-#define MSG_SIZE 	(REQ_SIZE - 4 * sizeof(int) - USRNAME_SIZE)
+#define MSG_SIZE 	(REQ_SIZE - 5 * sizeof(int) - USRNAME_SIZE)
+
+#define FLAG_ENCRYPTED 	1
 
 struct vcc_request {
 	int 		magic;
@@ -72,6 +74,7 @@ struct vcc_request {
 	/* to make server easy and fast */
 	int 		uid;
 	int 		session;
+	int 		flags;
 
 	char 		usrname[USRNAME_SIZE];
 	char 		msg[MSG_SIZE];

+ 2 - 2
include/vcc/vccd.h

@@ -43,13 +43,13 @@ int vcc_close_connection(int fd);
 int vcc_write_packages(int fd);
 int vcc_new_msg(int fd, struct vcc_request *buf);
 int vcc_send_req(struct klist_node *, void *);
-int vcc_message_broadcast(char *msg, char *usr, int except_fd);
+int vcc_message_broadcast(char *msg, char *usr, int except_fd, int flags);
 
 int do_user_login(int fd, struct vcc_request *buf);
 int do_user_logout(int fd);
 
 int create_session(int fd, char *name);
-int session_broadcast(int sid, char *msg, char *usr, int except_fd);
+int session_broadcast(int sid, char *msg, char *usr, int except_fd, int flags);
 int send_sessions(int fd);
 int join_session(int sid, int fd);
 

+ 1 - 1
include/vcc/version.h

@@ -1,2 +1,2 @@
-#define VCCD_VERSION "vccd 1.7.1 Tue Apr  5 17:09:06 CST 2022 root"
+#define VCCD_VERSION "vccd 1.7.1 Fri Apr  8 19:40:52 CST 2022 root"
 #define VCCD_SEMVER "vccd 1.7.1-rc "

+ 6 - 5
session.c

@@ -65,7 +65,7 @@ int create_session(int fd, char *name) {
 
 	n->value = next_sid++;
 
-	if (unlikely(!(n->usrname = strdup(name)))) {
+	if (unlikely(!(n->usrname = strndup(name, USRNAME_SIZE)))) {
 		vcc_log("*** malloc() failed\n");
 
 		return 1;
@@ -146,7 +146,7 @@ int join_session(int sid, int fd) {
 	klist_add(session->session, n);
 
 	sprintf(t, join_msg, n->connection->usrname, session->usrname, ++session->session->nusers);
-	session_broadcast(sid, t, "system", 0);
+	session_broadcast(sid, t, "system", 0, 0);
 
 	vcc_log("%d joined session %d. \n", fd, sid);
 
@@ -156,7 +156,7 @@ int join_session(int sid, int fd) {
 }
 
 
-int session_broadcast(int sid, char *msg, char *usrname, int except) {
+int session_broadcast(int sid, char *msg, char *usrname, int except, int flags) {
 	struct klist_node 	*session, *n;
 	struct vcc_request 	req, *out;
 	int 			i;
@@ -170,9 +170,10 @@ int session_broadcast(int sid, char *msg, char *usrname, int except) {
 	req.reqtype = htonl(REQ_MSG_NEW);
 	req.uid = 0;
 	req.session = htonl(sid);
+	req.flags = flags;
 
 	memcpy(req.msg, msg, MSG_SIZE);
-	strcpy(req.usrname, usrname);
+	strncpy(req.usrname, usrname, USRNAME_SIZE);
 
 	i = 0;
 	
@@ -219,7 +220,7 @@ int send_sessions(int fd) {
 	req->reqtype = htonl(REQ_CTL_SESS);
 
 	for (n = sessions.next, i = 0; n != &sessions; n = n->next, i++) {
-		strcpy(req->msg + i * USRNAME_SIZE, n->usrname);
+		strncpy(req->msg + i * USRNAME_SIZE, n->usrname, USRNAME_SIZE);
 
 		if (unlikely(i >= (int) MSG_SIZE / USRNAME_SIZE)) {
 			vcc_log("--- too many sessions. \n");

+ 6 - 6
users.c

@@ -90,7 +90,7 @@ static struct user *find_user_name(char *usrname) {
 	for (n = logged_users.next; n != &logged_users; n = n->next) {
 		u = n->data;
 
-		if (!strcmp(u->username, usrname)) 
+		if (!strncmp(u->username, usrname, USRNAME_SIZE)) 
 			return u;
 	}
 
@@ -188,7 +188,7 @@ int do_user_login(int fd, struct vcc_request *req) {
 	out->magic = htonl(VCC_MAGIC);
 	out->reqtype = htonl(REQ_CTL_LOGIN);
 
-	if (get_user(req->usrname, usr) || strcmp(req->msg, usr->passwd)) {
+	if (get_user(req->usrname, usr) || strncmp(req->msg, usr->passwd, PASSWD_SIZE)) {
 		vcc_log("%s:%s login failed. \n", req->usrname, req->msg);
 
 		out->uid = 0;
@@ -199,7 +199,7 @@ int do_user_login(int fd, struct vcc_request *req) {
 		return 0;
 	}
 
-	if (!strcmp(req->usrname, "system")) {
+	if (!strncmp(req->usrname, "system", USRNAME_SIZE)) {
 		if (system_fd) {
 			vcc_log("system already logged-in. \n");
 
@@ -241,7 +241,7 @@ int do_user_login(int fd, struct vcc_request *req) {
 	/* nusers already inc'ed when it connects */
 
 	sprintf(t, join_msg, req->usrname, nusers);
-	vcc_message_broadcast(t, "system", 0);
+	vcc_message_broadcast(t, "system", 0, 0);
 
 	afree(t, 128);
 
@@ -293,13 +293,13 @@ int do_user_logout(int fd) {
 
 	sync_user(usr->username, usr);
 
-	if (!strcmp(usr->username, "system")) 
+	if (!strncmp(usr->username, "system", USRNAME_SIZE)) 
 		system_fd = 0;
 
 	vcc_log("Bye, %s. \n", usr->username);
 
 	sprintf(t, leave_msg, usr->username, nusers);
-	vcc_message_broadcast(t, "system", fd);
+	vcc_message_broadcast(t, "system", fd, 0);
 
 	klist_del(&logged_users, n);
 

+ 12 - 6
vcc.c

@@ -158,11 +158,14 @@ int vcc_send_req(struct klist_node *node, void *p) {
 
 /* send to each user include who sends this message */
 
-int vcc_message_broadcast(char *msg, char *usrname, int except_fd) {
+int vcc_message_broadcast(char *msg, char *usrname, int except_fd, int flags) {
 	struct vcc_request *req;
 	struct klist_node *n;
 	int i;
 
+	if (ntohl(flags) & FLAG_ENCRYPTED) 
+		vcc_log("Encrypted message. \n");
+
 	for (n = connections.next, i = 0; n != &connections; n = n->next) {
 		if (n->value == except_fd) 
 			continue;
@@ -178,8 +181,11 @@ int vcc_message_broadcast(char *msg, char *usrname, int except_fd) {
 		req->uid = 0;
 		req->session = 0;
 
+		/* no htonl(), because we didn't ntohl() */
+		req->flags = flags;
+
 		memcpy(req->msg, msg, MSG_SIZE);
-		strcpy(req->usrname, usrname);
+		strncpy(req->usrname, usrname, USRNAME_SIZE);
 
 		vcc_send_req(n, req);
 		i++;
@@ -200,7 +206,7 @@ int vcc_update_usrname(int fd, const char *usrname) {
 	if (likely(n->usrname)) 
 		return 0;
 
-	n->usrname = strdup(usrname);
+	n->usrname = strndup(usrname, USRNAME_SIZE);
 	vcc_log("user name %s updated for %d\n", usrname, fd);
 
 	return 0;
@@ -224,7 +230,7 @@ int send_users(int fd) {
 	req->reqtype = htonl(REQ_CTL_USRS);
 
 	for (n = connections.next, i = 0; n != &connections; n = n->next, i++) {
-		strcpy(req->msg + i * USRNAME_SIZE, n->usrname ? n->usrname : "unnamed");
+		strncpy(req->msg + i * USRNAME_SIZE, n->usrname ? n->usrname : "unnamed", USRNAME_SIZE);
 
 		if (unlikely(i >= (int) MSG_SIZE / USRNAME_SIZE)) {
 			vcc_log("--- too many users\n");
@@ -274,10 +280,10 @@ int vcc_new_msg(int fd, struct vcc_request *req) {
 		vcc_log("(%d) New message\n", sid);
 
 		if (!sid) 
-			vcc_message_broadcast(msg, req->usrname, fd);
+			vcc_message_broadcast(msg, req->usrname, fd, req->flags);
 
 		else 
-			session_broadcast(sid, msg, req->usrname, fd);
+			session_broadcast(sid, msg, req->usrname, fd, req->flags);
 
 		break;