⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tcp_c source file.htm

📁 关于PPP点对点协议的一些详细资料
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0044)http://www.italios.it/os1/tcp_8c-source.html -->
<HTML><HEAD><TITLE>tcp.c Source File</TITLE>
<META http-equiv=Content-Type content=text/html;charset=iso-8859-1><LINK 
href="tcp_c Source File.files/doxygen.css" type=text/css rel=stylesheet>
<META content="MSHTML 6.00.2800.1106" name=GENERATOR></HEAD>
<BODY><!-- Generato da Doxygen 1.3-rc3 -->
<CENTER><A class=qindex href="http://www.italios.it/os1/index.html">Pagina 
Principale</A> &nbsp; <A class=qindex 
href="http://www.italios.it/os1/annotated.html">Lista dei composti</A> &nbsp; <A 
class=qindex href="http://www.italios.it/os1/files.html">Lista dei file</A> 
&nbsp; <A class=qindex href="http://www.italios.it/os1/functions.html">Membri 
dei composti</A> &nbsp; <A class=qindex 
href="http://www.italios.it/os1/globals.html">Membri dei file</A> &nbsp; 
</CENTER>
<HR>

<H1>tcp.c</H1><A href="http://www.italios.it/os1/tcp_8c.html">Vai alla 
documentazione di questo file.</A>
<DIV class=fragment><PRE>00001 <SPAN class=comment>/*</SPAN>
00002 <SPAN class=comment>ItaliOs</SPAN>
00003 <SPAN class=comment>This program is free software; you can redistribute it and/or</SPAN>
00004 <SPAN class=comment>modify it under the terms of the GNU General Public License</SPAN>
00005 <SPAN class=comment>as published by the Free Software Foundation; either version 2</SPAN>
00006 <SPAN class=comment>of the License, or (at your option) any later version.</SPAN>
00007 <SPAN class=comment></SPAN>
00008 <SPAN class=comment>This program is distributed in the hope that it will be useful,</SPAN>
00009 <SPAN class=comment>but WITHOUT ANY WARRANTY; without even the implied warranty of</SPAN>
00010 <SPAN class=comment>MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</SPAN>
00011 <SPAN class=comment>GNU General Public License for more details.</SPAN>
00012 <SPAN class=comment></SPAN>
00013 <SPAN class=comment>You should have received a copy of the GNU General Public License</SPAN>
00014 <SPAN class=comment>along with this program; if not, write to the Free Software</SPAN>
00015 <SPAN class=comment>Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.</SPAN>
00016 <SPAN class=comment>*/</SPAN>
00017 <SPAN class=preprocessor>#define r_TCP</SPAN>
00018 <SPAN class=preprocessor></SPAN><SPAN class=preprocessor>#include "<A class=code href="http://www.italios.it/os1/net_2rete_8h.html">rete.h</A>"</SPAN>
00019 <SPAN class=preprocessor>#define TCP_MTU 1452</SPAN>
00020 <SPAN class=preprocessor></SPAN>
00021 
00022 <SPAN class=preprocessor>#define Tcp(zz,Prt1,Prt2,seq1,seq2,flag,win) \</SPAN>
00023 <SPAN class=preprocessor>  *(u_short *)(zz+2)=scbyte(Prt1);*(u_short *)zz=scbyte(Prt2);\</SPAN>
00024 <SPAN class=preprocessor>  *(u_int *)(zz+4)=scbyte4(seq1); *(u_int *)(zz+8)=scbyte4(seq2); \</SPAN>
00025 <SPAN class=preprocessor>  *(zz+12)=0x50;*(zz+13)=flag;*(u_short *)(zz+14)=win;*(u_int *)(zz+16)=0;</SPAN>
00026 <SPAN class=preprocessor></SPAN>
00027 <SPAN class=preprocessor>#undef SKK</SPAN>
00028 <SPAN class=preprocessor></SPAN><SPAN class=preprocessor>#define SKK Sok[*sk]</SPAN>
00029 <SPAN class=preprocessor></SPAN>
00030   u_short proxPrt2=1030;
00031   u_int seq_SIN=0x10a23abc;
00032   u_int seq_ACK=0;
00033   <SPAN class=keyword>struct </SPAN>TSok *Skp; 
00034   u_char *bufTCP=NULL;
00035   u_char CC=0;
00036   
00037 
00038 <SPAN class=preprocessor>#undef SKK</SPAN>
00039 <SPAN class=preprocessor></SPAN><SPAN class=preprocessor>#define SKK Sok[*sk]</SPAN>
00040 <SPAN class=preprocessor></SPAN><SPAN class=preprocessor>#define SeqSIN scbyte4(*(u_int *)(buffTCP+8))</SPAN>
00041 <SPAN class=preprocessor></SPAN><SPAN class=preprocessor>#define SeqACK scbyte4(*(u_int *)(buffTCP+4))</SPAN>
00042 <SPAN class=preprocessor></SPAN><SPAN class=preprocessor>#define PrtSRC scbyte(*(u_short *)buffTCP)</SPAN>
00043 <SPAN class=preprocessor></SPAN><SPAN class=preprocessor>#define PrtDST scbyte(*(u_short *)(buffTCP+2))</SPAN>
00044 <SPAN class=preprocessor></SPAN><SPAN class=preprocessor>#define FLAg *(buffTCP+13)</SPAN>
00045 <SPAN class=preprocessor></SPAN>
00046 
00047 
00048 
00049 
00050 u_char connettiTCP(u_char *sk,u_int Ip,u_short Porta) {
00051   Debug(<SPAN class=stringliteral>"connettiTCP"</SPAN>);  
00052   ++proxPrt2; seq_SIN++; 
00053   *sk=registraIP(Porta,proxPrt2,Ip,0,6);<SPAN class=comment>/* binding */</SPAN>
00054   <SPAN class=keywordflow>if</SPAN> (*sk==255) Errore(3,<SPAN class=stringliteral>"connettiTCP: Sk=255"</SPAN>);   <SPAN class=comment>/* se sk=0 ritorna 0 */</SPAN>
00055   SKK-&gt;seqSIN=seq_SIN;SKK-&gt;seqACK=0;
00056         Debug(<SPAN class=stringliteral>"\nAperto Socket\n"</SPAN>); 
00057   SKK-&gt;stato=1;SKK-&gt;timeout=0;SKK-&gt;timeout2=adesso; SKK-&gt;flag=0;
00058   <SPAN class=keywordflow>while</SPAN> ((SKK-&gt;stato!=3)&amp;&amp;(!<A class=code href="http://www.italios.it/os1/keyboard_8c.html#a13">kbhit</A>())&amp;&amp;(SKK!=NULL)) ;
00059   <SPAN class=keywordflow>if</SPAN> (SKK-&gt;stato==3) <SPAN class=keywordflow>return</SPAN> 1;  <SPAN class=comment>/* se e' andato in stato connesso ritorna 1*/</SPAN>
00060 errore:
00061   <SPAN class=keywordflow>return</SPAN> 0;<SPAN class=comment>/* in tutti gli altri casi ritorna 0 */</SPAN>
00062 }
00063 <SPAN class=preprocessor>#undef SKK</SPAN>
00064 <SPAN class=preprocessor></SPAN><SPAN class=preprocessor>#define SKK Sok[sk]</SPAN>
00065 <SPAN class=preprocessor></SPAN>
00066 u_char ascoltaTCP(u_short Porta) {
00067    u_char sk;
00068    <SPAN class=keywordflow>if</SPAN> ((sk=registraIP(0,Porta,0,0,6))==255) <SPAN class=keywordflow>return</SPAN> 0;
00069    SKK-&gt;stato=8;delay(50);
00070    <SPAN class=keywordflow>while</SPAN> ((SKK!=NULL)&amp;&amp;(SKK-&gt;stato!=3)&amp;&amp;(!<A class=code href="http://www.italios.it/os1/keyboard_8c.html#a13">kbhit</A>())) delay(50);
00071    <SPAN class=keywordflow>if</SPAN> (reteDEBUG&amp;2) printf(<SPAN class=stringliteral>"ascoltaTCP(): SKK=%x SKK-&gt;stato=%u kbhit=%u\n"</SPAN>,SKK,SKK-&gt;stato,<A class=code href="http://www.italios.it/os1/keyboard_8c.html#a13">kbhit</A>());
00072    <SPAN class=keywordflow>if</SPAN> (SKK-&gt;stato==3) <SPAN class=keywordflow>return</SPAN> 1;
00073    <SPAN class=keywordflow>return</SPAN> 0;   
00074 }
00075 
00076 u_char disconnettiTCP(u_char sk) {
00077   Debug(<SPAN class=stringliteral>"disconnettiTCP\n"</SPAN>);  
00078   <SPAN class=keywordflow>if</SPAN> (SKK-&gt;stato!=3) <SPAN class=keywordflow>return</SPAN> 0;
00079 <SPAN class=comment>/*</SPAN>
00080 <SPAN class=comment>  Alloca(20);</SPAN>
00081 <SPAN class=comment>   Tcp(bufTCP,SKK-&gt;PRTsrc,SKK-&gt;PRTdst,SKK-&gt;seqSIN,SKK-&gt;seqACK,FIN|ACK,3000);</SPAN>
00082 <SPAN class=comment>  if (!mandaIP(SKK-&gt;IPdst,6,bufTCP,20)) Errore(3,"disconnettiTCP: erroreMandaIP(syn)");</SPAN>
00083 <SPAN class=comment>*/</SPAN>
00084   SKK-&gt;timeout=0;SKK-&gt;stato=4;SKK-&gt;timeout2=adesso;
00085   <SPAN class=keywordflow>return</SPAN> 1;
00086 }
00087 
00088 
00089 u_char riceviTCP(u_char sk,u_int Ip,u_char *buffTCP,u_short lungh) {
00090    <SPAN class=keywordtype>int</SPAN> ttt;
00091 <SPAN class=keywordtype>void</SPAN> MandaTcp(u_char flg) {
00092      setmem(bufTCP,0,60);
00093      Tcp(bufTCP,SKK-&gt;PRTsrc,SKK-&gt;PRTdst,SKK-&gt;seqSIN,SKK-&gt;seqACK,flg,3000);
00094      <SPAN class=keywordflow>if</SPAN> (!mandaIP(SKK-&gt;IPdst,6,bufTCP,20)) Errore(6,<SPAN class=stringliteral>"rxTCP:errore mandaIP"</SPAN>);
00095      SKK-&gt;timeout=adesso;
00096   errore:     
00097 }
00098   <SPAN class=keywordtype>void</SPAN> Aggiorna() {
00099      SKK-&gt;seqSIN=SeqSIN;    SKK-&gt;seqACK=SeqACK;   SKK-&gt;timeout=adesso; 
00100   }
00101 
00102   <SPAN class=keywordflow>if</SPAN> (SKK-&gt;stato==8) {
00103     <SPAN class=keywordflow>if</SPAN> (reteDEBUG&amp;2) printf(<SPAN class=stringliteral>"riceviTCP lungh=%u\n"</SPAN>,lungh);
00104      <SPAN class=keywordflow>if</SPAN> ((FLAg&amp;(SYN|ACK))==SYN) { <SPAN class=comment>/* ricevuto syn */</SPAN>
00105         SKK-&gt;seqACK=SeqACK+1;   
00106         SKK-&gt;seqSIN=++seq_SIN; 
00107         SKK-&gt;IPdst=Ip; SKK-&gt;PRTsrc=PrtSRC;SKK-&gt;PRTdst=PrtDST;
00108         MandaTcp(SYN|ACK);
00109         SKK-&gt;stato=2;SKK-&gt;flag=0;
00110         SKK-&gt;buflenTX=0;SKK-&gt;buflenRX=0;
00111      } <SPAN class=keywordflow>else</SPAN> {
00112         SKK-&gt;seqACK=SeqACK+1;   
00113         MandaTcp(ACK|RST);   
00114      }  
00115   }
00116   <SPAN class=keywordflow>if</SPAN> (SKK-&gt;stato==1) {
00117      <SPAN class=keywordflow>if</SPAN> ((FLAg&amp;(SYN|ACK))==(SYN|ACK)) { <SPAN class=comment>/* ricevuto synack */</SPAN>
00118         <SPAN class=keywordflow>if</SPAN> (SKK-&gt;stato!=1) Errore(4,<SPAN class=stringliteral>"rxTCP:SynAck e socket!=1"</SPAN>);
00119         <SPAN class=keywordflow>if</SPAN> (SeqSIN!=(SKK-&gt;seqSIN+1)) Errore(5,<SPAN class=stringliteral>"rxTCP:SynAck e seqSIN!=+1"</SPAN>);
00120         SKK-&gt;seqACK=SeqACK+1;   
00121         SKK-&gt;seqSIN=SeqSIN; 
00122         MandaTcp(ACK);
00123         SKK-&gt;stato=3;
00124         SKK-&gt;buflenTX=0;SKK-&gt;buflenRX=0;
00125      } <SPAN class=keywordflow>else</SPAN> {
00126         SKK-&gt;seqACK=SeqACK+1;   
00127         MandaTcp(ACK|RST);   
00128      }  
00129   } <SPAN class=keywordflow>else</SPAN> {
00130     <SPAN class=keywordflow>if</SPAN> (FLAg&amp;ACK) {   <SPAN class=comment>/* ricevuto ack */</SPAN>
00131        <SPAN class=keywordflow>if</SPAN> (SKK-&gt;stato==2)  
00132           <SPAN class=keywordflow>if</SPAN> (((SKK-&gt;seqSIN+1)==SeqSIN)&amp;&amp;(SKK-&gt;seqACK==SeqACK)) {
00133              SKK-&gt;stato=3; Aggiorna(); <SPAN class=keywordflow>goto</SPAN> errore; 
00134           }
00135  <SPAN class=comment>/*      while (CC&amp;8) delay(50);</SPAN>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -