-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFTPclient.cpp
More file actions
206 lines (167 loc) · 4.96 KB
/
FTPclient.cpp
File metadata and controls
206 lines (167 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
* FTPclient.cpp
*
* Created on: Mar 4, 2016
* Author: jeremy
*/
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <istream>
#include <fstream>
#include <stdlib.h>
#include <iostream>
#include <time.h>
#include <strings.h>
#include <sstream>
#include "packet.h"
//This is the line that displays if there are not 2 parameters input.
#define USAGE "Usage:\r\nc [tux machine number]"
#define PORT 10038
#define PAKSIZE 256
#define BUFSIZE 249
#define ACK 0
#define NAK 1
#define FILENAME "Some.txt"
using namespace std;
bool seqNum;
bool isvpack(unsigned char * p) {
cout << endl << "=== IS VALID PACKET TESTING" << endl;
char * sns = new char[2];
memcpy(sns, &p[0], 1);
sns[1] = '\0';
char * css = new char[6];
memcpy(css, &p[1], 6);
css[5] = '\0';
char * db = new char[249 + 1];
memcpy(db, &p[7], 249);
db[249] = '\0';
cout << "Seq. num: " << sns << endl;
cout << "Checksum: " << css << endl;
cout << "Message: " << db << endl;
int sn = atoi(sns);
int cs = atoi(css);
Packet pk;
createPacket(0, db, &pk);
setSeqNum(sn, &pk);
if(sn != seqNum) return false;
if(cs != generateCkSum(pk)) return false;
return true;
}
int main(int argc, char** argv) {
struct sockaddr_in server;
struct sockaddr_in client;
socklen_t calen = sizeof(server);
int rlen;
int s;
bool ack;
seqNum = true;
//If there correct amount of arguement are not input, the method will end.
if(argc != 2) {
cout << USAGE << endl;
return 1;
}
//Uses the param (tux number) to append to the known ip of Auburn machine.
string hs = string("131.204.14.") + argv[1];
/* Create our socket. */
if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
cout << "Socket creation failed. (socket s)" << endl;
return 0;
}
/*
* Bind our socket to an IP (whatever the computer decides)
* and a specified port.
*
*/
memset((char *)&client, 0, sizeof(client));
client.sin_family = AF_INET;
client.sin_addr.s_addr = htonl(INADDR_ANY);
client.sin_port = htons(PORT);
if (bind(s, (struct sockaddr *)&client, sizeof(client)) < 0) {
cout << "Socket binding failed. (socket s, address a)" << endl;
return 0;
}
//Initialize the client information. the sin_addr is set from the hs string up to along with the
//tux machine number input in the first parameter.
memset((char *)&server, 0, sizeof(server));
server.sin_family = AF_INET;
server.sin_port = htons(PORT);
inet_pton(AF_INET, hs.c_str(), &(server.sin_addr));
cout << endl;
//Prints the address of the machine it wants to send it to.
cout << "Server address (inet mode): " << inet_ntoa(server.sin_addr) << endl;
cout << "Port: " << ntohs(server.sin_port) << endl;
cout << endl << endl;
ofstream file("Dumpfile2.txt");
char filename[] = FILENAME;
cout << endl << "Filename sent to Server" << filename << endl;
if(sendto(s, filename, strlen(FILENAME), 0, (struct sockaddr *)&server, sizeof(server)) < 0) {
cout << "Package sending failed. (socket s, client address client, message m)" << endl;
return 0;
}
bool isSeqNumSet = false;
for(;;) {
unsigned char packet[PAKSIZE + 1];
unsigned char dataPull[PAKSIZE - 7 + 1];
//Wait for a message from the client to see if the packet was received correctly
rlen = recvfrom(s, packet, PAKSIZE, 0, (struct sockaddr *)&server, &calen);
packet[rlen] = '\0';
if(!isSeqNumSet) {
isSeqNumSet = true;
char * str = new char[1];
memcpy(str, &packet[0], 1);
seqNum = atoi(str);
}
for(int x = 0; x < PAKSIZE - 7; x++) {
dataPull[x] = packet[x + 7];
}
dataPull[PAKSIZE - 7] = '\0';
packet[PAKSIZE] = '\0';
if (rlen > 0) {
char * css = new char[6];
memcpy(css, &packet[1], 6);
css[5] = '\0';
cout << endl << endl << "=== RECEIPT" << endl;
cout << "Seq. num: " << packet[0] << endl;
cout << "Checksum: " << css << endl;
cout << "Received message: " << dataPull << endl;
cout << "Sent response: ";
if(isvpack(packet)) {
ack = ACK;
if (seqNum) {
seqNum = false;
}
else {
seqNum = true;
}
file << dataPull;
file.flush();
} else {
ack = NAK;
}
if (ack == ACK) {
cout << "ACK" << endl;
}
else {
cout << "NAK" << endl;
}
Packet p;
if (seqNum) {
createPacket(false, reinterpret_cast<const char *>(dataPull), &p);
} else {
createPacket(true, reinterpret_cast<const char *>(dataPull), &p);
}
setCkSum(atoi(css), &p);
setAck(ack, &p);
file.flush();
if(sendto(s, str(p), PAKSIZE, 0, (struct sockaddr *)&server, calen) < 0) {
cout << "Acknowledgement failed. (socket s, acknowledgement message ack, client address ca, client address length calen)" << endl;
return 0;
}
delete css;
file.flush();
}
}
file.close();
}