summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMukund Sivaraman <muks@banu.com>2009-11-09 01:13:41 +0530
committerMukund Sivaraman <muks@banu.com>2009-11-09 01:13:41 +0530
commitc978015af1014ab81d894cf5786df343a8ac14c1 (patch)
treead697de0c368d24df98b3e23bfe78fd7a658a957 /src
parent25057baf549876a507d31b5b8ea3831dcbc98ed9 (diff)
downloadtinyproxy-c978015af1014ab81d894cf5786df343a8ac14c1.tar.gz
tinyproxy-c978015af1014ab81d894cf5786df343a8ac14c1.zip
Add IPV6 support to listen_sock()
Diffstat (limited to 'src')
-rw-r--r--src/sock.c48
1 files changed, 34 insertions, 14 deletions
diff --git a/src/sock.c b/src/sock.c
index 091633f..184edde 100644
--- a/src/sock.c
+++ b/src/sock.c
@@ -166,41 +166,61 @@ int socket_blocking (int sock)
*/
int listen_sock (uint16_t port, socklen_t * addrlen)
{
+ struct addrinfo hints, *result, *rp;
+ char portstr[32];
int listenfd;
const int on = 1;
- struct sockaddr_in addr;
assert (port > 0);
assert (addrlen != NULL);
- listenfd = socket (AF_INET, SOCK_STREAM, 0);
- setsockopt (listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on));
+ memset (&hints, 0, sizeof (struct addrinfo));
+ hints.ai_family = AF_UNSPEC;
+ hints.ai_socktype = SOCK_STREAM;
- memset (&addr, 0, sizeof (addr));
- addr.sin_family = AF_INET;
- addr.sin_port = htons (port);
+ sprintf (portstr, "%6d", port);
- if (config.ipAddr) {
- addr.sin_addr.s_addr = inet_addr (config.ipAddr);
- } else {
- addr.sin_addr.s_addr = inet_addr ("0.0.0.0");
+ if (getaddrinfo (config.ipAddr, portstr, &hints, &result) != 0) {
+ log_message (LOG_ERR,
+ "Unable to getaddrinfo() because of %s",
+ strerror (errno));
+ return -1;
}
- if (bind (listenfd, (struct sockaddr *) &addr, sizeof (addr)) < 0) {
+ for (rp = result; rp != NULL; rp = rp->ai_next) {
+ listenfd = socket (rp->ai_family, rp->ai_socktype,
+ rp->ai_protocol);
+ if (listenfd == -1)
+ continue;
+
+ if (bind (listenfd, rp->ai_addr, rp->ai_addrlen) == 0)
+ break; /* success */
+ }
+
+ if (rp == NULL) {
+ /* was not able to bind to any address */
log_message (LOG_ERR,
- "Unable to bind listening socket because of %s",
- strerror (errno));
+ "Unable to bind listening socket "
+ "to any address.");
+
+ freeaddrinfo (result);
return -1;
}
+ setsockopt (listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on));
+
if (listen (listenfd, MAXLISTEN) < 0) {
log_message (LOG_ERR,
"Unable to start listening socket because of %s",
strerror (errno));
+
+ freeaddrinfo (result);
return -1;
}
- *addrlen = sizeof (addr);
+ *addrlen = rp->ai_addrlen;
+
+ freeaddrinfo (result);
return listenfd;
}