summaryrefslogtreecommitdiffstats
path: root/xrdpapi/simple.c
blob: afe1a22c410ab284f68f3c491200deb03554bf1e (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
/**
 * xrdp: A Remote Desktop Protocol server.
 *
 * Copyright (C) Jay Sorg 2012
 * Copyright (C) Laxmikant Rashinkar 2012
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * sample program to demonstrate use of xrdpapi
 *
 */

/*
 * build instructions:
 *     gcc simple.c -o simple -L./.libs -lxrdpapi
 */

#ifdef __WIN32__
#include <mstsapi.h>
#endif

#include "xrdpapi.h"
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

/* forward declarations */
int run_echo_test();
int run_tsmf_test();

int
main(int argc, char **argv)
{
    if (argc < 2)
    {
        printf("usage: simple <echo|tsmf>\n");
        return 1;
    }

    if (strcasecmp(argv[1], "echo") == 0)
    {
        return run_echo_test();
    }
    else if (strcasecmp(argv[1], "tsmf") == 0)
    {
        return run_tsmf_test();
    }
    else
    {
        printf("usage: simple <echo|tsmf>\n");
        return 1;
    }
}

/**
 * perform an ECHO test with a Microsoft Windows RDP client
 *
 * A Microsoft Windows RDP client echos data written
 * to a dynamic virtual channel named ECHO
 *
 * NOTE: THIS TEST WILL FAIL IF CONNECTED FROM A NON
 *       WINDOWS RDP CLIENT
 *
 * @return 0 on success, -1 on failure
 */
int
run_echo_test()
{
    char    out_buf[8192];
    char    in_buf[1700];
    void   *channel;
    int     bytes_left;
    int     rv;
    int     count;
    int     pkt_count;
    int     i;
    int     bytes_written;
    int     bytes_read;

    unsigned char c;
    unsigned char *rd_ptr;
    unsigned char *wr_ptr;

    /* fill out_buf[] with incremental values */
    for (i = 0, c = 0; i < 8192; i++, c++)
    {
        out_buf[i] = c;
    }

    /* open a virtual channel named ECHO */
    channel = WTSVirtualChannelOpenEx(WTS_CURRENT_SESSION, "ECHO", WTS_CHANNEL_OPTION_DYNAMIC_PRI_LOW);

    if (channel == NULL)
    {
        printf("###  WTSVirtualChannelOpenEx() failed!\n");
        return -1;
    }

    bytes_left = 8192;
    wr_ptr = out_buf;
    rd_ptr = out_buf;
    pkt_count = 1;

    while (bytes_left > 0)
    {
        /* write data to virtual channel */
        count = (bytes_left > 1700) ? 1700 : bytes_left;
        rv = WTSVirtualChannelWrite(channel, wr_ptr, count, &bytes_written);

        if ((rv == 0) || (bytes_written == 0))
        {
            printf("### WTSVirtualChannelWrite() failed\n");
            return -1;
        }

        count = bytes_written;

        while (count)
        {
            /* read back the echo */
            rv = WTSVirtualChannelRead(channel, 5000, in_buf, count, &bytes_read);

            if ((rv == 0) || (bytes_read == 0))
            {
                printf("### WTSVirtualChannelRead() failed\n");
                return -1;
            }

            /* validate the echo */
            for (i = 0; i < bytes_read; i++, rd_ptr++)
            {
                if (*rd_ptr != (unsigned char) in_buf[i])
                {
                    printf("### data mismatch: expected 0x%x got 0x%x\n",
                           (unsigned char) *rd_ptr, (unsigned char) in_buf[i]);
                    return -1;
                }
            }

            count -= bytes_read;
        }

        bytes_left -= bytes_written;
        wr_ptr += bytes_written;
        printf("### pkt %d passed echo test\n", pkt_count++);
    }

    WTSVirtualChannelClose(channel);
    return 0;
}

int
run_tsmf_test()
{
    void *channel;

    printf("this test not yet implemented!\n");
    return 1;

    /* open virtual channel */
    channel = WTSVirtualChannelOpenEx(WTS_CURRENT_SESSION, "TSMF", WTS_CHANNEL_OPTION_DYNAMIC_PRI_LOW);

    if (channel == NULL)
    {
        printf("###  WTSVirtualChannelOpenEx() failed!\n");
        return 1;
    }

    WTSVirtualChannelClose(channel);
    return 0;
}