2 * Copyright (c) 2023 Omar Polo <op@openbsd.org>
3 * Copyright (c) 2003-2004 Daniel Hartmeier
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/types.h>
33 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
56 void icbirc_quit(void);
57 int sync_write(int, const char *, int);
58 static void usage(void);
61 unsigned long bytes_in, bytes_out;
66 extern char *__progname;
68 fprintf(stderr, "usage: %s host[:port][/room]\n",
74 main(int argc, char *argv[])
76 struct pollfd pfds[2], *pirc, *picb;
77 struct addrinfo hints, *res, *res0;
78 const char *host, *cause = NULL;
80 int ch, error, save_errno;
81 int listen_fd = -1, server_fd = -1;
84 if (pledge("stdio inet dns", NULL) == -1)
86 #endif /* __OpenBSD__ */
88 while ((ch = getopt(argc, argv, "")) != -1) {
100 signal(SIGPIPE, SIG_IGN);
101 if (fcntl(0, F_SETFL, fcntl(listen_fd, F_GETFL) | O_NONBLOCK))
104 if ((port = strchr(argv[0], ':')) != NULL) {
106 if ((room = strchr(port, '/')) != NULL) {
108 strlcpy(irc_pass, room, sizeof(irc_pass));
110 } else if ((room = strchr(argv[0], '/')) != NULL) {
112 strlcpy(irc_pass, room, sizeof(irc_pass));
120 irc_send_notice(1, "*** Connecting to server %s:%s", host, port);
122 memset(&hints, 0, sizeof(hints));
123 hints.ai_family = AF_UNSPEC;
124 hints.ai_socktype = SOCK_STREAM;
125 error = getaddrinfo(host, port, &hints, &res0);
127 errx(1, "can't resolve %s: %s", host, gai_strerror(error));
129 for (res = res0; res; res = res->ai_next) {
130 server_fd = socket(res->ai_family, res->ai_socktype,
132 if (server_fd == -1) {
137 if (connect(server_fd, res->ai_addr, res->ai_addrlen) == -1) {
152 if (fcntl(server_fd, F_SETFL, fcntl(server_fd, F_GETFL) | O_NONBLOCK))
157 if (pledge("stdio", NULL) == -1)
159 #endif /* __OpenBSD__ */
161 irc_send_notice(1, "*** Connected");
164 memset(&pfds, 0, sizeof(pfds));
169 pirc->events = POLLIN;
171 picb->fd = server_fd;
172 picb->events = POLLIN;
178 if (poll(pfds, 2, 10000) == -1) {
184 if (picb->revents & (POLLIN|POLLNVAL|POLLHUP)) {
185 len = read(server_fd, buf, sizeof(buf));
193 warnx("connection closed by server");
194 irc_send_notice(1, "*** Connection "
198 icb_recv(buf, len, 1, server_fd);
202 if (pirc->revents & (POLLIN|POLLNVAL|POLLHUP)) {
203 len = read(0, buf, sizeof(buf));
211 warnx("connection closed by client");
214 irc_recv(buf, len, 1, server_fd);
225 irc_send_notice(1, "*** Closing connection "
226 "(%u seconds, %lu:%lu bytes)",
227 time(NULL) - t, bytes_out, bytes_in);
232 sync_write(int fd, const char *buf, int len)
242 FD_SET(fd, &writefds);
243 memset(&tv, 0, sizeof(tv));
245 r = select(fd + 1, NULL, &writefds, NULL, &tv);
247 if (errno != EINTR) {
253 if (r > 0 && FD_ISSET(fd, &writefds)) {
254 r = write(fd, buf + off, len - off);