/* discard extraneous data after timeout */ int news_discard( int cn ) { int16 bytes_available,x; int sbuf=sizeof(buffer); /* fprintf( log, "in news_discard(%d)\n", cn ); fflush(log); */ while ((bytes_available=CNbyte_count(cn))>0) { if (handle_escape_events()==27) break; /* if (Bconstat(2)==-1 && (Bconin(2) & 0xFF)==27) { break; } */ if (bytes_available>sbuf) { bytes_available=sbuf; } CNget_block(cn, buffer, bytes_available); } if (bytes_available<0) { x=bytes_available; fprintf( log, "CNbyte_count(%d) returns (%d) %s \n", cn, x, get_err_text(x)); fflush( log ); } /* fprintf( log, "completed news_discard(%d)\n", cn ); fflush(log); */ return(bytes_available); }
int checkdccchat() { short lcount=0,offset=0,sln; short mcto=0; char *dp; char temps[2048]; int16 stat; while(mcto<30){ if(chan[mcto].stat>2){ stat = CNbyte_count(chan[mcto].cn); if (chan[mcto].stat == SAWAIT && stat >= E_NODATA) {chan[mcto].stat = SACTIVE; sendout("--DCC chat opened",cwin,col[CDCCINFO]); wn[chan[mcto].win].chan=mcto; dowindinfo(chan[mcto].win);} if (stat < E_NODATA && chan[mcto].stat !=SAWAIT) { sendout("--DCC chat terminated by remote",cwin,col[CDCCINFO]); closchan(mcto); } else{ if (stat > 0){ strcpy(dccp,"\0"); CNget_block(chan[mcto].cn,(char *)dccp,stat); *(dccp+stat)=0; sln=(int16)strlen(dccp)+1; dp=dccp; while(offset<sln){ if(*(dp+lcount)==9){*(dp+lcount)=' ';} if(*(dp+lcount)==10){ *(dp+lcount)=0; sprintf(temps,"[DCC:%s] %s",chan[mcto].name,dp); sendout(temps,chan[mcto].win,col[CDCCINFO]);dp=dp+lcount+1; lcount=-1; } offset++; lcount++; } if(offset!=sln){ if(!conf.boldnicks) sprintf(temps,"[DCC:%s] ",chan[mcto].name); else sprintf(temps,"[DCC:%s] ",chan[mcto].name); strncat(temps,dp,strlen(dp)-1); sendout(temps,chan[mcto].win,col[CDCCINFO]); } } } } mcto++; } return 0; }
int dcc_send(void) { short count=0; int16 tstat; long stat; char temps[2048],temps2[2048]; while(count<10){ if(dcc[count].dir==DCOUT && dcc[count].stat){ tstat=CNbyte_count(dcc[count].cn); if(tstat<E_NODATA && dcc[count].stat==SACTIVE){ sprintf(temps2,"**DCC: Error in send to %s",dcc[count].avec); sendout(temps2,cwin,col[CERRORS]); resetadcc(count); if(is_dw_open(dcc[count].dcw))wd_close(dcc[count].dcw); return 0; } if(dcc[count].stat==SAWAIT && tstat>=E_NODATA){ sprintf(temps,"--DCC send to %s connected",dcc[count].avec); sendout(temps,cwin,col[CDCCINFO]); dcc[count].stat=SACTIVE; dcc[count].lack=dcc[count].cnt; } if(tstat>=4 && dcc[count].stat==SACTIVE){ CNget_block(dcc[count].cn,(char *)&dcc[count].lack,4); } if(dcc[count].lack==dcc[count].cnt && dcc[count].dcfh<0){ sprintf(temps,"--DCC send to %s completed",dcc[count].avec); sendout(temps,cwin,col[CDCCINFO]); resetadcc(count); if(is_dw_open(dcc[count].dcw))wd_close(dcc[count].dcw); return 0; } if(dcc[count].stat==SACTIVE && dcc[count].lack==dcc[count].cnt){ stat=Fread(dcc[count].dcfh,(long)dctbufsize,(char *)&dcctempbuffer); if(stat>0)TCP_send(dcc[count].cn,(char *)&dcctempbuffer,(int16)stat); dobar(dcc[count].dcw,(long)dcc[count].cnt,dcc[count].len,count); if(stat<dctbufsize){ Fclose(dcc[count].dcfh); dcc[count].dcfh=-1; } dcc[count].cnt=dcc[count].cnt+stat; } } count++; } return 0; }
int dcc_recv(void) { short count=0; int16 tstat; char temps[2048]; while(count<10){ if(dcc[count].dir==DCIN && dcc[count].stat==RAWAIT){ tstat=CNbyte_count(dcc[count].cn); if(tstat>0){ CNget_block(dcc[count].cn,(char *)&dcctempbuffer,tstat); Fwrite(dcc[count].dcfh,(long)tstat,(char *)&dcctempbuffer); dcc[count].cnt=dcc[count].cnt+tstat; dobar(dcc[count].dcw,(long)dcc[count].cnt,dcc[count].len,count); TCP_send(dcc[count].cn,(char *)&dcc[count].cnt,4); } if(tstat<E_NODATA){ printf("%d",tstat); if(dcc[count].cnt<dcc[count].len){ sprintf(temps,"**DCC: %s from %s incompleted",dcc[count].fname,dcc[count].avec); sendout(temps,cwin,col[CERRORS]); if(is_dw_open(dcc[count].dcw))wd_close(dcc[count].dcw); resetadcc(count); return 0; } sprintf(temps,"++DCC:%s received from %s",dcc[count].fname,dcc[count].avec); sendout(temps,cwin,col[CDCCINFO]); sprintf(temps,"++Bytes: %lu",dcc[count].cnt); sendout(temps,cwin,col[CDCCINFO]); if(is_dw_open(dcc[count].dcw))wd_close(dcc[count].dcw); resetadcc(count); return 0; } } count++; } return 0; }
static int16 getResponse(int16 cnId) { char response[RESPONSESIZE]; int16 nInQueue; int16 rc; clock_t to = clock() + TIMEOUT * CLK_TCK; /* time to quit at */ /* poll for input */ while ( (nInQueue = CNbyte_count(cnId)) <= 0 ) { if (clock() > to) { uiPrintf(uiH, uiPrERR, "get timed out"); return -1; } uiYield(uiH, YIELDMS); } rc = CNget_block(cnId, response, min(nInQueue, RESPONSESIZE) ); if (rc != min(nInQueue, RESPONSESIZE)) { uiPrintf(uiH, uiPrERR, "get failed"); return -1; } return response[0]; } /* getResponse */
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"); } }
int sendAndReceive(BYTE tcpNotUdp, DWORD blockSize, int handle, BYTE getBlockNotNdb) { //---------- // send int res; DWORD now, endTime; DWORD kbs = 1 + (blockSize / 1024); endTime = getTicks() + (kbs * 200); // for each kB of data give 1 second to transfer while(1) { // try to send if(tcpNotUdp) { res = TCP_send(handle, wBuf, blockSize); } else { res = UDP_send(handle, wBuf, blockSize); } if(res != E_OBUFFULL) { break; } now = getTicks(); if(now >= endTime) { // timeout? out_result_error_string(0, blockSize, "send() timeout"); return 0; } } if(res != E_NORMAL) { out_result_error_string(0, res, "send() failed"); return 0; } //---------- // wait endTime = getTicks() + (kbs * 200); // for each kB of data give 1 second to transfer memset(rBuf, 0, blockSize); BYTE *pBuf = rBuf; DWORD toGet = blockSize; while(toGet > 0) { now = getTicks(); if(now >= endTime) { // timeout? out_result_error_string(0, blockSize, "CNbyte_count() timeout"); return 0; } res = CNbyte_count(handle); // find out how many bytes are waiting if(res > 0) { // something waiting? read it if(getBlockNotNdb) { // retrieve using CNget_block? res = CNget_block(handle, pBuf, res); if(res > 0) { pBuf += res; toGet -= res; } else if(res != E_NODATA && res < 0) { // if it's some error, and that error is not E_NODATA, fail out_result_error_string(0, blockSize, "CNget_block() failed"); return 0; } } else { // retrieve using CNget_NDB NDB *ndb = CNget_NDB(handle); if(ndb) { // if something retrieved memcpy(pBuf, ndb->ndata, ndb->len); // copy in the data pBuf += ndb->len; // it was this many bytes toGet -= ndb->len; KRfree (ndb->ptr); // free the ram KRfree (ndb); } } } } //---------- // data are valid? res = memcmp(rBuf, wBuf, blockSize); if(res != 0) { out_result_string(0, "Received data mismatch"); return 0; } //------- // if came here, everything is OK return 1; }
/* 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 */ }