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

📄 forward.c

📁 嵌入式TCP/IP协议栈。 C 源码
💻 C
📖 第 1 页 / 共 2 页
字号:
			 */
			cp = &m->line[strlen(m->line)] + 1;
			while(*cp != '\0' && isspace(*cp)) /* strip blanks */
			     ++cp;
			for(i=1; i<=m->nmsgs; i++)
				if(sendmsgtobbs(m, i, cp, bulletin) == -1) {
					err = 1;	/* abort */
					break;
				}
		}
		fclose(m->tfile);
		m->tfile = NULL;
		if(*oldarea != '\0')
			changearea(m,oldarea);
	}
	if(m->state == MBX_FORWARD)
		return 0;
	printf("*** Done\n");
	if((m->sid & MBX_RLI_SID))	/* disconnect if it is a W0RLI bbs */
		return domboxbye(0,NULL,m);
	return 0;
}

/* Read the forward file for a record for the connected BBS. If found,
 * return 1 if this is the right time to forward, m->tfile is left pointing
 * at the first message area to be forwarded.
 */
static int
fwdinit(m)
struct mbx *m;
{
	char host[80];
	int start = 1;
	if((m->tfile = fopen(Forwardfile,READ_TEXT)) == NULL)
		return -1;
	while(fgets(m->line,MBXLINE,m->tfile) != NULL) {
		if(*m->line == '\n')
			continue;
		/* lines starting with '-' separate the forwarding records */
		if(*m->line == '-') {
			start = 1;
			continue;
		}
		if(start) {
			start = 0;
			/* get the name of this forwarding record */
			findident(m->line,1,host);
			if(stricmp(m->name,host) == 0) {
				if(!timeok(m->line))
					break;
				/* eat the connect command line */
				fgets(m->line,MBXLINE,m->tfile);
				return 0;
			}
		}
	}
	fclose(m->tfile);
	m->tfile = NULL;
	return -1;
}
/* Read the forward file for a record for the connected BBS. If found,
 * determine if this is the right time to forward, and return the command
 * line to establish a forwarding connection. m->tfile is left pointing
 * at the first message area to be forwarded.
 */
static char *
fwdanybbs(m)
struct mbx *m;
{
	char host[80];
	int start = 1;
	if(m->tfile == NULL && (m->tfile = fopen(Forwardfile,READ_TEXT))
					== NULL)
		return NULL;
	while(fgets(m->line,MBXLINE,m->tfile) != NULL) {
		if(*m->line == '\n')
			continue;
		/* lines starting with '-' separate the forwarding records */
		if(*m->line == '-') {
			start = 1;
			continue;
		}
		if(start) {
			start = 0;
			/* get the name of this forwarding record */
			findident(m->line,1,host);
			strcpy(m->name,host);
			if(!timeok(m->line))
				continue;	/* too late or too early */
			/* get the connect command line */
			fgets(m->line,MBXLINE,m->tfile);
			return strdup(m->line);
		}
	}
	fclose(m->tfile);
	m->tfile = NULL;
	return NULL;
}

/* get any groups of four digits that specify the begin and ending hours of
 * forwarding. Returns 1 if forwarding may take place.
 */
static int
timeok(line)
char *line;
{
	char hours[80], *now;
	long t;
	int t1, t2, pos = 2;
	findident(line,pos++,hours);
	if(*hours == '\0')
		return 1;	/* no digits default to 0023, ie. anytime */
	time(&t);
	now = ctime(&t) + 11;
	*(now + 2) = '\0';
	while(*hours != '\0') {
		t1 = (*hours - '0') * 10 + (*(hours+1) - '0');
		t2 = (*(hours+2) - '0') * 10 + (*(hours+3) - '0');
		if(atoi(now) >= t1 && atoi(now) <= t2)
			return 1;		/* right in time */
		findident(line,pos++,hours);	/* get next group if any */
	}
	return 0;	/* too early or too late */
}

int
dombtimer(argc,argv,p)
int argc;
char *argv[];
void *p;
{
	if(argc < 2){
		printf("Forwarding timer: %lu/%lu\n",
		read_timer(&fwdtimer)/1000L,
		dur_timer(&fwdtimer)/1000L);
		return 0;
	}
	fwdtimer.func = (void (*)())fwdtick;/* what to call on timeout */
	fwdtimer.arg = NULL;		/* dummy value */
	set_timer(&fwdtimer,atol(argv[1])*1000L); /* set timer duration */
	start_timer(&fwdtimer);		/* and fire it up */
	return 0;
}

int
dombkick(argc,argv,p)
int argc;
char *argv[];
void *p;
{
	fwdtick(NULL);
	return 0;
}

/* called when the forward timer expires or explicitly by dombkick() */
static void
fwdtick(v)
void *v;
{
	char *cc, *cp;
	struct mbx *m;
	int i, bulletin, skip = 0;
	/* restart the timer */
	start_timer(&fwdtimer);
	if((m = newmbx()) == NULL)
		return;
	m->user = stdout;
	m->state = MBX_TRYING;
	while((cc = fwdanybbs(m)) != NULL) {
		if(isconnbbs(m)) /* already connected to this BBS, skip it */
			skip = 1;
		while(fgets(m->line,MBXLINE,m->tfile) != NULL) {
			if(*m->line == '-') {	/* end of record reached */
				skip = 0;
				break;
			}
			if((cp = strchr(m->line,' ')) != NULL)
				*cp = '\0';
			if((cp = strchr(m->line,'\t')) != NULL)
				*cp = '\0';
			if(skip || *m->line == '\0' || *m->line == '.')
				continue;
			rip(m->line);
			changearea(m,m->line);
			bulletin = isarea(m->line);	/* public area */
			/* check if there are any messages in this area
			 * that need to be forwarded.
			 */
			for(i=1; i<=m->nmsgs; i++)
				if(makecl(m, i, NULL, NULL, NULL,
				   bulletin) == 0) {
					newproc("Mbox forwarding", 2048,
						startfwd, 0, (void *)cc,
						(void *)strdup(m->name),0);
					skip = 1;
					cc = NULL;
					break;
				}
		}
		free(cc);
	}
	exitbbs(m);
}

/* returns 1 if m->name matches the name of another connected mailbox. */
static int
isconnbbs(m)
struct mbx *m;
{
	int i;
	for(i = 0; i < NUMMBX; ++i)
		if(Mbox[i] != NULL && Mbox[i] != m &&
			stricmp(m->name,Mbox[i]->name) == 0)
				return 1;
	return 0;
}

/* possible commands on the command line in the forwarding file */
static struct cmds cfwdcmds[] = {
	"tcp",		openconn,	0, 0, NULL,
	"telnet",	openconn,	0, 0, NULL,
#ifdef AX25
	"ax25",		openconn,	0, 0, NULL,
	"connect",	openconn,	0, 0, NULL,
#endif
#ifdef NETROM
	"netrom",	openconn,	0, 0, NULL,
#endif
	NULL
};

/* this function is called whenever the forwarding timer expires */
static void
startfwd(a,v1,v2)
int a;
void *v1, *v2;
{
	struct mbx *m;
	char *cc;
	cc = (char *) v1;
	if((m = newmbx()) == NULL) {
		free(cc);
		free(v2);
		return;
	}
	strcpy(m->name,(char *)v2);
	free(v2);
	m->state = MBX_TRYING;
	/* open the connection, m->user will be the new stream */
	if(cmdparse(cfwdcmds,cc,(void *)m) == -1) {
		free(cc);
		exitbbs(m);
		return;
	}
	free(cc);
	m->state = MBX_FORWARD;
	sockowner(fileno(m->user),Curproc);
	
	/* m->user will be closed automatically when this process exits */
	stdin = stdout = m->user;

	if(fwdinit(m) == -1) {
		/* it is probably not the right time to forward anymore */
		exitbbs(m);
		return;
	}
	/* read the connect script. Lines starting with a dot will be sent
	 * to the remote BBS.
	 */
	while(fgets(m->line,MBXLINE,m->tfile) != NULL)
		if(*m->line == '.')
			fputs(m->line + 1,stdout);
		else
			break;
	fflush(m->user);
	fclose(m->tfile);
	m->tfile = NULL;

	/* read the initial output from the bbs, looking for the SID */
	for(;;) {
		if(fgets(m->line,MBXLINE,m->user) == NULL) {
			exitbbs(m);
			return;
		}
		if(ISPROMPT(m->line))
			break;
		if(*m->line == '[') {		/* parse the SID */
			rip(m->line);
			mbx_parse(m);
			continue;
		}
	}
	/* Now sync the two ends as telnet password messes them up */
	if(socklen(fileno(m->user),0))		/* discard any remaining input */
		recv_mbuf(fileno(m->user),NULL,0,NULL,0);

	/* send our SID if the peer announced its SID */
	if(m->sid & MBX_SID) {
		puts("[NET-HMR$]");
		fflush(m->user);
		for(;;) {
			if(fgets(m->line,MBXLINE,m->user) == NULL) {
				exitbbs(m);
				return;
			}
			if(ISPROMPT(m->line))
				break;
		}
	}
	/* start the actual forwarding */
	dorevfwd(0,NULL,(void *)m);
	/* ask for reverse forwarding or just disconnect */
	if(((m->sid & MBX_SID) && puts("F>") == -1) ||
	   (m->sid & MBX_SID) == 0) {
		exitbbs(m);
		fclose(stdout);
		return;
	}
	fflush(m->user);
	/* parse the commands that are are received during reverse
	 * forwarding.
	 */
	while(fgets(m->line,MBXLINE,m->user) != NULL) {
		rip(m->line);
		if(mbx_parse(m) == 2)	/* got the "*** Done" command */
			break;
		puts("F>");
		fflush(m->user);
	}
	exitbbs(m);
	fclose(stdout);
}

/* open a network connection based upon information in the cc line.
 * m->user is set to the socket number.
 */
static int
openconn(argc,argv,p)
int argc;
char *argv[];
void *p;
{
	struct mbx *m;
	struct sockaddr sock;
	uint8 *np;
	char alias[AXBUF];
	union sp sp;
	int len;
	int s;

	m = (struct mbx *)p;
	sp.sa = &sock;
	if(argc < 2)
		return -1;
	switch(*argv[0]) {
	case 't':
		sp.in->sin_family = AF_INET;
		if((sp.in->sin_addr.s_addr = resolve(argv[1])) == 0)
			return -1;
		/* get the optional port number */
		if(argc > 2)
			sp.in->sin_port = atoi(argv[2]);
		else
			sp.in->sin_port = IPPORT_TELNET;
		if((s = socket(AF_INET,SOCK_STREAM,0)) == -1)
			return -1;
		m->user = fdopen(s,"r+t");
		len = sizeof(*sp.in);
		break;
#ifdef AX25
	case 'a':
	case 'c':	/* allow 'c' for 'connect' as well */
		if(argc < 3)
			return -1;
		sp.ax->sax_family = AF_AX25;
		strncpy(sp.ax->iface,argv[1],ILEN); /* the interface name */
		setcall(sp.ax->ax25_addr,argv[2]); /* the remote callsign */
		/* no digipeaters for now, use the "ax25 route add" command */
		if((s = socket(AF_AX25,SOCK_STREAM,0)) == -1)
			return -1;
		m->user = fdopen(s,"r+t");
		len = sizeof(*sp.ax);
		break;
#endif /* AX25 */
#ifdef NETROM
	case 'n':
		sp.nr->nr_family = AF_NETROM;
		len = sizeof(*sp.nr);
		if((s = socket(AF_NETROM,SOCK_SEQPACKET,0)) == -1)
			return -1;
		m->user = fdopen(s,"r+t");
		memcpy(sp.nr->nr_addr.user,Nr4user,AXALEN);
		memcpy(sp.nr->nr_addr.node,Mycall,AXALEN);
		bind(s,sp.sa,len);
		/* See if the requested destination could be an alias, and
		 * use it if it is.  Otherwise assume it is an AX.25
		 * address.
		 */
		if (putalias(alias,argv[1],0) != -1 &&
			(np = find_nralias(alias)) != NULL) {
				memcpy(sp.nr->nr_addr.user,np,AXALEN) ;
				memcpy(sp.nr->nr_addr.node,np,AXALEN) ;
		}
		else {	/* parse ax25 callsign */
		/* Only the user callsign of the remote station is never
		 * used by NET/ROM, but it is needed for the psocket() call.
		 */
			setcall(sp.nr->nr_addr.user,argv[1]);
			setcall(sp.nr->nr_addr.node,argv[1]);
		}
		break;
#endif /* NETROM */
	default:
		return -1;
	}
	if(connect(fileno(m->user),sp.sa,len) == -1) {
		logmsg(fileno(m->user),"MBOX forward failed: %s errno %d",
				sockerr(fileno(m->user)),errno);
		fclose(m->user);
		return -1;
	}
	return 0;
}

⌨️ 快捷键说明

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