-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSERVERphase2.cpp
More file actions
314 lines (291 loc) · 7.25 KB
/
SERVERphase2.cpp
File metadata and controls
314 lines (291 loc) · 7.25 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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#include<iostream>
#include<fstream>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<regex>
#include<unistd.h>
#include<sys/types.h>
#include<dirent.h>
using namespace std;
int backlog = 5;
const int chunk_size = 1024;
int send_message(int sockfd, string message){
//Prints error message if not sent and returns -10
//Else returns number of bytes sent
//Convert to char array
char mess[message.length()+1];
for (int i=0; i< message.length();i++){
mess[i] = message[i];
}
mess[message.length()] = '\0';
//Send char array
int x = send(sockfd,&mess,strlen(mess),0);
if(x<0){
cerr<<"Message not sent: "<<message<<endl;
return -10;
}
return x;
}
string rec_message(int sockfd){
//received upto chunk_size bytes and outputs as string.
//Prints appropriate errors
char buff[chunk_size] = {0};
int read_bytes = recv(sockfd,(void*)&buff, chunk_size, 0);
if(read_bytes == 0){
cerr<<"Connection closed before reading\n";
exit(0);
}
else if(read_bytes == -1){
cerr<<"Error on receive\n";
exit(0);
}
string buf(buff);
//cout<<"a"<<endl;
return buf;
}
bool match_user(const char* filename, string buf, string& user)
{
ifstream file;
file.open(filename);
if(!file.good()){
cerr<<"Unable to open file\n";
exit(3);
}
regex message("(User:\\s)(.*)( Pass:\\s)(.*)");
string username;
string password;
if(regex_match(buf, message)){
int f1 = buf.find(" ");
//cout<<"f1 is "<<f1<<endl;
int f2 = buf.find("Pass:",f1+1);
// //cout<<"f2 is "<<f2<<endl;
username = buf.substr(f1+1,f2-7);
f1 = buf.find(": ",f2+1);
password = buf.substr(f1+2);
//bool an = match_user(passfile, message_username, message_password);
/*if(!an)
{
//cout<<"User not valid";
return -2;
}
else{ return 1;}*/
}
while(!file.eof())
{
string line;
getline(file, line);
int f1 = line.find(" ");
//cout<<"f1 is "<<f1<<endl;
int f2 = line.find("Pass:",f1+1);
// //cout<<"f2 is "<<f2<<endl;
string file_username = line.substr(f1+1,f2-7);
f1 = line.find(": ",f2+1);
string file_password = line.substr(f1+2);
if(line==username+" "+ password)
{
user = username;
return true;}
else if (file_username==username)
{
cout<<"Wrong Password";
return false;
}
}
cout<<"Wrong username";
return false;
}
int send_list(string username, const char* database, string& mess)
{
DIR* p;
struct dirent* dp;
if((p=opendir(database))==NULL)
{
cerr<<"database";
return -1;
}
while((dp=readdir(p))!= NULL)
{
if(string(dp->d_name)==username)
{
DIR* q;
string a =string(database) + username;
int x = a.length();
char u[x+1];
strcpy(u, a.c_str());
if((q=opendir((const char*) u))==NULL)
{
cerr<<"username";
return -2;
}
int n;
struct dirent* dp1;
while((dp1=readdir(q))!=NULL)
{
n=n+1;
}
mess = username+" Number of messages "+to_string(n)+"\n";
cout<<mess;
return 2;
}
}
}
//int parse_message(const char* filename, string buf, const char* database){
//
//// ifstream passfile;
//// passfile.open(filename);
//// if(!passfile.good()){
//// cerr<<"Unable to open file\n";
//// exit(3);
//// }
//
// regex quit("quit");
// regex message("(User:\\s)(.*)( Pass:\\s)(.*)");
// regex words("[^:\\s]");
// regex lists("LIST");
////Quit case
// if(regex_match(buf, quit)){
// cout<<"Bye "<<endl;
// return -1;
// }
////Extracting Username and Password
// else if(regex_match(buf, message)){
//
// int f1 = buf.find(" ");
// //cout<<"f1 is "<<f1<<endl;
// int f2 = buf.find("Pass:",f1+1);
//// //cout<<"f2 is "<<f2<<endl;
// string message_username = buf.substr(f1+1,f2-7);
// f1 = buf.find(": ",f2+1);
// string message_password = buf.substr(f1+2);
// bool an = match_user(passfile, message_username, message_password);
// if(!an)
// {
// //cout<<"User not valid";
// return -2;
// }
// else{ return 1;}
//// if(username!=message_username){
//// cout<<"Invalid User\n";
//// //cout<<"Extracted username is "<<message_username<<"A"<<endl;
//// //cout<<"Extracted password is "<<message_password<<"A"<<endl;
//// //cout<<"Username is "<<username<<"A"<<endl;
//// //cout<<"Password is "<<password<<"A"<<endl;
//// exit(0);
//// }
//// else if(password!=message_password){
//// cout<<"Wrong Passwd\n";
//// exit(0);
//// }
//// else{
//// return 0;
//// }
// }
// //else if(regex_match(buf, lists)){
// // send_list(username, database);
// //return 2;
// //}
// //Invalid message
// else{
// cout<<"Unknown Command\n";
// return -1;
// }
//}
int quit(string buf)
{
regex quit("quit");
if (regex_match(buf, quit))
{
cout<<"Bye"<<endl;
return -1;
}
}
int main(int argc, char*argv[]){
//Validates Arguments
if (argc != 4){
cerr<<"Invalid number of arguments\nUsage: SimpleEmailServerPhase1 <PortNum> <PasswordFile>\n";
exit(1);
}
char **x;
int port = strtol(argv[1],x,10);
if(port == 0){
cout<<"Invalid port\n";
exit(2);
}
//cout<<"port is"<<" "<<port<<endl;
char *filename = argv[2];
//cout<<"filename is"<<" "<<filename<<endl;
char* database = argv[3];
// Password file
// string username,password;
// myfile>>username>>password;
// // cout<<"Username is "<<username<<endl;
// // cout<<"Password is "<<password<<endl;
// myfile.close();
//Initialize sockaddr
sockaddr_in myaddress;
myaddress.sin_family = AF_INET;
myaddress.sin_addr.s_addr = INADDR_ANY;//inet_aton("127.0.0.1")
myaddress.sin_port = htons(port);
memset(&(myaddress.sin_zero), '\0', 8); // zero the rest of the struct
int size_addr = sizeof(struct sockaddr);
//Set up socket
int opt = 1;
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt));
if(sockfd == 0){
cerr<<"Couldn't form socket\n";
exit(2);
}
else if(bind(sockfd,(sockaddr *) &myaddress, size_addr) < 0
|| listen(sockfd,backlog) < 0){
cerr<<"Bind failed\n";
exit(2);
}
else{
cout<<"BindDone: "<<port<<"\n";
cout<<"ListenDone: "<<port<<"\n";
}
//Accept connections
while(true)
{
sockaddr_in client_address;
int in_socket = accept(sockfd,(struct sockaddr *)&client_address, (socklen_t*)&size_addr);
if(in_socket < 0){
cerr<<"Couldn't accept connection\n";
exit(2);
}
else{
cout<<"Client: "<<inet_ntoa(client_address.sin_addr)<<":"<<ntohs(client_address.sin_port)<<endl;
}
//Receive message
string buf = rec_message(in_socket);
string username;
bool a = match_user(filename,buf, username);
if(!a){
close(in_socket);
}
else{
string welcome_message = "Welcome "+username+"\n";
cout<<welcome_message;
if(send_message(in_socket,welcome_message) < 0){
cout<<"Message not sent"<<endl;
}
}
//Second Message
string buf1 = rec_message(in_socket);
string mess;
int b = send_list(username, database, mess);
if(b<0){
close(in_socket);
}else{
send_message(in_socket, mess);
}
//Third Message
string buf2 = rec_message(in_socket);
if(quit(buf2)<0)
{
close(in_socket);
}
}
}