vcc.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /* vcc/vcc.c
  2. *
  3. * This file is part of vcc.
  4. *
  5. * vcc 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. * vcc 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 vcc. 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 <signal.h>
  31. #include <netdb.h>
  32. #include <klist.h>
  33. #include <vcc/vcc.h>
  34. #include <vcc/plugin.h>
  35. #include <vcc/version.h>
  36. #include <vcc/interfaces.h>
  37. #define clear_req(req) memset(req, 0, sizeof(*req))
  38. int current_sid = 0;
  39. struct klist_node sname_cache = KLIST_NODE_INIT(&sname_cache);
  40. int init_socket(const char *server_addr, int server_port) {
  41. struct sockaddr_in addr, server;
  42. struct hostent *hentry;
  43. if (unlikely((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)) {
  44. perror("socket()");
  45. exit(1);
  46. }
  47. memset(&addr, 0, sizeof(struct sockaddr_in));
  48. addr.sin_family = AF_INET;
  49. addr.sin_addr.s_addr = htonl(INADDR_ANY);
  50. addr.sin_port = htons(0);
  51. if (unlikely(bind(fd, (struct sockaddr *) &addr, sizeof(struct sockaddr_in)) < 0)) {
  52. perror("bind()");
  53. exit(1);
  54. }
  55. memset(&server, 0, sizeof(struct sockaddr_in));
  56. server.sin_family = AF_INET;
  57. server.sin_port = htons(server_port);
  58. if (unlikely(!inet_aton(server_addr, &server.sin_addr))) {
  59. if (!(hentry = gethostbyname(server_addr))) {
  60. fprintf(stderr, _("invalid address or host. "
  61. "leave blank if you want to connect the default server. \n"));
  62. exit(1);
  63. }
  64. inet_aton(inet_ntoa(*(struct in_addr *) hentry->h_addr), &server.sin_addr);
  65. }
  66. if (unlikely(connect(fd, (struct sockaddr *) &server, sizeof(struct sockaddr_in)) < 0)) {
  67. perror("connect()");
  68. exit(1);
  69. }
  70. return fd;
  71. }
  72. int __send_msg(const char *msg, const char *usrname, int flags) {
  73. struct vcc_request req;
  74. clear_req(&req);
  75. req.magic = htonl(VCC_MAGIC);
  76. req.reqtype = htonl(REQ_MSG_SEND);
  77. req.uid = 0;
  78. req.session = htonl(current_sid);
  79. req.flags = htonl(flags);
  80. strncpy(req.usrname, usrname, USRNAME_SIZE);
  81. strncpy(req.msg, msg, MSG_SIZE);
  82. write(fd, &req, REQ_SIZE);
  83. return 0;
  84. }
  85. extern inline int send_msg(const char *msg, const char *usrname) {
  86. return __send_msg(msg, usrname, 0);
  87. }
  88. extern inline int send_msg_encrypted(const char *msg, const char *usrname) {
  89. return __send_msg(msg, usrname, FLAG_ENCRYPTED);
  90. }
  91. int __send_relay_msg(const char *msg, const char *usrname, const char *visible, int sid) {
  92. struct vcc_relay_header req;
  93. int len;
  94. len = strlen(msg);
  95. clear_req(&req);
  96. req.magic = htonl(VCC_MAGIC_RL);
  97. req.reqtype = htonl(REQ_REL_MSG);
  98. req.uid = 0;
  99. req.session = htonl(sid);
  100. req.size = htonl(sizeof(struct vcc_relay_header) + len);
  101. strncpy(req.usrname, usrname, USRNAME_SIZE);
  102. if (visible)
  103. strncpy(req.visible, visible, USRNAME_SIZE);
  104. else
  105. memset(req.visible, 0, USRNAME_SIZE);
  106. write(fd, &req, sizeof(struct vcc_relay_header));
  107. write(fd, msg, len);
  108. return 0;
  109. }
  110. int send_relay_msg(const char *msg, const char *usrname, const char *visible) {
  111. return __send_relay_msg(msg, usrname, visible, current_sid);
  112. }
  113. int read_until(int fd, void *p, int until) {
  114. int did_read = 0;
  115. int n;
  116. while ((did_read += n = read(fd, p, until)) < until) {
  117. if (!n)
  118. return 0;
  119. p += n;
  120. }
  121. return until;
  122. }
  123. char *relay_get_msg(int fd, struct vcc_relay_header *hdr) {
  124. char *p;
  125. int size;
  126. size = ntohl(hdr->size);
  127. if (unlikely(!(p = malloc(size)))) {
  128. fprintf(stderr, "*** malloc() failed\n");
  129. return NULL;
  130. }
  131. memset(p, 0, size);
  132. read_until(fd, p, size - sizeof(struct vcc_relay_header));
  133. return p;
  134. }
  135. int get_user_info(char *usrname) {
  136. struct vcc_request req;
  137. clear_req(&req);
  138. req.magic = htonl(VCC_MAGIC);
  139. req.reqtype = htonl(REQ_CTL_UINFO);
  140. req.uid = 0;
  141. strncpy(req.msg, usrname, USRNAME_SIZE);
  142. write(fd, &req, REQ_SIZE);
  143. return 0;
  144. }
  145. int get_users(void) {
  146. struct vcc_request req;
  147. clear_req(&req);
  148. req.magic = htonl(VCC_MAGIC);
  149. req.reqtype = htonl(REQ_CTL_USRS);
  150. req.uid = 0;
  151. write(fd, &req, REQ_SIZE);
  152. return 0;
  153. }
  154. int get_sessions(void) {
  155. struct vcc_request req;
  156. clear_req(&req);
  157. req.magic = htonl(VCC_MAGIC);
  158. req.reqtype = htonl(REQ_CTL_SESS);
  159. req.uid = 0;
  160. write(fd, &req, REQ_SIZE);
  161. return 0;
  162. }
  163. int do_bh(struct vcc_request *req) {
  164. switch (ntohl(req->reqtype)) {
  165. case REQ_CTL_LOGIN:
  166. vcc_login_bh(req);
  167. break;
  168. #if 0
  169. /* obsolete, see do_relay_bh() */
  170. case REQ_CTL_USRS:
  171. do_cmd_ls_bh(req);
  172. break;
  173. #endif
  174. case REQ_CTL_SESS:
  175. do_cmd_lsse_bh(req);
  176. break;
  177. case REQ_CTL_UINFO:
  178. do_cmd_uinfo_bh(req);
  179. break;
  180. case REQ_SYS_SCRINC:
  181. do_cmd_incr_bh(req);
  182. break;
  183. default:
  184. fprintf(stderr, "*** unexpected packet type %d. \n", ntohl(req->reqtype));
  185. break;
  186. }
  187. return 0;
  188. }
  189. int do_relay_bh(struct vcc_relay_header *req) {
  190. switch (ntohl(req->reqtype)) {
  191. case REQ_CTL_USRS:
  192. do_cmd_ls_bh(req);
  193. break;
  194. default:
  195. fprintf(stderr, "*** unexpected packet type %d. \n", ntohl(req->reqtype));
  196. break;
  197. }
  198. return 0;
  199. }
  200. int __login(char *usrname, char *passwd, int type) {
  201. struct vcc_request req;
  202. clear_req(&req);
  203. req.magic = htonl(VCC_MAGIC);
  204. /* REQ_CTL_LOGIN or REQ_CTL_IALOG */
  205. req.reqtype = htonl(type);
  206. strncpy(req.usrname, usrname, USRNAME_SIZE);
  207. strncpy(req.msg, passwd, MSG_SIZE);
  208. write(fd, &req, REQ_SIZE);
  209. return 0;
  210. }
  211. int vcc_login(char *usrname, char *passwd) {
  212. return __login(usrname, passwd, REQ_CTL_LOGIN);
  213. }
  214. int ia_login(char *session) {
  215. return __login(usrname, session, REQ_CTL_IALOG);
  216. }
  217. int vcc_login_bh(struct vcc_request *req) {
  218. if (ntohl(req->reqtype) != REQ_CTL_LOGIN) {
  219. fprintf(stderr, "*** bad package. \n");
  220. return 1;
  221. }
  222. if (ntohl(req->uid))
  223. printf(_("ready. \n"));
  224. else {
  225. fprintf(stderr, _("login failed: wrong password or user doesn't exist. \n"));
  226. fprintf(stderr, _("if you haven't got an account on this server, ask the administrator "
  227. "and you can get one. \n"));
  228. exit(1);
  229. }
  230. return 0;
  231. }
  232. int vcc_create_session(char *name) {
  233. struct vcc_request req;
  234. clear_req(&req);
  235. req.magic = htonl(VCC_MAGIC);
  236. req.reqtype = htonl(REQ_CTL_NEWSE);
  237. strncpy(req.usrname, name, USRNAME_SIZE);
  238. write(fd, &req, REQ_SIZE);
  239. return 0;
  240. }
  241. int join_session(int sid) {
  242. struct vcc_request req;
  243. clear_req(&req);
  244. req.magic = htonl(VCC_MAGIC);
  245. req.reqtype = htonl(REQ_CTL_JOINS);
  246. req.session = htonl(sid);
  247. write(fd, &req, REQ_SIZE);
  248. return 0;
  249. }
  250. int quit_session(int sid) {
  251. struct vcc_request req;
  252. clear_req(&req);
  253. req.magic = htonl(VCC_MAGIC);
  254. req.reqtype = htonl(REQ_CTL_QUITS);
  255. req.session = htonl(sid);
  256. write(fd, &req, REQ_SIZE);
  257. return 0;
  258. }
  259. int session_name(int sid, char *buf) {
  260. struct vcc_request req;
  261. clear_req(&req);
  262. req.magic = htonl(VCC_MAGIC);
  263. req.reqtype = htonl(REQ_CTL_SENAME);
  264. req.session = htonl(sid);
  265. write(fd, &req, REQ_SIZE);
  266. for (;;) {
  267. read(fd, &req, REQ_SIZE);
  268. if (ntohl(req.reqtype) != REQ_CTL_SENAME)
  269. continue;
  270. if (ntohl(req.session) == -1u)
  271. strcpy(buf, "*** no such session");
  272. else
  273. strcpy(buf, req.msg);
  274. break;
  275. }
  276. return 0;
  277. }
  278. char *getsname(int sid) {
  279. struct klist_node *n;
  280. struct sname_cache *p;
  281. for (n = sname_cache.next; n != &sname_cache; n = n->next) {
  282. p = sname_cache_of(n);
  283. if (p->sid == sid)
  284. return p->name;
  285. }
  286. if (unlikely(!(p = malloc(sizeof(struct sname_cache))))) {
  287. fprintf(stderr, "*** malloc() failed\n");
  288. return 0;
  289. }
  290. klist_init(&p->node);
  291. klist_add(&sname_cache, &p->node);
  292. session_name(sid, p->name);
  293. p->sid = sid;
  294. return p->name;
  295. }
  296. int get_name_sid(char *sname) {
  297. char *p;
  298. int i;
  299. for (i = 0; ; i++) {
  300. p = getsname(i);
  301. if (!strcmp(p, "*** no such session"))
  302. return -1;
  303. if (!strcmp(sname, p))
  304. return i;
  305. }
  306. return -1;
  307. }
  308. int incr_score(char *name, int incr) {
  309. struct vcc_request req;
  310. clear_req(&req);
  311. req.magic = htonl(VCC_MAGIC);
  312. req.reqtype = htonl(REQ_SYS_SCRINC);
  313. req.session = htonl(incr);
  314. strcpy(req.usrname, name);
  315. write(fd, &req, REQ_SIZE);
  316. return 0;
  317. }
  318. int get_sys_info(void) {
  319. struct vcc_request req;
  320. clear_req(&req);
  321. req.magic = htonl(VCC_MAGIC);
  322. req.reqtype = htonl(REQ_SYS_INFO);
  323. strcpy(req.usrname, usrname);
  324. write(fd, &req, REQ_SIZE);
  325. return 0;
  326. }