Develop and Download Open Source Software

Browse Subversion Repository

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 6972 - (show annotations) (download) (as text)
Tue Nov 7 22:26:20 2017 UTC (6 years, 4 months ago) by doda
File MIME type: text/x-csrc
File size: 9218 byte(s)
EtM 絡みのコード整理。

・コメント追加
・読みやすいようにコードを修正
1 /*
2 * Copyright (c) 1998-2001, Robert O'Callahan
3 * (C) 2004-2017 TeraTerm Project
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (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 /*
31 This code is copyright (C) 1998-1999 Robert O'Callahan.
32 See LICENSE.TXT for the license.
33 */
34
35 #include "ttxssh.h"
36 #include "util.h"
37 #include "pkt.h"
38
39 #define READAMOUNT CHAN_SES_WINDOW_DEFAULT
40
41 void PKT_init(PTInstVar pvar)
42 {
43 buf_create(&pvar->pkt_state.buf, &pvar->pkt_state.buflen);
44 pvar->pkt_state.datastart = 0;
45 pvar->pkt_state.datalen = 0;
46 pvar->pkt_state.seen_server_ID = FALSE;
47 pvar->pkt_state.seen_newline = FALSE;
48 pvar->pkt_state.predecrypted_packet = FALSE;
49 }
50
51 /* Read some data, leave no more than up_to_amount bytes in the buffer,
52 return the number of bytes read or -1 on error or blocking. */
53 static int recv_data(PTInstVar pvar, unsigned long up_to_amount)
54 {
55 int amount_read;
56
57 /* Shuffle data to the start of the buffer */
58 if (pvar->pkt_state.datastart != 0) {
59 memmove(pvar->pkt_state.buf,
60 pvar->pkt_state.buf + pvar->pkt_state.datastart,
61 pvar->pkt_state.datalen);
62 pvar->pkt_state.datastart = 0;
63 }
64
65 buf_ensure_size(&pvar->pkt_state.buf, &pvar->pkt_state.buflen, up_to_amount);
66
67 _ASSERT(pvar->pkt_state.buf != NULL);
68
69 amount_read = (pvar->Precv) (pvar->socket,
70 pvar->pkt_state.buf + pvar->pkt_state.datalen,
71 up_to_amount - pvar->pkt_state.datalen,
72 0);
73
74 if (amount_read > 0) {
75 /* Update seen_newline if necessary */
76 if (!pvar->pkt_state.seen_server_ID && !pvar->pkt_state.seen_newline) {
77 int i;
78
79 for (i = 0; i < amount_read; i++) {
80 if (pvar->pkt_state.buf[pvar->pkt_state.datalen + i] == '\n') {
81 pvar->pkt_state.seen_newline = 1;
82 }
83 }
84 }
85 pvar->pkt_state.datalen += amount_read;
86 }
87
88 return amount_read;
89 }
90
91 // ���s�R�[�h���o��������������
92 static int recv_line_data(PTInstVar pvar)
93 {
94 int amount_read;
95 char buf[256];
96 size_t up_to_amount = sizeof(buf);
97 int i;
98
99 /* Shuffle data to the start of the buffer */
100 if (pvar->pkt_state.datastart != 0) {
101 memmove(pvar->pkt_state.buf,
102 pvar->pkt_state.buf + pvar->pkt_state.datastart,
103 pvar->pkt_state.datalen);
104 pvar->pkt_state.datastart = 0;
105 }
106
107 buf_ensure_size(&pvar->pkt_state.buf, &pvar->pkt_state.buflen, up_to_amount);
108
109 for (i = 0 ; i < (int)up_to_amount ; i++) {
110 amount_read = (pvar->Precv) (pvar->socket, &buf[i], 1, 0);
111 if (amount_read != 1) {
112 return 0; // error
113 }
114
115 pvar->pkt_state.datalen += amount_read;
116
117 if (buf[i] == '\n') { // 0x0a
118 buf[i+1] = 0;
119 break;
120 }
121 }
122 amount_read = i + 1; // ���������T�C�Y�iLF�������j
123 memcpy(pvar->pkt_state.buf, buf, amount_read);
124
125 pvar->pkt_state.seen_newline = 1;
126
127 return amount_read;
128 }
129
130 /* This function does two things:
131 -- reads data from the sshd and feeds the SSH protocol packets to ssh.c
132 -- copies any available decrypted session data into the application buffer
133 */
134 int PKT_recv(PTInstVar pvar, char *buf, int buflen)
135 {
136 int amount_in_buf = 0;
137 BOOL connection_closed = FALSE;
138
139 while (SSH_is_any_payload(pvar) ? buflen > 0 : !connection_closed) {
140 if (SSH_is_any_payload(pvar)) {
141 /* ssh.c has some session data for us to give to Tera Term. */
142 int grabbed = SSH_extract_payload(pvar, buf, buflen);
143
144 amount_in_buf += grabbed;
145 buf += grabbed;
146 buflen -= grabbed;
147 }
148 else if (!pvar->pkt_state.seen_server_ID && (pvar->pkt_state.seen_newline || pvar->pkt_state.datalen >= 255)) {
149 /*
150 * We're looking for the initial ID string and either we've seen the
151 * terminating newline, or we've exceeded the limit at which we should see a newline.
152 */
153 unsigned int i;
154
155 for (i = 0; pvar->pkt_state.buf[i] != '\n' && i < pvar->pkt_state.datalen; i++) {
156 }
157 if (pvar->pkt_state.buf[i] == '\n') {
158 i++;
159 }
160
161 // SSH�T�[�o���o�[�W�����`�F�b�N���s��
162 if (SSH_handle_server_ID(pvar, pvar->pkt_state.buf, i)) {
163 pvar->pkt_state.seen_server_ID = 1;
164
165 if (SSHv2(pvar)) {
166 // send Key Exchange Init
167 SSH2_send_kexinit(pvar);
168 }
169 } else {
170 // reset flag to re-read server ID (2008.1.24 yutaka)
171 pvar->pkt_state.seen_newline = 0;
172 }
173
174 pvar->pkt_state.datastart += i;
175 pvar->pkt_state.datalen -= i;
176 }
177 else if (pvar->pkt_state.seen_server_ID && pvar->pkt_state.datalen >= (unsigned int) SSH_get_min_packet_size(pvar)) {
178 char *data = pvar->pkt_state.buf + pvar->pkt_state.datastart;
179 uint32 padding;
180 uint32 pktsize;
181 uint32 total_packet_size;
182 struct Mac *mac = &pvar->ssh2_keys[MODE_IN].mac;
183 int etm;
184
185 etm = mac && mac->enabled && mac->etm;
186
187 /*
188 * ������ MAC ���� (E&M: Encrypt & MAC) �����p�P�b�g�����������������������������A
189 * ������ 1 �u���b�N�����������BMAC ������ EtM (Encrypt then MAC) ������
190 * �p�P�b�g�������������������������������������K�v�����B
191 */
192 if (!pvar->pkt_state.predecrypted_packet && !etm) {
193 SSH_predecrpyt_packet(pvar, data);
194 pvar->pkt_state.predecrypted_packet = TRUE;
195 }
196
197 if (SSHv1(pvar)) {
198 uint32 realpktsize = get_uint32_MSBfirst(data);
199
200 padding = 8 - (realpktsize % 8);
201 pktsize = realpktsize + padding;
202 } else {
203 // SSH2 �����p�P�b�g�������� uint32 (4�o�C�g) ���p�P�b�g��������
204 pktsize = get_uint32_MSBfirst(data);
205
206 // ���� 1 �o�C�g�� padding ������
207 if (etm) {
208 // EtM ���� padding length ���~�������������������B
209 // �������_�������������������������� padding length �������������B
210 // ���� 0 ���������u���B
211 padding = 0;
212 }
213 else {
214 // E&M ������������
215 padding = (unsigned char) data[4];
216 }
217 }
218
219 // �p�P�b�g(TCP�y�C���[�h)���S�����T�C�Y���ASSH�y�C���[�h+4�i+MAC�j�������B
220 // +4���ASSH�y�C���[�h���T�C�Y���i�[�������������iint�^�j�B
221 total_packet_size = pktsize + 4 + SSH_get_clear_MAC_size(pvar);
222
223 if (total_packet_size <= pvar->pkt_state.datalen) {
224 // ���M�����f�[�^���\���L���������p�P�b�g�����������s��
225 if (SSHv1(pvar)) {
226 // SSH1 �� EtM ������
227 SSH_handle_packet1(pvar, data, pktsize, padding);
228 }
229 else {
230 SSH_handle_packet2(pvar, data, pktsize, padding, etm);
231 }
232
233 pvar->pkt_state.predecrypted_packet = FALSE;
234 pvar->pkt_state.datastart += total_packet_size;
235 pvar->pkt_state.datalen -= total_packet_size;
236
237 } else if (total_packet_size > PACKET_MAX_SIZE) {
238 // �p�P�b�g�������������������������I�������B
239 // �����������������v�����������s���p�P�b�g���������������������������B
240 UTIL_get_lang_msg("MSG_PKT_OVERSIZED_ERROR", pvar,
241 "Oversized packet received from server; connection will close.");
242 notify_fatal_error(pvar, pvar->ts->UIMsg, TRUE);
243 } else {
244 int amount_read = recv_data(pvar, max(total_packet_size, READAMOUNT));
245
246 if (amount_read == SOCKET_ERROR) {
247 if (amount_in_buf == 0) {
248 return SOCKET_ERROR;
249 } else {
250 return amount_in_buf;
251 }
252 } else {
253 if (amount_read == 0) {
254 connection_closed = TRUE;
255 }
256 }
257 }
258 } else {
259 // �p�P�b�g�����M
260 int amount_read;
261
262 amount_read = recv_data(pvar, READAMOUNT);
263
264 if (amount_read == SOCKET_ERROR) {
265 if (amount_in_buf == 0) {
266 return SOCKET_ERROR;
267 } else {
268 return amount_in_buf;
269 }
270 } else if (amount_read == 0) {
271 connection_closed = TRUE;
272 }
273 }
274
275 if (pvar->fatal_error) {
276 return amount_in_buf;
277 }
278 }
279
280 if (SSH_is_any_payload(pvar)) {
281 PostMessage(pvar->NotificationWindow, WM_USER_COMMNOTIFY, pvar->socket, MAKELPARAM(FD_READ, 0));
282 }
283
284 return amount_in_buf;
285 }
286
287 void PKT_end(PTInstVar pvar)
288 {
289 buf_destroy(&pvar->pkt_state.buf, &pvar->pkt_state.buflen);
290 }

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