-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathftp_client.c
More file actions
executable file
·168 lines (151 loc) · 4.96 KB
/
ftp_client.c
File metadata and controls
executable file
·168 lines (151 loc) · 4.96 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* ftpc.c to transfer a file using TCP stream sockets */
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <string.h>
#include <unistd.h>
//Socket related header files
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
// For boolean
#include <stdbool.h>
// For errors
#include <errno.h>
// For isdigit
#include <ctype.h>
// Header files
#include "lib_tcp.h"
#define BUFFER_SIZE 1000
#define CLIENTPORT 54321
bool is_port_number(char number[], int *port)
{
int i = 0;
// Checking for negative numbers
if (number[0] == '-') {
printf("Please choose a port number in the range 1024 - 65535\n");
return false;
}
// Check if it is a valid number
for (; number[i] != 0; i++) {
if (!isdigit(number[i])) {
printf
("Incorrect format for port number. Please choose a number in the range 1024 - 65535\n");
return false;
}
}
// Check if it is in valid range
*port = atoi(number);
if (*port < 1024 || *port > 65535) {
printf("Please choose a port number in the range 1024 - 65535\n");
return false;
}
return true;
}
/* ftp client program called with <remote-IP> <remote-port> <local-file-to-transfer> to transfer a file to the server */
int main(int argc, char *argv[])
{
int sock; /* initial socket descriptor */
struct sockaddr_in sin_addr; /* structure for socket name setup */
struct sockaddr_in sin_addr_client; /* structure for socket name setup */
char *buffer = NULL;
char *file_name;
struct hostent *hp;
FILE *fp;
int total_bytes, size, no_bytes, test;
if (argc != 4) {
printf("\n number of arguments are incorrect \n");
printf
("\n Please start ftpc in this way:\n \t ftpc <remote-IP> <remote-port> <local-file-to-transfer> \n");
return -1;
}
/* initialize socket connection in unix domain */
if ((sock = SOCKET(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("error opening socket");
exit(1);
}
// Getting host info. & checking if the IP is valid
hp = gethostbyname(argv[1]);
if (hp == NULL) {
fprintf(stderr, "%s: Invalid IP/host name \n", argv[1]);
exit(2);
}
// Check if the port number is valid
int port;
if (!is_port_number(argv[2], &port))
exit(0);
/* Define client socket */
sin_addr_client.sin_family = AF_INET;
sin_addr_client.sin_addr.s_addr = INADDR_ANY;
sin_addr_client.sin_port = htons(CLIENTPORT);
/* bind socket name to socket */
if (BIND
(sock, (struct sockaddr *) &sin_addr_client,
sizeof(struct sockaddr_in)) < 0) {
perror("error binding stream socket");
exit(1);
}
/* construct name of socket to send to */
bcopy((void *) hp->h_addr, (void *) &sin_addr.sin_addr, hp->h_length);
sin_addr.sin_family = AF_INET;
sin_addr.sin_port = htons(port); /* fixed by adding htons() */
memset(sin_addr.sin_zero, '\0', sizeof(sin_addr.sin_zero));
/* establish connection with server */
if (CONNECT(sock, (struct sockaddr *) &sin_addr,
sizeof(struct sockaddr_in)) < 0) {
close(sock);
perror("error connecting stream socket");
exit(1);
}
file_name = argv[3];
fp = fopen(file_name, "r"); // opening input-file specified by argv[3]
if (fp == NULL) {
printf
("\n input-filename %s doesn't exist. Please enter a valid filename\n",
argv[1]);
printf
("\n Please invoke the program in this way:\n \t count <input-filename> <search-string> <output-filename> \n");
return -1;
}
// Finding the size of the file
fseek(fp, 0, SEEK_END); // setting fp to the end of file
total_bytes = (ftell(fp));
printf("Total File Size : %d", total_bytes);
size = htonl(total_bytes);
fseek(fp, 0, SEEK_SET); // setting fp to the start of file
// Allocating the buffer
buffer = malloc(sizeof(unsigned char) * (BUFFER_SIZE)); //allocating buffer of size 1000 bytes to read the file
printf("Starting the transfer of the file %s\n", file_name);
do {
memset(buffer, '\0', BUFFER_SIZE);
if (ftell(fp) != 0) // For all iterations of the while-loop except the first one
{
no_bytes = fread(buffer, 1, (BUFFER_SIZE), fp); // copy 1000 bytes from the position pointed by fp to the buffer
printf("Reading the next no of btyes : %d %d\n", no_bytes, total_bytes);
total_bytes -= no_bytes;
} else // For the 1st iteration of the loop
{
memcpy(buffer, &size, 4);
memcpy(buffer + 4, file_name, strlen(file_name));
// copy 1000-24 bytes from the position pointed by fp to buffer
test = (fread(buffer + 24, 1, (BUFFER_SIZE - 24), fp));
no_bytes = test + 24;
printf("Reading the first time :%d %d %d\n", test, no_bytes, total_bytes);
total_bytes -= test;
}
// write buffer to sock
if ((SEND(sock, buffer, no_bytes, 0)) < 0) {
perror("error writing on stream socket");
exit(1);
}
//usleep(1000);
//sleep(1);
} while (total_bytes > 0);
printf("Transfer complete \n");
free(buffer);
fclose(fp);
close(sock);
return 0;
}