summaryrefslogtreecommitdiff
path: root/src/connect_client_struct.c
blob: a07ed50c08af3a07e68b09da3661cd6e4856df78 (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
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
/*
 * active port forwarder - software for secure forwarding
 * Copyright (C) 2003-2006 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 "string_functions.h"
#include "timeval_functions.h"
#include "connect_client_struct.h"

/*
 * Function name: ConnectClient_new
 * Description: Create and initialize new ConnectClient structure.
 * Returns: Pointer to newly created ConnectClient structure.
 */

ConnectClient*
ConnectClient_new()
{
  ConnectClient* tmp = calloc(1, sizeof(ConnectClient));
  if (tmp == NULL) {
    return NULL;
  }
  tmp->sslFd = SslFd_new();
  if (tmp->sslFd == NULL) {
    ConnectClient_free(&tmp);
    return NULL;
  }
  tmp->auditList = AuditList_new();
  if (tmp->auditList == NULL) {
    ConnectClient_free(&tmp);
    return NULL;
  }
  tmp->header = HeaderBuffer_new();
  if (tmp->header == NULL) {
    ConnectClient_free(&tmp);
    return NULL;
  }
  return tmp;
}

/*
 * Function name: ConnectClient_free
 * Description: Free the memory allocated for ConnectClient structure.
 * Arguments: cc - pointer to pointer to ConnectClient structure
 */

void
ConnectClient_free(ConnectClient** cc)
{
  SslFd* sftmp;
  AuditList* altmp;
  if (cc == NULL) {
    return;
  }
  if ((*cc) == NULL) {
    return;
  }
  sftmp = ConnectClient_get_sslFd((*cc));
  altmp = ConnectClient_get_auditList((*cc));
  SslFd_free(&sftmp);
  AuditList_free(&altmp);
  if ((*cc)->users) {
    free((*cc)->users);
    (*cc)->users = NULL;
  }
  if ((*cc)->sClientId) {
    free((*cc)->sClientId);
    (*cc)->sClientId = NULL;
  }
  free((*cc));
  (*cc) = NULL;
}

/*
 * Function name: ConnectClient_set_state
 * Description: Set state of the connected client.
 * Arguments: cc - pointer to ConnectClient structure
 *            state - state of the connected client
 */

void
ConnectClient_set_state(ConnectClient* cc, char state)
{
  if (cc == NULL) {
    return;
  }
  cc->state = state;
}

/*
 * Function name: ConnectClient_set_sslFd
 * Description: Set SslFd structure.
 * Arguments: cc - pointer to ConnectClient structure
 *            sf - SslFd structure
 */

void
ConnectClient_set_sslFd(ConnectClient* cc, SslFd* sf)
{
  SslFd* sftmp;
  if (cc == NULL) {
    return;
  }
  sftmp = ConnectClient_get_sslFd(cc);
  SslFd_free(&sftmp);
  cc->sslFd = sf;
}

/*
 * Function name: ConnectClient_set_timer
 * Description: Set timer of the client used for internal time counting.
 * Arguments: cc - pointer to ConnectClient structure
 *            timer - timer of the client used for internal time counting
 */

void
ConnectClient_set_timer(ConnectClient* cc, struct timeval timer)
{
  if (cc == NULL) {
    return;
  }
  cc->timer = timer;
}

/*
 * Function name: ConnectClient_set_users
 * Description: Set users descriptor table.
 * Arguments: cc - pointer to ConnectClient structure
 *            users - users descriptor table
 */

void
ConnectClient_set_users(ConnectClient* cc, int* users)
{
  if (cc == NULL) {
    return;
  }
  if (cc->users) {
    free(cc->users);
  }
  cc->users = users;
}

/*
 * Functions name: ConnectClient_set_connected
 * Description: Set number of connected users.
 * Arguments: cc - pointer to ConnectClient structure
 *            connected - number of connected users
 */

void
ConnectClient_set_connected(ConnectClient* cc, int connected)
{
  if (cc == NULL) {
    return;
  }
  cc->connected = connected;
}

/*
 * Function name: ConnectClient_set_limit
 * Description: Set limit of connected users.
 * Arguments: cc - pointer to ConnectClient structure
 *            limit - limit of connected users
 */

void
ConnectClient_set_limit(ConnectClient* cc, int limit)
{
  if (cc == NULL) {
    return;
  }
  cc->limit = limit;
}

/*
 * Function name: ConnectClient_set_listenFd
 * Description: Set listen socket descriptor.
 * Arguments: cc - pointer to ConnectClient structure
 *            listenFd - listen socket descriptor
 */

void
ConnectClient_set_listenFd(ConnectClient* cc, int listenFd)
{
  if (cc == NULL) {
    return;
  }
  cc->listenFd = listenFd;
}

/*
 * Function name: ConnectClient_set_usrCliPair
 * Description: Set user-client pair number.
 * Arguments: cc - pointer to ConnectClient structure
 *            usrCliPair - user-client pair number
 */

void
ConnectClient_set_usrCliPair(ConnectClient* cc, int usrCliPair)
{
  if (cc == NULL) {
    return;
  }
  cc->usrCliPair = usrCliPair;
}

/*
 * Function name: ConnectClient_set_clientId
 * Description: Set client identification number.
 * Arguments: cc - pointer to ConnectClient structure
 *            clientId - client identification number
 */

void
ConnectClient_set_clientId(ConnectClient* cc, int clientId)
{
  if (cc == NULL) {
    return;
  }
  cc->clientId = clientId;
}

/*
 * Function name: ConnectClient_set_connectTime
 * Description: Set start time of the connection.
 * Arguments: cc - pointer to ConnectClient structure
 *            connectTime - start time of the connection
 */

void
ConnectClient_set_connectTime(ConnectClient* cc, time_t connectTime)
{
  if (cc == NULL) {
    return;
  }
  cc->connectTime = connectTime;
}

/*
 * Function name: ConnectClient_set_sClientId
 * Description: Set client identification string.
 * Arguments: cc - pointer to ConnectClient structure
 *            sClientId - client identification string
 */

void
ConnectClient_set_sClientId(ConnectClient* cc, char* sClientId)
{
  if (cc == NULL) {
    return;
  }
  string_cp(&(cc->sClientId), sClientId);
}

/*
 * Function name: ConnectClient_set_nameBuf
 * Description: Set name of the client.
 * Arguments: cc - pointer to ConnectClient structure
 *            nameBuf - name of the client
 */

void
ConnectClient_set_nameBuf(ConnectClient* cc, char* nameBuf)
{
  if (cc == NULL) {
    return;
  }
  memset(cc->nameBuf, 0, 128);
  strncpy(cc->nameBuf, nameBuf, 127);
}

/*
 * Function name: ConnectClient_set_portBuf
 * Description: Set port from which client is connected.
 * Arguments: cc - pointer to ConnectClient structure
 *            portBuf - port from which client is connected
 */

void
ConnectClient_set_portBuf(ConnectClient* cc, char* portBuf)
{
  if (cc == NULL) {
    return;
  }
  memset(cc->portBuf, 0, 7);
  strncpy(cc->portBuf, portBuf, 6);
}

/*
 * Function name: ConnectClient_set_tunnelType
 * Description: Set tupe of the client tunnel.
 * Arguments: cc - pointer to ConnectClient structure
 *            tunnelType - tupe of the client tunnel
 */

void
ConnectClient_set_tunnelType(ConnectClient* cc, char tunnelType)
{
  if (cc == NULL) {
    return;
  }
  cc->tunnelType = tunnelType;
}

/*
 * Function name: ConnectClient_set_auditList
 * Description: Set audit list for the audit feature.
 * Arguments: cc - pointer to ConnectClient structure
 *            al - audit list for the audit feature
 */

void
ConnectClient_set_auditList(ConnectClient* cc, AuditList* al)
{
  AuditList* altmp;
  if (cc == NULL) {
    return;
  }
  altmp = ConnectClient_get_auditList(cc);
  AuditList_free(&altmp);
  cc->auditList = al;
}

/*
 * Function name: ConnectClient_set_header
 * Description: Set header buffer for incomplete headers.
 * Arguments: cc - pointer to ConnectClient structure
 *            hb - header buffer for incomplete headers
 */

void
ConnectClient_set_header(ConnectClient* cc, HeaderBuffer* hb)
{
  HeaderBuffer* hbtmp;
  if (cc == NULL) {
    return;
  }
  hbtmp = ConnectClient_get_header(cc);
  HeaderBuffer_free(&hbtmp);
  cc->header = hb;
}

/*
 * Function name: ConnectClient_get_state
 * Description: Get state of the connected client.
 * Arguments: cc - pointer to ConnectClient structure
 * Returns: State of the connected client.
 */

char
ConnectClient_get_state(ConnectClient* cc)
{
  if (cc == NULL) {
    return CONNECTCLIENT_STATE_UNKNOWN;
  }
  return cc->state;
}

/*
 * Function name: ConnectClient_get_sslFd
 * Description: Get SslFd structure.
 * Arguments: cc - pointer to ConnectClient structure
 * Returns: SslFd structure.
 */

SslFd*
ConnectClient_get_sslFd(ConnectClient* cc)
{
  if (cc == NULL) {
    return NULL;
  }
  return cc->sslFd;
}

/*
 * Function name: ConnectClient_get_timer
 * Description: Get timer of the client used for internal time counting. 
 * Arguments: cc - pointer to ConnectClient structure
 * Returns: Timer of the client used for internal time counting.
 */

struct timeval
ConnectClient_get_timer(ConnectClient* cc)
{
  if (cc == NULL) {
    return timeval_create(0, 0);
  }
  return cc->timer;
}

/*
 * Function name: ConnectClient_get_users
 * Description: Get users descriptor table.
 * Arguments: cc - pointer to ConnectClient structure
 * Returns: Users descriptor table.
 */

int*
ConnectClient_get_users(ConnectClient* cc)
{
  if (cc == NULL) {
    return NULL;
  }
  return cc->users;
}

/*
 * Function name: ConnectClient_get_connected
 * Description: Get number of connected users.
 * Arguments: cc - pointer to ConnectClient structure
 * Returns: Number of connected users.
 */

int
ConnectClient_get_connected(ConnectClient* cc)
{
  if (cc == NULL) {
    return -1;
  }
  return cc->connected;
}

/*
 * Function name: ConnectClient_get_limit
 * Description: Get limit of connected users.
 * Arguments: cc - pointer to ConnectClient structure
 * Returns: Limit of connected users.
 */

int
ConnectClient_get_limit(ConnectClient* cc)
{
  if (cc == NULL) {
    return -1;
  }
  return cc->limit;
}

/*
 * Function name: ConnectClient_get_listenFd
 * Description: Get listen socket descriptor.
 * Arguments: cc - pointer to ConnectClient structure
 * Returns: Listen socket descriptor.
 */

int
ConnectClient_get_listenFd(ConnectClient* cc)
{
  if (cc == NULL) {
    return -1;
  }
  return cc->listenFd;
}

/*
 * Function name: ConnectClient_get_usrCliPair
 * Description: Get user-client pair number.
 * Arguments: cc - pointer to ConnectClient structure
 * Returns: User-client pair number.
 */

int
ConnectClient_get_usrCliPair(ConnectClient* cc)
{
  if (cc == NULL) {
    return -1;
  }
  return cc->usrCliPair;
}

/*
 * Function name: ConnectClient_get_clientId
 * Description: Get client identification number.
 * Arguments: cc - pointer to ConnectClient structure
 * Returns: Client identification number.
 */

int
ConnectClient_get_clientId(ConnectClient* cc)
{
  if (cc == NULL) {
    return -1;
  }
  return cc->clientId;
}

/*
 * Function name: ConnectClient_get_connectTime
 * Description: Get start time of the connection.
 * Arguments: cc - pointer to ConnectClient structure
 * Returns: Start time of the connection.
 */

time_t
ConnectClient_get_connectTime(ConnectClient* cc)
{
  if (cc == NULL) {
    return 0;
  }
  return cc->connectTime;
}

/*
 * Function name: ConnectClient_get_sClientId
 * Description: Get client identification string.
 * Arguments: cc - pointer to ConnectClient structure
 * Returns: Client identification string.
 */

char*
ConnectClient_get_sClientId(ConnectClient* cc)
{
  if (cc == NULL) {
    return NULL;
  }
  return cc->sClientId;
}

/*
 * Function name: ConnectClient_get_nameBuf
 * Description: Get name of the client.
 * Arguments: cc - pointer to ConnectClient structure
 * Returns: Name of the client.
 */

char*
ConnectClient_get_nameBuf(ConnectClient* cc)
{
  if (cc == NULL) {
    return NULL;
  }
  return cc->nameBuf;
}

/*
 * Function name: ConnectClient_get_portBuf
 * Description: Get port from which client is connected.
 * Arguments: cc - pointer to ConnectClient structure
 * Returns: Port from which client is connected.
 */

char*
ConnectClient_get_portBuf(ConnectClient* cc)
{
  if (cc == NULL) {
    return NULL;
  }
  return cc->portBuf;
}

/*
 * Function name: ConnectClient_get_tunnelType
 * Description: Get tupe of the client tunnel.
 * Arguments: cc - pointer to ConnectClient structure
 * Returns: Tupe of the client tunnel.
 */

char
ConnectClient_get_tunnelType(ConnectClient* cc)
{
  if (cc == NULL) {
    return CONNECTCLIENT_TUNNELTYPE_UNKNOWN;
  }
  return cc->tunnelType;
}

/*
 * Function name: ConnectClient_get_auditList
 * Description: Get audit list for the audit feature.
 * Arguments: cc - pointer to ConnectClient structure
 * Returns: Audit list for the audit feature.
 */

AuditList*
ConnectClient_get_auditList(ConnectClient* cc)
{
  if (cc == NULL) {
    return NULL;
  }
  return cc->auditList;
}

/*
 * Function name: ConnectClient_get_header
 * Description: Get header buffer for incomplete headers.
 * Arguments: cc - pointer to ConnectClient structure
 * Returns: Header buffer for incomplete headers.
 */

HeaderBuffer*
ConnectClient_get_header(ConnectClient* cc)
{
  if (cc == NULL) {
    return NULL;
  }
  return cc->header;
}

/*
 * Function name: ConnectClient_create_users
 * Description: Create user descriptor table. Memory for the table is allocated according
 *              to the previously set 'limit' value. All the descriptors are set to -1.
 * Arguments: cc - pointer to ConnectClient structure
 * Returns: >0 - there were some problems with memory allocation
 *          0 - everything was ok
 */

int
ConnectClient_create_users(ConnectClient* cc)
{
  int i, limit;
  if (cc == NULL) {
    return 1;
  }
  limit = ConnectClient_get_limit(cc);
  if (limit == -1) {
    return 2;
  }
  cc->users = malloc(limit * sizeof(int));
  if (cc->users == NULL) {
    return 3;
  }
  for (i = 0; i < limit; ++i) {
    cc->users[i] = -1;
  }
  return 0;
}

/*
 * Function name: ConnectClient_get_timerp
 * Description: Get pointer to timer of the client used for internal time counting. 
 * Arguments: cc - pointer to ConnectClient structure
 * Returns: Pointer to timer of the client used for internal time counting.
 */

struct timeval*
ConnectClient_get_timerp(ConnectClient* cc)
{
  if (cc == NULL) {
    return NULL;
  }
  return (&(cc->timer));
}

/*
 * Function name: ConnectClient_increase_connected
 * Description: Increase number of connected users.
 * Arguments: cc - pointer to ConnectClient structure
 */

void
ConnectClient_increase_connected(ConnectClient* cc)
{
  if (cc == NULL) {
    return;
  }
  ConnectClient_set_connected(cc, ConnectClient_get_connected(cc) + 1);
}

/*
 * Function name: ConnectClient_decrease_connected
 * Description: Decrease number of connected users.
 * Arguments: cc - pointer to ConnectClient structure
 */

void
ConnectClient_decrease_connected(ConnectClient* cc)
{
  if (cc == NULL) {
    return;
  }
  ConnectClient_set_connected(cc, ConnectClient_get_connected(cc) - 1);
}

/*
 * Function name: ConnectClient_get_listenFdp
 * Description: Get pointer to listen socket descriptor.
 * Arguments: cc - pointer to ConnectClient structure
 * Returns: Pointer to listen socket descriptor.
 */

int*
ConnectClient_get_listenFdp(ConnectClient* cc)
{
  if (cc == NULL) {
    return NULL;
  }
  return (&(cc->listenFd));
}