1 /* $Id: icbirc.c,v 1.3 2016/04/25 08:17:01 dhartmei Exp $ */
4 * Copyright (c) 2023 Omar Polo <op@openbsd.org>
5 * Copyright (c) 2003-2004 Daniel Hartmeier
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * - Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/types.h>
35 #include <sys/socket.h>
38 #include <netinet/in.h>
39 #include <arpa/inet.h>
58 void icbirc_quit(void);
59 int sync_write(int, const char *, int);
60 static void usage(void);
63 unsigned long bytes_in, bytes_out;
68 extern char *__progname;
70 fprintf(stderr, "usage: %s host[:port][/room]\n",
76 main(int argc, char *argv[])
78 struct pollfd pfds[2], *pirc, *picb;
79 struct addrinfo hints, *res, *res0;
80 const char *host, *cause = NULL;
82 int ch, error, save_errno;
83 int listen_fd = -1, server_fd = -1;
86 if (pledge("stdio inet dns", NULL) == -1)
88 #endif /* __OpenBSD__ */
90 while ((ch = getopt(argc, argv, "")) != -1) {
102 signal(SIGPIPE, SIG_IGN);
103 if (fcntl(0, F_SETFL, fcntl(listen_fd, F_GETFL) | O_NONBLOCK))
106 if ((port = strchr(argv[0], ':')) != NULL) {
108 if ((room = strchr(port, '/')) != NULL) {
110 strlcpy(irc_pass, room, sizeof(irc_pass));
112 } else if ((room = strchr(argv[0], '/')) != NULL) {
114 strlcpy(irc_pass, room, sizeof(irc_pass));
122 irc_send_notice(1, "*** Connecting to server %s:%s", host, port);
124 memset(&hints, 0, sizeof(hints));
125 hints.ai_family = AF_UNSPEC;
126 hints.ai_socktype = SOCK_STREAM;
127 error = getaddrinfo(host, port, &hints, &res0);
129 errx(1, "can't resolve %s: %s", host, gai_strerror(error));
131 for (res = res0; res; res = res->ai_next) {
132 server_fd = socket(res->ai_family, res->ai_socktype,
134 if (server_fd == -1) {
139 if (connect(server_fd, res->ai_addr, res->ai_addrlen) == -1) {
154 if (fcntl(server_fd, F_SETFL, fcntl(server_fd, F_GETFL) | O_NONBLOCK))
159 if (pledge("stdio", NULL) == -1)
161 #endif /* __OpenBSD__ */
163 irc_send_notice(1, "*** Connected");
166 memset(&pfds, 0, sizeof(pfds));
171 pirc->events = POLLIN;
173 picb->fd = server_fd;
174 picb->events = POLLIN;
180 if (poll(pfds, 2, 10000) == -1) {
186 if (picb->revents & (POLLIN|POLLNVAL|POLLHUP)) {
187 len = read(server_fd, buf, sizeof(buf));
195 warnx("connection closed by server");
196 irc_send_notice(1, "*** Connection "
200 icb_recv(buf, len, 1, server_fd);
204 if (pirc->revents & (POLLIN|POLLNVAL|POLLHUP)) {
205 len = read(0, buf, sizeof(buf));
213 warnx("connection closed by client");
216 irc_recv(buf, len, 1, server_fd);
227 irc_send_notice(1, "*** Closing connection "
228 "(%u seconds, %lu:%lu bytes)",
229 time(NULL) - t, bytes_out, bytes_in);
234 sync_write(int fd, const char *buf, int len)
244 FD_SET(fd, &writefds);
245 memset(&tv, 0, sizeof(tv));
247 r = select(fd + 1, NULL, &writefds, NULL, &tv);
249 if (errno != EINTR) {
255 if (r > 0 && FD_ISSET(fd, &writefds)) {
256 r = write(fd, buf + off, len - off);