⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mrelayd.c

📁 The Javatm Telnet Application/Applet 很好用的
💻 C
📖 第 1 页 / 共 2 页
字号:
/** * mrelayd.c -- a relay daemon * -- * $Id: mrelayd.c,v 1.10 2002/10/26 07:15:59 leo Exp $ * * (c) Matthias L. Jugel, Marcus Mei遪er 1996-2002. All Rights Reserved. * * Please visit http://javatelnet.org/ for updates and contact. * * --LICENSE NOTICE-- * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. *  * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * --LICENSE NOTICE-- * *//* mrelayd.c (c) 1996,1997 Marcus Meissner <marcus@mud.de> *//* Compile with: cc -o relayd relayd.c *           or: gcc -o relayd relayd.c * Solaris: (g)cc -o relayd relayd.c -lsocket -lnsl *//* this program does not except any arguments, just start it: * 	./mrelayd * The applet will pass it "relay targethost targetport" as first line * after which mrelayd will connect to the target host. * mrelayd listens on port 31415 (think PI). this can only be changed below. *//* adjust this to a reasonable limit */#define MAXUSERS  120/* message printed if all slots are used ... */#define FAILMESSAGE "Sorry, all slots are full.\r\n"/* string printed before connection *//* empty, or you cannot relay ssh */#define RELAYHEADER ""/* the tcp port this demons is listening on ... */#define LISTENPORT  31415/* default connect port (telnet) */#define DEFAULTPORT 23/* default buffersize */#define DEFAULTSIZE	2000#include <stdio.h>#ifdef _WIN32#include <winsock.h>#include <signal.h>#define ioctl ioctlsocket#define read(a,b,c) recv(a,b,c,0)#define write(a,b,c) send(a,b,c,0)#define close _lclose#define EINTR WSAEINTR#define perror xperrorvoid xperror(char *s) {	fprintf(stderr,"%s: %d\n",s,GetLastError());}#else#include <sys/time.h>#include <unistd.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <netdb.h>#include <sys/errno.h>#include <sys/signal.h>#include <sys/fcntl.h>#include <sys/ioctl.h>#endif#include <memory.h>/* #include <malloc.h> */#include <string.h>#include <sys/types.h>#if defined(sun) && defined(__GNUC__)int socket(int,int,int);int shutdown(int,int);int close(int);int bind(int,struct sockaddr*,int);int listen(int,int);int select(int,fd_set *,fd_set*,fd_set*,struct timeval*);int accept(int,struct sockaddr*,int*);int connect(int,struct sockaddr*,int);int recvfrom(int,char*,int,int,struct sockaddr*,int*);/*void perror(char*); SLOWLARIS HASS*//*int sendto(int,char*,int,int,struct sockaddr*,int); SLOWLARIS HASS*/#endif#ifdef hpux/* redefinition... to avoid prototype in <time.h> */#define FD_CAST int#endif#ifndef FD_CAST#define FD_CAST fd_set#endifextern int errno;struct relay {	char	*inbuf,*outbuf;	int	infd,outfd,incur,outcur,insize,outsize;	struct	sockaddr_in	inaddr,outaddr;	int	state;#define STATE_ACCEPTED		0#define STATE_OK		1	int	flags;#define FLAG_EOF_USER		1#define FLAG_EOF_TARGET		2#define FLAG_CLOSED_TARGET	4#define FLAG_CLOSED_USER	8} *relays = NULL;int nrofrelays = 0;void*xmalloc(int size) {	void*x;	x=malloc(size);	if (!x && size) {		fprintf(stderr,"Out of memory, exiting.\n");		exit(1);	}	return x;}void*xrealloc(void *y,int size) {	void*x;	x=realloc(y,size);	if (!x && size) {		fprintf(stderr,"Out of memory, exiting.\n");		exit(1);	}	return x;}static intfd_make_nonblocking(int fd) {  int     isnonblock=0;#ifdef FIONBIO  if (!isnonblock) {    int     b;    b=1;    if (-1==ioctl(fd,FIONBIO,&b)) {      perror("ioctl FIONBIO");    } else      isnonblock=1;  }#endif#ifdef O_NDELAY  if (!isnonblock) {    int     flags;    if (-1==(flags=fcntl(fd,F_GETFL))) {      perror("fcntl F_GETFL");    } else {      flags|=O_NDELAY;      if (-1==fcntl(fd,F_SETFL,flags)) {        perror("fcntl F_SETFL  O_NDELAY");      } else        isnonblock=1;    }  }#endif#ifdef O_NONBLOCK  if (!isnonblock) {    int     flags;    if (-1==(flags=fcntl(fd,F_GETFL))) {      perror("fcntl F_GETFL");    } else {      flags|=O_NONBLOCK;      if (-1==fcntl(fd,F_SETFL,flags)) {        perror("fcntl F_SETFL  O_NONBLOCK");      } else        isnonblock=1;    }  }#endif  return isnonblock;}/* Turn into a daemon. It is easy, isn't it? */voiddaemonize() {#ifndef _WIN32     switch (fork()) {    default:	exit(0);    case -1:    	perror("fork");	exit(1);    case 0: /* parent exits */	/* close all common descriptors and reopen them with /dev/null */	close(0);close(1);close(2);	open("/dev/null",O_RDONLY);	open("/dev/null",O_WRONLY);	open("/dev/null",O_WRONLY);	setsid();	/* become session leader to detach from controlling tty */	return;	/* and return */    }#endif}voidclean_connection(struct relay *relay) {  if (!relay) return;  if (relay->outfd>=0) {    if (-1==close(relay->outfd))      perror("close");    relay->outfd=-1;  }  if (relay->infd>=0) {    if (-1==close(relay->infd))      perror("close");    relay->infd=-1;  }  free(relay->outbuf);free(relay->inbuf);  memcpy(relay,relay+1,sizeof(struct relay)*(nrofrelays-(relay-relays)-1));  relays = xrealloc(relays,sizeof(struct relay)*(--nrofrelays));}intmain(argc,argv)int argc;char  **argv;{  int i,j,res;  int acfd;  struct  sockaddr_in acsa;  char	readbuf[1000],relaystring[1000];  struct  in_addr targetaddr;#ifdef _WIN32   {	WSADATA wsad;		WSAStartup(0x0101,&wsad);  }#else  daemonize();#endif#ifdef SIGPIPE  signal(SIGPIPE,SIG_IGN);#endif#ifdef SIGHUP  signal(SIGHUP,SIG_IGN); /* don't terminate on session detach */#endif  strcpy(relaystring,FAILMESSAGE);  if (-1==(acfd=socket(PF_INET,SOCK_STREAM,0))) {    perror("socket(accept_socket)");    exit(1);  }  acsa.sin_family=AF_INET;  acsa.sin_port=htons(LISTENPORT);  acsa.sin_addr.s_addr=INADDR_ANY;#ifdef SO_REUSEADDR  {    int reuseit=1;    if (-1==setsockopt(acfd,SOL_SOCKET,SO_REUSEADDR,(char*)&reuseit,sizeof(reuseit)))	perror("setsockopt SOL_SOCKET SO_REUSEADDR");  }#endif  if (-1==bind(acfd,(struct sockaddr*)&acsa,sizeof(struct sockaddr_in))) {    perror("bind");    exit(1);  }  /* 5 is usual the maximum anyway */  if (-1==listen(acfd,5)) {    perror("listen");    exit(1);  }  while (1) {    fd_set  readfds,writefds;    int width;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -