Blob


1 /* $Id: icbirc.c,v 1.3 2016/04/25 08:17:01 dhartmei Exp $ */
3 /*
4 * Copyright (c) 2003-2004 Daniel Hartmeier
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
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.
17 *
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.
30 *
31 */
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <sys/stat.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
38 #include <ctype.h>
39 #include <err.h>
40 #include <errno.h>
41 #include <fcntl.h>
42 #include <netdb.h>
43 #include <poll.h>
44 #include <signal.h>
45 #include <stdarg.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <time.h>
50 #include <unistd.h>
51 #include "icb.h"
52 #include "irc.h"
54 void icbirc_quit(void);
55 int sync_write(int, const char *, int);
56 static void usage(void);
58 time_t t;
59 unsigned long bytes_in, bytes_out;
61 static void
62 usage(void)
63 {
64 extern char *__progname;
66 fprintf(stderr, "usage: %s host[:port][/room]\n",
67 getprogname());
68 exit(1);
69 }
71 int
72 main(int argc, char *argv[])
73 {
74 struct pollfd pfds[2], *pirc, *picb;
75 struct addrinfo hints, *res, *res0;
76 const char *host, *cause = NULL;
77 char *port, *room;
78 int ch, error, save_errno;
79 int listen_fd = -1, server_fd = -1;
81 #ifdef __OpenBSD__
82 if (pledge("stdio inet dns", NULL) == -1)
83 err(1, "pledge");
84 #endif /* __OpenBSD__ */
86 while ((ch = getopt(argc, argv, "")) != -1) {
87 switch (ch) {
88 default:
89 usage();
90 }
91 }
92 argc -= optind;
93 argv += optind;
95 if (argc != 1)
96 usage();
98 signal(SIGPIPE, SIG_IGN);
99 if (fcntl(0, F_SETFL, fcntl(listen_fd, F_GETFL) | O_NONBLOCK))
100 err(1, "fcntl");
102 if ((port = strchr(argv[0], ':')) != NULL) {
103 *port++ = '\0';
104 if ((room = strchr(port, '/')) != NULL) {
105 *room++ = '\0';
106 strlcpy(irc_pass, room, sizeof(irc_pass));
108 } else if ((room = strchr(argv[0], '/')) != NULL) {
109 *room++ = '\0';
110 strlcpy(irc_pass, room, sizeof(irc_pass));
112 host = argv[0];
113 if (port == NULL)
114 port = "7326";
116 t = time(NULL);
118 irc_send_notice(1, "*** Connecting to server %s:%s", host, port);
120 memset(&hints, 0, sizeof(hints));
121 hints.ai_family = AF_UNSPEC;
122 hints.ai_socktype = SOCK_STREAM;
123 error = getaddrinfo(host, port, &hints, &res0);
124 if (error)
125 errx(1, "can't resolve %s: %s", host, gai_strerror(error));
127 for (res = res0; res; res = res->ai_next) {
128 server_fd = socket(res->ai_family, res->ai_socktype,
129 res->ai_protocol);
130 if (server_fd == -1) {
131 cause = "socket";
132 continue;
135 if (connect(server_fd, res->ai_addr, res->ai_addrlen) == -1) {
136 cause = "connect";
137 save_errno = errno;
138 close(server_fd);
139 errno = save_errno;
140 server_fd = -1;
141 continue;
144 break;
146 freeaddrinfo(res0);
147 if (server_fd == -1)
148 err(1, "%s", cause);
150 if (fcntl(server_fd, F_SETFL, fcntl(server_fd, F_GETFL) | O_NONBLOCK))
151 err(1, "fcntl");
153 #ifdef __OpenBSD__
154 /* drop inet dns */
155 if (pledge("stdio", NULL) == -1)
156 err(1, "pledge");
157 #endif /* __OpenBSD__ */
159 irc_send_notice(1, "*** Connected");
160 icb_init();
162 memset(&pfds, 0, sizeof(pfds));
163 pirc = &pfds[0];
164 picb = &pfds[1];
166 pirc->fd = 0;
167 pirc->events = POLLIN;
169 picb->fd = server_fd;
170 picb->events = POLLIN;
172 for (;;) {
173 char buf[65535];
174 int len;
176 if (poll(pfds, 2, 10) == -1) {
177 if (errno == EINTR)
178 continue;
179 err(1, "poll");
182 if (picb->revents & (POLLIN|POLLNVAL|POLLHUP)) {
183 len = read(server_fd, buf, sizeof(buf));
184 if (len < 0) {
185 if (errno == EINTR)
186 continue;
187 warnx("read");
188 len = 0;
190 if (len == 0) {
191 warnx("connection closed by server");
192 irc_send_notice(1, "*** Connection "
193 "closed by server");
194 break;
196 icb_recv(buf, len, 0, server_fd);
197 bytes_in += len;
200 if (pirc->revents & (POLLIN|POLLNVAL|POLLHUP)) {
201 len = read(0, buf, sizeof(buf));
202 if (len < 0) {
203 if (errno == EINTR)
204 continue;
205 warnx("read");
206 len = 0;
208 if (len == 0) {
209 warnx("connection closed by client");
210 break;
212 irc_recv(buf, len, 1, server_fd);
213 bytes_out += len;
217 icbirc_quit();
220 void
221 icbirc_quit(void)
223 irc_send_notice(1, "*** Closing connection "
224 "(%u seconds, %lu:%lu bytes)",
225 time(NULL) - t, bytes_out, bytes_in);
226 exit(0);
229 int
230 sync_write(int fd, const char *buf, int len)
232 int off = 0;
234 while (len > off) {
235 fd_set writefds;
236 struct timeval tv;
237 int r;
239 FD_ZERO(&writefds);
240 FD_SET(fd, &writefds);
241 memset(&tv, 0, sizeof(tv));
242 tv.tv_sec = 10;
243 r = select(fd + 1, NULL, &writefds, NULL, &tv);
244 if (r < 0) {
245 if (errno != EINTR) {
246 perror("select");
247 return (1);
249 continue;
251 if (r > 0 && FD_ISSET(fd, &writefds)) {
252 r = write(fd, buf + off, len - off);
253 if (r < 0) {
254 perror("write");
255 return (1);
257 off += r;
260 return (0);