亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? tcp.c

?? 基于單片機C8051F020與以太網進行通信程序
?? C
?? 第 1 頁 / 共 2 頁
字號:
//-----------------------------------------------------------------------------
// Copyright (c) 2002 Jim Brady
// Do not use commercially without author's permission
// Last revised August 2002
// Net TCP.C
//
// This module handles TCP segments
// Refer to RFC 793, 896, 1122, 1323, 2018, 2581
//
// A "connection" is a unique combination of 4 items:  His IP address,
// his port number, my IP address, and my port number.
//
// Note that a SYN and a FIN count as a byte of data, but a RST does
// not count. Neither do any of the other flags.
// See "TCP/IP Illustrated, Volume 1" Sect 17.3 for info on flags
//-----------------------------------------------------------------------------
#include <string.h>
#include <stdlib.h>
#include <ctype.h>		// toupper
#include "C8051f.h"
#include "net.h"
#include "cksum.h"
#include "serial.h"
#include "ip.h"
#include "http.h"
#include "tcp.h"


// These structures keep track of connection information
CONNECTION xdata conxn[5];

ULONG idata initial_sequence_nr;
UINT xdata sender_tcpport;
static ULONG xdata sender_ipaddr;
UCHAR idata just_closed; // Keeps track of when a conxn closed

extern ULONG code my_ipaddr;
extern UCHAR idata debug;
extern char xdata text[];

// Options: MSS (4 bytes), NOPS (2 bytes), Selective ACK (2 bytes) 
UCHAR code opt[10] = {
0x02, 0x04, 0x05, 0xB4,
0x01, 0x01,
0x04, 0x02};



//------------------------------------------------------------------------
//  Initialize variables declared in this module
//
//------------------------------------------------------------------------
void init_tcp(void)
{
   memset(conxn, 0, sizeof(conxn));
   just_closed = FALSE;
   initial_sequence_nr = 1;
}



//------------------------------------------------------------------------
// This sends a TCP segments that do not include any data.
// http_send() in the HTTP module is used to send data.
// See "TCP/IP Illustrated, Volume 1" Sect 17.3
//------------------------------------------------------------------------
void tcp_send(UINT flags, UINT hdr_len, UCHAR nr)
{
   ULONG idata sum, dest;
  	UINT idata result;
   UCHAR xdata * outbuf;
   TCP_HEADER xdata * tcp;
   IP_HEADER xdata * ip;
          
   
   // Allocate memory for entire outgoing message including
   // eth & IP headers 
   outbuf = (UCHAR xdata *)malloc(34 + hdr_len);
   if (outbuf == NULL)
   {
      if (debug) serial_send("TCP: Oops, out of memory\r");
      return;
   }

	   
   tcp = (TCP_HEADER xdata *)(outbuf + 34);
   ip = (IP_HEADER xdata *)(outbuf + 14);

   // If no connection, then message is probably a reset
   // message which goes back to the sender
   // Otherwise, use information from the connection.
   if (nr == NO_CONNECTION)
   {
      tcp->source_port = HTTP_PORT;
      tcp->dest_port = sender_tcpport;
      tcp->sequence = 0;
      tcp->ack_number = 0;
      dest = sender_ipaddr;
   }
   else if (nr < 5)
   {
      // This message is to connected port
      tcp->source_port = HTTP_PORT;
      tcp->dest_port = conxn[nr].port;
      tcp->sequence = conxn[nr].my_sequence;
      tcp->ack_number = conxn[nr].his_sequence;
      dest = conxn[nr].ipaddr;
   }
   else
   {
    	if (debug) serial_send("TCP: Oops, sock nr out of range\r");
		free(outbuf);
		return;
	}

   // Total segment length = header length
      
   // Insert header len
   tcp->flags = (hdr_len << 10) | flags;
   tcp->window = 1024;
   tcp->checksum = 0;
   tcp->urgent_ptr = 0;
   
   // Sending SYN with header options
   if (hdr_len == 28)
   {
      memcpy(&tcp->options, opt, 8);
   }   
   
   // Compute checksum including 12 bytes of pseudoheader
	// Must pre-fill 2 items in ip header to do this
	ip->dest_ipaddr = dest;
	ip->source_ipaddr = my_ipaddr;
		
	// Sum source_ipaddr, dest_ipaddr, and entire TCP message 
	sum = (ULONG)cksum(outbuf + 26, 8 + hdr_len);
				
	// Add in the rest of pseudoheader which is
	// protocol id and TCP segment length
	sum += (ULONG)0x0006;
	sum += (ULONG)hdr_len;

	// In case there was a carry, add it back around
	result = (UINT)(sum + (sum >> 16));
	tcp->checksum = ~result;
   
   if (debug) serial_send("TCP: Sending msg to IP layer\r");
	ip_send(outbuf, dest, TCP_TYPE, hdr_len);

   // (Re)start TCP retransmit timer
   conxn[nr].timer = TCP_TIMEOUT;
}




//------------------------------------------------------------------------
// This runs every 0.5 seconds.  If the other end has not ACK'd
// everyting we have sent, it re-sends it.  To save RAM space, we 
// regenerate a segment rather than keeping a bunch of segments 
// hanging around eating up RAM.  A connection should not be in an
// opening or closing state when this timer expires, so we simply
// send a reset.
//
//	If a connection is in the ESTABLISHED state when the timer expires
// then we have just sent a web page so re-send the page
//------------------------------------------------------------------------
void tcp_retransmit(void)
{
   static UCHAR idata retries = 0;
   UCHAR idata nr;

   // Scan through all active connections 
   for (nr = 0; nr < 5; nr++)
   {
      if ((conxn[nr].ipaddr != 0) && (conxn[nr].timer))
      {
         // Decrement the timer and see if it hit 0
         conxn[nr].timer--;
         if (conxn[nr].timer == 0)
         {
            // Socket just timed out. If we are not in ESTABLISHED state
            // something is amiss so send reset and close connection
            if (conxn[nr].state != STATE_ESTABLISHED)
            {
               // Send reset and close connection
               if (debug) serial_send("TCP: Timeout, sending reset\r");
               tcp_send(FLG_RST, 20, nr);
               conxn[nr].ipaddr = 0;
               return;
            }
            else
            {
               // Socket is in ESTABLISHED state. First make sure his
               // ack number is not bogus.
               if (conxn[nr].his_ack > conxn[nr].my_sequence)
               {
                  // Send reset and close connection
                  if (debug) serial_send("TCP: Timeout, sending reset\r");
                  tcp_send(FLG_RST, 20, nr);
                  conxn[nr].ipaddr = 0;
                  return;
               }
               
               // We always increment our sequence number immediately
					// after sending, so the ack number from the other end
					// should be equal to our sequence number.  If it is less,
					// it means he lost some of our data.
               if (conxn[nr].his_ack < conxn[nr].my_sequence)
					{
                  retries++;
						if (retries <= 2)
						{
                   	// The only thing we send is a web page, and it looks
                  	// like other end did not get it, so resend
                  	// but do not increase my sequence number
                  	if (debug) serial_send("TCP: Timeout, resending data\r");
                  	http_server(conxn[nr].query, 0, nr, 1);
			            conxn[nr].inactivity = INACTIVITY_TIME;
                  }
						else
						{
						  	if (debug) serial_send("TCP: Giving up, sending reset\r");
							// Send reset and close connection
               		tcp_send(FLG_RST, 20, nr);
               		conxn[nr].ipaddr = 0;
               	}
               }
            }
         }
      }
   }
}




//------------------------------------------------------------------------
// This runs every 0.5 seconds.  If the connection has had no activity
// it initiates closing the connection.
//
//------------------------------------------------------------------------
void tcp_inactivity(void)
{
   UCHAR idata nr;
   
   // Look for active connections in the established state
   for (nr = 0; nr < 5; nr++)
   {
      if ((conxn[nr].ipaddr != 0) && 
          (conxn[nr].state == STATE_ESTABLISHED) &&
          (conxn[nr].inactivity))
      {
         // Decrement the timer and see if it hit 0
         conxn[nr].inactivity--;
         if (conxn[nr].inactivity == 0)
         {
            // Inactivity timer has just timed out.
            // Initiate close of connection
            tcp_send((FLG_ACK | FLG_FIN), 20, nr);
            conxn[nr].my_sequence++;    // For my FIN
            conxn[nr].state = STATE_FIN_WAIT_1;
            if (debug) serial_send("TCP: Entered FIN_WAIT_1 state\r");	
         }
      }
   }
}



//------------------------------------------------------------------------
// This handles incoming TCP messages and manages the TCP state machine
// Note - both the SYN and FIN flags consume a sequence number.
// See "TCP/IP Illustrated, Volume 1" Sect 18.6 for info on TCP states
// See "TCP/IP Illustrated, Volume 1" Sect 17.3 for info on flags
//------------------------------------------------------------------------
void tcp_rcve(UCHAR xdata * inbuf, UINT len)
{
   UCHAR idata i, j, nr;
   UINT idata result, header_len, data_len;
   TCP_HEADER xdata * tcp;
   IP_HEADER xdata * ip;
   ULONG idata sum;
   
   // IP header is always 20 bytes so message starts at index 34      
   tcp = (TCP_HEADER xdata *)(inbuf + 34);
   ip = (IP_HEADER xdata *)(inbuf + 14);
				   
	// Compute TCP checksum including 12 byte pseudoheader
	// Sum source_ipaddr, dest_ipaddr, and entire TCP message 
	sum = (ULONG)cksum(inbuf + 26, 8 + len);
		
	// Add in the rest of pseudoheader which is
	// protocol id and TCP segment length
	sum += (ULONG)0x0006;     
	sum += (ULONG)len;

	// In case there was a carry, add it back around
	result = (UINT)(sum + (sum >> 16));
		
	if (result != 0xFFFF)
	{
		if (debug) serial_send("TCP: Error, bad cksum\r");
		return;
   }

	if (debug) serial_send("TCP: Msg rcvd with good cksum\r");
   
	// See if message is for http server
	if (tcp->dest_port != HTTP_PORT)	
   {
      if (debug)
      {
         serial_send("TCP: Error, msg to port ");
         memset(text, 0, 10);
         itoa(tcp->dest_port, text, 10);
         serial_send(text);
		   serial_send("\r");
      }
      tcp_send(FLG_RST, 20, NO_CONNECTION);
      return;
   }
   
   // Capture sender's IP address and port number
   sender_ipaddr = ip->source_ipaddr;
   sender_tcpport = tcp->source_port;
   
   // See if the TCP segment is from someone we are already
   // connected to. 
   for (i=0; i < 5; i++)
   {
      if ((ip->source_ipaddr == conxn[i].ipaddr) &&
         (tcp->source_port == conxn[i].port))
      {   
         nr = i;
         if (debug) serial_send("TCP: Rcvd msg from existing conxn\r");
         break;
      }       
   }

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
欧美大片顶级少妇| 天堂成人免费av电影一区| 最新不卡av在线| 成人免费毛片嘿嘿连载视频| 国产片一区二区| 99久久精品免费看| 欧美亚州韩日在线看免费版国语版| 成人永久免费视频| 久久一区二区三区四区| 国产精品乱码久久久久久| 国产精品一区二区在线播放 | 成人高清在线视频| 中文字幕精品一区| 一本久久精品一区二区| 亚洲一级二级三级在线免费观看| 欧美日韩国产一级| 蜜桃一区二区三区在线观看| 国产剧情一区在线| 欧美视频在线观看一区二区| 五月天视频一区| 精品免费国产二区三区| 成人成人成人在线视频| 亚洲成人一区在线| 精品国产一区二区亚洲人成毛片| 成人免费观看视频| 午夜亚洲福利老司机| 久久婷婷色综合| 欧美最猛性xxxxx直播| 久久精品国产精品亚洲精品| 国产精品色一区二区三区| 欧美bbbbb| 亚洲欧美激情在线| 日韩一区二区高清| 不卡的av电影| 蜜乳av一区二区| 亚洲欧美色图小说| 久久久亚洲欧洲日产国码αv| 91蝌蚪porny成人天涯| 蜜桃视频一区二区| 最新日韩av在线| 日韩精品一区二区三区在线| 99视频一区二区三区| 久久超碰97人人做人人爱| 秋霞av亚洲一区二区三| 麻豆成人综合网| 亚洲人成网站影音先锋播放| 精品国产a毛片| 中文字幕国产一区二区| 欧美日韩国产免费| av在线不卡电影| 久久久久久久av麻豆果冻| 欧美精品乱码久久久久久| 97久久超碰精品国产| 国产原创一区二区| 日本欧美一区二区| 欧美久久一区二区| 国产精品一区在线| 精品久久一区二区三区| 欧美日韩一级黄| 色一区在线观看| www.视频一区| 国产成人自拍高清视频在线免费播放| 亚洲18色成人| 99久久免费精品| 亚洲精品五月天| 欧美色电影在线| 午夜久久久久久久久久一区二区| 国产精品成人午夜| 欧美激情一二三区| 国产欧美日韩在线看| 国产亚洲美州欧州综合国| 日韩精品一区二区三区在线播放| 777久久久精品| 欧美军同video69gay| 欧美三区在线观看| 欧美视频一区二区三区在线观看 | 精品毛片乱码1区2区3区| 欧美三级在线看| 欧美日韩一区二区在线观看视频| 日本精品视频一区二区三区| 色视频成人在线观看免| 色狠狠一区二区| 91国偷自产一区二区开放时间| 色综合天天综合色综合av | 亚洲国产岛国毛片在线| 亚洲国产精品黑人久久久| 国产精品免费视频观看| 中文字幕日韩av资源站| 中文字幕亚洲不卡| 亚洲福利一区二区| 日本亚洲电影天堂| 美日韩一级片在线观看| 尤物av一区二区| 亚洲最大色网站| 日本欧美韩国一区三区| 国产精品99久久久久久久vr | 91蜜桃免费观看视频| 91成人国产精品| 91精品国产综合久久久久久| 26uuu欧美| 亚洲欧美怡红院| 日韩精品国产精品| 国内精品嫩模私拍在线| 成人h版在线观看| 欧美偷拍一区二区| 欧美成人vr18sexvr| 中文字幕精品一区二区三区精品| 亚洲欧美精品午睡沙发| 免费看精品久久片| 国产 日韩 欧美大片| 91久久精品一区二区| 91精品国产综合久久精品| 久久久久久免费网| 依依成人精品视频| 麻豆精品一区二区| 色先锋久久av资源部| 精品久久久久久久久久久久包黑料 | 777奇米成人网| 欧美激情中文不卡| 五月婷婷欧美视频| 国产成人啪午夜精品网站男同| 欧美日韩国产天堂| 色噜噜狠狠成人网p站| 日韩欧美一二三四区| 最新日韩av在线| 精品一区二区免费看| 色av综合在线| 久久精品人人做人人爽人人| 亚洲国产精品一区二区久久恐怖片| 精品无人码麻豆乱码1区2区| 日本电影亚洲天堂一区| 久久婷婷成人综合色| 亚洲第一福利一区| 91丨porny丨中文| 久久亚洲影视婷婷| 婷婷综合另类小说色区| 99热在这里有精品免费| 欧美精品一区二区三区久久久| 亚洲在线中文字幕| 成人午夜短视频| 日韩你懂的在线观看| 亚洲电影第三页| 91浏览器在线视频| 国产色产综合产在线视频| 日本伊人色综合网| 欧美日韩视频一区二区| 亚洲欧美精品午睡沙发| 国产**成人网毛片九色| 26uuu欧美| 黄色日韩网站视频| 欧美一区二区三区喷汁尤物| 亚洲已满18点击进入久久| heyzo一本久久综合| 久久久国产精品不卡| 久久精品国产亚洲一区二区三区| 欧美日韩精品一区二区| 一区二区在线免费观看| 国产在线看一区| 在线不卡欧美精品一区二区三区| 亚洲综合视频网| 色偷偷88欧美精品久久久| 国产精品久久毛片av大全日韩| 国产激情一区二区三区| 精品成人一区二区| 久久99国内精品| 精品99999| 国产精品伊人色| 国产精品天干天干在观线| 成人午夜电影小说| 亚洲视频中文字幕| 色婷婷综合久色| 国产成人激情av| 中文字幕免费一区| av电影在线观看不卡 | 国产精品嫩草99a| 99天天综合性| 亚洲色图色小说| 色吊一区二区三区| 亚洲成人www| 日韩欧美一二三四区| 国产一区二区影院| 欧美极品aⅴ影院| 99视频精品在线| 无吗不卡中文字幕| 日韩精品一区二区三区在线| 国产精品主播直播| 国产精品成人免费在线| 欧美在线不卡一区| 蜜臀久久99精品久久久画质超高清| 欧美www视频| 成人自拍视频在线| 亚洲一区二区四区蜜桃| 欧美精品乱码久久久久久按摩 | 成人免费高清在线| 亚洲乱码国产乱码精品精小说 | 一区在线播放视频| 日本韩国欧美三级| 免费在线视频一区| 国产精品嫩草久久久久| 欧美亚洲一区二区在线|