|
|
@@ -37,7 +37,7 @@
|
|
|
|
|
|
|
|
|
char *vccd_dir;
|
|
|
-int nusers = 0;
|
|
|
+int nusers = 0, system_fd = 0;
|
|
|
struct klist_node logged_users = KLIST_NODE_INIT(&logged_users);
|
|
|
|
|
|
|
|
|
@@ -69,6 +69,37 @@ int users_init(void) {
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+static struct klist_node *find_user(int fd) {
|
|
|
+ struct klist_node *n;
|
|
|
+
|
|
|
+ for (n = logged_users.next; n != &logged_users; n = n->next)
|
|
|
+ if (n->value == fd)
|
|
|
+ return n;
|
|
|
+
|
|
|
+ vcc_log("--- user not found: %d\n", fd);
|
|
|
+
|
|
|
+ return NULL;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+static struct user *find_user_name(char *usrname) {
|
|
|
+ struct klist_node *n;
|
|
|
+ struct user *u;
|
|
|
+
|
|
|
+ for (n = logged_users.next; n != &logged_users; n = n->next) {
|
|
|
+ u = n->data;
|
|
|
+
|
|
|
+ if (!strcmp(u->username, usrname))
|
|
|
+ return u;
|
|
|
+ }
|
|
|
+
|
|
|
+ vcc_log("--- find_user_name: not found %s. \n", usrname);
|
|
|
+
|
|
|
+ return NULL;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
int open_user_file(char *usr) {
|
|
|
char *fname;
|
|
|
int fdt;
|
|
|
@@ -81,7 +112,7 @@ int open_user_file(char *usr) {
|
|
|
|
|
|
sprintf(fname, "%s/users/%s", vccd_dir, usr);
|
|
|
|
|
|
- if (unlikely((fdt = open(fname, O_RDONLY)) < 0)) {
|
|
|
+ if (unlikely((fdt = open(fname, O_RDWR)) < 0)) {
|
|
|
perror("open()");
|
|
|
|
|
|
return -1;
|
|
|
@@ -94,7 +125,14 @@ int open_user_file(char *usr) {
|
|
|
|
|
|
|
|
|
int get_user(char *usr, struct user *buf) {
|
|
|
- int fdt;
|
|
|
+ int fdt;
|
|
|
+ struct user *u;
|
|
|
+
|
|
|
+ if ((u = find_user_name(usr))) {
|
|
|
+ memcpy(buf, u, sizeof(struct user));
|
|
|
+
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
|
|
|
if (unlikely(((fdt = open_user_file(usr)) == -1))) {
|
|
|
vcc_log("*** user not found. \n");
|
|
|
@@ -108,6 +146,25 @@ int get_user(char *usr, struct user *buf) {
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
+int sync_user(char *usr, struct user *buf) {
|
|
|
+ struct user *u;
|
|
|
+ int fdt;
|
|
|
+
|
|
|
+ if ((u = find_user_name(usr)))
|
|
|
+ memcpy(u, buf, sizeof(struct user));
|
|
|
+
|
|
|
+ if (unlikely((fdt = open_user_file(usr)) == -1)) {
|
|
|
+ vcc_log("*** user not found. \n");
|
|
|
+
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ write(fdt, buf, sizeof(struct user));
|
|
|
+ close(fdt);
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
|
|
|
int do_user_login(int fd, struct vcc_request *req) {
|
|
|
struct vcc_request *out;
|
|
|
@@ -142,6 +199,21 @@ int do_user_login(int fd, struct vcc_request *req) {
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
+ if (!strcmp(req->usrname, "system")) {
|
|
|
+ if (system_fd) {
|
|
|
+ vcc_log("system already logged-in. \n");
|
|
|
+
|
|
|
+ out->uid = 0;
|
|
|
+ vcc_send_req(find_connection(fd), out);
|
|
|
+
|
|
|
+ afree(usr, sizeof(struct user));
|
|
|
+
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ system_fd = fd;
|
|
|
+ }
|
|
|
+
|
|
|
vcc_log("%s logged in. \n", req->usrname);
|
|
|
|
|
|
if (unlikely(!(t = amalloc(128)))) {
|
|
|
@@ -177,19 +249,6 @@ int do_user_login(int fd, struct vcc_request *req) {
|
|
|
}
|
|
|
|
|
|
|
|
|
-static struct klist_node *find_user(int fd) {
|
|
|
- struct klist_node *n;
|
|
|
-
|
|
|
- for (n = logged_users.next; n != &logged_users; n = n->next)
|
|
|
- if (n->value == fd)
|
|
|
- return n;
|
|
|
-
|
|
|
- vcc_log("--- user not found: %d\n", fd);
|
|
|
-
|
|
|
- return NULL;
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
int get_user_info(int fd, struct vcc_request *req) {
|
|
|
struct vcc_request *out;
|
|
|
struct user *u;
|
|
|
@@ -204,51 +263,39 @@ int get_user_info(int fd, struct vcc_request *req) {
|
|
|
out->reqtype = htonl(REQ_CTL_UINFO);
|
|
|
u = (struct user *) out->msg;
|
|
|
|
|
|
- if (get_user(req->msg, u) == -1)
|
|
|
- out->uid = htonl(-1);
|
|
|
-
|
|
|
- else {
|
|
|
- out->uid = 0;
|
|
|
-
|
|
|
- u->score = htonl(u->score);
|
|
|
- u->level = htonl(u->level);
|
|
|
- }
|
|
|
-
|
|
|
- memset(((struct user *) out->msg)->passwd, 0, PASSWD_SIZE);
|
|
|
+ out->uid = htonl(get_user(req->msg, u));
|
|
|
+ u->score = htonl(u->score);
|
|
|
+ u->level = htonl(u->level);
|
|
|
|
|
|
+ memset(u->passwd, 0, PASSWD_SIZE);
|
|
|
vcc_send_req(find_connection(fd), out);
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
-
|
|
|
int do_user_logout(int fd) {
|
|
|
struct klist_node *n;
|
|
|
struct user *usr;
|
|
|
static const char *leave_msg = "%s left, now there are %d users here. \n";
|
|
|
char *t;
|
|
|
- int fdt;
|
|
|
|
|
|
if (unlikely(!(n = find_user(fd))))
|
|
|
return 1;
|
|
|
|
|
|
usr = n->data;
|
|
|
|
|
|
- if (unlikely(((fdt = open_user_file(usr->username)) == -1))) {
|
|
|
- vcc_log("*** user not found: %s. \n", usr->username);
|
|
|
-
|
|
|
- return 1;
|
|
|
- }
|
|
|
-
|
|
|
if (unlikely(!(t = amalloc(128)))) {
|
|
|
vcc_log("*** malloc() failed\n");
|
|
|
|
|
|
return 1;
|
|
|
}
|
|
|
|
|
|
- write(fdt, usr, sizeof(struct user));
|
|
|
+ sync_user(usr->username, usr);
|
|
|
+
|
|
|
+ if (!strcmp(usr->username, "system"))
|
|
|
+ system_fd = 0;
|
|
|
+
|
|
|
vcc_log("Bye, %s. \n", usr->username);
|
|
|
|
|
|
sprintf(t, leave_msg, usr->username, nusers);
|
|
|
@@ -259,7 +306,52 @@ int do_user_logout(int fd) {
|
|
|
afree(usr, sizeof(struct user));
|
|
|
afree(n, sizeof(struct klist_node));
|
|
|
|
|
|
- close(fdt);
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+int incr_score(int fd, struct vcc_request *req) {
|
|
|
+ struct vcc_request *out;
|
|
|
+ struct user u;
|
|
|
+
|
|
|
+ if (unlikely(!(out = amalloc(sizeof(struct vcc_request ))))) {
|
|
|
+ vcc_log("*** malloc() failed\n");
|
|
|
+
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ out->magic = htonl(VCC_MAGIC);
|
|
|
+ out->reqtype = htonl(REQ_SYS_SCRINC);
|
|
|
+
|
|
|
+ if (fd != system_fd) {
|
|
|
+ out->uid = htonl(-1);
|
|
|
+ vcc_send_req(find_connection(fd), out);
|
|
|
+
|
|
|
+ vcc_log("%d tried to modify the score. \n", fd);
|
|
|
+
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (unlikely(get_user(req->usrname, &u) == -1)) {
|
|
|
+ out->uid = htonl(-1);
|
|
|
+ vcc_send_req(find_connection(fd), out);
|
|
|
+
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ u.score += ntohl(req->session);
|
|
|
+
|
|
|
+ if (u.score > 65535) {
|
|
|
+ u.level -= u.score >> 16;
|
|
|
+ u.score %= 65535;
|
|
|
+ }
|
|
|
+
|
|
|
+ sync_user(req->usrname, &u);
|
|
|
+
|
|
|
+ out->uid = 0;
|
|
|
+ vcc_send_req(find_connection(fd), out);
|
|
|
+
|
|
|
+ vcc_log("%s is %d:%d scores now. \n", req->usrname, u.level, u.score);
|
|
|
|
|
|
return 0;
|
|
|
}
|