/[rdesktop]/sourceforge.net/branches/seamlessrdp-branch/rdesktop/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

Diff of /sourceforge.net/branches/seamlessrdp-branch/rdesktop/rdesktop.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 685 by stargo, Thu Apr 29 19:40:40 2004 UTC revision 867 by stargo, Wed Mar 23 12:25:54 2005 UTC
# Line 1  Line 1 
1  /* -*- c-basic-offset: 8 -*-  /* -*- c-basic-offset: 8 -*-
2     rdesktop: A Remote Desktop Protocol client.     rdesktop: A Remote Desktop Protocol client.
3     Entrypoint and utility functions     Entrypoint and utility functions
4     Copyright (C) Matthew Chapman 1999-2003     Copyright (C) Matthew Chapman 1999-2005
5    
6     This program is free software; you can redistribute it and/or modify     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     it under the terms of the GNU General Public License as published by
# Line 30  Line 30 
30  #include <errno.h>  #include <errno.h>
31  #include "rdesktop.h"  #include "rdesktop.h"
32    
33    #ifdef HAVE_ICONV
34    #ifdef HAVE_LOCALE_H
35    #include <locale.h>
36    #endif
37    #ifdef HAVE_LANGINFO_H
38    #include <langinfo.h>
39    #endif
40    #endif
41    
42  #ifdef EGD_SOCKET  #ifdef EGD_SOCKET
43  #include <sys/socket.h>         /* socket connect */  #include <sys/socket.h>         /* socket connect */
44  #include <sys/un.h>             /* sockaddr_un */  #include <sys/un.h>             /* sockaddr_un */
45  #endif  #endif
46    
 #ifdef WITH_OPENSSL  
47  #include <openssl/md5.h>  #include <openssl/md5.h>
 #else  
 #include "crypto/md5.h"  
 #endif  
48    
49  char g_title[64] = "";  char g_title[64] = "";
50  char g_username[64];  char g_username[64];
51  char hostname[16];  char g_hostname[16];
52  char keymapname[16];  char keymapname[16];
53  int keylayout = 0x409;          /* Defaults to US keyboard layout */  int g_keylayout = 0x409;        /* Defaults to US keyboard layout */
54    
55  int g_width = 800;              /* width is special: If 0, the  int g_width = 800;              /* width is special: If 0, the
56                                     geometry will be fetched from                                     geometry will be fetched from
# Line 53  int g_width = 800;             /* width is special: Line 58  int g_width = 800;             /* width is special:
58                                     absolute value specifies the                                     absolute value specifies the
59                                     percent of the whole screen. */                                     percent of the whole screen. */
60  int g_height = 600;  int g_height = 600;
61  int tcp_port_rdp = TCP_PORT_RDP;  int g_xpos = 0;
62    int g_ypos = 0;
63    int g_pos = 0;                  /* 0 position unspecified,
64                                       1 specified,
65                                       2 xpos neg,
66                                       4 ypos neg  */
67    extern int g_tcp_port_rdp;
68  int g_server_bpp = 8;  int g_server_bpp = 8;
69  int g_win_button_size = 0;      /* If zero, disable single app mode */  int g_win_button_size = 0;      /* If zero, disable single app mode */
70  BOOL g_bitmap_compression = True;  BOOL g_bitmap_compression = True;
71  BOOL g_sendmotion = True;  BOOL g_sendmotion = True;
72  BOOL g_bitmap_cache = True;  BOOL g_bitmap_cache = True;
73    BOOL g_bitmap_cache_persist_enable = False;
74    BOOL g_bitmap_cache_precache = True;
75  BOOL g_encryption = True;  BOOL g_encryption = True;
76  BOOL packet_encryption = True;  BOOL packet_encryption = True;
77  BOOL g_desktop_save = True;  BOOL g_desktop_save = True;     /* desktop save order */
78    BOOL g_polygon_ellipse_orders = True;   /* polygon / ellipse orders */
79  BOOL g_fullscreen = False;  BOOL g_fullscreen = False;
80  BOOL g_grab_keyboard = True;  BOOL g_grab_keyboard = True;
81  BOOL g_hide_decorations = False;  BOOL g_hide_decorations = False;
# Line 78  uint32 g_rdp5_performanceflags = Line 92  uint32 g_rdp5_performanceflags =
92  BOOL g_rdpsnd = False;  BOOL g_rdpsnd = False;
93  #endif  #endif
94    
95    #ifdef HAVE_ICONV
96    char g_codepage[16] = "";
97    #endif
98    
99  extern RDPDR_DEVICE g_rdpdr_device[];  extern RDPDR_DEVICE g_rdpdr_device[];
100  extern uint32 g_num_devices;  extern uint32 g_num_devices;
101  extern char *g_rdpdr_clientname;  extern char *g_rdpdr_clientname;
# Line 94  static void Line 112  static void
112  usage(char *program)  usage(char *program)
113  {  {
114          fprintf(stderr, "rdesktop: A Remote Desktop Protocol client.\n");          fprintf(stderr, "rdesktop: A Remote Desktop Protocol client.\n");
115          fprintf(stderr, "Version " VERSION ". Copyright (C) 1999-2003 Matt Chapman.\n");          fprintf(stderr, "Version " VERSION ". Copyright (C) 1999-2005 Matt Chapman.\n");
116          fprintf(stderr, "See http://www.rdesktop.org/ for more information.\n\n");          fprintf(stderr, "See http://www.rdesktop.org/ for more information.\n\n");
117    
118          fprintf(stderr, "Usage: %s [options] server[:port]\n", program);          fprintf(stderr, "Usage: %s [options] server[:port]\n", program);
# Line 112  usage(char *program) Line 130  usage(char *program)
130          fprintf(stderr, "   -g: desktop geometry (WxH)\n");          fprintf(stderr, "   -g: desktop geometry (WxH)\n");
131          fprintf(stderr, "   -f: full-screen mode\n");          fprintf(stderr, "   -f: full-screen mode\n");
132          fprintf(stderr, "   -b: force bitmap updates\n");          fprintf(stderr, "   -b: force bitmap updates\n");
133    #ifdef HAVE_ICONV
134            fprintf(stderr, "   -L: local codepage\n");
135    #endif
136          fprintf(stderr, "   -B: use BackingStore of X-server (if available)\n");          fprintf(stderr, "   -B: use BackingStore of X-server (if available)\n");
137          fprintf(stderr, "   -e: disable encryption (French TS)\n");          fprintf(stderr, "   -e: disable encryption (French TS)\n");
138          fprintf(stderr, "   -E: disable encryption from client to server\n");          fprintf(stderr, "   -E: disable encryption from client to server\n");
# Line 124  usage(char *program) Line 145  usage(char *program)
145          fprintf(stderr, "   -N: enable numlock syncronization\n");          fprintf(stderr, "   -N: enable numlock syncronization\n");
146          fprintf(stderr, "   -X: embed into another window with a given id.\n");          fprintf(stderr, "   -X: embed into another window with a given id.\n");
147          fprintf(stderr, "   -a: connection colour depth\n");          fprintf(stderr, "   -a: connection colour depth\n");
148          fprintf(stderr,          fprintf(stderr, "   -z: enable rdp compression\n");
149                  "   -x: RDP5 experience (m[odem 28.8], b[roadband], l[an] or hex number)\n");          fprintf(stderr, "   -x: RDP5 experience (m[odem 28.8], b[roadband], l[an] or hex nr.)\n");
150            fprintf(stderr, "   -P: use persistent bitmap caching\n");
151          fprintf(stderr, "   -r: enable specified device redirection (this flag can be repeated)\n");          fprintf(stderr, "   -r: enable specified device redirection (this flag can be repeated)\n");
152          fprintf(stderr,          fprintf(stderr,
153                  "         '-r comport:COM1=/dev/ttyS0': enable serial redirection of /dev/ttyS0 to COM1\n");                  "         '-r comport:COM1=/dev/ttyS0': enable serial redirection of /dev/ttyS0 to COM1\n");
154          fprintf(stderr, "             or      COM1=/dev/ttyS0,COM2=/dev/ttyS1\n");          fprintf(stderr, "             or      COM1=/dev/ttyS0,COM2=/dev/ttyS1\n");
155          fprintf(stderr,          fprintf(stderr,
156                  "         '-r disk:A=/mnt/floppy': enable redirection of /mnt/floppy to A:\n");                  "         '-r disk:floppy=/mnt/floppy': enable redirection of /mnt/floppy to 'floppy' share\n");
157          fprintf(stderr, "             or   A=/mnt/floppy,D=/mnt/cdrom'\n");          fprintf(stderr, "             or   'floppy=/mnt/floppy,cdrom=/mnt/cdrom'\n");
158          fprintf(stderr, "         '-r clientname=<client name>': Set the client name displayed\n");          fprintf(stderr, "         '-r clientname=<client name>': Set the client name displayed\n");
159          fprintf(stderr, "             for redirected disks\n");          fprintf(stderr, "             for redirected disks\n");
160          fprintf(stderr,          fprintf(stderr,
# Line 305  parse_server_and_port(char *server) Line 327  parse_server_and_port(char *server)
327                  if (*server == '[' && p != NULL)                  if (*server == '[' && p != NULL)
328                  {                  {
329                          if (*(p + 1) == ':' && *(p + 2) != '\0')                          if (*(p + 1) == ':' && *(p + 2) != '\0')
330                                  tcp_port_rdp = strtol(p + 2, NULL, 10);                                  g_tcp_port_rdp = strtol(p + 2, NULL, 10);
331                          /* remove the port number and brackets from the address */                          /* remove the port number and brackets from the address */
332                          *p = '\0';                          *p = '\0';
333                          strncpy(server, server + 1, strlen(server));                          strncpy(server, server + 1, strlen(server));
# Line 317  parse_server_and_port(char *server) Line 339  parse_server_and_port(char *server)
339                  p = strchr(server, ':');                  p = strchr(server, ':');
340                  if (p != NULL)                  if (p != NULL)
341                  {                  {
342                          tcp_port_rdp = strtol(p + 1, NULL, 10);                          g_tcp_port_rdp = strtol(p + 1, NULL, 10);
343                          *p = 0;                          *p = 0;
344                  }                  }
345          }          }
# Line 325  parse_server_and_port(char *server) Line 347  parse_server_and_port(char *server)
347          p = strchr(server, ':');          p = strchr(server, ':');
348          if (p != NULL)          if (p != NULL)
349          {          {
350                  tcp_port_rdp = strtol(p + 1, NULL, 10);                  g_tcp_port_rdp = strtol(p + 1, NULL, 10);
351                  *p = 0;                  *p = 0;
352          }          }
353  #endif /* IPv6 */  #endif /* IPv6 */
# Line 364  main(int argc, char *argv[]) Line 386  main(int argc, char *argv[])
386  #define VNCOPT  #define VNCOPT
387  #endif  #endif
388    
389          while ((c = getopt(argc, argv, VNCOPT "u:d:s:c:p:n:k:g:fbBeEmCDKS:T:NX:a:x:r:045h?")) != -1)          while ((c = getopt(argc, argv,
390                               VNCOPT "u:L:d:s:c:p:n:k:g:fbBeEmzCDKS:T:NX:a:x:Pr:045h?")) != -1)
391          {          {
392                  switch (c)                  switch (c)
393                  {                  {
# Line 387  main(int argc, char *argv[]) Line 410  main(int argc, char *argv[])
410                                  username_option = 1;                                  username_option = 1;
411                                  break;                                  break;
412    
413                            case 'L':
414    #ifdef HAVE_ICONV
415                                    STRNCPY(g_codepage, optarg, sizeof(g_codepage));
416    #else
417                                    error("iconv support not available\n");
418    #endif
419                                    break;
420    
421                          case 'd':                          case 'd':
422                                  STRNCPY(domain, optarg, sizeof(domain));                                  STRNCPY(domain, optarg, sizeof(domain));
423                                  break;                                  break;
# Line 416  main(int argc, char *argv[]) Line 447  main(int argc, char *argv[])
447                                  break;                                  break;
448    
449                          case 'n':                          case 'n':
450                                  STRNCPY(hostname, optarg, sizeof(hostname));                                  STRNCPY(g_hostname, optarg, sizeof(g_hostname));
451                                  break;                                  break;
452    
453                          case 'k':                          case 'k':
# Line 439  main(int argc, char *argv[]) Line 470  main(int argc, char *argv[])
470                                  }                                  }
471    
472                                  if (*p == 'x')                                  if (*p == 'x')
473                                          g_height = strtol(p + 1, NULL, 10);                                          g_height = strtol(p + 1, &p, 10);
474    
475                                  if (g_height <= 0)                                  if (g_height <= 0)
476                                  {                                  {
# Line 448  main(int argc, char *argv[]) Line 479  main(int argc, char *argv[])
479                                  }                                  }
480    
481                                  if (*p == '%')                                  if (*p == '%')
482                                    {
483                                          g_width = -g_width;                                          g_width = -g_width;
484                                            p++;
485                                    }
486    
487                                    if (*p == '+' || *p == '-')
488                                    {
489                                            g_pos |= (*p == '-') ? 2 : 1;
490                                            g_xpos = strtol(p, &p, 10);
491    
492                                    }
493                                    if (*p == '+' || *p == '-')
494                                    {
495                                            g_pos |= (*p == '-') ? 4 : 1;
496                                            g_ypos = strtol(p, NULL, 10);
497                                    }
498    
499                                  break;                                  break;
500    
# Line 525  main(int argc, char *argv[]) Line 571  main(int argc, char *argv[])
571                                  }                                  }
572                                  break;                                  break;
573    
574                          case 'x':                          case 'z':
575                                    DEBUG(("rdp compression enabled\n"));
576                                    flags |= RDP_COMPRESSION;
577                                    break;
578    
579                            case 'x':
580                                  if (strncmp("modem", optarg, 1) == 0)                                  if (strncmp("modem", optarg, 1) == 0)
581                                  {                                  {
582                                          g_rdp5_performanceflags =                                          g_rdp5_performanceflags =
# Line 547  main(int argc, char *argv[]) Line 597  main(int argc, char *argv[])
597                                  }                                  }
598                                  break;                                  break;
599    
600                            case 'P':
601                                    g_bitmap_cache_persist_enable = True;
602                                    break;
603    
604                          case 'r':                          case 'r':
605    
606                                  if (strncmp("sound", optarg, 5) == 0)                                  if (strncmp("sound", optarg, 5) == 0)
# Line 565  main(int argc, char *argv[]) Line 619  main(int argc, char *argv[])
619  #ifdef WITH_RDPSND  #ifdef WITH_RDPSND
620                                                                  g_rdpsnd = True;                                                                  g_rdpsnd = True;
621  #else  #else
622                                                                  warning("Not compiled with sound support");                                                                  warning("Not compiled with sound support\n");
623  #endif  #endif
624    
625                                                          if (strncmp("off", optarg, 3) == 0)                                                          if (strncmp("off", optarg, 3) == 0)
626  #ifdef WITH_RDPSND  #ifdef WITH_RDPSND
627                                                                  g_rdpsnd = False;                                                                  g_rdpsnd = False;
628  #else  #else
629                                                                  warning("Not compiled with sound support");                                                                  warning("Not compiled with sound support\n");
630  #endif  #endif
631    
632                                                          optarg = p;                                                          optarg = p;
# Line 583  main(int argc, char *argv[]) Line 637  main(int argc, char *argv[])
637  #ifdef WITH_RDPSND  #ifdef WITH_RDPSND
638                                                  g_rdpsnd = True;                                                  g_rdpsnd = True;
639  #else  #else
640                                                  warning("Not compiled with sound support");                                                  warning("Not compiled with sound support\n");
641  #endif  #endif
642                                          }                                          }
643                                  }                                  }
# Line 656  main(int argc, char *argv[]) Line 710  main(int argc, char *argv[])
710                  STRNCPY(g_username, pw->pw_name, sizeof(g_username));                  STRNCPY(g_username, pw->pw_name, sizeof(g_username));
711          }          }
712    
713          if (hostname[0] == 0)  #ifdef HAVE_ICONV
714            if (g_codepage[0] == 0)
715            {
716                    if (setlocale(LC_CTYPE, ""))
717                    {
718                            STRNCPY(g_codepage, nl_langinfo(CODESET), sizeof(g_codepage));
719                    }
720                    else
721                    {
722                            STRNCPY(g_codepage, DEFAULT_CODEPAGE, sizeof(g_codepage));
723                    }
724            }
725    #endif
726    
727            if (g_hostname[0] == 0)
728          {          {
729                  if (gethostname(fullhostname, sizeof(fullhostname)) == -1)                  if (gethostname(fullhostname, sizeof(fullhostname)) == -1)
730                  {                  {
# Line 668  main(int argc, char *argv[]) Line 736  main(int argc, char *argv[])
736                  if (p != NULL)                  if (p != NULL)
737                          *p = 0;                          *p = 0;
738    
739                  STRNCPY(hostname, fullhostname, sizeof(hostname));                  STRNCPY(g_hostname, fullhostname, sizeof(g_hostname));
740            }
741    
742            if ((flags & RDP_COMPRESSION) && (g_server_bpp > 8))
743            {
744                    warning("rdp compression not supported for bpp > 8, compression disabled\n");
745                    flags ^= RDP_COMPRESSION;
746          }          }
747    
748          if (prompt_password && read_password(password, sizeof(password)))          if (prompt_password && read_password(password, sizeof(password)))
# Line 714  main(int argc, char *argv[]) Line 788  main(int argc, char *argv[])
788    
789          DEBUG(("Disconnecting...\n"));          DEBUG(("Disconnecting...\n"));
790          rdp_disconnect();          rdp_disconnect();
791            cache_save_state();
792          ui_deinit();          ui_deinit();
793    
794          if (ext_disc_reason >= 2)          if (ext_disc_reason >= 2)
# Line 843  xmalloc(int size) Line 918  xmalloc(int size)
918  void *  void *
919  xrealloc(void *oldmem, int size)  xrealloc(void *oldmem, int size)
920  {  {
921          void *mem = realloc(oldmem, size);          void *mem;
922    
923            if (size < 1)
924                    size = 1;
925            mem = realloc(oldmem, size);
926          if (mem == NULL)          if (mem == NULL)
927          {          {
928                  error("xrealloc %d\n", size);                  error("xrealloc %d\n", size);
# Line 1056  load_licence(unsigned char **data) Line 1135  load_licence(unsigned char **data)
1135          if (home == NULL)          if (home == NULL)
1136                  return -1;                  return -1;
1137    
1138          path = (char *) xmalloc(strlen(home) + strlen(hostname) + sizeof("/.rdesktop/licence."));          path = (char *) xmalloc(strlen(home) + strlen(g_hostname) + sizeof("/.rdesktop/licence."));
1139          sprintf(path, "%s/.rdesktop/licence.%s", home, hostname);          sprintf(path, "%s/.rdesktop/licence.%s", home, g_hostname);
1140    
1141          fd = open(path, O_RDONLY);          fd = open(path, O_RDONLY);
1142          if (fd == -1)          if (fd == -1)
# Line 1083  save_licence(unsigned char *data, int le Line 1162  save_licence(unsigned char *data, int le
1162          if (home == NULL)          if (home == NULL)
1163                  return;                  return;
1164    
1165          path = (char *) xmalloc(strlen(home) + strlen(hostname) + sizeof("/.rdesktop/licence."));          path = (char *) xmalloc(strlen(home) + strlen(g_hostname) + sizeof("/.rdesktop/licence."));
1166    
1167          sprintf(path, "%s/.rdesktop", home);          sprintf(path, "%s/.rdesktop", home);
1168          if ((mkdir(path, 0700) == -1) && errno != EEXIST)          if ((mkdir(path, 0700) == -1) && errno != EEXIST)
# Line 1094  save_licence(unsigned char *data, int le Line 1173  save_licence(unsigned char *data, int le
1173    
1174          /* write licence to licence.hostname.new, then atomically rename to licence.hostname */          /* write licence to licence.hostname.new, then atomically rename to licence.hostname */
1175    
1176          sprintf(path, "%s/.rdesktop/licence.%s", home, hostname);          sprintf(path, "%s/.rdesktop/licence.%s", home, g_hostname);
1177          tmppath = (char *) xmalloc(strlen(path) + sizeof(".new"));          tmppath = (char *) xmalloc(strlen(path) + sizeof(".new"));
1178          strcpy(tmppath, path);          strcpy(tmppath, path);
1179          strcat(tmppath, ".new");          strcat(tmppath, ".new");
# Line 1121  save_licence(unsigned char *data, int le Line 1200  save_licence(unsigned char *data, int le
1200          xfree(tmppath);          xfree(tmppath);
1201          xfree(path);          xfree(path);
1202  }  }
1203    
1204    /* Create the bitmap cache directory */
1205    BOOL
1206    rd_pstcache_mkdir(void)
1207    {
1208            char *home;
1209            char bmpcache_dir[256];
1210    
1211            home = getenv("HOME");
1212    
1213            if (home == NULL)
1214                    return False;
1215    
1216            sprintf(bmpcache_dir, "%s/%s", home, ".rdesktop");
1217    
1218            if ((mkdir(bmpcache_dir, S_IRWXU) == -1) && errno != EEXIST)
1219            {
1220                    perror(bmpcache_dir);
1221                    return False;
1222            }
1223    
1224            sprintf(bmpcache_dir, "%s/%s", home, ".rdesktop/cache");
1225    
1226            if ((mkdir(bmpcache_dir, S_IRWXU) == -1) && errno != EEXIST)
1227            {
1228                    perror(bmpcache_dir);
1229                    return False;
1230            }
1231    
1232            return True;
1233    }
1234    
1235    /* open a file in the .rdesktop directory */
1236    int
1237    rd_open_file(char *filename)
1238    {
1239            char *home;
1240            char fn[256];
1241            int fd;
1242    
1243            home = getenv("HOME");
1244            if (home == NULL)
1245                    return -1;
1246            sprintf(fn, "%s/.rdesktop/%s", home, filename);
1247            fd = open(fn, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
1248            if (fd == -1)
1249                    perror(fn);
1250            return fd;
1251    }
1252    
1253    /* close file */
1254    void
1255    rd_close_file(int fd)
1256    {
1257            close(fd);
1258    }
1259    
1260    /* read from file*/
1261    int
1262    rd_read_file(int fd, void *ptr, int len)
1263    {
1264            return read(fd, ptr, len);
1265    }
1266    
1267    /* write to file */
1268    int
1269    rd_write_file(int fd, void *ptr, int len)
1270    {
1271            return write(fd, ptr, len);
1272    }
1273    
1274    /* move file pointer */
1275    int
1276    rd_lseek_file(int fd, int offset)
1277    {
1278            return lseek(fd, offset, SEEK_SET);
1279    }
1280    
1281    /* do a write lock on a file */
1282    BOOL
1283    rd_lock_file(int fd, int start, int len)
1284    {
1285            struct flock lock;
1286    
1287            lock.l_type = F_WRLCK;
1288            lock.l_whence = SEEK_SET;
1289            lock.l_start = start;
1290            lock.l_len = len;
1291            if (fcntl(fd, F_SETLK, &lock) == -1)
1292                    return False;
1293            return True;
1294    }

Legend:
Removed from v.685  
changed lines
  Added in v.867

  ViewVC Help
Powered by ViewVC 1.1.26