From e52a1057fd15e4738bf480a3afa58d98b353779c Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Fri, 29 May 2009 18:40:29 +0200 Subject: [PATCH] libssh2_knownhost_add() got an additional argument: 'store' so that an application can get a pointer back to the internal representation of the host it just added. Useful for example when the app wants to add a host, and then convert that exact same host to a line for storing in a known host file. 'store' can also be set to NULL to simple not care. --- docs/libssh2_knownhost_add.3 | 7 ++++- include/libssh2.h | 18 ++++++------ src/knownhost.c | 53 ++++++++++++++++++++---------------- 3 files changed, 45 insertions(+), 33 deletions(-) diff --git a/docs/libssh2_knownhost_add.3 b/docs/libssh2_knownhost_add.3 index c9855be..b977620 100644 --- a/docs/libssh2_knownhost_add.3 +++ b/docs/libssh2_knownhost_add.3 @@ -10,7 +10,8 @@ libssh2_knownhost_add - add a known host int libssh2_knownhost_add(LIBSSH2_KNOWNHOSTS *hosts, char *host, char *salt, char *key, size_t keylen, - int typemask); + int typemask, + struct libssh2_knownhost **store); .SH DESCRIPTION Adds a known host to the collection of known hosts identified by the 'hosts' handle. @@ -42,6 +43,10 @@ LIBSSH2_KNOWNHOST_KEYENC_RAW or LIBSSH2_KNOWNHOST_KEYENC_BASE64. The key is using one of these algorithms: LIBSSH2_KNOWNHOST_KEY_RSA1, LIBSSH2_KNOWNHOST_KEY_SSHRSA or LIBSSH2_KNOWNHOST_KEY_SSHDSS. + +\fIstore\fP should point to a pointer that gets filled in to point to the +known host data after the addition. NULL can be passed if you don't care about +this pointer. .SH RETURN VALUE Returns a regular libssh2 error code, where negative values are error codes and 0 indicates success. diff --git a/include/libssh2.h b/include/libssh2.h index 2d12c7b..9832568 100644 --- a/include/libssh2.h +++ b/include/libssh2.h @@ -670,6 +670,14 @@ LIBSSH2_API const char *libssh2_version(int req_version_num); +struct libssh2_knownhost { + unsigned int magic; /* magic stored by the library */ + void *node; /* handle to the internal representation of this host */ + char *name; /* this is NULL if no plain text host name exists */ + char *key; /* key in base64/printable format */ + int typemask; +}; + /* * libssh2_knownhost_init * @@ -721,7 +729,8 @@ LIBSSH2_API int libssh2_knownhost_add(LIBSSH2_KNOWNHOSTS *hosts, const char *host, const char *salt, - const char *key, size_t keylen, int typemask); + const char *key, size_t keylen, int typemask, + struct libssh2_knownhost **store); /* * libssh2_knownhost_check @@ -741,13 +750,6 @@ libssh2_knownhost_add(LIBSSH2_KNOWNHOSTS *hosts, * LIBSSH2_KNOWNHOST_CHECK_* values, see below * */ -struct libssh2_knownhost { - unsigned int magic; /* magic stored by the library */ - void *node; /* handle to the internal representation of this host */ - char *name; /* this is NULL if no plain text host name exists */ - char *key; /* key in base64/printable format */ - int typemask; -}; #define LIBSSH2_KNOWNHOST_CHECK_MATCH 0 #define LIBSSH2_KNOWNHOST_CHECK_MISMATCH 1 diff --git a/src/knownhost.c b/src/knownhost.c index d38132b..7e608b7 100644 --- a/src/knownhost.c +++ b/src/knownhost.c @@ -95,6 +95,27 @@ libssh2_knownhost_init(LIBSSH2_SESSION *session) return knh; } +#define KNOWNHOST_MAGIC 0xdeadcafe +/* + * knownhost_to_external() + * + * Copies data from the internal to the external representation struct. + * + */ +static struct libssh2_knownhost *knownhost_to_external(struct known_host *node) +{ + struct libssh2_knownhost *ext = &node->external; + + ext->magic = KNOWNHOST_MAGIC; + ext->node = node; + ext->name = ((node->typemask & LIBSSH2_KNOWNHOST_TYPE_MASK) == + LIBSSH2_KNOWNHOST_TYPE_PLAIN)? node->name:NULL; + ext->key = node->key; + ext->typemask = node->typemask; + + return ext; +} + /* * libssh2_knownhost_add * @@ -119,7 +140,7 @@ LIBSSH2_API int libssh2_knownhost_add(LIBSSH2_KNOWNHOSTS *hosts, const char *host, const char *salt, const char *key, size_t keylen, - int typemask) + int typemask, struct libssh2_knownhost **store) { struct known_host *entry = LIBSSH2_ALLOC(hosts->session, sizeof(struct known_host)); @@ -190,33 +211,15 @@ libssh2_knownhost_add(LIBSSH2_KNOWNHOSTS *hosts, /* add this new host to the big list of known hosts */ _libssh2_list_add(&hosts->head, &entry->node); + if(store) + *store = knownhost_to_external(entry); + return LIBSSH2_ERROR_NONE; error: free_host(hosts->session, entry); return rc; } -#define KNOWNHOST_MAGIC 0xdeadcafe -/* - * knownhost_to_external() - * - * Copies data from the internal to the external representation struct. - * - */ -static struct libssh2_knownhost *knownhost_to_external(struct known_host *node) -{ - struct libssh2_knownhost *ext = &node->external; - - ext->magic = KNOWNHOST_MAGIC; - ext->node = node; - ext->name = ((node->typemask & LIBSSH2_KNOWNHOST_TYPE_MASK) == - LIBSSH2_KNOWNHOST_TYPE_PLAIN)? node->name:NULL; - ext->key = node->key; - ext->typemask = node->typemask; - - return ext; -} - /* * libssh2_knownhost_check * @@ -498,7 +501,8 @@ static int hostline(LIBSSH2_KNOWNHOSTS *hosts, hostbuf[seplen]=0; rc = libssh2_knownhost_add(hosts, hostbuf, salt, key, keylen, - type | LIBSSH2_KNOWNHOST_KEYENC_BASE64); + type | LIBSSH2_KNOWNHOST_KEYENC_BASE64, + NULL); if(rc) return rc; } @@ -507,7 +511,8 @@ static int hostline(LIBSSH2_KNOWNHOSTS *hosts, hostbuf[hostlen]=0; rc = libssh2_knownhost_add(hosts, hostbuf, salt, key, keylen, - type | LIBSSH2_KNOWNHOST_KEYENC_BASE64); + type | LIBSSH2_KNOWNHOST_KEYENC_BASE64, + NULL); return rc; }