-
Notifications
You must be signed in to change notification settings - Fork 0
/
ftpc.c
101 lines (86 loc) · 2.77 KB
/
ftpc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//lab2
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <sys/unistd.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include "srbaclib.h"
void sendFile(int * sockfd, FILE * transFile, uint32_t fsize, char * filename)
{
fprintf(stderr, "size of file is %d\n", fsize);
void * buf = (void *) malloc(1000);
bzero(buf,1000);
uint32_t netfsize = htonl(fsize);
memcpy(buf, &netfsize,sizeof(uint32_t));
memcpy(buf+4,filename,strlen(filename));
SEND(*sockfd,buf,24,0);
//sendto(*sockfd, buf, 24,0,&dest_addr,sizeof(struct sockaddr_in));
int read = 0;
while(feof(transFile) == 0)
{
read = fread(buf,1,1000,transFile);
int sent = SEND(*sockfd,buf,read,0);
usleep(10 * 1000);
//int sent= sendto(*sockfd, buf, read,0,(struct sockaddr *)&dest_addr,sizeof(struct sockaddr_in));
if(sent != read)
{
fprintf(stdout, "sent %d Reason %s\n\n", sent, strerror(errno));
fprintf(stderr,"%s\n","failed to send correctly");
exit(0);
}
}
free(buf);
return;
}
unsigned long fileSize(const char *filePath)
{
struct stat fileStat;//declare file statistics struct
stat(filePath, &fileStat);//filling fileStat with statistics of file
return (uint32_t)fileStat.st_size;//returning only the size of file from stat stru ct
}
void createConnection(int * sockfd, struct sockaddr_in * sockaddr, char *port, char * serverIp)
{
*sockfd = SOCKET(AF_INET, SOCK_DGRAM,IPPROTO_UDP);
if(*sockfd < 0)
{
fprintf(stdout, "%s\n", "socket could not be made");
exit(0);
}
sockaddr->sin_family = AF_INET;
sockaddr->sin_port = htons(atoi(port));
sockaddr->sin_addr.s_addr = inet_addr(serverIp);
memset(&(sockaddr->sin_zero),'\0',8);
/*if(BIND(*sockfd,(struct sockaddr *)sockaddr,sizeof(struct sockaddr_in)) < 0)
{
fprintf(stderr, "%s %s\n", "failed to bind, failed because", strerror(errno));
close(*sockfd);
exit(0);
}*/
dest_addr = *((struct sockaddr *)sockaddr);
return;
}
int main(int args, char * argv[])
{
if (args != 4)
{
fprintf(stderr,"%s %d %s\n" , "please give three aurguments,",args - 1,"were entered, the aurguments are remote-ip, remote-port, local-file-to-transfer");
}
FILE* transFile = fopen(argv[3],"r");//trying to open local-file-to-transfer in read mode
if(transFile == NULL)//Checking if input file was opened correctly
{
fprintf(stdout,"%s","please enter a valid path to an existing file for the third argument\n");
exit(0);//if not opened correctly, exit program
}
uint32_t fsize = fileSize(argv[3]);
int sockfd;
struct sockaddr_in sockaddr;
createConnection(&sockfd, &sockaddr, argv[2], argv[1]);
sendFile(&sockfd,transFile,fsize,argv[3]);
fprintf(stdout, "%s %lu %d \n", "ended normally", sizeof(uint32_t), fsize);
}