예제 #1
0
void test0130(void)
{
    int handle, res, ok;
    int a,b,c;
    
    out_test_header(0x0130, "UDP - CNbyte_count() on 3 datagrams");
    handle = UDP_open(SERVER_ADDR, SERVER_PORT_START + 4);
    if(handle >= 0) {
        // send 3 x 100 bytes
        (void) UDP_send(handle, wBuf, 100);
        (void) UDP_send(handle, wBuf, 100);
        (void) UDP_send(handle, wBuf, 100);
        
        sleep(1);
        
        res = CNbyte_count(handle);         // see how many bytes we got for reading on this socket
        ok = (res == 300) ? 1 : 0;      
        
        out_result_error(ok, res);
        //-----------------------------------
        out_test_header(0x0131, "UDP - CNget_block() on 3 datagrams");
        
                                            // receive it using 3x CNget_block()
        a = CNget_block (handle, rBuf, 100);
        b = CNget_block (handle, rBuf, 100);
        c = CNget_block (handle, rBuf, 100);
        
        ok = (a == 100 && b == 100 && c == 100) ? 1 : 0;
        out_result_error(ok, res);
        //-----------------------------------
        
        UDP_close(handle);
    } else {
        out_result_string(0, "UDP_open failed");
    }
    
    //////////////////////////////////////////////////////////////////////////////
    
    out_test_header(0x0132, "UDP - CNbyte_count() after partial read");
    handle = UDP_open(SERVER_ADDR, SERVER_PORT_START + 4);
    if(handle >= 0) {
        // send 300 bytes
        (void) UDP_send(handle, wBuf, 300);
        
        sleep(1);
        
        res = CNbyte_count(handle);         // see how many bytes we got for reading on this socket
        ok = (res == 300) ? 1 : 0;      

        if(!ok) {                           // if not enough data, fail
            out_result_error_string(ok, res, "not enough data");
        } else {
            a = CNget_block (handle, rBuf, 100);
            b = CNbyte_count(handle);
            
            ok = (a == 100 && b == 200) ? 1 : 0;
            out_result_error(ok, b);
         }
        
        UDP_close(handle);
    } else {
        out_result_string(0, "UDP_open failed");
    }    
    
    //////////////////////////////////////////////////////////////////////////////
    
    out_test_header(0x0133, "UDP - get 3 DGRAMs with 1 CNget_block");
    handle = UDP_open(SERVER_ADDR, SERVER_PORT_START + 4);
    if(handle >= 0) {
        // send 300 bytes
        (void) UDP_send(handle, wBuf, 100);
        (void) UDP_send(handle, wBuf, 100);
        (void) UDP_send(handle, wBuf, 100);
        
        sleep(1);
        
        res = CNbyte_count(handle);         // see how many bytes we got for reading on this socket
        ok = (res == 300) ? 1 : 0;      

        if(!ok) {                           // if not enough data, fail
            out_result_error_string(ok, res, "not enough data");
        } else {
            a = CNget_block (handle, rBuf, 300);
            b = CNbyte_count(handle);
            
            ok = (a == 300 && b == 0) ? 1 : 0;
            out_result_error(ok, b);
         }
        
        UDP_close(handle);
    } else {
        out_result_string(0, "UDP_open failed");
    }        
    
    //////////////////////////////////////////////////////////////////////////////
    
    out_test_header(0x0134, "UDP - get 3 DGRAMs with 3x CNget_NDB");
    handle = UDP_open(SERVER_ADDR, SERVER_PORT_START + 4);
    if(handle >= 0) {
        // send 300 bytes
        (void) UDP_send(handle, wBuf, 100);
        (void) UDP_send(handle, wBuf, 100);
        (void) UDP_send(handle, wBuf, 100);
        
        sleep(1);
        
        res = CNbyte_count(handle);         // see how many bytes we got for reading on this socket
        ok = (res == 300) ? 1 : 0;      

        if(!ok) {                           // if not enough data, fail
            out_result_error_string(ok, res, "not enough data");
        } else {
            NDB *m,*n,*o;
            
            m = CNget_NDB(handle);
            n = CNget_NDB(handle);
            o = CNget_NDB(handle);
            
            ok = (m != NULL && n != NULL && o != NULL) ? 1 : 0;
            
            if(!ok) {
                out_result_string(ok, "some CNget_NDB failed");
            } else {
                ok = (m->len == 100 && n->len == 100 && o->len == 100) ? 1 : 0;
                
                if(!ok) {
                    out_result_string(ok, "length of NDB block wrong");
                } else {
                    out_result(1);
                }
            }
         }
        
        UDP_close(handle);
    } else {
        out_result_string(0, "UDP_open failed");
    }        

    //////////////////////////////////////////////////////////////////////////////
    
    out_test_header(0x0135, "UDP - get 3 DGRAMs with CNget_char");
    handle = UDP_open(SERVER_ADDR, SERVER_PORT_START + 4);
    if(handle >= 0) {
        // send 300 bytes
        (void) UDP_send(handle, wBuf, 100);
        (void) UDP_send(handle, wBuf, 100);
        (void) UDP_send(handle, wBuf, 100);
        
        sleep(1);
        
        res = CNbyte_count(handle);         // see how many bytes we got for reading on this socket
        ok = (res == 300) ? 1 : 0;      

        b = 1;                              // good for now
        if(!ok) {                           // if not enough data, fail
            out_result_error_string(ok, res, "not enough data");
        } else {
            int i;
            for(i=0; i<300; i++) {
                a = CNget_char(handle);
                
                if(a >= 0) {
                    rBuf[i] = a;
                } else {
                    out_result_error_string(0, a, "error on CNget_char");
                    b = 0;                  // failed
                    break;
                }
            }
        
            if(b) {                         // if good, check data
                a = memcmp(rBuf,       wBuf, 100);
                b = memcmp(rBuf + 100, wBuf, 100);
                c = memcmp(rBuf + 200, wBuf, 100);
                
                ok = (a == 0 && b == 0 && c == 0) ? 1 : 0;
                out_result(ok);
            }
         }
        
        UDP_close(handle);
    } else {
        out_result_string(0, "UDP_open failed");
    }
}
예제 #2
0
/* wait for the response from the newsserver */
int news_receive(int cn, char *file, int file_reqd)
{
	/* copied from http_get in CABCOVL.C  */
	int16 x, bytes_available, eof=0;
	long bytes_read=0,rc;
	int return_code=0;
	int file_handle, status=0, header_pos=0;
	time_t timeout,kicker,total;
	int bug=0;
	int sbuf=sizeof(buffer);

	if (file_reqd) {
		file_handle=(int) Fcreate(file, 0);
		if (bug) {
			fprintf( log, "news_receive - opening file = %d\n", file_handle );
			fflush( log );
		}
		if (file_handle<0) {
			#ifdef ERR_MSGS
				printf("Couldn't create %s!\n", file);
			#endif
			return(1);
		}
	}
	total=clock();					/* total time in routine */
	timeout=clock()+max_to*CLK_TCK;	/* timeout */
	kicker=clock()+kick*CLK_TCK;		/* conversation kicker */
	if (file_reqd) {
		browser->msg_status(2,0); /* Getting data... */
	}
	if (file_reqd==2) { status=2; }

	while (eof==0) {
		bytes_available=CNbyte_count(cn);
		/* printf("received %d bytes \n", (long)bytes_available ); */

		if (handle_escape_events()==27) { eof=4; }
		/*
		if (Bconstat(2)==-1 && (Bconin(2) & 0xFF)==27) { eof=4;	}
		*/
		if (clock()>timeout) {
			#ifdef MDEBUG
				printf("Timeout!\n");
			#endif
			eof=2;
		} else if (clock()>kicker) {
			#ifdef MDEBUG
				printf("Kick Connection!\n");
			#endif
			CNkick(cn);
			kicker=clock()+kick*CLK_TCK;	/* conversation kicker */
		} else if (bytes_available==E_EOF) {
			#ifdef MDEBUG
				printf("EOF!\n");
			#endif
			eof=1;
		} else if (bytes_available<E_NODATA) {
			#ifdef ERR_MSGS
				printf("CNbyte_count() returns: %s\n", get_err_text(bytes_available));
			#endif
			eof=3;
		} else if (bytes_available>0) {
			timeout=clock()+char_to*CLK_TCK;	/* timeout after last char */
			kicker=clock()+kick*CLK_TCK;		/* conversation kicker */
			if (status==2) {
				if (bytes_available>sbuf) { bytes_available=sbuf; }
				if (CNget_block(cn, buffer, bytes_available)>=E_NODATA) {
					rc = Fwrite(file_handle, bytes_available, &buffer);
					if (rc!=bytes_available) { 	/* didn't write everything */
						file_write_error(rc);
						Fclose(file_handle);
						return(1);
					}
					bytes_read+=bytes_available;
					if (bug) {
						fprintf( log, "received %d bytes, total %ld\n",
							bytes_available , bytes_read);
						fflush( log );
					} else { browser->msg_status(2, bytes_read); }

				/* look for period (.) on a line by itself */
					if (strncmp(&buffer[bytes_available-5], "\r\n.\r\n", 5)==0 ||
					    strncmp(&buffer[bytes_available-5], "\n\r.\n\r", 5)==0 ||
					    strncmp(&buffer[bytes_available-3], "\r.\r", 3)==0 ||
					    strncmp(&buffer[bytes_available-3], "\n.\n", 3)==0)
					eof=1;
					if (bytes_read==3 &&
					    strncmp(&buffer[bytes_available-3], ".\r\n", 3)==0)
					eof=1;
				} else {
					#ifdef ERR_MSGS
						printf("Error in CNget_block()!\n");
					#endif
					eof=3;
				}
			} else {
				x = CNget_char(cn);
				if (x<E_NODATA) {
					#ifdef ERR_MSGS
						printf("CNget_char() returns: %s\n", get_err_text(x));
					#endif
					eof=3;
				} else
					header[header_pos++]=(char) x;
			}
			/* only an empty line and response line in NNTP responses */
			/* when status = 0, drop until valid non-control, when = 1, store in header */
			if (status==0) {
				if (strncmp(&header[header_pos-1], " ", 1)<0) {
					header_pos = 0;
				} else { status++;}
			}
			if (status==1) {
				if (strncmp(&header[header_pos-2], "\r\n", 2)==0 ||
				    strncmp(&header[header_pos-2], "\n\r", 2)==0) {
				/*	strncmp(&header[header_pos-1], "\r", 1)==0 ||
				    strncmp(&header[header_pos-1], "\n", 1)==0) { */
					header[header_pos-2]=0;
					if (log_resp=='Y') { fprintf( log, "%s\n", header ); fflush(log); }
					#ifdef MDEBUG
						printf("Header: %s\n", header );
						printf("End of header.\n");
					#endif
					if (!file_reqd) { eof=1; } else { status++; }
					if (memcmp(header,"423",3)==0) { eof=1; }
				} else if (header_pos>2000) {
					rc = Fwrite(file_handle, header_pos, header);
					if (rc!=header_pos) { 	/* didn't write everything */
						file_write_error(rc);
						Fclose(file_handle);
						return(1);
					}
					status++;
				}
			}
		}
	}
	#ifdef MDEBUG
	printf("EOF= %d \n", (long)eof );
	#endif

	if (eof>1) {	/* timeout or worse*/
	/*	x = (int16)TCP_close(cn, 2);
		if (x < 0) {
			#ifdef MDEBUG
				printf("TCP_close() returns: %s\n", get_err_text(x));
			#endif
		} 
	*/
		if (bug) {
			fprintf( log, "news_receive - error - eof = %d\n", eof );
			fflush( log );
		}	
		if (file_reqd) {
		#ifdef MDEBUG
			printf("Fclose returns %i\n", Fclose(file_handle));
		#else
			/**** I might get a bug here! ****/
			if((x=(int16)Fclose(file_handle))<0) {
				#ifdef ERR_MSGS
					printf("Error with Fclose! (%i)\n", x);
				#endif
			}
		#endif
			if (bug) {
				fprintf( log, "news_receive - error - closing file = %d\n", x );
				fflush( log );
			}
		}
		if (eof==3) { eof=bytes_available; }  /* return negative value */
		return(eof);
	}	

	if (file_reqd) {
		if (log_data=='Y') {
			total=clock()-total;	/* total time in clock ticks */
			if (total) {
				fprintf( log, "Transfer Rate = %ld cps\n", (bytes_read*CLK_TCK)/total ); fflush(log);
			}
		}
		#ifdef MDEBUG
			printf("Fclose returns %d\n", Fclose(file_handle));
		#else
			if((x=(int16)Fclose(file_handle))<0)	browser->msg_error(x);
		#endif
		if (bug) {
			fprintf( log, "news_receive - good - closing file = %d\n", x );
			fflush( log );
		}
	}
	#ifdef MDEBUG
		printf("Hit a key.\n");
		Bconin(2);
	#endif
	return(return_code);	/* return(0)     if getting data was successful,	 */
        					/* return(errno) if it fails, return an error number */
}