1 /* $Id: icbirc.c,v 1.3 2016/04/25 08:17:01 dhartmei Exp $ */
4 * Copyright (c) 2003-2004 Daniel Hartmeier
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/types.h>
34 #include <sys/socket.h>
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
57 void icbirc_quit(void);
58 int sync_write(int, const char *, int);
59 static void usage(void);
62 unsigned long bytes_in, bytes_out;
67 extern char *__progname;
69 fprintf(stderr, "usage: %s host[:port][/room]\n",
75 main(int argc, char *argv[])
77 struct pollfd pfds[2], *pirc, *picb;
78 struct addrinfo hints, *res, *res0;
79 const char *host, *cause = NULL;
81 int ch, error, save_errno;
82 int listen_fd = -1, server_fd = -1;
85 if (pledge("stdio inet dns", NULL) == -1)
87 #endif /* __OpenBSD__ */
89 while ((ch = getopt(argc, argv, "")) != -1) {
101 signal(SIGPIPE, SIG_IGN);
102 if (fcntl(0, F_SETFL, fcntl(listen_fd, F_GETFL) | O_NONBLOCK))
105 if ((port = strchr(argv[0], ':')) != NULL) {
107 if ((room = strchr(port, '/')) != NULL) {
109 strlcpy(irc_pass, room, sizeof(irc_pass));
111 } else if ((room = strchr(argv[0], '/')) != NULL) {
113 strlcpy(irc_pass, room, sizeof(irc_pass));
121 irc_send_notice(1, "*** Connecting to server %s:%s", host, port);
123 memset(&hints, 0, sizeof(hints));
124 hints.ai_family = AF_UNSPEC;
125 hints.ai_socktype = SOCK_STREAM;
126 error = getaddrinfo(host, port, &hints, &res0);
128 errx(1, "can't resolve %s: %s", host, gai_strerror(error));
130 for (res = res0; res; res = res->ai_next) {
131 server_fd = socket(res->ai_family, res->ai_socktype,
133 if (server_fd == -1) {
138 if (connect(server_fd, res->ai_addr, res->ai_addrlen) == -1) {
153 if (fcntl(server_fd, F_SETFL, fcntl(server_fd, F_GETFL) | O_NONBLOCK))
158 if (pledge("stdio", NULL) == -1)
160 #endif /* __OpenBSD__ */
162 irc_send_notice(1, "*** Connected");
165 memset(&pfds, 0, sizeof(pfds));
170 pirc->events = POLLIN;
172 picb->fd = server_fd;
173 picb->events = POLLIN;
179 if (poll(pfds, 2, 10) == -1) {
185 if (picb->revents & (POLLIN|POLLNVAL|POLLHUP)) {
186 len = read(server_fd, buf, sizeof(buf));
194 warnx("connection closed by server");
195 irc_send_notice(1, "*** Connection "
199 icb_recv(buf, len, 1, server_fd);
203 if (pirc->revents & (POLLIN|POLLNVAL|POLLHUP)) {
204 len = read(0, buf, sizeof(buf));
212 warnx("connection closed by client");
215 irc_recv(buf, len, 1, server_fd);
226 irc_send_notice(1, "*** Closing connection "
227 "(%u seconds, %lu:%lu bytes)",
228 time(NULL) - t, bytes_out, bytes_in);
233 sync_write(int fd, const char *buf, int len)
243 FD_SET(fd, &writefds);
244 memset(&tv, 0, sizeof(tv));
246 r = select(fd + 1, NULL, &writefds, NULL, &tv);
248 if (errno != EINTR) {
254 if (r > 0 && FD_ISSET(fd, &writefds)) {
255 r = write(fd, buf + off, len - off);