-
Notifications
You must be signed in to change notification settings - Fork 0
/
userLogins.h
238 lines (206 loc) · 7.59 KB
/
userLogins.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
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <threads.h>
#include <errno.h>
#include <time.h>
#include <ctype.h>
#include <sysexits.h>
#include <assert.h>
#include <time.h>
#ifndef USERLIST_H
#define USERLIST_H
/** External view of IntList ... implementation in IntList.c */
typedef struct UserListRep *user_List;
typedef struct clientsRep *client_List;
typedef struct loginTimesRep *loginTimes_List;
typedef struct loginTimesNode *loginTimesNode;
typedef struct offlineMsgRep *offlineMsg_List;
typedef struct clientsNode *clientNode;
typedef struct UserListNode *userNode;
typedef struct offlineMsgNode *offlineMsgNode;
typedef struct timeoutNode *timeKeeper;
// data structures representing UserList
struct UserListNode {
int loginAttempts; /**< Records the amount of login attempts */
int loggedIn; /**< Is 1 when the user is logged in */
time_t blockTime; /** signed 32-bit Int */
time_t loggedInTimer; /** Timer that controls when to log out AFK*/
int sendTag; /** Is 1 when it is the servers time to send*/
char *username;
int client_fd;
char *welcomeBroadcast;
struct UserListNode *next;
/**< pointer to node containing next element */
};
struct timeoutNode {
char *username;
time_t currTime;
int timeout;
int socket;
struct UserListRep *users;
};
struct UserListRep {
int size; /**< number of elements in list */
/** This is the name of the person calling the function */
struct UserListNode *first;
/**< node containing first value */
struct UserListNode *last;
/**< node containing last value */
struct clientsRep *client;
struct offlineMsgRep * offline;
int waitPrivate;
time_t timeSince;
char *message;
char *userToMessage;
char *username;
};
///////////////////////////////////////////////////////////////////////////////
//
// Clients list
//
///////////////////////////////////////////////////////////////////////////////
struct clientsNode {
char * username;
int fd;
struct clientsNode *next;
};
struct clientsRep {
struct clientsNode *first;
/**< node containing first value */
int size;
struct clientsNode *last;
};
// Linkedlists to deal with offline messages
struct offlineMsgRep {
struct offlineMsgNode *first;
/**< node containing first value */
int size;
struct offlineMsgNode *last;
};
struct offlineMsgNode {
char * username;
char ** messages;
int messagesSize;
struct offlineMsgNode *next;
};
///////////////////////////////////////////////////////////////////////////////
//
// Persistant login-times
//
///////////////////////////////////////////////////////////////////////////////
struct loginTimesRep {
struct loginTimesNode *first;
/**< node containing first value */
int size;
struct loginTimesNode *last;
};
struct loginTimesNode {
char * username;
time_t firstLogin;
time_t logoutTime;
char **blacklist;
int blacklistSize;
int loggedIn;
int attempts;
time_t blockTime;
struct loginTimesNode *next;
};
////////////////////////////////////////////////////////////////////////
// List ADT
typedef struct client_list *List;
typedef struct args *Args;
typedef struct client_node *Node;
// Arguments struct (and creation function) to pass the required info
// into the thread handlers.
struct args {
List list;
int fd;
int block_duration;
int timeout;
struct sockaddr_in *tempSa; // Hold the clients information temporarily
user_List *clients;
};
struct client_list {
Node head, tail;
};
struct client_info {
char host[INET_ADDRSTRLEN];
int port;
char *user;
};
struct client_node {
struct client_info client;
Node next, prev;
};
Args new_args(List list, int fd);
List list_new(void);
void list_add_node(List list, Node node);
void list_add(List list, struct client_info client);
void list_remove_client(List list, struct client_info client);
void list_remove(List list, Node to_remove);
Node list_find(List list, struct client_info client);
void list_destroy(List list);
void print_list(List list);
struct client_info get_client_info(struct sockaddr_in *sa, char *user);
Node node_new(struct client_info client);
void node_destroy(Node node);
////////////////////////////////////////////////////////////////////////
/** Create a new, empty IntList. */
struct UserListRep *NewUserList (void);
/** Release all resources associated with an IntList. */
void freeUserList (user_List list);
/** Apppend one integer to the end of an IntList. */
void UserListInsert (user_List list, char *name, int loginAttempts,
int loggedIn, int fd);
/** Delete first occurrence of `v` from an IntList.
* If `v` does not occur in IntList, no effect. */
void UserListDelete (user_List list, int v);
void incrementAttemptUserList (user_List list, char *username);
void flagLoggedInUserList (user_List list, char *username, int flag,
time_t loggedOut);
int getLoginFlagInUserList (user_List list, char *username);
void setLoginFlagInUserList (user_List list, char *username, int loggedIn);
void setLoginTimeUserList (user_List list, char *name, time_t time);
int isInUserList (user_List list, char *name);
void setFirstLoginUserList (user_List list, char *username, time_t time);
time_t getFirstLoginUserList (user_List list, char *name); // when first loggin occurs
void setServerSendTagUserList (user_List list, char *name, int tag);
int getServerSendTagUserList (user_List list, char *name);
void setBroadcastUserList (user_List list, char *username, char *msg);
// BlackList user functions
void setBlackListUser (loginTimes_List list, char *myname, char *userToBl);
int getUserBlackListed (loginTimes_List list, char *myname, char *user);
void removeBlackList (loginTimes_List L, char *myname, char *unblName);
// Offline messages
void addOfflineMsg (offlineMsg_List list, char *name, char *message);
void removeOfflineMsg (offlineMsg_List L, char *myname, char *message);
void offlineMsgListInsert (offlineMsg_List L, char *name, char *message);
struct offlineMsgNode *newofflineMsgNode (char *name, char *message);
struct offlineMsgRep* newofflineMsgList (void);
// Login times functions
void loginTimesInsert (loginTimes_List L, char *username, time_t firstLogin,
int flag);
struct loginTimesNode *newLoginTimeNode (char *username, time_t firstLogin,
int flag);
int getLoginFlag (loginTimes_List list, char *username);
struct loginTimesRep* newLoginTimesList (void);
time_t getLoginTime (loginTimes_List list, char *name);
time_t getLogoutTime (loginTimes_List list, char *name);
void setLoginTime(loginTimes_List list, char *name, time_t time, int flag);
int isInTimeTracker (loginTimes_List list, char *name);
// for 3 attempts retry login
void resetAttemptUser (loginTimes_List list, char *name);
void incrementAttemptUser (loginTimes_List list, char *name);
void setBlockTimeUser (loginTimes_List list, char *name, time_t time);
int getAttemptUser (loginTimes_List list, char *name);
time_t getBlockTimeUser(loginTimes_List list, char *name);
//// Client list functions
struct clientsNode *newClientListNode (char *name, int client_fd);
struct clientsRep* newClientsList (void);
void clientListInsert (client_List L, char *name, int client_fd);
#endif