void *sender() { char *buffer = (char *)malloc (BUF); do { fgets (buffer, BUF, stdin); TCP_send (&sock, buffer, strlen (buffer)); } while (strcmp (buffer, "quit\n") != 0); close_socket (&sock); exit(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; }
/** * @brief This function implements the tcp_sent LwIP callback (called when ACK * is received from remote host for sent data) * @param None * @retval None */ static err_t TCP_sent(void *arg, struct tcp_pcb *tpcb, u16_t len) { TCP *es; LWIP_UNUSED_ARG(len); es = (TCP *)arg; if (es->tx != NULL) { /* still got pbufs to send */ tcp_sent(es->pcb, TCP_sent); TCP_send(es->pcb, es); } else { /* if no more data to send and client closed connection*/ if (es->state == ES_CLOSING) { TCP_close(tpcb, es); } } return ERR_OK; }
static void output(int16 cnId, char *oStr, int oLen) { int16 rc; clock_t tQuit = clock() + TIMEOUT * CLK_TCK; /* time to quit at */ while ( (rc = TCP_send(cnId, oStr, oLen)) == E_OBUFFULL ) { if (clock() > tQuit) { uiPrintf(uiH, uiPrERR, "output timed out"); return; } uiYield(uiH, YIELDMS); } if (rc != E_NORMAL) { uiPrintf(uiH, uiPrERR, "output|%s", get_err_text(rc)); } } /* end output */
void _tcp_flush(void *v) { TCP *es=v; if(es->pcb->snd_queuelen > TCP_SND_QUEUELEN-1) // !!!! pred vpisom preveri, ce ni queue ze poln tcp_output(es->pcb); // kratkih blokov (Nagle algoritem bo javil MEM error....) else if(es->io) { // sicer nadaljevanje... char c[256]; int k,n=0; if(es->rx) { // a je kaj za sprejem ??? struct pbuf *q; for(q=es->rx; q != NULL; q=es->rx) { // preskanirat je treba celo verigo pbuf n+=k=_buffer_push(es->io[0],q->payload, q->len); // push v io if(k < q->len) { pbuf_header(q,-k); // skrajsaj header break; } es->rx = es->rx->next; if (es->rx != NULL) pbuf_ref(es->rx); pbuf_free(q); } tcp_recved(es->pcb,n); // free raw input } n=_buffer_count(es->io[1]); // koliko je v buferju za izpis ... if(n > tcp_sndbuf(es->pcb)) // ne sme biti vec kot je placa na raw output .... n = tcp_sndbuf(es->pcb); if(n > 256) // ne sme bit vec kot 1024.... glej c[1024] n=256; if(n) { // ce je sploh kej ... struct pbuf *p = pbuf_alloc(PBUF_TRANSPORT, n , PBUF_POOL); if(p != NULL) { // ce je alokacija pbuf uspela n=_buffer_pull(es->io[1],c,n); // kopiraj pull vsebino v vmesni buffer pbuf_take(p,c,n); // formiraj pbuf if(es->tx) // verizi, ce je se kaj od prej.. pbuf_chain(es->tx,p); // else es->tx = p; // sicer nastavi nov pointer tcp_sent(es->pcb, TCP_sent); // set callback & send.. TCP_send(es->pcb, es); tcp_output(es->pcb); } } } }
int initiatedcc(char *usr,char *ftg){ short temp; struct FILEINFO finf; uint32 length; uint32 cip; char temps2[2048]; if(!strlen(ftg)){form_alert(1,"[1][DCC Error:|Please specify a file][Okay]");return 0;} if(!strlen(usr)){form_alert(1,"[1][DCC Error:|Please specify recipient][Okay]");return 0;} temp=findfreedc(); if(temp==-1){form_alert(1,"[1][DCC Error:|No free channels!][Okay]");return 0;} dcc[temp].dcfh=Fopen(ftg,0); if(dcc[temp].dcfh<0){form_alert(1,"[DCC Error:|Unable to open file][Okay]");return 0;} dcc[temp].cn=TCP_open(0L,0,0,4096); if(dcc[temp].cn<0){form_alert(1,"[1][DCC Error:|Unable to open socket][Okay]");return 0;} dcc[temp].stat=SAWAIT; dcc[temp].dir=DCOUT; Fsetdta(&finf); Fsfirst(ftg,0); length=finf.size; strcpy(dname,ftg); basename(dname); cb=CNgetinfo(cn[wn[cwin].cnn].cn); cip=stik_cfg->client_ip; if(cip==0||cip==0x7F000001) cip=cb->lhost; cb=CNgetinfo(dcc[temp].cn); sprintf(temps2,":%s PRIVMSG %s :\001DCC SEND %s %lu %u %lu\001\n",cn[wn[cwin].cnn].nick,usr,dname,cip,cb->lport,length); TCP_send(cn[wn[cwin].cnn].cn,(char *)&temps2,(int16)strlen(temps2)); dcc[temp].rport=cb->lport; dcc[temp].rip=cip; strcpy(dcc[temp].fname,dname); dcc[temp].len=length; strcpy(dcc[temp].avec,usr); if(!conf.boldnicks) sprintf(temps2,"--Initiating DCC send to %s: %s",usr,dname); else sprintf(temps2,"--Initiating DCC send to %s: %s",usr,dname); sendout(temps2,cwin,col[CDCCINFO]); if(conf.autodcd)opendccsenddialog(temp,dname); return 0; }
static void output(int16 cnId, char *o_str, int o_len) { int16 rc; clock_t to = clock() + TIMEOUT * CLK_TCK; /* time to quit at */ /* wait if previous stuff was not yet sent */ while ( (rc = TCP_send(cnId, o_str, o_len)) == E_OBUFFULL) { if (clock() > to) { uiPrintf(uiH, uiPrERR, "output: send timed out"); return; } uiYield(uiH, YIELDMS); } if (rc != E_NORMAL) { uiPrintf(uiH, uiPrERR, "output: %s", get_err_text(rc)); } } /* end output */
/** * @brief This function is the implementation for tcp_recv LwIP callback * @param arg: pointer on a argument for the tcp_pcb connection * @param tpcb: pointer on the tcp_pcb connection * @param pbuf: pointer on the received pbuf * @param err: error information regarding the reveived pbuf * @retval err_t: error code */ static err_t TCP_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err) { err_t ret_err; TCP *es; LWIP_ASSERT("arg != NULL",arg != NULL); es = (TCP *)arg; if (p == NULL) { /* if we receive an empty tcp frame from client => close connection */ es->state = ES_CLOSING; /* remote host closed connection */ if (es->tx == NULL) { TCP_close(tpcb, es); /* we're done sending, close connection */ } else { /* we're not done yet */ tcp_sent(tpcb, TCP_sent); /* send remaining data*/ TCP_send(tpcb, es); /* acknowledge received packet */ } ret_err = ERR_OK; } else if(err != ERR_OK) { /* else : a non empty frame was received from client but for some reason err != ERR_OK */ if (p != NULL) { es->tx = NULL; pbuf_free(p); /* free received pbuf*/ } ret_err = err; } else if(es->state == ES_ACCEPTED) { /* first data chunk in p->payload */ es->state = ES_RECEIVED; es->rx = p; /* store reference to incoming pbuf (chain) */ ret_err = ERR_OK; } else if (es->state == ES_RECEIVED) { if (es->rx) pbuf_chain(es->rx,p); else es->rx = p; ret_err = ERR_OK; } else { /* data received when connection already closed */ tcp_recved(tpcb, p->tot_len); /* Acknowledge data reception */ es->tx = NULL; pbuf_free(p); /* free pbuf and do nothing */ ret_err = ERR_OK; } return ret_err; }
/** * @brief This function implements the tcp_poll LwIP callback function * @param arg: pointer on argument passed to callback * @param tpcb: pointer on the tcp_pcb for the current tcp connection * @retval err_t: error code */ static err_t TCP_poll(void *arg, struct tcp_pcb *tpcb) { err_t ret_err; TCP *es; es = (TCP *)arg; if (es != NULL) { if (es->tx != NULL) { /* there is a remaining pbuf (chain) , try to send data */ TCP_send(tpcb, es); } else { /* no remaining pbuf (chain) */ if (es->state == ES_CLOSING) { TCP_close(tpcb, es); /* close tcp connection */ } } ret_err = ERR_OK; } else { tcp_abort(tpcb); /* nothing to be done */ ret_err = ERR_ABRT; } return ret_err; }
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; }
/* send data to the server on connection cn */ int news_send(int cn, char *data) { int x; char data_string[300]; x=news_discard( cn ); /* discard residual data */ if (x<E_NORMAL) { browser->msg_status(0, x); if (x==E_BADHANDLE&&cn==ncn) { ncn= NO_CONNECTION; } return(x); } sprintf( data_string, "%s\r\n", data ); do { x = TCP_send( cn, data_string, (int16) strlen(data_string) ); } while (x==E_OBUFFULL); /* retry if 'Output Buffer full */ browser->msg_status(5, 0); if (x<E_NORMAL) { fprintf( log, "TCP_send returns %s \n", get_err_text(x)); fflush( log ); browser->msg_status(0, x); } return(x); }
// Exit nicely void my_handler(int s) { TCP_send (&sock, "quit\n", strlen ("quit\n")); close_socket (&sock); exit(0); }
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; }
int ftp_send_file(char *filename, char *command) { int rc,state,x; size_t bytes,total=0,timeout; char buff[512]; FILE *fid; int dtp=E_REFUSE; if (ftp_use_port!='Y') { dtp = ftp_passive_connect(); } /* if (dtp==E_REFUSE) { dtp = ftp_listen_connect(); } */ if (dtp==E_REFUSE) { dtp = ftp_port_connect(); } if (dtp>=0) { rc= ftp_allocate(filename); rc= news_send_command( fpi, command ); /* rc=CNbyte_count(dtp); fprintf( log, "Pre-Listen CNbyte_count returns %d, %s \n", rc, get_err_text(rc)); */ if (CNbyte_count( dtp )==E_LISTEN) { if ((state=ftp_listen( dtp ))>=0) { state = E_NORMAL; } } else { state = TCP_wait_state( dtp, TESTABLISH, 15 ); } if (state==E_NORMAL) { rc = ftp_receive_continuations( fpi, "dummy", FALSE ); fid = fopen( filename, "rb" ); if (fid!=NULL) { while ((bytes=fread( buff, 1, 512, fid))>0) { timeout=clock()+max_to*CLK_TCK; /* timeout */ do { x = TCP_send( dtp, buff, (int16)bytes ); if (x!=0) { if (clock()>timeout) { if (x==E_OBUFFULL) { bytes=-FTP_OBUFF_SIZE; } x=E_CNTIMEOUT; } } if (handle_escape_events()==27) { x=E_USERTIMEOUT; } /* if (Bconstat(2)==-1 && (Bconin(2) & 0xFF)==27) { /* Esc key */ /* x=E_USERTIMEOUT; } */ } while (x==E_OBUFFULL); /* retry if 'Output Buffer full */ total+=bytes; browser->msg_status(8, total); if (x<0) { fprintf( log, "TCP_send returns %s \n", get_err_text(x)); browser->msg_status(0, x); break; } } fclose( fid ); } } else { fprintf( log, "TCP_wait_state returns %s \n", get_err_text(state)); rc=CNbyte_count(dtp); fprintf( log, "CNbyte_count returns %d, %s \n", rc, get_err_text(rc)); rc = state; } TCP_close( dtp, 5 ); /* wait for connection to close */ } else { fprintf( log, "open_connection returns %s \n", get_err_text(dtp)); rc = dtp; } return(rc); }