summaryrefslogtreecommitdiffstats
path: root/xrdp/xrdp_listen.c
blob: 406f6383943be73e5786b489e411878ae67a976f (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
/*
   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., 675 Mass Ave, Cambridge, MA 02139, USA.

   xrdp: A Remote Desktop Protocol server.
   Copyright (C) Jay Sorg 2004-2008

   listen for incoming connection

*/

#include "xrdp.h"

/* 'g_process' is protected by the semaphore 'g_process_sem'.  One thread sets
   g_process and waits for the other to process it */
static tbus g_process_sem = 0;
static struct xrdp_process* g_process = 0;

/*****************************************************************************/
struct xrdp_listen* APP_CC
xrdp_listen_create(void)
{
  struct xrdp_listen* self;

  self = (struct xrdp_listen*)g_malloc(sizeof(struct xrdp_listen), 1);
  self->process_list_max = 100;
  g_process_sem = tc_sem_create(0);
  return self;
}

/*****************************************************************************/
void APP_CC
xrdp_listen_delete(struct xrdp_listen* self)
{
  tc_sem_delete(g_process_sem);
  g_free(self);
}

/*****************************************************************************/
static int APP_CC
xrdp_listen_term_processes(struct xrdp_listen* self)
{
  int i;

  /* tell all xrdp processes to end */
  for (i = 0; i < self->process_list_count; i++)
  {
    if (self->process_list[i] != 0)
    {
      self->process_list[i]->term = 1;
    }
  }
  /* make sure they are done */
  for (i = 0; i < self->process_list_count; i++)
  {
    if (self->process_list[i] != 0)
    {
      while (self->process_list[i]->status > 0)
      {
        g_sleep(10);
      }
    }
  }
  /* free them all */
  for (i = 0; i < self->process_list_count; i++)
  {
    if (self->process_list[i] != 0)
    {
      xrdp_process_delete(self->process_list[i]);
    }
  }
  return 0;
}

/*****************************************************************************/
/* returns error */
static int APP_CC
xrdp_listen_add_pro(struct xrdp_listen* self, struct xrdp_process* process)
{
  int i;

  for (i = 0; i < self->process_list_max; i++)
  {
    /* add process in new slot */
    if (self->process_list[i] == 0)
    {
      self->process_list[i] = process;
      self->process_list_count++;
      return 0;
    }
    /* add process in unused slot */
    /* this shouldn't happen */
    if (self->process_list[i]->status < 0)
    {
      xrdp_process_delete(self->process_list[i]);
      self->process_list[i] = process;
      return 0;
    }
  }
  return 1;
}

/*****************************************************************************/
int APP_CC
xrdp_listen_delete_pro(struct xrdp_listen* self, struct xrdp_process* pro)
{
  int i;

  for (i = 0; i < self->process_list_max; i++)
  {
    if (self->process_list[i] == pro)
    {
      DEBUG(("process deleted"));
      xrdp_process_delete(pro);
      self->process_list[i] = 0;
      return 0;
    }
  }
  return 0;
}

/*****************************************************************************/
/* i can't get stupid in_val to work, hum using global var for now */
THREAD_RV THREAD_CC
xrdp_process_run(void* in_val)
{
  struct xrdp_process* process;

  DEBUG(("process started"));
  process = g_process;
  g_process = 0;
  tc_sem_inc(g_process_sem);
  xrdp_process_main_loop(process);
  DEBUG(("process done"));
  return 0;
}

/*****************************************************************************/
static int
xrdp_listen_get_port(char* port, int port_bytes)
{
  int fd;
  int error;
  int index;
  char* val;
  struct list* names;
  struct list* values;

  /* default to port 3389 */
  g_strncpy(port, "3389", port_bytes - 1);
  /* see if port is in xrdp.ini file */
  fd = g_file_open(XRDP_CFG_FILE);
  if (fd > 0)
  {
    names = list_create();
    names->auto_free = 1;
    values = list_create();
    values->auto_free = 1;
    if (file_read_section(fd, "globals", names, values) == 0)
    {
      for (index = 0; index < names->count; index++)
      {
        val = (char*)list_get_item(names, index);
        if (val != 0)
        {
          if (g_strcasecmp(val, "port") == 0)
          {
            val = (char*)list_get_item(values, index);
            error = g_atoi(val);
            if ((error > 0) && (error < 65000))
            {
              g_strncpy(port, val, port_bytes - 1);
            }
            break;
          }
        }
      }
    }
    list_delete(names);
    list_delete(values);
    g_file_close(fd);
  }
  return 0;
}

/*****************************************************************************/
/* wait for incoming connections */
int APP_CC
xrdp_listen_main_loop(struct xrdp_listen* self)
{
  int error;
  char port[8];

  self->status = 1;
  xrdp_listen_get_port(port, sizeof(port));
  self->sck = g_tcp_socket();
  g_tcp_set_non_blocking(self->sck);
  error = g_tcp_bind(self->sck, port);
  if (error != 0)
  {
    g_writeln("bind error in xrdp_listen_main_loop");
    g_tcp_close(self->sck);
    self->status = -1;
    return 1;
  }
  error = g_tcp_listen(self->sck);
  if (error == 0)
  {
    while (!g_is_term() && !self->term)
    {
      error = g_tcp_accept(self->sck);
      if ((error == -1) && g_tcp_last_error_would_block(self->sck))
      {
        g_sleep(100);
        g_loop();
      }
      else if (error == -1)
      {
        break;
      }
      else
      {
        g_process = xrdp_process_create(self);
        if (xrdp_listen_add_pro(self, g_process) == 0)
        {
          /* start thread */
          g_process->sck = error;
          tc_thread_create(xrdp_process_run, 0);
          tc_sem_dec(g_process_sem); /* this will wait */
        }
        else
        {
          xrdp_process_delete(g_process);
        }
      }
    }
  }
  else
  {
    DEBUG(("listen error in xrdp_listen_main_loop"));
  }
  xrdp_listen_term_processes(self);
  g_tcp_close(self->sck);
  self->status = -1;
  return 0;
}