summaryrefslogtreecommitdiff
path: root/src/server_remove.c
blob: c8f78f11ed15ec07c6377595eea71e12d46bb07d (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
/*
 * 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 <assert.h>

#include "server_remove.h"

/*
 * Function name: remove_client
 * Description: Removes the client.
 * Arguments: ptr - the server realm
 *            client - the client number
 *            set - the set of file descriptors for reading
 *            wset - the set of file descriptors for writing
 *            scheduler - the task scheduler
 */

void
remove_client(ServerRealm* ptr, int client, fd_set* set, fd_set* wset, TaskScheduler* scheduler)
{
  int i;
  Task* task;
  
  assert(ptr != NULL);
  assert(client >= 0);
  assert(set != NULL);
  assert(wset != NULL);
  
  if (ConnectClient_get_state(ServerRealm_get_clientsTable(ptr)[client]) == CONNECTCLIENT_STATE_ACCEPTED) {
    for (i = 0; i < ServerRealm_get_usersLimit(ptr); ++i) {
      if (ConnectUser_get_whatClient(ServerRealm_get_usersTable(ptr)[i]) == client) {
        if (ConnectUser_get_state(ServerRealm_get_usersTable(ptr)[i]) != S_STATE_CLEAR) {
          ConnectUser_set_state(ServerRealm_get_usersTable(ptr)[i], S_STATE_CLEAR);
          FD_CLR(ConnectUser_get_connFd(ServerRealm_get_usersTable(ptr)[i]), set);
          FD_CLR(ConnectUser_get_connFd(ServerRealm_get_usersTable(ptr)[i]), wset);
          close(ConnectUser_get_connFd(ServerRealm_get_usersTable(ptr)[i]));
          ServerRealm_decrease_connectedUsers(ptr);
        }
      }
    }
  }
  for (i = 0; i < ConnectClient_get_limit(ServerRealm_get_clientsTable(ptr)[client]); ++i) {
    ConnectClient_get_users(ServerRealm_get_clientsTable(ptr)[client])[i] = -1;
  }
  if ((ServerRealm_get_clientsLimit(ptr) != client) && (ServerRealm_get_basePortOn(ptr) == 1)) {
    close(ConnectClient_get_listenFd(ServerRealm_get_clientsTable(ptr)[client]));
    FD_CLR(ConnectClient_get_listenFd(ServerRealm_get_clientsTable(ptr)[client]), set);
  }
  ConnectClient_set_sClientId(ServerRealm_get_clientsTable(ptr)[client], NULL);
  ConnectClient_set_connected(ServerRealm_get_clientsTable(ptr)[client], 0);
  close(SslFd_get_fd(ConnectClient_get_sslFd(ServerRealm_get_clientsTable(ptr)[client])));
  FD_CLR(SslFd_get_fd(ConnectClient_get_sslFd(ServerRealm_get_clientsTable(ptr)[client])), set);
  if (scheduler) {
    if ((task = ConnectClient_get_task(ServerRealm_get_clientsTable(ptr)[client]))) {
      TaskScheduler_removeTask(scheduler, task);
      ConnectClient_set_task(ServerRealm_get_clientsTable(ptr)[client], NULL);
    }
  }
  SSL_clear(SslFd_get_ssl(ConnectClient_get_sslFd(ServerRealm_get_clientsTable(ptr)[client])));
  ConnectClient_set_state(ServerRealm_get_clientsTable(ptr)[client], CONNECTCLIENT_STATE_FREE);
  ServerRealm_decrease_connectedClients(ptr);
}

/*
 * Function name: remove_raclient
 * Description: Removes the remote admin client.
 * Arguments: ptr - the server realm
 *            client - the client number
 *            set - the set of file descriptors for reading
 *            wset - the set of file descriptors for writing
 *            scheduler - the task scheduler
 */

void
remove_raclient(ServerRealm* ptr, int client, fd_set* set, fd_set* wset, TaskScheduler* scheduler)
{
  int i;
  Task* task;
  
  assert(ptr != NULL);
  assert(client >= 0);
  assert(set != NULL);
  assert(wset != NULL);
  
  for (i = 0; i < ConnectClient_get_limit(ServerRealm_get_raClientsTable(ptr)[client]); ++i) {
    ConnectClient_get_users(ServerRealm_get_raClientsTable(ptr)[client])[i] = -1;
  }
  ConnectClient_set_sClientId(ServerRealm_get_raClientsTable(ptr)[client], NULL);
  ConnectClient_set_connected(ServerRealm_get_raClientsTable(ptr)[client], 0);
  close(SslFd_get_fd(ConnectClient_get_sslFd(ServerRealm_get_raClientsTable(ptr)[client])));
  FD_CLR(SslFd_get_fd(ConnectClient_get_sslFd(ServerRealm_get_raClientsTable(ptr)[client])), set);
  if (scheduler) {
    if ((task = ConnectClient_get_task(ServerRealm_get_raClientsTable(ptr)[client]))) {
      TaskScheduler_removeTask(scheduler, task);
      ConnectClient_set_task(ServerRealm_get_raClientsTable(ptr)[client], NULL);
    }
  }
  SSL_clear(SslFd_get_ssl(ConnectClient_get_sslFd(ServerRealm_get_raClientsTable(ptr)[client])));
  ServerRealm_decrease_connectedClients(ptr);
  if (ConnectClient_get_state(ServerRealm_get_raClientsTable(ptr)[client]) == CONNECTCLIENT_STATE_ACCEPTED) {
    ServerRealm_decrease_connectedRaClients(ptr);
  }
  ConnectClient_set_state(ServerRealm_get_raClientsTable(ptr)[client], CONNECTCLIENT_STATE_FREE);
}