Collapse state flags into single bitmask attribute

This commit is contained in:
Sara Golemon 2005-02-03 05:41:35 +00:00
parent 2c5c54e999
commit a05bf84ecf
6 changed files with 22 additions and 17 deletions

2
README
View File

@ -4,6 +4,8 @@ libssh2 - SSH2 library
Version 0.6 Version 0.6
----------- -----------
Collapsed exchanging_keys/newkeys/authenticated flags into single state attribute.
Fix zlib compression issue when internal buffer state misses partial sync. Fix zlib compression issue when internal buffer state misses partial sync.
Fix segfault when libssh2_session_methods() is called prior to session_startup(). Fix segfault when libssh2_session_methods() is called prior to session_startup().

View File

@ -180,9 +180,7 @@ struct _LIBSSH2_SESSION {
char *kex_prefs; char *kex_prefs;
char *hostkey_prefs; char *hostkey_prefs;
int exchanging_keys; int state;
int newkeys;
int authenticated;
/* Agreed Key Exchange Method */ /* Agreed Key Exchange Method */
LIBSSH2_KEX_METHOD *kex; LIBSSH2_KEX_METHOD *kex;
@ -233,6 +231,11 @@ struct _LIBSSH2_SESSION {
int err_code; int err_code;
}; };
/* session.state bits */
#define LIBSSH2_STATE_EXCHANGING_KEYS 0x00000001
#define LIBSSH2_STATE_NEWKEYS 0x00000002
#define LIBSSH2_STATE_AUTHENTICATED 0x00000004
/* libssh2 extensible ssh api, ultimately I'd like to allow loading additional methods via .so/.dll */ /* libssh2 extensible ssh api, ultimately I'd like to allow loading additional methods via .so/.dll */
struct _LIBSSH2_KEX_METHOD { struct _LIBSSH2_KEX_METHOD {

View File

@ -263,7 +263,7 @@ static int libssh2_kex_method_diffie_hellman_groupGP_sha1_key_exchange(LIBSSH2_S
goto clean_exit; goto clean_exit;
} }
/* The first key exchange has been performed, switch to active crypt/comp/mac mode */ /* The first key exchange has been performed, switch to active crypt/comp/mac mode */
session->newkeys = 1; session->state |= LIBSSH2_STATE_NEWKEYS;
/* This will actually end up being just packet_type(1) for this packet type anyway */ /* This will actually end up being just packet_type(1) for this packet type anyway */
LIBSSH2_FREE(session, tmp); LIBSSH2_FREE(session, tmp);
@ -1145,7 +1145,7 @@ int libssh2_kex_exchange(LIBSSH2_SESSION *session, int reexchange) /* session->f
unsigned long data_len; unsigned long data_len;
/* Prevent loop in packet_add() */ /* Prevent loop in packet_add() */
session->exchanging_keys = 1; session->state |= LIBSSH2_STATE_EXCHANGING_KEYS;
if (reexchange) { if (reexchange) {
session->kex = NULL; session->kex = NULL;
@ -1191,7 +1191,7 @@ int libssh2_kex_exchange(LIBSSH2_SESSION *session, int reexchange) /* session->f
session->remote.kexinit = NULL; session->remote.kexinit = NULL;
} }
session->exchanging_keys = 0; session->state &= ~LIBSSH2_STATE_EXCHANGING_KEYS;
return 0; return 0;
} }

View File

@ -510,7 +510,7 @@ static int libssh2_packet_add(LIBSSH2_SESSION *session, unsigned char *data, siz
packet->prev = NULL; packet->prev = NULL;
} }
if (data[0] == SSH_MSG_KEXINIT && !session->exchanging_keys) { if (data[0] == SSH_MSG_KEXINIT && !(session->state & LIBSSH2_STATE_EXCHANGING_KEYS)) {
/* Remote wants new keys /* Remote wants new keys
* Well, it's already in the brigade, * Well, it's already in the brigade,
* let's just call back into ourselves * let's just call back into ourselves
@ -591,7 +591,7 @@ int libssh2_packet_read(LIBSSH2_SESSION *session, int should_block)
ioctlsocket(session->socket_fd, FIONBIO, &non_block); ioctlsocket(session->socket_fd, FIONBIO, &non_block);
} }
#endif #endif
if (session->newkeys) { if (session->state & LIBSSH2_STATE_NEWKEYS) {
/* Temporary Buffer /* Temporary Buffer
* The largest blocksize (currently) is 32, the largest MAC (currently) is 20 * The largest blocksize (currently) is 32, the largest MAC (currently) is 20
*/ */
@ -854,14 +854,14 @@ int libssh2_packet_require_ex(LIBSSH2_SESSION *session, unsigned char packet_typ
int libssh2_packet_write(LIBSSH2_SESSION *session, unsigned char *data, unsigned long data_len) int libssh2_packet_write(LIBSSH2_SESSION *session, unsigned char *data, unsigned long data_len)
{ {
unsigned long packet_length = data_len + 1; unsigned long packet_length = data_len + 1;
unsigned long block_size = (session->newkeys) ? session->local.crypt->blocksize : 8; unsigned long block_size = (session->state & LIBSSH2_STATE_NEWKEYS) ? session->local.crypt->blocksize : 8;
/* At this point packet_length doesn't include the packet_len field itself */ /* At this point packet_length doesn't include the packet_len field itself */
unsigned long padding_length; unsigned long padding_length;
int free_data = 0; int free_data = 0;
unsigned char buf[246]; /* 6 byte header plus max padding size(240) */ unsigned char buf[246]; /* 6 byte header plus max padding size(240) */
int i; int i;
if (session->newkeys && if ((session->state & LIBSSH2_STATE_NEWKEYS) &&
strcmp(session->local.comp->name, "none")) { strcmp(session->local.comp->name, "none")) {
if (session->local.comp->comp(session, 1, &data, &data_len, LIBSSH2_PACKET_MAXCOMP, &free_data, data, data_len, &session->local.comp_abstract)) { if (session->local.comp->comp(session, 1, &data, &data_len, LIBSSH2_PACKET_MAXCOMP, &free_data, data, data_len, &session->local.comp_abstract)) {
@ -894,7 +894,7 @@ int libssh2_packet_write(LIBSSH2_SESSION *session, unsigned char *data, unsigned
buf[5 + i] = '\0'; buf[5 + i] = '\0';
} }
if (session->newkeys) { if (session->state & LIBSSH2_STATE_NEWKEYS) {
/* Encryption is in effect */ /* Encryption is in effect */
unsigned char *encbuf, *s; unsigned char *encbuf, *s;
int ret; int ret;

View File

@ -325,7 +325,7 @@ LIBSSH2_API void libssh2_session_free(LIBSSH2_SESSION *session)
libssh2_channel_forward_cancel(session->listeners); libssh2_channel_forward_cancel(session->listeners);
} }
if (session->newkeys) { if (session->state & LIBSSH2_STATE_NEWKEYS) {
/* hostkey */ /* hostkey */
if (session->hostkey && session->hostkey->dtor) { if (session->hostkey && session->hostkey->dtor) {
session->hostkey->dtor(session, &session->server_hostkey_abstract); session->hostkey->dtor(session, &session->server_hostkey_abstract);

View File

@ -80,7 +80,7 @@ LIBSSH2_API char *libssh2_userauth_list(LIBSSH2_SESSION *session, char *username
if (libssh2_packet_ask(session, SSH_MSG_USERAUTH_SUCCESS, &data, &data_len, 1) == 0) { if (libssh2_packet_ask(session, SSH_MSG_USERAUTH_SUCCESS, &data, &data_len, 1) == 0) {
/* Wow, who'dve thought... */ /* Wow, who'dve thought... */
LIBSSH2_FREE(session, data); LIBSSH2_FREE(session, data);
session->authenticated = 1; session->state |= LIBSSH2_STATE_AUTHENTICATED;
return NULL; return NULL;
} }
@ -105,7 +105,7 @@ LIBSSH2_API char *libssh2_userauth_list(LIBSSH2_SESSION *session, char *username
*/ */
LIBSSH2_API int libssh2_userauth_authenticated(LIBSSH2_SESSION *session) LIBSSH2_API int libssh2_userauth_authenticated(LIBSSH2_SESSION *session)
{ {
return session->authenticated; return session->state & LIBSSH2_STATE_AUTHENTICATED;
} }
/* }}} */ /* }}} */
@ -151,7 +151,7 @@ LIBSSH2_API int libssh2_userauth_password_ex(LIBSSH2_SESSION *session, char *use
while (1) { while (1) {
if (libssh2_packet_ask(session, SSH_MSG_USERAUTH_SUCCESS, &data, &data_len, 1) == 0) { if (libssh2_packet_ask(session, SSH_MSG_USERAUTH_SUCCESS, &data, &data_len, 1) == 0) {
LIBSSH2_FREE(session, data); LIBSSH2_FREE(session, data);
session->authenticated = 1; session->state |= LIBSSH2_STATE_AUTHENTICATED;
return 0; return 0;
} }
@ -389,7 +389,7 @@ LIBSSH2_API int libssh2_userauth_publickey_fromfile_ex(LIBSSH2_SESSION *session,
LIBSSH2_FREE(session, packet); LIBSSH2_FREE(session, packet);
LIBSSH2_FREE(session, method); LIBSSH2_FREE(session, method);
LIBSSH2_FREE(session, pubkeydata); LIBSSH2_FREE(session, pubkeydata);
session->authenticated = 1; session->state |= LIBSSH2_STATE_AUTHENTICATED;
return 0; return 0;
} }
@ -485,7 +485,7 @@ LIBSSH2_API int libssh2_userauth_publickey_fromfile_ex(LIBSSH2_SESSION *session,
if (libssh2_packet_ask(session, SSH_MSG_USERAUTH_SUCCESS, &data, &data_len, 1) == 0) { if (libssh2_packet_ask(session, SSH_MSG_USERAUTH_SUCCESS, &data, &data_len, 1) == 0) {
/* We are us and we've proved it. */ /* We are us and we've proved it. */
LIBSSH2_FREE(session, data); LIBSSH2_FREE(session, data);
session->authenticated = 1; session->state |= LIBSSH2_STATE_AUTHENTICATED;
return 0; return 0;
} }