]> git.karo-electronics.de Git - mdnsd.git/blobdiff - mhttp.c
Add futher files to .gitignore
[mdnsd.git] / mhttp.c
diff --git a/mhttp.c b/mhttp.c
index 24d5e5af0c4e2e5741342f7bbf02faf1b6636702..dab93cbde0257ed201299ac7dfd6ced3e65c0d40 100644 (file)
--- a/mhttp.c
+++ b/mhttp.c
@@ -54,6 +54,7 @@ typedef struct _service_info
     TMdnsdRecord  *srv_to_host;
     TMdnsdRecord  *txt_for_srv;
 
+    TMdnsdRecord  *ptr_to_srvtype;
     TMdnsdRecord  *ptr_to_srv;
 
     TMdnsdState    state;
@@ -68,7 +69,7 @@ char *increment_name (char *name)
 {
     int   id = 1;
     char *pos;
-       char *end = NULL;
+    char *end = NULL;
     char *ret = NULL;
 
     pos = strrchr (name, '-');
@@ -77,9 +78,9 @@ char *increment_name (char *name)
         id = strtol (pos + 1, &end, 10);
         if (*end == '\0') {
             *pos = '\0';
-               } else {
+        } else {
             id = 1;
-               }
+        }
     }
 
     id += 1;
@@ -91,7 +92,7 @@ char *increment_name (char *name)
 
 
 /* conflict handling */
-void handle_conflict (TMdnsdRecord *record, uint8_t *name, int UNUSED(type), void *arg)
+void handle_conflict (TMdnsdRecord *record, char *name, int UNUSED(type), void *arg)
 {
     ServiceInfo *info = (ServiceInfo *) arg;
     char *newname;
@@ -120,16 +121,16 @@ void handle_conflict (TMdnsdRecord *record, uint8_t *name, int UNUSED(type), voi
 
     if (record == info->srv_to_host) {
         info->srv_to_host = NULL;
-       }
+    }
 
     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 */
+    /* The hostname was changed, so go back to probe state */
     info->state = MDNSD_PROBE;
 }
 
@@ -147,8 +148,8 @@ void sighandler (int sig)
 int msock ()
 {
     int    sock_fd;
-       int    flag = 1;
-       int    ittl = 255;
+    int    flag = 1;
+    int    ittl = 255;
     char   ttl = 255;
     struct sockaddr_in in;
     struct ip_mreq mc;
@@ -160,7 +161,7 @@ int msock ()
 
     if ((sock_fd = socket (AF_INET, SOCK_DGRAM, 0)) < 0) {
         return 0;
-       }
+    }
 
     setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, (char*) &flag, sizeof (flag));
     if (bind (sock_fd, (struct sockaddr*) &in, sizeof (in))) {
@@ -184,7 +185,7 @@ int msock ()
 void request_ip_addresses (ServiceInfo *info)
 {
     char revlookup[256];
-       char hostlocal[256];
+    char hostlocal[256];
     struct in_addr ip;
     int  num_ips = 0;
 
@@ -202,7 +203,7 @@ void request_ip_addresses (ServiceInfo *info)
                 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);
+            MdnsdSetRaw (info->mdnsd, info->host_to_ip, (char *) &ip, 4);
 
             if (!info->ip_to_host) {
                 info->ip_to_host  = MdnsdAllocUnique(info->mdnsd, revlookup,
@@ -217,10 +218,10 @@ void request_ip_addresses (ServiceInfo *info)
     } 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;
@@ -231,8 +232,8 @@ void request_ip_addresses (ServiceInfo *info)
 void request_service (ServiceInfo *info, int stage)
 {
     uint8_t *packet;
-       char     servlocal[256];
-       char     hostlocal[256];
+    char     servlocal[256];
+    char     hostlocal[256];
     int      len = 0;
 
     sprintf (servlocal, "%s._http._tcp.local.",
@@ -286,11 +287,17 @@ void request_service (ServiceInfo *info, int stage)
             }
             MdnsdSetHost (info->mdnsd, info->ptr_to_srv, servlocal);
 
+                       if (!info->ptr_to_srvtype) {
+                               info->ptr_to_srvtype = MdnsdAllocShared (info->mdnsd, "_services._dns-sd._udp.local.",
+                                                                                                 QTYPE_PTR, 4500);
+                       }
+                       MdnsdSetHost (info->mdnsd, info->ptr_to_srvtype, "_http._tcp.local.");
+
             if (info->ip) {
                 fprintf (stderr, "Announcing \"%s.local\" to %s:%d\n",
                          info->servicename ? info->servicename : info->hostname,
                          info->ip, info->port);
-                       }
+            }
             break;
 
         default:
@@ -305,12 +312,12 @@ int main(int argc, char *argv[])
     uint16_t   port;
     struct timeval tv;
     int        bsize;
-       int        ssize = sizeof(struct sockaddr_in);
+    int        ssize = sizeof(struct sockaddr_in);
     uint8_t    buf[MAX_PACKET_LEN];
     struct sockaddr_in from;
-       struct sockaddr_in to;
+    struct sockaddr_in to;
     int        idx;
-       int        s;
+    int        s;
     struct in_addr remote_ip;
     char      *value;
     int        polltime = 0;
@@ -319,32 +326,34 @@ int main(int argc, char *argv[])
 
     if(argc < 4)
     {
-        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, "usage: mhttp <hostname> <ip> <port> <key1>=<value1> <key2>=<value2> ...\n");
+        fprintf (stderr, "   <hostname>  The hostname which shall be promoted as 'hostname.local'\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");
         return -1;
     }
 
     service_info.mdnsd = MdnsdNew (1, 1000);
 
-       //gethostname (service_info.hostname, HOSTNAMESIZE);
-       sprintf(service_info.hostname, "reinhardt");
+    //gethostname (service_info.hostname, HOSTNAMESIZE);
+    strncpy(service_info.hostname, argv[1], HOSTNAMESIZE-1);
     service_info.hostname[HOSTNAMESIZE-1] = '\0';
+
     if (strchr (service_info.hostname, '.'))
         strchr (service_info.hostname, '.')[0] = '\0';
 
     service_info.servicename = NULL;
 
-    service_info.ip          = strdup(argv[1]);
+    service_info.ip          = strdup(argv[2]);
     service_info.announce_ip.s_addr = inet_addr(service_info.ip);
     service_info.host_to_ip  = NULL;
     service_info.ip_to_host  = NULL;
 
-    service_info.port = atoi(argv[2]);
+    service_info.port = atoi(argv[3]);
 
     service_info.metadata = SHashInit (11);
-    for (idx = 2; idx < argc; idx++) {
+    for (idx = 3; idx < argc; idx++) {
         value = index (argv[idx], '=');
         if (value) {
             value[0] = '\0';
@@ -353,6 +362,7 @@ int main(int argc, char *argv[])
         }
     }
 
+    service_info.ptr_to_srvtype = NULL;
     service_info.ptr_to_srv     = NULL;
     service_info.srv_to_host    = NULL;
     service_info.txt_for_srv    = NULL;
@@ -383,29 +393,34 @@ int main(int argc, char *argv[])
         {
             case MDNSD_PROBE:
 
-                               if (service_info.ptr_to_srv) {
+                if (service_info.ptr_to_srvtype) {
+                    MdnsdDone (service_info.mdnsd, service_info.ptr_to_srvtype);
+                }
+
+                if (service_info.ptr_to_srv) {
                     MdnsdDone (service_info.mdnsd, service_info.ptr_to_srv);
-                               }
+                }
 
-                               if (service_info.srv_to_host) {
+                if (service_info.srv_to_host) {
                     MdnsdDone (service_info.mdnsd, service_info.srv_to_host);
-                               }
+                }
 
                 if (service_info.txt_for_srv) {
                     MdnsdDone (service_info.mdnsd, service_info.txt_for_srv);
-                               }
+                }
 
+                service_info.ptr_to_srvtype = NULL;
                 service_info.ptr_to_srv     = NULL;
                 service_info.srv_to_host    = NULL;
                 service_info.txt_for_srv    = NULL;
 
                 if (service_info.host_to_ip) {
                     MdnsdDone (service_info.mdnsd, service_info.host_to_ip);
-                               }
+                }
 
                 if (service_info.ip_to_host) {
                     MdnsdDone (service_info.mdnsd, service_info.ip_to_host);
-                               }
+                }
 
                 service_info.host_to_ip  = NULL;
                 service_info.ip_to_host  = NULL;
@@ -433,13 +448,13 @@ int main(int argc, char *argv[])
                         polltime = cur_tv.tv_sec * 1000 + cur_tv.tv_usec / 1000;
                         if (polltime >= 756) {
                             polltime = 756;
-                                               }
+                        }
                     } else {
                         cur_tv = *MdnsdGetMaxSleepTime (service_info.mdnsd);
                         polltime = cur_tv.tv_sec * 1000 + cur_tv.tv_usec / 1000;
                         if (polltime >= 756 - msecs) {
                             polltime = 756 - msecs;
-                                               }
+                        }
                     }
                 } else {
                     tv = *MdnsdGetMaxSleepTime (service_info.mdnsd);
@@ -491,7 +506,7 @@ int main(int argc, char *argv[])
 
         if (service_info.state == MDNSD_SHUTDOWN) {
             break;
-               }
+        }
     }
 
     MdnsdShutdown (service_info.mdnsd);