+} /* int network_send */
+#endif
+
+static int network_write (const data_set_t *ds, const value_list_t *vl)
+{
+ char buf[BUFF_SIZE];
+ char *buf_ptr;
+ int buf_len;
+
+ sockent_t *se;
+
+ DBG ("host = %s; plugin = %s; plugin_instance = %s; type = %s; type_instance = %s;",
+ vl->host, vl->plugin, vl->plugin_instance, ds->type, vl->type_instance);
+
+ buf_len = sizeof (buf);
+ buf_ptr = buf;
+ if (write_part_string (&buf_ptr, &buf_len, TYPE_HOST,
+ vl->host, strlen (vl->host)) != 0)
+ return (-1);
+ if (write_part_string (&buf_ptr, &buf_len, TYPE_PLUGIN,
+ vl->plugin, strlen (vl->plugin)) != 0)
+ return (-1);
+ if (strlen (vl->plugin_instance) > 0)
+ if (write_part_string (&buf_ptr, &buf_len, TYPE_PLUGIN_INSTANCE,
+ vl->plugin_instance,
+ strlen (vl->plugin_instance)) != 0)
+ return (-1);
+ if (write_part_string (&buf_ptr, &buf_len, TYPE_TYPE,
+ ds->type, strlen (ds->type)) != 0)
+ return (-1);
+ if (strlen (vl->type_instance) > 0)
+ if (write_part_string (&buf_ptr, &buf_len, TYPE_PLUGIN_INSTANCE,
+ vl->type_instance,
+ strlen (vl->type_instance)) != 0)
+ return (-1);
+
+ write_part_values (&buf_ptr, &buf_len, ds, vl);
+
+ buf_len = sizeof (buf) - buf_len;
+
+ for (se = sending_sockets; se != NULL; se = se->next)
+ {
+ int status;
+
+ while (42)
+ {
+ status = sendto (se->fd, buf, buf_len, 0 /* no flags */,
+ (struct sockaddr *) se->addr, se->addrlen);
+ if (status < 0)
+ {
+ if (errno == EINTR)
+ continue;
+ syslog (LOG_ERR, "network: sendto failed: %s",
+ strerror (errno));
+ break;
+ }
+
+ break;
+ } /* while (42) */
+ } /* for (sending_sockets) */
+
+ return 0;
+}
+
+static int network_config (const char *key, const char *val)
+{
+ char *node;
+ char *service;
+
+ char *fields[3];
+ int fields_num;
+
+ if ((strcasecmp ("Listen", key) == 0)
+ || (strcasecmp ("Server", key) == 0))
+ {
+ char *val_cpy = strdup (val);
+ if (val_cpy == NULL)
+ return (1);
+
+ service = NET_DEFAULT_PORT;
+ fields_num = strsplit (val_cpy, fields, 3);
+ if ((fields_num != 1)
+ && (fields_num != 2))
+ return (1);
+ else if (fields_num == 2)
+ service = fields[1];
+ node = fields[0];
+
+ if (strcasecmp ("Listen", key) == 0)
+ network_add_listen_socket (node, service);
+ else
+ network_add_sending_socket (node, service);
+ }
+ else if (strcasecmp ("TimeToLive", key) == 0)
+ {
+ int tmp = atoi (val);
+ if ((tmp > 0) && (tmp < 256))
+ network_config_ttl = tmp;
+ else
+ return (1);
+ }
+ else
+ {
+ return (-1);
+ }
+ return (0);
+}
+
+static int network_shutdown (void)
+{
+ DBG ("Shutting down.");
+
+ listen_loop++;
+
+ pthread_kill (listen_thread, SIGTERM);
+ pthread_join (listen_thread, NULL /* no return value */);
+
+ listen_thread = 0;
+
+ return (0);
+}
+
+static int network_init (void)
+{
+ plugin_register_shutdown ("network", network_shutdown);
+
+ /* setup socket(s) and so on */
+ if (sending_sockets != NULL)
+ plugin_register_write ("network", network_write);
+
+ if ((listen_sockets_num != 0) && (listen_thread == 0))
+ {
+ int status;
+
+ status = pthread_create (&listen_thread, NULL /* no attributes */,
+ receive_thread, NULL /* no argument */);
+
+ if (status != 0)
+ syslog (LOG_ERR, "network: pthread_create failed: %s",
+ strerror (errno));
+ }
+ return (0);
+} /* int network_init */
+
+void module_register (void)
+{
+ plugin_register_config ("network", network_config,
+ config_keys, config_keys_num);
+ plugin_register_init ("network", network_init);