Develop and Download Open Source Software

Browse Subversion Repository

Diff of /trunk/ttssh2/ttxssh/pkt.c

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

ttssh2/trunk/ttxssh/pkt.c revision 3003 by maya, Mon Aug 20 14:21:57 2007 UTC trunk/ttssh2/ttxssh/pkt.c revision 6969 by doda, Thu Nov 2 11:37:41 2017 UTC
# Line 1  Line 1 
1  /*  /*
2  Copyright (c) 1998-2001, Robert O'Callahan   * Copyright (c) 1998-2001, Robert O'Callahan
3  All rights reserved.   * (C) 2004-2017 TeraTerm Project
4     * All rights reserved.
5  Redistribution and use in source and binary forms, with or without modification,   *
6  are permitted provided that the following conditions are met:   * Redistribution and use in source and binary forms, with or without
7     * modification, are permitted provided that the following conditions
8  Redistributions of source code must retain the above copyright notice, this list of   * are met:
9  conditions and the following disclaimer.   *
10     * 1. Redistributions of source code must retain the above copyright
11  Redistributions in binary form must reproduce the above copyright notice, this list   *    notice, this list of conditions and the following disclaimer.
12  of conditions and the following disclaimer in the documentation and/or other materials   * 2. Redistributions in binary form must reproduce the above copyright
13  provided with the distribution.   *    notice, this list of conditions and the following disclaimer in the
14     *    documentation and/or other materials provided with the distribution.
15  The name of Robert O'Callahan may not be used to endorse or promote products derived from   * 3. The name of the author may not be used to endorse or promote products
16  this software without specific prior written permission.   *    derived from this software without specific prior written permission.
17     *
18  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND   * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
19  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES   * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,   * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF   * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  */   * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28     */
29  /*  
30  This code is copyright (C) 1998-1999 Robert O'Callahan.  /*
31  See LICENSE.TXT for the license.  This code is copyright (C) 1998-1999 Robert O'Callahan.
32  */  See LICENSE.TXT for the license.
33    */
34  #include "ttxssh.h"  
35  #include "util.h"  #include "ttxssh.h"
36    #include "util.h"
37  //#define READAMOUNT 60000  #include "pkt.h"
38  // 60000 -> 65536 へ拡張。SSH2ではwindow制御を行うため、SSH2のwindow sizeと  
39  // 合わせておく必要がある。(2004.10.17 yutaka)  #define READAMOUNT CHAN_SES_WINDOW_DEFAULT
40  #define READAMOUNT 65536  
41    void PKT_init(PTInstVar pvar)
42  void PKT_init(PTInstVar pvar)  {
43  {          buf_create(&pvar->pkt_state.buf, &pvar->pkt_state.buflen);
44          buf_create(&pvar->pkt_state.buf, &pvar->pkt_state.buflen);          pvar->pkt_state.datastart = 0;
45          pvar->pkt_state.datastart = 0;          pvar->pkt_state.datalen = 0;
46          pvar->pkt_state.datalen = 0;          pvar->pkt_state.seen_server_ID = FALSE;
47          pvar->pkt_state.seen_server_ID = FALSE;          pvar->pkt_state.seen_newline = FALSE;
48          pvar->pkt_state.seen_newline = FALSE;          pvar->pkt_state.predecrypted_packet = FALSE;
49          pvar->pkt_state.predecrypted_packet = FALSE;  }
50  }  
51    /* Read some data, leave no more than up_to_amount bytes in the buffer,
52  /* Read some data, leave no more than up_to_amount bytes in the buffer,     return the number of bytes read or -1 on error or blocking. */
53     return the number of bytes read or -1 on error or blocking. */  static int recv_data(PTInstVar pvar, unsigned long up_to_amount)
54  static int recv_data(PTInstVar pvar, unsigned long up_to_amount)  {
55  {          int amount_read;
56          int amount_read;  
57            /* Shuffle data to the start of the buffer */
58          /* Shuffle data to the start of the buffer */          if (pvar->pkt_state.datastart != 0) {
59          if (pvar->pkt_state.datastart != 0) {                  memmove(pvar->pkt_state.buf,
60                  memmove(pvar->pkt_state.buf,                          pvar->pkt_state.buf + pvar->pkt_state.datastart,
61                          pvar->pkt_state.buf + pvar->pkt_state.datastart,                          pvar->pkt_state.datalen);
62                          pvar->pkt_state.datalen);                  pvar->pkt_state.datastart = 0;
63                  pvar->pkt_state.datastart = 0;          }
64          }  
65            buf_ensure_size(&pvar->pkt_state.buf, &pvar->pkt_state.buflen, up_to_amount);
66          buf_ensure_size(&pvar->pkt_state.buf, &pvar->pkt_state.buflen,  
67                          up_to_amount);          _ASSERT(pvar->pkt_state.buf != NULL);
68    
69          _ASSERT(pvar->pkt_state.buf != NULL);          amount_read = (pvar->Precv) (pvar->socket,
70                                         pvar->pkt_state.buf + pvar->pkt_state.datalen,
71          amount_read = (pvar->Precv) (pvar->socket,                                       up_to_amount - pvar->pkt_state.datalen,
72                                       pvar->pkt_state.buf +                                       0);
73                                       pvar->pkt_state.datalen,  
74                                       up_to_amount - pvar->pkt_state.datalen,          if (amount_read > 0) {
75                                       0);                  /* Update seen_newline if necessary */
76                    if (!pvar->pkt_state.seen_server_ID && !pvar->pkt_state.seen_newline) {
77          if (amount_read > 0) {                          int i;
78                  /* Update seen_newline if necessary */  
79                  if (!pvar->pkt_state.seen_server_ID                          for (i = 0; i < amount_read; i++) {
80                   && !pvar->pkt_state.seen_newline) {                                  if (pvar->pkt_state.buf[pvar->pkt_state.datalen + i] == '\n') {
81                          int i;                                          pvar->pkt_state.seen_newline = 1;
82                                    }
83                          for (i = 0; i < amount_read; i++) {                          }
84                                  if (pvar->pkt_state.buf[pvar->pkt_state.datalen + i] ==                  }
85                                      '\n') {                  pvar->pkt_state.datalen += amount_read;
86                                          pvar->pkt_state.seen_newline = 1;          }
87                                  }  
88                          }          return amount_read;
89                  }  }
90    
91                  pvar->pkt_state.datalen += amount_read;  
92          }  // 改行コードが出てくるまで読む
93    static int recv_line_data(PTInstVar pvar)
94          return amount_read;  {
95  }          int amount_read;
96            char buf[256];
97            size_t up_to_amount = sizeof(buf);
98  // 改行コードが出てくるまで読む          int i;
99  static int recv_line_data(PTInstVar pvar)  
100  {          /* Shuffle data to the start of the buffer */
101          int amount_read;          if (pvar->pkt_state.datastart != 0) {
102          char buf[256];                  memmove(pvar->pkt_state.buf,
103          size_t up_to_amount = sizeof(buf);                          pvar->pkt_state.buf + pvar->pkt_state.datastart,
104          int i;                          pvar->pkt_state.datalen);
105                    pvar->pkt_state.datastart = 0;
106          /* Shuffle data to the start of the buffer */          }
107          if (pvar->pkt_state.datastart != 0) {  
108                  memmove(pvar->pkt_state.buf,          buf_ensure_size(&pvar->pkt_state.buf, &pvar->pkt_state.buflen, up_to_amount);
109                          pvar->pkt_state.buf + pvar->pkt_state.datastart,  
110                          pvar->pkt_state.datalen);          for (i = 0 ; i < (int)up_to_amount ; i++) {
111                  pvar->pkt_state.datastart = 0;                  amount_read = (pvar->Precv) (pvar->socket, &buf[i], 1, 0);
112          }                  if (amount_read != 1) {
113                            return 0; // error
114          buf_ensure_size(&pvar->pkt_state.buf, &pvar->pkt_state.buflen,                  }
115                          up_to_amount);  
116                    pvar->pkt_state.datalen += amount_read;
117          for (i = 0 ; i < (int)up_to_amount ; i++) {  
118                  amount_read = (pvar->Precv) (pvar->socket,                  if (buf[i] == '\n') { // 0x0a
119                                               &buf[i], 1, 0);                          buf[i+1] = 0;
120                  if (amount_read != 1) {                          break;
121                          return 0; // error                  }
122                  }          }
123            amount_read = i + 1; // 読み込みサイズ(LFも含む)
124                  pvar->pkt_state.datalen += amount_read;          memcpy(pvar->pkt_state.buf, buf, amount_read);
125    
126                  if (buf[i] == '\n') { // 0x0a          pvar->pkt_state.seen_newline = 1;
127                          buf[i+1] = 0;  
128                          break;          return amount_read;
129                  }  }
130          }  
131          amount_read = i + 1; // 読み込みサイズ(LFも含む)  
132          memcpy(pvar->pkt_state.buf, buf, amount_read);  /* This function does two things:
133       -- reads data from the sshd and feeds the SSH protocol packets to ssh.c
134          pvar->pkt_state.seen_newline = 1;     -- copies any available decrypted session data into the application buffer
135    */
136          return amount_read;  int PKT_recv(PTInstVar pvar, char *buf, int buflen)
137  }  {
138            int amount_in_buf = 0;
139            BOOL connection_closed = FALSE;
140  /* This function does two things:  
141     -- reads data from the sshd and feeds the SSH protocol packets to ssh.c          while (SSH_is_any_payload(pvar) ? buflen > 0 : !connection_closed) {
142     -- copies any available decrypted session data into the application buffer                  if (SSH_is_any_payload(pvar)) {
143  */                          /* ssh.c has some session data for us to give to Tera Term. */
144  int PKT_recv(PTInstVar pvar, char FAR * buf, int buflen)                          int grabbed = SSH_extract_payload(pvar, buf, buflen);
145  {  
146          int amount_in_buf = 0;                          amount_in_buf += grabbed;
147          BOOL connection_closed = FALSE;                          buf += grabbed;
148                            buflen -= grabbed;
149          while (SSH_is_any_payload(pvar) ? buflen > 0 : !connection_closed) {                  }
150                  if (SSH_is_any_payload(pvar)) {                  else if (!pvar->pkt_state.seen_server_ID && (pvar->pkt_state.seen_newline || pvar->pkt_state.datalen >= 255)) {
151                          /* ssh.c has some session data for us to give to Teraterm. */                          /*
152                          int grabbed = SSH_extract_payload(pvar, buf, buflen);                           * We're looking for the initial ID string and either we've seen the
153                             * terminating newline, or we've exceeded the limit at which we should see a newline.
154                          amount_in_buf += grabbed;                           */
155                          buf += grabbed;                          unsigned int i;
156                          buflen -= grabbed;  
157                            for (i = 0; pvar->pkt_state.buf[i] != '\n' && i < pvar->pkt_state.datalen; i++) {
158                  } else if (!pvar->pkt_state.seen_server_ID &&                          }
159                             (pvar->pkt_state.seen_newline                          if (pvar->pkt_state.buf[i] == '\n') {
160                              || pvar->pkt_state.datalen >= 255)) {                                  i++;
161                          /* We're looking for the initial ID string and either we've seen the                          }
162                             terminating newline, or we've exceeded the limit at which we should see  
163                             a newline. */                          // SSHサーバのバージョンチェックを行う
164                          unsigned int i;                          if (SSH_handle_server_ID(pvar, pvar->pkt_state.buf, i)) {
165                                    pvar->pkt_state.seen_server_ID = 1;
166                          for (i = 0;  
167                               pvar->pkt_state.buf[i] != '\n'                                  if (SSHv2(pvar)) {
168                               && i < pvar->pkt_state.datalen; i++) {                                          // send Key Exchange Init
169                          }                                          SSH2_send_kexinit(pvar);
170                          if (pvar->pkt_state.buf[i] == '\n') {                                  }
171                                  i++;                          } else {
172                          }                                  // reset flag to re-read server ID (2008.1.24 yutaka)
173                                    pvar->pkt_state.seen_newline = 0;
174                          // SSHサーバのバージョンチェックを行う                          }
175                          if (SSH_handle_server_ID(pvar, pvar->pkt_state.buf, i)) {  
176                                  pvar->pkt_state.seen_server_ID = 1;                          pvar->pkt_state.datastart += i;
177                            pvar->pkt_state.datalen -= i;
178                                  if (SSHv1(pvar)) {                  }
179                    else if (pvar->pkt_state.seen_server_ID && pvar->pkt_state.datalen >= (unsigned int) SSH_get_min_packet_size(pvar)) {
180                                  } else { // for SSH2(yutaka)                          char *data = pvar->pkt_state.buf + pvar->pkt_state.datastart;
181                                          // send Key Exchange Init                          uint32 padding;
182                                          SSH2_send_kexinit(pvar);                          uint32 pktsize;
183                                  }                          uint32 total_packet_size;
184                            struct Mac *mac = &pvar->ssh2_keys[MODE_IN].mac;
185                          }                          int etm;
186    
187                          pvar->pkt_state.datastart += i;                          etm = mac && mac->enabled && mac->etm;
188                          pvar->pkt_state.datalen -= i;  
189                            // 暗号化パケットの一部を復号化する。
190                  } else if (pvar->pkt_state.seen_server_ID                          if (!pvar->pkt_state.predecrypted_packet && !etm) {
191                             && pvar->pkt_state.datalen >=                                  SSH_predecrpyt_packet(pvar, data);
192                             (unsigned int) SSH_get_min_packet_size(pvar)) {                                  pvar->pkt_state.predecrypted_packet = TRUE;
193                          char FAR *data =                          }
194                                  pvar->pkt_state.buf + pvar->pkt_state.datastart;  
195                          uint32 padding;                          if (SSHv1(pvar)) {
196                          uint32 pktsize;                                  uint32 realpktsize = get_uint32_MSBfirst(data);
197                          uint32 total_packet_size;  
198                                    padding = 8 - (realpktsize % 8);
199                          //debug_print(10, data, pvar->pkt_state.datalen);                                  pktsize = realpktsize + padding;
200                            } else {
201                          // SSH2なら暗号化パケットの一部を復号化する。                                  // SSH2のパケットは先頭に packet-size(4)+padding(1)+type(1) が続く。
202                          if (!pvar->pkt_state.predecrypted_packet) {                                  pktsize = get_uint32_MSBfirst(data);
203                                  //DEBUG_PRINT_TO_FILE(0, data, pvar->pkt_state.datalen);                                  if (etm) {
204                                  SSH_predecrpyt_packet(pvar, data);                                          padding = 0;
205                                    }
206                                  if (SSHv1(pvar)) {                                  else {
207                                          pvar->pkt_state.predecrypted_packet = TRUE;                                          padding = (unsigned char) data[4];
208                                  } else { // for SSH2(yutaka)                                  }
209                                          // do nothing                          }
210                                          pvar->pkt_state.predecrypted_packet = TRUE;  
211                                  }                          // パケット(TCPペイロード)の全体のサイズは、SSHペイロード+4(+MAC)となる。
212                          }                          // +4は、SSHペイロードのサイズを格納している部分(int型)。
213                            total_packet_size = pktsize + 4 + SSH_get_clear_MAC_size(pvar);
214                          if (SSHv1(pvar)) {  
215                                  uint32 realpktsize = get_uint32_MSBfirst(data);                          if (total_packet_size <= pvar->pkt_state.datalen) {
216                                    /* the data must be 4 byte aligned. */
217                                  padding = 8 - (realpktsize % 8);                                  if (SSHv1(pvar)) {
218                                  pktsize = realpktsize + padding;                                          SSH_handle_packet1(pvar, data, pktsize, padding);
219                          } else {                                  }
220                                  // SSH2のパケットは先頭に packet-size(4)+padding(1)+type(1) が続く。                                  else {
221                                  pktsize = get_uint32_MSBfirst(data);                                          SSH_handle_packet2(pvar, data, pktsize, padding, etm);
222                                  padding = (unsigned char) data[4];                                  }
223                          }                                  pvar->pkt_state.predecrypted_packet = FALSE;
224    
225                          // パケット(TCPペイロード)の全体のサイズは、SSHペイロード+4(+MAC)となる。                                  pvar->pkt_state.datastart += total_packet_size;
226                          // +4は、SSHペイロードのサイズを格納している部分(int型)。                                  pvar->pkt_state.datalen -= total_packet_size;
227                          total_packet_size = pktsize + 4 + SSH_get_clear_MAC_size(pvar);  
228                            } else if (total_packet_size > PACKET_MAX_SIZE) {
229                          if (total_packet_size <= pvar->pkt_state.datalen) {                                  // 4MBを超える巨大なパケットが届いたら、異常終了する。
230                                  /* the data must be 4 byte aligned. */                                  // 実際にはデータ化けで復号失敗時に、誤認識することが多い。
231                                  SSH_handle_packet(pvar, data, pktsize, padding);                                  UTIL_get_lang_msg("MSG_PKT_OVERSIZED_ERROR", pvar,
232                                  pvar->pkt_state.predecrypted_packet = FALSE;                                                    "Oversized packet received from server; connection will close.");
233                                    notify_fatal_error(pvar, pvar->ts->UIMsg, TRUE);
234                                  pvar->pkt_state.datastart += total_packet_size;                          } else {
235                                  pvar->pkt_state.datalen -= total_packet_size;                                  int amount_read = recv_data(pvar, max(total_packet_size, READAMOUNT));
236    
237                          } else if (total_packet_size > 4 * 1024 * 1024) {                                  if (amount_read == SOCKET_ERROR) {
238                                  // 4MBを超える巨大なパケットが届いたら、異常終了する。                                          if (amount_in_buf == 0) {
239                                  // 実際にはデータ化けで復号失敗時に、誤認識することが多い。                                                  return SOCKET_ERROR;
240                                  UTIL_get_lang_msg("MSG_PKT_OVERSIZED_ERROR", pvar,                                          } else {
241                                                                    "Oversized packet received from server; connection will close.");                                                  return amount_in_buf;
242                                  notify_fatal_error(pvar, pvar->ts->UIMsg);                                          }
243                          } else {                                  } else {
244                                  int amount_read =                                          if (amount_read == 0) {
245                                          recv_data(pvar, max(total_packet_size, READAMOUNT));                                                  connection_closed = TRUE;
246                                            }
247                                  if (amount_read == SOCKET_ERROR) {                                  }
248                                          if (amount_in_buf == 0) {                          }
249                                                  return SOCKET_ERROR;                  } else {
250                                          } else {                          // パケットの受信
251                                                  return amount_in_buf;                          int amount_read;
252                                          }  
253                                  } else {                          amount_read = recv_data(pvar, READAMOUNT);
254                                          if (amount_read == 0) {  
255                                                  connection_closed = TRUE;                          if (amount_read == SOCKET_ERROR) {
256                                          }                                  if (amount_in_buf == 0) {
257                                  }                                          return SOCKET_ERROR;
258                          }                                  } else {
259                                            return amount_in_buf;
260                                    }
261                  } else {                          } else if (amount_read == 0) {
262                          // パケットの受信(最大60KB)                                  connection_closed = TRUE;
263                          int amount_read;                          }
264                    }
265                          if (pvar->pkt_state.seen_server_ID == 0) {  
266                                  //amount_read = recv_line_data(pvar);                  if (pvar->fatal_error) {
267                                  amount_read = recv_data(pvar, READAMOUNT);                          return amount_in_buf;
268                    }
269                          } else {          }
270                                  amount_read = recv_data(pvar, READAMOUNT);  
271            if (SSH_is_any_payload(pvar)) {
272                          }                  PostMessage(pvar->NotificationWindow, WM_USER_COMMNOTIFY, pvar->socket, MAKELPARAM(FD_READ, 0));
273            }
274                          if (amount_read == SOCKET_ERROR) {  
275                                  if (amount_in_buf == 0) {          return amount_in_buf;
276                                          return SOCKET_ERROR;  }
277                                  } else {  
278                                          return amount_in_buf;  void PKT_end(PTInstVar pvar)
279                                  }  {
280                          } else if (amount_read == 0) {          buf_destroy(&pvar->pkt_state.buf, &pvar->pkt_state.buflen);
281                                  connection_closed = TRUE;  }
                         }  
                 }  
   
                 if (pvar->fatal_error) {  
                         return amount_in_buf;  
                 }  
         }  
   
         if (SSH_is_any_payload(pvar)) {  
                 PostMessage(pvar->NotificationWindow, WM_USER_COMMNOTIFY,  
                             pvar->socket, MAKELPARAM(FD_READ, 0));  
         }  
   
         return amount_in_buf;  
 }  
   
 void PKT_end(PTInstVar pvar)  
 {  
         buf_destroy(&pvar->pkt_state.buf, &pvar->pkt_state.buflen);  
 }  

Legend:
Removed from v.3003  
changed lines
  Added in v.6969

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