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

📄 tcpport.c

📁 dos下开发TCP网络的库文件部分
💻 C
📖 第 1 页 / 共 2 页
字号:

int tn_sttyp(void)
{                            /* Send telnet terminal type. */
    char *ttn;
    int ttl;                 /* Name & length of terminal type. */

    ttn = termtype;		/* we already got this from environment */
    if ((*ttn == 0) || ((ttl = strlen(ttn)) >= TSBUFSIZ)) {
        ttn = "UNKNOWN";
        ttl = 7;
    }
    ttn = strcpy(&sb[1],ttn);		/* Copy to subnegotiation buffer */
    ttn = strchr( strupr(ttn), 0 );

    *sb    = 0;				/* 'is'... */
    *ttn++ = IAC;
    *ttn   = SE;

    if (send_iac(SB,TELOPT_TTYPE))	/* Send: Terminal Type */
	return(-1);

    sock_fastwrite( s, sb, ttl+3 );
    return(0);
}


/*
 * ttinc   - destructively read a character from our buffer
 *	   - if fast = 0, never times out
 */
int ttinc( int fast )
{
    char ch;

    /* organized to reduce number of set_timeouts when data waiting */
    while ( rec_in == rec_out ) {
        if ( !tcp_tick( s )) {
	    sock_status = 0;
            s = NULL;
            return( -1 );
        }

        /* do processing */
	kbhit();
        do_transmission();
        do_reception();

        if (fast) break;
    }
    ch = receive_buffer[ rec_out ];
    if ( ++rec_out >= RECEIVE_BUF_SIZE ) rec_out = 0;
    return( (word)(ch) & 0x00ff );
}

void interrupt ourhandler(unsigned bp /* , unsigned di, unsigned si, unsigned ds,
   unsigned es, unsigned dx, unsigned cx, unsigned bx, unsigned ax,
   unsigned ip, unsigned cs, unsigned flgs */)  /* trailing parms not used */
{
    stkptr = (struct stk far *)&bp;

    oldss = _SS;
    oldsp = _SP;
    _SS = FP_SEG(stack);
    _SP = FP_OFF(&stack[ sizeof( stack ) - 2 ]);
    stkptr->ax = sytek_int( stkptr->ax /*, stkptr->dx*/ );
    _SS = oldss;
    _SP = oldsp;
}

void stuff_char( char ch )
{
    transmit_buffer[ tran_in ] = ch;
    tran_in = (tran_in + 1) % TRANSMIT_BUF_SIZE;
}

unsigned  sytek_int( unsigned ax /*, unsigned dx */ )
{
    unsigned char ah;
    unsigned char ch;
/*    int sent; */
    unsigned int status;


    if ( !s ) {
        return( 0x1000 );   /* timeout */
    }

    if (chk_timeout( recvtimeout )) {
        do_reception();
        recvtimeout = set_ttimeout( 2 );    /* 10 ms */
    }

    /* disable(); receive_clock = RECEIVE_TICKS; enable(); } */

    ah = ax >> 8;

    if( ah == 1 ) {			/* send char in AL */
	ch = ax & 0x0FF;
/*	if (ch == '\r') ch = '\n'; */

	if( ((tran_in + 1) % TRANSMIT_BUF_SIZE) == tran_out ) {
	    outs( "?tr_buf_full?" );
	    status = 0x08000 | ch;
	} else {
	    if ((ch == '\r') && echo ) stuff_char( '\n' );
	    else stuff_char( ch );

	    status = 0x06000 | ch;

	    /* local echoing if requested */
	    if ( echo ) {
		receive_buffer[ rec_in ] = ch;
		rec_in = (rec_in + 1) % RECEIVE_BUF_SIZE;
		if (ch == '\r') {
		    receive_buffer[ rec_in ] = '\n';
		    rec_in = (rec_in + 1) % RECEIVE_BUF_SIZE;
		}
	    }
	}
    } else if( ah == 2 ) {		/* receive char into AL */
        do {
            ch = 0;
            if( rec_in == rec_out )
	        status = 0x08000;
            else {
		status = ch = (ttinc( 0 ) & 0xff);
                if ( ch == IAC ) {
                    /* process this stuff */
		    ch = ttinc( 0 );
                    if ( ch == IAC ) ch = 0; /* let it pass through */
		    else tn_doop(ch);
                }
            }
	} while ( ch == IAC );
	/* status = 0x0800; timeout */
    }
    else if( ah == 3 ) {		/* get status */
	if( rec_in == rec_out )
	    status = 0x06010;
	else {
	    status = 0x06110;
	}

    } else if( ah == 0 ) {		/* init port */
	status = 0x06010;
    } else {
	status = ax;
	outs( "?command_err?" );
    }

    /* here we do the io */
    if( transmit_clock <= 0 ) {
        do_transmission();
        disable();
        transmit_clock = TRANSMIT_TICKS;
        enable();
    }

    return( status );
}

void sytek_tick( void )
{
    if( transmit_clock ) transmit_clock--;
    if( receive_clock ) receive_clock--;
}

void interrupt tcpport_tick( void )
{
    (*old8)();
    if( transmit_clock ) transmit_clock--;
    if( receive_clock ) receive_clock--;
}

void do_reception(void)
{
    unsigned int maxtransfer;
#ifdef OLD
    unsigned int newtransfer;
    int status;
    unsigned int chars_avail, i;
#endif

    sock_tick( s, &status );

#ifdef OLD
    if ( sock_dataready( s )) {
	if (rec_out > rec_in ) {
	    /* we can fill intermediate portion of buffer */
	    maxtransfer = rec_out - rec_in;
	} else {
	    /* we fill end of buffer and leave start for next attempt */
	    maxtransfer = RECEIVE_BUF_SIZE - rec_in;
	}
	if (maxtransfer) {
	    rec_in += sock_fastread( s, &receive_buffer[ rec_in ], maxtransfer );
	    if ( rec_in >= RECEIVE_BUF_SIZE )
		rec_in -= RECEIVE_BUF_SIZE;
	}
    }
#else
    maxtransfer = RECEIVE_BUF_SIZE - rec_in;
    if (rec_out > rec_in) maxtransfer = rec_out - rec_in;
    maxtransfer = sock_recv( s, &receive_buffer[ rec_in ], maxtransfer );
    if (maxtransfer)
	rec_in = (rec_in + maxtransfer) % RECEIVE_BUF_SIZE ;
#endif OLD
/*    return( 0 ); */
    return;

sock_err:
    switch (status) {
	case 1 : outs("\n\r\7[??Host closed connection??]\n\r");
		 break;
	case-1 : outs("\n\r\7[??Host reset connection??]\n\r");
		 break;
    }
    s = NULL;
    sock_status = 0;
}

int do_transmission(void)
{
    unsigned int send_chars;
    int status;

    if( tran_in == tran_out ) return(0);

    if( tran_in > tran_out )
	send_chars = tran_in - tran_out;
    else
	send_chars = TRANSMIT_BUF_SIZE - tran_out;

    if( send_chars > TRANSMIT_MAX ) send_chars = TRANSMIT_MAX;

    sock_flushnext( s );
    send_chars = sock_fastwrite( s, &transmit_buffer[ tran_out], send_chars );

    /* this only changes it by the number of bytes we have emptied out */
    tran_out = (tran_out + send_chars) % TRANSMIT_BUF_SIZE;

    return(0);
}


int main( int argc, char *argv[] )
{
/*    int i; */
    int status = 0;
    char *temp;

    if (argc < 4 ) {
	outs("SERTN host port program options\n\r");
	exit(1);
    }

    sock_init();

    if (!( host = resolve( argv[1] ))) {
	outs( "Bad Host\n\r" );
	exit(1);
    }

    if ( (temp = getenv( TCPTERM )) != NULL ) {
	/* deal with strncpy limitation */
	movmem( temp, termtype, sizeof( termtype ));
	termtype[ sizeof(termtype) -1 ] = 0;
	outs("TERMINAL EMULATION :");
	outs( termtype );
	outs("\n\r");
    } else
	strcpy(termtype, "UNKNOWN");


    s = &socketdata;
    if ( host == my_ip_addr ) {
	outs("Incomming sessions not supported...\n\r");
	sock_wait_established( s, 0, NULL, &status );
	exit( -3 );
    }

    if (! tcp_open( s, 0, host, atoi( argv[2]), NULL )) {
#ifndef OLD
	sock_recv_init( s, bigbuf, sizeof( bigbuf ), 0);
#endif OLD
	outs( "Unable to open\n\r");
	exit(1);
    }

    sock_wait_established( s, sock_delay, NULL, &status );

    sock_mode( s, TCP_MODE_NAGLE );

    sock_status = 1;		/* allow interrupts */

    /* move vectors */
    moved_vectors = 1;
    old8 =  getvect( 0x08 );
    old14 =  getvect( 0x014 );
/*
    setvect( 0x08, (void interrupt (*)())serial_t );
*/
    setvect( 0x08, tcpport_tick );
    setvect( 0x014,ourhandler);  /* was serial_2 */
    recvtimeout = set_ttimeout( 1 );

    outs("Running...");
    outs( argv[3] );
    outs( "\n\r");
    system( argv[ argc-1 ] );

    outs("Done, now closing session\n\r");

    setvect( 0x014, old14 );
    setvect( 0x08, old8 );
    moved_vectors = 0;

    if ( s ) {
	sock_close( s );
	sock_wait_closed( s, sock_delay, NULL, &status );
    }

sock_err:
    switch (status) {
    case 1 : outs("Done.\n\r");
	     break;
    case -1: outs("Remote host reset connection.");
	     break;
    }
    if (moved_vectors) {
	setvect( 0x014, old14 );
	setvect( 0x08, old8 );
    }

    exit( (status)? 2 : 0);
    return (0);   /* not reached */
}

⌨️ 快捷键说明

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