From 63524f157b0bf469ee90eacdd6a0cdaed19711a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20=C3=96man?= Date: Wed, 23 Jul 2008 13:13:33 +0000 Subject: [PATCH] Fix a couple of bugs related to enable/disable of TCP session handlers. While at it, add a function for changin destination hostname. --- tcp.c | 24 +++++++++++++++++++----- tcp.h | 2 ++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/tcp.c b/tcp.c index ada8a5b9..d82fd0f4 100644 --- a/tcp.c +++ b/tcp.c @@ -257,7 +257,7 @@ tcp_disconnect(tcp_session_t *ses, int err) if(ses->tcp_server != NULL) { free(ses->tcp_name); free(ses); - } else { + } else if(ses->tcp_enabled) { /* Try to reconnect in 2 seconds */ dtimer_arm(&ses->tcp_timer, tcp_client_reconnect_timeout, ses, 2); } @@ -372,6 +372,7 @@ tcp_client_connect_callback(int events, void *opaque, int fd) tcp_session_t *c = opaque; dispatch_delfd(c->tcp_dispatch_handle); + c->tcp_dispatch_handle = NULL; getsockopt(fd, SOL_SOCKET, SO_ERROR, (void *)&err, &errlen); @@ -381,7 +382,7 @@ tcp_client_connect_callback(int events, void *opaque, int fd) } close(c->tcp_fd); - tcp_client_connect_fail(c, errno); + tcp_client_connect_fail(c, err); } /** @@ -479,10 +480,10 @@ tcp_create_client(const char *hostname, int port, size_t session_size, c->tcp_callback = cb; c->tcp_name = strdup(name); c->tcp_port = port; - c->tcp_hostname = strdup(hostname); + c->tcp_hostname = hostname ? strdup(hostname) : NULL; c->tcp_enabled = enabled; - if(c->tcp_enabled) + if(c->tcp_enabled && c->tcp_hostname) tcp_session_try_connect(c); return c; } @@ -597,7 +598,7 @@ tcp_enable_disable(tcp_session_t *ses, int enabled) ses->tcp_enabled = enabled; - if(enabled) { + if(enabled && ses->tcp_hostname != NULL) { tcp_session_try_connect(ses); } else { if(ses->tcp_resolver != NULL) { @@ -611,3 +612,16 @@ tcp_enable_disable(tcp_session_t *ses, int enabled) dtimer_disarm(&ses->tcp_timer); } } + + +/** + * + */ +void +tcp_set_hostname(tcp_session_t *ses, const char *hostname) +{ + if(ses->tcp_hostname != NULL) + free(ses->tcp_hostname); + + ses->tcp_hostname = strdup(hostname); +} diff --git a/tcp.h b/tcp.h index 25cebfff..c47008f5 100644 --- a/tcp.h +++ b/tcp.h @@ -94,6 +94,8 @@ void tcp_destroy_client(tcp_session_t *ses); void tcp_enable_disable(tcp_session_t *ses, int enabled); +void tcp_set_hostname(tcp_session_t *ses, const char *hostname); + int tcp_send_msg(tcp_session_t *ses, int hiprio, void *data, size_t len); #endif /* TCP_H_ */