Skip to content

Commit

Permalink
TPP's use of MT-unsafe function netaddr() causes intermittent failure…
Browse files Browse the repository at this point in the history
…s in name resolution
  • Loading branch information
subhasisb committed Nov 23, 2018
1 parent f0b8f36 commit e3023ed
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/lib/Libtpp/tpp_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ extern "C" {
#include <limits.h>
#include <time.h>
#include <pthread.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>

#include "avltree.h"
Expand Down Expand Up @@ -498,6 +500,7 @@ int tpp_sock_resolve_ip(tpp_addr_t *addr, char *host, int len);
tpp_addr_t *tpp_sock_resolve_host(char *host, int *count);

char *tpp_netaddr(tpp_addr_t *);
char *tpp_netaddr_sa(struct sockaddr *);

extern void (*tpp_log_func)(int level, const char *id, char *mess); /* log function */

Expand Down
6 changes: 3 additions & 3 deletions src/lib/Libtpp/tpp_transport.c
Original file line number Diff line number Diff line change
Expand Up @@ -1563,7 +1563,7 @@ work(void *v)
em_event_t *events;
phy_conn_t *conn;
int slot_state;
struct sockaddr_in clientaddr;
struct sockaddr clientaddr;
int new_connection = 0;
int timeout, timeout2;
time_t now;
Expand Down Expand Up @@ -1765,8 +1765,8 @@ work(void *v)
return NULL;
}
conn->conn_params->auth_type = auth_type;
conn->conn_params->hostname = strdup(netaddr((struct sockaddr_in *) &clientaddr));
conn->conn_params->port = ntohs(clientaddr.sin_port);
conn->conn_params->hostname = strdup(tpp_netaddr_sa(&clientaddr));
conn->conn_params->port = ntohs(((struct sockaddr_in *) &clientaddr)->sin_port);

/**
* accept socket, and add socket to stream, assign stream to
Expand Down
36 changes: 36 additions & 0 deletions src/lib/Libtpp/tpp_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -1647,6 +1647,42 @@ tpp_netaddr(tpp_addr_t *ap)
return ptr->tppstaticbuf;
}

/**
* @brief return a human readable string representation of an address
* for either an ipv4 or ipv6 address
*
* @param[in] sa - address in sockaddr format
*
* @return - string representation of address
* (uses TLS area to make it easy and yet thread safe)
*
* @par MT-safe: Yes
**/
char *
tpp_netaddr_sa(struct sockaddr *sa)
{
tpp_tls_t *ptr;
int len = TPP_LOGBUF_SZ;

ptr = tpp_get_tls();
if (!ptr) {
fprintf(stderr, "Out of memory\n");
exit(1);
}
ptr->tppstaticbuf[0] = '\0';

#ifdef WIN32
WSAAddressToString((LPSOCKADDR)&sa, sizeof(struct sockaddr), NULL, (LPSTR)&ptr->tppstaticbuf, &len);
#else
if (sa->sa_family == AF_INET)
inet_ntop(sa->sa_family, &(((struct sockaddr_in *) sa)->sin_addr), ptr->tppstaticbuf, len);
else
inet_ntop(sa->sa_family, &(((struct sockaddr_in6 *) sa)->sin6_addr), ptr->tppstaticbuf, len);
#endif

return ptr->tppstaticbuf;
}

/*
* Convenience function to delete information about a router
*/
Expand Down

0 comments on commit e3023ed

Please sign in to comment.