summaryrefslogtreecommitdiffstats
path: root/common/trans.c
diff options
context:
space:
mode:
Diffstat (limited to 'common/trans.c')
-rw-r--r--common/trans.c159
1 files changed, 133 insertions, 26 deletions
diff --git a/common/trans.c b/common/trans.c
index aced0667..5503ea61 100644
--- a/common/trans.c
+++ b/common/trans.c
@@ -1,7 +1,7 @@
/**
* xrdp: A Remote Desktop Protocol server.
*
- * Copyright (C) Jay Sorg 2004-2013
+ * Copyright (C) Jay Sorg 2004-2014
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -24,12 +24,49 @@
#include "parse.h"
/*****************************************************************************/
-struct trans *APP_CC
+int APP_CC
+trans_tls_recv(struct trans *self, void *ptr, int len)
+{
+ if (self->tls == NULL)
+ {
+ return 1;
+ }
+ return xrdp_tls_read(self->tls, ptr, len);
+}
+
+/*****************************************************************************/
+int APP_CC
+trans_tls_send(struct trans *self, const void *data, int len)
+{
+ if (self->tls == NULL)
+ {
+ return 1;
+ }
+ return xrdp_tls_write(self->tls, data, len);
+}
+
+/*****************************************************************************/
+int APP_CC
+trans_tcp_recv(struct trans *self, void *ptr, int len)
+{
+ return g_tcp_recv(self->sck, ptr, len, 0);
+}
+
+/*****************************************************************************/
+int APP_CC
+trans_tcp_send(struct trans *self, const void *data, int len)
+{
+ return g_tcp_send(self->sck, data, len, 0);
+}
+
+/*****************************************************************************/
+struct trans *
+APP_CC
trans_create(int mode, int in_size, int out_size)
{
- struct trans *self = (struct trans *)NULL;
+ struct trans *self = (struct trans *) NULL;
- self = (struct trans *)g_malloc(sizeof(struct trans), 1);
+ self = (struct trans *) g_malloc(sizeof(struct trans), 1);
if (self != NULL)
{
@@ -38,6 +75,10 @@ trans_create(int mode, int in_size, int out_size)
make_stream(self->out_s);
init_stream(self->out_s, out_size);
self->mode = mode;
+ self->tls = 0;
+ /* assign tcp calls by default */
+ self->trans_recv = trans_tcp_recv;
+ self->trans_send = trans_tcp_send;
}
return self;
@@ -68,6 +109,11 @@ trans_delete(struct trans *self)
g_free(self->listen_filename);
}
+ if (self->tls != 0)
+ {
+ xrdp_tls_delete(self->tls);
+ }
+
g_free(self);
}
@@ -92,8 +138,7 @@ trans_get_wait_objs(struct trans *self, tbus *objs, int *count)
/*****************************************************************************/
int APP_CC
-trans_get_wait_objs_rw(struct trans *self,
- tbus *robjs, int *rcount,
+trans_get_wait_objs_rw(struct trans *self, tbus *robjs, int *rcount,
tbus *wobjs, int *wcount)
{
if (self == 0)
@@ -138,7 +183,7 @@ send_waiting(struct trans *self, int block)
if (g_tcp_can_send(self->sck, timeout))
{
bytes = (int) (temp_s->end - temp_s->p);
- sent = g_tcp_send(self->sck, temp_s->p, bytes, 0);
+ sent = self->trans_send(self, temp_s->p, bytes);
if (sent > 0)
{
temp_s->p += sent;
@@ -174,8 +219,8 @@ send_waiting(struct trans *self, int block)
int APP_CC
trans_check_wait_objs(struct trans *self)
{
- tbus in_sck = (tbus)0;
- struct trans *in_trans = (struct trans *)NULL;
+ tbus in_sck = (tbus) 0;
+ struct trans *in_trans = (struct trans *) NULL;
int read_bytes = 0;
int to_read = 0;
int read_so_far = 0;
@@ -224,8 +269,10 @@ trans_check_wait_objs(struct trans *self)
in_trans->type1 = TRANS_TYPE_SERVER;
in_trans->status = TRANS_STATUS_UP;
in_trans->is_term = self->is_term;
- g_strncpy(in_trans->addr, self->addr, sizeof(self->addr) - 1);
- g_strncpy(in_trans->port, self->port, sizeof(self->port) - 1);
+ g_strncpy(in_trans->addr, self->addr,
+ sizeof(self->addr) - 1);
+ g_strncpy(in_trans->port, self->port,
+ sizeof(self->port) - 1);
if (self->trans_conn_in(self, in_trans) != 0)
{
@@ -243,12 +290,12 @@ trans_check_wait_objs(struct trans *self)
{
if (g_tcp_can_recv(self->sck, 0))
{
- read_so_far = (int)(self->in_s->end - self->in_s->data);
+ read_so_far = (int) (self->in_s->end - self->in_s->data);
to_read = self->header_size - read_so_far;
if (to_read > 0)
{
- read_bytes = g_tcp_recv(self->sck, self->in_s->end, to_read, 0);
+ read_bytes = self->trans_recv(self, self->in_s->end, to_read);
if (read_bytes == -1)
{
@@ -275,7 +322,7 @@ trans_check_wait_objs(struct trans *self)
}
}
- read_so_far = (int)(self->in_s->end - self->in_s->data);
+ read_so_far = (int) (self->in_s->end - self->in_s->data);
if (read_so_far == self->header_size)
{
@@ -299,7 +346,6 @@ trans_check_wait_objs(struct trans *self)
return rv;
}
-
/*****************************************************************************/
int APP_CC
trans_force_read_s(struct trans *self, struct stream *in_s, int size)
@@ -318,7 +364,9 @@ trans_force_read_s(struct trans *self, struct stream *in_s, int size)
{
return 1;
}
- rcvd = g_tcp_recv(self->sck, in_s->end, size, 0);
+
+ rcvd = self->trans_recv(self, in_s->end, size);
+
if (rcvd == -1)
{
if (g_tcp_last_error_would_block(self->sck))
@@ -380,7 +428,7 @@ trans_force_write_s(struct trans *self, struct stream *out_s)
return 1;
}
- size = (int)(out_s->end - out_s->data);
+ size = (int) (out_s->end - out_s->data);
total = 0;
if (send_waiting(self, 1) != 0)
@@ -391,7 +439,7 @@ trans_force_write_s(struct trans *self, struct stream *out_s)
while (total < size)
{
- sent = g_tcp_send(self->sck, out_s->data + total, size - total, 0);
+ sent = self->trans_send(self, out_s->data + total, size - total);
if (sent == -1)
{
@@ -455,7 +503,7 @@ trans_write_copy(struct trans *self)
}
out_s = self->out_s;
- size = (int)(out_s->end - out_s->data);
+ size = (int) (out_s->end - out_s->data);
make_stream(wait_s);
init_stream(wait_s, size);
out_uint8a(wait_s, out_s->data, size);
@@ -501,12 +549,18 @@ trans_connect(struct trans *self, const char *server, const char *port,
if (self->mode == TRANS_MODE_TCP) /* tcp */
{
self->sck = g_tcp_socket();
+ if (self->sck < 0)
+ return 1;
+
g_tcp_set_non_blocking(self->sck);
error = g_tcp_connect(self->sck, server, port);
}
else if (self->mode == TRANS_MODE_UNIX) /* unix socket */
{
self->sck = g_tcp_local_socket();
+ if (self->sck < 0)
+ return 1;
+
g_tcp_set_non_blocking(self->sck);
error = g_tcp_local_connect(self->sck, port);
}
@@ -537,6 +591,10 @@ trans_connect(struct trans *self, const char *server, const char *port,
}
/*****************************************************************************/
+
+/**
+ * @return 0 on success, 1 on failure
+ */
int APP_CC
trans_listen_address(struct trans *self, char *port, const char *address)
{
@@ -548,6 +606,9 @@ trans_listen_address(struct trans *self, char *port, const char *address)
if (self->mode == TRANS_MODE_TCP) /* tcp */
{
self->sck = g_tcp_socket();
+ if (self->sck < 0)
+ return 1;
+
g_tcp_set_non_blocking(self->sck);
if (g_tcp_bind_address(self->sck, port, address) == 0)
@@ -565,7 +626,11 @@ trans_listen_address(struct trans *self, char *port, const char *address)
g_free(self->listen_filename);
self->listen_filename = 0;
g_file_delete(port);
+
self->sck = g_tcp_local_socket();
+ if (self->sck < 0)
+ return 1;
+
g_tcp_set_non_blocking(self->sck);
if (g_tcp_local_bind(self->sck, port) == 0)
@@ -574,7 +639,7 @@ trans_listen_address(struct trans *self, char *port, const char *address)
if (g_tcp_listen(self->sck) == 0)
{
- g_chmod_hex(port, 0xffff);
+ g_chmod_hex(port, 0x0660);
self->status = TRANS_STATUS_UP; /* ok */
self->type1 = TRANS_TYPE_LISTENER; /* listener */
return 0;
@@ -593,14 +658,15 @@ trans_listen(struct trans *self, char *port)
}
/*****************************************************************************/
-struct stream *APP_CC
+struct stream *
+APP_CC
trans_get_in_s(struct trans *self)
{
- struct stream *rv = (struct stream *)NULL;
+ struct stream *rv = (struct stream *) NULL;
if (self == NULL)
{
- rv = (struct stream *)NULL;
+ rv = (struct stream *) NULL;
}
else
{
@@ -611,14 +677,15 @@ trans_get_in_s(struct trans *self)
}
/*****************************************************************************/
-struct stream *APP_CC
+struct stream *
+APP_CC
trans_get_out_s(struct trans *self, int size)
{
- struct stream *rv = (struct stream *)NULL;
+ struct stream *rv = (struct stream *) NULL;
if (self == NULL)
{
- rv = (struct stream *)NULL;
+ rv = (struct stream *) NULL;
}
else
{
@@ -628,3 +695,43 @@ trans_get_out_s(struct trans *self, int size)
return rv;
}
+/*****************************************************************************/
+/* returns error */
+int APP_CC
+trans_set_tls_mode(struct trans *self, const char *key, const char *cert)
+{
+ self->tls = xrdp_tls_create(self, key, cert);
+ if (self->tls == NULL)
+ {
+ g_writeln("trans_set_tls_mode: xrdp_tls_create malloc error");
+ return 1;
+ }
+
+ if (xrdp_tls_accept(self->tls) != 0)
+ {
+ g_writeln("trans_set_tls_mode: xrdp_tls_accept failed");
+ return 1;
+ }
+
+ /* assign tls functions */
+ self->trans_recv = trans_tls_recv;
+ self->trans_send = trans_tls_send;
+
+ return 0;
+}
+/*****************************************************************************/
+/* returns error */
+int APP_CC
+trans_shutdown_tls_mode(struct trans *self)
+{
+ if (self->tls != NULL)
+ {
+ return xrdp_tls_disconnect(self->tls);
+ }
+
+ /* assign callback back to tcp cal */
+ self->trans_recv = trans_tcp_recv;
+ self->trans_send = trans_tcp_send;
+
+ return 0;
+}