1 /* -*- c-basic-offset: 8 -*-
2 rdesktop: A Remote Desktop Protocol client.
3 Entrypoint and utility functions
4 Copyright (C) Matthew Chapman 1999-2004
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.
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.
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.
21 #include <stdarg.h> /* va_list va_start va_end */
22 #include <unistd.h> /* read close getuid getgid getpid getppid gethostname */
23 #include <fcntl.h> /* open */
24 #include <pwd.h> /* getpwuid */
25 #include <termios.h> /* tcgetattr tcsetattr */
26 #include <sys/stat.h> /* stat */
27 #include <sys/time.h> /* gettimeofday */
28 #include <sys/times.h> /* times */
30 #include <X11/Xlib.h> /* Window */
34 #include <sys/socket.h> /* socket connect */
35 #include <sys/un.h> /* sockaddr_un */
38 #include <openssl/md5.h>
41 /* Read 32 random bytes from PRNGD or EGD socket (based on OpenSSL RAND_egd) */
43 generate_random_egd(uint8 * buf)
45 struct sockaddr_un addr;
49 fd = socket(AF_UNIX, SOCK_STREAM, 0);
53 addr.sun_family = AF_UNIX;
54 memcpy(addr.sun_path, EGD_SOCKET, sizeof(EGD_SOCKET));
55 if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1)
58 /* PRNGD and EGD use a simple communications protocol */
59 buf[0] = 1; /* Non-blocking (similar to /dev/urandom) */
60 buf[1] = 32; /* Number of requested random bytes */
61 if (write(fd, buf, 2) != 2)
64 if ((read(fd, buf, 1) != 1) || (buf[0] == 0)) /* Available? */
67 if (read(fd, buf, 32) != 32)
78 /* malloc; exit if out of memory */
82 void *mem = malloc(size);
85 error("xmalloc %d\n", size);
91 /* realloc; exit if out of memory */
93 xrealloc(void *oldmem, int size)
95 void *mem = realloc(oldmem, size);
98 error("xrealloc %d\n", size);
111 /* report an error */
113 error(char *format, ...)
117 fprintf(stderr, "ERROR: ");
119 va_start(ap, format);
120 vfprintf(stderr, format, ap);
124 /* report a warning */
126 warning(char *format, ...)
130 fprintf(stderr, "WARNING: ");
132 va_start(ap, format);
133 vfprintf(stderr, format, ap);
137 /* report an unimplemented protocol feature */
139 unimpl(char *format, ...)
143 fprintf(stderr, "NOT IMPLEMENTED: ");
145 va_start(ap, format);
146 vfprintf(stderr, format, ap);
150 /* produce a hex dump */
152 hexdump(unsigned char *p, int len)
154 unsigned char *line = p;
155 int i, thisline, offset = 0;
159 printf("%04x ", offset);
160 thisline = len - offset;
164 for (i = 0; i < thisline; i++)
165 printf("%02x ", line[i]);
170 for (i = 0; i < thisline; i++)
171 printf("%c", (line[i] >= 0x20 && line[i] < 0x7f) ? line[i] : '.');