]> git.karo-electronics.de Git - mdnsd.git/commitdiff
Cleaned up code. Removed netwatch code. Removed option to change the port
authorOle Reinhardt <ole.reinhardt@kernelconcepts.de>
Mon, 29 Apr 2013 23:59:26 +0000 (01:59 +0200)
committerOle Reinhardt <ole.reinhardt@kernelconcepts.de>
Mon, 29 Apr 2013 23:59:26 +0000 (01:59 +0200)
number, ip-address or hostname during runtime.

Tried to get a clean and simple reference implementation.

Removed debug output

mhttp.c

diff --git a/mhttp.c b/mhttp.c
index 3acb93773e9a4ff5130637f0d9a286eefbd9c2c7..24d5e5af0c4e2e5741342f7bbf02faf1b6636702 100644 (file)
--- a/mhttp.c
+++ b/mhttp.c
 #endif
 
 #define HOSTNAMESIZE 64
-#define FIFO_PATH "/tmp/mdns-fifo"
 
-#define MAX_ANNOUNCE_IP 1
-
-enum {
-    MDNSD_STARTUP,
+typedef enum {
     MDNSD_PROBE,
     MDNSD_ANNOUNCE,
     MDNSD_RUN,
     MDNSD_SHUTDOWN
-};
-
-typedef struct _ipcam_ip_info
-{
-    char          *label;
-    char          *ip;
-    int            link_id;
-
-    /* service-discovery records */
-    TMdnsdRecord  *host_to_ip;
-    TMdnsdRecord  *ip_to_host;
-    struct in_addr announce_ip;
-} IpcamIPInfo;
+} TMdnsdState;
 
-typedef struct _ipcam_service_info
+typedef struct _service_info
 {
     TMdnsd        *mdnsd;
     char           hostname[HOSTNAMESIZE];
     char          *servicename;
 
+    char          *ip;
     int            port;
 
-    IpcamIPInfo    ipinfos[MAX_ANNOUNCE_IP];
+    /* service-discovery records */
+    TMdnsdRecord  *host_to_ip;
+    TMdnsdRecord  *ip_to_host;
+    struct in_addr announce_ip;
 
     SHASH          metadata;
 
@@ -68,32 +56,30 @@ typedef struct _ipcam_service_info
 
     TMdnsdRecord  *ptr_to_srv;
 
-    int            state;
-} IpcamServiceInfo;
+    TMdnsdState    state;
+} ServiceInfo;
 
-static IpcamServiceInfo ipcam_info;
-static int signal_pipe[2];
-static int fifo_fd;
+static ServiceInfo service_info;
 
-void     request_service      (IpcamServiceInfo *info, int stage);
-void     request_ip_addresses (IpcamServiceInfo *info);
+void     request_service      (ServiceInfo *info, int stage);
+void     request_ip_addresses (ServiceInfo *info);
 
-char *
-increment_name (char *name)
+char *increment_name (char *name)
 {
     int   id = 1;
-    char *pos, *end = NULL;
+    char *pos;
+       char *end = NULL;
     char *ret = NULL;
 
     pos = strrchr (name, '-');
 
-    if (pos)
-    {
+    if (pos) {
         id = strtol (pos + 1, &end, 10);
-        if (*end == '\0')
+        if (*end == '\0') {
             *pos = '\0';
-        else
+               } else {
             id = 1;
+               }
     }
 
     id += 1;
@@ -105,171 +91,149 @@ increment_name (char *name)
 
 
 /* conflict handling */
-void
-handle_conflict (TMdnsdRecord *r, uint8_t *name, int UNUSED(type), void *arg)
+void handle_conflict (TMdnsdRecord *record, uint8_t *name, int UNUSED(type), void *arg)
 {
-    IpcamServiceInfo *info = (IpcamServiceInfo *) arg;
+    ServiceInfo *info = (ServiceInfo *) arg;
     char *newname;
-    int i;
 
-    for (i = 0; i < MAX_ANNOUNCE_IP; i++)
-    {
-        if (r == info->ipinfos[i].ip_to_host)
-        {
-            /* can't do anything about a reverse lookup conflict. Just stop
-             * announcing it. */
-            info->ipinfos[i].ip_to_host = NULL;
-            fprintf (stderr, "zeroconf reverse lookup conflict for %s!\n", info->ipinfos[i].label);
-            return;
-        }
-        if (r == info->ipinfos[i].host_to_ip)
-        {
-            info->ipinfos[i].host_to_ip = NULL;
-            info->ipinfos[i].announce_ip.s_addr = 0;
-        }
+    if (record == info->ip_to_host) {
+        /* can't do anything about a reverse lookup conflict. Just stop
+         * announcing it. */
+        info->ip_to_host = NULL;
+        fprintf (stderr, "zeroconf reverse lookup conflict for %s!\n", info->ip);
+        return;
     }
 
-    if (info->servicename == NULL)
-    {
-        newname = increment_name (info->hostname);
+    if (record == info->host_to_ip) {
+        info->host_to_ip = NULL;
+        info->announce_ip.s_addr = 0;
     }
-    else
-    {
+
+    if (info->servicename == NULL) {
+        newname = increment_name (info->hostname);
+    } else {
         newname = increment_name (info->servicename);
         free (info->servicename);
     }
 
     info->servicename = newname;
 
-    if (r == info->srv_to_host)
+    if (record == info->srv_to_host) {
         info->srv_to_host = NULL;
-    if (r == info->txt_for_srv)
+       }
+
+    if (record == info->txt_for_srv) {
         info->txt_for_srv = NULL;
+       }
 
     fprintf (stderr, "conflicting name \"%s\". trying %s\n",
              name, info->servicename);
 
+       /* The hostname was changed, so go back to probe state */
     info->state = MDNSD_PROBE;
-    write (signal_pipe[1], " ", 1);
 }
 
 
 /* quit and updates */
 void sighandler (int sig)
 {
-    if (sig != SIGHUP)
-    {
-        ipcam_info.state = MDNSD_SHUTDOWN;
+    if (sig != SIGHUP) {
+        service_info.state = MDNSD_SHUTDOWN;
     }
-
-    write (signal_pipe[1], " ", 1);
 }
 
 
 /* create multicast 224.0.0.251:5353 socket */
-int
-msock ()
+int msock ()
 {
-    int s, flag = 1, ittl = 255;
+    int    sock_fd;
+       int    flag = 1;
+       int    ittl = 255;
+    char   ttl = 255;
     struct sockaddr_in in;
     struct ip_mreq mc;
-    char ttl = 255;
 
     bzero (&in, sizeof (in));
     in.sin_family = AF_INET;
     in.sin_port = htons (MDNS_PORT);
     in.sin_addr.s_addr = 0;
 
-    if ((s = socket (AF_INET,SOCK_DGRAM,0)) < 0)
+    if ((sock_fd = socket (AF_INET, SOCK_DGRAM, 0)) < 0) {
         return 0;
+       }
 
-    setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*) &flag, sizeof (flag));
-    if (bind (s, (struct sockaddr*) &in, sizeof (in)))
-    {
-        close(s);
+    setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, (char*) &flag, sizeof (flag));
+    if (bind (sock_fd, (struct sockaddr*) &in, sizeof (in))) {
+        close(sock_fd);
         return 0;
     }
 
     mc.imr_multiaddr.s_addr = inet_addr ("224.0.0.251");
     mc.imr_interface.s_addr = htonl (INADDR_ANY);
-    setsockopt (s, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mc,   sizeof (mc));
-    setsockopt (s, IPPROTO_IP, IP_MULTICAST_TTL,  &ttl,  sizeof (ttl));
-    setsockopt (s, IPPROTO_IP, IP_MULTICAST_TTL,  &ittl, sizeof (ittl));
+    setsockopt (sock_fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mc,   sizeof (mc));
+    setsockopt (sock_fd, IPPROTO_IP, IP_MULTICAST_TTL,  &ttl,  sizeof (ttl));
+    setsockopt (sock_fd, IPPROTO_IP, IP_MULTICAST_TTL,  &ittl, sizeof (ittl));
 
-    flag =  fcntl (s, F_GETFL, 0);
+    flag =  fcntl (sock_fd, F_GETFL, 0);
     flag |= O_NONBLOCK;
-    fcntl (s, F_SETFL, flag);
+    fcntl (sock_fd, F_SETFL, flag);
 
-    return s;
+    return sock_fd;
 }
 
-void
-request_ip_addresses (IpcamServiceInfo *info)
+void request_ip_addresses (ServiceInfo *info)
 {
-    char revlookup[256], hostlocal[256];
-    int i;
+    char revlookup[256];
+       char hostlocal[256];
     struct in_addr ip;
-    int num_ips = 0;
+    int  num_ips = 0;
 
-    sprintf (hostlocal, "%s.local.",
-             info->servicename ? info->servicename : info->hostname);
+    sprintf (hostlocal, "%s.local.", info->servicename ? info->servicename : info->hostname);
 
-    for (i = 0; i < MAX_ANNOUNCE_IP; i++)
-    {
-printf("--> ip --> %s\n", info->ipinfos[i].ip);
-        if (info->ipinfos[i].ip)
-        {
-            ip.s_addr = inet_addr (info->ipinfos[i].ip);
+    if (info->ip) {
+        ip.s_addr = inet_addr (info->ip);
 
-            if (ip.s_addr != info->ipinfos[i].announce_ip.s_addr)
-            {
-                snprintf (revlookup, 256, "%d.%d.%d.%d.in-addr.arpa.",
-                          (ip.s_addr >> 24) & 0xff, (ip.s_addr >> 16) & 0xff, (ip.s_addr >> 8) & 0xff, (ip.s_addr >> 0) & 0xff);
-                // TODO: OR
-                printf("revlookup %d -> %s\n", i, revlookup);
-                if (!info->ipinfos[i].host_to_ip)
-                {
-                    info->ipinfos[i].host_to_ip  = MdnsdAllocUnique (info->mdnsd, hostlocal,
-                                                                 QTYPE_A, 120, handle_conflict, info);
-                }
-                MdnsdSetRaw (info->mdnsd, info->ipinfos[i].host_to_ip, (uint8_t *) &ip, 4);
+        if (ip.s_addr != info->announce_ip.s_addr) {
+            snprintf (revlookup, 256, "%d.%d.%d.%d.in-addr.arpa.",
+                      (ip.s_addr >> 24) & 0xff, (ip.s_addr >> 16) & 0xff,
+                      (ip.s_addr >> 8) & 0xff, (ip.s_addr >> 0) & 0xff);
 
-                if (!info->ipinfos[i].ip_to_host)
-                {
-                    info->ipinfos[i].ip_to_host  = MdnsdAllocUnique (info->mdnsd, revlookup,
-                                                                 QTYPE_PTR, 120, handle_conflict, info);
-                }
-                MdnsdSetHost (info->mdnsd, info->ipinfos[i].ip_to_host, hostlocal);
+            if (!info->host_to_ip) {
+                info->host_to_ip  = MdnsdAllocUnique(info->mdnsd, hostlocal,
+                                                             QTYPE_A, 120, handle_conflict, info);
+            }
+            MdnsdSetRaw (info->mdnsd, info->host_to_ip, (uint8_t *) &ip, 4);
 
-                info->ipinfos[i].announce_ip = ip;
+            if (!info->ip_to_host) {
+                info->ip_to_host  = MdnsdAllocUnique(info->mdnsd, revlookup,
+                                                             QTYPE_PTR, 120, handle_conflict, info);
             }
+            MdnsdSetHost (info->mdnsd, info->ip_to_host, hostlocal);
 
-            num_ips++;
+            info->announce_ip = ip;
         }
-        else
-        {
-            if (info->ipinfos[i].host_to_ip)
-                MdnsdDone (ipcam_info.mdnsd, info->ipinfos[i].host_to_ip);
-            if (info->ipinfos[i].ip_to_host)
-                MdnsdDone (ipcam_info.mdnsd, info->ipinfos[i].ip_to_host);
-
-            info->ipinfos[i].host_to_ip = NULL;
-            info->ipinfos[i].ip_to_host = NULL;
-            info->ipinfos[i].announce_ip.s_addr = 0;
-        }
-    }
 
-    if (!num_ips) {
-        printf("----> Set state startup\n");
-        info->state = MDNSD_STARTUP;
+        num_ips++;
+    } else {
+        if (info->host_to_ip) {
+            MdnsdDone (service_info.mdnsd, info->host_to_ip);
+               }
+        if (info->ip_to_host) {
+            MdnsdDone (service_info.mdnsd, info->ip_to_host);
+               }
+
+        info->host_to_ip = NULL;
+        info->ip_to_host = NULL;
+        info->announce_ip.s_addr = 0;
     }
 }
 
-void
-request_service (IpcamServiceInfo *info, int stage)
+void request_service (ServiceInfo *info, int stage)
 {
-    uint8_t *packet, servlocal[256], hostlocal[256];
-    int i, len = 0;
+    uint8_t *packet;
+       char     servlocal[256];
+       char     hostlocal[256];
+    int      len = 0;
 
     sprintf (servlocal, "%s._http._tcp.local.",
              info->servicename ? info->servicename : info->hostname);
@@ -290,7 +254,6 @@ request_service (IpcamServiceInfo *info, int stage)
     switch (stage)
     {
         case 0:
-            printf("---> request_service\n");
             request_ip_addresses (info);
 
             break;
@@ -299,39 +262,35 @@ request_service (IpcamServiceInfo *info, int stage)
             sprintf (hostlocal, "%s.local.",
                      info->servicename ? info->servicename : info->hostname);
 
-            if (!info->srv_to_host)
-        {
-            info->srv_to_host = MdnsdAllocUnique (info->mdnsd, servlocal,
-                                              QTYPE_SRV, 120, handle_conflict, info);
-        }
-            MdnsdSetSrv (info->mdnsd, info->srv_to_host, 0, 0,
-                           info->port, hostlocal);
+            if (!info->srv_to_host) {
+                info->srv_to_host = MdnsdAllocUnique (info->mdnsd, servlocal,
+                                                      QTYPE_SRV, 120, handle_conflict, info);
+            }
+
+            MdnsdSetSrv (info->mdnsd, info->srv_to_host, 0, 0, info->port, hostlocal);
+
+            if (!info->txt_for_srv) {
+                info->txt_for_srv = MdnsdAllocUnique (info->mdnsd, servlocal,
+                                                      QTYPE_TXT, 4500, handle_conflict, info);
+            }
 
-            if (!info->txt_for_srv)
-        {
-            info->txt_for_srv = MdnsdAllocUnique (info->mdnsd, servlocal,
-                                              QTYPE_TXT, 4500, handle_conflict, info);
-        }
             packet = DnsSd2Txt (info->metadata, &len);
             MdnsdSetRaw (info->mdnsd, info->txt_for_srv, packet, len);
             free(packet);
             break;
 
         case 2:
-            if (!info->ptr_to_srv)
-        {
-            info->ptr_to_srv  = MdnsdAllocShared (info->mdnsd, "_http._tcp.local.",
-                                              QTYPE_PTR, 4500);
-        }
+            if (!info->ptr_to_srv) {
+                info->ptr_to_srv  = MdnsdAllocShared (info->mdnsd, "_http._tcp.local.",
+                                                  QTYPE_PTR, 4500);
+            }
             MdnsdSetHost (info->mdnsd, info->ptr_to_srv, servlocal);
 
-            for (i = 0; i < MAX_ANNOUNCE_IP; i++)
-        {
-            if (info->ipinfos[i].ip)
+            if (info->ip) {
                 fprintf (stderr, "Announcing \"%s.local\" to %s:%d\n",
                          info->servicename ? info->servicename : info->hostname,
-                         info->ipinfos[i].ip, info->port);
-        }
+                         info->ip, info->port);
+                       }
             break;
 
         default:
@@ -340,167 +299,64 @@ request_service (IpcamServiceInfo *info, int stage)
     }
 }
 
-void
-update_port_info (IpcamServiceInfo *info, int port)
-{
-    uint8_t hostlocal[256];
-
-    if (port == info->port)
-        return;
-
-    info->port = port;
-
-    if (!info->srv_to_host)
-        return;
-
-    sprintf (hostlocal, "%s.local.",
-             info->servicename ? info->servicename : info->hostname);
-
-    fprintf (stderr, "mhttp: updating port info to port %d\n", info->port);
-    MdnsdSetSrv (info->mdnsd, info->srv_to_host, 0, 0,
-                   info->port, hostlocal);
-}
-
-#if 0
-void
-iface_change_callback (int   link_index,
-                       char *label,
-                       char *ipaddr,
-                       int   add,
-                       void *user_data)
-{
-    IpcamServiceInfo *info = (IpcamServiceInfo *) user_data;
-    int i;
-
-    for (i = 0; i < MAX_ANNOUNCE_IP; i++)
-    {
-        if (strcmp (info->ipinfos[i].label, label) != 0)
-            continue;
-
-        if (add && (!info->ipinfos[i].ip ||
-                    strcmp (info->ipinfos[i].ip, ipaddr) != 0 ||
-                    info->ipinfos[i].link_id != link_index))
-        {
-            if (info->ipinfos[i].ip)
-                free (info->ipinfos[i].ip);
-            info->ipinfos[i].ip = strdup (ipaddr);
-            info->ipinfos[i].link_id = link_index;
-            fprintf (stderr, "new ip address on %s: %s\n", label, ipaddr);
-        }
-
-        if (!add && info->ipinfos[i].ip)
-        {
-            fprintf (stderr, "lost ip address on %s\n", label);
-            free (info->ipinfos[i].ip);
-            info->ipinfos[i].ip = NULL;
-            info->ipinfos[i].link_id = -1;
-        }
-    }
-
-    if (add && info->state == MDNSD_STARTUP)
-    {
-        info->state = MDNSD_PROBE;
-    }
-    else
-    {
-        printf("---> iface_change_callback\n");
-        request_ip_addresses (info);
-    }
-
-    write (signal_pipe[1], " ", 1);
-}
-
-
-void
-iface_link_callback (int   link_index,
-                     int   running,
-                     void *user_data)
-{
-    IpcamServiceInfo *info = (IpcamServiceInfo *) user_data;
-    int i;
-    int link_changed = 0;
-
-    for (i = 0; i < MAX_ANNOUNCE_IP; i++)
-        if (link_index == info->ipinfos[i].link_id)
-        link_changed = 1;
-
-    if (!link_changed)
-        return;
-
-    info->state = running ? MDNSD_PROBE : MDNSD_STARTUP;
-    write (signal_pipe[1], " ", 1);
-}
-#endif
-
 int main(int argc, char *argv[])
 {
     DNSMESSAGE msg;
-    uint16_t port;
+    uint16_t   port;
     struct timeval tv;
-    int bsize, ssize = sizeof(struct sockaddr_in);
-    uint8_t buf[MAX_PACKET_LEN];
-    struct sockaddr_in from, to;
-    int i, s;
-//  int nlink;
+    int        bsize;
+       int        ssize = sizeof(struct sockaddr_in);
+    uint8_t    buf[MAX_PACKET_LEN];
+    struct sockaddr_in from;
+       struct sockaddr_in to;
+    int        idx;
+       int        s;
     struct in_addr remote_ip;
-    char *value;
-    int polltime = 0;
-    int announce_stage = 0;
-    struct pollfd fds[4];
+    char      *value;
+    int        polltime = 0;
+    int        announce_stage = 0;
+    struct pollfd fds[1];
 
     if(argc < 4)
     {
-        fprintf (stderr, "usage: mhttp <label> <ip> <port> <key1>=<value1> <key2>=<value2> ...\n");
-        fprintf (stderr, "   <label1> are the labels of the network interface to be watched\n");
+        fprintf (stderr, "usage: mhttp <ip> <port> <key1>=<value1> <key2>=<value2> ...\n");
+        fprintf (stderr, "   <ip>  The IP address to promote\n");
         fprintf (stderr, "   <port> is the port number of the service to be advertized\n");
         fprintf (stderr, "   <key>=<value> are the keys that get embedded into the TXT record.\n");
-        fprintf (stderr, "\n   The port later can be changed by writing \"port:8080\" to " FIFO_PATH ".\n");
         return -1;
     }
 
-    ipcam_info.mdnsd = MdnsdNew (1, 1000);
+    service_info.mdnsd = MdnsdNew (1, 1000);
 
-    ipcam_info.state = MDNSD_STARTUP;
-    // TODO: OR
-    //  gethostname (ipcam_info.hostname, HOSTNAMESIZE);
-    strcpy(ipcam_info.hostname, "reinhardt");
-    ipcam_info.hostname[HOSTNAMESIZE-1] = '\0';
-    if (strchr (ipcam_info.hostname, '.'))
-        strchr (ipcam_info.hostname, '.')[0] = '\0';
+       //gethostname (service_info.hostname, HOSTNAMESIZE);
+       sprintf(service_info.hostname, "reinhardt");
+    service_info.hostname[HOSTNAMESIZE-1] = '\0';
+    if (strchr (service_info.hostname, '.'))
+        strchr (service_info.hostname, '.')[0] = '\0';
 
-    ipcam_info.servicename = NULL;
+    service_info.servicename = NULL;
 
-    for (i = 0; i < MAX_ANNOUNCE_IP; i++)
-    {
-        ipcam_info.ipinfos[i].label       = strdup("ethx");
-        ipcam_info.ipinfos[i].label[3] = '0' + i;
-
-        ipcam_info.ipinfos[i].ip          = strdup(argv[i+1]);
-        ipcam_info.ipinfos[i].link_id     = -1;
-        ipcam_info.ipinfos[i].announce_ip.s_addr = inet_addr(ipcam_info.ipinfos[i].ip);
-        ipcam_info.ipinfos[i].host_to_ip  = NULL;
-        ipcam_info.ipinfos[i].ip_to_host  = NULL;
-    }
+    service_info.ip          = strdup(argv[1]);
+    service_info.announce_ip.s_addr = inet_addr(service_info.ip);
+    service_info.host_to_ip  = NULL;
+    service_info.ip_to_host  = NULL;
 
-    ipcam_info.port = atoi(argv[1 + MAX_ANNOUNCE_IP]);
+    service_info.port = atoi(argv[2]);
 
-    ipcam_info.metadata = SHashInit (11);
-    for (i = 2 + MAX_ANNOUNCE_IP; i < argc; i++)
-    {
-        value = index (argv[i], '=');
-        if (value)
-        {
+    service_info.metadata = SHashInit (11);
+    for (idx = 2; idx < argc; idx++) {
+        value = index (argv[idx], '=');
+        if (value) {
             value[0] = '\0';
             value++;
-            SHashSet (ipcam_info.metadata, argv[i], value);
+            SHashSet (service_info.metadata, argv[idx], value);
         }
     }
 
-    ipcam_info.ptr_to_srv     = NULL;
-    ipcam_info.srv_to_host    = NULL;
-    ipcam_info.txt_for_srv    = NULL;
+    service_info.ptr_to_srv     = NULL;
+    service_info.srv_to_host    = NULL;
+    service_info.txt_for_srv    = NULL;
 
-    pipe (signal_pipe);
     signal(SIGHUP,  sighandler);
     signal(SIGINT,  sighandler);
     signal(SIGQUIT, sighandler);
@@ -512,220 +368,94 @@ int main(int argc, char *argv[])
         return -1;
     }
 
-#if 0
-    if ((nlink = netwatch_open ()) < 0)
-    {
-         fprintf (stderr, "can't connect to netlink: %s\n", strerror(errno));
-         return -1;
-    }
-
-    netwatch_register_callbacks (iface_change_callback,
-                                 iface_link_callback,
-                                 &ipcam_info);
-    netwatch_queue_inforequest (nlink);
-#endif
-
-    // TODO: OR Set startup state
-    ipcam_info.state = MDNSD_STARTUP;
-
-
-    if (mkfifo (FIFO_PATH, S_IRWXU) < 0)
-    {
-        if (errno != EEXIST)
-        {
-            fprintf (stderr, "can't create named pipe: %s\n", strerror(errno));
-            return -1;
-        }
-    }
-
-    if ((fifo_fd = open (FIFO_PATH, O_RDONLY | O_NONBLOCK)) < 0)
-    {
-        fprintf (stderr, "can't open named pipe: %s\n", strerror(errno));
-        return -1;
-    }
-
-    /* we need to open the fifo for writing as well (although we'll never
-                                                     * use it for this) to avoid POLLHUP to happen when no client wants
-        * something from us. Ugh. */
+    request_ip_addresses (&service_info);
 
-    if ((i = open (FIFO_PATH, O_WRONLY)) < 0)
-    {
-        fprintf (stderr, "can't dummy-open write end of pipe: %s\n",
-                 strerror(errno));
-        return -1;
-    }
+    service_info.state = MDNSD_PROBE;
 
-
-// TODO: OR
-    request_ip_addresses (&ipcam_info);
-    write (signal_pipe[1], " ", 1);
-ipcam_info.state = MDNSD_PROBE;
-//
-    while(1)
-    {
-        fds[0].fd      = signal_pipe[0];
+    while(1) {
+        fds[0].fd      = s;
         fds[0].events  = POLLIN;
         fds[0].revents = 0;
 
-        fds[1].fd      = s;
-        fds[1].events  = POLLIN;
-        fds[1].revents = 0;
-
-        fds[2].fd      = fifo_fd;
-        fds[2].events  = POLLIN;
-        fds[2].revents = 0;
-/*
-        fds[3].fd      = nlink;
-        fds[3].events  = POLLIN;
-        fds[3].revents = 0;
-*/
-        poll (fds, 4, polltime);
-
-        /* only used when we wake-up from a signal */
-        if (fds[0].revents)
-        {
-            char hostname[HOSTNAMESIZE];
+        poll (fds, 1, polltime);
 
-            read (signal_pipe[0], buf, MAX_PACKET_LEN);
+        switch (service_info.state)
+        {
+            case MDNSD_PROBE:
 
-            //TODO: OR
-            //          gethostname (hostname, HOSTNAMESIZE);
-            strcpy(hostname, "reinhardt");
-            hostname[HOSTNAMESIZE-1] = '\0';
-            if (strchr (hostname, '.'))
-                strchr (hostname, '.')[0] = '\0';
-            if (strcmp (hostname, ipcam_info.hostname))
-            {
-                /* hostname changed */
-                strcpy (ipcam_info.hostname, hostname);
-                free (ipcam_info.servicename);
-                ipcam_info.servicename = NULL;
+                               if (service_info.ptr_to_srv) {
+                    MdnsdDone (service_info.mdnsd, service_info.ptr_to_srv);
+                               }
 
-                ipcam_info.state = MDNSD_PROBE;
-            }
-        }
+                               if (service_info.srv_to_host) {
+                    MdnsdDone (service_info.mdnsd, service_info.srv_to_host);
+                               }
 
-        if (fds[2].revents)
-        {
-            char message[1024];
-            int ret;
+                if (service_info.txt_for_srv) {
+                    MdnsdDone (service_info.mdnsd, service_info.txt_for_srv);
+                               }
 
-            ret = read (fifo_fd, message, 1023);
+                service_info.ptr_to_srv     = NULL;
+                service_info.srv_to_host    = NULL;
+                service_info.txt_for_srv    = NULL;
 
-            if (ret > 0)
-            {
-                message[ret] = '\0';
+                if (service_info.host_to_ip) {
+                    MdnsdDone (service_info.mdnsd, service_info.host_to_ip);
+                               }
 
-                if (!strncmp ("port:", message, 5))
-                {
-                    int port = atoi (message + 5);
-                    if (port > 0 && port < 65536)
-                        update_port_info (&ipcam_info, port);
-                }
-                else
-                {
-                    fprintf (stderr, "mdnsd: got unknown fifo message: %s", message);
-                }
-            }
-            else if (ret < 0)
-            {
-                fprintf (stderr, "mdnsd: can't read from pipe: %s\n", strerror (errno));
-            }
-        }
-/*
-        if (fds[3].revents)
-        {
-            netwatch_dispatch (nlink);
-        }
-*/
-        // TODO: OR
-        printf("---> ipcam_info.state = %d\n", ipcam_info.state);
-        switch (ipcam_info.state)
-        {
-            case MDNSD_STARTUP:
-printf("--->Startup\n");
-                /* we're waiting for a netwatch based statechange */
-                /* fprintf (stderr, "in STARTUP\n"); */
-                polltime = 5000;
-                break;
+                if (service_info.ip_to_host) {
+                    MdnsdDone (service_info.mdnsd, service_info.ip_to_host);
+                               }
 
-            case MDNSD_PROBE:
-printf("--->Probe\n");
-                /* fprintf (stderr, "in PROBE\n"); */
-                if (ipcam_info.ptr_to_srv)
-                    MdnsdDone (ipcam_info.mdnsd, ipcam_info.ptr_to_srv);
-                if (ipcam_info.srv_to_host)
-                    MdnsdDone (ipcam_info.mdnsd, ipcam_info.srv_to_host);
-                if (ipcam_info.txt_for_srv)
-                    MdnsdDone (ipcam_info.mdnsd, ipcam_info.txt_for_srv);
-
-                ipcam_info.ptr_to_srv     = NULL;
-                ipcam_info.srv_to_host    = NULL;
-                ipcam_info.txt_for_srv    = NULL;
-
-                for (i = 0; i < MAX_ANNOUNCE_IP; i++)
-                {
-                    if (ipcam_info.ipinfos[i].host_to_ip)
-                        MdnsdDone (ipcam_info.mdnsd, ipcam_info.ipinfos[i].host_to_ip);
-                    if (ipcam_info.ipinfos[i].ip_to_host)
-                        MdnsdDone (ipcam_info.mdnsd, ipcam_info.ipinfos[i].ip_to_host);
-                    ipcam_info.ipinfos[i].host_to_ip  = NULL;
-                    ipcam_info.ipinfos[i].ip_to_host  = NULL;
-                    ipcam_info.ipinfos[i].announce_ip.s_addr = 0;
-                }
+                service_info.host_to_ip  = NULL;
+                service_info.ip_to_host  = NULL;
+                service_info.announce_ip.s_addr = 0;
 
-                ipcam_info.state = MDNSD_ANNOUNCE;
+                service_info.state = MDNSD_ANNOUNCE;
                 announce_stage = 0;
                 tv.tv_sec = 0;
                 tv.tv_usec = 0;
                 break;
 
             case MDNSD_ANNOUNCE:
-printf("--->Announce\n");
-                /* fprintf (stderr, "in ANNOUNCE\n"); */
-                if (announce_stage < 3)
-                {
+                if (announce_stage < 3) {
                     struct timeval cur_tv;
                     long msecs;
+
                     gettimeofday (&cur_tv, NULL);
                     msecs = (cur_tv.tv_sec - tv.tv_sec) * 1000 + cur_tv.tv_usec / 1000 - tv.tv_usec / 1000;
 
-                    if (tv.tv_sec == 0 || msecs > 755)
-                    {
-                        request_service (&ipcam_info, announce_stage);
+                    if ((tv.tv_sec == 0) || (msecs > 755)) {
+                        request_service (&service_info, announce_stage);
                         announce_stage ++;
                         tv = cur_tv;
-                        cur_tv = *MdnsdGetMaxSleepTime (ipcam_info.mdnsd);
+                        cur_tv = *MdnsdGetMaxSleepTime (service_info.mdnsd);
                         polltime = cur_tv.tv_sec * 1000 + cur_tv.tv_usec / 1000;
-                        if (polltime >= 756)
+                        if (polltime >= 756) {
                             polltime = 756;
-                    }
-                    else
-                    {
-                        cur_tv = *MdnsdGetMaxSleepTime (ipcam_info.mdnsd);
+                                               }
+                    } else {
+                        cur_tv = *MdnsdGetMaxSleepTime (service_info.mdnsd);
                         polltime = cur_tv.tv_sec * 1000 + cur_tv.tv_usec / 1000;
-                        if (polltime >= 756 - msecs)
+                        if (polltime >= 756 - msecs) {
                             polltime = 756 - msecs;
+                                               }
                     }
-                }
-                    else
-                {
-                    tv = *MdnsdGetMaxSleepTime (ipcam_info.mdnsd);
+                } else {
+                    tv = *MdnsdGetMaxSleepTime (service_info.mdnsd);
                     polltime = tv.tv_sec * 1000 + tv.tv_usec / 1000;
 
-                    ipcam_info.state = MDNSD_RUN;
+                    service_info.state = MDNSD_RUN;
                 }
                 break;
 
             case MDNSD_RUN:
-printf("--->Run\n");
-                tv = *MdnsdGetMaxSleepTime (ipcam_info.mdnsd);
+                tv = *MdnsdGetMaxSleepTime (service_info.mdnsd);
                 polltime = tv.tv_sec * 1000 + tv.tv_usec / 1000;
                 break;
 
             case MDNSD_SHUTDOWN:
-printf("--->Shutdown\n");
-                MdnsdShutdown (ipcam_info.mdnsd);
+                MdnsdShutdown (service_info.mdnsd);
                 break;
 
             default:
@@ -733,43 +463,39 @@ printf("--->Shutdown\n");
                 break;
         }
 
-        if (fds[1].revents)
-        {
-            while ((bsize = recvfrom (s, buf, MAX_PACKET_LEN, 0,
-                                      (struct sockaddr*) &from, &ssize)) > 0)
+        if (fds[0].revents) {
+            while ((bsize = recvfrom (s, buf, MAX_PACKET_LEN, 0, (struct sockaddr*) &from, &ssize)) > 0)
             {
                 bzero (&msg, sizeof (DNSMESSAGE));
                 DnsParseMsg (&msg, buf);
-                MdnsdInput(ipcam_info.mdnsd, &msg,
-                          from.sin_addr,
-                          from.sin_port);
+                MdnsdInput(service_info.mdnsd, &msg,
+                           from.sin_addr,
+                           from.sin_port);
             }
-            if (bsize < 0 && errno != EAGAIN)
-            {
+
+            if (bsize < 0 && errno != EAGAIN) {
                 fprintf (stderr, "can't read from socket: %s\n", strerror (errno));
             }
         }
 
-        while (MdnsdOutput (ipcam_info.mdnsd, &msg, &remote_ip, &port))
-        {
+        while (MdnsdOutput (service_info.mdnsd, &msg, &remote_ip, &port)) {
             bzero (&to, sizeof (to));
             to.sin_family = AF_INET;
             to.sin_port = port;
             to.sin_addr.s_addr = remote_ip.s_addr;
-            if (sendto (s, DnsMsg2Pkt (&msg), DnsMsgLen(&msg),
-                        0, (struct sockaddr *) &to,
-                        sizeof (struct sockaddr_in)) != DnsMsgLen(&msg))
-            {
+
+            if (sendto (s, DnsMsg2Pkt (&msg), DnsMsgLen(&msg), 0, (struct sockaddr *) &to, sizeof (struct sockaddr_in)) != DnsMsgLen(&msg)) {
                 fprintf (stderr, "can't write to socket: %s\n", strerror(errno));
             }
         }
 
-        if (ipcam_info.state == MDNSD_SHUTDOWN)
+        if (service_info.state == MDNSD_SHUTDOWN) {
             break;
+               }
     }
 
-    MdnsdShutdown (ipcam_info.mdnsd);
-    MdnsdFree (ipcam_info.mdnsd);
+    MdnsdShutdown (service_info.mdnsd);
+    MdnsdFree (service_info.mdnsd);
     return 0;
 }