📄 udp_8c-source.html
字号:
00518 UDP_DEBUGOUT(<span class="stringliteral">"Sending UDP...\r\n"</span>);00519 00520 i = <a class="code" href="ip_8c.html#a4">process_ip_out</a>(remip, <a class="code" href="ip_8h.html#a2">IP_UDP</a>, soc-><a class="code" href="structucb.html#m1">tos</a>, 100, buf, dlen + <a class="code" href="tcp__ip_8h.html#a8">UDP_HLEN</a>);00521 00522 <span class="comment">/* Errors? */</span>00523 00524 <span class="keywordflow">if</span>( i < 0 )00525 <span class="keywordflow">return</span>(i);00526 00527 UDP_DEBUGOUT(<span class="stringliteral">"UDP packet sent\r\n"</span>);00528 00529 <span class="keywordflow">return</span>(i - <a class="code" href="tcp__ip_8h.html#a8">UDP_HLEN</a>);00530 00531 00532 }00533 00534 00535 <a name="l00550"></a><a class="code" href="udp_8c.html#a8">00550</a> INT16 <a class="code" href="udp_8c.html#a8">process_udp_in</a>(<span class="keyword">struct</span> <a class="code" href="structip__frame.html">ip_frame</a>* frame, UINT16 len)00551 {00552 <span class="keyword">struct </span><a class="code" href="structucb.html">ucb</a>* soc;00553 UINT16 checksum;00554 UINT16 i;00555 INT8 sochandle;00556 00557 <span class="comment">/* Is this UDP? */</span>00558 00559 UDP_DEBUGOUT(<span class="stringliteral">"Processing UDP...\n\r"</span>);00560 00561 <span class="keywordflow">if</span>( frame-><a class="code" href="structip__frame.html#m6">protocol</a> != <a class="code" href="ip_8h.html#a2">IP_UDP</a> ) {00562 UDP_DEBUGOUT(<span class="stringliteral">"ERROR: The protocol is not UDP\n\r"</span>);00563 <span class="keywordflow">return</span>(-1);00564 }00565 00566 <span class="comment">/* Start processing the message */</span>00567 00568 <a class="code" href="system_8h.html#a14">NETWORK_RECEIVE_INITIALIZE</a>(frame-><a class="code" href="structip__frame.html#m11">buf_index</a>);00569 00570 received_udp_packet.<a class="code" href="structudp__frame.html#m0">sport</a> = ((UINT16)<a class="code" href="system_8h.html#a9">RECEIVE_NETWORK_B</a>()) << 8;00571 received_udp_packet.<a class="code" href="structudp__frame.html#m0">sport</a> |= <a class="code" href="system_8h.html#a9">RECEIVE_NETWORK_B</a>();00572 00573 received_udp_packet.<a class="code" href="structudp__frame.html#m1">dport</a> = ((UINT16)<a class="code" href="system_8h.html#a9">RECEIVE_NETWORK_B</a>()) << 8;00574 received_udp_packet.<a class="code" href="structudp__frame.html#m1">dport</a> |= <a class="code" href="system_8h.html#a9">RECEIVE_NETWORK_B</a>(); 00575 00576 received_udp_packet.<a class="code" href="structudp__frame.html#m2">tlen</a> = ((UINT16)<a class="code" href="system_8h.html#a9">RECEIVE_NETWORK_B</a>()) << 8;00577 received_udp_packet.<a class="code" href="structudp__frame.html#m2">tlen</a> |= <a class="code" href="system_8h.html#a9">RECEIVE_NETWORK_B</a>();00578 00579 received_udp_packet.<a class="code" href="structudp__frame.html#m3">checksum</a> = ((UINT16)<a class="code" href="system_8h.html#a9">RECEIVE_NETWORK_B</a>()) << 8;00580 received_udp_packet.<a class="code" href="structudp__frame.html#m3">checksum</a> |= <a class="code" href="system_8h.html#a9">RECEIVE_NETWORK_B</a>();00581 00582 <span class="keywordflow">if</span>(received_udp_packet.<a class="code" href="structudp__frame.html#m2">tlen</a> < <a class="code" href="tcp__ip_8h.html#a8">UDP_HLEN</a> ) {00583 UDP_DEBUGOUT(<span class="stringliteral">"UDP frame too short\n\r"</span>);00584 <span class="keywordflow">return</span>(-1);00585 }00586 00587 00588 <span class="comment">/* Map UDP socket */</span>00589 00590 sochandle = -1;00591 00592 <span class="keywordflow">for</span>( i=0; i < NO_OF_UDPSOCKETS; i++) {00593 soc = &udp_socket[i]; <span class="comment">/* Get referense */</span>00594 00595 <span class="keywordflow">if</span>(soc-><a class="code" href="structucb.html#m0">state</a> != <a class="code" href="tcp__ip_8h.html#a30">UDP_STATE_OPENED</a> )00596 <span class="keywordflow">continue</span>;00597 00598 <span class="keywordflow">if</span>(soc-><a class="code" href="structucb.html#m2">locport</a> != received_udp_packet.<a class="code" href="structudp__frame.html#m1">dport</a>)00599 <span class="keywordflow">continue</span>;00600 00601 <span class="comment">/* Socket found */</span>00602 00603 sochandle = i;00604 00605 <span class="keywordflow">break</span>;00606 }00607 00608 <span class="keywordflow">if</span>( sochandle < 0 ) {00609 UDP_DEBUGOUT(<span class="stringliteral">"No socket found for received UDP Packet\r\n"</span>);00610 00611 <span class="comment">/* TODO: Send ICMP */</span>00612 00613 <span class="keywordflow">return</span>(-1);00614 }00615 00616 <span class="comment">/* Calculate checksum for received packet */</span>00617 00618 <span class="keywordflow">if</span>(soc-><a class="code" href="structucb.html#m3">opts</a> & <a class="code" href="tcp__ip_8h.html#a6">UDP_OPT_CHECK_CS</a>) {00619 <span class="keywordflow">if</span>(received_udp_packet.<a class="code" href="structudp__frame.html#m3">checksum</a> != 0) {00620 checksum = 0;00621 i = 0;00622 00623 <span class="comment">/* Do it firstly to IP pseudo header */</span>00624 00625 checksum = <a class="code" href="ip_8c.html#a7">ip_checksum</a>(checksum, (UINT8)(frame-><a class="code" href="structip__frame.html#m9">dip</a> >> 24), (UINT8)i++); 00626 checksum = <a class="code" href="ip_8c.html#a7">ip_checksum</a>(checksum, (UINT8)(frame-><a class="code" href="structip__frame.html#m9">dip</a> >> 16), (UINT8)i++);00627 checksum = <a class="code" href="ip_8c.html#a7">ip_checksum</a>(checksum, (UINT8)(frame-><a class="code" href="structip__frame.html#m9">dip</a> >> 8), (UINT8)i++);00628 checksum = <a class="code" href="ip_8c.html#a7">ip_checksum</a>(checksum, (UINT8)frame-><a class="code" href="structip__frame.html#m9">dip</a>, (UINT8)i++);00629 00630 checksum = <a class="code" href="ip_8c.html#a7">ip_checksum</a>(checksum, (UINT8)(frame-><a class="code" href="structip__frame.html#m8">sip</a> >> 24), (UINT8)i++); 00631 checksum = <a class="code" href="ip_8c.html#a7">ip_checksum</a>(checksum, (UINT8)(frame-><a class="code" href="structip__frame.html#m8">sip</a> >> 16), (UINT8)i++);00632 checksum = <a class="code" href="ip_8c.html#a7">ip_checksum</a>(checksum, (UINT8)(frame-><a class="code" href="structip__frame.html#m8">sip</a> >> 8), (UINT8)i++);00633 checksum = <a class="code" href="ip_8c.html#a7">ip_checksum</a>(checksum, (UINT8)frame-><a class="code" href="structip__frame.html#m8">sip</a>, (UINT8)i++); 00634 00635 checksum = <a class="code" href="ip_8c.html#a7">ip_checksum</a>(checksum, 0, (UINT8)i++);00636 00637 checksum = <a class="code" href="ip_8c.html#a7">ip_checksum</a>(checksum, (UINT8)<a class="code" href="ip_8h.html#a2">IP_UDP</a>, (UINT8)i++);00638 00639 checksum = <a class="code" href="ip_8c.html#a7">ip_checksum</a>(checksum, (UINT8)(len >> 8), (UINT8)i++);00640 checksum = <a class="code" href="ip_8c.html#a7">ip_checksum</a>(checksum, (UINT8)len, (UINT8)i++); 00641 00642 00643 <a class="code" href="system_8h.html#a14">NETWORK_RECEIVE_INITIALIZE</a>(frame-><a class="code" href="structip__frame.html#m11">buf_index</a>);00644 00645 <span class="keywordflow">for</span>(i=0; i < len; i++)00646 checksum = <a class="code" href="ip_8c.html#a7">ip_checksum</a>(checksum, <a class="code" href="system_8h.html#a9">RECEIVE_NETWORK_B</a>(), (UINT8)i);00647 00648 checksum = ~ checksum;00649 00650 <span class="keywordflow">if</span>(checksum != IP_GOOD_CS) {00651 UDP_DEBUGOUT(<span class="stringliteral">"ERROR: UDP Checksum failed!\n\r"</span>);00652 <span class="keywordflow">return</span> (-1);00653 }00654 00655 }00656 00657 UDP_DEBUGOUT(<span class="stringliteral">"UDP Checksum OK\n\r"</span>);00658 00659 }00660 00661 00662 received_udp_packet.<a class="code" href="structudp__frame.html#m4">buf_index</a> = frame-><a class="code" href="structip__frame.html#m11">buf_index</a> + <a class="code" href="tcp__ip_8h.html#a8">UDP_HLEN</a>;00663 <a class="code" href="system_8h.html#a14">NETWORK_RECEIVE_INITIALIZE</a>(received_udp_packet.<a class="code" href="structudp__frame.html#m4">buf_index</a>);00664 00665 <span class="comment">/* Generate data event */</span>00666 00667 soc->event_listener(sochandle, <a class="code" href="tcp__ip_8h.html#a31">UDP_EVENT_DATA</a>, frame-><a class="code" href="structip__frame.html#m8">sip</a>, received_udp_packet.<a class="code" href="structudp__frame.html#m0">sport</a>, received_udp_packet.<a class="code" href="structudp__frame.html#m4">buf_index</a>, received_udp_packet.<a class="code" href="structudp__frame.html#m2">tlen</a> - <a class="code" href="tcp__ip_8h.html#a8">UDP_HLEN</a>);00668 00669 <span class="keywordflow">return</span>(1);00670 00671 }00672 <a name="l00684"></a><a class="code" href="udp_8c.html#a9">00684</a> UINT16 <a class="code" href="udp_8c.html#a71">udp_getfreeport</a> (<span class="keywordtype">void</span>){00685 <span class="keyword">struct </span><a class="code" href="structucb.html">ucb</a>* soc;00686 <span class="keyword">static</span> UINT16 lastport = 1;00687 UINT16 start;00688 UINT16 i;00689 00690 00691 <span class="comment">/* Try with every port to every socket untill free found */</span>00692 00693 <span class="keywordflow">for</span>( start = lastport++; start != lastport; lastport++) {00694 <span class="keywordflow">if</span>(lastport == <a class="code" href="group__opentcp__config.html#a8">UDP_PORTS_END</a>)00695 lastport = 1;00696 00697 <span class="keywordflow">for</span>(i = 0; i < NO_OF_UDPSOCKETS; i++) {00698 soc = &udp_socket[i]; <span class="comment">/* Get socket */</span>00699 00700 <span class="keywordflow">if</span>( (soc-><a class="code" href="structucb.html#m0">state</a> > <a class="code" href="tcp__ip_8h.html#a29">UDP_STATE_CLOSED</a>) && (soc-><a class="code" href="structucb.html#m2">locport</a> == lastport) ) {00701 <span class="comment">/* Blaah, this socket has reserved the port, go to next one */</span>00702 <span class="keywordflow">break</span>; 00703 }00704 00705 } 00706 00707 <span class="comment">/* Did we found it? */</span>00708 00709 <span class="keywordflow">if</span>( i == NO_OF_UDPSOCKETS)00710 <span class="keywordflow">break</span>; 00711 00712 }00713 00714 <span class="keywordflow">if</span>(lastport == start) {00715 DEBUGOUT(<span class="stringliteral">"Out of UDP ports!!\n\r"</span>);00716 <span class="keywordflow">return</span>(0);00717 }00718 00719 <span class="keywordflow">return</span>(lastport);00720 00721 }00722 00723 00724 00725 00726 </pre></div><hr><address style="align: right;"><small>Generated on Sun Aug 3 20:33:00 2003 for OpenTCP by<a href="http://www.doxygen.org/index.html"><img src="doxygen.png" alt="doxygen" align="middle" border=0 width=110 height=53></a>1.2.18 </small></address></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -