📄 udp_8c-source.html
字号:
00274 }00275 00276 <a name="l00291"></a><a class="code" href="udp_8c.html#a5">00291</a> INT8 <a class="code" href="udp_8c.html#a5">udp_open</a> (INT8 sochandle, UINT16 locport)00292 {00293 <span class="keyword">struct </span><a class="code" href="structucb.html">ucb</a>* soc;00294 00295 <span class="keywordflow">if</span>( NO_OF_UDPSOCKETS < 0 )00296 <span class="keywordflow">return</span>(-1);00297 00298 <span class="keywordflow">if</span>( NO_OF_UDPSOCKETS == 0 )00299 <span class="keywordflow">return</span>(-1);00300 00301 <span class="keywordflow">if</span>( sochandle > NO_OF_UDPSOCKETS ) {00302 UDP_DEBUGOUT(<span class="stringliteral">"Socket handle non-valid\r\n"</span>);00303 <span class="keywordflow">return</span>(-1);00304 }00305 00306 <span class="keywordflow">if</span>( sochandle < 0 ) {00307 UDP_DEBUGOUT(<span class="stringliteral">"Socket handle non-valid\r\n"</span>);00308 <span class="keywordflow">return</span>(-1);00309 }00310 00311 <span class="keywordflow">if</span>(locport == 0) {00312 locport=<a class="code" href="udp_8c.html#a71">udp_getfreeport</a>();00313 <span class="keywordflow">return</span>(-1);00314 }00315 00316 soc = &udp_socket[sochandle]; <span class="comment">/* Get referense */</span>00317 00318 soc-><a class="code" href="structucb.html#m0">state</a> = <a class="code" href="tcp__ip_8h.html#a30">UDP_STATE_OPENED</a>;00319 soc-><a class="code" href="structucb.html#m2">locport</a> = locport;00320 00321 <span class="keywordflow">return</span>(sochandle);00322 00323 }00324 00325 00326 <a name="l00340"></a><a class="code" href="udp_8c.html#a6">00340</a> INT8 <a class="code" href="udp_8c.html#a6">udp_close</a> (INT8 sochandle)00341 {00342 <span class="keyword">struct </span><a class="code" href="structucb.html">ucb</a>* soc;00343 00344 <span class="keywordflow">if</span>( NO_OF_UDPSOCKETS < 0 )00345 <span class="keywordflow">return</span>(-1);00346 00347 <span class="keywordflow">if</span>( NO_OF_UDPSOCKETS == 0 )00348 <span class="keywordflow">return</span>(-1);00349 00350 <span class="keywordflow">if</span>( sochandle > NO_OF_UDPSOCKETS ) {00351 UDP_DEBUGOUT(<span class="stringliteral">"Socket handle non-valid\r\n"</span>);00352 <span class="keywordflow">return</span>(-1);00353 }00354 00355 <span class="keywordflow">if</span>( sochandle < 0 ) {00356 UDP_DEBUGOUT(<span class="stringliteral">"Socket handle non-valid\r\n"</span>);00357 <span class="keywordflow">return</span>(-1);00358 }00359 00360 soc = &udp_socket[sochandle]; <span class="comment">/* Get referense */</span>00361 00362 soc-><a class="code" href="structucb.html#m0">state</a> = <a class="code" href="tcp__ip_8h.html#a29">UDP_STATE_CLOSED</a>;00363 00364 <span class="keywordflow">return</span>(sochandle);00365 00366 }00367 00368 <a name="l00395"></a><a class="code" href="udp_8c.html#a7">00395</a> INT16 <a class="code" href="udp_8c.html#a7">udp_send</a> (INT8 sochandle, UINT32 remip, UINT16 remport, UINT8* buf, UINT16 blen, UINT16 dlen)00396 {00397 <span class="keyword">struct </span><a class="code" href="structucb.html">ucb</a>* soc;00398 UINT8* user_buf_start;00399 UINT16 cs;00400 UINT8 cs_cnt;00401 INT16 i;00402 00403 <span class="keywordflow">if</span>( NO_OF_UDPSOCKETS < 0 )00404 <span class="keywordflow">return</span>(-1);00405 00406 <span class="keywordflow">if</span>( NO_OF_UDPSOCKETS == 0 )00407 <span class="keywordflow">return</span>(-1);00408 00409 <span class="keywordflow">if</span>( sochandle > NO_OF_UDPSOCKETS ) {00410 UDP_DEBUGOUT(<span class="stringliteral">"Socket handle non-valid\r\n"</span>);00411 <span class="keywordflow">return</span>(-1);00412 }00413 00414 <span class="keywordflow">if</span>( sochandle < 0 ) {00415 UDP_DEBUGOUT(<span class="stringliteral">"Socket handle non-valid\r\n"</span>);00416 <span class="keywordflow">return</span>(-1);00417 }00418 00419 <span class="keywordflow">if</span>(remip == 0) {00420 UDP_DEBUGOUT(<span class="stringliteral">"Remote IP 0 not allowed\r\n"</span>);00421 <span class="keywordflow">return</span>(-1);00422 }00423 00424 <span class="keywordflow">if</span>(remport == 0) {00425 UDP_DEBUGOUT(<span class="stringliteral">"Remote port 0 not allowed\r\n"</span>);00426 <span class="keywordflow">return</span>(-1);00427 }00428 00429 <span class="keywordflow">if</span>( dlen > blen )00430 dlen = blen;00431 00432 <span class="keywordflow">if</span>( (dlen + <a class="code" href="tcp__ip_8h.html#a8">UDP_HLEN</a>) > UDP_SEND_MTU)00433 dlen = UDP_SEND_MTU - <a class="code" href="tcp__ip_8h.html#a8">UDP_HLEN</a>;00434 00435 soc = &udp_socket[sochandle]; <span class="comment">/* Get referense */</span>00436 00437 <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> ) {00438 UDP_DEBUGOUT(<span class="stringliteral">"UDP Socket Closed\r\n"</span>);00439 <span class="keywordflow">return</span>(-3);00440 }00441 00442 <span class="keywordflow">if</span>(soc-><a class="code" href="structucb.html#m2">locport</a> == 0) {00443 UDP_DEBUGOUT(<span class="stringliteral">"ERROR:Socket local port is zero\r\n"</span>);00444 <span class="keywordflow">return</span>(-1);00445 }00446 00447 user_buf_start = buf;00448 00449 buf -= <a class="code" href="tcp__ip_8h.html#a8">UDP_HLEN</a>;00450 00451 <span class="comment">/* Put header */</span>00452 00453 *buf++ = (UINT8)(soc-><a class="code" href="structucb.html#m2">locport</a> >> 8);00454 *buf++ = (UINT8)soc-><a class="code" href="structucb.html#m2">locport</a>;00455 *buf++ = (UINT8)(remport >> 8);00456 *buf++ = (UINT8)remport;00457 *buf++ = (UINT8)((dlen + <a class="code" href="tcp__ip_8h.html#a8">UDP_HLEN</a>) >> 8);00458 *buf++ = (UINT8)(dlen + <a class="code" href="tcp__ip_8h.html#a8">UDP_HLEN</a>);00459 *buf++ = 0;00460 *buf = 0;00461 00462 buf = user_buf_start;00463 buf -= <a class="code" href="tcp__ip_8h.html#a8">UDP_HLEN</a>;00464 00465 <span class="comment">/* Calculate checksum if needed */</span>00466 00467 cs = 0;00468 00469 <span class="keywordflow">if</span>( soc-><a class="code" href="structucb.html#m3">opts</a> & <a class="code" href="tcp__ip_8h.html#a5">UDP_OPT_SEND_CS</a>) {00470 cs = 0;00471 cs_cnt = 0;00472 00473 <span class="comment">/* Do it firstly to IP pseudo header */</span>00474 00475 cs = <a class="code" href="ip_8c.html#a7">ip_checksum</a>(cs, (UINT8)(localmachine.localip >> 24), cs_cnt++); 00476 cs = <a class="code" href="ip_8c.html#a7">ip_checksum</a>(cs, (UINT8)(localmachine.localip >> 16), cs_cnt++);00477 cs = <a class="code" href="ip_8c.html#a7">ip_checksum</a>(cs, (UINT8)(localmachine.localip >> 8), cs_cnt++);00478 cs = <a class="code" href="ip_8c.html#a7">ip_checksum</a>(cs, (UINT8)localmachine.localip, cs_cnt++);00479 00480 cs = <a class="code" href="ip_8c.html#a7">ip_checksum</a>(cs, (UINT8)(remip >> 24), cs_cnt++); 00481 cs = <a class="code" href="ip_8c.html#a7">ip_checksum</a>(cs, (UINT8)(remip >> 16), cs_cnt++);00482 cs = <a class="code" href="ip_8c.html#a7">ip_checksum</a>(cs, (UINT8)(remip >> 8), cs_cnt++);00483 cs = <a class="code" href="ip_8c.html#a7">ip_checksum</a>(cs, (UINT8)remip, cs_cnt++); 00484 00485 cs = <a class="code" href="ip_8c.html#a7">ip_checksum</a>(cs, 0, cs_cnt++);00486 00487 cs = <a class="code" href="ip_8c.html#a7">ip_checksum</a>(cs, (UINT8)<a class="code" href="ip_8h.html#a2">IP_UDP</a>, cs_cnt++);00488 00489 cs = <a class="code" href="ip_8c.html#a7">ip_checksum</a>(cs, (UINT8)((dlen + <a class="code" href="tcp__ip_8h.html#a8">UDP_HLEN</a>) >> 8), cs_cnt++);00490 cs = <a class="code" href="ip_8c.html#a7">ip_checksum</a>(cs, (UINT8)(dlen + <a class="code" href="tcp__ip_8h.html#a8">UDP_HLEN</a>), cs_cnt++); 00491 00492 <span class="comment">/* Go to UDP header + data */</span>00493 00494 buf = user_buf_start;00495 buf -= <a class="code" href="tcp__ip_8h.html#a8">UDP_HLEN</a>;00496 00497 cs = <a class="code" href="ip_8c.html#a8">ip_checksum_buf</a>(cs, buf, dlen + <a class="code" href="tcp__ip_8h.html#a8">UDP_HLEN</a>);00498 00499 cs = ~ cs;00500 00501 <span class="keywordflow">if</span>(cs == 0)00502 cs = 0xFFFF;00503 00504 <span class="comment">/* Save checksum in correct place */</span>00505 buf = user_buf_start;00506 buf -= <a class="code" href="tcp__ip_8h.html#a8">UDP_HLEN</a>;00507 buf += 6;00508 *buf++ = (UINT8)(cs >> 8);00509 *buf = (UINT8)cs; 00510 00511 buf = user_buf_start;00512 buf -= <a class="code" href="tcp__ip_8h.html#a8">UDP_HLEN</a>;00513 00514 }00515 00516 <span class="comment">/* Send it to IP */</span>00517
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -