home *** CD-ROM | disk | FTP | other *** search
- /* piped.c PiPELiNE v0.3a */
- /* by BlindPoet (Tiago Rodrigues) */
- /* */
- /* 7/4/97 -> Dataflow logged to 'slog' */
- /* 8/4/97 -> Commented source & improved logs */
- /* 9/4/97 -> Logfile is no longer killed upon restart */
- /* 10/4/97 -> HEADACHE */
- /* 11/4/97 -> Added SpyPort :) Please, try to avoid to use */
- /* this feature for unethical purposes */
-
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <sys/wait.h>
- #include <netinet/in.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <errno.h>
- #include <unistd.h>
- #include <netdb.h>
-
- #ifdef linux
- #include <linux/time.h>
- #elsif
- #define STRERROR
- #endif
-
- #define mysig1 "piped - PiPELiNE v0.3a"
- #define mysig2 "by: BlindPoet (Tiago Rodrigues)"
-
- #ifdef STRERROR
- extern char *sys_errlist[];
- extern int sys_nerr;
- char *undef = "Undefined error";
-
- char *strerror(error)
- int error;
- {
- if (error > sys_nerr)
- return undef;
- return sys_errlist[error];
- }
- #endif
-
- main(argc, argv)
- int argc;
- char **argv;
- {
- int lsock, csock, osock; /* lsock=listen / csock=client / osock=outgoing */
- int ssock, dsock;
- FILE *cfile, *p_fich; /* cfile=client_sock / p_fich = dataflow_log */
- FILE *dfile;
- char buf[4096];
- int aaa=4096;
- struct sockaddr_in laddr, caddr, oaddr;
- struct sockaddr_in saddr, daddr;
- int caddrlen = sizeof(caddr); /* &fromlen (accept) */
- int daddrlen = sizeof(daddr);
- fd_set fdsr, fdse; /* file descriptors */
- struct hostent *h;
- struct servent *s;
- struct hostent *t;
- int nbyt, nbyt2;
- unsigned long a;
- unsigned short oport;
-
- if (argc < 5) {
- fprintf(stderr,"Usage: %s localport remoteport remotehost spyport\n",argv[0]);
- return 30;
- }
-
- a = inet_addr(argv[3]); /* hostname */
- if (!(h = gethostbyname(argv[3])) &&
- !(h = gethostbyaddr(&a, 4, AF_INET))) {
- perror(argv[3]);
- return 25;
- }
-
- if ((p_fich = fopen ("slog", "a+")) == NULL)
- {
- printf("PANIC: cannot open log file");
- return -1;
- }
-
- oport = atol(argv[2]); /* remote port */
- laddr.sin_port = htons((unsigned short)(atol(argv[1]))); /* local port */
- saddr.sin_port = htons((unsigned short)(atol(argv[4]))); /* spy port */
-
- if ((ssock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
- perror("socket"); /* inet socket */
- return 20;
- }
-
- saddr.sin_family = htons(AF_INET); /* domain */
- saddr.sin_addr.s_addr = htonl(0);
- /* bind(s, name, namelen) */
- if (bind(ssock, &saddr, sizeof(saddr))) { /* bind */
- perror("bind");
- return 20;
- }
- /* listen(s, max_num_of_connections); */
- if (listen(ssock, 1)) { /* listen */
- perror("listen");
- return 20;
- }
-
- if ((nbyt2 = fork()) == -1) {
- perror("fork");
- return 20;
- }
-
-
- if (nbyt2 > 0)
- return 0;
- setsid();
- /* fromlen = sizeof (from); */
- /* newsock = accept(s, (struct sockaddr *)&from, &fromlen); */
- while ((dsock = accept(ssock, &daddr, &daddrlen)) != -1) {
- dfile = fdopen(dsock,"r+"); /* open new r+ */
- fprintf(p_fich, "|piped| -> Client connecting on spy port...\n");
- if ((nbyt2 = fork()) == -1) { /* fork new */
- fprintf(dfile, "500 fork: %s\n", strerror(errno));
- shutdown(dsock,2); /* end r/w */
- fclose(dfile); /* discard */
- continue;
- }
- if (nbyt2 == 0) /* ok? */
- goto sec; /* yeah.. */
- fclose(dfile); /* nope! close */
- while (waitpid(-1, NULL, WNOHANG) > 0); /* kill child? */
- }
- /* return 20; */
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- sec:
-
- /* s = socket(domain, type, protocol); */
- if ((lsock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
- perror("socket"); /* inet socket */
- return 20;
- }
-
- laddr.sin_family = htons(AF_INET); /* domain */
- laddr.sin_addr.s_addr = htonl(0);
- /* bind(s, name, namelen) */
- if (bind(lsock, &laddr, sizeof(laddr))) { /* bind */
- perror("bind");
- return 20;
- }
- /* listen(s, max_num_of_connections); */
- if (listen(lsock, 1)) { /* listen */
- perror("listen");
- return 20;
- }
- /* */ if ((nbyt = fork()) == -1) {
- perror("fork");
- return 20;
- }
-
- if (nbyt > 0)
- return 0;
- setsid();
- /* fromlen = sizeof (from); */
- /* newsock = accept(s, (struct sockaddr *)&from, &fromlen); */
- while ((csock = accept(lsock, &caddr, &caddrlen)) != -1) {
- cfile = fdopen(csock,"r+"); /* open new r+ */
- fprintf(p_fich, "|piped| -> Client connecting...\n");
- if ((nbyt = fork()) == -1) { /* fork new */
- fprintf(cfile, "500 fork: %s\n", strerror(errno));
- shutdown(csock,2); /* end r/w */
- fclose(cfile); /* discard */
- continue;
- }
- if (nbyt == 0) /* ok? */
- goto gotsock; /* yeah.. */
- fclose(cfile); /* nope! close */
- while (waitpid(-1, NULL, WNOHANG) > 0); /* kill child? */
- }
- return 20;
-
- gotsock:
- if ((osock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
- fprintf(cfile, "500 socket: %s\n", strerror(errno)); /* rmote sock */
- goto quit1;
- }
- oaddr.sin_family = h->h_addrtype;
- oaddr.sin_port = htons(oport);
- memcpy(&oaddr.sin_addr, h->h_addr, h->h_length);
-
- /* fprintf(p_fich, "|piped| -> Client connection from...: %s \n", caddr.sin_addr); */
-
- /* connect(s, (struct sockaddr *)&server, sizeof (server)); */
- if (connect(osock, &oaddr, sizeof(oaddr))) { /* connect to */
- fprintf(cfile, "500 connect: %s\n", strerror(errno)); /* rmote sock */
- goto quit1;
- }
- while (1) {
- FD_ZERO(&fdsr); /* zero file descriptors before use */
- FD_ZERO(&fdse);
- FD_SET(csock,&fdsr); /* set fd masks */
- FD_SET(csock,&fdse);
- FD_SET(osock,&fdsr);
- FD_SET(osock,&fdse);
- /* I/O multiplexing */
- /* nfds=range of file descriptors */
- /* select(nfds, &readmask, &writemask, &exceptmask, &timeout); */
- if (select(20, &fdsr, NULL, &fdse, NULL) == -1) {
- fprintf(cfile, "500 select: %s\n", strerror(errno));
- goto quit2;
- }
- if (FD_ISSET(csock,&fdsr) || FD_ISSET(csock,&fdse)) {
- if ((nbyt = read(csock,buf,aaa)) <= 0) /* read clnt */
- goto quit2;
-
- fwrite(buf, sizeof(char), nbyt, p_fich); /* write log */
- fflush(p_fich);
-
- /* write(s, buf, sizeof (buf)); */
- /* read(s, buf, sizeof (buf)); */
- if ((write(osock,buf,nbyt)) <= 0) /* write remt */
- goto quit2;
- } else if (FD_ISSET(osock,&fdsr) || FD_ISSET(osock,&fdse)) {
- if ((nbyt = read(osock,buf,aaa)) <= 0) /* read remt */
- goto quit2;
-
- fwrite(buf, sizeof(char), nbyt, p_fich); /* write log */
- fflush(p_fich);
-
- if ((write(dsock,buf,nbyt)) <= 0)
- printf("Unable to write on spy socket...");
- if ((write(csock,buf,nbyt)) <= 0) /* write clnt */
- goto quit2;
- }
- }
-
-
-
- quit2:
- shutdown(osock,2); /* no more r/w on sock */
- close(osock); /* discard remote sock */
- quit1:
- fflush(cfile); /* flush sock */
- shutdown(csock,2); /* no more r/w on sock */
- quit0:
- fclose(cfile); /* discard client sock */
-
- fprintf(p_fich, "|piped| -> Closing connection...\n");
-
- fflush (p_fich); /* flush dataflow log file */
- fclose (p_fich); /* close dataflow log file */
-
- return 0; /* the_end :) */
- }
-