TFTP protocol file transfer


Download model

Upload model

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/select.h>
#include <sys/wait.h>
#include <time.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <signal.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <errno.h>

#define LOG(s) printf("[%s] {%s:%d} %s \
", __DATE__, __FILE__, __LINE__, s);

int do_download(int client, struct sockaddr_in remote)
{<!-- -->
//Group download request package
char buf[516] = {<!-- -->0};
char filename[30] = "";
puts("Please enter the file name to be downloaded >>>>");
scanf("%s", filename);
while(getchar() != 10);

//Operation code
unsigned short* cmd = (unsigned short*)buf;
*cmd = htons(1);

\t//file name
char* f_n = buf + 2;
strcpy(f_n, filename);

\t//model
char* mode = f_n + 1 + strlen(f_n);
strcpy(mode, "octet");

//Request length
int size = strlen(mode) + strlen(f_n) + 4;

//Send download request sendto
socklen_t len = sizeof(remote);
if(sendto(client, buf, size, 0, (struct sockaddr*) & amp;remote, len) == -1)
{<!-- -->
LOG("sendto error");
return -1;
}

int res = 0;
unsigned short num = 0;
int fd_w = -1;

while(1)
{<!-- -->
//accept data
bzero(buf, sizeof(buf));
if((res = recvfrom(client, buf, sizeof(buf), 0, (struct sockaddr*) & amp;remote, & amp;len)) == -1)
{<!-- -->
LOG("recvfrom error");
return -1;
}

//Data packet reception
if(3 == buf[1])
{<!-- -->
num + + ;
//Determine whether the block number is legal
if(*(unsigned short*)(buf + 2) == htons(num))
{<!-- -->
//The first data packet creates a file
if(1 == num)
{<!-- -->
if((fd_w = open("./5.png", O_WRONLY | O_CREAT | O_TRUNC, 0664)) == -1)
{<!-- -->
LOG("open error");
return -1;
}
}
if(write(fd_w, buf + 4, res-4) == -1)
{<!-- -->
LOG("write error");
return -1;
}
}

//Group ACK
*cmd = htons(4);
//Send ACK
if(sendto(client, buf, sizeof(int), 0, (struct sockaddr*) & amp;remote, len) == -1)
{<!-- -->
LOG("sendto error");
return -1;
}


//Determine whether the data is less than 512 bytes. If it is less than 512 bytes, the download is completed.
if(res - 4 < 512)
{<!-- -->
puts("Download completed");
break;
}
}
//Judge error code information
if(5 == buf[1])
{<!-- -->
fprintf(stderr, "Error code: %d, error message: %s\
", ntohs(*(unsigned short*)(buf + 2)), buf + 4);
break;
}


}


close(fd_w);

return 0;
}

int do_upload(int client, struct sockaddr_in remote)
{<!-- -->
//Group write request packet
char buf[516] = {<!-- -->0};
char filename[30] = "";
puts("Please enter the name of the file to be uploaded >>>>");
scanf("%s", filename);
while(getchar() != 10);


//Operation code
unsigned short* cmd = (unsigned short*)buf;
*cmd = htons(2);

\t//file name
char* f_n = buf + 2;
strcpy(f_n, filename);

\t//model
char* mode = f_n + 1 + strlen(f_n);
strcpy(mode, "octet");

//Request length
int size = strlen(mode) + strlen(f_n) + 4;

//Send download request sendto
socklen_t len = sizeof(remote);
if(sendto(client, buf, size, 0, (struct sockaddr*) & amp;remote, len) == -1)
{<!-- -->
LOG("sendto error");
return -1;
}

int res = 0; //Record the length of the read file
unsigned short num = 0; //Record block encoding
int fd_r = -1; //Define read file descriptor
bzero(buf, sizeof(buf));
\t//Receive data
if((res = recvfrom(client, buf, sizeof(buf), 0, (struct sockaddr*) & amp;remote, & amp;len) == -1))
{<!-- -->
LOG("recvfrom error");
return -1;
}
//Determine whether it is the 0th ACK response packet
if(buf[1] != 4 || *(unsigned short*)(buf + 2) != htons(num))
{<!-- -->
puts("ACK packet is illegal");
return -1;
}

puts("Server connection successful, uploading...");

\t//open a file
if((fd_r = open("./sly.jpg", O_RDONLY)) == -1)
{<!-- -->
LOG("open error");
return -1;
}


while(1)
{<!-- -->
//Determine whether the ACK response sequence is legal
if(buf[1] != 4 || *(unsigned short*)(buf + 2) != htons(num))
{<!-- -->
puts("ACK packet is illegal");
return -1;
}
//The data packet block number is incremented
num + + ;
bzero(buf, sizeof(buf));
//read file
if((res = read(fd_r, buf + 4, 512)) == -1)
{<!-- -->
LOG("read error");
return -1;
}
//Assign values to the operands and block encoding of the data packet
*cmd = htons(3);
*(unsigned short*)(buf + 2) = htons(num);

//Calculate packet length
size = res + 4;

//Send packet
if(sendto(client, buf, size, 0, (struct sockaddr*) & amp;remote, len) == -1)
{<!-- -->
LOG("sendto error");
return -1;
}

//Receive ACK response packet
bzero(buf, sizeof(buf));
if(recvfrom(client, buf, sizeof(buf), 0, (struct sockaddr*) & amp;remote, & amp;len) == -1)
{<!-- -->
LOG("recvfrom error");
return -1;
}


//end of file flag
if(res < 512)
{<!-- -->
puts("Upload successful");
break;
}

//Judge error code information
if(5 == buf[1])
{<!-- -->
fprintf(stderr, "Error code: %d, error message: %s\
", ntohs(*(unsigned short*)(buf + 2)), buf + 4);
break;
}
}
close(fd_r);

return 0;
}

int main(int argc, char *argv[])
{<!-- -->

//Create socket
int client = 0;
if((client = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
{<!-- -->
LOG("socket error");
return -1;
}

//Assignment of address information structure
struct sockaddr_in remote = {<!-- -->0};

remote.sin_family = AF_INET;
remote.sin_port = htons(69);
remote.sin_addr.s_addr = inet_addr("192.168.114.227");

char choose = 0;
while(1)
{<!-- -->
system("clear");
printf("-----------------------\
");
printf("---------1. Download-------\
");
printf("---------2. Upload-------\
");
printf("---------3. Exit-------\
");
printf("-----------------------\
");
printf("Please enter >>>>");
scanf("%c", & amp;choose);
while(getchar() != 10);

switch(choose)
{<!-- -->
case '1':
do_download(client, remote);
break;

case '2':
do_upload(client, remote);
break;

case '3':
goto END;
break;

default:
puts("Incorrect input, please re-enter");
break;
}

puts("Enter any character to clear the screen>>>");
while(getchar() != 10);

}




END:
//Close file descriptor
close(client);

return 0;
}