-
Notifications
You must be signed in to change notification settings - Fork 2
/
serverS.cpp
438 lines (427 loc) · 13.6 KB
/
serverS.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
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
//UDPServer.c
/* login = works
logout = works
* gcc -o server UDPServer.c
* ./server
*/
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <netdb.h>
#include <iostream>
#include <string>
#include <map>
#include <utility>
#include <vector>
#include "duckchat.h"
//defined
#define BUFLEN 1024
using namespace std;
//globals
struct sockaddr recAddr;
socklen_t fromlen = sizeof(recAddr);
int sockfd;
struct addrinfo *addrAr;
multimap<pair<string,string>, string> addrToUser;
multimap<string, pair<string,string> > userToAddr;
map<string,string> usrTlkChan;
map<string,struct sockaddr_in> userToAddrStrct;
map<string,vector<string> > chanTlkUser;
vector<string> channels;
//methods
int connectToSocket(char*, char*);
void err(char*);
int readRequestType(struct request*, int);
int sayReq(struct request_say*);
int checkValidAddr();
string getUserOfCurrAddr();
string getAddr_string();
string getSemiAddr_string();
int getAddr_Port();
struct sockaddr_in getAddrStruct();
//program
int main(int argc, char **argv)
{
addrAr = NULL;
sockfd = 0;
connectToSocket(argv[1], argv[2]);
while(1)
{
//for multiple requests maybe
// requests = (struct request*) malloc(sizeof (struct request) + BUFLEN);
char *buf = new char[BUFLEN];
struct request *requests = (struct request*)malloc(sizeof(struct request*) + BUFLEN);
int bal = 0;
bal = recvfrom(sockfd, buf, BUFLEN, 0, (struct sockaddr*)&recAddr, &fromlen);
if(bal > 0) {
printf("recv()'d %d bytes of data in buf\n", bal);
requests = (request*) buf;
cout << "do I get herer??? \n";
readRequestType(requests, bal);
for(map<string,sockaddr_in>::iterator beem = userToAddrStrct.begin(); beem !=userToAddrStrct.end(); beem++) {
cout << "users are: " << beem->first << " \n";
}
}
//requests = NULL;
delete[] buf;
}
return 0;
}
//returns string of username of current request address
string getUserOfCurrAddr()
{
struct sockaddr_in address =(sockaddr_in)recAddr;
string aTmp = "";
map<string,struct sockaddr_in>::iterator i;
for(i=userToAddrStrct.begin(); i != userToAddrStrct.end(); i++) {
if(i->second == address) {
aTmp = i->first;
}
}
return aTmp;
}
//return semi address
string getSemiAddr_string()
{
struct sockaddr_in* address = (struct sockaddr_in*)&recAddr;
char *addrString = (char*)malloc(sizeof(char)*BUFLEN);
//make address string
inet_ntop(AF_INET, &address, addrString, BUFLEN);
//have tmp var
string realAddrString = addrString;
free (addrString);
return realAddrString;
}
struct sockaddr_in getAddrStruct()
{
struct sockaddr_in address = (sockaddr_in)recAddr;
return address;
}
//returns string form of address
string getAddr_string() {
//new request address info
struct sockaddr_in* address = (struct sockaddr_in*)&recAddr;
char *addrString = (char*)malloc(sizeof(char)*BUFLEN);
//make address string
inet_ntop(AF_INET, &(address->sin_addr), addrString, BUFLEN);
//have tmp var
string realAddrString = addrString;
free (addrString);
return realAddrString;
}
//returns string form of address
int getAddr_Port() {
struct sockaddr_in* address = (struct sockaddr_in*)&recAddr;
int realAddrString = htons(address->sin_port);
return realAddrString;
}
//check if current request address is valid or exist in map
int checkValidAddr(struct request *r)
{
string realAddrString = getAddr_string();
string smiAddrString = getSemiAddr_string();
map<pair<string,string>,string>::iterator it = addrToUser.find(pair<string,string>(realAddrString,smiAddrString));
if(it == addrToUser.end()) {
cout << "super baddd addressss mann\n";
cout << realAddrString << " that THING\n";
return -1;
}
return 0;
}
//for errors
void err(char *str)
{
perror(str);
exit(1);
}
//handle say requests
int sayReq(struct request_say *rs)
{
string channel = rs->req_channel;
string message = rs->req_text;
string username = getUserOfCurrAddr();
cout << "Username fromuser: " << username << "\n";
map<string,vector<string> >::iterator hit = chanTlkUser.find(channel);
if(hit == chanTlkUser.end()) {
cout << "JESUS CHRIST: " << channel <<"\n";
return -1;
}
vector<string> tmpU = hit->second;
for(int i=0; i<tmpU.size(); i++) {
cout << "user: " << tmpU[i] << " on channel: " << channel << " in iteration on say loop: " << i <<"\n";
struct sockaddr_in address;
map<string, struct sockaddr_in>::iterator ui = userToAddrStrct.find(tmpU[i]);
address = ui->second;
struct text_say *msg= (struct text_say*) malloc(sizeof(struct text_say));
msg->txt_type= htonl(TXT_SAY);
strncpy(msg->txt_username, username.c_str(), USERNAME_MAX);
strncpy(msg->txt_text, message.c_str(), SAY_MAX);
strncpy(msg->txt_channel, channel.c_str(), CHANNEL_MAX);
int size = sizeof(struct sockaddr*);
int res= sendto(sockfd, msg, sizeof(struct text_say), 0, (struct sockaddr*)&address, size);
if (res == -1) {
cout << "sendto very badd \n";
//return -1;
}
free(msg);
}
tmpU.clear();
return 0;
}
//handle login requests
int loginReq(struct request_login *rl)
{
int poort = getAddr_Port();
cout << "this should be PORT " << poort << " \n";
int prt = (int)poort;
string realAddrString = getAddr_string();
string username = rl->req_username;
string smiAddr = getSemiAddr_string();
struct sockaddr_in strctAddr = getAddrStruct();
userToAddrStrct[username] = strctAddr;
addrToUser.insert(pair<pair<string,string>,string>(pair<string,string>(realAddrString,smiAddr), username));
userToAddr.insert(pair<string,pair<string,string> >(username, pair<string,string>(realAddrString,smiAddr)));
map<string,vector<string> >::iterator it = chanTlkUser.find("Common");
vector<string> usersC;
if(it == chanTlkUser.end()) {
chanTlkUser.insert(pair<string,vector<string> >("Common", usersC));
channels.push_back("Common");
}
it = chanTlkUser.find("Common");
usersC = it->second;
usersC.insert(usersC.begin(), username);
chanTlkUser["Common"] = usersC;
usrTlkChan.insert(pair<string,string>(username, "Common"));
return 0;
}
//handle login requests
int logoutReq(struct request_logout *rl)
{
string realAddrString = getAddr_string();
string username = getUserOfCurrAddr();
string tmpaddr;
map<string, struct sockaddr_in>::iterator sockIt = userToAddrStrct.find(username);
userToAddrStrct.erase(sockIt);
multimap<pair<string,string>, string>::iterator i;
for(i=addrToUser.begin(); i!=addrToUser.end(); i++) {
if(i->second == username) {
tmpaddr = i->first.first;
cout << "deleting 1 \n";
addrToUser.erase(i);
}
}
multimap<string, pair<string,string> >::iterator ii;
for(ii=userToAddr.begin(); ii!=userToAddr.end(); ii++) {
if(ii->first == username) {
cout << "deleting 2 \n";
userToAddr.erase(ii);
}
}
// look for user in channel listen
//look for user in channel talk
map<string,string>::iterator git;
git = usrTlkChan.find(username);
if(git != usrTlkChan.end()) {
cout << "deleting 5 \n";
usrTlkChan.erase(username);
}
//erase user on channels in chanTlkUser
for(int ick=0; ick<channels.size(); ick++) {
cout << "in outerr \n";
map<string,vector<string> >::iterator itck = chanTlkUser.find(channels[ick]);
vector<string> usersC = itck->second;
for(int j=0; j<usersC.size(); j++) {
if(usersC[j] == username) {
cout << "deleting user: " << usersC[j] << " \n";
usersC.erase(usersC.begin()+j);
}
}
chanTlkUser.erase(itck);
chanTlkUser.insert(pair<string,vector<string> >(channels[ick],usersC));
}
return 0;
}
//handle login requests
int joinReq(struct request_join *rj)
{
//create tmp vars for username and channel of request
string chan = (string)rj->req_channel;
string user = getUserOfCurrAddr();
int trig = 0;
map<string,vector<string> >::iterator it = chanTlkUser.find(chan);
vector<string> usersC;
if(it == chanTlkUser.end()) {
//NEW CHANNEL
usersC.insert(usersC.begin(), user);
chanTlkUser.insert(pair<string,vector<string> >(chan, usersC));
channels.push_back(chan);
} else {
//old channel
it = chanTlkUser.find(chan);
usersC = it->second;
for(int i=0; i<usersC.size(); i++) {
if(usersC[i] == user) {
return -1;
}
}
usersC.insert(usersC.begin(), user);
chanTlkUser[chan] = usersC;
}
string chanTlk = usrTlkChan[user];
chanTlk = chan;
usrTlkChan[user] = chanTlk;
return 0;
}
//handle login requests
int leaveReq(struct request_leave *rl)
{
return 0;
}
//handle login requests
int listReq(struct request_list *rl)
{
struct sockaddr* address;
multimap<string, pair<string,string> >::iterator ui = userToAddr.find(getUserOfCurrAddr());
pair<string,string> tad = ui->second;
string ad = tad.second;
char *s= (char*) malloc(sizeof(char)*BUFLEN);
strncpy(s, ad.c_str(), strlen(ad.c_str()));
inet_pton(AF_INET, s, &address);
struct text_list *msg= (struct text_list*) malloc(sizeof(struct text_list) + BUFLEN);
msg->txt_type= htonl(TXT_LIST);
msg->txt_nchannels = htonl(channels.size());
for (int i = 0; i < channels.size(); i++) {
strncpy(msg->txt_channels[i].ch_channel, channels[i].c_str(), CHANNEL_MAX);
}
int size = sizeof(struct sockaddr);
int res= sendto(sockfd, msg, sizeof(struct text_list), 0, address, size);
if (res == -1) {
cout << "sendto very badd \n";
//return -1;
}
free(msg);
return 0;
}
//handle login requests
int whoReq(struct request_who *rw)
{
return 0;
}
int readRequestType(struct request *r, int b)
{
int fin = 0;
int netHost = 0;
netHost = ntohl(r->req_type);
//check if addres is a crazy number or normal
if(netHost > 6 || netHost < 0) {
netHost = r->req_type;
}
//check if request address is valid
if(netHost != 0) {
if(checkValidAddr(r) == -1) {
cout << "invalid address\n";
return -1;
}
}
switch(netHost) {
case REQ_LOGIN:
if(sizeof(struct request_login) == b) {
cout << "login request\n";
fin = loginReq((struct request_login*) r);
break;
} else {
cout << "login request FAILED\n";
break;
}
case REQ_LOGOUT:
if(sizeof(struct request_logout) == b) {
cout << "logout request\n";
fin = logoutReq((struct request_logout*) r);
break;
} else {
cout << "logout request bad size\n";
break;
}
case REQ_JOIN:
//printf("join case made \n");
if(sizeof(struct request_join) == b) {
cout << "join request\n";
fin = joinReq((struct request_join*) r);
break;
} else {
cout << "switch request bad size\n";
break;
}
case REQ_LEAVE:
if(sizeof(struct request_leave) == b) {
cout << "leave request\n";
fin = leaveReq((struct request_leave*) r);
break;
} else {
cout << "leave request bad size\n";
break;
}
case REQ_SAY:
if(sizeof(struct request_say) == b) {
cout << "say request\n";
fin = sayReq((struct request_say*) r);
break;
} else {
cout << "say request bad size\n";
break;
}
case REQ_LIST:
if(sizeof(struct request_list) == b) {
cout << "list request\n";
fin = listReq((struct request_list*) r);
break;
} else {
cout << "list request bad size\n";
break;
}
case REQ_WHO:
if(sizeof(struct request_who) == b) {
cout << "who request\n";
fin = whoReq((struct request_who*) r);
break;
} else {
cout << "who request bad size\n";
break;
}
default:
cout << "default case hit!!\n";
}
return fin;
}
int connectToSocket(char* ip, char* port)
{
struct addrinfo addressTmp;
memset(&addressTmp, 0, sizeof addressTmp);
addressTmp.ai_family = AF_INET;
addressTmp.ai_socktype = SOCK_DGRAM;
addressTmp.ai_flags = AI_PASSIVE;
int check = 0;
if((check = getaddrinfo(ip, port, &addressTmp, &addrAr))!= 0)
{
cout << "Server : getaddrinfo() NOT successful \n";
return false;
}
if((sockfd = socket(addrAr->ai_family, addrAr->ai_socktype, addrAr->ai_protocol)) == -1)
{
cout << "Server : socket() NOT successful \n";
return false;
}
if(bind(sockfd, addrAr->ai_addr, addrAr->ai_addrlen) == -1)
{
cout << "Server : bind() NOT successful \n";
return false;
}
cout << "socket and bind successful! \n";
return true;
}