aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAris Adamantiadis <aris@0xbadc0de.be>2009-11-16 23:20:16 +0100
committerAris Adamantiadis <aris@0xbadc0de.be>2009-11-16 23:20:16 +0100
commitae11589205291f78c4cf5e9417f69853440d5307 (patch)
tree6c06f35afa99b2c3005b97213711a8abd7982bd1
parent70b947544958eb982984b4d37d584037d1801c6f (diff)
downloadlibssh-ae11589205291f78c4cf5e9417f69853440d5307.tar.gz
libssh-ae11589205291f78c4cf5e9417f69853440d5307.tar.xz
libssh-ae11589205291f78c4cf5e9417f69853440d5307.zip
Pcap: more cleanup and minimalist API
-rw-r--r--examples/sample.c6
-rw-r--r--include/libssh/libssh.h7
-rw-r--r--include/libssh/pcap.h9
-rw-r--r--libssh/pcap.c23
-rw-r--r--libssh/session.c6
5 files changed, 36 insertions, 15 deletions
diff --git a/examples/sample.c b/examples/sample.c
index 639605e1..27fd7c7c 100644
--- a/examples/sample.c
+++ b/examples/sample.c
@@ -464,7 +464,6 @@ static int client(ssh_session session){
ssh_pcap_file pcap;
void set_pcap(ssh_session session);
void set_pcap(ssh_session session){
- ssh_pcap_context ctx;
if(!pcap_file)
return;
pcap=ssh_pcap_file_new();
@@ -474,10 +473,9 @@ void set_pcap(ssh_session session){
pcap=NULL;
return;
}
- ctx=ssh_pcap_context_new(session);
- ssh_pcap_context_set_file(ctx,pcap);
- ssh_set_pcap_context(session,ctx);
+ ssh_set_pcap_file(session,pcap);
}
+
void cleanup_pcap(void);
void cleanup_pcap(){
ssh_pcap_file_free(pcap);
diff --git a/include/libssh/libssh.h b/include/libssh/libssh.h
index 53423ad3..a17e90a2 100644
--- a/include/libssh/libssh.h
+++ b/include/libssh/libssh.h
@@ -109,6 +109,7 @@ typedef struct ssh_agent_struct* ssh_agent;
typedef struct ssh_buffer_struct* ssh_buffer;
typedef struct ssh_channel_struct* ssh_channel;
typedef struct ssh_message_struct* ssh_message;
+typedef struct ssh_pcap_file_struct* ssh_pcap_file;
typedef struct ssh_private_key_struct* ssh_private_key;
typedef struct ssh_public_key_struct* ssh_public_key;
typedef struct ssh_scp_struct* ssh_scp;
@@ -377,7 +378,10 @@ LIBSSH_API int ssh_options_getopt(ssh_session session, int *argcptr, char **argv
LIBSSH_API int ssh_options_parse_config(ssh_session session, const char *filename);
LIBSSH_API int ssh_options_set(ssh_session session, enum ssh_options_e type,
const void *value);
-
+LIBSSH_API int ssh_pcap_file_close(ssh_pcap_file pcap);
+LIBSSH_API void ssh_pcap_file_free(ssh_pcap_file pcap);
+LIBSSH_API ssh_pcap_file ssh_pcap_file_new(void);
+LIBSSH_API int ssh_pcap_file_open(ssh_pcap_file pcap, const char *filename);
LIBSSH_API void ssh_print_hexa(const char *descr, const unsigned char *what, size_t len);
LIBSSH_API int ssh_scp_accept_request(ssh_scp scp);
LIBSSH_API int ssh_scp_close(ssh_scp scp);
@@ -403,6 +407,7 @@ LIBSSH_API void ssh_set_fd_except(ssh_session session);
LIBSSH_API void ssh_set_fd_toread(ssh_session session);
LIBSSH_API void ssh_set_fd_towrite(ssh_session session);
LIBSSH_API void ssh_silent_disconnect(ssh_session session);
+LIBSSH_API int ssh_set_pcap_file(ssh_session session, ssh_pcap_file pcapfile);
#ifndef _WIN32
LIBSSH_API int ssh_userauth_agent_pubkey(ssh_session session, const char *username,
ssh_public_key publickey);
diff --git a/include/libssh/pcap.h b/include/libssh/pcap.h
index 558ce563..831a5647 100644
--- a/include/libssh/pcap.h
+++ b/include/libssh/pcap.h
@@ -6,17 +6,11 @@
#ifdef WITH_PCAP
typedef struct ssh_pcap_context_struct* ssh_pcap_context;
-typedef struct ssh_pcap_file_struct* ssh_pcap_file;
-ssh_pcap_file ssh_pcap_file_new(void);
-int ssh_pcap_file_open(ssh_pcap_file pcap, const char *filename);
-int ssh_pcap_file_close(ssh_pcap_file pcap);
-void ssh_pcap_file_free(ssh_pcap_file pcap);
-
-/* to be removed from here after tests */
int ssh_pcap_file_write_packet(ssh_pcap_file pcap, ssh_buffer packet, u_int32_t original_len);
ssh_pcap_context ssh_pcap_context_new(ssh_session session);
+void ssh_pcap_context_free(ssh_pcap_context ctx);
enum ssh_pcap_direction{
SSH_PCAP_DIR_IN,
@@ -26,7 +20,6 @@ void ssh_pcap_context_set_file(ssh_pcap_context, ssh_pcap_file);
int ssh_pcap_context_write(ssh_pcap_context,enum ssh_pcap_direction direction, void *data,
u_int32_t len, u_int32_t origlen);
-void ssh_set_pcap_context(ssh_session session, ssh_pcap_context pcap);
#endif /* WITH_PCAP */
#endif /* PCAP_H_ */
diff --git a/libssh/pcap.c b/libssh/pcap.c
index 2ff2e4d5..2da0087e 100644
--- a/libssh/pcap.c
+++ b/libssh/pcap.c
@@ -220,6 +220,10 @@ ssh_pcap_context ssh_pcap_context_new(ssh_session session){
return ctx;
}
+void ssh_pcap_context_free(ssh_pcap_context ctx){
+ SAFE_FREE(ctx);
+}
+
void ssh_pcap_context_set_file(ssh_pcap_context ctx, ssh_pcap_file pcap){
ctx->file=pcap;
}
@@ -355,8 +359,23 @@ int ssh_pcap_context_write(ssh_pcap_context ctx,enum ssh_pcap_direction directio
return err;
}
-void ssh_set_pcap_context(ssh_session session, ssh_pcap_context pcap){
- session->pcap_ctx=pcap;
+/** @brief sets the pcap file used to trace the session
+ * @param current session
+ * @param pcap an handler to a pcap file. A pcap file may be used in several
+ * sessions.
+ * @returns SSH_ERROR in case of error, SSH_OK otherwise.
+ */
+int ssh_set_pcap_file(ssh_session session, ssh_pcap_file pcap){
+ ssh_pcap_context ctx=ssh_pcap_context_new(session);
+ if(ctx==NULL){
+ ssh_set_error_oom(session);
+ return SSH_ERROR;
+ }
+ ctx->file=pcap;
+ if(session->pcap_ctx)
+ ssh_pcap_context_free(session->pcap_ctx);
+ session->pcap_ctx=ctx;
+ return SSH_OK;
}
diff --git a/libssh/session.c b/libssh/session.c
index a3e2ed85..b2c0368e 100644
--- a/libssh/session.c
+++ b/libssh/session.c
@@ -118,6 +118,12 @@ void ssh_free(ssh_session session) {
SAFE_FREE(session->serverbanner);
SAFE_FREE(session->clientbanner);
SAFE_FREE(session->banner);
+#ifdef WITH_PCAP
+ if(session->pcap_ctx){
+ ssh_pcap_context_free(session->pcap_ctx);
+ session->pcap_ctx=NULL;
+ }
+#endif
buffer_free(session->in_buffer);
buffer_free(session->out_buffer);
session->in_buffer=session->out_buffer=NULL;