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

📄 tftp.c

📁 基于三星44B0芯片的移植操作系统
💻 C
字号:
#include "skbuff.h"
#include "eth.h"
#include "ip.h"
#include "udp.h"
#include "tftp.h"
#include "tftpput.h"
#include "utils.h"

unsigned long client_ip;
unsigned short client_port;
unsigned short client_block;

extern void Uart_Printf(char *fmt,...);
extern struct sk_buff* sk_buff_alloc(void);
extern void skb_headerinit(struct sk_buff *skb);
extern void sk_buff_free(struct sk_buff* sk_buff_unused);

int tftp_send_ack(struct tftphdr *tftp_hdr, int block)
{
	struct tftphdr *tftp_ack;
	struct sk_buff *skb;

	//skb = alloc_skb(ETH_FRAME_LEN);
	skb = (struct sk_buff*)sk_buff_alloc();
	//skb分配内存缓冲
	if(!skb)
	{
		//Uart_Printf("sk_buff空闲链表为空!\n");
		return -1;
	}
	skb_headerinit(skb);
	udp_skb_reserve(skb);
	tftp_ack = (struct tftphdr *)skb_put(skb, sizeof(struct tftphdr));
	tftp_ack->th_opcode = htons(ACK);
	tftp_ack->th_block = htons(block);

	udp_send(skb, client_ip, TFTP, client_port);
    sk_buff_free(skb);
	return 0;
}

int tftp_rcv_wrq(struct sk_buff *skb)
{
	struct tftphdr *tftp_hdr;

	client_ip = ip_get_source_ip(skb);
	client_port = udp_get_source_port(skb);

	tftp_hdr = (struct tftphdr *)skb->data;
	tftp_send_ack(tftp_hdr, 0);
	client_block = 1;

	tftp_put_begin();

	return 0;
}

int tftp_rcv_data(struct sk_buff *skb)
{
	struct tftphdr *tftp_hdr;
	int len;

	if (client_ip != ip_get_source_ip(skb))
		return -1;
	if (client_port != udp_get_source_port(skb))
		return -1;

	tftp_hdr = (struct tftphdr *)skb->data;
	if (client_block == ntohs(tftp_hdr->th_block)) {

		len = skb->len - sizeof(struct tftphdr);
//		tftp_put(tftp_hdr->th_data, len);
			//tftp_send_ack(tftp_hdr, client_block);
		tftp_put((unsigned char *)tftp_hdr+sizeof(struct tftphdr), len);

		tftp_send_ack(tftp_hdr, client_block);
		client_block++;

		if (len < 512)		
			tftp_put_end();		

	} else if (client_block > ntohs(tftp_hdr->th_block)) {

		tftp_send_ack(tftp_hdr, ntohs(tftp_hdr->th_block));

	} else {

		tftp_send_ack(tftp_hdr, client_block);
	}

	return 0;
}

int tftp_rcv_packet(struct sk_buff *skb)
{
	struct tftphdr *tftp_hdr;
	
	tftp_hdr = (struct tftphdr *)skb->data;

    Uart_Printf("tftp\n");
	switch (ntohs(tftp_hdr->th_opcode)) {

	case RRQ:
		break;
	case WRQ:
		Uart_Printf("tftp_rcv_wrq(skb);\n");
		tftp_rcv_wrq(skb);
		break;
	case DATA:
		Uart_Printf("tftp_rcv_data(skb);\n");
		tftp_rcv_data(skb);
		break;
	case ACK:
		break;
	case ERROR:
		break;
	default:
		break;
	}

	return 0;
}

⌨️ 快捷键说明

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