/[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 651 by astrand, Thu Apr 15 20:12:42 2004 UTC revision 800 by stargo, Thu Nov 18 11:18:49 2004 UTC
# Line 35  Line 35 
35  #include <sys/un.h>             /* sockaddr_un */  #include <sys/un.h>             /* sockaddr_un */
36  #endif  #endif
37    
 #ifdef WITH_OPENSSL  
38  #include <openssl/md5.h>  #include <openssl/md5.h>
 #else  
 #include "crypto/md5.h"  
 #endif  
39    
40  char g_title[64] = "";  char g_title[64] = "";
41  char g_username[64];  char g_username[64];
42  char hostname[16];  char g_hostname[16];
43  char keymapname[16];  char keymapname[16];
44  int keylayout = 0x409;          /* Defaults to US keyboard layout */  int g_keylayout = 0x409;        /* Defaults to US keyboard layout */
45    
46  int g_width = 800;              /* width is special: If 0, the  int g_width = 800;              /* width is special: If 0, the
47                                     geometry will be fetched from                                     geometry will be fetched from
# Line 53  int g_width = 800;             /* width is special: Line 49  int g_width = 800;             /* width is special:
49                                     absolute value specifies the                                     absolute value specifies the
50                                     percent of the whole screen. */                                     percent of the whole screen. */
51  int g_height = 600;  int g_height = 600;
52  int tcp_port_rdp = TCP_PORT_RDP;  int g_xpos = 0;
53    int g_ypos = 0;
54    extern int g_tcp_port_rdp;
55  int g_server_bpp = 8;  int g_server_bpp = 8;
56  int g_win_button_size = 0;      /* If zero, disable single app mode */  int g_win_button_size = 0;      /* If zero, disable single app mode */
57  BOOL g_bitmap_compression = True;  BOOL g_bitmap_compression = True;
58  BOOL g_sendmotion = True;  BOOL g_sendmotion = True;
59  BOOL g_orders = True;  BOOL g_bitmap_cache = True;
60    BOOL g_bitmap_cache_persist_enable = False;
61    BOOL g_bitmap_cache_precache = True;
62  BOOL g_encryption = True;  BOOL g_encryption = True;
63  BOOL packet_encryption = True;  BOOL packet_encryption = True;
64  BOOL g_desktop_save = True;  BOOL g_desktop_save = True;
# Line 124  usage(char *program) Line 124  usage(char *program)
124          fprintf(stderr, "   -N: enable numlock syncronization\n");          fprintf(stderr, "   -N: enable numlock syncronization\n");
125          fprintf(stderr, "   -X: embed into another window with a given id.\n");          fprintf(stderr, "   -X: embed into another window with a given id.\n");
126          fprintf(stderr, "   -a: connection colour depth\n");          fprintf(stderr, "   -a: connection colour depth\n");
127          fprintf(stderr,          fprintf(stderr, "   -z: enable rdp compression\n");
128                  "   -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");
129            fprintf(stderr, "   -P: use persistent bitmap caching\n");
130          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");
131          fprintf(stderr,          fprintf(stderr,
132                  "         '-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");
133          fprintf(stderr, "             or      COM1=/dev/ttyS0,COM2=/dev/ttyS1\n");          fprintf(stderr, "             or      COM1=/dev/ttyS0,COM2=/dev/ttyS1\n");
134          fprintf(stderr,          fprintf(stderr,
135                  "         '-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");
136          fprintf(stderr, "             or   A=/mnt/floppy,D=/mnt/cdrom'\n");          fprintf(stderr, "             or   'floppy=/mnt/floppy,cdrom=/mnt/cdrom'\n");
137          fprintf(stderr, "         '-r clientname=<client name>': Set the client name displayed\n");          fprintf(stderr, "         '-r clientname=<client name>': Set the client name displayed\n");
138          fprintf(stderr, "             for redirected disks\n");          fprintf(stderr, "             for redirected disks\n");
139          fprintf(stderr,          fprintf(stderr,
# Line 148  usage(char *program) Line 149  usage(char *program)
149          fprintf(stderr, "   -5: use RDP version 5 (default)\n");          fprintf(stderr, "   -5: use RDP version 5 (default)\n");
150  }  }
151    
152    void
153    print_disconnect_reason(uint16 reason)
154    {
155            char *text;
156    
157            switch (reason)
158            {
159                    case exDiscReasonNoInfo:
160                            text = "No information available";
161                            break;
162    
163                    case exDiscReasonAPIInitiatedDisconnect:
164                            text = "Server initiated disconnect";
165                            break;
166    
167                    case exDiscReasonAPIInitiatedLogoff:
168                            text = "Server initiated logoff";
169                            break;
170    
171                    case exDiscReasonServerIdleTimeout:
172                            text = "Server idle timeout reached";
173                            break;
174    
175                    case exDiscReasonServerLogonTimeout:
176                            text = "Server logon timeout reached";
177                            break;
178    
179                    case exDiscReasonReplacedByOtherConnection:
180                            text = "The session was replaced";
181                            break;
182    
183                    case exDiscReasonOutOfMemory:
184                            text = "The server is out of memory";
185                            break;
186    
187                    case exDiscReasonServerDeniedConnection:
188                            text = "The server denied the connection";
189                            break;
190    
191                    case exDiscReasonServerDeniedConnectionFips:
192                            text = "The server denied the connection for security reason";
193                            break;
194    
195                    case exDiscReasonLicenseInternal:
196                            text = "Internal licensing error";
197                            break;
198    
199                    case exDiscReasonLicenseNoLicenseServer:
200                            text = "No license server available";
201                            break;
202    
203                    case exDiscReasonLicenseNoLicense:
204                            text = "No valid license available";
205                            break;
206    
207                    case exDiscReasonLicenseErrClientMsg:
208                            text = "Invalid licensing message";
209                            break;
210    
211                    case exDiscReasonLicenseHwidDoesntMatchLicense:
212                            text = "Hardware id doesn't match software license";
213                            break;
214    
215                    case exDiscReasonLicenseErrClientLicense:
216                            text = "Client license error";
217                            break;
218    
219                    case exDiscReasonLicenseCantFinishProtocol:
220                            text = "Network error during licensing protocol";
221                            break;
222    
223                    case exDiscReasonLicenseClientEndedProtocol:
224                            text = "Licensing protocol was not completed";
225                            break;
226    
227                    case exDiscReasonLicenseErrClientEncryption:
228                            text = "Incorrect client license enryption";
229                            break;
230    
231                    case exDiscReasonLicenseCantUpgradeLicense:
232                            text = "Can't upgrade license";
233                            break;
234    
235                    case exDiscReasonLicenseNoRemoteConnections:
236                            text = "The server is not licensed to accept remote connections";
237                            break;
238    
239                    default:
240                            if (reason > 0x1000 && reason < 0x7fff)
241                            {
242                                    text = "Internal protocol error";
243                            }
244                            else
245                            {
246                                    text = "Unknown reason";
247                            }
248            }
249            fprintf(stderr, "disconnect: %s.\n", text);
250    }
251    
252  static BOOL  static BOOL
253  read_password(char *password, int size)  read_password(char *password, int size)
254  {  {
# Line 205  parse_server_and_port(char *server) Line 306  parse_server_and_port(char *server)
306                  if (*server == '[' && p != NULL)                  if (*server == '[' && p != NULL)
307                  {                  {
308                          if (*(p + 1) == ':' && *(p + 2) != '\0')                          if (*(p + 1) == ':' && *(p + 2) != '\0')
309                                  tcp_port_rdp = strtol(p + 2, NULL, 10);                                  g_tcp_port_rdp = strtol(p + 2, NULL, 10);
310                          /* remove the port number and brackets from the address */                          /* remove the port number and brackets from the address */
311                          *p = '\0';                          *p = '\0';
312                          strncpy(server, server + 1, strlen(server));                          strncpy(server, server + 1, strlen(server));
# Line 217  parse_server_and_port(char *server) Line 318  parse_server_and_port(char *server)
318                  p = strchr(server, ':');                  p = strchr(server, ':');
319                  if (p != NULL)                  if (p != NULL)
320                  {                  {
321                          tcp_port_rdp = strtol(p + 1, NULL, 10);                          g_tcp_port_rdp = strtol(p + 1, NULL, 10);
322                          *p = 0;                          *p = 0;
323                  }                  }
324          }          }
# Line 225  parse_server_and_port(char *server) Line 326  parse_server_and_port(char *server)
326          p = strchr(server, ':');          p = strchr(server, ':');
327          if (p != NULL)          if (p != NULL)
328          {          {
329                  tcp_port_rdp = strtol(p + 1, NULL, 10);                  g_tcp_port_rdp = strtol(p + 1, NULL, 10);
330                  *p = 0;                  *p = 0;
331          }          }
332  #endif /* IPv6 */  #endif /* IPv6 */
# Line 242  main(int argc, char *argv[]) Line 343  main(int argc, char *argv[])
343          char password[64];          char password[64];
344          char shell[128];          char shell[128];
345          char directory[32];          char directory[32];
346          BOOL prompt_password, rdp_retval = False;          BOOL prompt_password, deactivated;
347          struct passwd *pw;          struct passwd *pw;
348          uint32 flags;          uint32 flags, ext_disc_reason = 0;
349          char *p;          char *p;
350          int c;          int c;
351    
# Line 264  main(int argc, char *argv[]) Line 365  main(int argc, char *argv[])
365  #define VNCOPT  #define VNCOPT
366  #endif  #endif
367    
368          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,
369                               VNCOPT "u:d:s:c:p:n:k:g:fbBeEmzCDKS:T:NX:a:x:Pr:045h?")) != -1)
370          {          {
371                  switch (c)                  switch (c)
372                  {                  {
# Line 316  main(int argc, char *argv[]) Line 418  main(int argc, char *argv[])
418                                  break;                                  break;
419    
420                          case 'n':                          case 'n':
421                                  STRNCPY(hostname, optarg, sizeof(hostname));                                  STRNCPY(g_hostname, optarg, sizeof(g_hostname));
422                                  break;                                  break;
423    
424                          case 'k':                          case 'k':
# Line 339  main(int argc, char *argv[]) Line 441  main(int argc, char *argv[])
441                                  }                                  }
442    
443                                  if (*p == 'x')                                  if (*p == 'x')
444                                          g_height = strtol(p + 1, NULL, 10);                                          g_height = strtol(p + 1, &p, 10);
445    
446                                  if (g_height <= 0)                                  if (g_height <= 0)
447                                  {                                  {
# Line 348  main(int argc, char *argv[]) Line 450  main(int argc, char *argv[])
450                                  }                                  }
451    
452                                  if (*p == '%')                                  if (*p == '%')
453                                    {
454                                          g_width = -g_width;                                          g_width = -g_width;
455                                            p++;
456                                    }
457    
458                                    if (*p == '+' || *p == '-')
459                                            g_xpos = strtol(p, &p, 10);
460                                    
461                                    if (*p == '+' || *p == '-')
462                                            g_ypos = strtol(p, NULL, 10);
463    
464                                  break;                                  break;
465    
# Line 357  main(int argc, char *argv[]) Line 468  main(int argc, char *argv[])
468                                  break;                                  break;
469    
470                          case 'b':                          case 'b':
471                                  g_orders = False;                                  g_bitmap_cache = False;
472                                  break;                                  break;
473    
474                          case 'B':                          case 'B':
# Line 425  main(int argc, char *argv[]) Line 536  main(int argc, char *argv[])
536                                  }                                  }
537                                  break;                                  break;
538    
539                          case 'x':                          case 'z':
540                                    DEBUG(("rdp compression enabled\n"));
541                                    flags |= RDP_COMPRESSION;
542                                    break;
543    
544                            case 'x':
545                                  if (strncmp("modem", optarg, 1) == 0)                                  if (strncmp("modem", optarg, 1) == 0)
546                                  {                                  {
547                                          g_rdp5_performanceflags =                                          g_rdp5_performanceflags =
# Line 447  main(int argc, char *argv[]) Line 562  main(int argc, char *argv[])
562                                  }                                  }
563                                  break;                                  break;
564    
565                            case 'P':
566                                    g_bitmap_cache_persist_enable = True;
567                                    break;
568    
569                          case 'r':                          case 'r':
570    
571                                  if (strncmp("sound", optarg, 5) == 0)                                  if (strncmp("sound", optarg, 5) == 0)
# Line 465  main(int argc, char *argv[]) Line 584  main(int argc, char *argv[])
584  #ifdef WITH_RDPSND  #ifdef WITH_RDPSND
585                                                                  g_rdpsnd = True;                                                                  g_rdpsnd = True;
586  #else  #else
587                                                                  warning("Not compiled with sound support");                                                                  warning("Not compiled with sound support\n");
588  #endif  #endif
589    
590                                                          if (strncmp("off", optarg, 3) == 0)                                                          if (strncmp("off", optarg, 3) == 0)
591    #ifdef WITH_RDPSND
592                                                                  g_rdpsnd = False;                                                                  g_rdpsnd = False;
593    #else
594                                                                    warning("Not compiled with sound support\n");
595    #endif
596    
597                                                          optarg = p;                                                          optarg = p;
598                                                  }                                                  }
# Line 479  main(int argc, char *argv[]) Line 602  main(int argc, char *argv[])
602  #ifdef WITH_RDPSND  #ifdef WITH_RDPSND
603                                                  g_rdpsnd = True;                                                  g_rdpsnd = True;
604  #else  #else
605                                                  warning("Not compiled with sound support");                                                  warning("Not compiled with sound support\n");
606  #endif  #endif
607                                          }                                          }
608                                  }                                  }
# Line 552  main(int argc, char *argv[]) Line 675  main(int argc, char *argv[])
675                  STRNCPY(g_username, pw->pw_name, sizeof(g_username));                  STRNCPY(g_username, pw->pw_name, sizeof(g_username));
676          }          }
677    
678          if (hostname[0] == 0)          if (g_hostname[0] == 0)
679          {          {
680                  if (gethostname(fullhostname, sizeof(fullhostname)) == -1)                  if (gethostname(fullhostname, sizeof(fullhostname)) == -1)
681                  {                  {
# Line 564  main(int argc, char *argv[]) Line 687  main(int argc, char *argv[])
687                  if (p != NULL)                  if (p != NULL)
688                          *p = 0;                          *p = 0;
689    
690                  STRNCPY(hostname, fullhostname, sizeof(hostname));                  STRNCPY(g_hostname, fullhostname, sizeof(g_hostname));
691          }          }
692    
693          if (prompt_password && read_password(password, sizeof(password)))          if (prompt_password && read_password(password, sizeof(password)))
# Line 604  main(int argc, char *argv[]) Line 727  main(int argc, char *argv[])
727    
728          if (ui_create_window())          if (ui_create_window())
729          {          {
730                  rdp_retval = rdp_main_loop();                  rdp_main_loop(&deactivated, &ext_disc_reason);
731                  ui_destroy_window();                  ui_destroy_window();
732          }          }
733    
734          DEBUG(("Disconnecting...\n"));          DEBUG(("Disconnecting...\n"));
735          rdp_disconnect();          rdp_disconnect();
736            cache_save_state();
737          ui_deinit();          ui_deinit();
738    
739          if (True == rdp_retval)          if (ext_disc_reason >= 2)
740                    print_disconnect_reason(ext_disc_reason);
741    
742            if (deactivated)
743            {
744                    /* clean disconnect */
745                  return 0;                  return 0;
746            }
747          else          else
748                  return 2;          {
749                    if (ext_disc_reason == exDiscReasonAPIInitiatedDisconnect
750                        || ext_disc_reason == exDiscReasonAPIInitiatedLogoff)
751                    {
752                            /* not so clean disconnect, but nothing to worry about */
753                            return 0;
754                    }
755                    else
756                    {
757                            /* return error */
758                            return 2;
759                    }
760            }
761    
762  #endif  #endif
763    
# Line 721  xmalloc(int size) Line 863  xmalloc(int size)
863  void *  void *
864  xrealloc(void *oldmem, int size)  xrealloc(void *oldmem, int size)
865  {  {
866          void *mem = realloc(oldmem, size);          void *mem;
867    
868            if (size < 1)
869                    size = 1;
870            mem = realloc(oldmem, size);
871          if (mem == NULL)          if (mem == NULL)
872          {          {
873                  error("xrealloc %d\n", size);                  error("xrealloc %d\n", size);
# Line 934  load_licence(unsigned char **data) Line 1080  load_licence(unsigned char **data)
1080          if (home == NULL)          if (home == NULL)
1081                  return -1;                  return -1;
1082    
1083          path = (char *) xmalloc(strlen(home) + strlen(hostname) + sizeof("/.rdesktop/licence."));          path = (char *) xmalloc(strlen(home) + strlen(g_hostname) + sizeof("/.rdesktop/licence."));
1084          sprintf(path, "%s/.rdesktop/licence.%s", home, hostname);          sprintf(path, "%s/.rdesktop/licence.%s", home, g_hostname);
1085    
1086          fd = open(path, O_RDONLY);          fd = open(path, O_RDONLY);
1087          if (fd == -1)          if (fd == -1)
# Line 961  save_licence(unsigned char *data, int le Line 1107  save_licence(unsigned char *data, int le
1107          if (home == NULL)          if (home == NULL)
1108                  return;                  return;
1109    
1110          path = (char *) xmalloc(strlen(home) + strlen(hostname) + sizeof("/.rdesktop/licence."));          path = (char *) xmalloc(strlen(home) + strlen(g_hostname) + sizeof("/.rdesktop/licence."));
1111    
1112          sprintf(path, "%s/.rdesktop", home);          sprintf(path, "%s/.rdesktop", home);
1113          if ((mkdir(path, 0700) == -1) && errno != EEXIST)          if ((mkdir(path, 0700) == -1) && errno != EEXIST)
# Line 972  save_licence(unsigned char *data, int le Line 1118  save_licence(unsigned char *data, int le
1118    
1119          /* write licence to licence.hostname.new, then atomically rename to licence.hostname */          /* write licence to licence.hostname.new, then atomically rename to licence.hostname */
1120    
1121          sprintf(path, "%s/.rdesktop/licence.%s", home, hostname);          sprintf(path, "%s/.rdesktop/licence.%s", home, g_hostname);
1122          tmppath = (char *) xmalloc(strlen(path) + sizeof(".new"));          tmppath = (char *) xmalloc(strlen(path) + sizeof(".new"));
1123          strcpy(tmppath, path);          strcpy(tmppath, path);
1124          strcat(tmppath, ".new");          strcat(tmppath, ".new");
# Line 999  save_licence(unsigned char *data, int le Line 1145  save_licence(unsigned char *data, int le
1145          xfree(tmppath);          xfree(tmppath);
1146          xfree(path);          xfree(path);
1147  }  }
1148    
1149    /* Create the bitmap cache directory */
1150    BOOL
1151    rd_pstcache_mkdir(void)
1152    {
1153            char *home;
1154            char bmpcache_dir[256];
1155    
1156            home = getenv("HOME");
1157    
1158            if (home == NULL)
1159                    return False;
1160    
1161            sprintf(bmpcache_dir, "%s/%s", home, ".rdesktop");
1162    
1163            if ((mkdir(bmpcache_dir, S_IRWXU) == -1) && errno != EEXIST)
1164            {
1165                    perror(bmpcache_dir);
1166                    return False;
1167            }
1168    
1169            sprintf(bmpcache_dir, "%s/%s", home, ".rdesktop/cache");
1170    
1171            if ((mkdir(bmpcache_dir, S_IRWXU) == -1) && errno != EEXIST)
1172            {
1173                    perror(bmpcache_dir);
1174                    return False;
1175            }
1176    
1177            return True;
1178    }
1179    
1180    /* open a file in the .rdesktop directory */
1181    int
1182    rd_open_file(char *filename)
1183    {
1184            char *home;
1185            char fn[256];
1186            int fd;
1187    
1188            home = getenv("HOME");
1189            if (home == NULL)
1190                    return -1;
1191            sprintf(fn, "%s/.rdesktop/%s", home, filename);
1192            fd = open(fn, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
1193            if (fd == -1)
1194                    perror(fn);
1195            return fd;
1196    }
1197    
1198    /* close file */
1199    void
1200    rd_close_file(int fd)
1201    {
1202            close(fd);
1203    }
1204    
1205    /* read from file*/
1206    int
1207    rd_read_file(int fd, void *ptr, int len)
1208    {
1209            return read(fd, ptr, len);
1210    }
1211    
1212    /* write to file */
1213    int
1214    rd_write_file(int fd, void *ptr, int len)
1215    {
1216            return write(fd, ptr, len);
1217    }
1218    
1219    /* move file pointer */
1220    int
1221    rd_lseek_file(int fd, int offset)
1222    {
1223            return lseek(fd, offset, SEEK_SET);
1224    }
1225    
1226    /* do a write lock on a file */
1227    BOOL
1228    rd_lock_file(int fd, int start, int len)
1229    {
1230            struct flock lock;
1231    
1232            lock.l_type = F_WRLCK;
1233            lock.l_whence = SEEK_SET;
1234            lock.l_start = start;
1235            lock.l_len = len;
1236            if (fcntl(fd, F_SETLK, &lock) == -1)
1237                    return False;
1238            return True;
1239    }

Legend:
Removed from v.651  
changed lines
  Added in v.800

  ViewVC Help
Powered by ViewVC 1.1.26