commit 42a582d675b09f387b154f8149c0c75e303955e0 Author: Jan Klemkow Date: Sat Sep 2 12:36:34 2017 +0200 replace socket handling with ucspi backend diff --git a/ii.c b/ii.c index c402a87..193b0b9 100644 --- a/ii.c +++ b/ii.c @@ -1,16 +1,13 @@ /* See LICENSE file for license details. */ #include -#include #include #include -#include #include #include #include #include #include -#include #include #include #include @@ -20,6 +17,9 @@ #include #include +#define READ_FD 6 +#define WRITE_FD 7 + char *argv0; #include "arg.h" @@ -59,16 +59,16 @@ static void create_dirtree(const char *); static void create_filepath(char *, size_t, const char *, const char *, const char *); static void die(const char *, ...); static void ewritestr(int, const char *); -static void handle_channels_input(int, Channel *); -static void handle_server_output(int); +static void handle_channels_input(Channel *); +static void handle_server_output(void); static int isnumeric(const char *); -static void loginkey(int, const char *); -static void loginuser(int, const char *, const char *); -static void proc_channels_input(int, Channel *, char *); -static void proc_channels_privmsg(int, Channel *, char *); -static void proc_server_cmd(int, char *); +static void loginkey(const char *); +static void loginuser(const char *, const char *); +static void proc_channels_input(Channel *, char *); +static void proc_channels_privmsg(Channel *, char *); +static void proc_server_cmd(char *); static int read_line(int, char *, size_t); -static void run(int, const char *); +static void run(const char *); static void setup(void); static void sighandler(int); static int tcpopen(const char *, const char *); @@ -341,73 +341,19 @@ channel_leave(Channel *c) } static void -loginkey(int ircfd, const char *key) +loginkey(const char *key) { snprintf(msg, sizeof(msg), "PASS %s\r\n", key); - ewritestr(ircfd, msg); + ewritestr(WRITE_FD, msg); } static void -loginuser(int ircfd, const char *host, const char *fullname) +loginuser(const char *host, const char *fullname) { snprintf(msg, sizeof(msg), "NICK %s\r\nUSER %s localhost %s :%s\r\n", nick, nick, host, fullname); puts(msg); - ewritestr(ircfd, msg); -} - -static int -udsopen(const char *uds) -{ - struct sockaddr_un sun; - size_t len; - int fd; - - if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) - die("%s: socket: %s\n", argv0, strerror(errno)); - - sun.sun_family = AF_UNIX; - if (strlcpy(sun.sun_path, uds, sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) - die("%s: UNIX domain socket path truncation\n", argv0); - - len = strlen(sun.sun_path) + 1 + sizeof(sun.sun_family); - if (connect(fd, (struct sockaddr *)&sun, len) == -1) - die("%s: connect: %s\n", argv0, strerror(errno)); - - return fd; -} - -static int -tcpopen(const char *host, const char *service) -{ - struct addrinfo hints, *res = NULL, *rp; - int fd = -1, e; - - memset(&hints, 0, sizeof(hints)); - hints.ai_family = AF_UNSPEC; /* allow IPv4 or IPv6 */ - hints.ai_flags = AI_NUMERICSERV; /* avoid name lookup for port */ - hints.ai_socktype = SOCK_STREAM; - - if ((e = getaddrinfo(host, service, &hints, &res))) - die("%s: getaddrinfo: %s\n", argv0, gai_strerror(e)); - - for (rp = res; rp; rp = rp->ai_next) { - fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); - if (fd == -1) - continue; - if (connect(fd, rp->ai_addr, rp->ai_addrlen) == -1) { - close(fd); - fd = -1; - continue; - } - break; /* success */ - } - if (fd == -1) - die("%s: could not connect to %s:%s: %s\n", - argv0, host, service, strerror(errno)); - - freeaddrinfo(res); - return fd; + ewritestr(WRITE_FD, msg); } static int @@ -459,16 +405,16 @@ channel_print(Channel *c, const char *buf) } static void -proc_channels_privmsg(int ircfd, Channel *c, char *buf) +proc_channels_privmsg(Channel *c, char *buf) { snprintf(msg, sizeof(msg), "<%s> %s", nick, buf); channel_print(c, msg); snprintf(msg, sizeof(msg), "PRIVMSG %s :%s\r\n", c->name, buf); - ewritestr(ircfd, msg); + ewritestr(WRITE_FD, msg); } static void -proc_channels_input(int ircfd, Channel *c, char *buf) +proc_channels_input(Channel *c, char *buf) { char *p = NULL; size_t buflen; @@ -476,7 +422,7 @@ proc_channels_input(int ircfd, Channel *c, char *buf) if (buf[0] == '\0') return; if (buf[0] != '/') { - proc_channels_privmsg(ircfd, c, buf); + proc_channels_privmsg(c, buf); return; } @@ -501,7 +447,7 @@ proc_channels_input(int ircfd, Channel *c, char *buf) channel_join(&buf[3]); } else if (p) { if ((c = channel_join(&buf[3]))) - proc_channels_privmsg(ircfd, c, p + 1); + proc_channels_privmsg(c, p + 1); return; } break; @@ -533,7 +479,7 @@ proc_channels_input(int ircfd, Channel *c, char *buf) else snprintf(msg, sizeof(msg), "PART %s :leaving\r\n", c->name); - ewritestr(ircfd, msg); + ewritestr(WRITE_FD, msg); channel_leave(c); return; break; @@ -543,7 +489,7 @@ proc_channels_input(int ircfd, Channel *c, char *buf) else snprintf(msg, sizeof(msg), "QUIT %s\r\n", "bye"); - ewritestr(ircfd, msg); + ewritestr(WRITE_FD, msg); isrunning = 0; return; break; @@ -556,11 +502,11 @@ proc_channels_input(int ircfd, Channel *c, char *buf) snprintf(msg, sizeof(msg), "%s\r\n", &buf[1]); } if (msg[0] != '\0') - ewritestr(ircfd, msg); + ewritestr(WRITE_FD, msg); } static void -proc_server_cmd(int fd, char *buf) +proc_server_cmd(char *buf) { Channel *c; const char *channel; @@ -608,7 +554,7 @@ proc_server_cmd(int fd, char *buf) return; } else if (!strcmp("PING", argv[TOK_CMD])) { snprintf(msg, sizeof(msg), "PONG %s\r\n", argv[TOK_TEXT]); - ewritestr(fd, msg); + ewritestr(WRITE_FD, msg); return; } else if (!argv[TOK_NICKSRV] || !argv[TOK_USER]) { /* server command */ @@ -695,7 +641,7 @@ read_line(int fd, char *buf, size_t bufsiz) } static void -handle_channels_input(int ircfd, Channel *c) +handle_channels_input(Channel *c) { /* * Do not allow to read this fully, since commands will be @@ -711,20 +657,19 @@ handle_channels_input(int ircfd, Channel *c) channel_rm(c); return; } - proc_channels_input(ircfd, c, buf); + proc_channels_input(c, buf); } static void -handle_server_output(int ircfd) +handle_server_output(void) { char buf[IRC_MSG_MAX]; - if (read_line(ircfd, buf, sizeof(buf)) == -1) + if (read_line(READ_FD, buf, sizeof(buf)) == -1) die("%s: remote host closed connection: %s\n", argv0, strerror(errno)); - fprintf(stdout, "%lu %s\n", (unsigned long)time(NULL), buf); fflush(stdout); - proc_server_cmd(ircfd, buf); + proc_server_cmd(buf); } static void @@ -746,7 +691,7 @@ setup(void) } static void -run(int ircfd, const char *host) +run(const char *host) { Channel *c, *tmp; fd_set rdset; @@ -756,9 +701,9 @@ run(int ircfd, const char *host) snprintf(ping_msg, sizeof(ping_msg), "PING %s\r\n", host); while (isrunning) { - maxfd = ircfd; + maxfd = READ_FD; FD_ZERO(&rdset); - FD_SET(ircfd, &rdset); + FD_SET(READ_FD, &rdset); for (c = channels; c; c = c->next) { if (c->fdin > maxfd) maxfd = c->fdin; @@ -777,17 +722,17 @@ run(int ircfd, const char *host) cleanup(); exit(2); /* status code 2 for timeout */ } - ewritestr(ircfd, ping_msg); + ewritestr(WRITE_FD, ping_msg); continue; } - if (FD_ISSET(ircfd, &rdset)) { - handle_server_output(ircfd); + if (FD_ISSET(READ_FD, &rdset)) { + handle_server_output(); last_response = time(NULL); } for (c = channels; c; c = tmp) { tmp = c->next; if (FD_ISSET(c->fdin, &rdset)) - handle_channels_input(ircfd, c); + handle_channels_input(c); } } } @@ -799,7 +744,7 @@ main(int argc, char *argv[]) const char *key = NULL, *fullname = NULL, *host = ""; const char *uds = NULL, *service = "6667"; char prefix[PATH_MAX]; - int ircfd, r; + int r; /* use nickname and home dir of user by default */ if (!(spw = getpwuid(getuid()))) @@ -838,11 +783,6 @@ main(int argc, char *argv[]) if (!*host) usage(); - if (uds) - ircfd = udsopen(uds); - else - ircfd = tcpopen(host, service); - #ifdef __OpenBSD__ /* OpenBSD pledge(2) support */ if (pledge("stdio rpath wpath cpath dpath", NULL) == -1) @@ -856,10 +796,10 @@ main(int argc, char *argv[]) channelmaster = channel_add(""); /* master channel */ if (key) - loginkey(ircfd, key); - loginuser(ircfd, host, fullname && *fullname ? fullname : nick); + loginkey(key); + loginuser(host, fullname && *fullname ? fullname : nick); setup(); - run(ircfd, host); + run(host); cleanup(); return 0;