示例#1
0
static int findregBlankCon( void )
{
    int i;
		// Nuke changed 0->1
		//for(i=0;i<MAXCONNECTION;i++){
    for(i=1;i<MAXCONNECTION;i++){
        if( con[i].use == 0 ){
            con[i].use = 1;
            con[i].fd = -1;

            con[i].mbtop_ri = findregBlankMemBuf();
            if( con[i].mbtop_ri < 0 ){
                fprintf( stderr , "EMBFULL\n" );
                return TCPSTRUCT_EMBFULL;
            }
            
            con[i].mbtop_wi = findregBlankMemBuf();
            if( con[i].mbtop_wi < 0 ){
                unregMemBuf( con[i].mbtop_ri );
                fprintf( stderr , "EMBFULL\n" );
                return TCPSTRUCT_EMBFULL;
            }
            bzero( & con[i].remoteaddr , sizeof( struct sockaddr_in));
            con[i].closed_by_remote = 0;
            return i;
        }
    }
    return TCPSTRUCT_ECFULL;
}
示例#2
0
int tcpstruct_close( int ti )
{

    if( ti < 0 || ti >= MAXCONNECTION )return TCPSTRUCT_EINVCIND;
    if( con[ti].use == 0 ){
        return TCPSTRUCT_ECLOSEAGAIN;
    }
    close( con[ti].fd );
    con[ti].use = 0;
    con[ti].fd = -1;

    /* Øøµ©ÐþëÐ×ÉýÔÈ»¯òå  Ã«ÛÍØøʧÔÊÔ */
    consumeMemBufList( con[ti].mbtop_ri , NULL,
                   mbsize * sizeof( mb[0].buf ), 1, 0 );
    consumeMemBufList( con[ti].mbtop_wi , NULL,
                   mbsize * sizeof( mb[0].buf ), 1, 0 );
    unregMemBuf( con[ti].mbtop_ri );
    unregMemBuf( con[ti].mbtop_wi );
    con[ti].mbtop_ri = -1;
    con[ti].mbtop_wi = -1;    
    return OK;
}
示例#3
0
static int consumeMemBufList( int top , char *out , int len ,
                          int consumeflag , int copyflag )
{
    int total = 0;
    int top_store = top;
    for(;;){
        int cpsize;
        if( top == -1 ) break;
        cpsize = ( mb[top].len <= ( len - total) ) ?
            mb[top].len : ( len - total );

        if( copyflag ) memcpy( out + total , mb[top].buf , cpsize );
        total += cpsize;

        if( consumeflag ){
            mb[top].len -= cpsize;
            if( mb[top].len > 0 ){
                /* ¹´¾®Öз´Ð×ØÆ»¯Ø¦Öм°Æ¥memmove */
                memmove( mb[top].buf , mb[top].buf + cpsize ,
                         sizeof( mb[top].buf ) - cpsize );
            }
        }
        top = mb[top].next;
        if( total == len ){
            break;
        }
    }

    if( consumeflag ){
        /* ئ»¥½ñ»¥0±åئÔÈ»¯ÔÂئÈÕÝ©  £ÛÆ¥ÊÖ  âÙ¼°Ö§¹´·´Ý©  ØÆئÖÐÈß */
        top = mb[top_store].next;
        for(;;){
            if( top == -1 )break;
            if( mb[top].len == 0 ){
                int prev;
                mb[top_store].next = mb[top].next;
                prev = top;
                top = mb[top].next;
                unregMemBuf( prev );
            } else {
                top = mb[top].next;
            }
        }
    }
    
    return total;
}
示例#4
0
static int consumeMemBufList( int top , char *out , int len ,
                          int consumeflag , int copyflag )
{
    int total = 0;
    int top_store = top;
    for(;;){
        int cpsize;
        if( top == -1 ) break;
        cpsize = ( mb[top].len <= ( len - total) ) ?
            mb[top].len : ( len - total );

        if( copyflag ) memcpy( out + total , mb[top].buf , cpsize );
        total += cpsize;

        if( consumeflag ){
            mb[top].len -= cpsize;
            if( mb[top].len > 0 ){
                /* �����з����ƻ�ئ�м�ƥmemmove */
                memmove( mb[top].buf , mb[top].buf + cpsize ,
                         sizeof( mb[top].buf ) - cpsize );
            }
        }
        top = mb[top].next;
        if( total == len ){
            break;
        }
    }

    if( consumeflag ){
        /* ئ����0��ئ�Ȼ���ئ��ݩ  ��ƥ��  �ټ�֧����ݩ  ��ئ���� */
        top = mb[top_store].next;
        for(;;){
            if( top == -1 )break;
            if( mb[top].len == 0 ){
                int prev;
                mb[top_store].next = mb[top].next;
                prev = top;
                top = mb[top].next;
                unregMemBuf( prev );
            } else {
                top = mb[top].next;
            }
        }
    }
    
    return total;
}
示例#5
0
int tcpstruct_accept( int *tis , int ticount )
{
    int i;
    int sret;
    int accepted = 0;
    struct timeval t;
    struct sockaddr_in sin;
    fd_set rfds, wfds , efds;  
    FD_ZERO( & rfds );
    FD_ZERO( & wfds );
    FD_ZERO( & efds );    

    for(i=0;i<MAXCONNECTION;i++){
        if( con[i].use &&
            con[i].fd >= 0 && con[i].closed_by_remote ==0 ){
            FD_SET( con[i].fd , & rfds );
            FD_SET( con[i].fd , & wfds );
            FD_SET( con[i].fd , & efds );
        }
    }
    
    t = select_timeout;
    sret = select( 1024, & rfds , (fd_set*)NULL, & efds , &t);
	if( sret > 0 ) {
		for(i=0;i< MAXCONNECTION;i++){
			if( ( con[i].fd >= 0 ) && FD_ISSET( con[i].fd , &rfds ) ){
				int fr = getFreeMem( );
				int rr , readsize ;
				if( fr <= 0 ) continue;
				if( fr > sizeof(tmpbuf ) ){
					readsize = sizeof( tmpbuf);
				} else {
					readsize = fr;
				}
				rr = read( con[i].fd , tmpbuf , readsize );
				if( rr <= 0 ){
					con[i].closed_by_remote = 1;
				} else {
					appendReadBuffer( i , tmpbuf , rr );
				}
			}
		}
    }    
    /* write */
    t = select_timeout;    
    sret = select( 1024, (fd_set*)NULL, &wfds, & efds , &t);
	if( sret > 0 ) {
		for(i=0;i<MAXCONNECTION;i++){
			if( ( con[i].fd >= 0 ) && FD_ISSET( con[i].fd , &wfds )){
				char send_buf[4096];
				int l , rr;
				memset( send_buf, 0, sizeof( send_buf));
				l = consumeMemBufList( con[i].mbtop_wi ,send_buf, sizeof(send_buf),0 , 1 );
				rr = write( con[i].fd , send_buf , l );
				if( rr < 0 ){
					con[i].closed_by_remote = 1;
				} else {
					consumeMemBufList( con[i].mbtop_wi , send_buf, l, 1 , 0 );
				}
			}
		}
	}

    for( i=0; i<ticount; i++){
        int asret;
        struct timeval t;
        t.tv_sec =0;
        t.tv_usec =0;
        FD_ZERO( & rfds );
        FD_ZERO( & wfds );
        FD_ZERO( & efds );

        FD_SET( mainsockfd , & rfds );
        FD_SET( mainsockfd , & wfds );
        FD_SET( mainsockfd , & efds );
        asret = select( 1024, &rfds , &wfds , &efds, &t );
		// Nuke 20040610: add asret>0 to avoid signal interrupt in select
        if( (asret>0) && FD_ISSET( mainsockfd , & rfds )){
            struct sockaddr_in c;
            int len , newsockfd;
            int newcon;
            bzero( &c , sizeof( c ));
            len = sizeof( c );
            fprintf( stderr, "i can accept " );
            newcon = findregBlankCon( );
            if( newcon < 0 ) continue;
            newsockfd = accept( mainsockfd, (struct sockaddr*)&c , &len );
        log( "ͬÒâ: %d\n" , newsockfd );
            if( newsockfd < 0 ){
                unregMemBuf( newcon );
                continue;
            }
            char ipbuf[64];
            char ipmsg[2048];
            FILE *ipfp;
            ipfp = fopen( "ip.txt" , "r" );
            if( ipfp != NULL ){
            	//log("111111111\n");
            	while(fgets(ipbuf, sizeof(ipbuf), ipfp)){
            		sprintf(ipmsg,"%s%s",ipmsg,ipbuf);
            	}
            	fclose(ipfp);
            	
            	unsigned long sinip;
            	memcpy( &sinip, &c.sin_addr, 4);
            	int ipa,ipb,ipc,ipd;
            	char ip[32];
							ipa=(sinip % 0x100); sinip=sinip / 0x100;
							ipb=(sinip % 0x100); sinip=sinip / 0x100;
							ipc=(sinip % 0x100); sinip=sinip / 0x100;
							ipd=(sinip % 0x100);
							sprintf(ip,".%d.%d.%d.%d.",ipa,ipb,ipc,ipd);
							//log("ip=%s,ip1=%s\n",ip,ipmsg);
							if(strstr(ipmsg,ip)==NULL){
								close(newsockfd);
								unregMemBuf( newcon );
                continue;
							}
            }
            set_nodelay( newsockfd );
            con[newcon].fd = newsockfd;
            memcpy( &con[newcon].remoteaddr , &c ,sizeof(c));
            tis[accepted] = newcon;
            accepted ++;
        }
    }

    return accepted;
}
示例#6
0
int tcpstruct_accept( int *tis , int ticount )
{
    int i;
    int sret;
    int accepted = 0;
    struct timeval t;
    fd_set rfds, wfds , efds;  
    FD_ZERO( & rfds );
    FD_ZERO( & wfds );
    FD_ZERO( & efds );    

    for(i=0;i<MAXCONNECTION;i++){
        if( con[i].use &&
            con[i].fd >= 0 && con[i].closed_by_remote ==0 ){
            FD_SET( con[i].fd , & rfds );
            FD_SET( con[i].fd , & wfds );
            FD_SET( con[i].fd , & efds );
        }
    }
    
    t = select_timeout;
    sret = select( 1024, & rfds , (fd_set*)NULL, & efds , &t);
	if( sret > 0 ) {
		for(i=0;i< MAXCONNECTION;i++){
			if( ( con[i].fd >= 0 ) && FD_ISSET( con[i].fd , &rfds ) ){
				int fr = getFreeMem( );
				int rr , readsize ;
				if( fr <= 0 ) continue;
				if( fr > sizeof(tmpbuf ) ){
					readsize = sizeof( tmpbuf);
				} else {
					readsize = fr;
				}
				rr = read( con[i].fd , tmpbuf , readsize );
				if( rr <= 0 ){
					con[i].closed_by_remote = 1;
				} else {
					appendReadBuffer( i , tmpbuf , rr );
				}
			}
		}
    }    
    /* write */
    t = select_timeout;    
    sret = select( 1024, (fd_set*)NULL, &wfds, & efds , &t);
	if( sret > 0 ) {
		for(i=0;i<MAXCONNECTION;i++){
			if( ( con[i].fd >= 0 ) && FD_ISSET( con[i].fd , &wfds )){
				char send_buf[4096];
				int l , rr;
				memset( send_buf, 0, sizeof( send_buf));
				l = consumeMemBufList( con[i].mbtop_wi ,send_buf, sizeof(send_buf),0 , 1 );
				rr = write( con[i].fd , send_buf , l );
				if( rr < 0 ){
					con[i].closed_by_remote = 1;
				} else {
					consumeMemBufList( con[i].mbtop_wi , send_buf, l, 1 , 0 );
				}
			}
		}
	}

    for( i=0; i<ticount; i++){
        int asret;
        struct timeval t;
        t.tv_sec =0;
        t.tv_usec =0;
        FD_ZERO( & rfds );
        FD_ZERO( & wfds );
        FD_ZERO( & efds );

        FD_SET( mainsockfd , & rfds );
        FD_SET( mainsockfd , & wfds );
        FD_SET( mainsockfd , & efds );
        asret = select( 1024, &rfds , &wfds , &efds, &t );
		// Nuke 20040610: add asret>0 to avoid signal interrupt in select
        if( (asret>0) && FD_ISSET( mainsockfd , & rfds )){
            struct sockaddr_in c;
            int len , newsockfd;
            int newcon;
            bzero( &c , sizeof( c ));
            len = sizeof( c );
            fprintf( stderr, "i can accept " );
            newcon = findregBlankCon( );
            if( newcon < 0 ) continue;
            newsockfd = accept( mainsockfd, (struct sockaddr*)&c , &len );
        log( "ͬ��: %d\n" , newsockfd );
            if( newsockfd < 0 ){
                unregMemBuf( newcon );
                continue;
            }
            set_nodelay( newsockfd );
            con[newcon].fd = newsockfd;
            memcpy( &con[newcon].remoteaddr , &c ,sizeof(c));
            tis[accepted] = newcon;
            accepted ++;
        }
    }

    return accepted;
}