net_tftpc.c
来自「MIPS YAMON, a famous monitor inc. source」· C语言 代码 · 共 2,096 行 · 第 1/5 页
C
2,096 行
rcode = OK ; } else { rcode = ERROR_NET_TFTP_FILE_TOO_BIG ;error_out: /* New state: 'ERROR' */ sap_context.sap_state = TFTPC_SAP_STATE_ERROR ; sap_context.error_cause = rcode; } return( rcode ) ;}/************************************************************************ * * NET_TFTPC_receive_data_byte * Description : * ------------- * This function is registered in the UDP-module and linked with a * unique UDP-'port', idnetifying our SAP. This allows the UDP to call * us back with an reference to the received frame, * containing a TFTP-frame. * * * * Parameters : * ------------ * 'src_ip_adr': senders IP-address in BE-format * 'src_port': senders UDP-port in BE-format * 'src_mac_adr': senders MAC-address * 'udp_sp_hd': UDP defined handle, identifying our SAP (i.e. port) * 'length': length of received ethernet frame * 'data': pointer for received ethernet frame (in driver's space) * * * Return values : * --------------- * 'ERROR_NET_TFTPC_SAP_ERROR_STATE' * 'ERROR_NET_TFTPC_INVALID_TID' * 'ERROR_NET_TFTP_FILE_TOO_BIG' * 'ERROR_NET_TFTPC_INVALID_BLOCK_NUMBER' * 'ERROR_NET_TFTPC_CONGESTION' * 'OK' * ************************************************************************/staticUINT32 NET_TFTPC_receive_data_byte( UINT32 src_ip_adr, /* IP BE-format */ UINT16 src_port, /* port BE-format */ t_mac_addr *src_mac_adr,/* MAC */ UINT32 udp_sp_hd, /* handle of open */ UINT32 length, /* total length */ UINT8 *data ) /* start of frame */{ UINT32 rcode = OK ; UINT8 *p ; UINT16 opcode ; UINT16 tmp2 ; UINT32 tftp_data_length ; int index ;#ifdef NET_DEBUG int i ; printf("NET_TFTPC_receive_data_byte\n") ; printf("src_ip_adr = %x\n", (unsigned)BE32_TO_CPU(src_ip_adr) ) ; printf("src_port = %d\n", BE16_TO_CPU(src_port) ) ; printf("length = %d\n", length ) ; printf("src MAC = " ) ; p = (UINT8*) src_mac_adr ; for (i=0; i<6; i++) { printf(" %02x",*p++ ) ; } printf("\n" ) ;#endif /* We already registered an ERROR */ if (sap_context.sap_state == TFTPC_SAP_STATE_ERROR) { /* do nothing */ return(ERROR_NET_TFTPC_SAP_ERROR_STATE) ; } /* get TFTP-'opcode'-field of this received frame */ p = (data + TFTP_HEADER_BASE + TFTP_HEADER_OPCODE) ; get2( p, opcode) ; opcode = BE16_TO_CPU( opcode ) ; get2( p, tmp2) ; /* NOTE: 'tmp2' now keeps block-number or host-error */ tmp2 = BE16_TO_CPU( tmp2 ) ; if (opcode != TFTP_HEADER_OPCODE_DATA) { /* We expect nothing else than 'DATA' or 'ERROR' */ /* check for HOST error */ if (opcode == TFTP_HEADER_OPCODE_ERROR) { if (sap_context.sap_state == TFTPC_SAP_STATE_WAIT_FIRST_BLOCK) { /* save HOST error */ sap_context.host_error = tmp2 ; data[length] = 0 ; strcpy( sap_context.host_error_msg, &data[TFTP_HEADER_BASE + TFTP_HEADER_ERRORMSG] ) ; sprintf( net_diag_msg, "Host returned: ErrorCode = %d, ErrorMsg = %s", sap_context.host_error, sap_context.host_error_msg ); sap_context.error_cause = ERROR_NET_TFTP_READ_ERROR ; } if ( (sap_context.sap_state == TFTPC_SAP_STATE_WAIT_NEXT_BLOCK) || (sap_context.sap_state == TFTPC_SAP_STATE_CONGESTION) || (sap_context.sap_state == TFTPC_SAP_STATE_LAST_BLOCK_READ) ) { /* validate 'src_port' */ if (sap_context.dst_port != src_port) { /* Ignore packet, and reply 'unknown TID' */ rcode = NET_TFTPC_unknownTID( src_ip_adr, src_port, sap_context.udp_sp_hd ) ; /* do nothing */ return( ERROR_NET_TFTPC_INVALID_TID ) ; } /* save HOST error */ sap_context.host_error = tmp2 ; data[length] = 0 ; strcpy( sap_context.host_error_msg, &data[TFTP_HEADER_BASE + TFTP_HEADER_ERRORMSG] ) ; sprintf( net_diag_msg, "Host returned: ErrorCode = %d, ErrorMsg = %s", sap_context.host_error, sap_context.host_error_msg ); sap_context.error_cause = ERROR_NET_TFTP_DATA_ERROR ; } /* New state: 'ERROR' */ sap_context.sap_state = TFTPC_SAP_STATE_ERROR ; } /* return, despite if it is not DATA */ return( sap_context.error_cause ) ; } /* We have 'DATA' */ /* Check for 1. Block data and save 'src_port', and src-MAC */ if (sap_context.sap_state == TFTPC_SAP_STATE_WAIT_FIRST_BLOCK) { sap_context.dst_port = src_port ; memcpy( &sap_context.dst_mac_adr, src_mac_adr, SYS_MAC_ADDR_SIZE ) ; } else { /* validate 'src_port' */ if (sap_context.dst_port != src_port) { /* Ignore packet, and reply 'unknown TID' */ rcode = NET_TFTPC_unknownTID( src_ip_adr, src_port, sap_context.udp_sp_hd ) ; /* do nothing */ return( ERROR_NET_TFTPC_INVALID_TID ) ; } } /* 'TID' is valid */ /* Validate block number */ if (tmp2 != (sap_context.block_number + 1) ) { if (tmp2 == TFTP_HEADER_MAX_BLOCKNUMBER) { /* New state: 'ERROR' */ sap_context.sap_state = TFTPC_SAP_STATE_ERROR ; sap_context.error_cause = ERROR_NET_TFTP_FILE_TOO_BIG ; return( ERROR_NET_TFTP_FILE_TOO_BIG ) ; } /* To avoid the "Sorcerer's Apprentice: do nothing and let timer retransmit if host doesn't */ return( ERROR_NET_TFTPC_INVALID_BLOCK_NUMBER ) ;#ifdef NET_ENABLE_TFTPC_INVALID_BLOCK_NUMBER if (tmp2 == sap_context.block_number) { /* This is the case, when an 'ACK' has got lost */ /* Send 'ACK' */ rcode = NET_TFTPC_sendACK( &sap_context ) ; return( rcode ) ; } else { /* New state: 'ERROR' */ sap_context.sap_state = TFTPC_SAP_STATE_ERROR ; sap_context.error_cause = ERROR_NET_TFTPC_INVALID_BLOCK_NUMBER ; return( ERROR_NET_TFTPC_INVALID_BLOCK_NUMBER ) ; }#endif } /* Try to save data into our buffer */ tftp_data_length = length - (MAC_HEADER_SIZE + IP_HEADER_SIZE + UDP_HEADER_SIZE + TFTP_HEADER_DATA) ; /* No data, re-evaluate buffer status */ index = sap_context.write_idx ; if ( sap_context.buf[ index ].ctrl == BUFFER_STATE_EMPTY ) { /* we seem to have an empty buffer */ /* OK, update block number */ sap_context.block_number = tmp2 ; /* save data */ memcpy( sap_context.buf[ index ].blink, &data[ TFTP_HEADER_BASE + TFTP_HEADER_DATA ], tftp_data_length ) ; /* update context */ sap_context.buf[ index ].count = tftp_data_length ; sap_context.buf[ index ].ctrl = BUFFER_STATE_FULL ; /* proceed to next buffer */ sap_context.write_idx++ ; if (sap_context.write_idx == TFTPC_SAP_BUFFER_COUNT) { sap_context.write_idx = 0 ; } index = sap_context.write_idx ; if (tftp_data_length < TFTP_HEADER_DATA_MAX_SIZE) { /* Set state 'LAST_BLOCK_READ' */ sap_context.sap_state = TFTPC_SAP_STATE_LAST_BLOCK_READ ; } else { if ( sap_context.buf[ index ].ctrl == BUFFER_STATE_EMPTY ) { /* We have space for the next block, send ACK now */ rcode = NET_TFTPC_sendACK( &sap_context ) ; /* Set timer and retry count */ /* get time now (unit is seconds since 1.1.1970) */ NET_gettime( &sap_context.timeout ) ; sap_context.timeout = sap_context.timeout + TFTPC_SAP_TIMEOUT ; sap_context.retry_left = TFTPC_SAP_MAX_RETRY_COUNT ; /* Set state 'WAIT_NEXT_BLOCK' */ sap_context.sap_state = TFTPC_SAP_STATE_WAIT_NEXT_BLOCK ; } else { /* We need to delay our ACK */ /* Set state 'CONGESTION' */ sap_context.sap_state = TFTPC_SAP_STATE_CONGESTION ; } } rcode = OK ; } else { /* Set state 'CONGESTION' */ sap_context.sap_state = TFTPC_SAP_STATE_CONGESTION ; rcode = ERROR_NET_TFTPC_CONGESTION ; } return( rcode ) ;}/************************************************************************ * * NET_TFTPC_init_sap * Description : * ------------- * Initialize the TFTPC SAP context. * * * Parameters : * ------------ * - * * * Return values : * --------------- * 'OK'(=0), successfull initialization * ************************************************************************/staticUINT32 NET_TFTPC_init_sap( t_tftpc_sap_context *context ){ UINT32 rcode ; /* initialize SAP */ /* bind this SAP to UDP and let UDP allocate a unique port */ rcode = NET_UDP_open( UDP_SAP_UNDEFINED, NET_TFTPC_receive_data_byte, &(context->udp_sp_hd) ) ; if (rcode == OK) { /* New state 'BOUND_TO_UDP' */ context->sap_state = TFTPC_SAP_STATE_BOUND_TO_UDP ; /* Reset context */ rcode = NET_TFTPC_reset_sap( context, NET_TFTPC_receive_data_byte ) ; } else { /* New state 'IDLE' */ context->sap_state = TFTPC_SAP_STATE_IDLE ; } return( rcode ) ;}/************************************************************************ * * NET_TFTPC_reset_sap * Description : * ------------- * Reset the TFTPC SAP context. * * * Parameters : * ------------ * - * * * Return values : * --------------- * 'OK'(=0), successfull initialization * ************************************************************************/staticUINT32 NET_TFTPC_reset_sap( t_tftpc_sap_context *context, t_udp_usr_receive rec ){ UINT32 rcode = OK ; int i ; /* Reset SAP */ if (context->sap_state != TFTPC_SAP_STATE_IDLE) { /* re-allocate a new UDP SAP */ IF_ERROR( (rcode), (NET_UDP_close( context->udp_sp_hd ) ) ) IF_ERROR( (rcode), (NET_UDP_open( UDP_SAP_UNDEFINED, rec, &(context->udp_sp_hd) ) ) ) /* clean up context */ context->dst_ip_adr = IP_ADDR_UNDEFINED ; context->dst_port = CPU_TO_BE16( UDP_SAP_TFTP ) ; memcpy( context->dst_mac_adr, &mac_undefined_adr, SYS_MAC_ADDR_SIZE) ; context->block_number = 0 ; context->timeout = 0 ; context->retry_left = 0 ; context->error_cause = OK ; context->send_rc = OK ; context->host_error = 0 ; context->host_error_msg = context->data ; context->filename = context->data ; context->pread = NULL ; context->puser = NULL ; context->user_size = 0 ; context->bytes_left = 0 ; context->read_idx = 0 ; context->write_idx = 0 ; context->sap_state = TFTPC_SAP_STATE_BOUND_TO_UDP ; } for (i=0; i<TFTPC_SAP_BUFFER_COUNT; i++) { context->buf[i].ctrl = BUFFER_STATE_EMPTY ; context->buf[i].count = 0 ; } return( rcode ) ;}/************************************************************************ * * NET_TFTPC_sendDATA * Description : * ------------- * Send 'DATA' of this SAP context. * * * Parameters : * ------------ * - * * * Retu
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?