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

📄 tcp.lst

📁 ENC28J60 System HTTP
💻 LST
📖 第 1 页 / 共 5 页
字号:
 282:net/tcp.c     **** 										softuart_putc(' ');
 283:net/tcp.c     **** 										#endif
 284:net/tcp.c     **** 									
 285:net/tcp.c     **** 										break;
 286:net/tcp.c     **** 									case (TCP_APPSTATE_FINISHED):
 287:net/tcp.c     **** 										#if TCP_DEBUG
 288:net/tcp.c     **** 										softuart_puts_progmem("TCP : app closed socket ");
 289:net/tcp.c     **** 										softuart_put_uint8(socketnum);
 290:net/tcp.c     **** 										#endif
 291:net/tcp.c     **** 										tcp_sockets[socketnum].state = TCP_STATE_FIN_WAIT1;
 292:net/tcp.c     **** 										tcp_send_packet(buffer, socketnum, (TCP_FIN_FLAG|TCP_PSH_FLAG|TCP_ACK_FLAG), 0);
 293:net/tcp.c     **** 										//seq counter update:
 294:net/tcp.c     **** 										tcp_sockets[socketnum].seq = tcp_sockets[socketnum].seq + 1;
 295:net/tcp.c     **** 										break;
 296:net/tcp.c     **** 									default:
 297:net/tcp.c     **** 										//do nothing... should not happen!
 298:net/tcp.c     **** 										break;
 299:net/tcp.c     **** 								}
 300:net/tcp.c     **** 							}
 301:net/tcp.c     **** 							break;
 302:net/tcp.c     **** 
 303:net/tcp.c     **** 						///CLOSE WAIT
 304:net/tcp.c     **** 						case(TCP_STATE_CLOSE_WAIT):
 305:net/tcp.c     **** 							//goto last_ack & send FIN packet
 306:net/tcp.c     **** 							tcp_sockets[socketnum].state = TCP_STATE_LAST_ACK;
 307:net/tcp.c     **** 							tcp_sockets[socketnum].ack = nic_buffer_to_seq(&buffer[TCP_POS_SEQ])+1;
 308:net/tcp.c     **** 							tcp_sockets[socketnum].seq = nic_buffer_to_seq(&buffer[TCP_POS_ACK]);
 309:net/tcp.c     **** 
 310:net/tcp.c     **** 							tcp_send_packet(buffer, socketnum, (TCP_FIN_FLAG), 0);
 311:net/tcp.c     **** 							#if TCP_DEBUG
 312:net/tcp.c     **** 							softuart_puts_progmem("LAST_ACK");
 313:net/tcp.c     **** 							#endif
 314:net/tcp.c     **** 							break;
 315:net/tcp.c     **** 
 316:net/tcp.c     **** 						///LAST_ACK
 317:net/tcp.c     **** 						case(TCP_STATE_LAST_ACK):
 318:net/tcp.c     **** 							//socket is closed
 319:net/tcp.c     **** 							tcp_close_socket(socketnum);
 320:net/tcp.c     **** 							#if TCP_DEBUG
 321:net/tcp.c     **** 							softuart_puts_progmem("LAST_ACK->CLOSED");
 322:net/tcp.c     **** 							#endif
 323:net/tcp.c     **** 							break;
 324:net/tcp.c     **** 
 325:net/tcp.c     **** 						///FIN WAIT 1
 326:net/tcp.c     **** 						case(TCP_STATE_FIN_WAIT1):
 327:net/tcp.c     **** 							//if we receive FIN
 328:net/tcp.c     **** 							tcp_sockets[socketnum].ack = nic_buffer_to_seq(&buffer[TCP_POS_SEQ])+1;
 329:net/tcp.c     **** 							
 330:net/tcp.c     **** 							#if TCP_DEBUG
 331:net/tcp.c     **** 							softuart_puts_progmem("FIN_WAIT1->");
 332:net/tcp.c     **** 							#endif
 333:net/tcp.c     **** 							if (tcp_header_flags == TCP_FIN_FLAG){
 334:net/tcp.c     **** 								//goto CLOSING & send an ack:
 335:net/tcp.c     **** 								tcp_sockets[socketnum].state = TCP_STATE_CLOSING;
 336:net/tcp.c     **** 								tcp_send_packet(buffer, socketnum, (TCP_ACK_FLAG), 0);
 337:net/tcp.c     **** 								#if TCP_DEBUG
 338:net/tcp.c     **** 								softuart_puts_progmem("CLOSING");
 339:net/tcp.c     **** 								#endif
 340:net/tcp.c     **** 							}else if(tcp_header_flags == (TCP_FIN_FLAG | TCP_ACK_FLAG)){
 341:net/tcp.c     **** 								//FIN&ACK -> send ACK & close !
 342:net/tcp.c     **** 								if (nic_buffer_to_seq(&buffer[TCP_POS_ACK]) == tcp_sockets[socketnum].seq){
 343:net/tcp.c     **** 									//close socket!
 344:net/tcp.c     **** 									tcp_close_socket(socketnum);
 345:net/tcp.c     **** 									#if TCP_DEBUG
 346:net/tcp.c     **** 									softuart_puts_progmem("CLOSED !");
 347:net/tcp.c     **** 									#endif
 348:net/tcp.c     **** 								}else{
 349:net/tcp.c     **** 									//both have sent the connection close request, we must wait for an last ack
 350:net/tcp.c     **** 									tcp_sockets[socketnum].state = TCP_STATE_LAST_ACK;
 351:net/tcp.c     **** 									#if TCP_DEBUG
 352:net/tcp.c     **** 									softuart_puts_progmem("LAST ACK !");
 353:net/tcp.c     **** 									#endif
 354:net/tcp.c     **** 								}
 355:net/tcp.c     **** 								tcp_send_packet(buffer, socketnum, (TCP_ACK_FLAG), 0);
 356:net/tcp.c     **** 
 357:net/tcp.c     **** 							}else{
 358:net/tcp.c     **** 								//ACK -> goto FIN WAIT2
 359:net/tcp.c     **** 								tcp_sockets[socketnum].state = TCP_STATE_FIN_WAIT2;
 360:net/tcp.c     **** 								#if TCP_DEBUG
 361:net/tcp.c     **** 								softuart_puts_progmem("FIN_WAIT2");
 362:net/tcp.c     **** 								#endif
 363:net/tcp.c     **** 							}
 364:net/tcp.c     **** 							break;
 365:net/tcp.c     **** 						
 366:net/tcp.c     **** 						///FIN WAIT 2
 367:net/tcp.c     **** 						case(TCP_STATE_FIN_WAIT2):
 368:net/tcp.c     **** 							//if we receive FIN
 369:net/tcp.c     **** 							tcp_sockets[socketnum].ack = nic_buffer_to_seq(&buffer[TCP_POS_SEQ])+1;
 370:net/tcp.c     **** 							//tcp_sockets[socketnum].seq = nic_buffer_to_seq(&buffer[TCP_POS_ACK]);
 371:net/tcp.c     **** 
 372:net/tcp.c     **** 							if (tcp_header_flags & TCP_FIN_FLAG){
 373:net/tcp.c     **** 								//FIN -> goto TIMED WAIT
 374:net/tcp.c     **** 								tcp_sockets[socketnum].state = TCP_STATE_TIMED_WAIT;
 375:net/tcp.c     **** 
 376:net/tcp.c     **** 								tcp_send_packet(buffer, socketnum, (TCP_ACK_FLAG), 0);
 377:net/tcp.c     **** 								#if TCP_DEBUG
 378:net/tcp.c     **** 								softuart_puts_progmem("FIN_WAIT2->TIMED_WAIT");
 379:net/tcp.c     **** 								#endif
 380:net/tcp.c     **** 							}
 381:net/tcp.c     **** 							break;
 382:net/tcp.c     **** 
 383:net/tcp.c     **** 						///TIMED WAIT:
 384:net/tcp.c     **** 						case(TCP_STATE_TIMED_WAIT):
 385:net/tcp.c     **** 							#if TCP_DEBUG
 386:net/tcp.c     **** 							softuart_puts_progmem("TIMED_WAIT: packet in ?! ");
 387:net/tcp.c     **** 							#endif
 388:net/tcp.c     **** 							break;
 389:net/tcp.c     **** 
 390:net/tcp.c     **** 						///CLOSING
 391:net/tcp.c     **** 						case(TCP_STATE_CLOSING):
 392:net/tcp.c     **** 							if (tcp_header_flags & TCP_ACK_FLAG){
 393:net/tcp.c     **** 								tcp_sockets[socketnum].state = TCP_STATE_TIMED_WAIT;
 394:net/tcp.c     **** 								#if TCP_DEBUG
 395:net/tcp.c     **** 								softuart_puts_progmem("CLOSING->TIMED_WAIT");
 396:net/tcp.c     **** 								#endif
 397:net/tcp.c     **** 							}
 398:net/tcp.c     **** 							break;
 399:net/tcp.c     **** 
 400:net/tcp.c     **** 						default:
 401:net/tcp.c     **** 							#if TCP_DEBUG
 402:net/tcp.c     **** 							softuart_puts_progmem("ERROR: invalid tcp state ! socket closed!");
 403:net/tcp.c     **** 							#endif
 404:net/tcp.c     **** 							tcp_close_socket(socketnum);
 405:net/tcp.c     **** 					}
 406:net/tcp.c     **** 					#if TCP_DEBUG
 407:net/tcp.c     **** 					softuart_putnewline();
 408:net/tcp.c     **** 					#endif
 409:net/tcp.c     **** 
 410:net/tcp.c     **** 					//we must set timed wait TTL here because timed wait is not packet triggered
 411:net/tcp.c     **** 					if (tcp_sockets[socketnum].state == TCP_STATE_TIMED_WAIT){
 412:net/tcp.c     **** 						tcp_sockets[socketnum].ttl = 5; //5 seconds timeout
 413:net/tcp.c     **** 						#if TCP_DEBUG
 414:net/tcp.c     **** 						softuart_puts_progmem("TIMED_WAIT: timeout set to 5s");
 415:net/tcp.c     **** 						softuart_putnewline();
 416:net/tcp.c     **** 						#endif
 417:net/tcp.c     **** 					}
 418:net/tcp.c     **** 
 419:net/tcp.c     **** 					return;
 420:net/tcp.c     **** 				}
 421:net/tcp.c     **** 			}
 422:net/tcp.c     **** 		}
 423:net/tcp.c     **** 
 424:net/tcp.c     **** 	}
 425:net/tcp.c     **** 
 426:net/tcp.c     **** 	//there is no active socket! -> conn request ?
 427:net/tcp.c     **** 	if (tcp_header_flags == TCP_SYN_FLAG){
 428:net/tcp.c     **** 		//yes, connection request. find a free socket:
 429:net/tcp.c     **** 		socketnum = tcp_find_free_socket();
 430:net/tcp.c     **** 
 431:net/tcp.c     **** 		if (socketnum < TCP_SOCKET_COUNT){
 432:net/tcp.c     **** 			//free socket found (:= socketnum) -> handle request
 433:net/tcp.c     **** 			tcp_sockets[socketnum].state       = TCP_STATE_SYN_RECEIVED;
 434:net/tcp.c     **** 			tcp_sockets[socketnum].source_port = source_port;
 435:net/tcp.c     **** 			tcp_sockets[socketnum].source_ip   = source_ip;
 436:net/tcp.c     **** 			tcp_sockets[socketnum].dest_port   = dest_port;		
 437:net/tcp.c     **** 			tcp_sockets[socketnum].ack         = nic_buffer_to_seq(&buffer[TCP_POS_SEQ])+1;
 438:net/tcp.c     **** 			tcp_sockets[socketnum].seq         = 0;
 439:net/tcp.c     **** 			tcp_sockets[socketnum].ttl         = TCP_TTL_TIMEOUT;
 440:net/tcp.c     **** 			tcp_sockets[socketnum].misc_state  = 0;
 441:net/tcp.c     **** 
 442:net/tcp.c     **** 			//send SYN+ACK Packet
 443:net/tcp.c     **** 			tcp_send_packet(buffer, socketnum, (TCP_SYN_FLAG|TCP_ACK_FLAG), 0);
 444:net/tcp.c     **** 
 445:net/tcp.c     **** 			//init seq counter:
 446:net/tcp.c     **** 			tcp_sockets[socketnum].seq = 1;
 447:net/tcp.c     **** 
 448:net/tcp.c     **** 			//cleanup/initialise httpd connection:
 449:net/tcp.c     **** 			httpd_cleanup_conn(socketnum);
 450:net/tcp.c     **** 
 451:net/tcp.c     **** 			#if TCP_DEBUG
 452:net/tcp.c     **** 			softuart_puts_progmem("TCP : connection request. [OK]");
 453:net/tcp.c     **** 			softuart_putnewline();
 454:net/tcp.c     **** 			#endif
 455:net/tcp.c     **** 			return;
 456:net/tcp.c     **** 		}
 457:net/tcp.c     **** 	}
 458:net/tcp.c     **** 
 459:net/tcp.c     **** 	/////////////////////////////////////////////////////
 460:net/tcp.c     **** 	//if we get here something went wrong ...
 461:net/tcp.c     **** 	// - we have no open socket for this packet
 462:net/tcp.c     **** 	// - this is not an connection request
 463:net/tcp.c     **** 	// - we have no more free sockets left (!)
 464:net/tcp.c     **** 	//tcp_send_packet(buffer, socketnum, TCP_RST_FLAG, 0);//NO! WE HAVE NO SOCKNUM!
 465:net/tcp.c     **** 	#if TCP_DEBUG
 466:net/tcp.c     **** 	softuart_puts_progmem("TCP : WARN: ignoring packet (no socket open)");
 467:net/tcp.c     **** 	softuart_putnewline();
 468:net/tcp.c     **** 	#endif
 469:net/tcp.c     **** 	return;
 470:net/tcp.c     **** }
 471:net/tcp.c     **** 
 472:net/tcp.c     **** //called ~every second, removes dead tcp sockets
 473:net/tcp.c     **** // - decrement ttl (starts with TCP_TTL_TIMEOUT -> TCP_TTL_TIMEOUT seconds inactivity = timeout)
 474:net/tcp.c     **** // - if ttl==0 -> mark socket as closed !
 475:net/tcp.c     **** void tcp_ttl_cleanup(){
 476:net/tcp.c     **** 	unsigned char i;
 477:net/tcp.c     **** 	for(i=0; i<TCP_SOCKET_COUNT; i++){
 478:net/tcp.c     **** 		//decrement ttl:
 479:net/tcp.c     **** 		if (tcp_sockets[i].state != TCP_STATE_CLOSED){
 480:net/tcp.c     **** 			tcp_sockets[i].ttl--;
 481:net/tcp.c     **** 			
 482:net/tcp.c     **** 			//if socket TTL count is zero, close this socket!
 483:net/tcp.c     **** 			if (tcp_sockets[i].ttl == 0){
 484:net/tcp.c     **** 				tcp_close_socket(i);
 485:net/tcp.c     **** 
 486:net/tcp.c     **** 				#if TCP_DEBUG
 487:net/tcp.c     **** 				softuart_puts_progmem("TCP : TTL timeout for socket [");
 488:net/tcp.c     **** 				softuart_put_uint8(i);
 489:net/tcp.c     **** 				softuart_puts_progmem("]. socket closed.");
 490:net/tcp.c     **** 				softuart_putnewline();
 491:net/tcp.c     **** 				#endif
 492:net/tcp.c     **** 			}
 493:net/tcp.c     **** 		}
 494:net/tcp.c     **** 	}
 495:net/tcp.c     **** }
 496:net/tcp.c     **** 
 497:net/tcp.c     **** void tcp_close_socket(unsigned char socketnum){
 498:net/tcp.c     **** 	tcp_sockets[socketnum].state = TCP_STATE_CLOSED;
 499:net/tcp.c     **** 	httpd_cleanup_conn(socketnum);
 500:net/tcp.c     **** }
 501:net/tcp.c     **** 
 502:net/tcp.c     **** //send a tcp packet:
 503:net/tcp.c     **** void tcp_send_packet(unsigned char *buffer, unsigned char socketnum, unsigned char flags, unsigned 
 123               	
 124               	.LM0:
 125               	.LFBB1:
 126 0000 8F92      		push r8
 127 0002 9F92      		push r9
 128 0004 AF92      		push r10
 129 0006 BF92      		push r11
 130 0008 CF92      		push r12
 131 000a DF92      		push r13
 132 000c EF92      		push r14
 133 000e FF92      		push r15
 134 0010 0F93      		push r16
 135 0012 1F93      		push r17
 136 0014 CF93      		push r28
 137 0016 DF93      		push r29
 138               	/* prologue: function */
 139               	/* frame size = 0 */
 140 0018 EC01      		movw r28,r24
 141 001a F42E      		mov r15,r20
 142 001c 4901      		movw r8,r18
 504:net/tcp.c     **** void tcp_send_packet(unsigned char *buffer, unsigned char socketnum, unsigned char flags, unsigned 
 505:net/tcp.c     **** 	unsigned int arp_idx;
 506:net/tcp.c     **** 	unsigned int tmp;
 507:net/tcp.c     **** 	unsigned long *ip;
 508:net/tcp.c     **** 
 143               	M1-.LFBB1
 144               	.LM1:
 145 001e 062F      		mov r16,r22
 146 0020 10E0      		ldi r17,lo8(0)
 147 0022 83E1      		ldi r24,lo8(19)
 148 0024 90E0      		ldi r25,hi8(19)
 149 0026 089F      		mul r16,r24
 150 0028 F001      		movw r30,r0
 151 002a 099F      		mul r16,r25
 152 002c F00D      		add r31,r0
 153 002e 189F      		mul r17,r24
 154 0030 F00D      		add r31,r0
 155 0032 1124      		clr r1
 156 0034 E050      		subi r30,lo8(-(tcp_sockets))
 157 0036 F040      		sbci r31,hi8(-(tcp_sockets))
 158 0038 2481      		ldd r18,Z+4
 159 003a 3581      		ldd r19,Z+5
 160 003c 4681      		ldd r20,Z+6
 161 003e 5781      		ldd r21,Z+7
 162 0040 8091 0000 		lds r24,nic_ip
 163 0044 9091 0000 		lds r25,(nic_ip)+1
 164 0048 A091 0000 		lds r26,(nic_ip)+2
 165 004c B091 0000 		lds r27,(nic_ip)+3
 166 0050 8227      		eor r24,r18
 167 0052 9327      		eor r25,r19
 168 0054 A427      		eor r26,r20
 169 0056 B527      		eor r27,r21
 170 0058 8070      		andi r24,lo8(-256)
 171 005a 0097      		sbiw r24,0
 172 005c A105      		cpc r26,__zero_reg__
 173 005e B105      		cpc r27,__zero_reg__
 174 0060 01F0      		breq .L2
 175 0062 2091 0000 		lds r18,nic_gateway_ip
 176 0066 3091 0000 		lds r19,(nic_gateway_ip)+1
 177 006a 4091 0000 		lds r20,(nic_gateway_ip)+2
 178 006e 5091 0000 		lds r21,(nic_gateway_ip)+3
 179               	.L2:
 509:net/tcp.c     **** , unsigned char flags, unsigned int len){
 510:net/tcp.c     **** 	unsigned int arp_idx;
 511:net/tcp.c     **** 	unsigned int tmp;
 512:net/tcp.c     **** 	unsigned long *ip;
 513:net/tcp.c     **** 
 514:net/tcp.c     **** 	if ((tcp_sockets[socketnum].source_ip & NIC_IP_NETMASK) != (nic_ip & NIC_IP_NETMASK)){
 515:net/tcp.c     **** 		#if TCP_DEBUG
 516:net/tcp.c     **** 		softuart_puts_progmem("TCP : ip ");
 517:net/tcp.c     **** 		softuart_put_ip(tcp_sockets[socketnum].source_ip);
 518:net/tcp.c     **** 		softuart_puts_progmem(" outside home net -> use gateway\r\n");

⌨️ 快捷键说明

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