📄 tinytcp.lst
字号:
261 2 s = *sp;
262 2 if ( s == ds ) {
263 3 *sp = s->next;
264 3 break;
265 3 }
266 2 if ( s == NIL ) break;
267 2 sp = &s->next;
268 2 }
269 1 }
270
271 /*
272 * busy-wait loop for tcp. Also calls an "application proc"
273 */
274 //void tcp(procref application)
275 void tcp(void) reentrant
276 {
277 1 unsigned char i ;
278 1 char *ptr ;
279 1
280 1 //in_Header *ip;
281 1 //DWORD timeout, start;
282 1 //DWORD x;
283 1
284 1 sed_Receive(0);
285 1
286 1 timeout_tcp = clock_ValueRough() + tcp_RETRANSMITTIME;
287 1 while ( tcp_allsocs ) {
288 2 start_tcp = clock_ValueRough();
289 2 ip_tcp = (in_Header *)sed_IsPacket();
290 2
291 2 /* Modified by junku
292 2 * Retransmitter is on this
293 2 */
294 2 if ( ip_tcp == NIL ) {
295 3 if ( clock_ValueRough() > timeout_tcp ) {
296 4 tcp_Retransmitter();
297 4 timeout_tcp = clock_ValueRough() + tcp_RETRANSMITTIME;
298 4 }
299 3
300 3 //application();
301 3 //print("before test_application\n\r");
C51 COMPILER V7.07 TINYTCP 04/20/2004 18:04:42 PAGE 6
302 3 test_application();
303 3 //print("after test_application\n\r");
304 3 continue;
305 3 }
306 2
307 2 if ( sed_CheckPacket(ip_tcp, 0x806) == 1 ) {
*** WARNING C182 IN LINE 307 OF SRC\TINYTCP.C: pointer to different objects
308 3 #ifdef DEBUG
/* do arp */
print("arp packet") ;
for ( i=0,ptr=(char *)ip_tcp ; i<sizeof( arp_Header ); i++) {
putb_ser( *ptr++ ) ;
}
print("\r\n") ;
#endif
316 3 sar_CheckPacket(ip_tcp);
*** WARNING C182 IN LINE 316 OF SRC\TINYTCP.C: pointer to different objects
317 3 } else if ( sed_CheckPacket(ip_tcp, 0x800) == 1 ) {
*** WARNING C182 IN LINE 317 OF SRC\TINYTCP.C: pointer to different objects
318 3 #ifdef DEBUG /* do IP */
print("ip packet : ") ;
for ( i=0,ptr=(char *)ip_tcp ; i<sizeof( in_Header ); i++) {
putb_ser( *ptr++ ) ;
print("#");
}
print("\r\n") ;
#endif
326 3 if ( ip_tcp->destination == sin_lclINAddr && in_GetProtocol(ip_tcp) == 6
327 3 && checksum((WORD *)ip_tcp, in_GetHdrlenBytes(ip_tcp)) == 0xFFFF )
328 3 {
329 4 //print("it's our ip\r\n") ;
330 4 tcp_Handler(ip_tcp);
331 4 //print("\n\r1");
332 4 }
333 3 }
334 2 /* recycle buffer */
335 2 sed_Receive(ip_tcp);
*** WARNING C182 IN LINE 335 OF SRC\TINYTCP.C: pointer to different objects
336 2 //print("\n\r2");
337 2 x_tcp = clock_ValueRough() - start_tcp;
338 2 timeout_tcp -= x_tcp;
339 2 }
340 1 return;
341 1 }
*** WARNING C280 IN LINE 277 OF SRC\TINYTCP.C: 'i': unreferenced local variable
*** WARNING C280 IN LINE 278 OF SRC\TINYTCP.C: 'ptr': unreferenced local variable
342
343 /*
344 * Write data to a connection.
345 * Returns number of bytes written, == 0 when connection is not in
346 * established state.
347 */
348 //void tcp_Write( tcp_Socket *s, BYTE *dp, WORD len )
349 WORD tcp_Write( tcp_Socket *s, BYTE *dp, WORD len )
350 {
351 1 WORD x;
352 1
353 1 if ( s->state != tcp_StateESTAB ) len = 0;
354 1 if ( len > (x = tcp_MaxData - s->dataSize) ) len = x;
355 1 if ( len > 0 ) {
356 2 Move(dp, &s->datas[s->dataSize], len);
357 2 s->dataSize += len;
C51 COMPILER V7.07 TINYTCP 04/20/2004 18:04:42 PAGE 7
358 2 tcp_Flush(s);
359 2 }
360 1
361 1 return ( len );
362 1 }
363
364 /*
365 * Send pending data
366 */
367 void tcp_Flush(tcp_Socket *s)
368 {
369 1 if ( s->dataSize > 0 ) {
370 2 s->flags |= tcp_FlagPUSH;
371 2 tcp_Send(s);
372 2 }
373 1 }
374
375 /*
376 * Handler for incoming packets.
377 */
378 void tcp_Handler( in_Header *ip )
379 {
380 1 tcp_Header *tp;
381 1 tcp_PseudoHeader ph;
382 1 WORD len;
383 1 BYTE *dp;
384 1 WORD x, diff;
385 1 tcp_Socket *s;
386 1 WORD flags;
387 1
388 1 len = in_GetHdrlenBytes(ip);
389 1 tp = (tcp_Header *)((BYTE *)ip + len);
390 1 len = ip->length - len;
391 1
392 1 /* demux to active sockets */
393 1 for ( s = tcp_allsocs; s; s = s->next )
394 1 if ( s->hisport != 0 &&
395 1 tp->dstPort == s->myport &&
396 1 tp->srcPort == s->hisport &&
397 1 ip->source == s->hisaddr ) break;
398 1 if ( s == NIL ) {
399 2 /* demux to passive sockets */
400 2 for ( s = tcp_allsocs; s; s = s->next )
401 2 if ( s->hisport == 0 && tp->dstPort == s->myport ) break;
402 2 }
403 1 if ( s == NIL ) {
404 2 tcp_Reset( ip, tp );
405 2 #ifdef DEBUG
if ( tcp_logState & tcp_LOGPACKETS ) tcp_DumpHeader(ip, tp, "Discarding");
#endif
408 2 return;
409 2 }
410 1
411 1 #ifdef DEBUG
if ( tcp_logState & tcp_LOGPACKETS )
tcp_DumpHeader(ip, tp, "Received");
#endif
415 1
416 1 /* save his ethernet address */
417 1 Move(&((((eth_Header *)ip) - 1)->source[0]), &s->hisethaddr[0], sizeof(eth_HwAddress));
418 1
419 1 ph.src = ip->source;
C51 COMPILER V7.07 TINYTCP 04/20/2004 18:04:42 PAGE 8
420 1 ph.dst = ip->destination;
421 1 ph.mbz = 0;
422 1 ph.protocol = 6;
423 1 ph.length = len;
424 1 ph.checksum = checksum((WORD *)tp, len);
425 1 if ( checksum((WORD *)&ph, sizeof ph) != 0xFFFF )
426 1 {
427 2 #ifdef DEBUG
print("bad tcp checksum, received anyway\r\n");
#endif
430 2 }
431 1
432 1 flags = tp->flags;
433 1 if ( flags & tcp_FlagRST ) {
434 2 #ifdef DEBUG
print("connection reset\r\n");
#endif
437 2 s->state = tcp_StateCLOSED;
438 2 // s->dataHandler((tcp_Socket *)s,(BYTE*) 0, (WORD *)-1);
439 2 DATAHANDLER((tcp_Socket *)s,(BYTE*) 0,-1);
440 2 tcp_Unthread(s);
441 2 return;
442 2 }
443 1
444 1 switch ( s->state ) {
445 2 case tcp_StateLISTEN:
446 2 if ( flags & tcp_FlagSYN ) {
447 3 s->acknum = tp->seqnum + 1;
448 3 s->hisport = tp->srcPort;
449 3 s->hisaddr = ip->source;
450 3 s->flags = tcp_FlagSYN | tcp_FlagACK;
451 3 tcp_Send(s);
452 3 s->state = tcp_StateSYNREC;
453 3 s->unhappy = true;
454 3 s->timeout = tcp_TIMEOUT;
455 3 #ifdef DEBUG
print("Syn from 0x%x#%d (seq 0x%x)\r\n", s->hisaddr, s->hisport, tp->seqnum);
#endif
458 3 }
459 2 break;
460 2
461 2 case tcp_StateSYNSENT:
462 2 if ( flags & tcp_FlagSYN ) {
463 3 s->acknum++;
464 3 s->flags = tcp_FlagACK;
465 3 s->timeout = tcp_TIMEOUT;
466 3 if ( (flags & tcp_FlagACK) && tp->acknum == (s->seqnum + 1) ) {
467 4 #ifdef DEBUG
print("Open\r\n");
#endif
470 4 s->state = tcp_StateESTAB;
471 4 s->seqnum++;
472 4 s->acknum = tp->seqnum + 1;
473 4 s->unhappy = false;
474 4 } else {
475 4 s->state = tcp_StateSYNREC;
476 4 }
477 3 }
478 2 break;
479 2
480 2 case tcp_StateSYNREC:
481 2 if ( flags & tcp_FlagSYN ) {
C51 COMPILER V7.07 TINYTCP 04/20/2004 18:04:42 PAGE 9
482 3 s->flags = tcp_FlagSYN | tcp_FlagACK;
483 3 tcp_Send(s);
484 3 s->timeout = tcp_TIMEOUT;
485 3 #ifdef DEBUG
print(" retransmit of original syn\r\n");
#endif
488 3 }
489 2 if ( (flags & tcp_FlagACK) && tp->acknum == (s->seqnum + 1) ) {
490 3 s->flags = tcp_FlagACK;
491 3 tcp_Send(s);
492 3 s->seqnum++;
493 3 s->unhappy = false;
494 3 s->state = tcp_StateESTAB;
495 3 s->timeout = tcp_TIMEOUT;
496 3 #ifdef DEBUG
print("Synack received - connection established\r\n");
#endif
499 3 }
500 2 break;
501 2
502 2 case tcp_StateESTAB:
503 2 if ( (flags & tcp_FlagACK) == 0 ) return;
504 2 /* process ack value in packet */
505 2 diff = tp->acknum - s->seqnum;
506 2 if ( diff > 0 ) {
507 3 Move(&s->datas[diff], &s->datas[0], diff);
508 3 s->dataSize -= diff;
509 3 s->seqnum += diff;
510 3 }
511 2 s->flags = tcp_FlagACK;
512 2 tcp_ProcessData(s, tp, len);
513 2 break;
514 2
515 2 case tcp_StateFINWT1:
516 2 if ( (flags & tcp_FlagACK) == 0 ) return;
517 2 diff = tp->acknum - s->seqnum - 1;
518 2 s->flags = tcp_FlagACK | tcp_FlagFIN;
519 2
520 2 /*
521 2 * This is modified by Jun-Ku.
522 2 * If we send data and it's unacked. then send FIN. the receiver send diff = dataSize+1 (and it's ACK o
-n FIN ).
523 2 * if ( diff == 0 ) {
524 2 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -