| 1 |
/* |
| 2 |
* (C) 2021- TeraTerm Project |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions |
| 7 |
* are met: |
| 8 |
* |
| 9 |
* 1. Redistributions of source code must retain the above copyright |
| 10 |
* notice, this list of conditions and the following disclaimer. |
| 11 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 12 |
* notice, this list of conditions and the following disclaimer in the |
| 13 |
* documentation and/or other materials provided with the distribution. |
| 14 |
* 3. The name of the author may not be used to endorse or promote products |
| 15 |
* derived from this software without specific prior written permission. |
| 16 |
* |
| 17 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR |
| 18 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 19 |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 20 |
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 21 |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 22 |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 26 |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 |
*/ |
| 28 |
|
| 29 |
#include "ttxssh.h" |
| 30 |
#include "comp.h" |
| 31 |
#include "kex.h" |
| 32 |
|
| 33 |
|
| 34 |
struct ssh2_comp_t { |
| 35 |
compression_type type; |
| 36 |
char *name; |
| 37 |
}; |
| 38 |
|
| 39 |
static const struct ssh2_comp_t ssh2_comps[] = { |
| 40 |
{COMP_NOCOMP, "none"}, // RFC4253 |
| 41 |
{COMP_ZLIB, "zlib"}, // RFC4253 |
| 42 |
{COMP_DELAYED, "zlib@openssh.com"}, |
| 43 |
{COMP_NONE, NULL}, |
| 44 |
}; |
| 45 |
|
| 46 |
|
| 47 |
char* get_ssh2_comp_name(compression_type type) |
| 48 |
{ |
| 49 |
const struct ssh2_comp_t *ptr = ssh2_comps; |
| 50 |
|
| 51 |
while (ptr->name != NULL) { |
| 52 |
if (type == ptr->type) { |
| 53 |
return ptr->name; |
| 54 |
} |
| 55 |
ptr++; |
| 56 |
} |
| 57 |
|
| 58 |
// not found. |
| 59 |
return "unknown"; |
| 60 |
} |
| 61 |
|
| 62 |
void normalize_comp_order(char *buf) |
| 63 |
{ |
| 64 |
static char default_strings[] = { |
| 65 |
COMP_DELAYED, |
| 66 |
COMP_ZLIB, |
| 67 |
COMP_NOCOMP, |
| 68 |
COMP_NONE, |
| 69 |
}; |
| 70 |
|
| 71 |
normalize_generic_order(buf, default_strings, NUM_ELEM(default_strings)); |
| 72 |
} |
| 73 |
|
| 74 |
compression_type choose_SSH2_compression_algorithm(char *server_proposal, char *my_proposal) |
| 75 |
{ |
| 76 |
compression_type type = COMP_UNKNOWN; |
| 77 |
char str_comp[20]; |
| 78 |
const struct ssh2_comp_t *ptr = ssh2_comps; |
| 79 |
|
| 80 |
choose_SSH2_proposal(server_proposal, my_proposal, str_comp, sizeof(str_comp)); |
| 81 |
|
| 82 |
while (ptr->name != NULL) { |
| 83 |
if (strcmp(ptr->name, str_comp) == 0) { |
| 84 |
type = ptr->type; |
| 85 |
break; |
| 86 |
} |
| 87 |
ptr++; |
| 88 |
} |
| 89 |
|
| 90 |
return (type); |
| 91 |
} |
| 92 |
|
| 93 |
void SSH2_update_compression_myproposal(PTInstVar pvar) |
| 94 |
{ |
| 95 |
static char buf[128]; // TODO: malloc()�������� |
| 96 |
int index; |
| 97 |
int len, i; |
| 98 |
|
| 99 |
// ���M�������������������������L�[������ |
| 100 |
// �L�[������������������������ |
| 101 |
if (pvar->socket != INVALID_SOCKET) { |
| 102 |
return; |
| 103 |
} |
| 104 |
|
| 105 |
// ���k���x�����������Amyproposal[]�������������B(2005.7.9 yutaka) |
| 106 |
buf[0] = '\0'; |
| 107 |
for (i = 0 ; pvar->settings.CompOrder[i] != 0 ; i++) { |
| 108 |
index = pvar->settings.CompOrder[i] - '0'; |
| 109 |
if (index == COMP_NONE) // disabled line |
| 110 |
break; |
| 111 |
strncat_s(buf, sizeof(buf), get_ssh2_comp_name(index), _TRUNCATE); |
| 112 |
strncat_s(buf, sizeof(buf), ",", _TRUNCATE); |
| 113 |
} |
| 114 |
len = strlen(buf); |
| 115 |
if (len > 0) |
| 116 |
buf[len - 1] = '\0'; // get rid of comma |
| 117 |
|
| 118 |
// ���k�w���������������A���k���x�������������[���������B |
| 119 |
if (buf[0] == '\0') { |
| 120 |
pvar->settings.CompressionLevel = 0; |
| 121 |
} |
| 122 |
|
| 123 |
if (pvar->settings.CompressionLevel == 0) { |
| 124 |
_snprintf_s(buf, sizeof(buf), _TRUNCATE, get_ssh2_comp_name(COMP_NOCOMP)); |
| 125 |
} |
| 126 |
if (buf[0] != '\0') { |
| 127 |
myproposal[PROPOSAL_COMP_ALGS_CTOS] = buf; // Client To Server |
| 128 |
myproposal[PROPOSAL_COMP_ALGS_STOC] = buf; // Server To Client |
| 129 |
} |
| 130 |
} |