/[rdesktop]/jpeg/rdesktop/trunk/rdesktop.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /jpeg/rdesktop/trunk/rdesktop.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 30 - (hide annotations)
Fri Sep 14 13:51:38 2001 UTC (22 years, 8 months ago) by matty
Original Path: sourceforge.net/trunk/rdesktop/rdesktop.c
File MIME type: text/plain
File size: 7150 byte(s)
Portability fixes, including elimination of variable argument macros.
Rudimentary configure script.
Miscellaneous cleanups.

1 matty 10 /*
2     rdesktop: A Remote Desktop Protocol client.
3     Entrypoint and utility functions
4 matty 30 Copyright (C) Matthew Chapman 1999-2001
5 matty 10
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10    
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     GNU General Public License for more details.
15    
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19     */
20    
21 matty 24 #include <stdlib.h> /* malloc realloc free */
22 matty 30 #include <stdarg.h> /* va_list va_start va_end */
23 matty 24 #include <unistd.h> /* read close getuid getgid getpid getppid gethostname */
24     #include <fcntl.h> /* open */
25     #include <pwd.h> /* getpwuid */
26     #include <sys/stat.h> /* stat */
27     #include <sys/time.h> /* gettimeofday */
28     #include <sys/times.h> /* times */
29 matty 10 #include "rdesktop.h"
30    
31     char username[16];
32     char hostname[16];
33 matty 30 int width;
34     int height;
35 matty 10 int keylayout = 0x409;
36 matty 28 BOOL bitmap_compression = True;
37 matty 29 BOOL sendmotion = True;
38 matty 10 BOOL orders = True;
39     BOOL licence = True;
40 matty 30 BOOL encryption = True;
41 matty 28 BOOL desktop_save = True;
42     BOOL fullscreen = False;
43 matty 10
44     /* Display usage information */
45 matty 25 static void
46     usage(char *program)
47 matty 10 {
48 matty 30 printf("Usage: %s [options] server\n", program);
49     printf(" -u: user name\n");
50     printf(" -d: domain\n");
51     printf(" -s: shell\n");
52     printf(" -c: working directory\n");
53     printf(" -p: password (autologon)\n");
54     printf(" -n: client hostname\n");
55     printf(" -k: keyboard layout (hex)\n");
56     printf(" -g: desktop geometry (WxH)\n");
57     printf(" -f: full-screen mode\n");
58     printf(" -b: force bitmap updates\n");
59     printf(" -e: disable encryption (French TS)\n");
60     printf(" -m: do not send motion events\n");
61     printf(" -l: do not request licence\n\n");
62 matty 10 }
63    
64     /* Client program */
65 matty 25 int
66     main(int argc, char *argv[])
67 matty 10 {
68 matty 30 char fullhostname[64];
69 matty 21 char domain[16];
70     char password[16];
71     char shell[32];
72     char directory[32];
73 matty 10 char title[32];
74 matty 30 struct passwd *pw;
75     char *server, *p;
76     uint32 flags;
77 matty 10 int c;
78    
79 matty 30 printf("rdesktop: A Remote Desktop Protocol client.\n");
80     printf("Version " VERSION ". Copyright (C) 1999-2001 Matt Chapman.\n");
81     printf("See http://www.rdesktop.org/ for more information.\n\n");
82 matty 10
83 matty 21 flags = RDP_LOGON_NORMAL;
84     domain[0] = password[0] = shell[0] = directory[0] = 0;
85    
86 matty 30 while ((c = getopt(argc, argv, "u:d:s:c:p:n:k:g:fbemlh?")) != -1)
87 matty 10 {
88     switch (c)
89     {
90     case 'u':
91 matty 30 STRNCPY(username, optarg, sizeof(username));
92 matty 10 break;
93    
94 matty 21 case 'd':
95 matty 30 STRNCPY(domain, optarg, sizeof(domain));
96 matty 21 break;
97    
98     case 's':
99 matty 30 STRNCPY(shell, optarg, sizeof(shell));
100 matty 21 break;
101    
102     case 'c':
103 matty 30 STRNCPY(directory, optarg, sizeof(directory));
104 matty 21 break;
105    
106 matty 30 case 'p':
107     STRNCPY(password, optarg, sizeof(password));
108     flags |= RDP_LOGON_AUTO;
109     break;
110    
111 matty 10 case 'n':
112 matty 30 STRNCPY(hostname, optarg, sizeof(hostname));
113 matty 10 break;
114    
115     case 'k':
116     keylayout = strtol(optarg, NULL, 16);
117 matty 28 if (keylayout == 0)
118 matty 30 {
119     error("invalid keyboard layout\n");
120     return 1;
121     }
122 matty 10 break;
123    
124 matty 30 case 'g':
125     width = strtol(optarg, &p, 10);
126     if (*p == 'x')
127     height = strtol(p+1, NULL, 10);
128    
129     if ((width == 0) || (height == 0))
130     {
131     error("invalid geometry\n");
132     return 1;
133     }
134 matty 10 break;
135    
136 matty 30 case 'f':
137     fullscreen = True;
138     break;
139    
140 matty 10 case 'b':
141     orders = False;
142     break;
143    
144 matty 30 case 'e':
145     encryption = False;
146 matty 10 break;
147    
148 matty 30 case 'm':
149     sendmotion = False;
150 matty 28 break;
151 matty 29
152 matty 30 case 'l':
153     licence = False;
154 matty 28 break;
155 matty 29
156 matty 28 case 'h':
157 matty 10 case '?':
158     default:
159     usage(argv[0]);
160     return 1;
161     }
162     }
163    
164     if (argc - optind < 1)
165     {
166     usage(argv[0]);
167     return 1;
168     }
169    
170     server = argv[optind];
171    
172     if (username[0] == 0)
173     {
174     pw = getpwuid(getuid());
175     if ((pw == NULL) || (pw->pw_name == NULL))
176     {
177 matty 30 error("could not determine username, use -u\n");
178 matty 10 return 1;
179     }
180    
181 matty 30 STRNCPY(username, pw->pw_name, sizeof(username));
182 matty 10 }
183    
184     if (hostname[0] == 0)
185     {
186 matty 30 if (gethostname(fullhostname, sizeof(fullhostname)) == -1)
187 matty 10 {
188 matty 30 error("could not determine local hostname, use -n\n");
189 matty 10 return 1;
190     }
191 matty 30
192     p = strchr(fullhostname, '.');
193     if (p != NULL)
194     *p = 0;
195    
196     STRNCPY(hostname, fullhostname, sizeof(hostname));
197 matty 10 }
198    
199 matty 30 if (!strcmp(password, "-"))
200     {
201     p = getpass("Password: ");
202     if (p == NULL)
203     {
204     error("failed to read password\n");
205     return 0;
206     }
207     STRNCPY(password, p, sizeof(password));
208     }
209    
210     if ((width == 0) || (height == 0))
211     {
212     width = 800;
213     height = 600;
214     }
215    
216 matty 12 strcpy(title, "rdesktop - ");
217 matty 30 strncat(title, server, sizeof(title) - sizeof("rdesktop - "));
218 matty 12
219 matty 10 if (ui_create_window(title))
220     {
221 matty 28 if (!rdp_connect(server, flags, domain, password, shell,
222     directory))
223     return 1;
224    
225 matty 30 printf("Connection successful.\n");
226 matty 10 rdp_main_loop();
227     ui_destroy_window();
228     }
229    
230     rdp_disconnect();
231     return 0;
232     }
233    
234     /* Generate a 32-byte random for the secure transport code. */
235 matty 25 void
236     generate_random(uint8 *random)
237 matty 10 {
238     struct stat st;
239 matty 22 struct tms tmsbuf;
240 matty 24 uint32 *r = (uint32 *) random;
241 matty 10 int fd;
242    
243     /* If we have a kernel random device, use it. */
244 matty 30 if (((fd = open("/dev/urandom", O_RDONLY)) != -1)
245     || ((fd = open("/dev/random", O_RDONLY)) != -1))
246 matty 10 {
247     read(fd, random, 32);
248     close(fd);
249     return;
250     }
251    
252     /* Otherwise use whatever entropy we can gather - ideas welcome. */
253     r[0] = (getpid()) | (getppid() << 16);
254     r[1] = (getuid()) | (getgid() << 16);
255 matty 24 r[2] = times(&tmsbuf); /* system uptime (clocks) */
256     gettimeofday((struct timeval *) &r[3], NULL); /* sec and usec */
257 matty 10 stat("/tmp", &st);
258     r[5] = st.st_atime;
259     r[6] = st.st_mtime;
260     r[7] = st.st_ctime;
261     }
262    
263     /* malloc; exit if out of memory */
264 matty 25 void *
265     xmalloc(int size)
266 matty 10 {
267     void *mem = malloc(size);
268     if (mem == NULL)
269     {
270 matty 30 error("xmalloc %d\n", size);
271 matty 10 exit(1);
272     }
273     return mem;
274     }
275    
276     /* realloc; exit if out of memory */
277 matty 25 void *
278     xrealloc(void *oldmem, int size)
279 matty 10 {
280     void *mem = realloc(oldmem, size);
281     if (mem == NULL)
282     {
283 matty 30 error("xrealloc %d\n", size);
284 matty 10 exit(1);
285     }
286     return mem;
287     }
288    
289     /* free */
290 matty 25 void
291     xfree(void *mem)
292 matty 10 {
293     free(mem);
294     }
295    
296 matty 30 /* report an error */
297 matty 25 void
298 matty 30 error(char *format, ...)
299     {
300     va_list ap;
301    
302     fprintf(stderr, "ERROR: ");
303    
304     va_start(ap, format);
305     vfprintf(stderr, format, ap);
306     va_end(ap);
307     }
308    
309     /* report an unimplemented protocol feature */
310     void
311     unimpl(char *format, ...)
312     {
313     va_list ap;
314    
315     fprintf(stderr, "NOT IMPLEMENTED: ");
316    
317     va_start(ap, format);
318     vfprintf(stderr, format, ap);
319     va_end(ap);
320     }
321    
322     /* produce a hex dump */
323     void
324 matty 25 hexdump(unsigned char *p, unsigned int len)
325 matty 10 {
326     unsigned char *line = p;
327     unsigned int thisline, offset = 0;
328     int i;
329    
330     while (offset < len)
331     {
332 matty 30 printf("%04x ", offset);
333 matty 10 thisline = len - offset;
334     if (thisline > 16)
335     thisline = 16;
336    
337     for (i = 0; i < thisline; i++)
338 matty 30 printf("%02x ", line[i]);
339 matty 10
340 matty 30 for (; i < 16; i++)
341     printf(" ");
342    
343 matty 10 for (i = 0; i < thisline; i++)
344 matty 30 printf("%c",
345 matty 24 (line[i] >= 0x20
346     && line[i] < 0x7f) ? line[i] : '.');
347 matty 10
348 matty 30 printf("\n");
349 matty 10 offset += thisline;
350     line += thisline;
351     }
352     }

  ViewVC Help
Powered by ViewVC 1.1.26