1 /**
2  * xrdp: A Remote Desktop Protocol server.
3  *
4  * Copyright (C) Jay Sorg 2004-2014
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * generic transport
19  */
20 
21 #if !defined(TRANS_H)
22 #define TRANS_H
23 
24 #include "arch.h"
25 #include "parse.h"
26 
27 #define TRANS_MODE_TCP 1 /* tcp6 if defined, else tcp4 */
28 #define TRANS_MODE_UNIX 2
29 #define TRANS_MODE_VSOCK 3
30 #define TRANS_MODE_TCP4 4 /* tcp4 only */
31 #define TRANS_MODE_TCP6 6 /* tcp6 only */
32 
33 #define TRANS_TYPE_LISTENER 1
34 #define TRANS_TYPE_SERVER 2
35 #define TRANS_TYPE_CLIENT 3
36 
37 #define TRANS_STATUS_DOWN 0
38 #define TRANS_STATUS_UP 1
39 
40 struct trans; /* forward declaration */
41 struct xrdp_tls;
42 
43 typedef int (*ttrans_data_in)(struct trans *self);
44 typedef int (*ttrans_conn_in)(struct trans *self,
45                               struct trans *new_self);
46 typedef int (*tis_term)(void);
47 typedef int (*trans_recv_proc) (struct trans *self, char *ptr, int len);
48 typedef int (*trans_send_proc) (struct trans *self, const char *data, int len);
49 typedef int (*trans_can_recv_proc) (struct trans *self, int sck, int millis);
50 
51 /* optional source info */
52 
53 enum xrdp_source
54 {
55     XRDP_SOURCE_NONE = 0,
56     XRDP_SOURCE_CLIENT,
57     XRDP_SOURCE_SESMAN,
58     XRDP_SOURCE_CHANSRV,
59     XRDP_SOURCE_MOD,
60 
61     XRDP_SOURCE_MAX_COUNT
62 };
63 
64 /*
65  * @brief Provide flow control mechanism for (primarily) xrdp
66  *
67  * There is one of these data structures per-program.
68  *
69  * While input is being read from a 'struct trans' and processed, the
70  * cur_source member is set to the my_source member from the transport.
71  * During this processing, trans_write_copy() may be called to send output
72  * on another struct trans. If this happens, and the ouput needs to be
73  * buffered, trans_write_copy() can add the number of bytes generated by
74  * the input trans to the source field for the cur_source. This allows us to
75  * see how much output has been buffered for each input source.
76  *
77  * When the program assembles 'struct trans' objects to scan for input
78  * (normally in trans_get_wait_objs()), it is able to see how much buffered
79  * output is registered for each input. Inputs which have too much buffered
80  * output owing are skipped, and not considered for input.
81  *
82  * This provides a simple means of providing back-pressure on an input
83  * where the data it is providing is being processed and then sent out on
84  * a much slower link.
85  */
86 struct source_info
87 {
88     enum xrdp_source cur_source;
89     int source[XRDP_SOURCE_MAX_COUNT];
90 };
91 
92 struct trans
93 {
94     tbus sck; /* socket handle */
95     int mode; /* 1 tcp, 2 unix socket, 3 vsock */
96     int status;
97     int type1; /* 1 listener 2 server 3 client */
98     ttrans_data_in trans_data_in;
99     ttrans_conn_in trans_conn_in;
100     void *callback_data;
101     int header_size;
102     struct stream *in_s;
103     struct stream *out_s;
104     char *listen_filename;
105     tis_term is_term; /* used to test for exit */
106     struct stream *wait_s;
107     char addr[256];
108     char port[256];
109     int no_stream_init_on_data_in;
110     int extra_flags; /* user defined */
111     struct ssl_tls *tls;
112     const char *ssl_protocol; /* e.g. TLSv1, TLSv1.1, TLSv1.2, unknown */
113     const char *cipher_name;  /* e.g. AES256-GCM-SHA384 */
114     trans_recv_proc trans_recv;
115     trans_send_proc trans_send;
116     trans_can_recv_proc trans_can_recv;
117     struct source_info *si;
118     enum xrdp_source my_source;
119 };
120 
121 struct trans *
122 trans_create(int mode, int in_size, int out_size);
123 void
124 trans_delete(struct trans *self);
125 void
126 trans_delete_from_child(struct trans *self);
127 int
128 trans_get_wait_objs(struct trans *self, tbus *objs, int *count);
129 int
130 trans_get_wait_objs_rw(struct trans *self,
131                        tbus *robjs, int *rcount,
132                        tbus *wobjs, int *wcount, int *timeout);
133 int
134 trans_check_wait_objs(struct trans *self);
135 int
136 trans_force_read_s(struct trans *self, struct stream *in_s, int size);
137 int
138 trans_force_write_s(struct trans *self, struct stream *out_s);
139 int
140 trans_force_read(struct trans *self, int size);
141 int
142 trans_force_write(struct trans *self);
143 int
144 trans_write_copy(struct trans *self);
145 int
146 trans_write_copy_s(struct trans *self, struct stream *out_s);
147 int
148 trans_connect(struct trans *self, const char *server, const char *port,
149               int timeout);
150 int
151 trans_listen_address(struct trans *self, const char *port, const char *address);
152 int
153 trans_listen(struct trans *self, const char *port);
154 struct stream *
155 trans_get_in_s(struct trans *self);
156 struct stream *
157 trans_get_out_s(struct trans *self, int size);
158 int
159 trans_set_tls_mode(struct trans *self, const char *key, const char *cert,
160                    long ssl_protocols, const char *tls_ciphers);
161 int
162 trans_shutdown_tls_mode(struct trans *self);
163 int
164 trans_tcp_force_read_s(struct trans *self, struct stream *in_s, int size);
165 
166 #endif
167