vcc.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /* vccd/vcc.c
  2. *
  3. * This file is part of vccd.
  4. *
  5. * vccd is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * vccd is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with vccd. If not, see <https://www.gnu.org/licenses/>
  17. */
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <unistd.h>
  21. #include <fcntl.h>
  22. #include <errno.h>
  23. #include <string.h>
  24. #include <sys/stat.h>
  25. #include <sys/types.h>
  26. #include <sys/socket.h>
  27. #include <netinet/in.h>
  28. #include <arpa/inet.h>
  29. #include <poll.h>
  30. #include <vcc/vcc.h>
  31. #include <vcc/vccd.h>
  32. #include <vcc/version.h>
  33. #include <klist.h>
  34. struct klist_node connections = KLIST_NODE_INIT(&connections);
  35. extern int nusers;
  36. int vcc_new_connection(int fd, struct in_addr *addr) {
  37. struct klist_node *n;
  38. vcc_log("New connection from %s\n", inet_ntoa(*addr));
  39. vcc_info("ICONN %s %d\n", inet_ntoa(*addr), fd);
  40. if (unlikely(!(n = amalloc(sizeof(struct klist_node))))) {
  41. vcc_log("*** malloc() failed\n");
  42. return 1;
  43. }
  44. klist_init(n);
  45. n->data = addr;
  46. n->value = fd;
  47. n->stat = STAT_UNLOGGED;
  48. if (unlikely(!(n->sending = amalloc(sizeof(struct klist_node))))) {
  49. vcc_log("*** malloc() failed\n");
  50. return 1;
  51. }
  52. klist_init(n->sending);
  53. klist_add(&connections, n);
  54. return 0;
  55. }
  56. int vcc_close_connection(int fd) {
  57. struct klist_node *n, *t;
  58. if (unlikely(!(n = find_connection(fd))))
  59. return 1;
  60. do_user_logout(fd);
  61. klist_del(&connections, n);
  62. for (t = n->sending->next; t != n->sending; t = t->next)
  63. /* it's dangerous here */
  64. if (t->data)
  65. afree(t->data, REQ_SIZE);
  66. afree(n->sending, sizeof(struct klist_node));
  67. vcc_log("Client closed. \n");
  68. vcc_info("ILEFT %d\n", fd);
  69. return 0;
  70. }
  71. struct klist_node *find_connection(int fd) {
  72. struct klist_node *n;
  73. for (n = connections.next; n != &connections
  74. && n->value != fd; n = n->next)
  75. ;
  76. if (unlikely(n == &connections)) {
  77. vcc_log("--- connection not found: %d\n", fd);
  78. vcc_info("WCONNOTFND %d\n", fd);
  79. return NULL;
  80. }
  81. return n;
  82. }
  83. int write_packages(struct klist_node *node) {
  84. struct klist_node *n;
  85. if (unlikely(!node->sending))
  86. return 0;
  87. if (unlikely(klist_empty(node->sending)))
  88. return 0;
  89. for (n = node->sending->next; n != node->sending; n = n->next) {
  90. write(node->value, n->data, REQ_SIZE);
  91. klist_del(node->sending, n);
  92. afree(n->data, REQ_SIZE);
  93. afree(n, sizeof(struct klist_node));
  94. }
  95. return 0;
  96. }
  97. int vcc_write_packages(int fd) {
  98. return write_packages(find_connection(fd));
  99. }
  100. int vcc_write_all(void) {
  101. struct klist_node *n;
  102. for (n = connections.next; n != &connections; n = n->next)
  103. vcc_write_packages(n->value);
  104. return 0;
  105. }
  106. int vcc_send_req(struct klist_node *node, void *p) {
  107. struct klist_node *n;
  108. if (unlikely(!(n = amalloc(sizeof(struct klist_node))))) {
  109. vcc_log("*** malloc() failed\n");
  110. return 1;
  111. }
  112. klist_init(n);
  113. n->data = p;
  114. klist_add(node->sending, n);
  115. return 0;
  116. }
  117. /* send to each user include who sends this message, but excluding 'except_fd' */
  118. int vcc_message_broadcast(char *msg, char *usrname, int except_fd, int flags) {
  119. struct vcc_request *req;
  120. struct klist_node *n;
  121. int i;
  122. if (ntohl(flags) & FLAG_ENCRYPTED)
  123. vcc_log("Encrypted message. \n");
  124. for (n = connections.next, i = 0; n != &connections; n = n->next) {
  125. if (n->value == except_fd)
  126. continue;
  127. if (n->stat == STAT_UNLOGGED)
  128. continue;
  129. if (unlikely(!(req = amalloc(sizeof(struct vcc_request))))) {
  130. vcc_log("*** malloc() failed\n");
  131. continue;
  132. }
  133. memset(req, 0, sizeof(struct vcc_request));
  134. req->magic = htonl(VCC_MAGIC);
  135. req->reqtype = htonl(REQ_MSG_NEW);
  136. req->uid = 0;
  137. req->session = 0;
  138. /* no htonl(), because we didn't ntohl() */
  139. req->flags = flags;
  140. strncpy(req->msg, msg, MSG_SIZE);
  141. strncpy(req->usrname, usrname, USRNAME_SIZE);
  142. vcc_send_req(n, req);
  143. i++;
  144. }
  145. vcc_write_all();
  146. vcc_log("Message sent to %d users. \n", i);
  147. return 0;
  148. }
  149. int vcc_update_usrname(int fd, const char *usrname) {
  150. struct klist_node *n = find_connection(fd);
  151. if (unlikely(!n))
  152. return -1;
  153. if (likely(n->usrname))
  154. return 0;
  155. n->usrname = strndup(usrname, USRNAME_SIZE);
  156. vcc_log("user name %s updated for %d\n", usrname, fd);
  157. vcc_info("IUSR %s %d\n", usrname, fd);
  158. return 0;
  159. }
  160. int send_users(int fd) {
  161. struct vcc_relay_header req;
  162. struct klist_node *n;
  163. int i, size;
  164. char *buf;
  165. size = USRNAME_SIZE * nusers;
  166. if (unlikely(!(buf = amalloc(size)))) {
  167. vcc_log("*** malloc() failed\n");
  168. vcc_log("*** %d users, %d bytes. ", nusers, size);
  169. return 1;
  170. }
  171. vcc_log("%d users online. \n", nusers);
  172. req.magic = htonl(VCC_MAGIC_RL);
  173. req.reqtype = htonl(REQ_CTL_USRS);
  174. req.session = 0;
  175. for (n = connections.next, i = 0; n != &connections; n = n->next, i++)
  176. strncpy(buf + i * USRNAME_SIZE, n->usrname ? n->usrname : "unnamed", USRNAME_SIZE);
  177. req.uid = htonl(i);
  178. relay_send_packet(fd, buf, &req, size);
  179. vcc_log("%d user names sent to %d\n", i, fd);
  180. afree(buf, size);
  181. return 0;
  182. }
  183. int vcc_new_msg(int fd, struct vcc_request *req) {
  184. struct klist_node *n = find_connection(fd);
  185. char *msg;
  186. int sid, magic;
  187. if (unlikely(!n))
  188. return 1;
  189. msg = req->msg;
  190. /* it's not so beautiful... but arguments of each function are not common.
  191. *
  192. * so we CAN'T use requests[htonl(req->reqtype)] cetera instead.
  193. * f**k.
  194. * */
  195. magic = ntohl(req->magic);
  196. if (magic != VCC_MAGIC && magic != VCC_MAGIC_RL) {
  197. vcc_log("--- bad magic from %d. \n", fd);
  198. vcc_info("WBADMAGIC %d\n", fd);
  199. return 1;
  200. }
  201. switch (htonl(req->reqtype)) {
  202. case REQ_MSG_NEW:
  203. /* POLLOUT will be set, just because they sent us a message */
  204. return 0;
  205. case REQ_MSG_SEND:
  206. check_logged(fd);
  207. if (((unsigned char *) msg)[0] == 0xffu || !*msg)
  208. return 0;
  209. sid = ntohl(req->session);
  210. vcc_log("(%d) New message\n", sid);
  211. if (!sid)
  212. vcc_message_broadcast(msg, req->usrname, fd, req->flags);
  213. else
  214. session_broadcast(sid, msg, req->usrname, fd, req->flags);
  215. break;
  216. case REQ_CTL_USRS:
  217. check_logged(fd);
  218. send_users(fd);
  219. break;
  220. case REQ_CTL_LOGIN:
  221. if (!do_user_login(fd, req))
  222. vcc_update_usrname(fd, req->usrname);
  223. break;
  224. case REQ_CTL_IALOG:
  225. do_ia_login(fd, req);
  226. vcc_update_usrname(fd, req->usrname);
  227. break;
  228. case REQ_CTL_NEWSE:
  229. check_logged(fd);
  230. create_session(fd, req->usrname);
  231. break;
  232. case REQ_CTL_SESS:
  233. check_logged(fd);
  234. send_sessions(fd);
  235. break;
  236. case REQ_CTL_JOINS:
  237. check_logged(fd);
  238. join_session(ntohl(req->session), fd);
  239. break;
  240. case REQ_CTL_UINFO:
  241. check_logged(fd);
  242. get_user_info(fd, req);
  243. break;
  244. case REQ_SYS_SCRINC:
  245. check_logged(fd);
  246. incr_score(fd, req);
  247. break;
  248. case REQ_SYS_INFO:
  249. check_logged(fd);
  250. monitor_info(fd, req);
  251. break;
  252. case REQ_CTL_SENAME:
  253. check_logged(fd);
  254. session_name(fd, req);
  255. break;
  256. case REQ_CTL_QUITS:
  257. check_logged(fd);
  258. quit_session(ntohl(req->session), fd);
  259. break;
  260. default:
  261. vcc_log("--- bad request type from %d\n", fd);
  262. vcc_info("WBADREQ %d\n", fd);
  263. break;
  264. }
  265. vcc_write_packages(fd);
  266. return 0;
  267. }