summaryrefslogtreecommitdiff
path: root/src/sock.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/sock.c')
-rw-r--r--src/sock.c320
1 files changed, 150 insertions, 170 deletions
diff --git a/src/sock.c b/src/sock.c
index 5673d46..091633f 100644
--- a/src/sock.c
+++ b/src/sock.c
@@ -38,37 +38,34 @@
* returned if the bind succeeded. Otherwise, -1 is returned
* to indicate an error.
*/
-static int
-bind_socket (int sockfd, const char *addr)
+static int bind_socket (int sockfd, const char *addr)
{
- struct addrinfo hints, *res, *ressave;
+ struct addrinfo hints, *res, *ressave;
- assert (sockfd >= 0);
- assert (addr != NULL && strlen (addr) != 0);
+ assert (sockfd >= 0);
+ assert (addr != NULL && strlen (addr) != 0);
- memset (&hints, 0, sizeof (struct addrinfo));
- hints.ai_family = AF_UNSPEC;
- hints.ai_socktype = SOCK_STREAM;
+ memset (&hints, 0, sizeof (struct addrinfo));
+ hints.ai_family = AF_UNSPEC;
+ hints.ai_socktype = SOCK_STREAM;
- /* The local port it not important */
- if (getaddrinfo (addr, NULL, &hints, &res) != 0)
- return -1;
+ /* The local port it not important */
+ if (getaddrinfo (addr, NULL, &hints, &res) != 0)
+ return -1;
- ressave = res;
+ ressave = res;
- /* Loop through the addresses and try to bind to each */
- do
- {
- if (bind (sockfd, res->ai_addr, res->ai_addrlen) == 0)
- break; /* success */
- }
- while ((res = res->ai_next) != NULL);
+ /* Loop through the addresses and try to bind to each */
+ do {
+ if (bind (sockfd, res->ai_addr, res->ai_addrlen) == 0)
+ break; /* success */
+ } while ((res = res->ai_next) != NULL);
- freeaddrinfo (ressave);
- if (res == NULL) /* was not able to bind to any address */
- return -1;
+ freeaddrinfo (ressave);
+ if (res == NULL) /* was not able to bind to any address */
+ return -1;
- return sockfd;
+ return sockfd;
}
/*
@@ -76,98 +73,89 @@ bind_socket (int sockfd, const char *addr)
* the getaddrinfo() library function, which allows for a protocol
* independent implementation (mostly for IPv4 and IPv6 addresses.)
*/
-int
-opensock (const char *host, int port, const char *bind_to)
+int opensock (const char *host, int port, const char *bind_to)
{
- int sockfd, n;
- struct addrinfo hints, *res, *ressave;
- char portstr[6];
-
- assert (host != NULL);
- assert (port > 0);
-
- memset (&hints, 0, sizeof (struct addrinfo));
- hints.ai_family = AF_UNSPEC;
- hints.ai_socktype = SOCK_STREAM;
-
- snprintf (portstr, sizeof (portstr), "%d", port);
-
- n = getaddrinfo (host, portstr, &hints, &res);
- if (n != 0)
- {
- log_message (LOG_ERR, "opensock: Could not retrieve info for %s", host);
- return -1;
- }
-
- ressave = res;
- do
- {
- sockfd = socket (res->ai_family, res->ai_socktype, res->ai_protocol);
- if (sockfd < 0)
- continue; /* ignore this one */
-
- /* Bind to the specified address */
- if (bind_to)
- {
- if (bind_socket (sockfd, bind_to) < 0)
- {
- close (sockfd);
- continue; /* can't bind, so try again */
- }
- }
- else if (config.bind_address)
- {
- if (bind_socket (sockfd, config.bind_address) < 0)
- {
- close (sockfd);
- continue; /* can't bind, so try again */
- }
- }
+ int sockfd, n;
+ struct addrinfo hints, *res, *ressave;
+ char portstr[6];
- if (connect (sockfd, res->ai_addr, res->ai_addrlen) == 0)
- break; /* success */
+ assert (host != NULL);
+ assert (port > 0);
- close (sockfd);
- }
- while ((res = res->ai_next) != NULL);
+ memset (&hints, 0, sizeof (struct addrinfo));
+ hints.ai_family = AF_UNSPEC;
+ hints.ai_socktype = SOCK_STREAM;
- freeaddrinfo (ressave);
- if (res == NULL)
- {
- log_message (LOG_ERR,
- "opensock: Could not establish a connection to %s", host);
- return -1;
- }
+ snprintf (portstr, sizeof (portstr), "%d", port);
- return sockfd;
+ n = getaddrinfo (host, portstr, &hints, &res);
+ if (n != 0) {
+ log_message (LOG_ERR,
+ "opensock: Could not retrieve info for %s", host);
+ return -1;
+ }
+
+ ressave = res;
+ do {
+ sockfd =
+ socket (res->ai_family, res->ai_socktype, res->ai_protocol);
+ if (sockfd < 0)
+ continue; /* ignore this one */
+
+ /* Bind to the specified address */
+ if (bind_to) {
+ if (bind_socket (sockfd, bind_to) < 0) {
+ close (sockfd);
+ continue; /* can't bind, so try again */
+ }
+ } else if (config.bind_address) {
+ if (bind_socket (sockfd, config.bind_address) < 0) {
+ close (sockfd);
+ continue; /* can't bind, so try again */
+ }
+ }
+
+ if (connect (sockfd, res->ai_addr, res->ai_addrlen) == 0)
+ break; /* success */
+
+ close (sockfd);
+ } while ((res = res->ai_next) != NULL);
+
+ freeaddrinfo (ressave);
+ if (res == NULL) {
+ log_message (LOG_ERR,
+ "opensock: Could not establish a connection to %s",
+ host);
+ return -1;
+ }
+
+ return sockfd;
}
/*
* Set the socket to non blocking -rjkaes
*/
-int
-socket_nonblocking (int sock)
+int socket_nonblocking (int sock)
{
- int flags;
+ int flags;
- assert (sock >= 0);
+ assert (sock >= 0);
- flags = fcntl (sock, F_GETFL, 0);
- return fcntl (sock, F_SETFL, flags | O_NONBLOCK);
+ flags = fcntl (sock, F_GETFL, 0);
+ return fcntl (sock, F_SETFL, flags | O_NONBLOCK);
}
/*
* Set the socket to blocking -rjkaes
*/
-int
-socket_blocking (int sock)
+int socket_blocking (int sock)
{
- int flags;
+ int flags;
- assert (sock >= 0);
+ assert (sock >= 0);
- flags = fcntl (sock, F_GETFL, 0);
- return fcntl (sock, F_SETFL, flags & ~O_NONBLOCK);
+ flags = fcntl (sock, F_GETFL, 0);
+ return fcntl (sock, F_SETFL, flags & ~O_NONBLOCK);
}
/*
@@ -176,102 +164,94 @@ socket_blocking (int sock)
* the pointer, while the socket is returned as a default return.
* - rjkaes
*/
-int
-listen_sock (uint16_t port, socklen_t * addrlen)
+int listen_sock (uint16_t port, socklen_t * addrlen)
{
- 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 (&addr, 0, sizeof (addr));
- addr.sin_family = AF_INET;
- addr.sin_port = htons (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 (bind (listenfd, (struct sockaddr *) &addr, sizeof (addr)) < 0)
- {
- log_message (LOG_ERR,
- "Unable to bind listening socket because of %s",
- strerror (errno));
- return -1;
- }
-
- if (listen (listenfd, MAXLISTEN) < 0)
- {
- log_message (LOG_ERR,
- "Unable to start listening socket because of %s",
- strerror (errno));
- return -1;
- }
-
- *addrlen = sizeof (addr);
-
- return listenfd;
+ 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 (&addr, 0, sizeof (addr));
+ addr.sin_family = AF_INET;
+ addr.sin_port = htons (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 (bind (listenfd, (struct sockaddr *) &addr, sizeof (addr)) < 0) {
+ log_message (LOG_ERR,
+ "Unable to bind listening socket because of %s",
+ strerror (errno));
+ return -1;
+ }
+
+ if (listen (listenfd, MAXLISTEN) < 0) {
+ log_message (LOG_ERR,
+ "Unable to start listening socket because of %s",
+ strerror (errno));
+ return -1;
+ }
+
+ *addrlen = sizeof (addr);
+
+ return listenfd;
}
/*
* Takes a socket descriptor and returns the socket's IP address.
*/
-int
-getsock_ip (int fd, char *ipaddr)
+int getsock_ip (int fd, char *ipaddr)
{
- struct sockaddr_storage name;
- socklen_t namelen = sizeof (name);
+ struct sockaddr_storage name;
+ socklen_t namelen = sizeof (name);
- assert (fd >= 0);
+ assert (fd >= 0);
- if (getsockname (fd, (struct sockaddr *) &name, &namelen) != 0)
- {
- log_message (LOG_ERR, "getsock_ip: getsockname() error: %s",
- strerror (errno));
- return -1;
- }
+ if (getsockname (fd, (struct sockaddr *) &name, &namelen) != 0) {
+ log_message (LOG_ERR, "getsock_ip: getsockname() error: %s",
+ strerror (errno));
+ return -1;
+ }
- if (get_ip_string ((struct sockaddr *) &name, ipaddr, IP_LENGTH) == NULL)
- return -1;
+ if (get_ip_string ((struct sockaddr *) &name, ipaddr, IP_LENGTH) ==
+ NULL)
+ return -1;
- return 0;
+ return 0;
}
/*
* Return the peer's socket information.
*/
-int
-getpeer_information (int fd, char *ipaddr, char *string_addr)
+int getpeer_information (int fd, char *ipaddr, char *string_addr)
{
- struct sockaddr_storage sa;
- socklen_t salen = sizeof sa;
+ struct sockaddr_storage sa;
+ socklen_t salen = sizeof sa;
- assert (fd >= 0);
- assert (ipaddr != NULL);
- assert (string_addr != NULL);
+ assert (fd >= 0);
+ assert (ipaddr != NULL);
+ assert (string_addr != NULL);
- /* Set the strings to default values */
- ipaddr[0] = '\0';
- strlcpy (string_addr, "[unknown]", HOSTNAME_LENGTH);
+ /* Set the strings to default values */
+ ipaddr[0] = '\0';
+ strlcpy (string_addr, "[unknown]", HOSTNAME_LENGTH);
- /* Look up the IP address */
- if (getpeername (fd, (struct sockaddr *) &sa, &salen) != 0)
- return -1;
+ /* Look up the IP address */
+ if (getpeername (fd, (struct sockaddr *) &sa, &salen) != 0)
+ return -1;
- if (get_ip_string ((struct sockaddr *) &sa, ipaddr, IP_LENGTH) == NULL)
- return -1;
+ if (get_ip_string ((struct sockaddr *) &sa, ipaddr, IP_LENGTH) == NULL)
+ return -1;
- /* Get the full host name */
- return getnameinfo ((struct sockaddr *) &sa, salen,
- string_addr, HOSTNAME_LENGTH, NULL, 0, 0);
+ /* Get the full host name */
+ return getnameinfo ((struct sockaddr *) &sa, salen,
+ string_addr, HOSTNAME_LENGTH, NULL, 0, 0);
}