summaryrefslogtreecommitdiff
path: root/src/connect_user_struct.c
blob: ac6fe3c714ae1677904bbcdf94ab37e36c7dd3ed (plain)
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
/*
 * active port forwarder - software for secure forwarding
 * Copyright (C) 2003-2007 jeremian <jeremian [at] poczta.fm>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 */

#include <config.h>

#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include "activefor.h"
#include "connect_user_struct.h"

/*
 * Function name: ConnectUser_new
 * Description: Create and initialize new ConnectUser structure.
 * Returns: Newly created ConnectUser structure.
 */

ConnectUser*
ConnectUser_new()
{
  ConnectUser* tmp = calloc(1, sizeof(ConnectUser));
  assert(tmp != NULL);
  if (tmp == NULL) {
    return NULL;
  }
  tmp->bufList = BufList_new();
  assert(tmp->bufList != NULL);
  if (tmp->bufList == NULL) {
    ConnectUser_free(&tmp);
    return NULL;
  }
  tmp->stats = UserStats_new();
  assert(tmp->stats != NULL);
  if (tmp->stats == NULL) {
    ConnectUser_free(&tmp);
    return NULL;
  }
  return tmp;
}

/*
 * Function name: ConnectUser_free
 * Description: Free the memory allocated for ConnectUser structure.
 * Arguments: cu - pointer to pointer to ConnectUser structure
 */

void
ConnectUser_free(ConnectUser** cu)
{
  BufList* bftmp;
  UserStats* ustmp;
  assert(cu != NULL);
  if (cu == NULL) {
    return;
  }
  assert((*cu) != NULL);
  if ((*cu) == NULL) {
    return;
  }
  bftmp = ConnectUser_get_bufList((*cu));
  ustmp = ConnectUser_get_stats((*cu));
  BufList_free(&bftmp);
  UserStats_free(&ustmp);
  free((*cu));
  (*cu) = NULL;
}

/*
 * Function name: ConnectUser_set_state
 * Description: Set state of the connected user.
 * Arguments: cu - pointer to ConnectUser structure
 *            state - state of the connected User
 */

void
ConnectUser_set_state(ConnectUser* cu, char state)
{
  assert(cu != NULL);
  if (cu == NULL) {
    return;
  }
  cu->state = state;
}

/*
 * Function name: ConnectUser_set_connFd
 * Description: Set connection's file descriptor.
 * Arguments: cu - pointer to ConnectUser structure
 *            connFd - connection's file descriptor
 */

void
ConnectUser_set_connFd(ConnectUser* cu, int connFd)
{
  assert(cu != NULL);
  if (cu == NULL) {
    return;
  }
  cu->connFd = connFd;
}

/*
 * Function name: ConnectUser_set_whatClient
 * Description: Set client number, to which this user is connected to.
 * Arguments: cu - pointer to ConnectUser structure
 *            whatClient - client number, to which this user is connected to
 */

void
ConnectUser_set_whatClient(ConnectUser* cu, int whatClient)
{
  assert(cu != NULL);
  if (cu == NULL) {
    return;
  }
  cu->whatClient = whatClient;
}

/*
 * Function name: ConnectUser_set_userId
 * Description: Set user identification number.
 * Arguments: cu - pointer to ConnectUser structure
 *            userId - user identification number
 */

void
ConnectUser_set_userId(ConnectUser* cu, int userId)
{
  assert(cu != NULL);
  if (cu == NULL) {
    return;
  }
  cu->userId = userId;
}

/*
 * Function name: ConnectUser_set_connectTime
 * Description: Set connect time of the user.
 * Arguments: cu - pointer to ConnectUser structure
 *            connectTime - connect time of the user
 */

void
ConnectUser_set_connectTime(ConnectUser* cu, time_t connectTime)
{
  assert(cu != NULL);
  if (cu == NULL) {
    return;
  }
  cu->connectTime = connectTime;
}

/*
 * Function name: ConnectUser_set_nameBuf
 * Description: Set name of the user.
 * Arguments: cu - pointer to ConnectUser structure
 *            nameBuf - name of the user
 */

void
ConnectUser_set_nameBuf(ConnectUser* cu, char* nameBuf)
{
  assert(cu != NULL);
  if (cu == NULL) {
    return;
  }
  memset(cu->nameBuf, 0, 128);
  strncpy(cu->nameBuf, nameBuf, 127);
}

/*
 * Function name: ConnectUser_set_portBuf
 * Description: Set port from which user is connected.
 * Arguments: cu - pointer to ConnectUser structure
 *            portBuf - port from which user is connected
 */

void
ConnectUser_set_portBuf(ConnectUser* cu, char* portBuf)
{
  assert(cu != NULL);
  if (cu == NULL) {
    return;
  }
  memset(cu->portBuf, 0, 7);
  strncpy(cu->portBuf, portBuf, 6);
}

/*
 * Function name: ConnectUser_set_bufList
 * Description: Set buffer list for incoming packets.
 * Arguments: cu - pointer to ConnectUser structure
 *            bufList - buffer list for incoming packets
 */

void
ConnectUser_set_bufList(ConnectUser* cu, BufList* bufList)
{
  BufList* bftmp;
  assert(cu != NULL);
  if (cu == NULL) {
    return;
  }
  bftmp = ConnectUser_get_bufList(cu);
  BufList_free(&bftmp);
  cu->bufList = bufList;
}

/*
 * Function name: ConnectUser_set_stats
 * Description: Set stats object for this user.
 * Arguments: cu - pointer to ConnectUser structure
 *            stats - stats object for this user
 */

void
ConnectUser_set_stats(ConnectUser* cu, UserStats* stats)
{
  UserStats* ustmp;
  assert(cu != NULL);
  if (cu == NULL) {
    return;
  }
  ustmp = ConnectUser_get_stats(cu);
  UserStats_free(&ustmp);
  cu->stats = stats;
}

/*
 * Function name: ConnectUser_get_state
 * Description: Get state of the connected user.
 * Arguments: cu - pointer to ConnectUser structure
 * Returns: State of the connected user.
 */

char
ConnectUser_get_state(ConnectUser* cu)
{
  assert(cu != NULL);
  if (cu == NULL) {
    return S_STATE_CLEAR;
  }
  return cu->state;
}

/*
 * Function name: ConnectUser_get_connFd
 * Description: Get connection's file descriptor.
 * Arguments: cu - pointer to ConnectUser structure
 * Returns: Connection's file descriptor.
 */

int
ConnectUser_get_connFd(ConnectUser* cu)
{
  assert(cu != NULL);
  if (cu == NULL) {
    return -1;
  }
  return cu->connFd;
}

/*
 * Function name: ConnectUser_get_whatClient
 * Description: Get client number, to which this user is connected to.
 * Arguments: cu - pointer to ConnectUser structure
 * Returns: Client number, to which this user is connected to.
 */

int
ConnectUser_get_whatClient(ConnectUser* cu)
{
  assert(cu != NULL);
  if (cu == NULL) {
    return -1;
  }
  return cu->whatClient;
}

/*
 * Function name: ConnectUser_get_userId
 * Description: Get user identification number.
 * Arguments: cu - pointer to ConnectUser structure
 * Returns: User identification number.
 */

int
ConnectUser_get_userId(ConnectUser* cu)
{
  assert(cu != NULL);
  if (cu == NULL) {
    return -1;
  }
  return cu->userId;
}

/*
 * Function name: ConnectUser_get_connectTime
 * Description: Get connect time of the user.
 * Arguments: cu - pointer to ConnectUser structure
 * Returns: Connect time of the user.
 */

time_t
ConnectUser_get_connectTime(ConnectUser* cu)
{
  assert(cu != NULL);
  if (cu == NULL) {
    return 0;
  }
  return cu->connectTime;
}

/*
 * Function name: ConnectUser_get_nameBuf
 * Description: Get name of the user.
 * Arguments: cu - pointer to ConnectUser structure
 * Returns: Name of the user.
 */

char*
ConnectUser_get_nameBuf(ConnectUser* cu)
{
  assert(cu != NULL);
  if (cu == NULL) {
    return NULL;
  }
  return cu->nameBuf;
}

/*
 * Function name: ConnectUser_get_portBuf
 * Description: Get port from which user is connected.
 * Arguments: cu - pointer to ConnectUser structure
 * Returns: Port from which user is connected.
 */

char*
ConnectUser_get_portBuf(ConnectUser* cu)
{
  assert(cu != NULL);
  if (cu == NULL) {
    return NULL;
  }
  return cu->portBuf;
}

/*
 * Function name: ConnectUser_get_bufList
 * Description: Get buffer list for incoming packets.
 * Arguments: cu - pointer to ConnectUser structure
 * Returns: Buffer list for incoming packets.
 */

BufList*
ConnectUser_get_bufList(ConnectUser* cu)
{
  assert(cu != NULL);
  if (cu == NULL) {
    return NULL;
  }
  return cu->bufList;
}

/*
 * Function name: ConnectUser_get_stats
 * Description: Get stats object for this user.
 * Arguments: cu - pointer to ConnectUser structure
 * Returns: Stats object for this user.
 */

UserStats*
ConnectUser_get_stats(ConnectUser* cu)
{
  assert(cu != NULL);
  if (cu == NULL) {
    return NULL;
  }
  return cu->stats;
}