c语言tftp服务器编程代码
时间: 2024-10-26 21:02:12 浏览: 116
以下是一个简单的C语言TFTP服务器编程代码示例。该代码实现了基本的TFTP协议功能,包括读取和写入文件的能力。请注意,这是一个基础示例,可能需要根据具体需求进行优化和错误处理。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#define TFTP_PORT 69
#define MAX_DATA_SIZE 512
#define BLOCK_SIZE 512
#define TIMEOUT 5
enum Opcode {
OP_RRQ = 1,
OP_WRQ = 2,
OP_DATA = 3,
OP_ACK = 4,
OP_ERROR = 5
};
struct tftp_packet {
uint16_t opcode;
char data[MAX_DATA_SIZE];
};
void handle_rrq(int sockfd, struct sockaddr_in client_addr, socklen_t addr_len, char *filename, char *mode);
void handle_wrq(int sockfd, struct sockaddr_in client_addr, socklen_t addr_len, char *filename, char *mode);
int main() {
int sockfd;
struct sockaddr_in server_addr, client_addr;
socklen_t addr_len = sizeof(client_addr);
char buffer[BLOCK_SIZE];
// Create UDP socket
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("Socket creation failed");
exit(EXIT_FAILURE);
}
// Bind the socket to the port
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(TFTP_PORT);
if (bind(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("Bind failed");
close(sockfd);
exit(EXIT_FAILURE);
}
printf("TFTP server started on port %d\n", TFTP_PORT);
while (1) {
ssize_t bytes_received = recvfrom(sockfd, buffer, BLOCK_SIZE, 0, (struct sockaddr *)&client_addr, &addr_len);
if (bytes_received < 0) {
perror("Receive failed");
continue;
}
struct tftp_packet *packet = (struct tftp_packet *)buffer;
switch (ntohs(packet->opcode)) {
case OP_RRQ:
handle_rrq(sockfd, client_addr, addr_len, packet->data, packet->data + strlen(packet->data) + 1);
break;
case OP_WRQ:
handle_wrq(sockfd, client_addr, addr_len, packet->data, packet->data + strlen(packet->data) + 1);
break;
default:
printf("Unknown opcode: %d\n", ntohs(packet->opcode));
break;
}
}
close(sockfd);
return 0;
}
void handle_rrq(int sockfd, struct sockaddr_in client_addr, socklen_t addr_len, char *filename, char *mode) {
FILE *file;
char buffer[BLOCK_SIZE];
uint16_t block_num = 1;
ssize_t bytes_read;
if ((file = fopen(filename, "rb")) == NULL) {
printf("File not found: %s\n", filename);
// Send error packet
struct tftp_packet error_packet;
error_packet.opcode = htons(OP_ERROR);
error_packet.data[0] = 1; // File not found error code
strcpy(error_packet.data + 2, "File not found");
sendto(sockfd, &error_packet, 5 + strlen("File not found"), 0, (struct sockaddr *)&client_addr, addr_len);
return;
}
while ((bytes_read = fread(buffer + 2, 1, BLOCK_SIZE - 2, file)) > 0) {
struct tftp_packet data_packet;
data_packet.opcode = htons(OP_DATA);
data_packet.data[0] = (block_num >> 8) & 0xFF;
data_packet.data[1] = block_num & 0xFF;
memcpy(data_packet.data + 2, buffer + 2, bytes_read);
sendto(sockfd, &data_packet, bytes_read + 4, 0, (struct sockaddr *)&client_addr, addr_len);
// Wait for ACK
struct tftp_packet ack_packet;
ssize_t bytes_received = recvfrom(sockfd, &ack_packet, BLOCK_SIZE, 0, (struct sockaddr *)&client_addr, &addr_len);
if (bytes_received < 0 || ntohs(ack_packet.opcode) != OP_ACK || ntohs(*(uint16_t *)(ack_packet.data)) != block_num) {
printf("ACK error for block %d\n", block_num);
fseek(file, (block_num - 1) * (BLOCK_SIZE - 2), SEEK_SET);
continue;
}
block_num++;
}
fclose(file);
printf("File sent successfully: %s\n", filename);
}
void handle_wrq(int sockfd, struct sockaddr_in client_addr, socklen_t addr_len, char *filename, char *mode) {
FILE *file;
char buffer[BLOCK_SIZE];
uint16_t block_num = 0;
ssize_t bytes_received;
if ((file = fopen(filename, "wb")) == NULL) {
printf("Failed to open file for writing: %s\n", filename);
// Send error packet
struct tftp_packet error_packet;
error_packet.opcode = htons(OP_ERROR);
error_packet.data[0] = 3; // Disk full or allocation exceeded error code
strcpy(error_packet.data + 2, "Disk full or allocation exceeded");
sendto(sockfd, &error_packet, 5 + strlen("Disk full or allocation exceeded"), 0, (struct sockaddr *)&client_addr, addr_len);
return;
}
while (1) {
struct tftp_packet data_packet;
bytes_received = recvfrom(sockfd, &data_packet, BLOCK_SIZE, 0, (struct sockaddr *)&client_addr, &addr_len);
if (bytes_received < 0) {
perror("Receive failed");
continue;
}
if (ntohs(data_packet.opcode) != OP_DATA) {
printf("Unexpected packet received\n");
continue;
}
block_num = ntohs(*(uint16_t *)(data_packet.data));
if (fwrite(data_packet.data + 2, 1, bytes_received - 4, file) < bytes_received - 4) {
printf("Write error\n");
break;
}
if (bytes_received < BLOCK_SIZE) {
break;
}
// Send ACK
struct tftp_packet ack_packet;
ack_packet.opcode = htons(OP_ACK);
ack_packet.data[0] = (block_num >> 8) & 0xFF;
ack_packet.data[1] = block_num & 0xFF;
sendto(sockfd, &ack_packet, 4, 0, (struct sockaddr *)&client_addr, addr_len);
}
fclose(file);
printf("File received successfully: %s\n", filename);
}
```
### 代码说明
1. **主函数**:
- 创建一个UDP套接字并绑定到69端口。
- 进入无限循环,等待客户端请求。
- 根据收到的请求类型(RRQ或WRQ)调用相应的处理函数。
2. **handle_rrq**:
- 处理读文件请求(RRQ)。
- 打开文件,逐块读取数据并发送给客户端。
- 等待客户端的ACK确认,如果没有收到或收到错误的ACK,重新发送数据块。
3. **handle_wrq**:
- 处理写文件请求(WRQ)。
- 打开文件,逐块接收数据并写入文件。
- 发送ACK确认给客户端。
### 注意事项
- 该代码只是一个基础示例,实际应用中需要添加更多的错误处理和日志记录。
- TFTP协议有一些高级功能(如多线程、超时重传等)未在此示例中实现,可根据需要进行扩展。
- 该代码假设文件名和模式都是以NULL结尾的字符串,实际应用中需要确保这一点。
阅读全文