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.
AC_PROG_CC
AC_PROG_CC_C99
# Checks for libraries.
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];
int opt_on = 1;
struct addrinfo hints = {
.ai_family = AF_UNSPEC,
.ai_socktype = SOCK_STREAM,
.ai_protocol = IPPROTO_TCP
};
/* Client */
struct sockaddr_in client_addr;
socklen_t client_addr_len;
@ -224,7 +230,7 @@ int main(int argc, char* argv[]) {
}
/* 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",
gai_strerror(retval));
exit(EXIT_FAILURE);
@ -246,11 +252,25 @@ int main(int argc, char* argv[]) {
current_address = addresses;
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 */
if (bind(socket_fd,
current_address->ai_addr,
current_address->ai_addrlen) == 0) {
fprintf(stderr, "Successfully bound socket to "
"host %s, port %s\n", bound_address, bound_port);
/* Done if successful bind */
break;
@ -258,7 +278,9 @@ int main(int argc, char* argv[]) {
/* Otherwise log error */
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;
@ -315,13 +337,7 @@ 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.");
}
/* Log address and port */
if (getnameinfo(current_address->ai_addr, current_address->ai_addrlen,
bound_address, sizeof(bound_address),
bound_port, sizeof(bound_port),
0))
syslog(LOG_WARNING, "Could not resolve name of listening host.");
else
/* Log listening status */
syslog(LOG_INFO,
"Listening on host %s, port %s", bound_address, bound_port);