Bug#404018: Close the file descriptor when binding to a socket fails.
[collectd.git] / src / network.c
index 5f9aa74..e9ba84b 100644 (file)
@@ -1,6 +1,6 @@
 /**
  * collectd - src/network.c
- * Copyright (C) 2006  Florian octo Forster
+ * Copyright (C) 2005,2006  Florian octo Forster
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the
 /* 1500 - 40 - 8  =  Ethernet packet - IPv6 header - UDP header */
 /* #define BUFF_SIZE 1452 */
 
+#ifndef IPV6_ADD_MEMBERSHIP
+# ifdef IPV6_JOIN_GROUP
+#  define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
+# else
+#  error "Neither IP_ADD_MEMBERSHIP nor IPV6_JOIN_GROUP is defined"
+# endif
+#endif /* !IP_ADD_MEMBERSHIP */
+
 #define BUFF_SIZE 4096
 
-#ifdef HAVE_LIBRRD
 extern int operating_mode;
-#else
-static int operating_mode = MODE_CLIENT;
-#endif
 
 typedef struct sockent
 {
@@ -210,15 +214,25 @@ int network_create_socket (const char *node, const char *service)
 
        DBG ("node = %s, service = %s", node, service);
 
-       if (operating_mode == MODE_LOCAL)
+       if (operating_mode == MODE_LOCAL || operating_mode == MODE_LOG)
+       {
+               syslog (LOG_WARNING, "network_create_socket: There is no point opening a socket when you are in mode `%s'.",
+                               operating_mode == MODE_LOCAL ? "Local" : "Log");
                return (-1);
+       }
 
        socklist_tail = socklist_head;
        while ((socklist_tail != NULL) && (socklist_tail->next != NULL))
                socklist_tail = socklist_tail->next;
 
        memset (&ai_hints, '\0', sizeof (ai_hints));
-       ai_hints.ai_flags    = AI_PASSIVE | AI_ADDRCONFIG;
+       ai_hints.ai_flags    = 0;
+#ifdef AI_PASSIVE
+       ai_hints.ai_flags |= AI_PASSIVE;
+#endif
+#ifdef AI_ADDRCONFIG
+       ai_hints.ai_flags |= AI_ADDRCONFIG;
+#endif
        ai_hints.ai_family   = PF_UNSPEC;
        ai_hints.ai_socktype = SOCK_DGRAM;
        ai_hints.ai_protocol = IPPROTO_UDP;
@@ -270,6 +284,7 @@ int network_create_socket (const char *node, const char *service)
                {
                        if (network_bind_socket (se, ai_ptr) != 0)
                        {
+                               close (se->fd);
                                free (se->addr);
                                free (se);
                                continue;
@@ -437,7 +452,7 @@ int network_receive (char **host, char **type, char **inst, char **value)
        {
                syslog (LOG_WARNING, "Invalid message from `%s'", *host);
                free (*host); *host = NULL;
-               return (-1);
+               return (1);
        }
 
        if ((*type = strdup (fields[0])) == NULL)
@@ -502,8 +517,6 @@ int network_send (char *type, char *inst, char *value)
                if (se->mode != operating_mode)
                        continue;
 
-               DBG ("fd = %i", se->fd);
-
                while (1)
                {
                        status = sendto (se->fd, buf, buflen, 0,