OSDN Git Service

Add support for custom TXT records
authorChristopher Lane <lanechr@google.com>
Wed, 26 Feb 2014 03:08:58 +0000 (19:08 -0800)
committerChristopher Lane <lanechr@google.com>
Mon, 3 Mar 2014 20:40:29 +0000 (12:40 -0800)
This isn't supported higher up in the stack yet, so use command line to test.
E.g. "ndc mdns mdnssd register 4 Kitten _kitten._tcp 4242 sound=meow color=pink"

Change-Id: I261c17465ae677f91a289077b6e363a149c94c3e

MDnsSdListener.cpp

index 4944e18..1430273 100644 (file)
@@ -415,7 +415,7 @@ int MDnsSdListener::Handler::runCommand(SocketClient *cli,
     } else if (strcmp(cmd, "stop-discover") == 0) {
         stop(cli, argc, argv, "discover");
     } else if (strcmp(cmd, "register") == 0) {
-        if (argc != 6) {
+        if (argc < 6) {
             cli->sendMsg(ResponseCode::CommandParameterError,
                     "Invalid number of arguments to mdnssd register", false);
             return 0;
@@ -427,11 +427,29 @@ int MDnsSdListener::Handler::runCommand(SocketClient *cli,
         char *interfaceName = NULL; // will use all
         char *domain = NULL;        // will use default
         char *host = NULL;          // will use default hostname
-        int textLen = 0;
-        void *textRecord = NULL;
-
+        unsigned char txtRecord[2048] = "";
+        unsigned char *ptr = txtRecord;
+        for (int i = 6; i < argc; ++i) {
+          int dataLength = strlen(argv[i]);
+          if (dataLength < 1) {
+            continue;
+          }
+          if (dataLength > 255) {
+            cli->sendMsg(ResponseCode::CommandParameterError,
+                    "TXT record fields must not be longer than 255 characters", false);
+            return 0;
+          }
+          if (ptr + dataLength + 1 > txtRecord + sizeof(txtRecord)) {
+            cli->sendMsg(ResponseCode::CommandParameterError,
+                    "Total length of TXT record must be smaller than 2048 bytes", false);
+            return 0;
+          }
+          *ptr++ = dataLength;
+          strcpy( (char*) ptr, argv[i]);
+          ptr += dataLength;
+        }
         serviceRegister(cli, requestId, interfaceName, serviceName,
-                serviceType, domain, host, port, textLen, textRecord);
+                serviceType, domain, host, port, ptr - txtRecord, txtRecord);
     } else if (strcmp(cmd, "stop-register") == 0) {
         stop(cli, argc, argv, "register");
     } else if (strcmp(cmd, "resolve") == 0) {