Merge Irssi 0.8.16-rc1
[silc.git] / apps / irssi / src / core / network.c
index 4fc06c05f2fd06a15eb1fec73b1ad80ca162cc8f..d7c1017c7626c2d9a9ed9c4f330916fa9b6b7c91 100644 (file)
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU General Public License for more details.
 
-    You should have received a copy of the GNU General Public License
-    along with this program; if not, write to the Free Software
-    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+    You should have received a copy of the GNU General Public License along
+    with this program; if not, write to the Free Software Foundation, Inc.,
+    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
 
 #include "module.h"
 #include "network.h"
 
+#include <sys/un.h>
+
 #ifndef INADDR_NONE
 #  define INADDR_NONE INADDR_BROADCAST
 #endif
@@ -40,15 +42,27 @@ union sockaddr_union {
 #  define SIZEOF_SOCKADDR(so) (sizeof(so.sin))
 #endif
 
+GIOChannel *g_io_channel_new(int handle)
+{
+       GIOChannel *chan;
 #ifdef WIN32
-#  define g_io_channel_new(handle) g_io_channel_win32_new_stream_socket(handle)
+       chan = g_io_channel_win32_new_socket(handle);
 #else
-#  define g_io_channel_new(handle) g_io_channel_unix_new(handle)
+       chan = g_io_channel_unix_new(handle);
 #endif
+       g_io_channel_set_encoding(chan, NULL, NULL);
+       g_io_channel_set_buffered(chan, FALSE);
+       return chan;
+}
 
 /* Cygwin need this, don't know others.. */
 /*#define BLOCKING_SOCKETS 1*/
 
+IPADDR ip4_any = {
+       AF_INET,
+       { INADDR_ANY }
+};
+
 int net_ip_compare(IPADDR *ip1, IPADDR *ip2)
 {
        if (ip1->family != ip2->family)
@@ -63,13 +77,7 @@ int net_ip_compare(IPADDR *ip1, IPADDR *ip2)
 }
 
 
-/* copy IP to sockaddr */
-#ifdef G_CAN_INLINE
-G_INLINE_FUNC
-#else
-static
-#endif
-void sin_set_ip(union sockaddr_union *so, const IPADDR *ip)
+static void sin_set_ip(union sockaddr_union *so, const IPADDR *ip)
 {
        if (ip == NULL) {
 #ifdef HAVE_IPV6
@@ -103,27 +111,17 @@ void sin_get_ip(const union sockaddr_union *so, IPADDR *ip)
                memcpy(&ip->ip, &so->sin.sin_addr, 4);
 }
 
-#ifdef G_CAN_INLINE
-G_INLINE_FUNC
-#else
-static
-#endif
-void sin_set_port(union sockaddr_union *so, int port)
+static void sin_set_port(union sockaddr_union *so, int port)
 {
 #ifdef HAVE_IPV6
        if (so->sin.sin_family == AF_INET6)
-                so->sin6.sin6_port = htons(port);
+                so->sin6.sin6_port = htons((unsigned short)port);
        else
 #endif
                so->sin.sin_port = htons((unsigned short)port);
 }
 
-#ifdef G_CAN_INLINE
-G_INLINE_FUNC
-#else
-static
-#endif
-int sin_get_port(union sockaddr_union *so)
+static int sin_get_port(union sockaddr_union *so)
 {
 #ifdef HAVE_IPV6
        if (so->sin.sin_family == AF_INET6)
@@ -136,11 +134,9 @@ int sin_get_port(union sockaddr_union *so)
 GIOChannel *net_connect(const char *addr, int port, IPADDR *my_ip)
 {
        IPADDR ip4, ip6, *ip;
-        int family;
 
        g_return_val_if_fail(addr != NULL, NULL);
 
-        family = my_ip == NULL ? 0 : my_ip->family;
        if (net_gethostbyname(addr, &ip4, &ip6) == -1)
                return NULL;
 
@@ -191,15 +187,19 @@ GIOChannel *net_connect_ip(IPADDR *ip, int port, IPADDR *my_ip)
 #ifndef WIN32
        fcntl(handle, F_SETFL, O_NONBLOCK);
 #endif
-       setsockopt(handle, SOL_SOCKET, SO_REUSEADDR,
-                  (char *) &opt, sizeof(opt));
-       setsockopt(handle, SOL_SOCKET, SO_KEEPALIVE,
-                  (char *) &opt, sizeof(opt));
+       setsockopt(handle, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
+       setsockopt(handle, SOL_SOCKET, SO_KEEPALIVE, &opt, sizeof(opt));
 
-       /* set our own address, ignore if bind() fails */
+       /* set our own address */
        if (my_ip != NULL) {
                sin_set_ip(&so, my_ip);
-               bind(handle, &so.sa, SIZEOF_SOCKADDR(so));
+               if (bind(handle, &so.sa, SIZEOF_SOCKADDR(so)) < 0) {
+                       int old_errno = errno;
+
+                       close(handle);
+                       errno = old_errno;
+                       return NULL;
+               }
        }
 
        /* connect */
@@ -208,11 +208,47 @@ GIOChannel *net_connect_ip(IPADDR *ip, int port, IPADDR *my_ip)
        ret = connect(handle, &so.sa, SIZEOF_SOCKADDR(so));
 
 #ifndef WIN32
-       if (ret < 0 && errno != EINPROGRESS) {
+       if (ret < 0 && errno != EINPROGRESS)
 #else
-       if (ret < 0 && WSAGetLastError() != WSAEWOULDBLOCK) {
+       if (ret < 0 && WSAGetLastError() != WSAEWOULDBLOCK)
 #endif
+       {
+               int old_errno = errno;
                close(handle);
+               errno = old_errno;
+               return NULL;
+       }
+
+       return g_io_channel_new(handle);
+}
+
+/* Connect to named UNIX socket */
+GIOChannel *net_connect_unix(const char *path)
+{
+       struct sockaddr_un sa;
+       int handle, ret;
+
+       /* create the socket */
+       handle = socket(PF_UNIX, SOCK_STREAM, 0);
+       if (handle == -1)
+               return NULL;
+
+       /* set socket options */
+#ifndef WIN32
+       fcntl(handle, F_SETFL, O_NONBLOCK);
+#endif
+
+       /* connect */
+       memset(&sa, 0, sizeof(sa));
+       sa.sun_family = AF_UNIX;
+       strncpy(sa.sun_path, path, sizeof(sa.sun_path)-1);
+       sa.sun_path[sizeof(sa.sun_path)-1] = '\0';
+
+       ret = connect(handle, (struct sockaddr *) &sa, sizeof(sa));
+       if (ret < 0 && errno != EINPROGRESS) {
+               int old_errno = errno;
+               close(handle);
+               errno = old_errno;
                return NULL;
        }
 
@@ -239,13 +275,13 @@ GIOChannel *net_listen(IPADDR *my_ip, int *port)
        g_return_val_if_fail(port != NULL, NULL);
 
        memset(&so, 0, sizeof(so));
-       sin_set_port(&so, *port);
        sin_set_ip(&so, my_ip);
+       sin_set_port(&so, *port);
 
        /* create the socket */
        handle = socket(so.sin.sin_family, SOCK_STREAM, 0);
 #ifdef HAVE_IPV6
-       if (handle == -1 && errno == EINVAL) {
+       if (handle == -1 && (errno == EINVAL || errno == EAFNOSUPPORT)) {
                /* IPv6 is not supported by OS */
                so.sin.sin_family = AF_INET;
                so.sin.sin_addr.s_addr = INADDR_ANY;
@@ -260,10 +296,8 @@ GIOChannel *net_listen(IPADDR *my_ip, int *port)
 #ifndef WIN32
        fcntl(handle, F_SETFL, O_NONBLOCK);
 #endif
-       setsockopt(handle, SOL_SOCKET, SO_REUSEADDR,
-                  (char *) &opt, sizeof(opt));
-       setsockopt(handle, SOL_SOCKET, SO_KEEPALIVE,
-                  (char *) &opt, sizeof(opt));
+       setsockopt(handle, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
+       setsockopt(handle, SOL_SOCKET, SO_KEEPALIVE, &opt, sizeof(opt));
 
        /* specify the address/port we want to listen in */
        ret = bind(handle, &so.sa, SIZEOF_SOCKADDR(so));
@@ -313,37 +347,43 @@ GIOChannel *net_accept(GIOChannel *handle, IPADDR *addr, int *port)
 /* Read data from socket, return number of bytes read, -1 = error */
 int net_receive(GIOChannel *handle, char *buf, int len)
 {
-        unsigned int ret;
-       int err;
+        gsize ret;
+       GIOStatus status;
+       GError *err = NULL;
 
        g_return_val_if_fail(handle != NULL, -1);
        g_return_val_if_fail(buf != NULL, -1);
 
-       err = g_io_channel_read(handle, buf, len, &ret);
-       if (err == 0 && ret == 0)
+       status = g_io_channel_read_chars(handle, buf, len, &ret, &err);
+       if (err != NULL) {
+               g_warning(err->message);
+               g_error_free(err);
+       }
+       if (status == G_IO_STATUS_ERROR || status == G_IO_STATUS_EOF)
                return -1; /* disconnected */
 
-       if (err == G_IO_ERROR_AGAIN || (err != 0 && errno == EINTR))
-               return 0; /* no bytes received */
-
-       return err == 0 ? (int)ret : -1;
+       return ret;
 }
 
 /* Transmit data, return number of bytes sent, -1 = error */
 int net_transmit(GIOChannel *handle, const char *data, int len)
 {
-        unsigned int ret;
-       int err;
+        gsize ret;
+       GIOStatus status;
+       GError *err = NULL;
 
        g_return_val_if_fail(handle != NULL, -1);
        g_return_val_if_fail(data != NULL, -1);
 
-       err = g_io_channel_write(handle, (char *) data, len, &ret);
-       if (err == G_IO_ERROR_AGAIN ||
-           (err != 0 && (errno == EINTR || errno == EPIPE)))
-               return 0;
+       status = g_io_channel_write_chars(handle, (char *) data, len, &ret, &err);
+       if (err != NULL) {
+               g_warning(err->message);
+               g_error_free(err);
+       }
+       if (status == G_IO_STATUS_ERROR)
+               return -1;
 
-       return err == 0 ? (int)ret : -1;
+       return ret;
 }
 
 /* Get socket address/port */
@@ -373,11 +413,11 @@ int net_gethostbyname(const char *addr, IPADDR *ip4, IPADDR *ip6)
 {
 #ifdef HAVE_IPV6
        union sockaddr_union *so;
-       struct addrinfo hints, *ai, *origai;
-       char hbuf[NI_MAXHOST];
-       int host_error, count;
+       struct addrinfo hints, *ai, *ailist;
+       int ret, count_v4, count_v6, use_v4, use_v6;
 #else
        struct hostent *hp;
+       int count;
 #endif
 
        g_return_val_if_fail(addr != NULL, -1);
@@ -390,37 +430,61 @@ int net_gethostbyname(const char *addr, IPADDR *ip4, IPADDR *ip6)
        hints.ai_socktype = SOCK_STREAM;
 
        /* save error to host_error for later use */
-       host_error = getaddrinfo(addr, NULL, &hints, &ai);
-       if (host_error != 0)
-               return host_error;
+       ret = getaddrinfo(addr, NULL, &hints, &ailist);
+       if (ret != 0)
+               return ret;
+
+       /* count IPs */
+        count_v4 = count_v6 = 0;
+       for (ai = ailist; ai != NULL; ai = ai->ai_next) {
+               if (ai->ai_family == AF_INET)
+                       count_v4++;
+               else if (ai->ai_family == AF_INET6)
+                       count_v6++;
+       }
+
+       if (count_v4 == 0 && count_v6 == 0)
+               return HOST_NOT_FOUND; /* shouldn't happen? */
 
-       if (getnameinfo(ai->ai_addr, ai->ai_addrlen, hbuf,
-                       sizeof(hbuf), NULL, 0, NI_NUMERICHOST))
-               return 1;
+       /* if there are multiple addresses, return random one */
+       use_v4 = count_v4 <= 1 ? 0 : rand() % count_v4;
+       use_v6 = count_v6 <= 1 ? 0 : rand() % count_v6;
 
-        origai = ai; count = 0;
-       while (ai != NULL && count < 2) {
+       count_v4 = count_v6 = 0;
+       for (ai = ailist; ai != NULL; ai = ai->ai_next) {
                so = (union sockaddr_union *) ai->ai_addr;
 
-               if (ai->ai_family == AF_INET6 && ip6->family == 0) {
-                       sin_get_ip(so, ip6);
-                        count++;
-               } else if (ai->ai_family == AF_INET && ip4->family == 0) {
-                       sin_get_ip(so, ip4);
-                        count++;
+               if (ai->ai_family == AF_INET) {
+                       if (use_v4 == count_v4)
+                               sin_get_ip(so, ip4);
+                        count_v4++;
+               } else if (ai->ai_family == AF_INET6) {
+                       if (use_v6 == count_v6)
+                               sin_get_ip(so, ip6);
+                       count_v6++;
                }
-                ai = ai->ai_next;
        }
-       freeaddrinfo(origai);
+       freeaddrinfo(ailist);
+       return 0;
 #else
        hp = gethostbyname(addr);
-       if (hp == NULL) return h_errno;
+       if (hp == NULL)
+               return h_errno;
+
+       /* count IPs */
+       count = 0;
+       while (hp->h_addr_list[count] != NULL)
+               count++;
 
+       if (count == 0)
+               return HOST_NOT_FOUND; /* shouldn't happen? */
+
+       /* if there are multiple addresses, return random one */
        ip4->family = AF_INET;
-       memcpy(&ip4->ip, hp->h_addr, 4);
-#endif
+       memcpy(&ip4->ip, hp->h_addr_list[rand() % count], 4);
 
        return 0;
+#endif
 }
 
 /* Get name for host, *name should be g_free()'d unless it's NULL.
@@ -428,33 +492,31 @@ int net_gethostbyname(const char *addr, IPADDR *ip4, IPADDR *ip6)
 int net_gethostbyaddr(IPADDR *ip, char **name)
 {
 #ifdef HAVE_IPV6
-       struct addrinfo req, *ai;
+       union sockaddr_union so;
        int host_error;
+       char hostname[NI_MAXHOST];
 #else
        struct hostent *hp;
 #endif
-       char ipname[MAX_IP_LEN];
 
        g_return_val_if_fail(ip != NULL, -1);
        g_return_val_if_fail(name != NULL, -1);
 
-       net_ip2host(ip, ipname);
-
        *name = NULL;
 #ifdef HAVE_IPV6
-       memset(&req, 0, sizeof(struct addrinfo));
-       req.ai_socktype = SOCK_STREAM;
-       req.ai_flags = AI_CANONNAME;
+       memset(&so, 0, sizeof(so));
+       sin_set_ip(&so, ip);
 
        /* save error to host_error for later use */
-       host_error = getaddrinfo(ipname, NULL, &req, &ai);
-       if (host_error != 0)
-               return host_error;
-       *name = g_strdup(ai->ai_canonname);
+        host_error = getnameinfo((struct sockaddr *) &so, sizeof(so),
+                                 hostname, sizeof(hostname), NULL, 0, 0);
+        if (host_error != 0)
+                return host_error;
 
-       freeaddrinfo(ai);
+       *name = g_strdup(hostname);
 #else
-       hp = gethostbyaddr(ipname, strlen(ipname), AF_INET);
+       if (ip->family != AF_INET) return -1;
+       hp = gethostbyaddr((const char *) &ip->ip, 4, AF_INET);
        if (hp == NULL) return -1;
 
        *name = g_strdup(hp->h_name);
@@ -471,12 +533,16 @@ int net_ip2host(IPADDR *ip, char *host)
 #else
        unsigned long ip4;
 
-       ip4 = ntohl(ip->ip.s_addr);
-       g_snprintf(host, MAX_IP_LEN, "%lu.%lu.%lu.%lu",
-                  (ip4 & 0xff000000UL) >> 24,
-                  (ip4 & 0x00ff0000) >> 16,
-                  (ip4 & 0x0000ff00) >> 8,
-                  (ip4 & 0x000000ff));
+       if (ip->family != AF_INET) {
+               strcpy(host, "0.0.0.0");
+       } else {
+               ip4 = ntohl(ip->ip.s_addr);
+               g_snprintf(host, MAX_IP_LEN, "%lu.%lu.%lu.%lu",
+                          (ip4 & 0xff000000UL) >> 24,
+                          (ip4 & 0x00ff0000) >> 16,
+                          (ip4 & 0x0000ff00) >> 8,
+                          (ip4 & 0x000000ff));
+       }
 #endif
        return 0;
 }
@@ -485,15 +551,16 @@ int net_host2ip(const char *host, IPADDR *ip)
 {
        unsigned long addr;
 
-#ifdef HAVE_IPV6
        if (strchr(host, ':') != NULL) {
                /* IPv6 */
                ip->family = AF_INET6;
+#ifdef HAVE_IPV6
                if (inet_pton(AF_INET6, host, &ip->ip) == 0)
                        return -1;
-       } else
+#else
+               ip->ip.s_addr = 0;
 #endif
-       {
+       } else {
                /* IPv4 */
                ip->family = AF_INET;
 #ifdef HAVE_INET_ATON
@@ -530,12 +597,6 @@ const char *net_gethosterror(int error)
 #ifdef HAVE_IPV6
        g_return_val_if_fail(error != 0, NULL);
 
-       if (error == 1) {
-               /* getnameinfo() failed ..
-                  FIXME: does strerror return the right error message? */
-               return g_strerror(errno);
-       }
-
        return gai_strerror(error);
 #else
        switch (error) {
@@ -559,7 +620,11 @@ const char *net_gethosterror(int error)
 int net_hosterror_notfound(int error)
 {
 #ifdef HAVE_IPV6
+#ifdef EAI_NODATA /* NODATA is depricated */
        return error != 1 && (error == EAI_NONAME || error == EAI_NODATA);
+#else
+       return error != 1 && (error == EAI_NONAME);
+#endif
 #else
        return error == HOST_NOT_FOUND || error == NO_ADDRESS;
 #endif
@@ -577,7 +642,7 @@ char *net_getservbyport(int port)
 int is_ipv4_address(const char *host)
 {
        while (*host != '\0') {
-               if (*host != '.' && !isdigit(*host))
+               if (*host != '.' && !i_isdigit(*host))
                        return 0;
                 host++;
        }
@@ -588,7 +653,7 @@ int is_ipv4_address(const char *host)
 int is_ipv6_address(const char *host)
 {
        while (*host != '\0') {
-               if (*host != ':' && !isxdigit(*host))
+               if (*host != ':' && !i_isxdigit(*host))
                        return 0;
                 host++;
        }