summaryrefslogtreecommitdiffstats
path: root/xrdp/xrdp_listen.c
blob: 1e3993685a19b40040a482f157f605baad5fdb55 (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
/*
   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-2005

   listen for incoming connection

*/

#include "xrdp.h"

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;
  return self;
}

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

/*****************************************************************************/
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 */
int APP_CC
xrdp_listen_add_pro(struct xrdp_listen* self)
{
  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] = g_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] = g_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\n\r"));
      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)
{
  DEBUG(("process started\n\r"));
  xrdp_process_main_loop(g_process);
  DEBUG(("process done\n\r"));
  return 0;
}

/*****************************************************************************/
/* wait for incoming connections */
int APP_CC
xrdp_listen_main_loop(struct xrdp_listen* self)
{
  int error;

  self->status = 1;
  self->sck = g_tcp_socket();
  g_tcp_set_non_blocking(self->sck);
  error = g_tcp_bind(self->sck, "3389");
  if (error != 0)
  {
    error = g_tcp_bind(self->sck, "3390");
  }
  if (error != 0)
  {
    g_printf("bind error in xrdp_listen_main_loop\n\r");
    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);
      }
      else if (error == -1)
      {
        break;
      }
      else
      {
        g_process = xrdp_process_create(self);
        if (xrdp_listen_add_pro(self) == 0)
        {
          /* start thread */
          g_process->sck = error;
          g_thread_create(xrdp_process_run, 0);
          g_sleep(100);
        }
        else
        {
          xrdp_process_delete(g_process);
        }
      }
    }
  }
  else
  {
    DEBUG(("listen error in xrdp_listen_main_loop\n\r"));
  }
  xrdp_listen_term_processes(self);
  g_tcp_close(self->sck);
  self->status = -1;
  return 0;
}