IRC bouncer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

server.c 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* Copyright (C) 2019 C. McEnroe <june@causal.agency>
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. */
  16. #include <assert.h>
  17. #include <err.h>
  18. #include <netdb.h>
  19. #include <netinet/in.h>
  20. #include <stdarg.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <sys/socket.h>
  25. #include <sysexits.h>
  26. #include <tls.h>
  27. #include <unistd.h>
  28. #include "bounce.h"
  29. static struct tls *client;
  30. void serverConfig(bool insecure, const char *cert, const char *priv) {
  31. struct tls_config *config = tls_config_new();
  32. if (!config) errx(EX_SOFTWARE, "tls_config_new");
  33. int error = tls_config_set_ciphers(config, "compat");
  34. if (error) {
  35. errx(EX_SOFTWARE, "tls_config_set_ciphers: %s", tls_config_error(config));
  36. }
  37. if (insecure) {
  38. tls_config_insecure_noverifycert(config);
  39. tls_config_insecure_noverifyname(config);
  40. }
  41. if (cert) {
  42. error = tls_config_set_keypair_file(config, cert, (priv ? priv : cert));
  43. if (error) {
  44. errx(
  45. EX_SOFTWARE, "tls_config_set_keypair_file: %s",
  46. tls_config_error(config)
  47. );
  48. }
  49. }
  50. client = tls_client();
  51. if (!client) errx(EX_SOFTWARE, "tls_client");
  52. error = tls_configure(client, config);
  53. if (error) errx(EX_SOFTWARE, "tls_configure: %s", tls_error(client));
  54. tls_config_free(config);
  55. }
  56. int serverConnect(const char *host, const char *port) {
  57. assert(client);
  58. struct addrinfo *head;
  59. struct addrinfo hints = {
  60. .ai_family = AF_UNSPEC,
  61. .ai_socktype = SOCK_STREAM,
  62. .ai_protocol = IPPROTO_TCP,
  63. };
  64. int error = getaddrinfo(host, port, &hints, &head);
  65. if (error) errx(EX_NOHOST, "%s:%s: %s", host, port, gai_strerror(error));
  66. int sock = -1;
  67. for (struct addrinfo *ai = head; ai; ai = ai->ai_next) {
  68. sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
  69. if (sock < 0) err(EX_OSERR, "socket");
  70. error = connect(sock, ai->ai_addr, ai->ai_addrlen);
  71. if (!error) break;
  72. close(sock);
  73. sock = -1;
  74. }
  75. if (sock < 0) err(EX_UNAVAILABLE, "%s:%s", host, port);
  76. freeaddrinfo(head);
  77. int yes = 1;
  78. error = setsockopt(sock, SOL_SOCKET, SO_NOSIGPIPE, &yes, sizeof(yes));
  79. if (error) err(EX_OSERR, "setsockopt");
  80. error = tls_connect_socket(client, sock, host);
  81. if (error) errx(EX_PROTOCOL, "tls_connect: %s", tls_error(client));
  82. error = tls_handshake(client);
  83. if (error) errx(EX_PROTOCOL, "tls_handshake: %s", tls_error(client));
  84. return sock;
  85. }
  86. void serverSend(const char *ptr, size_t len) {
  87. if (verbose) fprintf(stderr, "\x1B[31m%.*s\x1B[m", (int)len, ptr);
  88. while (len) {
  89. ssize_t ret = tls_write(client, ptr, len);
  90. if (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT) continue;
  91. if (ret < 0) errx(EX_IOERR, "server tls_write: %s", tls_error(client));
  92. ptr += ret;
  93. len -= ret;
  94. }
  95. }
  96. void serverFormat(const char *format, ...) {
  97. char buf[1024];
  98. va_list ap;
  99. va_start(ap, format);
  100. int len = vsnprintf(buf, sizeof(buf), format, ap);
  101. va_end(ap);
  102. assert((size_t)len < sizeof(buf));
  103. serverSend(buf, len);
  104. }
  105. void serverRecv(void) {
  106. static char buf[4096];
  107. static size_t len;
  108. ssize_t read = tls_read(client, &buf[len], sizeof(buf) - len);
  109. if (read == TLS_WANT_POLLIN || read == TLS_WANT_POLLOUT) return;
  110. if (read < 0) errx(EX_IOERR, "server tls_read: %s", tls_error(client));
  111. len += read;
  112. char *crlf;
  113. char *line = buf;
  114. for (;;) {
  115. crlf = memmem(line, &buf[len] - line, "\r\n", 2);
  116. if (!crlf) break;
  117. crlf[0] = '\0';
  118. if (verbose) fprintf(stderr, "\x1B[32m%s\x1B[m\n", line);
  119. if (!strncmp(line, "PING ", 5)) {
  120. serverFormat("PONG %s\r\n", &line[5]);
  121. } else {
  122. if (stateReady()) ringProduce(line);
  123. stateParse(line);
  124. }
  125. line = crlf + 2;
  126. }
  127. len -= line - buf;
  128. memmove(buf, line, len);
  129. }