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

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

?? tcp.c

?? Cygnal公司高性能51的tcpip源程序,非常適合于51單片機上使用
?? C
?? 第 1 頁 / 共 2 頁
字號:
//-----------------------------------------------------------------------------
// 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;
      }       
   }
   
   // If i = 5, we are not connected. If it is a SYN then assign

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
久久影音资源网| 日韩在线观看一区二区| 亚洲bt欧美bt精品| 国产乱码字幕精品高清av| 99久久久精品| 日韩免费观看2025年上映的电影| 亚洲色图自拍偷拍美腿丝袜制服诱惑麻豆| 三级亚洲高清视频| 91年精品国产| 国产日本亚洲高清| 国产一区二区电影| 欧美一区三区四区| 亚洲高清三级视频| 一本久久精品一区二区| 国产精品久久午夜夜伦鲁鲁| 久久国产福利国产秒拍| 欧美二区乱c少妇| 一区二区三区在线观看网站| www.欧美日韩国产在线| 久久久美女毛片| 久久99久久99小草精品免视看| 欧美精品国产精品| 亚洲午夜久久久久久久久电影网| 99re免费视频精品全部| 国产精品欧美一区喷水| 国产成人精品一区二区三区四区 | 欧美另类z0zxhd电影| 国产精品久久久久毛片软件| 国产在线不卡一卡二卡三卡四卡| 91精品国产综合久久精品麻豆| 亚洲一级二级在线| 欧美日韩一二三区| 午夜久久电影网| 欧美日高清视频| 亚洲3atv精品一区二区三区| 欧美日韩国产综合草草| 亚洲成av人在线观看| 欧美日韩一区二区三区高清| 亚洲韩国一区二区三区| 欧美日韩国产精品自在自线| 蜜桃一区二区三区在线观看| 日韩欧美成人激情| 国产成人免费9x9x人网站视频| 欧美激情在线一区二区| 不卡的av在线播放| 亚洲黄色免费网站| 欧美电影影音先锋| 激情五月激情综合网| 中文字幕欧美国产| 一本色道久久综合狠狠躁的推荐| 亚洲大片免费看| 精品欧美乱码久久久久久1区2区| 国产另类ts人妖一区二区| 国产精品久久久久久福利一牛影视| 99精品热视频| 午夜不卡在线视频| 国产亚洲制服色| 色偷偷久久人人79超碰人人澡| 亚洲成人www| 精品99999| va亚洲va日韩不卡在线观看| 婷婷丁香久久五月婷婷| 26uuu精品一区二区| 色综合网站在线| 极品少妇xxxx精品少妇| 日韩理论片一区二区| 91精品国产高清一区二区三区| 国产大陆a不卡| 丝袜亚洲另类欧美综合| 国产欧美日韩亚州综合| 欧美美女网站色| 国产成人免费视频网站| 日韩二区三区在线观看| 国产精品久久三| 日韩精品最新网址| 色欲综合视频天天天| 国内欧美视频一区二区| 亚洲一级片在线观看| 日本一区二区不卡视频| 欧美一区二区三区爱爱| 972aa.com艺术欧美| 激情综合色播五月| 亚洲精品国产品国语在线app| 欧美电视剧免费全集观看| 色综合久久88色综合天天| 国产成人亚洲综合a∨婷婷图片| 亚洲国产人成综合网站| 日本一区免费视频| 91精品国产综合久久久久| 91蝌蚪porny成人天涯| 国产成人一区在线| 久久国产综合精品| 亚洲成人三级小说| 亚洲黄色av一区| 国产精品久久久久婷婷| 久久精品一区二区三区不卡牛牛| 6080亚洲精品一区二区| 欧洲激情一区二区| 99精品欧美一区| 国产精品1024| 精品一区二区久久| 婷婷丁香激情综合| 亚洲成人精品一区| 亚洲国产美女搞黄色| 亚洲三级免费观看| 亚洲色欲色欲www| 国产精品久久久久久久裸模| 中文字幕成人av| 欧美激情自拍偷拍| 国产午夜精品福利| 2017欧美狠狠色| 久久免费看少妇高潮| 久久午夜色播影院免费高清| 日韩欧美国产精品| 精品久久人人做人人爰| 精品久久一区二区三区| 精品sm捆绑视频| 国产午夜精品一区二区| 国产精品天美传媒沈樵| 国产精品久久久久影院色老大| 久久午夜色播影院免费高清| 久久综合999| 国产精品家庭影院| 亚洲精品免费看| 一区二区三区欧美| 爽爽淫人综合网网站| 伦理电影国产精品| 国产精一区二区三区| 成人高清视频在线| 在线观看国产日韩| 3d动漫精品啪啪一区二区竹菊 | 在线视频国内一区二区| 91亚洲大成网污www| 欧美性生活影院| 91精品综合久久久久久| 亚洲精品一区二区三区福利| 久久精子c满五个校花| 综合激情成人伊人| 亚洲444eee在线观看| 紧缚捆绑精品一区二区| 99国产欧美久久久精品| 欧美视频在线一区二区三区| 日韩情涩欧美日韩视频| 中文字幕一区三区| 亚洲精品成人在线| 毛片基地黄久久久久久天堂| 不卡视频在线观看| 欧美二区在线观看| 国产欧美精品一区二区色综合| 一区二区三区在线免费播放| 美女一区二区在线观看| 成人av在线看| 538在线一区二区精品国产| 久久欧美一区二区| 亚洲一区二区美女| 国产.欧美.日韩| 欧美日韩高清在线| 国产女人aaa级久久久级 | 久久99久久精品欧美| 99国产一区二区三精品乱码| 日韩亚洲欧美高清| 亚洲欧美日韩一区| 久久97超碰国产精品超碰| 色综合网色综合| 久久久三级国产网站| 日韩一区精品视频| 色欧美日韩亚洲| 国产清纯白嫩初高生在线观看91 | 国产亚洲成年网址在线观看| 亚洲自拍另类综合| 高清免费成人av| 日韩三级在线观看| 亚洲综合无码一区二区| 成人一区在线观看| 日韩免费一区二区| 日本午夜一本久久久综合| 96av麻豆蜜桃一区二区| 久久精品人人做人人爽人人| 青青草原综合久久大伊人精品优势| 一本色道久久加勒比精品| 国产精品理伦片| 精品亚洲免费视频| 欧美一区二区三区免费在线看| 夜夜精品视频一区二区| 99视频精品在线| 国产欧美日韩激情| 国产精品一区二区在线观看网站 | 欧美日本视频在线| 一个色综合网站| 91激情五月电影| 亚洲人成亚洲人成在线观看图片| 国产91色综合久久免费分享| 精品国产网站在线观看| 久久精品久久久精品美女| 欧美精品1区2区| 视频一区中文字幕国产| 欧美精品久久一区二区三区| 日韩综合小视频| 日韩色视频在线观看| 美女视频网站久久|