diff options
Diffstat (limited to 'libtac')
-rw-r--r-- | libtac/include/libtac.h | 2 | ||||
-rw-r--r-- | libtac/lib/connect.c | 36 |
2 files changed, 19 insertions, 19 deletions
diff --git a/libtac/include/libtac.h b/libtac/include/libtac.h index bcc5880..8a7381d 100644 --- a/libtac/include/libtac.h +++ b/libtac/include/libtac.h @@ -127,7 +127,7 @@ extern int tac_timeout; int tac_connect(struct addrinfo **, char **, int); int tac_connect_single(struct addrinfo *, const char *); -char *tac_ntop(const struct sockaddr *, size_t); +char *tac_ntop(const struct sockaddr *); int tac_authen_send(int, const char *, char *, char *, char *); diff --git a/libtac/lib/connect.c b/libtac/lib/connect.c index 1ec4c5f..1226797 100644 --- a/libtac/lib/connect.c +++ b/libtac/lib/connect.c @@ -75,7 +75,7 @@ int tac_connect_single(struct addrinfo *server, const char *key) { struct timeval tv; socklen_t len; struct sockaddr_storage addr; - char *ip = NULL; + char *ip; if(server == NULL) { TACSYSLOG((LOG_ERR, "%s: no TACACS+ server defined", __FUNCTION__)) @@ -83,8 +83,7 @@ int tac_connect_single(struct addrinfo *server, const char *key) { } /* format server address into a string for use in messages */ - /* FIXME this leaks memory, ip is not free()d */ - ip = tac_ntop(server->ai_addr, 0); + ip = tac_ntop(server->ai_addr); if((fd=socket(server->ai_family, server->ai_socktype, server->ai_protocol)) < 0) { TACSYSLOG((LOG_ERR,"%s: socket creation error", __FUNCTION__)) @@ -160,8 +159,6 @@ int tac_connect_single(struct addrinfo *server, const char *key) { tac_secret = key; } - free(ip); - /* if valid fd, but error experienced after open, close fd */ if ( fd >= 0 && retval < 0 ) { close(fd); @@ -175,29 +172,32 @@ int tac_connect_single(struct addrinfo *server, const char *key) { /* return value: * ptr to char* with format IP address - * must be freed by caller + * warning: returns a static buffer + * (which some ppl don't like, but it's robust and at last no more memory leaks) */ -char *tac_ntop(const struct sockaddr *sa, size_t unused) { - char portstr[7]; - char *str = (char *) xcalloc(1, INET6_ADDRSTRLEN+sizeof(portstr)); +char *tac_ntop(const struct sockaddr *sa) { + static char server_address[INET6_ADDRSTRLEN+16]; switch(sa->sa_family) { case AF_INET: inet_ntop(AF_INET, &(((struct sockaddr_in *)sa)->sin_addr), - str, INET_ADDRSTRLEN); - snprintf(portstr, sizeof(portstr), ":%hu", - htons(((struct sockaddr_in *)sa)->sin_port)); - strcat(str, portstr); + server_address, INET_ADDRSTRLEN); + + snprintf(server_address + strlen(server_address), 14, ":%hu", + htons(((struct sockaddr_in *)sa)->sin_port)); break; + case AF_INET6: inet_ntop(AF_INET6, &(((struct sockaddr_in6 *)sa)->sin6_addr), - str, INET6_ADDRSTRLEN); - snprintf(portstr, sizeof(portstr), ":%hu", + server_address, INET6_ADDRSTRLEN); + + snprintf(server_address + strlen(server_address), 14, ":%hu", htons(((struct sockaddr_in6 *)sa)->sin6_port)); - strcat(str, portstr); break; + default: - strncpy(str, "Unknown AF", INET6_ADDRSTRLEN); + strcpy(server_address, "Unknown AF"); } - return str; + return server_address; } /* tac_ntop */ + |