15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2011 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/dns_util.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <cstring>
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace net {
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Based on DJB's public domain code.
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool DNSDomainFromDot(const base::StringPiece& dotted, std::string* out) {
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const char* buf = dotted.data();
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  unsigned n = dotted.size();
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  char label[63];
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  unsigned int labellen = 0; /* <= sizeof label */
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  char name[255];
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  unsigned int namelen = 0; /* <= sizeof name */
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  char ch;
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (;;) {
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (!n)
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ch = *buf++;
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    --n;
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (ch == '.') {
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (labellen) {
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        if (namelen + labellen + 1 > sizeof name)
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          return false;
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        name[namelen++] = labellen;
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        memcpy(name + namelen, label, labellen);
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        namelen += labellen;
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        labellen = 0;
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      continue;
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (labellen >= sizeof label)
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return false;
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    label[labellen++] = ch;
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (labellen) {
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (namelen + labellen + 1 > sizeof name)
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return false;
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    name[namelen++] = labellen;
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    memcpy(name + namelen, label, labellen);
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    namelen += labellen;
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    labellen = 0;
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (namelen + 1 > sizeof name)
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
5390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (namelen == 0) // Empty names e.g. "", "." are not valid.
5490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return false;
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  name[namelen++] = 0;  // This is the root label (of length 0).
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  *out = std::string(name, namelen);
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return true;
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)std::string DNSDomainToString(const base::StringPiece& domain) {
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string ret;
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (unsigned i = 0; i < domain.size() && domain[i]; i += domain[i] + 1) {
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if CHAR_MIN < 0
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (domain[i] < 0)
67c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return std::string();
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (domain[i] > 63)
70c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return std::string();
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (i)
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      ret += ".";
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (static_cast<unsigned>(domain[i]) + i + 1 > domain.size())
76c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return std::string();
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    domain.substr(i + 1, domain[i]).AppendToString(&ret);
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return ret;
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)std::string TrimEndingDot(const base::StringPiece& host) {
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::StringPiece host_trimmed = host;
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  size_t len = host_trimmed.length();
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (len > 1 && host_trimmed[len - 1] == '.') {
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    host_trimmed.remove_suffix(1);
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return host_trimmed.as_string();
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace net
93