Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
06/18/07 08:34
Read: times


 
#140931 - Ya.
Responding to: ???'s previous message
Here is snap shoot of the code:

I have made it a little messy so...

//******************************************************************
//*	TCP Function
//*   This function uses TCP protocol to act as a Telnet/Web server on
//*   port 23/80 decimal.  The application function is called with
//*   
//******************************************************************
void tcp()
{
   
   //assemble the destination port address from the incoming packet
   portaddr = make16(packet[TCP_destport],packet[TCP_destport+1]);

   //calculate the length of the data coming in with the packet
   //tcpdatalen_in = incoming packet length - incoming ip header length - 
   //incoming tcp header length
   tcpdatalen_in = (make16(packet[ip_pktlen],packet[ip_pktlen+1]))- ((packet[ip_vers_len] & 0x0F)* 4)-(((packet[TCP_hdrflags] & 0xF0) >> 4) * 4);
   if(tcpdatalen_in>600)tcpdatalen_in=600;
   //If an ACK is received and the destination port address is valid 
   //and no data is in the packet
   if(ACK_IN && tcpdatalen_in == 0x00)
   {
      //assemble the acknowledgment number from the incoming packet
      incoming_ack =make32(packet[TCP_acknum],packet[TCP_acknum+1], \
	   packet[TCP_acknum+2],packet[TCP_acknum+3]);

      //if the incoming packet is a result of session establishment
      if(synflag_bit)
      {
         //clear the SYN flag
         clr_synflag;
         Connected=1;
         //the incoming acknowledgment is my new sequence number
         my_seqnum = incoming_ack;

         //if Telnet,send the Telnet server banner
         //strcpy(&packet[TCP_data],"ASBC alpha\r\n\r\n");
        // tcpdatalen_out = 14;

         //expect to get an acknowledgment of the banner message
         expected_ack = my_seqnum +tcpdatalen_out;

         //send the TCP/IP packet
         send_tcp_packet();
      }
   }

   //if an ack is received and the port address is valid and there is data 
   //in the incoming packet
   if((ACK_IN) && tcpdatalen_in)
   {
      
      for(i=0;i<tcpdatalen_in;++i)
         //receive the data and put it into the incoming data buffer
         dat_buff[i] = packet[TCP_data+i];
		  
      	  application_code();
		  tcpdatalen_out=strlen(&packet[TCP_data]);   
      
      //assemble the acknowledgment number from the incoming packet
      incoming_ack =make32(packet[TCP_acknum],packet[TCP_acknum+1], \
	  packet[TCP_acknum+2],packet[TCP_acknum+3]);

      //check for the number of bytes acknowledged
      //determine how many bytes are outstanding and adjust the outgoing 
	  //sequence number accordingly
      if(incoming_ack <= expected_ack)
         my_seqnum = expected_ack - (expected_ack - incoming_ack);
     
      //my expected acknowledgement number
      expected_ack = my_seqnum +tcpdatalen_out;
      send_tcp_packet();
	  
   }

   //this code segment processes the incoming SYN from the client
   //and sends back the initial sequence number (ISN) and acknowledges
   //the incoming SYN packet
   if(SYN_IN )
   {
      tcpdatalen_in = 0x01;
      set_synflag;

      setipaddrs();

      data_L = packet[TCP_srcport];
      packet[TCP_srcport] = packet[TCP_destport];
      packet[TCP_destport] = data_L;

      data_L = packet[TCP_srcport+1];
      packet[TCP_srcport+1] = packet[TCP_destport+1];
      packet[TCP_destport+1] = data_L;

      assemble_ack();

      if(++ISN == 0x0000 || ++ISN == 0xFFFF)
       	 my_seqnum = 0x1234FFFF;

      set_packet32(TCP_seqnum,my_seqnum);

      packet[TCP_hdrflags+1] = 0x00;
      SYN_OUT;
      ACK_OUT;

      packet[TCP_cksum] = 0x00;
      packet[TCP_cksum+1] = 0x00;
	  
	   
      hdr_chksum =0;
      
      tcplen = make16(packet[ip_pktlen],packet[ip_pktlen+1]) - ((packet[ip_vers_len] & 0x0F) * 4);
     
      hdrlen = tcplen;
      addr = &packet[TCP_srcport];
      cksum();
      
      hdrlen = 0x08;
      addr = &packet[ip_srcaddr];
      cksum();
      hdr_chksum = hdr_chksum + make16(0,packet[ip_proto])+ tcplen;
    
      
      chksum16= ~(hdr_chksum + ((hdr_chksum & 0xFFFF0000) >> 16));
      packet[TCP_cksum] = make8(chksum16,1);
      packet[TCP_cksum+1] = make8(chksum16,0);
      echo_packet();
   }

   //this code segment processes a FIN from the client
   //and acknowledges the FIN and any incoming data.
   if(FIN_IN && Connected)
   {
       for(i=0;i<tcpdatalen_in;++i)
            dat_buff[i] = packet[TCP_data+i];
      application_code();
	  tcpdatalen_out=strlen(&packet[TCP_data]);  
      set_finflag;
      Connected=0;
      ++tcpdatalen_in;

      incoming_ack =make32(packet[TCP_acknum],packet[TCP_acknum+1], \
	  packet[TCP_acknum+2],packet[TCP_acknum+3]);
      if(incoming_ack <= expected_ack)
         my_seqnum = expected_ack - (expected_ack - incoming_ack);

      expected_ack = my_seqnum +tcpdatalen_out;
      send_tcp_packet();

   }
   
  
   
   
}


List of 10 messages in thread
TopicAuthorDate
Ethernet question.            01/01/70 00:00      
   More info required            01/01/70 00:00      
      Details            01/01/70 00:00      
         Packet monitor            01/01/70 00:00      
            thanks.            01/01/70 00:00      
               Sequence Numbers...?            01/01/70 00:00      
                  Ya.            01/01/70 00:00      
                     Show the log when corruption occured            01/01/70 00:00      
                        I fix it..kinda.            01/01/70 00:00      
                  Not so unusual for 8-bit micros server            01/01/70 00:00      

Back to Subject List