|
|
@@ -0,0 +1,111 @@
|
|
|
+/* vccd/relay.c
|
|
|
+ *
|
|
|
+ * This file is part of vccd.
|
|
|
+ *
|
|
|
+ * vccd is free software: you can redistribute it and/or modify
|
|
|
+ * it under the terms of the GNU General Public License as published by
|
|
|
+ * the Free Software Foundation, either version 3 of the License, or
|
|
|
+ * (at your option) any later version.
|
|
|
+ *
|
|
|
+ * vccd is distributed in the hope that it will be useful,
|
|
|
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
+ * GNU General Public License for more details.
|
|
|
+ *
|
|
|
+ * You should have received a copy of the GNU General Public License
|
|
|
+ * along with vccd. If not, see <https://www.gnu.org/licenses/>
|
|
|
+ */
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+#include <stdio.h>
|
|
|
+#include <stdlib.h>
|
|
|
+#include <unistd.h>
|
|
|
+#include <fcntl.h>
|
|
|
+#include <errno.h>
|
|
|
+#include <string.h>
|
|
|
+
|
|
|
+#include <sys/stat.h>
|
|
|
+#include <sys/types.h>
|
|
|
+#include <sys/socket.h>
|
|
|
+#include <netinet/in.h>
|
|
|
+#include <arpa/inet.h>
|
|
|
+#include <poll.h>
|
|
|
+
|
|
|
+#include <vcc/vcc.h>
|
|
|
+#include <vcc/vccd.h>
|
|
|
+#include <vcc/version.h>
|
|
|
+#include <klist.h>
|
|
|
+
|
|
|
+
|
|
|
+extern struct klist_node connections;
|
|
|
+
|
|
|
+int relay_new_msg(int fd, void *t) {
|
|
|
+ struct vcc_relay_header *req;
|
|
|
+ char *msg;
|
|
|
+ int len;
|
|
|
+
|
|
|
+ req = t;
|
|
|
+ len = ntohl(req->size) - sizeof(struct vcc_relay_header);
|
|
|
+
|
|
|
+ if (unlikely(!(msg = amalloc(len)))) {
|
|
|
+ fprintf(stderr, "*** malloc() failed\n");
|
|
|
+
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ read_until(fd, msg, len);
|
|
|
+ relay_send_to(msg, 0, req);
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+int relay_send_to(const char *msg, int expect_fd, struct vcc_relay_header *hdr) {
|
|
|
+ struct vcc_relay_header *req;
|
|
|
+ struct klist_node *n;
|
|
|
+ int i, len;
|
|
|
+
|
|
|
+ /* make sure about that the write() bottom will execute after these */
|
|
|
+ vcc_write_all();
|
|
|
+
|
|
|
+ i = 0;
|
|
|
+ len = strlen(msg);
|
|
|
+
|
|
|
+ for (n = connections.next; n != &connections; n = n->next) {
|
|
|
+ if (n->value == expect_fd)
|
|
|
+ continue;
|
|
|
+
|
|
|
+ if (unlikely(!(req = amalloc(sizeof(struct vcc_relay_header))))) {
|
|
|
+ vcc_log("*** malloc() failed\n");
|
|
|
+
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ memset(req, 0, sizeof(struct vcc_relay_header));
|
|
|
+
|
|
|
+ req->magic = htonl(VCC_MAGIC_RL);
|
|
|
+ req->reqtype = htonl(REQ_REL_NEW);
|
|
|
+ req->uid = 0;
|
|
|
+ req->session = 0;
|
|
|
+ req->size = htonl(sizeof(struct vcc_relay_header) + len);
|
|
|
+
|
|
|
+ strncpy(req->usrname, hdr->usrname, USRNAME_SIZE);
|
|
|
+
|
|
|
+ /* direct writes here
|
|
|
+ * vcc_send_req(n, req);
|
|
|
+ * */
|
|
|
+
|
|
|
+ write(n->value, req, sizeof(struct vcc_relay_header));
|
|
|
+ write(n->value, msg, len);
|
|
|
+
|
|
|
+ i++;
|
|
|
+ }
|
|
|
+
|
|
|
+ vcc_log("Relay message sent to %d users. \n", i);
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+
|