Blame


1 d23d2886 2023-07-30 op /* $Id: icbirc.c,v 1.3 2016/04/25 08:17:01 dhartmei Exp $ */
2 d23d2886 2023-07-30 op
3 d23d2886 2023-07-30 op /*
4 d23d2886 2023-07-30 op * Copyright (c) 2003-2004 Daniel Hartmeier
5 d23d2886 2023-07-30 op * All rights reserved.
6 d23d2886 2023-07-30 op *
7 d23d2886 2023-07-30 op * Redistribution and use in source and binary forms, with or without
8 d23d2886 2023-07-30 op * modification, are permitted provided that the following conditions
9 d23d2886 2023-07-30 op * are met:
10 d23d2886 2023-07-30 op *
11 d23d2886 2023-07-30 op * - Redistributions of source code must retain the above copyright
12 d23d2886 2023-07-30 op * notice, this list of conditions and the following disclaimer.
13 d23d2886 2023-07-30 op * - Redistributions in binary form must reproduce the above
14 d23d2886 2023-07-30 op * copyright notice, this list of conditions and the following
15 d23d2886 2023-07-30 op * disclaimer in the documentation and/or other materials provided
16 d23d2886 2023-07-30 op * with the distribution.
17 d23d2886 2023-07-30 op *
18 d23d2886 2023-07-30 op * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 d23d2886 2023-07-30 op * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 d23d2886 2023-07-30 op * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 d23d2886 2023-07-30 op * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 d23d2886 2023-07-30 op * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 d23d2886 2023-07-30 op * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 d23d2886 2023-07-30 op * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 d23d2886 2023-07-30 op * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 d23d2886 2023-07-30 op * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 d23d2886 2023-07-30 op * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 d23d2886 2023-07-30 op * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 d23d2886 2023-07-30 op * POSSIBILITY OF SUCH DAMAGE.
30 d23d2886 2023-07-30 op *
31 d23d2886 2023-07-30 op */
32 d23d2886 2023-07-30 op
33 d23d2886 2023-07-30 op #include <sys/types.h>
34 d23d2886 2023-07-30 op #include <sys/socket.h>
35 d23d2886 2023-07-30 op #include <sys/stat.h>
36 fe009782 2023-08-01 op
37 d23d2886 2023-07-30 op #include <netinet/in.h>
38 d23d2886 2023-07-30 op #include <arpa/inet.h>
39 fe009782 2023-08-01 op
40 d23d2886 2023-07-30 op #include <ctype.h>
41 3e0dfc16 2023-08-01 op #include <err.h>
42 d23d2886 2023-07-30 op #include <errno.h>
43 d23d2886 2023-07-30 op #include <fcntl.h>
44 d23d2886 2023-07-30 op #include <netdb.h>
45 4070540c 2023-08-01 op #include <poll.h>
46 d23d2886 2023-07-30 op #include <signal.h>
47 d23d2886 2023-07-30 op #include <stdarg.h>
48 d23d2886 2023-07-30 op #include <stdio.h>
49 d23d2886 2023-07-30 op #include <stdlib.h>
50 d23d2886 2023-07-30 op #include <string.h>
51 d23d2886 2023-07-30 op #include <time.h>
52 d23d2886 2023-07-30 op #include <unistd.h>
53 fe009782 2023-08-01 op
54 d23d2886 2023-07-30 op #include "icb.h"
55 d23d2886 2023-07-30 op #include "irc.h"
56 d23d2886 2023-07-30 op
57 c8394ca1 2023-08-01 op void icbirc_quit(void);
58 d23d2886 2023-07-30 op int sync_write(int, const char *, int);
59 d23d2886 2023-07-30 op static void usage(void);
60 d23d2886 2023-07-30 op
61 c8394ca1 2023-08-01 op time_t t;
62 c8394ca1 2023-08-01 op unsigned long bytes_in, bytes_out;
63 c8394ca1 2023-08-01 op
64 d23d2886 2023-07-30 op static void
65 d23d2886 2023-07-30 op usage(void)
66 d23d2886 2023-07-30 op {
67 d23d2886 2023-07-30 op extern char *__progname;
68 d23d2886 2023-07-30 op
69 2e004277 2023-08-01 op fprintf(stderr, "usage: %s host[:port][/room]\n",
70 25d0780c 2023-08-01 op getprogname());
71 d23d2886 2023-07-30 op exit(1);
72 d23d2886 2023-07-30 op }
73 d23d2886 2023-07-30 op
74 d23d2886 2023-07-30 op int
75 d23d2886 2023-07-30 op main(int argc, char *argv[])
76 d23d2886 2023-07-30 op {
77 4070540c 2023-08-01 op struct pollfd pfds[2], *pirc, *picb;
78 7d9bde6e 2023-08-01 op struct addrinfo hints, *res, *res0;
79 7d9bde6e 2023-08-01 op const char *host, *cause = NULL;
80 2e004277 2023-08-01 op char *port, *room;
81 7d9bde6e 2023-08-01 op int ch, error, save_errno;
82 3e0dfc16 2023-08-01 op int listen_fd = -1, server_fd = -1;
83 d23d2886 2023-07-30 op
84 3e0dfc16 2023-08-01 op #ifdef __OpenBSD__
85 3e0dfc16 2023-08-01 op if (pledge("stdio inet dns", NULL) == -1)
86 3e0dfc16 2023-08-01 op err(1, "pledge");
87 3e0dfc16 2023-08-01 op #endif /* __OpenBSD__ */
88 3e0dfc16 2023-08-01 op
89 2e004277 2023-08-01 op while ((ch = getopt(argc, argv, "")) != -1) {
90 d23d2886 2023-07-30 op switch (ch) {
91 d23d2886 2023-07-30 op default:
92 d23d2886 2023-07-30 op usage();
93 d23d2886 2023-07-30 op }
94 d23d2886 2023-07-30 op }
95 d23d2886 2023-07-30 op argc -= optind;
96 2e004277 2023-08-01 op argv += optind;
97 2e004277 2023-08-01 op
98 2e004277 2023-08-01 op if (argc != 1)
99 d23d2886 2023-07-30 op usage();
100 d23d2886 2023-07-30 op
101 7d9bde6e 2023-08-01 op signal(SIGPIPE, SIG_IGN);
102 7d9bde6e 2023-08-01 op if (fcntl(0, F_SETFL, fcntl(listen_fd, F_GETFL) | O_NONBLOCK))
103 7d9bde6e 2023-08-01 op err(1, "fcntl");
104 7d9bde6e 2023-08-01 op
105 2e004277 2023-08-01 op if ((port = strchr(argv[0], ':')) != NULL) {
106 2e004277 2023-08-01 op *port++ = '\0';
107 2e004277 2023-08-01 op if ((room = strchr(port, '/')) != NULL) {
108 2e004277 2023-08-01 op *room++ = '\0';
109 2e004277 2023-08-01 op strlcpy(irc_pass, room, sizeof(irc_pass));
110 2e004277 2023-08-01 op }
111 2e004277 2023-08-01 op } else if ((room = strchr(argv[0], '/')) != NULL) {
112 2e004277 2023-08-01 op *room++ = '\0';
113 2e004277 2023-08-01 op strlcpy(irc_pass, room, sizeof(irc_pass));
114 2e004277 2023-08-01 op }
115 7d9bde6e 2023-08-01 op host = argv[0];
116 7d9bde6e 2023-08-01 op if (port == NULL)
117 7d9bde6e 2023-08-01 op port = "7326";
118 2e004277 2023-08-01 op
119 7d9bde6e 2023-08-01 op t = time(NULL);
120 d23d2886 2023-07-30 op
121 7d9bde6e 2023-08-01 op irc_send_notice(1, "*** Connecting to server %s:%s", host, port);
122 d23d2886 2023-07-30 op
123 7d9bde6e 2023-08-01 op memset(&hints, 0, sizeof(hints));
124 7d9bde6e 2023-08-01 op hints.ai_family = AF_UNSPEC;
125 7d9bde6e 2023-08-01 op hints.ai_socktype = SOCK_STREAM;
126 7d9bde6e 2023-08-01 op error = getaddrinfo(host, port, &hints, &res0);
127 7d9bde6e 2023-08-01 op if (error)
128 7d9bde6e 2023-08-01 op errx(1, "can't resolve %s: %s", host, gai_strerror(error));
129 d23d2886 2023-07-30 op
130 7d9bde6e 2023-08-01 op for (res = res0; res; res = res->ai_next) {
131 7d9bde6e 2023-08-01 op server_fd = socket(res->ai_family, res->ai_socktype,
132 7d9bde6e 2023-08-01 op res->ai_protocol);
133 7d9bde6e 2023-08-01 op if (server_fd == -1) {
134 7d9bde6e 2023-08-01 op cause = "socket";
135 7d9bde6e 2023-08-01 op continue;
136 7d9bde6e 2023-08-01 op }
137 7d9bde6e 2023-08-01 op
138 7d9bde6e 2023-08-01 op if (connect(server_fd, res->ai_addr, res->ai_addrlen) == -1) {
139 7d9bde6e 2023-08-01 op cause = "connect";
140 7d9bde6e 2023-08-01 op save_errno = errno;
141 7d9bde6e 2023-08-01 op close(server_fd);
142 7d9bde6e 2023-08-01 op errno = save_errno;
143 7d9bde6e 2023-08-01 op server_fd = -1;
144 7d9bde6e 2023-08-01 op continue;
145 7d9bde6e 2023-08-01 op }
146 7d9bde6e 2023-08-01 op
147 7d9bde6e 2023-08-01 op break;
148 d23d2886 2023-07-30 op }
149 7d9bde6e 2023-08-01 op freeaddrinfo(res0);
150 7d9bde6e 2023-08-01 op if (server_fd == -1)
151 7d9bde6e 2023-08-01 op err(1, "%s", cause);
152 d23d2886 2023-07-30 op
153 3e0dfc16 2023-08-01 op if (fcntl(server_fd, F_SETFL, fcntl(server_fd, F_GETFL) | O_NONBLOCK))
154 3e0dfc16 2023-08-01 op err(1, "fcntl");
155 d23d2886 2023-07-30 op
156 3e0dfc16 2023-08-01 op #ifdef __OpenBSD__
157 3e0dfc16 2023-08-01 op /* drop inet dns */
158 3e0dfc16 2023-08-01 op if (pledge("stdio", NULL) == -1)
159 3e0dfc16 2023-08-01 op err(1, "pledge");
160 3e0dfc16 2023-08-01 op #endif /* __OpenBSD__ */
161 d23d2886 2023-07-30 op
162 3e0dfc16 2023-08-01 op irc_send_notice(1, "*** Connected");
163 d23d2886 2023-07-30 op icb_init();
164 3e0dfc16 2023-08-01 op
165 4070540c 2023-08-01 op memset(&pfds, 0, sizeof(pfds));
166 4070540c 2023-08-01 op pirc = &pfds[0];
167 4070540c 2023-08-01 op picb = &pfds[1];
168 4070540c 2023-08-01 op
169 4070540c 2023-08-01 op pirc->fd = 0;
170 4070540c 2023-08-01 op pirc->events = POLLIN;
171 4070540c 2023-08-01 op
172 4070540c 2023-08-01 op picb->fd = server_fd;
173 4070540c 2023-08-01 op picb->events = POLLIN;
174 4070540c 2023-08-01 op
175 3e0dfc16 2023-08-01 op for (;;) {
176 4070540c 2023-08-01 op char buf[65535];
177 4070540c 2023-08-01 op int len;
178 d23d2886 2023-07-30 op
179 4070540c 2023-08-01 op if (poll(pfds, 2, 10) == -1) {
180 4070540c 2023-08-01 op if (errno == EINTR)
181 4070540c 2023-08-01 op continue;
182 4070540c 2023-08-01 op err(1, "poll");
183 d23d2886 2023-07-30 op }
184 d23d2886 2023-07-30 op
185 4070540c 2023-08-01 op if (picb->revents & (POLLIN|POLLNVAL|POLLHUP)) {
186 4070540c 2023-08-01 op len = read(server_fd, buf, sizeof(buf));
187 4070540c 2023-08-01 op if (len < 0) {
188 4070540c 2023-08-01 op if (errno == EINTR)
189 4070540c 2023-08-01 op continue;
190 4070540c 2023-08-01 op warnx("read");
191 4070540c 2023-08-01 op len = 0;
192 d23d2886 2023-07-30 op }
193 4070540c 2023-08-01 op if (len == 0) {
194 4070540c 2023-08-01 op warnx("connection closed by server");
195 4070540c 2023-08-01 op irc_send_notice(1, "*** Connection "
196 4070540c 2023-08-01 op "closed by server");
197 4070540c 2023-08-01 op break;
198 d23d2886 2023-07-30 op }
199 84fbe5aa 2023-08-01 op icb_recv(buf, len, 1, server_fd);
200 4070540c 2023-08-01 op bytes_in += len;
201 d23d2886 2023-07-30 op }
202 4070540c 2023-08-01 op
203 4070540c 2023-08-01 op if (pirc->revents & (POLLIN|POLLNVAL|POLLHUP)) {
204 4070540c 2023-08-01 op len = read(0, buf, sizeof(buf));
205 4070540c 2023-08-01 op if (len < 0) {
206 4070540c 2023-08-01 op if (errno == EINTR)
207 4070540c 2023-08-01 op continue;
208 4070540c 2023-08-01 op warnx("read");
209 4070540c 2023-08-01 op len = 0;
210 4070540c 2023-08-01 op }
211 4070540c 2023-08-01 op if (len == 0) {
212 4070540c 2023-08-01 op warnx("connection closed by client");
213 4070540c 2023-08-01 op break;
214 4070540c 2023-08-01 op }
215 4070540c 2023-08-01 op irc_recv(buf, len, 1, server_fd);
216 4070540c 2023-08-01 op bytes_out += len;
217 4070540c 2023-08-01 op }
218 d23d2886 2023-07-30 op }
219 d23d2886 2023-07-30 op
220 c8394ca1 2023-08-01 op icbirc_quit();
221 c8394ca1 2023-08-01 op }
222 c8394ca1 2023-08-01 op
223 c8394ca1 2023-08-01 op void
224 c8394ca1 2023-08-01 op icbirc_quit(void)
225 c8394ca1 2023-08-01 op {
226 3e0dfc16 2023-08-01 op irc_send_notice(1, "*** Closing connection "
227 3e0dfc16 2023-08-01 op "(%u seconds, %lu:%lu bytes)",
228 3e0dfc16 2023-08-01 op time(NULL) - t, bytes_out, bytes_in);
229 c8394ca1 2023-08-01 op exit(0);
230 d23d2886 2023-07-30 op }
231 d23d2886 2023-07-30 op
232 d23d2886 2023-07-30 op int
233 d23d2886 2023-07-30 op sync_write(int fd, const char *buf, int len)
234 d23d2886 2023-07-30 op {
235 d23d2886 2023-07-30 op int off = 0;
236 d23d2886 2023-07-30 op
237 d23d2886 2023-07-30 op while (len > off) {
238 d23d2886 2023-07-30 op fd_set writefds;
239 d23d2886 2023-07-30 op struct timeval tv;
240 d23d2886 2023-07-30 op int r;
241 d23d2886 2023-07-30 op
242 d23d2886 2023-07-30 op FD_ZERO(&writefds);
243 d23d2886 2023-07-30 op FD_SET(fd, &writefds);
244 d23d2886 2023-07-30 op memset(&tv, 0, sizeof(tv));
245 d23d2886 2023-07-30 op tv.tv_sec = 10;
246 d23d2886 2023-07-30 op r = select(fd + 1, NULL, &writefds, NULL, &tv);
247 d23d2886 2023-07-30 op if (r < 0) {
248 d23d2886 2023-07-30 op if (errno != EINTR) {
249 d23d2886 2023-07-30 op perror("select");
250 d23d2886 2023-07-30 op return (1);
251 d23d2886 2023-07-30 op }
252 d23d2886 2023-07-30 op continue;
253 d23d2886 2023-07-30 op }
254 d23d2886 2023-07-30 op if (r > 0 && FD_ISSET(fd, &writefds)) {
255 d23d2886 2023-07-30 op r = write(fd, buf + off, len - off);
256 d23d2886 2023-07-30 op if (r < 0) {
257 d23d2886 2023-07-30 op perror("write");
258 d23d2886 2023-07-30 op return (1);
259 d23d2886 2023-07-30 op }
260 d23d2886 2023-07-30 op off += r;
261 d23d2886 2023-07-30 op }
262 d23d2886 2023-07-30 op }
263 d23d2886 2023-07-30 op return (0);
264 d23d2886 2023-07-30 op }