-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_methods.h
285 lines (262 loc) · 8.44 KB
/
server_methods.h
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
#ifndef SERVER_STUFF_H
#define SERVER_STUFF_H
#include "shared.h"
#include "server_data.h"
#include <errno.h>
/**
* short hand function to create a repsonse object
*/
struct response getResponse(unsigned short err, unsigned short messageType, unsigned short what, unsigned long msgLength) {
struct response ret;
ret.secretKey = RESPONSE_KEY;
ret.ERROR = err;
ret.messageType = messageType;
ret.msgLength = msgLength;
ret.what = what;
ret.duration = getBlockDuration();
return ret;
}
/**
* cusotom error and success message functions
*/
void sendErrorMsg(int fd, unsigned short err) {
char buffer[512];
struct response r = getResponse(err,NO_MSG,ERROR_MSG,0);
char *p = serialize_response(buffer,r);
int ret = send(fd,buffer,sizeof(r),MSG_NOSIGNAL);
if( ret < 0) {
printf("in sendErrorMsg %d\n",ret);
}
}
void sendSuccessMsg(int fd) {
char buffer[512];
struct response r = getResponse(SUCCESS,NO_MSG,SUCCESS_MSG,0);
char *p = serialize_response(buffer,r);
int ret = send(fd,buffer,sizeof(r),MSG_NOSIGNAL);
if( ret < 0) {
printf("in sendSuccessMsg %d\n",ret);
}
}
/**
* custom read function that will also print out error messages
*
* @param fd file descriptor or socket
* @param b buffer to read into
* @param s maximum number of bytes to read
* @param numBytes will add number of bytes read into the value pointed to by this pointer
*/
int serverRead(int fd,char* b, int s,int* numBytes) {
int retVal = read(fd,b,s);
if(retVal < 0 ) {
perror("something went wrong while reading\n");
sendErrorMsg(fd,INTERNAL_SERVER_ERROR);
return 1;
}
*numBytes = *numBytes + retVal;
return 0;
}
/**
* @param u userId to send message to
* @param mess null terminated string that holds the message
*
*/
void sendMsgToUser(int u,char* mess) {
if(isUserOnline(u)) {
int toSocket = getUserSocket(u);
struct response r = getResponse(SUCCESS,RAW,MESSAGE,strlen(mess)+1);
char buffer[1024];
char *p = serialize_response(buffer,r);
p = serialize_string(p,mess,strlen(mess) + 1);
int ret = send(toSocket,buffer,sizeof(r) + strlen(mess) + 1,MSG_NOSIGNAL);
if( ret < 0) {
printf("in sendMsgToUser %d\n",ret);
}
} else {
offlineMessage(u, mess);
}
}
/**
* @returns 1: it succeeded, 0: it failed
*/
int tryLogin(int sk) {
struct requestHeader header;
int error, retVal, numBytes=0;
char buffer[1024];
if(serverRead(sk,buffer,1024,&numBytes))
return 0;
//if a a port is closed unexpectedly select will (isPortOpen) will return true
if(numBytes == 0)
return 0;
char* p = deserialize_req_header(buffer,&header);
//verify key
if(header.secretKey != REQUEST_KEY) {
sendErrorMsg(sk,DATA_CORRUPT);
} else {
if(header.messageType != KEY_VALUE && header.msgLength != sizeof(struct keyValue)) {
sendErrorMsg(sk,INVALID_DATA);
} else {
struct keyValue kv;
deserialize_key_value(p,&kv);
int retVal = find_and_login(kv.key,kv.value,sk);
if(retVal) {
sendErrorMsg(sk,retVal);
} else {
sendSuccessMsg(sk);
char completeMsg[64];
strcpy(completeMsg,"user ");
strcat(completeMsg,kv.key);
strcat(completeMsg," has come online");
int userId = getUserId(kv.key);
for(int i=0;i<getNumUsers();i++) {
if(i == userId || !isUserOnline(i) || isBlocked(userId,i)) {
continue;
}
sendMsgToUser(i,completeMsg);
}
usleep(1000);
int id = getUserId(kv.key);
while(isBacklog(id)) {
char m[64];
backlogPop(id,m);
sendMsgToUser(id,m);
//TODO flush should do the same thing right?
//OR maybe just wait for a byte of data back or something?
usleep(1000000);
}
return 1;
}
}
}
//assumed failure except for one case, look for the return 1
return 0;
}
void returnHistory(int id,int sk,long timeSince) {
int ids[12] = {-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1};
int num = 0;
for(int i=0;i<getNumUsers();i++) {
if(timeSince >= 0) {
time_t crnt;
time(&crnt);
if(crnt - getLoggedInTime(i) < timeSince && isUserOnline(i) && i != id) {
ids[num++] = i;
}
} else {
if(isUserOnline(i) && i != id )
ids[num++] = i;
}
}
struct response r = getResponse(SUCCESS,KEY,HISTORY,num*sizeof(struct key));
char buffer[1024];
int size = sizeof(r);
char *ptr = serialize_response(buffer,r);
for(int i =0;i<num;i++) {
struct key k;
strcpy(k.key,getUsername(ids[i]));
ptr = serialize_key(ptr,k);
size += sizeof(k);
}
int ret = send(sk,buffer,size,MSG_NOSIGNAL);
if( ret < 0) {
printf("in sendMsgToUser %d\n",ret);
}
}
/**
* Main Connection handler
* handles all commadns except login
*
*/
void connectionHandler(int sk, int userId) {
char buffer[1024];
struct requestHeader h;
int retVal = read(sk,buffer,1024);
struct key k;
char completeMsg[64];
char* m;
if(retVal < 0) {
perror("error while reading..");
} else if(retVal >= sizeof(h)) {
char* ptr = deserialize_req_header(buffer,&h);
if(h.secretKey != REQUEST_KEY) {
sendErrorMsg(sk,DATA_CORRUPT);
return;
}
switch(h.command) {
case SEND_MESSAGE:
m = deserialize_key(ptr,&k);
int id = getUserId(k.key);
if(id == userId) {
sendErrorMsg(sk,SAME_USER);
return;
}
if(id == -1) {
sendErrorMsg(sk,NO_SUCH_USER);
return;
}
if(isBlocked(userId,id)) {
sendErrorMsg(sk,USER_BLACKLISTED);
return;
}
strcpy(completeMsg,getUsername(userId));
strcat(completeMsg,": ");
strcat(completeMsg,m);
sendMsgToUser(id,completeMsg);
break;
case BROADCAST:
strcpy(completeMsg,getUsername(userId));
strcat(completeMsg,": ");
strcat(completeMsg,ptr);
for(int i=0;i<getNumUsers();i++) {
if(isBlocked(userId,i)) {
continue;
//TODO send notification to sender that these users did not get the message
}
sendMsgToUser(i,completeMsg);
}
break;
case BLOCK:
if(h.messageType != KEY) {
sendErrorMsg(sk,INVALID_DATA);
return;
}
deserialize_key(ptr,&k);
retVal = blockUser(userId,getUserId(k.key));
if(retVal) {
sendErrorMsg(sk,retVal);
return;
}
break;
case UNBLOCK:
if(h.messageType != KEY) {
sendErrorMsg(sk,INVALID_DATA);
return;
}
deserialize_key(ptr,&k);
retVal = unblockUser(userId,getUserId(k.key));
if(retVal) {
sendErrorMsg(sk,retVal);
return;
}
break;
case HISTORY:
if(h.messageType == NO_MSG) {
returnHistory(userId,sk,-1);
} else if (h.messageType == KEY) {
deserialize_key(ptr,&k);
returnHistory(userId,sk,atol(k.key));
} else {
sendErrorMsg(sk,INVALID_COMMAND);
return;
}
break;
case USER_LOGOUT:
logout(userId);
close(sk);
return;
default:
sendErrorMsg(sk,INVALID_COMMAND);
return;
}
sendSuccessMsg(sk);
}
}
#endif