使用C语言实现tftp协议
时间: 2023-07-20 21:08:01 浏览: 179
tftp server 的C语言实现
4星 · 用户满意度95%
TFTP(Trivial File Transfer Protocol)是一种简单的文件传输协议,常用于将文件从服务器传输到客户端。下面是一个使用C语言实现TFTP协议的例子:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#define TFTP_OPCODE_RRQ 1
#define TFTP_OPCODE_DATA 3
#define TFTP_OPCODE_ACK 4
#define TFTP_OPCODE_ERROR 5
#define TFTP_ERROR_UNDEFINED 0
#define TFTP_ERROR_FILE_NOT_FOUND 1
#define TFTP_ERROR_ACCESS_VIOLATION 2
#define TFTP_ERROR_DISK_FULL 3
#define TFTP_ERROR_ILLEGAL_OPERATION 4
#define TFTP_ERROR_UNKNOWN_TID 5
#define TFTP_ERROR_FILE_EXISTS 6
#define TFTP_ERROR_NO_SUCH_USER 7
#define TFTP_BLOCK_SIZE 512
struct tftp_packet {
unsigned short opcode;
union {
struct {
char filename[1];
char mode[1];
} rrq;
struct {
unsigned short block_num;
char data[TFTP_BLOCK_SIZE];
} data;
struct {
unsigned short block_num;
} ack;
struct {
unsigned short error_code;
char error_msg[1];
} error;
} data;
};
int main(int argc, char *argv[]) {
if (argc != 4) {
fprintf(stderr, "Usage: %s <ip> <port> <file>\n", argv[0]);
exit(EXIT_FAILURE);
}
int sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
if (sock_fd < 0) {
perror("socket");
exit(EXIT_FAILURE);
}
struct sockaddr_in server_addr;
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr(argv[1]);
server_addr.sin_port = htons(atoi(argv[2]));
char *filename = argv[3];
char mode[] = "octet";
int filename_len = strlen(filename);
int mode_len = strlen(mode);
int packet_len = 2 + filename_len + 1 + mode_len + 1;
char *packet_buf = malloc(packet_len);
if (!packet_buf) {
perror("malloc");
exit(EXIT_FAILURE);
}
struct tftp_packet *packet = (struct tftp_packet *)packet_buf;
packet->opcode = htons(TFTP_OPCODE_RRQ);
memcpy(packet->data.rrq.filename, filename, filename_len);
memcpy(packet->data.rrq.filename + filename_len, "\0", 1);
memcpy(packet->data.rrq.mode, mode, mode_len);
memcpy(packet->data.rrq.mode + mode_len, "\0", 1);
int ret = sendto(sock_fd, packet_buf, packet_len, 0, (struct sockaddr *)&server_addr, sizeof(server_addr));
if (ret < 0) {
perror("sendto");
exit(EXIT_FAILURE);
}
int block_num = 1;
FILE *file = fopen(filename, "wb");
if (!file) {
perror("fopen");
exit(EXIT_FAILURE);
}
while (1) {
char recv_buf[TFTP_BLOCK_SIZE + 4];
struct sockaddr_in client_addr;
socklen_t client_len = sizeof(client_addr);
ret = recvfrom(sock_fd, recv_buf, sizeof(recv_buf), 0, (struct sockaddr *)&client_addr, &client_len);
if (ret < 0) {
perror("recvfrom");
exit(EXIT_FAILURE);
}
struct tftp_packet *recv_packet = (struct tftp_packet *)recv_buf;
unsigned short opcode = ntohs(recv_packet->opcode);
if (opcode == TFTP_OPCODE_DATA) {
unsigned short recv_block_num = ntohs(recv_packet->data.data.block_num);
if (recv_block_num == block_num) {
int data_len = ret - 4;
fwrite(recv_packet->data.data.data, 1, data_len, file);
block_num++;
char ack_buf[4];
struct tftp_packet *ack_packet = (struct tftp_packet *)ack_buf;
ack_packet->opcode = htons(TFTP_OPCODE_ACK);
ack_packet->data.ack.block_num = htons(recv_block_num);
ret = sendto(sock_fd, ack_buf, sizeof(ack_buf), 0, (struct sockaddr *)&client_addr, client_len);
if (ret < 0) {
perror("sendto");
exit(EXIT_FAILURE);
}
if (data_len < TFTP_BLOCK_SIZE) {
break;
}
}
} else if (opcode == TFTP_OPCODE_ERROR) {
unsigned short error_code = ntohs(recv_packet->data.error.error_code);
fprintf(stderr, "TFTP error: %d\n", error_code);
exit(EXIT_FAILURE);
} else {
fprintf(stderr, "Unexpected TFTP opcode: %d\n", opcode);
exit(EXIT_FAILURE);
}
}
fclose(file);
close(sock_fd);
free(packet_buf);
return 0;
}
```
这个例子实现了TFTP的读取请求(RRQ)和数据(DATA)包的处理,以及确认(ACK)包的发送。它会将服务器上的文件传输到本地,并保存为和服务器上的文件名相同的文件。
阅读全文