Blame


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