+/*
+ * NAME
+ * hddtemp_query_daemon
+ *
+ * DESCRIPTION
+ * Connect to the hddtemp daemon and receive data.
+ *
+ * ARGUMENTS:
+ * `buffer' The buffer where we put the received ascii string.
+ * `buffer_size' Size of the buffer
+ *
+ * RETURN VALUE:
+ * >= 0 if ok, < 0 otherwise.
+ *
+ * NOTES:
+ * Example of possible strings, as received from daemon:
+ * |/dev/hda|ST340014A|36|C|
+ * |/dev/hda|ST380011A|46|C||/dev/hdd|ST340016A|SLP|*|
+ *
+ * FIXME:
+ * we need to create a new socket each time. Is there another way?
+ * Hm, maybe we can re-use the `sockaddr' structure? -octo
+ */
+static int hddtemp_query_daemon (char *buffer, int buffer_size)
+{
+ int fd;
+ ssize_t status;
+ int buffer_fill;
+
+ const char *host;
+ const char *port;
+
+ struct addrinfo ai_hints;
+ struct addrinfo *ai_list, *ai_ptr;
+ int ai_return;
+
+ memset (&ai_hints, '\0', sizeof (ai_hints));
+ ai_hints.ai_flags = 0;
+#ifdef AI_ADDRCONFIG
+ ai_hints.ai_flags |= AI_ADDRCONFIG;
+#endif
+ ai_hints.ai_family = PF_UNSPEC;
+ ai_hints.ai_socktype = SOCK_STREAM;
+ ai_hints.ai_protocol = IPPROTO_TCP;
+
+ host = hddtemp_host;
+ if (host == NULL)
+ host = HDDTEMP_DEF_HOST;
+
+ port = hddtemp_port;
+ if (strlen (port) == 0)
+ port = HDDTEMP_DEF_PORT;
+
+ if ((ai_return = getaddrinfo (host, port, &ai_hints, &ai_list)) != 0)
+ {
+ char errbuf[1024];
+ ERROR ("hddtemp: getaddrinfo (%s, %s): %s",
+ host, port,
+ (ai_return == EAI_SYSTEM)
+ ? sstrerror (errno, errbuf, sizeof (errbuf))
+ : gai_strerror (ai_return));
+ return (-1);
+ }
+
+ fd = -1;
+ for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
+ {
+ /* create our socket descriptor */
+ if ((fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol)) < 0)
+ {
+ char errbuf[1024];
+ ERROR ("hddtemp: socket: %s",
+ sstrerror (errno, errbuf, sizeof (errbuf)));
+ continue;
+ }