Develop and Download Open Source Software

Browse Subversion Repository

Diff of /branches/ssh_chacha20poly1305/ttssh2/ttxssh/ssh.c

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

revision 3083 by yutakapon, Fri Dec 28 12:24:29 2007 UTC revision 3084 by yutakapon, Sat Dec 29 10:21:19 2007 UTC
# Line 95  typedef struct scp { Line 95  typedef struct scp {
95          struct _stat filestat;         // file status information          struct _stat filestat;         // file status information
96          HWND progress;          HWND progress;
97          HANDLE thread;          HANDLE thread;
98            unsigned int tid;
99          PTInstVar pvar;          PTInstVar pvar;
100    
101          // receiving file          // receiving file
# Line 196  static Channel_t *ssh2_channel_new(unsig Line 197  static Channel_t *ssh2_channel_new(unsig
197                  c->scp.state = SCP_INIT;                  c->scp.state = SCP_INIT;
198                  c->scp.progress = NULL;                  c->scp.progress = NULL;
199                  c->scp.thread = (HANDLE)-1;                  c->scp.thread = (HANDLE)-1;
200                    c->scp.sendfp = NULL;
201          }          }
202    
203          return (c);          return (c);
# Line 273  static void ssh2_channel_delete(Channel_ Line 275  static void ssh2_channel_delete(Channel_
275    
276          if (c->type == TYPE_SCP) {          if (c->type == TYPE_SCP) {
277                  c->scp.state = SCP_CLOSING;                  c->scp.state = SCP_CLOSING;
278                  fclose(c->scp.sendfp);                  if (c->scp.sendfp != NULL)
279                            fclose(c->scp.sendfp);
280                  if (c->scp.progress != NULL) {                  if (c->scp.progress != NULL) {
281                          DestroyWindow(c->scp.progress);                          DestroyWindow(c->scp.progress);
282                          c->scp.progress = NULL;                          c->scp.progress = NULL;
# Line 3413  static int SSH_scp_transaction(PTInstVar Line 3416  static int SSH_scp_transaction(PTInstVar
3416                          goto error;                          goto error;
3417                  }                  }
3418          } else { // copy remote to local          } else { // copy remote to local
3419                  fp = fopen(sendfile, "wb");                  // ローカルファイルの保存先はまだ決め打ち (yutaka)
3420                    _snprintf_s(c->scp.sendfilefull, sizeof(c->scp.sendfilefull), _TRUNCATE, "d:\\%s", sendfile);
3421                    strncpy_s(c->scp.sendfile, sizeof(c->scp.sendfile), sendfile, _TRUNCATE);  // full path
3422    
3423                    fp = fopen(c->scp.sendfilefull, "wb");
3424                  if (fp == NULL)                  if (fp == NULL)
3425                          goto error;                          goto error;
3426    
                 strncpy_s(c->scp.sendfilefull, sizeof(c->scp.sendfilefull), sendfile, _TRUNCATE);  
                 strncpy_s(c->scp.sendfile, sizeof(c->scp.sendfile), sendfile, _TRUNCATE);  
3427                  c->scp.sendfp = fp;     // file pointer                  c->scp.sendfp = fp;     // file pointer
3428          }          }
3429    
# Line 3457  int SSH_start_scp(PTInstVar pvar, char * Line 3462  int SSH_start_scp(PTInstVar pvar, char *
3462  {  {
3463          return SSH_scp_transaction(pvar, sendfile, TOLOCAL);          return SSH_scp_transaction(pvar, sendfile, TOLOCAL);
3464    
3465          //return SSH_scp_transaction(pvar, "bigfile100.dat", FROMREMOTE);  #if 0
3466            //return SSH_scp_transaction(pvar, "bigfile30.dat", FROMREMOTE);
3467    #endif
3468  }  }
3469    
3470    
# Line 7209  static unsigned __stdcall ssh_scp_thread Line 7216  static unsigned __stdcall ssh_scp_thread
7216          HWND hWnd = c->scp.progress;          HWND hWnd = c->scp.progress;
7217          scp_dlg_parm_t parm;          scp_dlg_parm_t parm;
7218    
7219          SendMessage(GetDlgItem(hWnd, IDC_FILENAME), WM_SETTEXT, 0, (LPARAM)c->scp.sendfile);          //SendMessage(GetDlgItem(hWnd, IDC_FILENAME), WM_SETTEXT, 0, (LPARAM)c->scp.sendfile);
7220            SendMessage(GetDlgItem(hWnd, IDC_FILENAME), WM_SETTEXT, 0, (LPARAM)c->scp.sendfilefull);
7221    
7222          do {          do {
7223                  // Cancelボタンが押下されたらウィンドウが消える。                  // Cancelボタンが押下されたらウィンドウが消える。
# Line 7319  static void SSH2_scp_tolocal(PTInstVar p Line 7327  static void SSH2_scp_tolocal(PTInstVar p
7327          }          }
7328  }  }
7329    
7330    
7331    #define WM_RECEIVING_FILE (WM_USER + 2)
7332    
7333    static unsigned __stdcall ssh_scp_receive_thread(void FAR * p)
7334    {
7335            Channel_t *c = (Channel_t *)p;
7336            PTInstVar pvar = c->scp.pvar;
7337            long long total_size = 0;
7338            char s[80];
7339            HWND hWnd = c->scp.progress;
7340            MSG msg;
7341            unsigned char *data;
7342            unsigned int buflen;
7343    
7344            for (;;) {
7345                    // Cancelボタンが押下されたらウィンドウが消える。
7346                    if (IsWindowVisible(hWnd) == 0)
7347                            goto cancel_abort;
7348    
7349                    if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) != 0) {
7350                            switch (msg.message) {
7351                            case WM_RECEIVING_FILE:
7352                                    data = (unsigned char *)msg.wParam;
7353                                    buflen = (unsigned int)msg.lParam;
7354    
7355                                    c->scp.filercvsize += buflen;
7356    
7357                                    if (fwrite(data, 1, buflen, c->scp.sendfp) < buflen) { // error
7358                                            // TODO:
7359                                    }
7360    
7361                                    free(data);  // free!
7362    
7363                                    _snprintf_s(s, sizeof(s), _TRUNCATE, "%lld / %lld (%d%%)", c->scp.filercvsize, c->scp.filetotalsize,
7364                                            (100 * c->scp.filercvsize / c->scp.filetotalsize)%100 );
7365                                    SendMessage(GetDlgItem(c->scp.progress, IDC_PROGRESS), WM_SETTEXT, 0, (LPARAM)s);
7366    
7367                                    if (c->scp.filercvsize >= c->scp.filetotalsize) { // EOF
7368                                            c->scp.state = SCP_CLOSING;
7369                                            ShowWindow(c->scp.progress, SW_HIDE);
7370                                            goto done;
7371                                    }
7372    
7373                                    break;
7374                            }
7375                    }
7376                    Sleep(0);
7377            }
7378    
7379    done:
7380            return 0;
7381    
7382    cancel_abort:
7383            ssh2_channel_send_close(pvar, c);
7384            return 0;
7385    }
7386    
7387  static BOOL SSH2_scp_fromremote(PTInstVar pvar, Channel_t *c, unsigned char *data, unsigned int buflen)  static BOOL SSH2_scp_fromremote(PTInstVar pvar, Channel_t *c, unsigned char *data, unsigned int buflen)
7388  {  {
7389          int permission;          int permission;
7390          long long size;          long long size;
7391          char filename[MAX_PATH];          char filename[MAX_PATH];
         char s[80];  
7392          char ch;          char ch;
7393          HWND hDlgWnd;          HWND hDlgWnd;
         MSG msg;  
         int i;  
7394    
7395          if (buflen == 0)          if (buflen == 0)
7396                  return FALSE;                  return FALSE;
# Line 7345  static BOOL SSH2_scp_fromremote(PTInstVa Line 7407  static BOOL SSH2_scp_fromremote(PTInstVa
7407                          goto reply;                          goto reply;
7408    
7409                  } else if (data[0] == 'C') {  // C0666 size file                  } else if (data[0] == 'C') {  // C0666 size file
7410                            HANDLE thread;
7411                            unsigned int tid;
7412    
7413                          sscanf_s(data, "C%o %lld %s", &permission, &size, filename, sizeof(filename));                          sscanf_s(data, "C%o %lld %s", &permission, &size, filename, sizeof(filename));
7414    
7415                          // Windowsなのでパーミッションは無視。サイズのみ記録。                          // Windowsなのでパーミッションは無視。サイズのみ記録。
# Line 7360  static BOOL SSH2_scp_fromremote(PTInstVa Line 7425  static BOOL SSH2_scp_fromremote(PTInstVa
7425                          if (hDlgWnd != NULL) {                          if (hDlgWnd != NULL) {
7426                                  c->scp.progress = hDlgWnd;                                  c->scp.progress = hDlgWnd;
7427                                  SetWindowText(hDlgWnd, "TTSSH: SCP receiving file");                                  SetWindowText(hDlgWnd, "TTSSH: SCP receiving file");
7428                                  SendMessage(GetDlgItem(hDlgWnd, IDC_FILENAME), WM_SETTEXT, 0, (LPARAM)c->scp.sendfile);                                  SendMessage(GetDlgItem(hDlgWnd, IDC_FILENAME), WM_SETTEXT, 0, (LPARAM)c->scp.sendfilefull);
7429                                  ShowWindow(hDlgWnd, SW_SHOW);                                  ShowWindow(hDlgWnd, SW_SHOW);
7430                          }                          }
7431    
7432                          c->scp.thread = (HANDLE)-1;                          thread = (HANDLE)_beginthreadex(NULL, 0, ssh_scp_receive_thread, c, 0, &tid);
7433                            if (thread == (HANDLE)-1) {
7434                                    // TODO:
7435                            }
7436                            c->scp.thread = thread;
7437                            c->scp.tid = tid;
7438    
7439                          goto reply;                          goto reply;
7440    
# Line 7374  static BOOL SSH2_scp_fromremote(PTInstVa Line 7444  static BOOL SSH2_scp_fromremote(PTInstVa
7444                  }                  }
7445    
7446          } else if (c->scp.state == SCP_DATA) {  // payloadの受信          } else if (c->scp.state == SCP_DATA) {  // payloadの受信
7447                  // Cancelボタンが押下されたらウィンドウが消える。                  unsigned char *newdata = malloc(buflen);
7448                  if (IsWindowVisible(c->scp.progress) == 0)                  if (newdata != NULL) {
7449                          goto cancel_abort;                          memcpy(newdata, data, buflen);
7450                            PostThreadMessage(c->scp.tid, WM_RECEIVING_FILE, (WPARAM)newdata, (LPARAM)buflen);
                 c->scp.filercvsize += buflen;  
   
                 if (fwrite(data, 1, buflen, c->scp.sendfp) < buflen) { // error  
                         // TODO:  
                 }  
   
                 if (c->scp.filercvsize >= c->scp.filetotalsize) { // EOF  
                         c->scp.state = SCP_CLOSING;  
7451                  }                  }
7452    
                 _snprintf_s(s, sizeof(s), _TRUNCATE, "%lld / %lld (%d%%)", c->scp.filercvsize, c->scp.filetotalsize,  
                         (100 * c->scp.filercvsize / c->scp.filetotalsize)%100 );  
                 SendMessage(GetDlgItem(c->scp.progress, IDC_PROGRESS), WM_SETTEXT, 0, (LPARAM)s);  
   
                 // 以下の workaround がないと、モードレスダイアログの操作すらできない。  
                 // この処置でもTeraTermウィンドウの操作ができない。要検討。  
 #if 1  
                 for (i = 0 ; i < 10 ; i++) {  
                         if (PeekMessage(&msg, c->scp.progress, 0, 0, PM_REMOVE)) {  
                                 TranslateMessage(&msg);  
                                 DispatchMessage(&msg);  
                         }  
                         Sleep(0);  
                 }  
 #endif  
   
7453          } else if (c->scp.state == SCP_CLOSING) {  // EOFの受信          } else if (c->scp.state == SCP_CLOSING) {  // EOFの受信
7454                  ssh2_channel_send_close(pvar, c);                  ssh2_channel_send_close(pvar, c);
7455    
# Line 7414  reply: Line 7460  reply:
7460          ch = '\0';          ch = '\0';
7461          SSH2_send_channel_data(pvar, c, &ch, 1);          SSH2_send_channel_data(pvar, c, &ch, 1);
7462          return TRUE;          return TRUE;
   
 cancel_abort:  
         ssh2_channel_send_close(pvar, c);  
   
         return TRUE;  
7463  }  }
7464    
7465    

Legend:
Removed from v.3083  
changed lines
  Added in v.3084

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26