ii-2.0-ucspi.diff - sites - public wiki contents of suckless.org
(HTM) git clone git://git.suckless.org/sites
(DIR) Log
(DIR) Files
(DIR) Refs
---
ii-2.0-ucspi.diff (9012B)
---
1 commit 42a582d675b09f387b154f8149c0c75e303955e0
2 Author: Jan Klemkow <j.klemkow@wemelug.de>
3 Date: Sat Sep 2 12:36:34 2017 +0200
4
5 replace socket handling with ucspi backend
6
7 diff --git a/ii.c b/ii.c
8 index c402a87..193b0b9 100644
9 --- a/ii.c
10 +++ b/ii.c
11 @@ -1,16 +1,13 @@
12 /* See LICENSE file for license details. */
13 #include <sys/select.h>
14 -#include <sys/socket.h>
15 #include <sys/stat.h>
16 #include <sys/types.h>
17 -#include <sys/un.h>
18
19 #include <ctype.h>
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <limits.h>
23 #include <netdb.h>
24 -#include <netinet/in.h>
25 #include <pwd.h>
26 #include <signal.h>
27 #include <stdarg.h>
28 @@ -20,6 +17,9 @@
29 #include <time.h>
30 #include <unistd.h>
31
32 +#define READ_FD 6
33 +#define WRITE_FD 7
34 +
35 char *argv0;
36
37 #include "arg.h"
38 @@ -59,16 +59,16 @@ static void create_dirtree(const char *);
39 static void create_filepath(char *, size_t, const char *, const char *, const char *);
40 static void die(const char *, ...);
41 static void ewritestr(int, const char *);
42 -static void handle_channels_input(int, Channel *);
43 -static void handle_server_output(int);
44 +static void handle_channels_input(Channel *);
45 +static void handle_server_output(void);
46 static int isnumeric(const char *);
47 -static void loginkey(int, const char *);
48 -static void loginuser(int, const char *, const char *);
49 -static void proc_channels_input(int, Channel *, char *);
50 -static void proc_channels_privmsg(int, Channel *, char *);
51 -static void proc_server_cmd(int, char *);
52 +static void loginkey(const char *);
53 +static void loginuser(const char *, const char *);
54 +static void proc_channels_input(Channel *, char *);
55 +static void proc_channels_privmsg(Channel *, char *);
56 +static void proc_server_cmd(char *);
57 static int read_line(int, char *, size_t);
58 -static void run(int, const char *);
59 +static void run(const char *);
60 static void setup(void);
61 static void sighandler(int);
62 static int tcpopen(const char *, const char *);
63 @@ -341,73 +341,19 @@ channel_leave(Channel *c)
64 }
65
66 static void
67 -loginkey(int ircfd, const char *key)
68 +loginkey(const char *key)
69 {
70 snprintf(msg, sizeof(msg), "PASS %s\r\n", key);
71 - ewritestr(ircfd, msg);
72 + ewritestr(WRITE_FD, msg);
73 }
74
75 static void
76 -loginuser(int ircfd, const char *host, const char *fullname)
77 +loginuser(const char *host, const char *fullname)
78 {
79 snprintf(msg, sizeof(msg), "NICK %s\r\nUSER %s localhost %s :%s\r\n",
80 nick, nick, host, fullname);
81 puts(msg);
82 - ewritestr(ircfd, msg);
83 -}
84 -
85 -static int
86 -udsopen(const char *uds)
87 -{
88 - struct sockaddr_un sun;
89 - size_t len;
90 - int fd;
91 -
92 - if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
93 - die("%s: socket: %s\n", argv0, strerror(errno));
94 -
95 - sun.sun_family = AF_UNIX;
96 - if (strlcpy(sun.sun_path, uds, sizeof(sun.sun_path)) >= sizeof(sun.sun_path))
97 - die("%s: UNIX domain socket path truncation\n", argv0);
98 -
99 - len = strlen(sun.sun_path) + 1 + sizeof(sun.sun_family);
100 - if (connect(fd, (struct sockaddr *)&sun, len) == -1)
101 - die("%s: connect: %s\n", argv0, strerror(errno));
102 -
103 - return fd;
104 -}
105 -
106 -static int
107 -tcpopen(const char *host, const char *service)
108 -{
109 - struct addrinfo hints, *res = NULL, *rp;
110 - int fd = -1, e;
111 -
112 - memset(&hints, 0, sizeof(hints));
113 - hints.ai_family = AF_UNSPEC; /* allow IPv4 or IPv6 */
114 - hints.ai_flags = AI_NUMERICSERV; /* avoid name lookup for port */
115 - hints.ai_socktype = SOCK_STREAM;
116 -
117 - if ((e = getaddrinfo(host, service, &hints, &res)))
118 - die("%s: getaddrinfo: %s\n", argv0, gai_strerror(e));
119 -
120 - for (rp = res; rp; rp = rp->ai_next) {
121 - fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
122 - if (fd == -1)
123 - continue;
124 - if (connect(fd, rp->ai_addr, rp->ai_addrlen) == -1) {
125 - close(fd);
126 - fd = -1;
127 - continue;
128 - }
129 - break; /* success */
130 - }
131 - if (fd == -1)
132 - die("%s: could not connect to %s:%s: %s\n",
133 - argv0, host, service, strerror(errno));
134 -
135 - freeaddrinfo(res);
136 - return fd;
137 + ewritestr(WRITE_FD, msg);
138 }
139
140 static int
141 @@ -459,16 +405,16 @@ channel_print(Channel *c, const char *buf)
142 }
143
144 static void
145 -proc_channels_privmsg(int ircfd, Channel *c, char *buf)
146 +proc_channels_privmsg(Channel *c, char *buf)
147 {
148 snprintf(msg, sizeof(msg), "<%s> %s", nick, buf);
149 channel_print(c, msg);
150 snprintf(msg, sizeof(msg), "PRIVMSG %s :%s\r\n", c->name, buf);
151 - ewritestr(ircfd, msg);
152 + ewritestr(WRITE_FD, msg);
153 }
154
155 static void
156 -proc_channels_input(int ircfd, Channel *c, char *buf)
157 +proc_channels_input(Channel *c, char *buf)
158 {
159 char *p = NULL;
160 size_t buflen;
161 @@ -476,7 +422,7 @@ proc_channels_input(int ircfd, Channel *c, char *buf)
162 if (buf[0] == '\0')
163 return;
164 if (buf[0] != '/') {
165 - proc_channels_privmsg(ircfd, c, buf);
166 + proc_channels_privmsg(c, buf);
167 return;
168 }
169
170 @@ -501,7 +447,7 @@ proc_channels_input(int ircfd, Channel *c, char *buf)
171 channel_join(&buf[3]);
172 } else if (p) {
173 if ((c = channel_join(&buf[3])))
174 - proc_channels_privmsg(ircfd, c, p + 1);
175 + proc_channels_privmsg(c, p + 1);
176 return;
177 }
178 break;
179 @@ -533,7 +479,7 @@ proc_channels_input(int ircfd, Channel *c, char *buf)
180 else
181 snprintf(msg, sizeof(msg),
182 "PART %s :leaving\r\n", c->name);
183 - ewritestr(ircfd, msg);
184 + ewritestr(WRITE_FD, msg);
185 channel_leave(c);
186 return;
187 break;
188 @@ -543,7 +489,7 @@ proc_channels_input(int ircfd, Channel *c, char *buf)
189 else
190 snprintf(msg, sizeof(msg),
191 "QUIT %s\r\n", "bye");
192 - ewritestr(ircfd, msg);
193 + ewritestr(WRITE_FD, msg);
194 isrunning = 0;
195 return;
196 break;
197 @@ -556,11 +502,11 @@ proc_channels_input(int ircfd, Channel *c, char *buf)
198 snprintf(msg, sizeof(msg), "%s\r\n", &buf[1]);
199 }
200 if (msg[0] != '\0')
201 - ewritestr(ircfd, msg);
202 + ewritestr(WRITE_FD, msg);
203 }
204
205 static void
206 -proc_server_cmd(int fd, char *buf)
207 +proc_server_cmd(char *buf)
208 {
209 Channel *c;
210 const char *channel;
211 @@ -608,7 +554,7 @@ proc_server_cmd(int fd, char *buf)
212 return;
213 } else if (!strcmp("PING", argv[TOK_CMD])) {
214 snprintf(msg, sizeof(msg), "PONG %s\r\n", argv[TOK_TEXT]);
215 - ewritestr(fd, msg);
216 + ewritestr(WRITE_FD, msg);
217 return;
218 } else if (!argv[TOK_NICKSRV] || !argv[TOK_USER]) {
219 /* server command */
220 @@ -695,7 +641,7 @@ read_line(int fd, char *buf, size_t bufsiz)
221 }
222
223 static void
224 -handle_channels_input(int ircfd, Channel *c)
225 +handle_channels_input(Channel *c)
226 {
227 /*
228 * Do not allow to read this fully, since commands will be
229 @@ -711,20 +657,19 @@ handle_channels_input(int ircfd, Channel *c)
230 channel_rm(c);
231 return;
232 }
233 - proc_channels_input(ircfd, c, buf);
234 + proc_channels_input(c, buf);
235 }
236
237 static void
238 -handle_server_output(int ircfd)
239 +handle_server_output(void)
240 {
241 char buf[IRC_MSG_MAX];
242
243 - if (read_line(ircfd, buf, sizeof(buf)) == -1)
244 + if (read_line(READ_FD, buf, sizeof(buf)) == -1)
245 die("%s: remote host closed connection: %s\n", argv0, strerror(errno));
246 -
247 fprintf(stdout, "%lu %s\n", (unsigned long)time(NULL), buf);
248 fflush(stdout);
249 - proc_server_cmd(ircfd, buf);
250 + proc_server_cmd(buf);
251 }
252
253 static void
254 @@ -746,7 +691,7 @@ setup(void)
255 }
256
257 static void
258 -run(int ircfd, const char *host)
259 +run(const char *host)
260 {
261 Channel *c, *tmp;
262 fd_set rdset;
263 @@ -756,9 +701,9 @@ run(int ircfd, const char *host)
264
265 snprintf(ping_msg, sizeof(ping_msg), "PING %s\r\n", host);
266 while (isrunning) {
267 - maxfd = ircfd;
268 + maxfd = READ_FD;
269 FD_ZERO(&rdset);
270 - FD_SET(ircfd, &rdset);
271 + FD_SET(READ_FD, &rdset);
272 for (c = channels; c; c = c->next) {
273 if (c->fdin > maxfd)
274 maxfd = c->fdin;
275 @@ -777,17 +722,17 @@ run(int ircfd, const char *host)
276 cleanup();
277 exit(2); /* status code 2 for timeout */
278 }
279 - ewritestr(ircfd, ping_msg);
280 + ewritestr(WRITE_FD, ping_msg);
281 continue;
282 }
283 - if (FD_ISSET(ircfd, &rdset)) {
284 - handle_server_output(ircfd);
285 + if (FD_ISSET(READ_FD, &rdset)) {
286 + handle_server_output();
287 last_response = time(NULL);
288 }
289 for (c = channels; c; c = tmp) {
290 tmp = c->next;
291 if (FD_ISSET(c->fdin, &rdset))
292 - handle_channels_input(ircfd, c);
293 + handle_channels_input(c);
294 }
295 }
296 }
297 @@ -799,7 +744,7 @@ main(int argc, char *argv[])
298 const char *key = NULL, *fullname = NULL, *host = "";
299 const char *uds = NULL, *service = "6667";
300 char prefix[PATH_MAX];
301 - int ircfd, r;
302 + int r;
303
304 /* use nickname and home dir of user by default */
305 if (!(spw = getpwuid(getuid())))
306 @@ -838,11 +783,6 @@ main(int argc, char *argv[])
307 if (!*host)
308 usage();
309
310 - if (uds)
311 - ircfd = udsopen(uds);
312 - else
313 - ircfd = tcpopen(host, service);
314 -
315 #ifdef __OpenBSD__
316 /* OpenBSD pledge(2) support */
317 if (pledge("stdio rpath wpath cpath dpath", NULL) == -1)
318 @@ -856,10 +796,10 @@ main(int argc, char *argv[])
319
320 channelmaster = channel_add(""); /* master channel */
321 if (key)
322 - loginkey(ircfd, key);
323 - loginuser(ircfd, host, fullname && *fullname ? fullname : nick);
324 + loginkey(key);
325 + loginuser(host, fullname && *fullname ? fullname : nick);
326 setup();
327 - run(ircfd, host);
328 + run(host);
329 cleanup();
330
331 return 0;