1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
|
/*
* ircio.c: A quaint little program to make irc life PING free
*
* Written By Michael Sandrof
*
* Copyright(c) 1990
*
* See the COPYRIGHT file, or do a HELP IRCII COPYRIGHT
*/
#ifndef lint
static char rcsid[] = "@(#)$Id: ircio.c,v 2.8 1997/01/17 19:53:51 mrg Exp $";
#endif /* lint */
#include "defs.h"
#include <sys/types.h>
#include <stdio.h>
#include <sys/socket.h>
#ifdef HAVE_SYS_SELECT_H
# include <sys/select.h>
#endif /* HAVE_SYS_SELECT_H */
#if defined(HAVE_UNISTD_H) && !defined(pyr) && !defined(_SEQUENT_)
# include <unistd.h>
#endif /* HAVE_UNISTD_H && !pyr && !_SEQUENT_ */
#include <sys/file.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <errno.h>
#include <signal.h>
#ifdef TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# ifdef HAVE_SYS_TIME_H
# include <sys/time.h>
# else
# include <time.h>
# endif /* HAVE_SYS_TIME_H */
#endif /* TIME_WITH_SYS_TIME */
#include "irc_std.h"
#include "newio.h"
/* machines we don't want to use <unistd.h> on 'cause its broken */
#if defined(pyr) || defined(_SEQUENT_)
# undef HAVE_UNISTD_H
#endif /* pyr || _SEQUENT_ */
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif /* HAVE_UNISTD_H */
#ifdef HAVE_SYS_UN_H
#include <sys/un.h>
static int connect_to_unix _((char *));
#endif /* HAVE_SYS_UN_H */
#undef NON_BLOCKING
void new_free _((char **));
char *new_malloc _((int));
static int connect_by_number _((int, char *));
char *
new_malloc(size)
int size;
{
char *ptr;
if ((ptr = (char *) malloc(size)) == (char *) 0)
{
printf("-1 0\n");
exit(1);
}
return (ptr);
}
/*
* new_free: Why do this? Why not? Saves me a bit of trouble here and
* there
*/
void
new_free(ptr)
char **ptr;
{
if (*ptr)
{
free(*ptr);
*ptr = 0;
}
}
/*
* Connect_By_Number Performs a connecting to socket 'service' on host
* 'host'. Host can be a hostname or ip-address. If 'host' is null, the
* local host is assumed. The parameter full_hostname will, on return,
* contain the expanded hostname (if possible). Note that full_hostname is a
* pointer to a char *, and is allocated by connect_by_numbers()
*
* Errors:
*
* -1 get service failed
*
* -2 get host failed
*
* -3 socket call failed
*
* -4 connect call failed
*/
static int
connect_by_number(service, host)
int service;
char *host;
{
int s;
char buf[100];
struct sockaddr_in server;
struct hostent *hp;
if (host == (char *) 0)
{
gethostname(buf, sizeof(buf));
host = buf;
}
if ((server.sin_addr.s_addr = inet_addr(host)) == -1)
{
if ((hp = gethostbyname(host)) != NULL)
{
bzero((char *) &server, sizeof(server));
bcopy(hp->h_addr, (char *) &server.sin_addr,
hp->h_length);
server.sin_family = hp->h_addrtype;
}
else
return (-2);
}
else
server.sin_family = AF_INET;
server.sin_port = (unsigned short) htons(service);
if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
return (-3);
set_socket_options(s);
#ifdef PRIV_PORT
if (geteuid() == 0)
{
struct sockaddr_in localaddr;
int portnum;
localaddr = server;
localaddr.sin_addr.s_addr = INADDR_ANY;
for (portnum = 1023; portnum > PRIV_PORT; portnum--)
{
localaddr.sin_port = htons(portnum);
if (bind(s, (struct sockaddr *) &localaddr,
sizeof(localaddr)) != -1)
break;
}
}
#endif /*PRIV_PORT*/
if (connect(s, (struct sockaddr *) &server, sizeof(server)) < 0)
{
new_close(s);
return (-4);
}
return (s);
}
/*
* ircio: This little program connects to the server (given as arg 1) on
* the given port (given as arg 2). It then accepts input from stdin and
* sends it to that server. Likewise, it reads stuff sent from the server and
* sends it to stdout. Simple? Yes, it is. But wait! There's more! It
* also intercepts server PINGs and automatically responds to them. This
* frees up the process that starts ircio (such as IRCII) to pause without
* fear of being pooted off the net.
*
* Future enhancements: No-blocking io. It will either discard or dynamically
* buffer anything that would block.
*/
int
main(argc, argv)
int argc;
char **argv;
{
int des;
fd_set rd;
int done = 0,
c;
char *ptr,
buffer[BUFSIZ + 1],
pong[BUFSIZ + 1];
#ifdef NON_BLOCKING
char block_buffer[BUFSIZ + 1];
fd_set *wd_ptr = (fd_set *) 0,
wd;
int wrote;
#endif /* NON_BLOCKING */
if (argc < 3)
exit(1);
#ifdef SOCKS
SOCKSinit(*argv);
#endif /* SOCKS */
#ifdef HAVE_SYS_UN_H
if (*argv[1] == '/')
des = connect_to_unix(argv[1]);
else
#endif /* HAVE_SYS_UN_H */
des = connect_by_number(atoi(argv[2]), argv[1]);
if (des < 0)
exit(des);
fflush(stdout);
(void) MY_SIGNAL(SIGTERM, (sigfunc *) SIG_IGN, 0);
(void) MY_SIGNAL(SIGSEGV, (sigfunc *) SIG_IGN, 0);
(void) MY_SIGNAL(SIGPIPE, (sigfunc *) SIG_IGN, 0);
#ifdef SIGWINCH
(void) MY_SIGNAL(SIGWINCH, (sigfunc *) SIG_IGN, 0);
#endif /* SIGWINCH */
#ifdef SIGBUS
(void) MY_SIGNAL(SIGBUS, (sigfunc *) SIG_IGN, 0);
#endif /* SIGBUS */
#ifdef NON_BLOCKING
if (fcntl(1, F_SETFL, FNDELAY))
exit(1);
#endif /* NON_BLOCKING */
while (!done)
{
fflush(stderr);
FD_ZERO(&rd);
FD_SET(0, &rd);
FD_SET(des, &rd);
#ifdef NON_BLOCKING
if (wd_ptr)
{
FD_ZERO(wd_ptr);
FD_SET(1, wd_ptr);
}
switch (new_select(&rd, wd_ptr, NULL))
{
#else
switch (new_select(&rd, (fd_set *) 0, NULL))
{
#endif /* NON_BLOCKING */
case -1:
case 0:
break;
default:
#ifdef NON_BLOCKING
if (wd_ptr)
{
if (FD_ISSET(1, wd_ptr))
{
c = strlen(block_buffer);
if ((wrote = write(1, block_buffer,
c)) == -1)
{
wd_ptr = &wd;
}
else if (wrote < c)
{
strcpy(block_buffer,
&(block_buffer[wrote]));
wd_ptr = &wd;
}
else
wd_ptr = (fd_set *) 0;
}
}
#endif /* NON_BLOCKING */
if (FD_ISSET(0, &rd))
{
if (0 != (c = dgets(buffer, BUFSIZ, 0,
(char *) 0)))
write(des, buffer, c);
else
done = 1;
}
if (FD_ISSET(des, &rd))
{
if (0 != (c = dgets(buffer, BUFSIZ, des,
(char *) 0)))
{
if (strncmp(buffer, "PING ", 5) == 0)
{
if ((ptr = (char *)
index(buffer, ' ')) != NULL)
{
sprintf(pong, "PONG user@host %s\n", ptr + 1);
write(des, pong, strlen(pong));
}
}
else
{
#ifdef NON_BLOCKING
if ((wrote = write(1, buffer,
c)) == -1)
wd_ptr = &wd;
else if (wrote < c)
{
strcpy(block_buffer,
&(buffer[wrote]));
wd_ptr = &wd;
}
#else
write(1, buffer, c);
#endif /* NON_BLOCKING */
}
}
else
done = 1;
}
}
}
}
#ifdef HAVE_SYS_UN_H
/*
* Connect to a UNIX domain socket. Only works for servers.
* submitted by Avalon for use with server 2.7.2 and beyond.
*/
static int
connect_to_unix(path)
char *path;
{
struct sockaddr_un un;
int sock;
sock = socket(AF_UNIX, SOCK_STREAM, 0);
un.sun_family = AF_UNIX;
strcpy(un.sun_path, path);
if (connect(sock, (struct sockaddr *)&un, strlen(path)+2) == -1)
{
new_close(sock);
return -1;
}
return sock;
}
#endif /* HAVE_SYS_UN_H */
|