vcc.c 7.1 KB

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