Allow disabling DIAL through SageTV properties

Change-Id: Id94ef4977b6862c0c2dbfc7c10a56d667947bd20
diff --git a/src/server/dial_server.c b/src/server/dial_server.c
index 8929594..b60da30 100644
--- a/src/server/dial_server.c
+++ b/src/server/dial_server.c
@@ -204,6 +204,27 @@
     ds_unlock(ds);
 }
 
+// Add logic to test if we should drop DIAL requests
+#define SAGE_PROPERTIES_PATH "/rw/sage/SageClient.properties"
+#define DIAL_DISABLED_PROPERTY "allow_dial=false"
+
+int dial_allowed() {
+    char property[1024];
+    FILE *sageProperties;
+    if (!(sageProperties = fopen(SAGE_PROPERTIES_PATH,"r"))) {
+        return 1;
+    }
+    while (!feof(sageProperties)) {
+        fgets(property, sizeof(property), sageProperties);
+        if (strncmp(property, DIAL_DISABLED_PROPERTY, strlen(DIAL_DISABLED_PROPERTY))==0) {
+            fclose(sageProperties);
+            return 0;
+        }
+    }
+    fclose(sageProperties);
+    return 1;
+}
+
 #define APPS_URI "/apps/"
 #define RUN_URI "/run"
 
@@ -211,6 +232,9 @@
                              struct mg_connection *conn,
                              const struct mg_request_info *request_info) {
     if (event == MG_NEW_REQUEST) {
+        // If DIAL is disabled, drop the request
+        if (!dial_allowed())
+          return "done";
         // URL ends with run
         if (!strncmp(request_info->uri + strlen(request_info->uri)-4, RUN_URI, strlen(RUN_URI))) {
             char app_name[256] = {0,}; // assuming the application name is not over 256 chars.
diff --git a/src/server/quick_ssdp.c b/src/server/quick_ssdp.c
index fa1d84a..91e46a0 100644
--- a/src/server/quick_ssdp.c
+++ b/src/server/quick_ssdp.c
@@ -84,10 +84,14 @@
 static char model_name[256];
 static struct mg_context *ctx;
 
+extern int dial_allowed();
+
 static void *request_handler(enum mg_event event,
                              struct mg_connection *conn,
                              const struct mg_request_info *request_info) {
   if (event == MG_NEW_REQUEST) {
+    if (!dial_allowed())
+          return "done";
     if (!strcmp(request_info->uri, "/dd.xml") &&
         !strcmp(request_info->request_method, "GET")) {
       mg_printf(conn, "HTTP/1.1 200 OK\r\n"
@@ -190,11 +194,13 @@
 #endif
       continue;
     }
-    printf("Sending SSDP reply to %s:%d\n",
-           inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
-    if (-1 == sendto(s, send_buf, send_size, 0, (struct sockaddr *)&saddr, addrlen)) {
-      perror("sendto");
-      continue;
+    if (dial_allowed()) {
+        printf("Sending SSDP reply to %s:%d\n",
+              inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
+        if (-1 == sendto(s, send_buf, send_size, 0, (struct sockaddr *)&saddr, addrlen)) {
+          perror("sendto");
+          continue;
+        }
     }
   }
 }