-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.cpp
268 lines (225 loc) · 6.99 KB
/
server.cpp
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#include <iostream>
#include <string>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <netdb.h>
#include <sys/uio.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <fstream>
#define TMP_LENGTH 1024
using namespace std;
void receiveFromClient();
int newSd;
bool heloAccepted, fromAccepted, toAccepted, dataAccepted, quit;
char* fname;
int flag =0;
int serverSd;
void writeToFile(char content[],char* target);
void closeConnection();
char* extractName(const char* msg);
void sendStatusCode(string code) {
char buf[TMP_LENGTH];
bzero(buf,TMP_LENGTH);
strcpy(buf, code.c_str());
strcat(buf,"\n");
send(newSd, (char*)&buf, strlen(buf), 0);
}
bool doesFileExist(const char* name) {
//strcat(name, ".txt");
if(freopen(name,"r",stdin))
return true;
else
return false;
}
void checkClientMessage(char msg[]) {
char buf[TMP_LENGTH];
if(strstr(msg, "HELO")) {
heloAccepted = true;
sendStatusCode("220 Service ready");
} else if(strstr(msg, "MAIL FROM")) {
if(!heloAccepted) {
sendStatusCode("503 Bad sequence of commands");
return;
}
fromAccepted = true;
sendStatusCode("250 Requested mail action okay completed");
string duplicate = buf;
cout<<duplicate<<"\n";
checkClientMessage(buf);
fname = extractName(duplicate.c_str());
} else if(strstr(msg, "RCPT TO")) {
if(!fromAccepted) {
sendStatusCode("503 Bad sequence of commands");
return;
}
strtok(msg, ":");
char* targetUserName = strtok(NULL, "@");
strcat(targetUserName, ".txt");
if(doesFileExist(targetUserName)) {
toAccepted = true;
sendStatusCode("250 Requested mail action okay, completed");
} else {
//mailbox unavailable
toAccepted = false;
sendStatusCode("450 Requested mail action not taken: mailbox unavailable");
}
} else if(strstr(msg, "DATA")) {
if(!toAccepted) {
sendStatusCode("503 Bad sequence of commands");
return;
}
sendStatusCode("354 Start mail input; end with <CRLF>.<CRLF>");
//header
bzero(buf,TMP_LENGTH);
recv(newSd, (char*)&buf, sizeof(buf), 0);
writeToFile(buf, fname);
cout<<buf <<"\n";
//msg-body
bzero(buf,TMP_LENGTH);
recv(newSd, (char*)&buf, sizeof(buf), 0);
cout<<buf <<"\n";
writeToFile(buf, fname);
sendStatusCode("250 Requested mail action okay, completed");
bzero(buf,TMP_LENGTH);
recv(newSd, (char*)&buf, sizeof(buf), 0);
writeToFile(buf, fname);
cout<<buf <<"\n";
} else if(strstr(msg, "QUIT")) {
sendStatusCode("221 Service closing transmission channel");
closeConnection();
} else {
sendStatusCode("500 Command not implemented");
}
}
//Server side
int main(int argc, char *argv[]) {
//for the server, we only need to specify a port number
if(argc != 2) {
cerr << "Usage: port" << endl;
exit(0);
}
//grab the port number
int port = atoi(argv[1]);
//buffer to send and receive messages with
char msg[1500];
//setup a socket and connection tools
sockaddr_in servAddr;
bzero((char*)&servAddr, sizeof(servAddr));
servAddr.sin_family = AF_INET;
servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
servAddr.sin_port = htons(port);
//open stream oriented socket with internet address
//also keep track of the socket descriptor
int serverSd = socket(AF_INET, SOCK_STREAM, 0);
if(serverSd < 0) {
cerr << "Error establishing the server socket" << endl;
exit(0);
}
//bind the socket to its local address
int bindStatus = bind(serverSd, (struct sockaddr*) &servAddr,
sizeof(servAddr));
if(bindStatus < 0) {
cerr << "Error binding socket to local address" << endl;
exit(0);
}
cout << "Server ready..." << endl;
listening:
listen(serverSd, 5);
//receive a request from client using accept
//we need a new address to connect with the client
sockaddr_in newSockAddr;
socklen_t newSockAddrSize = sizeof(newSockAddr);
//accept, create a new socket descriptor to
//handle the new connection with client
newSd = accept(serverSd, (sockaddr *)&newSockAddr, &newSockAddrSize);
if(newSd < 0) {
cerr << "Error accepting request from client!" << endl;
exit(1);
}
cout << "Connected with client" << endl;
receiveFromClient();
if(flag == 3370) {
goto listening;
} else
close(serverSd);
cout << "Connection closed..." << endl;
return 0;
}
void receiveFromClient() {
char buf[TMP_LENGTH];
char msg[TMP_LENGTH];
while(1) {
memset(&msg, 0, sizeof(msg));//clear the buffer
recv(newSd, (char*)&msg, sizeof(msg), 0);
cout << "Client: " << msg << endl;
cout << ">";
checkClientMessage(msg);
}
/*
//receiving MAIL FROM
bzero(buf,TMP_LENGTH);
recv(newSd, (char*)&buf, sizeof(buf), 0);
cout<<buf <<"\n";
checkClientMessage(buf);
// RCPT TO
bzero(buf,TMP_LENGTH);
recv(newSd, (char*)&buf, sizeof(buf), 0);
cout<<buf <<"\n";
string duplicate = buf;
cout<<duplicate<<"\n";
checkClientMessage(buf);
char* fname = extractName(duplicate.c_str());
//char* fname = "nobody.txt";
//DATA
bzero(buf,TMP_LENGTH);
recv(newSd, (char*)&buf, sizeof(buf), 0);
cout<<buf <<"\n";
checkClientMessage(buf);
//header
bzero(buf,TMP_LENGTH);
recv(newSd, (char*)&buf, sizeof(buf), 0);
writeToFile(buf, fname);
cout<<buf <<"\n";
//msg-body
bzero(buf,TMP_LENGTH);
recv(newSd, (char*)&buf, sizeof(buf), 0);
cout<<buf <<"\n";
writeToFile(buf, fname);
sendStatusCode("250 Requested mail action okay, completed");
bzero(buf,TMP_LENGTH);
recv(newSd, (char*)&buf, sizeof(buf), 0);
writeToFile(buf, fname);
cout<<buf <<"\n";
//QUIT
bzero(buf,TMP_LENGTH);
recv(newSd, (char*)&buf, sizeof(buf), 0);
cout<<buf <<"\n";
checkClientMessage(buf);
*/
}
void writeToFile(char content[], char* target) {
ofstream outfile;
outfile.open(target, std::ios_base::app);
outfile << content<<endl<<endl;
}
char* extractName(const char* msg) {
char* msg2 = (char*) msg;
strtok(msg2, ":");
char* targetUserName = strtok(NULL, "@");
strcat(targetUserName, ".txt");
cout<<targetUserName<<endl;
return targetUserName;
}
void closeConnection() {
close(newSd);
flag = 1000;
cout<<"Done processing this client, closed connection" <<"\n";
}