Restrict addresses returned for bind, improve logging.

This commit is contained in:
Michael Jumper 2012-03-01 14:05:12 -08:00
parent fcf37ecd11
commit aecd6347e3
2 changed files with 28 additions and 11 deletions

View File

@ -39,6 +39,7 @@ AM_INIT_AUTOMAKE(guacd, 0.5.0)
# Checks for programs. # Checks for programs.
AC_PROG_CC AC_PROG_CC
AC_PROG_CC_C99
# Checks for libraries. # Checks for libraries.
AC_CHECK_LIB([guac], [guac_client_plugin_open],, AC_MSG_ERROR("libguac must be installed first")) AC_CHECK_LIB([guac], [guac_client_plugin_open],, AC_MSG_ERROR("libguac must be installed first"))

View File

@ -184,6 +184,12 @@ int main(int argc, char* argv[]) {
char bound_port[64]; char bound_port[64];
int opt_on = 1; int opt_on = 1;
struct addrinfo hints = {
.ai_family = AF_UNSPEC,
.ai_socktype = SOCK_STREAM,
.ai_protocol = IPPROTO_TCP
};
/* Client */ /* Client */
struct sockaddr_in client_addr; struct sockaddr_in client_addr;
socklen_t client_addr_len; socklen_t client_addr_len;
@ -224,7 +230,7 @@ int main(int argc, char* argv[]) {
} }
/* Get addresses for binding */ /* Get addresses for binding */
if ((retval = getaddrinfo(listen_address, listen_port, NULL, &addresses))) { if ((retval = getaddrinfo(listen_address, listen_port, &hints, &addresses))) {
fprintf(stderr, "Error parsing given address or port: %s\n", fprintf(stderr, "Error parsing given address or port: %s\n",
gai_strerror(retval)); gai_strerror(retval));
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
@ -246,11 +252,25 @@ int main(int argc, char* argv[]) {
current_address = addresses; current_address = addresses;
while (current_address != NULL) { while (current_address != NULL) {
int retval;
/* Resolve hostname */
if ((retval = getnameinfo(current_address->ai_addr,
current_address->ai_addrlen,
bound_address, sizeof(bound_address),
bound_port, sizeof(bound_port),
NI_NUMERICHOST | NI_NUMERICSERV)))
fprintf(stderr, "Unable to resolve host: %s\n",
gai_strerror(retval));
/* Attempt to bind socket to address */ /* Attempt to bind socket to address */
if (bind(socket_fd, if (bind(socket_fd,
current_address->ai_addr, current_address->ai_addr,
current_address->ai_addrlen) == 0) { current_address->ai_addrlen) == 0) {
fprintf(stderr, "Successfully bound socket to "
"host %s, port %s\n", bound_address, bound_port);
/* Done if successful bind */ /* Done if successful bind */
break; break;
@ -258,7 +278,9 @@ int main(int argc, char* argv[]) {
/* Otherwise log error */ /* Otherwise log error */
else else
fprintf(stderr, "Error binding socket: %s\n", strerror(errno)); fprintf(stderr, "Error binding socket to "
"host %s, port %s: %s\n",
bound_address, bound_port, strerror(errno));
current_address = current_address->ai_next; current_address = current_address->ai_next;
@ -315,15 +337,9 @@ int main(int argc, char* argv[]) {
syslog(LOG_ERR, "Could not set handler for SIGCHLD to ignore. Child processes may pile up in the process table."); syslog(LOG_ERR, "Could not set handler for SIGCHLD to ignore. Child processes may pile up in the process table.");
} }
/* Log address and port */ /* Log listening status */
if (getnameinfo(current_address->ai_addr, current_address->ai_addrlen, syslog(LOG_INFO,
bound_address, sizeof(bound_address), "Listening on host %s, port %s", bound_address, bound_port);
bound_port, sizeof(bound_port),
0))
syslog(LOG_WARNING, "Could not resolve name of listening host.");
else
syslog(LOG_INFO,
"Listening on host %s, port %s", bound_address, bound_port);
/* Free addresses */ /* Free addresses */
freeaddrinfo(addresses); freeaddrinfo(addresses);