intirain 4 rokov pred
rodič
commit
728aae00f0
6 zmenil súbory, kde vykonal 91 pridanie a 43 odobranie
  1. 2 2
      Makefile
  2. 12 34
      cmd.c
  3. 4 0
      include/vcc/vcc.h
  4. 1 1
      include/vcc/version.h
  5. 18 4
      main.c
  6. 54 2
      vcc.c

+ 2 - 2
Makefile

@@ -1,6 +1,6 @@
 VCCD_MAJOR 		= 0
-VCCD_MINOR 		= 5
-VCCD_PATCH 		= 1
+VCCD_MINOR 		= 6
+VCCD_PATCH 		= 0
 VCCD_COMPILE_TIME 	= $(shell date)
 VCCD_COMPILE_BY 	= $(shell whoami)
 

+ 12 - 34
cmd.c

@@ -45,7 +45,8 @@ int do_cmd_quit(char *);
 int do_cmd_ls(char *);
 int do_cmd_ls_bh(struct vcc_request *);
 
-extern int pid;
+int read_msg(struct vcc_request *);
+
 extern char usrname[];
 
 int get_users(void);
@@ -61,8 +62,6 @@ struct cmdtable {
 	{ NULL, NULL, NULL }
 };
 
-void *bh_list[] = {do_cmd_ls_bh};
-
 
 int do_cmd(char *s) {
 	struct cmdtable *t;
@@ -77,25 +76,6 @@ int do_cmd(char *s) {
 }
 
 
-/* bottom half */
-
-int do_cmd_bh(struct vcc_request *req) {
-	int i;
-	int (* fn) (struct vcc_request *);
-
-	for (i = 0; bh_list[i]; i++) 
-		if (!(fn = bh_list[i])(req)) {
-			pretty_prompt(usrname);
-
-			return 0;
-		}
-
-	fprintf(stderr, "--- unexpected bh\n");
-
-	return 1;
-}
-
-
 int do_cmd_help(char *s) {
 	struct cmdtable *t;
 
@@ -114,28 +94,26 @@ int do_cmd_quit(char *s) {
 }
 
 
-int do_cmd_ls_bh(struct vcc_request *req) {
-	int i, n = ntohl(req->uid);
+int do_cmd_ls(char *s) {
+	struct vcc_request req;
+	int i, n;
+
+	get_users();
+
+	read_msg(&req);
+	n = ntohl(req.uid);
 
 	/* clear current line */
-	printf("\033[2K\r");
 
-	if (unlikely(ntohl(req->reqtype) != REQ_CTL_USRS)) {
+	if (unlikely(ntohl(req.reqtype) != REQ_CTL_USRS)) {
 		fprintf(stderr, "--- do_cmd_ls_bh: bad package. \n");
 
 		return 1;
 	}
 
 	for (i = 0; i < n; i++) 
-		printf("%s\n", req->msg + i * USRNAME_SIZE);
-
-	return 0;
-}
-
+		printf("%s\n", req.msg + i * USRNAME_SIZE);
 
-int do_cmd_ls(char *s) {
-	get_users();
-	
 	return 0;
 }
 

+ 4 - 0
include/vcc/vcc.h

@@ -37,6 +37,10 @@
 /* client -> server. get users logged-in */
 #define REQ_CTL_USRS 	3
 
+/* client -> server. login with password and username.
+ * server -> client. login ack  */
+#define REQ_CTL_LOGIN 	4
+
 #define REQ_SIZE 	4096
 #define USRNAME_SIZE 	32
 #define MSG_SIZE 	(REQ_SIZE - 3 * sizeof(int) - USRNAME_SIZE)

+ 1 - 1
include/vcc/version.h

@@ -1 +1 @@
-#define VCCD_VERSION "vccd 0.5.1 Fri Mar 11 20:02:49 CST 2022 root"
+#define VCCD_VERSION "vccd 0.6.0 Sat Mar 12 15:40:08 CST 2022 root"

+ 18 - 4
main.c

@@ -41,9 +41,10 @@
 
 int send_msg(const char *msg, const char *usrname);
 int read_msg(struct vcc_request *buf);
+int vcc_login(char *, char *);
 
 int do_cmd(char *s);
-int do_cmd_bh(struct vcc_request *);
+int do_bh(struct vcc_request *);
 
 
 int fd;
@@ -131,6 +132,20 @@ int setup_signals(void) {
 }
 
 
+static int login(void) {
+	char *passwd;
+
+	fprintf(stderr, "login as: ");
+	scanf("%s", usrname);
+
+	passwd = getpass("password: ");
+
+	vcc_login(usrname, passwd);
+
+	return 0;
+}
+
+
 int main(int argc, const char **argv) {
 	struct pollfd 		fds[2];
 	struct vcc_request 	req;
@@ -147,8 +162,7 @@ int main(int argc, const char **argv) {
 
 	printf("Connected to server. \n");
 
-	fprintf(stderr, "login as: ");
-	scanf("%s", usrname);
+	login();
 
 	fds[0].fd = 0;
 	fds[0].events = POLLIN;
@@ -207,7 +221,7 @@ int main(int argc, const char **argv) {
 			}
 
 			if (ntohl(req.reqtype) != REQ_MSG_NEW) {
-				do_cmd_bh(&req);
+				do_bh(&req);
 
 				continue;
 			}

+ 54 - 2
vcc.c

@@ -38,9 +38,11 @@
 #include <vcc/version.h>
 
 
-
 extern int fd;
 
+int vcc_login_bh(struct vcc_request *);
+
+
 int send_msg(const char *msg, const char *usrname) {
 	struct vcc_request req;
 
@@ -70,7 +72,57 @@ int get_users(void) {
 
 	write(fd, &req, REQ_SIZE);
 
-	/* read in another task */
+	return 0;
+}
+
+
+int do_bh(struct vcc_request *req) {
+	switch (ntohl(req->reqtype)) {
+	case REQ_CTL_LOGIN:
+		vcc_login_bh(req);
+
+		break;
+
+	default:
+		fprintf(stderr, "*** unexpected packet. \n");
+
+		break;
+	}
+
+	return 0;
+}
+
+
+int vcc_login(char *usrname, char *passwd) {
+	struct vcc_request req;
+
+	req.magic = htonl(VCC_MAGIC);
+	req.reqtype = htonl(REQ_CTL_LOGIN);
+
+	strcpy(req.usrname, usrname);
+	strcpy(req.msg, passwd);
+
+	write(fd, &req, REQ_SIZE);
+
+	return 0;
+}
+
+
+int vcc_login_bh(struct vcc_request *req) {
+	if (ntohl(req->reqtype) != REQ_CTL_LOGIN) {
+		fprintf(stderr, "*** bad package. \n");
+
+		return 1;
+	}
+
+	if (ntohl(req->uid)) 
+		printf("login success. \n");
+
+	else {
+		printf("login failed: wrong password or user doesn't exists. \n");
+
+		exit(1);
+	}
 
 	return 0;
 }