Ejemplo n.º 1
0
/*
 * Check for enough room in Tx-buffer for a non-blocking socket
 * to transmit without waiting. Only called for SOCK_DGRAM/SOCK_STREAM
 * sockets.
 *
 * If '*len > room', modify '*len' on output to 'room' (the size of
 * bytes left in tx-buf).
 */
static __inline int check_non_block_tx (Socket *socket, int *len)
{
  sock_type *sk;
  int        room;

  if (socket->so_type == SOCK_DGRAM)
       sk = (sock_type*) socket->udp_sock;
  else sk = (sock_type*) socket->tcp_sock;

  room = sock_tbleft (sk);
  if (*len <= room)
     return (0);     /* okay, enough room, '*len' unmodified */

#if 0
  SOCK_YIELD();      /* a small delay to clear up things */
  tcp_tick (sk);

  room = sock_tbleft (sk);
  if (*len <= room)
     return (0);
#endif

  /* Still no room, but cannot split up datagrams (only in ip-fragments)
   */
  if (socket->so_type == SOCK_DGRAM)
     return (-1);

  /* stream: Tx room below (or equal) low-water mark is failure.
   */
  if (*len > 0 && room <= socket->send_lowat)
     return (-1);

  /* streams may be split up, modify '*len'
   */
  *len = room;
  return (0);
}
Ejemplo n.º 2
0
int ftpsession(struct Url *url,struct HTTPrecord *cache,char *uploadfile)
{
 longword host;
 char str[256];
 char buffer[BUFLEN+2];
 tcp_Socket datasocket;
 word dataport=0;
 int rv=0,len;
 char *ptr,*datahostptr,datahost[80];
 char isdir=0,retry=0;//,ascii=0;
 long total=0;

//!!glennmcc: Nov 11, 2007 -- for 'dblp code' below
 int dblp=0;
//!!glennmcc: end

//!!glennmcc: Nov 13, 2007 -- for EZNOS2 fix below
int eznos2=0;
//!!glennmcc: end
int log;

 if(!tcpip)return 0;
 free_socket();

 if((!url->file[0] || url->file[strlen(url->file)-1]=='/') && !uploadfile)
  isdir=1;

 sprintf(str,msg_askdns,url->host);
 outs(str);

 GlobalLogoStyle=0;			//SDL set resolve animation
 host=resolve_fn( url->host, (sockfunct_t) TcpIdleFunc ); //SDL
// host=resolve( url->host );
 if(!host)
 {
  DNSerr(url->host);
  return 0;
 }

// if(!uploadfile) //!glennmcc: Oct 20, 2012 - commented-out to make upload log more verbose
   log=a_open("FTP.LOG",O_BINARY|O_WRONLY|O_CREAT|O_TRUNC,S_IREAD|S_IWRITE);

 GlobalLogoStyle=2;			//SDL set connect animation
 if (!tcp_open( socket, locport(), host, url->port, NULL ))
 {
  sprintf(str,msg_errcon,url->host);
  outs(str);
  return 0;
 }
 sprintf(str,msg_con,url->host,url->port);
 outs(str);
 write(log,str,strlen(str));
 write(log,"\r\n",2);
 sock_wait_established(socket, sock_delay, (sockfunct_t) TcpIdleFunc,
		       &status);		//SDL

 GlobalLogoStyle=1;		//SDL set data animation
 sock_mode( socket, TCP_MODE_ASCII );
 outs(MSG_LOGIN);

 do
 {
  sock_wait_input( socket, sock_delay, (sockfunct_t) TcpIdleFunc,
		   &status );		//SDL
  sock_gets( socket, (unsigned char *)buffer, sizeof( buffer ));
  outs(buffer);
  write(log,buffer,strlen(buffer));
  write(log,"\r\n",2);


//  printf("FTP daemon said>");
//  puts(buffer);
  if ( *buffer != '2' && buffer[0]!=' ') goto quit;
 }
 while(buffer[3]=='-' || buffer[0]==' '); //continued message!

 if(!url->user[0])
  ptr="anonymous";
 else
  ptr=url->user;

 sprintf( str, "USER %s", ptr);
 write(log,str,strlen(str));
 write(log,"\r\n",2);
 sock_puts(socket,(unsigned char *)str);
 sock_wait_input( socket, sock_delay, (sockfunct_t) TcpIdleFunc,
		  &status );		//SDL
 sock_gets( socket, (unsigned char *)buffer, sizeof( buffer ));
 outs(buffer);
 write(log,buffer,strlen(buffer));
 write(log,"\r\n",2);

//  printf("FTP daemon said>");
//  puts(buffer);

//!!glennmcc: May 11, 2005
//removed due to the fact that not all sites use '3'
//see additional info below with respect to anonymous password
// if ( *buffer != '3' ) goto quit;
//!!glennmcc: end

 //open cache filename:

//!glennmcc: Oct 20, 2012 - commented-out to make upload log more verbose
// if(uploadfile)
//  strcpy(cache->locname,"FTP.LOG");
// else
//!glennmcc end: Oct 20, 2012

 {
//!!glennmcc: Oct 22, 2008 -- strchr() was preventing the use of 'CachePath .\cache\'
//  ptr=strchr(cache->locname,'.');
  ptr=strrchr(cache->locname,'.');
//!!glennmcc: end
  if(ptr)
  {
   strcpy(&ptr[1],"FTP");
   strcpy(cache->rawname,cache->locname);
  }
 }

 cache->handle=a_open(cache->locname,O_BINARY|O_WRONLY|O_CREAT|O_TRUNC,S_IREAD|S_IWRITE);
 if(cache->handle<0)
  goto quit;

 strcpy(cache->mime,"text/plain");

 if(url->password[0])
  ptr=url->password;
 else
 {
  if(url->user[0] && !strcmp(url->host,AUTHENTICATION->host)
		  && !strcmp(AUTHENTICATION->realm,"$ftp"))
   ptr=AUTHENTICATION->password;
  else
  {
   ptr=configvariable(&ARACHNEcfg,"FakeFTPeMail",NULL);
   if(!ptr || !strchr(ptr,'@'))
   {
    ptr=configvariable(&ARACHNEcfg,"eMail",NULL);
    if(!ptr)
     ptr="@";
   }
  }
 }

//!!glennmcc: May 11, 2005
//some sites do not require a password after 'anonymous'
//therefer, this entire block is now within this 'if()' so that
//the password (email address), will only be sent if asked for
//!!glennmcc: Nov 13, 2007 -- EZNOS2 says "Enter PASS command"
if (strstr(buffer,"sword") || strstr(buffer,"Enter PASS command"))
//if (strstr(buffer,"sword"))//original line
{
 sprintf( str, "PASS %s", ptr);
 sock_puts(socket,(unsigned char *)str);
 write(log,str,strlen(str));
 write(log,"\r\n",2);
}//!!glennmcc: inserted Mar 02, 2008
//Some servers need the following 'do/while' section,
//therefore, only the section above for sending the password needs to be
//'blocked' when no password was requested.

 do
 {
  sock_wait_input( socket, sock_delay, (sockfunct_t) TcpIdleFunc,
		   &status );		//SDL
  sock_gets( socket, (unsigned char *)buffer, sizeof( buffer ));
  outs(buffer);
  write(log,buffer,strlen(buffer));
  write(log,"\r\n",2);
//  printf("FTP daemon said>");
//  puts(buffer);
if (strstr(buffer,"Enter PASS command")) eznos2=1;

  if (*buffer != '2' && buffer[0]!=' ' && !eznos2)
  {
   write(cache->handle,buffer,strlen(buffer));
   rv=1;
   goto quit;
  }
  else if ((buffer[3]=='-' || buffer[0]==' ') && (isdir || uploadfile))
  {
   strcat(buffer,"\r\n");
   rv=1;
   write(cache->handle,buffer,strlen(buffer));
  }
 }
 while(buffer[3]=='-' || buffer[0]==' ' || buffer[0]=='3'); //continued message!
//}//!!glennmcc: end May 11, 2005 -- removed on Mar 02, 2008

 //ask server where we have to connect:

 sock_puts(socket,(unsigned char *)"PASV");
 sock_wait_input( socket, sock_delay, (sockfunct_t) TcpIdleFunc,
		  &status );		//SDL
 sock_gets( socket, (unsigned char *)buffer, sizeof( buffer ));
 outs(buffer);
 write(log,buffer,strlen(buffer));
 write(log,"\r\n",2);



//  printf("FTP daemon said>");
//  puts(buffer);

 //2xx Entering passive mode (a,b,c,d,x,y)

 if ( *buffer != '2' ) goto quit;
 datahostptr=strchr(buffer,'(');
//!!glennmcc: Nov 13, 2007 -- EZNOS2 doesn't enclose the info in ()
//therefore, if '(' is not found... look for the last 'space'.
 if(!datahostptr) {datahostptr=strrchr(buffer,' '); eznos2=1;}
//if that still fails... 'quit'
//!!glennmcc: end
 if(!datahostptr) goto quit;//original line

 ptr=++datahostptr;
 {
  int carka=0;
  char *portptr=NULL;

  while(*ptr)
  {
   if(*ptr==',')
   {
    carka++;
    if(carka<4)
     *ptr='.';
    else if(carka==4)
    {
     *ptr='\0';
     portptr=ptr+1;
    }
    else if (carka==5)
    {
     *ptr='\0';
     dataport=256*(word)atoi(portptr);  // ,x,y -> 256*x+y
     portptr=ptr+1;
//!!glennmcc: Nov 13, 2007 -- part of above fix for EZNO2 info not in ()
 if(eznos2) dataport+=atoi(portptr);      // ,x,y -> 256*x+y
//!!glennmcc: end
    }
   }
   else if(*ptr==')' && portptr)
   {
//!!glennmcc: Nov 11, 2007 -- some servers have double ')'
// at the end of the port address info...
// this 'dblp code' will prevent that from adding the final set twice
//eg: (99,167,219,186,234,255)) instead of.... (99,167,219,186,234,255)
//without this fix ... 255 gets added a 2nd time and
//we end-up-with... port 60414 instead of the correct port of 60159
    *ptr='\0';
    if(!dblp)//!!glennmcc: Nov 11, 2007
    dataport+=atoi(portptr);      // ,x,y -> 256*x+y
    dblp=1;//!!glennmcc: Nov 11, 2007
   }
   ptr++;
  }
 }

 if(!dataport)
  goto quit;

//!!glennmcc: Aug 31, 2009
//EZNOS2 sends the IP of the machine on a router as datahost
//therefore we need to go back to the original host
if(eznos2)
{
makestr(datahost,url->host,79);
outs(datahost);
Piip();
}
else
//!!glennmcc:end

 makestr(datahost,datahostptr,79);//original line

 retry:

 if(isdir)
 {
  if(url->file[0])
  {
//!!glennmcc: Oct 15, 2007 -- fix problems with CWD on FTP servers
//which interpret the leading '/' as an attempted CD to 'root'
   if(url->file[0]=='/')
   sprintf( str, "CWD %s", url->file+1);
   else
//!!glennmcc: end Oct 15, 2007
   sprintf( str, "CWD %s", url->file);
   sock_puts(socket,(unsigned char *)str);
   do
   {
    sock_wait_input( socket, sock_delay, (sockfunct_t) TcpIdleFunc,
		     &status );		//SDL
    sock_gets( socket, (unsigned char *)buffer, sizeof( buffer ));
    outs(buffer);

//!!glennmcc: Apr 08, 2005 -- commented-out this block
// to fix the problem of 'broken dir listing' when the
// 'FTP welcome message' contains linefeeds
// such as at ftp://ftp.cdrom.com/.2/simtelnet/
/*
    if ( *buffer != '2' && buffer[0]!=' ')
    {
     write(cache->handle,buffer,strlen(buffer));
     rv=1;
     goto quit;
    }
    else if (buffer[3]=='-' || buffer[0]==' ')
*/
//!!glennmcc: end
    {
     strcat(buffer,"\r\n");
     rv=1;
     write(cache->handle,buffer,strlen(buffer));
    }
   }
//!!glennmcc: Apr 08, 2005 -- added a test for !=' ' which is also
// needed for the same fix at ftp://ftp.cdrom.com/.2/simtelnet/
   while(buffer[3]=='-' || buffer[3]!=' ' || buffer[0]==' '); //continued message!
//   while(buffer[3]=='-' || buffer[0]==' '); //continued message!
//!!glennmcc: end

  }
  strcpy(cache->mime,"ftp/list");
  sprintf( str, "LIST");
 }
 else
 {
  char *fnameptr;
  char mimestr[80]="ftp/binary";

  fnameptr=strrchr(url->file,'/');
  if(!fnameptr)
  {
   fnameptr=strrchr(url->file,'\\');
   if(!fnameptr)
    fnameptr=url->file;
   else
    fnameptr++;
   }
   else
    fnameptr++;

  sprintf( str, "TYPE I");
  if(fnameptr)
  {
   char ext[5];
   strcpy(mimestr,"file/");
   strncat(mimestr,fnameptr,70);
   mimestr[79]='\0';
   get_extension(mimestr,ext);
   if(!strncmpi(ext,"TXT",3) || !strncmpi(ext,"HTM",3))
   {
//!!glennmcc: begin June 09, 2002
//optionally upload TXT and HTM in binary mode
    ptr=configvariable(&ARACHNEcfg,"UseBinaryFTP",NULL);
    if(!ptr || toupper(*ptr)=='N')
//!!glennmcc: end
    sprintf( str, "TYPE A");
//    ascii=1;
   }
  }
  strcpy(cache->mime,mimestr);

  sock_puts(socket,(unsigned char *)str);
  write(log,str,strlen(str));
  write(log,"\r\n",2);
  sock_wait_input( socket, sock_delay, (sockfunct_t) TcpIdleFunc,
		  &status );		//SDL
  sock_gets( socket, (unsigned char *)buffer, sizeof( buffer ));
  outs(buffer);
  write(log,buffer,strlen(buffer));
  write(log,"\r\n",2);
//  printf("FTP daemon said>");
//  puts(buffer);
  if ( *buffer != '2' || uploadfile)
  {
   strcat(buffer,"\n");
   write(cache->handle,buffer,strlen(buffer));
  }
  if ( *buffer != '2' )
  {
   rv=1;
   goto quit;
  }
  if(!uploadfile)

//!!glennmcc: Oct 17, 2007 -- fix problems with FTP servers
//which interpret the leading '/' as an attempted CD to 'root'
   if(url->file[0]=='/')
   sprintf( str, "RETR %s", url->file+1);
   else
   sprintf( str, "RETR %s", url->file);
//original single line above this comment
//!!glennmcc: end
  else
   sprintf( str, "STOR %s", url->file);
 }
 sock_puts(socket,(unsigned char *)str);
 write(log,str,strlen(str));
 write(log,"\r\n",2);

//!!glennmcc: Oct 19, 2008 -- back to original fix
//!!glennmcc: Nov 15, 2007 -- always 'close' the connection when done
//with both dir listings and file downloads
//Apr 10, 2007 fix did it only for dir listings
if(isdir || strstr(str,"RETR"))
//if(isdir)
 sock_puts(socket,(unsigned char *)"QUIT");//!!glennmcc: Apr 10, 2007
//!!glennmcc: end

 if(!retry)
 {
  //get file using datahost & dataport
  GlobalLogoStyle=0;		//SDL set resolve animation
  host=resolve_fn( datahost, (sockfunct_t) TcpIdleFunc );	//SDL
//  host=resolve( datahost );
  if(!host)
   goto quit;

  GlobalLogoStyle=2;		//SDL set connect animation
  if (!tcp_open( &datasocket, locport(), host, dataport, NULL ))
  {
   sprintf(str,msg_errcon,datahost);
   outs(str);
   goto quit;
  }
  sprintf(str,msg_con,datahost,dataport);
  outs(str);
  write(log,str,strlen(str));
  write(log,"\r\n",2);

  //wait for datasocket to open:
  sock_wait_established(&datasocket, sock_delay, (sockfunct_t) TcpIdleFunc,
			&status);	//SDL

//!!glennmcc: Sep 27, 2008 -- increase D/L speed on cable & DSL
//many thanks to 'mik' for pointing me in the right direction. :)
{
#ifdef DEBUG
 char sp[80];
 sprintf(sp,"Available stack = %u bytes",_SP);
 outs(sp);
 Piip(); Piip();
#endif
 if(_SP>(1024*SETBUFSIZE))
 {
  char setbuf[1024*SETBUFSIZE];
  sock_setbuf(&datasocket, (unsigned char *)setbuf, 1024*SETBUFSIZE);
 }
}
//!!glennmcc: end

  GlobalLogoStyle=1;		//SDL set data animation
 }
 //wait for "110 openning connection" (or "550 ....error....")
 sock_wait_input( socket, sock_delay, (sockfunct_t) TcpIdleFunc,
		  &status );		//SDL

 sock_gets( socket, (unsigned char *)buffer, sizeof( buffer ));
 outs(buffer);
 write(log,buffer,strlen(buffer));
 write(log,"\r\n",2);


// printf("FTP daemon said>");
// puts(buffer);
 if ( *buffer != '1' || uploadfile)
 {
  strcat(buffer,"\n");
  write(cache->handle,buffer,strlen(buffer));
 }

 if ( *buffer != '1' )
 {
  if(!strncmp(buffer,"550",3) && !retry)
  {
   retry=1;
   isdir=1-isdir;
   if(isdir)
    strcat(url->file,"/");
   else
   {
    int i=strlen(url->file);
    if(i>0 && url->file[i-1]=='/')
     url->file[i-1]='\0';
   }
   goto retry;
  }

  strcpy(cache->mime,"text/plain");
  rv=1;
  goto dataquit;
 }

 if(!uploadfile) //-------------------------------------- download ---------------
 {
  while ( 1 )
  {
   xChLogoTICK(1);
   if(GUITICK())
    if(GLOBAL.gotolocation || GLOBAL.abort)
     goto dataquit;

   if (sock_dataready( &datasocket ))
   {
    len = sock_fastread( &datasocket, (unsigned char*)buffer, BUFLEN );
    write(cache->handle,buffer,len);
    total+=len;
    sprintf(str,MSG_BYTESR,MSG_DOWNLD,total);
    outs(str);
   }
   else
    sock_tick( &datasocket, &status ); //shift TCP/IP
  }
 }
 else //-------------------------------------- upload ------------------
 {
  int f,lenread,done;
  long length;
  char pom[256];

/*  if(ascii)
   f=a_sopen(uploadfile,O_RDONLY|O_TEXT, SH_DENYNO, S_IREAD);
  else*/
   f=a_sopen(uploadfile,O_RDONLY|O_BINARY, SH_DENYNO, S_IREAD);

  if(f<0)
   goto dataquit;

  lenread=done=0;
  length=0l;

  {
   long filel=a_filelength(f);
   while(1)
   {
    sprintf(pom,MSG_UPLOAD,length,filel);
    outs(pom);
//!!glennmcc:Oct 23, 2008 -- 'reversed the logic'
// to keep from overflowing at 21megs
    if(filel>100)
    percentbar((int)(length/(filel/100)));
//  percentbar((int)(100*length/filel));
    lenread=a_read(f,buffer,BUFLEN);
    length+=lenread;
    if(lenread<=0)
     done=1;

    //wait until we can write to socket:
    while(sock_tbleft(&datasocket)<lenread) //SDL
//    while( datasocket.datalen > 1024)
    {
     sock_tick(&datasocket,&status);
     xChLogoTICK(1); // animation of logo
     if(GUITICK())
      goto dataquit;
    }

    if(done)
    {
     sock_close( &datasocket );
     a_close(f);
     goto dataclose;
    }

    sock_fastwrite(&datasocket,(unsigned char *)buffer,lenread);
    sock_tick(&datasocket,&status);
   }//loop
  }
 }


dataquit:
//!!glennmcc: Nov 15,  2007 -- removed sock_abort because it was
// sometimes preventing the connection from being closed,
// therefore preventing access to several files within a short time
// due to too many connections open from the same IP
// sock_abort( &datasocket );//original line
//!!glennmcc: end

dataclose:
 outs(MSG_CLOSE);

 sock_puts(socket,(unsigned char *)"QUIT");//!!glennmcc: Dec 04, 2006
 sock_wait_closed( &datasocket, sock_delay, (sockfunct_t) TcpIdleFunc,
		   &status );		//SDL

 if(uploadfile)
 {
  sock_wait_input( socket, sock_delay, (sockfunct_t) TcpIdleFunc,
		   &status );		//SDL
  sock_gets( socket, (unsigned char *)buffer, sizeof( buffer ));
  outs(buffer);
 }

quit:
  sock_puts(socket,(unsigned char *)"QUIT");
  sock_close( socket );
  closing[socknum]=1;
  sock_keepalive[socknum][0]='\0';
//    sock_wait_closed( socket, sock_delay, NULL, &status );

//we will better wait because we are about to deallocate datasocket

sock_err:
    switch (status) {
	case 1 : /* foreign host closed */
		 write(log,MSG_CLOSE,strlen(MSG_CLOSE));
		 write(log,"\r\n",2);
		 close(log);
		 break;
	case -1: /* timeout */
		 sprintf(str,MSG_TCPERR, sockerr(socket));
		 outs(str);
		 write(log,str,strlen(str));
		 write(log,"\r\n",2);
		 close(log);
		 break;
    }

 if(total)
 {
  cache->knowsize=1;
  cache->size=total;
  rv=1;
 }
 if(cache->handle>=0)
 {
  a_close(cache->handle);
  rv=1;
 }

 return rv;
}
Ejemplo n.º 3
0
/*
 *  Raw IP transmitter
 */
static int ip_transmit (Socket *socket, const void *tx, int len)
{
  eth_address eth;
  u_long      dest;
  unsigned    tx_len, tx_room;
  sock_type  *sk = (sock_type*)socket->udp_sock;

  struct ip   *ip  = (struct ip*) tx;
  const  BYTE *buf = (const BYTE*) tx;
  WORD   flags     = 0;
  DWORD  offset;
  UINT   h_len, o_len;

  tcp_tick (NULL);        /* process other TCBs too */
  tcp_Retransmitter (1);

  /* This should never happen
   */
  if (ip && (socket->so_state & SS_NBIO) &&
      sock_tbleft(sk) < (len + socket->send_lowat))
  {
    SOCK_DEBUGF ((socket, ", EWOULDBLOCK"));
    SOCK_ERR (EWOULDBLOCK);
    return (-1);
  }

  if (ip)
  {
    offset = ntohs (ip->ip_off);
    flags  = offset & ~IP_OFFMASK;
    offset = (offset & IP_OFFMASK) << 3; /* 0 <= ip_ofs <= 65536-8 */
  }

  SOCK_DEBUGF ((socket, ", %s / Raw",
                inet_ntoa(socket->remote_addr->sin_addr)));

  if (ip && (socket->inp_flags & INP_HDRINCL))
  {
    dest    = ip->ip_dst.s_addr;
    tx_len  = len;
    tx_room = mtu;
  }
  else
  {
    dest    = socket->remote_addr->sin_addr.s_addr;
    tx_len  = len + sizeof (*ip);
    tx_room = mtu + sizeof (*ip);
  }

  if (!dest || !_arp_resolve(ntohl(dest),&eth,0))
  {
    SOCK_DEBUGF ((socket, ", no route"));
    SOCK_ERR (EHOSTUNREACH);
    STAT (ipstats.ips_noroute++);
    return (-1);
  }

#if defined(USE_FRAGMENTS)
  if (!(socket->inp_flags & INP_HDRINCL) &&
      tx_len + socket->ip_opt_len > tx_room)
  {
    sk = (sock_type*)socket->raw_sock;

    if (flags & IP_DF)
    {
      SOCK_DEBUGF ((socket, ", EMSGSIZE"));
      SOCK_ERR (EMSGSIZE);
      STAT (ipstats.ips_toolong++);
      return (-1);
    }
    return SEND_IP_FRAGMENTS (sk, sk->raw.ip_type, dest, buf, len);
  }
#else
  if (!(socket->inp_flags & INP_HDRINCL) &&
      tx_len + socket->ip_opt_len > tx_room)
  {
    SOCK_DEBUGF ((socket, ", EMSGSIZE"));
    SOCK_ERR (EMSGSIZE);
    STAT (ipstats.ips_toolong++);
    return (-1);
  }
#endif

  ip = (struct ip*) _eth_formatpacket (&eth, IP_TYPE);

  if (socket->inp_flags & INP_HDRINCL)
  {
    memcpy (ip, buf, len);
    if (ip->ip_src.s_addr == 0)
    {
      ip->ip_src.s_addr = gethostid();
      ip->ip_sum = 0;
      ip->ip_sum = ~checksum ((void*)ip, ip->ip_hl << 2);
    }
    if (ip->ip_sum == 0)
        ip->ip_sum = ~checksum ((void*)ip, ip->ip_hl << 2);
  }
  else
  {
    if (socket->ip_opt && socket->ip_opt_len > 0)
    {
      BYTE *data;

      o_len = min (socket->ip_opt_len, sizeof(socket->ip_opt->ip_opts));
      h_len = sizeof(*ip) + o_len;
      data  = (BYTE*)ip + h_len;
      memcpy (ip+1, &socket->ip_opt->ip_opts, o_len);
      memcpy (data, buf, len);
      tx_len += o_len;
      if (socket->ip_opt->ip_dst.s_addr)   /* using source routing */
         dest = socket->ip_opt->ip_dst.s_addr;
    }
    else
    {
      if (buf)
         memcpy (ip+1, buf, len);
      h_len = sizeof (*ip);
    }

    ip->ip_v   = IPVERSION;
    ip->ip_hl  = h_len >> 2;
    ip->ip_tos = socket->ip_tos;
    ip->ip_len = htons (tx_len);
    ip->ip_id  = _get_ip_id();
    ip->ip_off = 0;
    ip->ip_ttl = socket->ip_ttl;
    ip->ip_p   = socket->so_proto;

    ip->ip_src.s_addr = gethostid();
    ip->ip_dst.s_addr = dest;

    ip->ip_sum = 0;
    ip->ip_sum = ~checksum (ip, h_len);
  }

  DEBUG_TX (NULL, ip);

  if (!_eth_send(tx_len))
  {
    SOCK_DEBUGF ((socket, ", ENETDOWN"));
    SOCK_ERR (ENETDOWN);
    return (-1);
  }
  if (buf)
     buf += tx_len;

  return (len);
}
Ejemplo n.º 4
0
/*
 *  UDP transmitter
 */
static int udp_transmit (Socket *socket, const void *buf, int len)
{
  sock_type *sk   = (sock_type*) socket->udp_sock;
  u_long     dest = socket->remote_addr->sin_addr.s_addr;
  int        tx_room, rc;
  int        is_bcast, is_multi;

  if (!tcp_tick(sk))
  {
    socket->so_state |= SS_CANTSENDMORE;
    SOCK_DEBUGF ((socket, ", ENOTCONN (can't send)")); /* !! or EPIPE */
    SOCK_ERR (ENOTCONN);
    return (-1);
  }
  tcp_Retransmitter (1);

  if ((socket->so_state & SS_NBIO) && check_non_block_tx(socket,&len) < 0)
  {
    SOCK_DEBUGF ((socket, ", EWOULDBLOCK"));
    SOCK_ERR (EWOULDBLOCK);
    return (-1);
  }

  is_bcast = (dest == INADDR_BROADCAST || dest == INADDR_ANY);
  is_multi = IN_MULTICAST (ntohl(socket->remote_addr->sin_addr.s_addr));

  SOCK_DEBUGF ((socket, ", %s (%d) / UDP %s",
                inet_ntoa(socket->remote_addr->sin_addr),
                ntohs(socket->remote_addr->sin_port),
                is_multi ? "(mc)" : ""));

  if (len == 0)   /* 0-byte probe packet */
     return ip_transmit (socket, NULL, 0);

  tx_room = sock_tbleft (sk);  /* always MTU-28 */

  /* Special tests for broadcast messages
   */
  if (is_bcast)
  {
    if (len > tx_room)        /* don't allow fragments */
    {
      SOCK_DEBUGF ((socket, ", EMSGSIZE"));
      SOCK_ERR (EMSGSIZE);
      STAT (ipstats.ips_odropped++);
      return (-1);
    }
    if (_pktserial)           /* Link-layer doesn't allow broadcast */
    {
      SOCK_DEBUGF ((socket, ", EADDRNOTAVAIL"));
      SOCK_ERR (EADDRNOTAVAIL);
      STAT (ipstats.ips_odropped++);
      return (-1);
    }
  }

  /* set new TTL if setsockopt() used before sending to Class-D socket
   */
  if (is_multi)
     sk->udp.ttl = socket->ip_ttl;

#if defined(USE_FRAGMENTS)
  if ((long)len > USHRT_MAX - sizeof(udp_Header))
  {
    SOCK_DEBUGF ((socket, ", EMSGSIZE"));
    SOCK_ERR (EMSGSIZE);
    STAT (ipstats.ips_toolong++);
    return (-1);
  }

  if (len > tx_room)
     return SEND_IP_FRAGMENTS (sk, UDP_PROTO, dest, buf, len);
#endif

  rc = sock_write (sk, (BYTE*)buf, len);
  if (rc <= 0)    /* error in udp_write() */
  {
    SOCK_DEBUGF ((socket, ", ENETDOWN"));
    SOCK_ERR (ENETDOWN);
    return (-1);
  }
  return (rc);
}
Ejemplo n.º 5
0
/*
 *  TCP transmitter
 */
static int tcp_transmit (Socket *socket, const void *buf, int len, int flags)
{
  sock_type *sk = (sock_type*)socket->tcp_sock;

  tcp_tick (sk);
  tcp_Retransmitter (1);

  if (sk->tcp.state < tcp_StateESTAB || sk->tcp.state >= tcp_StateLASTACK)
  {
    socket->so_state |= SS_CANTSENDMORE;
    SOCK_DEBUGF ((socket, ", ENOTCONN (%s)",  /* !! or EPIPE */
                 (sk->tcp.locflags & LF_GOT_FIN) ?
                   "got FIN" : "can't send"));
    SOCK_ERR (ENOTCONN);
    return (-1);
  }

  if (socket->so_state & SS_NBIO)
  {
    int in_len = len;

    if (check_non_block_tx(socket,&len) < 0)
    {
      SOCK_DEBUGF ((socket, ", EWOULDBLOCK"));
      SOCK_ERR (EWOULDBLOCK);
      return (-1);
    }
    if (in_len != len)
       SOCK_DEBUGF ((socket, " [%d]", len)); /* trace "len=x [y]" */
  }

  SOCK_DEBUGF ((socket, ", %s (%d) / TCP",
                inet_ntoa(socket->remote_addr->sin_addr),
                ntohs(socket->remote_addr->sin_port)));

#if 0
  /* Must wait for room in send buffer
   */
  if ((flags & MSG_WAITALL) || len > sock_tbleft(sk))
       len = sock_write (sk, (BYTE*)buf, len);
  else len = sock_fastwrite (sk, (BYTE*)buf, len);

#else
  /* This is more efficient. The above sock_fastwrite() would
   * effectively turn off Nagle's algorithm.
   */
  ARGSUSED (flags);
  len = sock_write (sk, (BYTE*)buf, len);
#endif

  if (len <= 0)    /* error in tcp_write() */
  {
    if (sk->tcp.state != tcp_StateESTAB)
    {
      SOCK_DEBUGF ((socket, ", ENOTCONN"));
      SOCK_ERR (ENOTCONN);   /* maybe EPIPE? */
    }
    else
    {
      SOCK_DEBUGF ((socket, ", ENETDOWN"));
      SOCK_ERR (ENETDOWN);
    }
    return (-1);
  }
  return (len);
}
Ejemplo n.º 6
0
int xsendmail(struct Url *url, char helo, char logfile)

{
 longword host;
 long length;
//!!glennmcc: Feb 05, 2008 -- increased from 128 to 512
 char str[512/*128*/],pom[512/*128*/];

//!!glennmcc Apr 30, 2004 -- for AuthSMTP
 char AuthSMTPusername[128]="",AuthSMTPpassword[128]="";
//!!glennmcc: end

 char buffer[BUFLEN];
 struct ffblk ff;
 char filename[80];
 int rv=0;             //default return value == error
 int f,nomoremail;
 int done,lenread,i,j,lastcarka,carka,field,success,u,z;
 int status,err=0;
 char *ptr;
 int log;

//!!glennmcc & Ray: Dec 14, 2006
 int bcc=0;
//!!glennmcc & Ray: end

 if(!tcpip)
  return 0;

 nomoremail=findfirst(&url->file[1],&ff,0);
 if(nomoremail)
  return 1;

 if(logfile)
  log= a_open( "SMTP.LOG" , O_BINARY|O_WRONLY|O_CREAT|O_TRUNC,S_IREAD|S_IWRITE );
 else
  log=-1;

 free_socket();

 //reset requests on IDENT port:

 reset_detected=0;
 tcp_listen(sock[1-socknum], 113, 0, 0, (dataHandler_t) resetport , 0);

 sprintf(str,msg_askdns,url->host);
 outs(str);

 GlobalLogoStyle=0;		//SDL set resolve animation
 host = resolve_fn(url->host, (sockfunct_t) TcpIdleFunc);	//SDL
// host = resolve(url->host);
 if(!host)
 {
  DNSerr(url->host);
  return 0;
 }

 sprintf(str,msg_con,url->host,url->port);
 outs(str);

 GlobalLogoStyle=2;		//SDL set connect animation
 if (!tcp_open( socket, locport(), host, url->port, NULL ))
 {
  sprintf(str,msg_errcon,url->host);
  outs(str);
  return 0;
 }

 sock_wait_established( socket, sock_delay, (sockfunct_t) TcpIdleFunc,
			&status);		//SDL
 GlobalLogoStyle=1;		//SDL set data animation

 sock_mode( socket, TCP_MODE_ASCII );
 outs(MSG_SMTP);

 //wait for daemon to appear:
 do
 {
  sock_wait_input( socket, sock_delay, (sockfunct_t) TcpIdleFunc,
		   &status );		//SDL
  sock_gets( socket, (unsigned char *)buffer, sizeof( buffer ));
  outs(buffer);
  if(log!=-1)
  {
   write(log,buffer,strlen(buffer));
   write(log,"\r\n",2);
  }
  if ( *buffer != '2' ) goto quit;
 }
 while(buffer[3]=='-'); //continued message!

 if(log!=-1 && reset_detected)
 {
  strcpy(str,MSG_IDENT);
  write(log,str,strlen(str));
  write(log,"\r\n",2);
 }

 if(helo)
// if(helo!=0)
 {
  //HELO protocol
  char *mydomain=strchr(url->user,'@');
  if(mydomain)
   mydomain++; // xx@->yyy
  else
//!!glennmcc: Feb 22, 2006 -- use host domain instead of user
  {
   mydomain=strchr(url->host,'.');
   mydomain++; // xx@->yyy
  }
//   mydomain=url->user;
//!!glennmcc: end

//!!glennmcc: begin Nov 09, 2003 --- EHLO == Authenticated SMTP
//finally finished it on Apr 30, 2004 ;-)
if(helo==2)
{
//!!glennmcc: Feb 14, 2006 -- use 'authuser' for AuthSMTP
     mydomain=strrchr(url->authuser,'@');
     if(mydomain)
      mydomain++; // xx@->yyy
      else
//!!glennmcc: Feb 22, 2006 -- use host domain instead of authuser
      {
       mydomain=strchr(url->host,'.');
       mydomain++; // xx@->yyy
      }
//     mydomain=url->authuser;
//!!glennmcc: end

  sprintf( str, "EHLO %s", mydomain);
  outs(str);
  if(log!=-1)
  {
   write(log,str,strlen(str));
   write(log,"\r\n",2);
  }
  sock_puts(socket,(unsigned char *)str);


  do
  {
   sock_wait_input( socket, sock_delay, (sockfunct_t) TcpIdleFunc,
		    &status );		//SDL
   sock_gets( socket, (unsigned char *)buffer, sizeof( buffer ));
   outs(buffer);
   if(log!=-1)
   {
    write(log,buffer,strlen(buffer));
    write(log,"\r\n",2);
   }
   if ( *buffer != '2' ) goto quit;
  }
  while(buffer[3]=='-'); //continued message!

  sprintf( str, "AUTH LOGIN");
  outs(str);
  if(log!=-1)
  {
   write(log,str,strlen(str));
   write(log,"\r\n",2);
  }
  sock_puts(socket,(unsigned char *)str);

  do
  {
   sock_wait_input( socket, sock_delay, (sockfunct_t) TcpIdleFunc,
		    &status );		//SDL
   sock_gets( socket, (unsigned char *)buffer, sizeof( buffer ));
   outs(buffer);
   if(log!=-1)
   {
    write(log,buffer,strlen(buffer));
    write(log,"\r\n",2);
   }
   if ( *buffer != '3' ) goto quit;
  }
  while(buffer[3]=='-'); //continued message!
//!!glennmcc: Sept 17, 2004
// changed so that "email" will always get used for "mail from"
//  base64code((unsigned char *)url->user,AuthSMTPusername);
  base64code((unsigned char *)url->authuser,AuthSMTPusername);
//!!glennmcc: end
  sprintf( str, AuthSMTPusername);
  outs(str);
  if(log!=-1)
  {
   write(log,str,strlen(str));
   write(log,"\r\n",2);
  }
  sock_puts(socket,(unsigned char *)str);

  do
  {
   sock_wait_input( socket, sock_delay, (sockfunct_t) TcpIdleFunc,
		    &status );		//SDL
   sock_gets( socket, (unsigned char *)buffer, sizeof( buffer ));
   outs(buffer);
   if(log!=-1)
   {
    write(log,buffer,strlen(buffer));
    write(log,"\r\n",2);
   }
   if ( *buffer != '3' ) goto quit;
  }
  while(buffer[3]=='-'); //continued message!

//!!glennmcc: Feb 17, 2006 -- switch to new variable 'authpassword'
  base64code((unsigned char *)url->authpassword,AuthSMTPpassword);
//  base64code((unsigned char *)url->password,AuthSMTPpassword);
  sprintf( str, AuthSMTPpassword);
  outs(str);
  if(log!=-1)
  {
   write(log,str,strlen(str));
   write(log,"\r\n",2);
  }
  sock_puts(socket,(unsigned char *)str);

  do
  {
   sock_wait_input( socket, sock_delay, (sockfunct_t) TcpIdleFunc,
		    &status );		//SDL
   sock_gets( socket, (unsigned char *)buffer, sizeof( buffer ));
   outs(buffer);
   if(log!=-1)
   {
    write(log,buffer,strlen(buffer));
    write(log,"\r\n",2);
   }
   if ( *buffer != '2' ) goto quit;
  }
  while(buffer[3]=='-'); //continued message!
}
else //begin else HELO
{
//!!glennmcc: end

  sprintf( str, "HELO %s", mydomain);
  outs(str);
  if(log!=-1)
  {
   write(log,str,strlen(str));
   write(log,"\r\n",2);
  }
  sock_puts(socket,(unsigned char *)str);

  do
  {
   sock_wait_input( socket, sock_delay, (sockfunct_t) TcpIdleFunc,
		    &status );		//SDL
   sock_gets( socket, (unsigned char *)buffer, sizeof( buffer ));
   outs(buffer);
   if(log!=-1)
   {
    write(log,buffer,strlen(buffer));
    write(log,"\r\n",2);
   }
   if ( *buffer != '2' ) goto quit;
  }
  while(buffer[3]=='-'); //continued message!

}//!!glennmcc: end of else HELO

 }

 //do for all messages
 while (!nomoremail)
 {
  //process msg body

  if(err)
  {
   outs("RSET");
   if(log!=-1)
   {
    write(log,"RSET\r\n",6);
   }
   sock_puts(socket,(unsigned char *)"RSET");

   do
   {
    sock_wait_input( socket, sock_delay, (sockfunct_t) TcpIdleFunc,
		    &status );		//SDL
    sock_gets( socket, (unsigned char *)buffer, sizeof( buffer ));
    outs(buffer);
    if(log!=-1)
    {
     write(log,buffer,strlen(buffer));
     write(log,"\r\n",2);
    }
    if ( *buffer != '2' ) goto quit; //cannot reset ??
   }
   while(buffer[3]=='-'); //continued message!
   err=0;
  }

  strcpy(filename,&url->file[1]);
  ptr=strrchr(filename,'\\');
  if(ptr)
   ptr++; //point after "\"
  else
   ptr=filename; //current dir ? (not likelky case)
  strcpy(ptr,ff.ff_name);

  //open file
  f=a_fast_open(filename, O_TEXT|O_RDONLY,0);
  if(f<0)
   goto cont; //go to next message!


  //start SMTP
  sprintf( str, "MAIL FROM: <%s>", url->user);

  outs(str);
  if(log!=-1)
  {
   write(log,str,strlen(str));
   write(log,"\r\n",2);
  }
  sock_puts(socket,(unsigned char *)str);
  do
  {
   sock_wait_input( socket, sock_delay, (sockfunct_t) TcpIdleFunc,
		    &status );		//SDL
   sock_gets( socket, (unsigned char *)buffer, sizeof( buffer ));
   outs(buffer);
   if(log!=-1)
   {
    write(log,buffer,strlen(buffer));
    write(log,"\r\n",2);
   }
   if ( *buffer != '2' ) goto quit; //I am not accepted ?!
  }
  while(buffer[3]=='-'); //continued message!

  //process msg header
  done=lenread=j=lastcarka=carka=field=success=u=z=0;
  i=1; //force reading!
  do
  {

   if(i>=lenread)
   {
    lenread=a_read(f,buffer,BUFLEN);
    if(lenread<=0)
     done=1;
    i=0;
   }

   str[j]=buffer[i++];

   if(str[j]=='\"')u=1-u;
   else
   if(str[j]=='(')z=1;
   else
   if(str[j]==')')z=0;
   else
   if(str[j]==',' && !u && !z)carka=1;

   if(j>=127 || str[j]=='\n' || carka || done)
   {
    str[j]='\0';

    if(!str[0] && !lastcarka) //empty line -> end of message header
    {
     done=1;
     field=0;
    }
    else
    if(!strncmpi("TO:",str,3) || !strncmpi("CC:",str,3))
    {
     ptr=&str[3];
     field=1;
    }
    else
    if(!strncmpi("BCC:",str,4))
    {
//!!glennmcc & Ray: Dec 14, 2006
     bcc=1;
//!!glennmcc & Ray: end
     ptr=&str[4];
     field=1;
    }
    else
    if(field && (lastcarka || str[0]==' '))
     ptr=str;
    else
     field=0;

    if(field)
    {
     struct ib_editor expandlist;
     int rcpt;

     if(ptr[0]==' ' && ptr[1]=='@') //expand mailing list
     {
      makestr(expandlist.filename,&ptr[2],79);
      if(ie_openf(&expandlist,CONTEXT_TMP)==1)
       field=-1;
      else
      {
       field=0;
       expandlist.filename[0]='\0';
      }
     }
     else
      expandlist.filename[0]='\0';

     if(field!=0)
      rcpt=1;
     else
      rcpt=0;

     while(rcpt)
     {
      if(field==1) //address in ptr
       rcpt=0;
      else
      {
       ptr=ie_getline(&expandlist,expandlist.y++);
       if(expandlist.y>=expandlist.lines)
       {
	rcpt=0;
	field=0;
       }
      }

      cutaddress(ptr);
      if(*ptr)
      {
       //add SMTP recipient
       sprintf( pom, "RCPT TO: <%s>", ptr);
       outs(pom);
       if(log!=-1)
       {
	write(log,pom,strlen(pom));
	write(log,"\r\n",2);
       }
       sock_puts(socket,(unsigned char *)pom);
       sock_wait_input( socket, sock_delay, (sockfunct_t) TcpIdleFunc,
			&status );		//SDL
       sock_gets( socket, (unsigned char *)pom, sizeof( pom ));
       outs(pom);
       if(log!=-1)
       {
	write(log,pom,strlen(pom));
	write(log,"\r\n",2);
       }
       if(*pom == '2')
	success++;
      }
     }//loop

     if(expandlist.filename[0])
     {
      ie_killcontext(CONTEXT_TMP); //clear temporary file
//      ie_closef(&expandlist);
     }
    }//end temp variables

    lastcarka=carka;
    carka=0;
    j=0;
   }
   else
    j++;

  }
  while(!done);

  if(!success) //cannot send to any recipient ?
  {
   err=1;
   goto cont;
  }


  //process msg body
  outs("DATA");
  if(log!=-1)
  {
   write(log,"DATA\r\n",6);
  }
  sock_puts(socket,(unsigned char *)"DATA");
  sock_wait_input( socket, sock_delay, (sockfunct_t) TcpIdleFunc,
		   &status );		//SDL
  sock_gets( socket, (unsigned char *)buffer, sizeof( buffer ));
  outs(buffer);
  if(log!=-1)
  {
   write(log,buffer,strlen(buffer));
   write(log,"\r\n",2);
  }
  if ( *buffer != '3')
  {
   err=1;
   goto cont; //failed ? try next message!
  }

  a_lseek(f,0L,SEEK_SET); //reset file
  j=lenread=done=0;
  i=1;
  length=0;
  {
   long fll=a_filelength(f);
   do
   {
    if(i>=lenread)
    {
     sprintf(pom,MSG_SEND,length,fll);
     outs(pom);
/*
//!!glennmcc:Oct 23, 2008 -- 'reversed the logic'
// to keep from overflowing at 21megs
     if(fll>100)
     percentbar((int)(length/(fll/100)));
*/
   percentbar((int)(100*length/fll));
     lenread=a_read(f,buffer,BUFLEN);
     length+=lenread;
     if(lenread<=0)
      done=1;
     i=0;
 //    printf("\nlenread=%d \n",lenread);
//!!glennmcc & Ray: Dec 14, 2006 -- bypass Bcc:
if(bcc)
  {
   char *ptr = strstr(buffer, "Bcc:");//Ray
   if(ptr)
     {
//Ray's method replaces the address(s) with spaces
//it also looks for '\r' (carrage return) as the end of the Bcc line
//      ptr += 4;                        // PASS 'Bcc:'
//    while(*ptr && *ptr != '\r')
//    *ptr++ = ' ';
//printf("After: \n\n%40s", ptr); getch(); // SEE THE RESULT

//!!glennmcc: this method works better by 'nulling-out' the Bcc line and
//looking for '\n' (new line) as the end of the Bcc line
/* method #1 */
/**/
      if(*ptr--=='\n')*ptr='\0';//remove previous '\r\n' if it exists
      do
      {*ptr='\0'; ptr++;}
      while( *ptr && *ptr!='\n');
/**/
/* end method #1 */

/* method *2 */
/*
      if(*ptr--=='\n')*ptr='\0';//remove previous '\r\n' if it exists
      while(*ptr && *ptr!='\n')
      *ptr++ ='\0';
      *ptr='\0';//also remove this '\n'
*/
/* end method #2 */
      bcc=0;
     }
  }
//!!glennmcc & Ray: end
    }
    str[j]=buffer[i++];

//!!glennmcc: Feb 05, 2008 -- prevent character from being lost at line break
//by breaking at a space instead of 127 explicitly
//also increased line length limit to the next space past 200
//instead of just 127 explicitly
    if((j>=200 && str[j]==' ') || str[j]=='\n' || done)
//  if(j>=127 || str[j]=='\n' || done) //original line
//!!glennmcc: end
    {

     str[j]='\0';
     length++; //ASCI mode !!!

     //wait until we can write to socket:
     while(sock_tbleft(socket)<j+1)	//SDL
//     while( socket->datalen > 1024)
     {
      sock_tick(socket,&status);
      xChLogoTICK(1); // animace loga
      if(GUITICK())
      goto quit;
     }

     if (str[0]=='.')			//SDL always double up
      sock_putc(socket,'.');  		//SDL leading periods
     sock_puts(socket,(unsigned char *)str);
     if(log!=-1)
     {
      write(log,str,strlen(str));
      write(log,"\r\n",2);
     }
     sock_tick(socket,&status);
     j=0;
    }
    else
     j++;
   }
   while(!done);
  }
  a_close(f);

  sock_puts(socket,(unsigned char *)".");
  if(log!=-1)
  {
   write(log,".\r\n",3);
  }
  sock_wait_input( socket, sock_delay, (sockfunct_t) TcpIdleFunc,
		   &status );		//SDL
  sock_gets( socket, (unsigned char *)buffer, sizeof( buffer ));
  outs(buffer);
  if(log!=-1)
  {
   write(log,buffer,strlen(buffer));
   write(log,"\r\n",2);
  }
  if ( *buffer != '2' )
  {
   err=1;
  }
  else
  {
   //delete or rename
   ptr=strrchr(filename,'\\');
   if(!ptr)
    ptr=filename;
   else
    ptr++;

   if(*ptr=='!')
    unlink(filename);
   else
   {
    strcpy(str,filename);
    ptr=strrchr(str,'.');
    if(ptr)
     strcpy(&ptr[1],"SNT");
    rename(filename,str);
   }
  }//endif

  cont:
  nomoremail=findnext(&ff);
 }//loop - next message

 rv=1;

quit:
    outs("QUIT");
    if(log!=-1)
    {
     write(log,"QUIT\r\n",6);
     close(log);
    }
    sock_puts(socket,(unsigned char *)"QUIT");
    sock_close( socket );
    closing[socknum]=1;
    sock_keepalive[socknum][0]='\0';

sock_err:
    switch (status) {
	case 1 : /* foreign host closed */
		 break;
	case -1: /* timeout */
                 sprintf(str,MSG_TCPERR, sockerr(socket));
                 outs(str);
		 break;
    }
 return rv;
}
Ejemplo n.º 7
0
void main()
{
	int state;
	long timeout;
	int bytes_read;

	static char buffer[64];
	static tcp_Socket socket;

	// Initialize I/O to use PowerCoreFLEX prototyping board
	brdInit();

	serXopen(BAUD_RATE);				// set up serial port
   serMode(0);

	sock_init();						// initialize DCRTCP
	tcp_reserveport(PORT);			// set up PORT for SYN Queueing
											// which will hold a pending connection
											// until we are ready to process it.
	state=CONNECTION_INIT;

	while(1) {
		if(state==CONNECTION_OPEN) {
			if(debounce_key()) {
				sock_close(&socket);
				state=CONNECTION_CLOSED;
			}
		}
		/*
		 *		Make sure that the connection hasn't closed on us.
		 *
		 */

		if(tcp_tick(&socket)==0 && state!=CONNECTION_INIT) {
				sock_close(&socket);
				state=CONNECTION_CLOSED;
		}

		switch(state) {
			case CONNECTION_INIT:
				tcp_listen(&socket,PORT,0,0,NULL,0);
				state=CONNECTION_LISTEN;
				break;

			case CONNECTION_LISTEN:
				if(sock_established(&socket)) {
					state=CONNECTION_OPEN;
					timeout=SEC_TIMER+TIMEOUT;
					serXrdFlush();
					serXwrFlush();
					led(RESET_LED);
 			}
				break;

			case CONNECTION_OPEN:

				/*
				 *		close the socket on a timeout
				 *
				 */
				if((long) (SEC_TIMER-timeout) >= 0) {
					sock_close(&socket);
					state=CONNECTION_CLOSED;
					break;
				}

				/*
				 *		read as many bytes from the socket as we have
				 *		room in the serial buffer. Also strip out the
				 *    telnet commands.
				 */
				bytes_read = 0;
				if(sock_bytesready(&socket) != -1)
				{
					bytes_read=sock_fastread(&socket, buffer, min(sizeof(buffer), serXwrFree()));
					bytes_read=strip_telnet_cmds(buffer, bytes_read);
				}

				/*
				 *		close the socket on an error
				 *
				 */

				if(bytes_read<0) {
					sock_close(&socket);
					state=CONNECTION_CLOSED;
					break;
				}

				/*
				 *		copy any bytes that we read
				 *
				 */

				if(bytes_read > 0) {
					timeout=SEC_TIMER+TIMEOUT;
					serXwrite(buffer, bytes_read);
				}

				/*
				 *		read as many bytes from the serial port as we
				 *		have room in the socket buffer.
				 *
				 */

				bytes_read=serXread(buffer,min(sizeof(buffer),sock_tbleft(&socket)),100);
				if(bytes_read>0) {
					timeout=SEC_TIMER+TIMEOUT;
					if(sock_fastwrite(&socket,buffer,bytes_read)<0) {
						sock_close(&socket);
						state=CONNECTION_CLOSED;
					}
				}
				led(TOGGLE_LED);
				break;

			case CONNECTION_CLOSED:
				serXrdFlush();
				serXwrFlush();
				sprintf(buffer, "\n\rConnection closed\n\r");
				serXwrite(buffer, strlen(buffer));
				led(RESET_LED);
				state=CONNECTION_INIT;
				break;
		}
	}
}
Ejemplo n.º 8
0
void vs_handler(VsState* state)
{
	auto tcp_Socket* socket;
	auto int ch, bytes_written;
	auto int bytes_to_write;

	if(vs_info.mode==VS_MODEOFF)
		return;

	socket=&state->socket;

	/*
	 *		was the connection reset?
	 */
	if(state->state!=VS_INIT && tcp_tick(socket)==0) {
#ifdef VERBOSE
		printf("Connection closed\n");
#endif
		state->state=VS_INIT;
		state->open_timeout=vs_info.open_timeout;
	}

	switch(state->state)
	{
		case VS_INIT:
			/*
			 *		passive open on the socket port
			 */

			if(state->open_timeout && vs_info.mode == VS_MODEACTIVE) {
				costate {
					waitfor(DelayMs(state->open_timeout));
					state->open_timeout=0;
				}

				if(state->open_timeout)
					break;
			}

			serYopen(vs_info.baud);

			if(vs_info.mode == VS_MODEPASSIVE) {
				if (tcp_listen(socket,vs_info.port,0,0,NULL,0) != 0) {
					state->state=VS_LISTEN;
#ifdef VERBOSE
					printf("\nListening on socket\n");
#endif
				}
				else {
					printf("\nError listening on socket!\n");
				}
			} else if(vs_info.mode == VS_MODEACTIVE) {
				if (tcp_open(socket,0,vs_info.dest_ip,vs_info.dest_port,NULL) != 0) {
					state->state=VS_OPENING;
#ifdef VERBOSE
					printf("\nOpening socket\n");
#endif
				}
				else {
					printf("\nError opening socket!\n");
				}
			}
			break;

		case VS_LISTEN:
		case VS_OPENING:
			/*
			 *		wait for a connection
			 */

			if(sock_established(socket) || sock_bytesready(socket) >= 0) {
				state->state=VS_OPEN;
				sock_mode(socket,vs_info.binary);

#ifdef VERBOSE
				printf("New Connection\n");
#endif
			}
			break;

		case VS_OPEN:
			if(vs_info.timeout!=0 &&
				state->offset &&
				(long) ((state->last_character+vs_info.timeout) - MS_TIMER) < 0) {
				bytes_written=sock_fastwrite(socket,state->buffer,state->offset);
				if (bytes_written < 0) {
					state->state = VS_WAITCLOSE;
					sock_close(socket);
#ifdef VERBOSE
					printf("Connection closed\n");
#endif
					break;
				}

				/*
				 *		Hmmm... We weren't able to write all the bytes out.
				 *		Since we don't want to loose any characters, we will
				 *		shift everything over and hopefully write it soon.
				 *
				 */

				if(bytes_written!=state->offset) {
					memcpy(state->buffer,state->buffer+bytes_written,state->offset-bytes_written);
					state->offset = bytes_written;
					break;
				} else
					state->offset = 0;
			}

			/*
			 *		process any characters.
			 */

			bytes_to_write=sock_bytesready(socket);
			if(bytes_to_write>serYwrFree())
				bytes_to_write=serYwrFree();

			if(bytes_to_write>(int)sizeof(state->buffer))
				bytes_to_write=sizeof(state->buffer);

			if(bytes_to_write>0) {
				sock_read(socket,state->buffer,bytes_to_write);
				serYwrite(state->buffer,bytes_to_write);
			}

			/*
			 *		If we aren't worried about interpacket delay
			 *		just send the characters if there is room in
			 *		the buffer.
			 *
			 */

			if(vs_info.timeout==0) {
				bytes_to_write=serYrdUsed();

				if(bytes_to_write>sock_tbleft(socket))
					bytes_to_write=sock_tbleft(socket);

				if(bytes_to_write>(int)sizeof(state->buffer))
					bytes_to_write=sizeof(state->buffer);

				if(bytes_to_write>0) {
					serYread(state->buffer,bytes_to_write,0);
					sock_write(socket,state->buffer,bytes_to_write);
				}
			} else {
				while(state->offset<VS_MAXOFFSET && (ch=serYgetc())!=-1) {
#ifdef USE_STDIO
					printf("%c",ch);
#endif
					state->buffer[state->offset++]=ch;
					state->last_character=MS_TIMER;
				}
			}

			/*
			 *		We should immediately flush characters if the buffer
			 *		is full.
			 *
			 */

			if(state->offset==VS_MAXOFFSET) {
				bytes_written=sock_fastwrite(socket,state->buffer,state->offset);
				if (bytes_written < 0) {
					state->state = VS_WAITCLOSE;
					sock_close(socket);
#ifdef VERBOSE
					printf("Connection closed\n");
#endif
					break;
				}

				/*
				 *		Hmmm... We weren't able to write all the bytes out.
				 *		Since we don't want to loose any characters, we will
				 *		shift everything over and hopefully write it soon.
				 *
				 */

				if(bytes_written!=state->offset) {
					memcpy(state->buffer,state->buffer+bytes_written,state->offset-bytes_written);
					state->offset = bytes_written;
				} else
					state->offset = 0;
			}

			break;

		case VS_WAITCLOSE:
			break;

		default:
			/*
			 *		how did we get here?  programming error?
			 */

			state->state=VS_INIT;
			break;
	}