vcc.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. /* if we kill vcc before it logs in, the nusers will dec but it
  55. * didn't login the value will be something bad. */
  56. nusers++;
  57. return 0;
  58. }
  59. int vcc_close_connection(int fd) {
  60. struct klist_node *n, *t;
  61. if (unlikely(!(n = find_connection(fd))))
  62. return 1;
  63. nusers--;
  64. do_user_logout(fd);
  65. klist_del(&connections, n);
  66. for (t = n->sending->next; t != n->sending; t = t->next)
  67. /* it's dangerous here */
  68. if (t->data)
  69. afree(t->data, REQ_SIZE);
  70. afree(n->sending, sizeof(struct klist_node));
  71. vcc_log("Client closed. \n");
  72. vcc_info("ILEFT %d\n", fd);
  73. return 0;
  74. }
  75. struct klist_node *find_connection(int fd) {
  76. struct klist_node *n;
  77. for (n = connections.next; n != &connections
  78. && n->value != fd; n = n->next)
  79. ;
  80. if (unlikely(n == &connections)) {
  81. vcc_log("--- connection not found: %d\n", fd);
  82. vcc_info("WCONNOTFND %d\n", fd);
  83. return NULL;
  84. }
  85. return n;
  86. }
  87. int write_packages(struct klist_node *node) {
  88. struct klist_node *n;
  89. if (unlikely(!node->sending))
  90. return 0;
  91. if (unlikely(klist_empty(node->sending)))
  92. return 0;
  93. for (n = node->sending->next; n != node->sending; n = n->next) {
  94. write(node->value, n->data, REQ_SIZE);
  95. klist_del(node->sending, n);
  96. afree(n->data, REQ_SIZE);
  97. afree(n, sizeof(struct klist_node));
  98. }
  99. return 0;
  100. }
  101. int vcc_write_packages(int fd) {
  102. return write_packages(find_connection(fd));
  103. }
  104. int vcc_write_all(void) {
  105. struct klist_node *n;
  106. for (n = connections.next; n != &connections; n = n->next)
  107. vcc_write_packages(n->value);
  108. return 0;
  109. }
  110. int vcc_send_req(struct klist_node *node, void *p) {
  111. struct klist_node *n;
  112. if (unlikely(!(n = amalloc(sizeof(struct klist_node))))) {
  113. vcc_log("*** malloc() failed\n");
  114. return 1;
  115. }
  116. klist_init(n);
  117. n->data = p;
  118. klist_add(node->sending, n);
  119. return 0;
  120. }
  121. /* send to each user include who sends this message, but excluding 'except_fd' */
  122. int vcc_message_broadcast(char *msg, char *usrname, int except_fd, int flags) {
  123. struct vcc_request *req;
  124. struct klist_node *n;
  125. int i;
  126. if (ntohl(flags) & FLAG_ENCRYPTED)
  127. vcc_log("Encrypted message. \n");
  128. for (n = connections.next, i = 0; n != &connections; n = n->next) {
  129. if (n->value == except_fd)
  130. continue;
  131. if (n->stat == STAT_UNLOGGED)
  132. continue;
  133. if (unlikely(!(req = amalloc(sizeof(struct vcc_request))))) {
  134. vcc_log("*** malloc() failed\n");
  135. continue;
  136. }
  137. memset(req, 0, sizeof(struct vcc_request));
  138. req->magic = htonl(VCC_MAGIC);
  139. req->reqtype = htonl(REQ_MSG_NEW);
  140. req->uid = 0;
  141. req->session = 0;
  142. /* no htonl(), because we didn't ntohl() */
  143. req->flags = flags;
  144. strncpy(req->msg, msg, MSG_SIZE);
  145. strncpy(req->usrname, usrname, USRNAME_SIZE);
  146. vcc_send_req(n, req);
  147. i++;
  148. }
  149. vcc_write_all();
  150. vcc_log("Message sent to %d users. \n", i);
  151. return 0;
  152. }
  153. int vcc_update_usrname(int fd, const char *usrname) {
  154. struct klist_node *n = find_connection(fd);
  155. if (unlikely(!n))
  156. return -1;
  157. if (likely(n->usrname))
  158. return 0;
  159. n->usrname = strndup(usrname, USRNAME_SIZE);
  160. vcc_log("user name %s updated for %d\n", usrname, fd);
  161. vcc_info("IUSR %s %d\n", usrname, fd);
  162. return 0;
  163. }
  164. int send_users(int fd) {
  165. struct vcc_relay_header req;
  166. struct klist_node *n;
  167. int i, size;
  168. char *buf;
  169. size = USRNAME_SIZE * nusers;
  170. if (unlikely(!(buf = amalloc(size)))) {
  171. vcc_log("*** malloc() failed\n");
  172. vcc_log("*** %d users, %d bytes. ", nusers, size);
  173. return 1;
  174. }
  175. vcc_log("%d users online. \n", nusers);
  176. req.magic = htonl(VCC_MAGIC_RL);
  177. req.reqtype = htonl(REQ_CTL_USRS);
  178. req.session = 0;
  179. for (n = connections.next, i = 0; n != &connections; n = n->next, i++)
  180. strncpy(buf + i * USRNAME_SIZE, n->usrname ? n->usrname : "unnamed", USRNAME_SIZE);
  181. req.uid = htonl(i);
  182. relay_send_packet(fd, buf, &req, size);
  183. vcc_log("%d user names sent to %d\n", i, fd);
  184. afree(buf, size);
  185. return 0;
  186. }
  187. int vcc_new_msg(int fd, struct vcc_request *req) {
  188. struct klist_node *n = find_connection(fd);
  189. char *msg;
  190. int sid, magic;
  191. if (unlikely(!n))
  192. return 1;
  193. msg = req->msg;
  194. /* it's not so beautiful... but arguments of each function are not common.
  195. *
  196. * so we CAN'T use requests[htonl(req->reqtype)] cetera instead.
  197. * f**k.
  198. * */
  199. magic = ntohl(req->magic);
  200. if (magic != VCC_MAGIC && magic != VCC_MAGIC_RL) {
  201. vcc_log("--- bad magic from %d. \n", fd);
  202. vcc_info("WBADMAGIC %d\n", fd);
  203. return 1;
  204. }
  205. switch (htonl(req->reqtype)) {
  206. case REQ_MSG_NEW:
  207. /* POLLOUT will be set, just because they sent us a message */
  208. return 0;
  209. case REQ_MSG_SEND:
  210. check_logged(fd);
  211. if (((unsigned char *) msg)[0] == 0xffu || !*msg)
  212. return 0;
  213. sid = ntohl(req->session);
  214. vcc_log("(%d) New message\n", sid);
  215. if (!sid)
  216. vcc_message_broadcast(msg, req->usrname, fd, req->flags);
  217. else
  218. session_broadcast(sid, msg, req->usrname, fd, req->flags);
  219. break;
  220. case REQ_CTL_USRS:
  221. check_logged(fd);
  222. send_users(fd);
  223. break;
  224. case REQ_CTL_LOGIN:
  225. do_user_login(fd, req);
  226. vcc_update_usrname(fd, req->usrname);
  227. break;
  228. case REQ_CTL_IALOG:
  229. check_logged(fd);
  230. do_ia_login(fd, req);
  231. vcc_update_usrname(fd, req->usrname);
  232. break;
  233. case REQ_CTL_NEWSE:
  234. check_logged(fd);
  235. create_session(fd, req->usrname);
  236. break;
  237. case REQ_CTL_SESS:
  238. check_logged(fd);
  239. send_sessions(fd);
  240. break;
  241. case REQ_CTL_JOINS:
  242. check_logged(fd);
  243. join_session(ntohl(req->session), fd);
  244. break;
  245. case REQ_CTL_UINFO:
  246. check_logged(fd);
  247. get_user_info(fd, req);
  248. break;
  249. case REQ_SYS_SCRINC:
  250. check_logged(fd);
  251. incr_score(fd, req);
  252. break;
  253. case REQ_SYS_INFO:
  254. check_logged(fd);
  255. monitor_info(fd, req);
  256. break;
  257. case REQ_CTL_SENAME:
  258. check_logged(fd);
  259. session_name(fd, req);
  260. break;
  261. default:
  262. vcc_log("--- bad request type from %d\n", fd);
  263. vcc_info("WBADREQ %d\n", fd);
  264. break;
  265. }
  266. vcc_write_packages(fd);
  267. return 0;
  268. }