domain_registry.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright 2011 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef DOMAIN_REGISTRY_DOMAIN_REGISTRY_H_
  17. #define DOMAIN_REGISTRY_DOMAIN_REGISTRY_H_
  18. #include <stdlib.h>
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. /*
  23. * Call once at program startup to enable domain registry
  24. * search. Calls to GetRegistryLength will crash if this is not
  25. * called.
  26. */
  27. void InitializeDomainRegistry(void);
  28. /*
  29. * Finds the length in bytes of the registrar portion of the host in
  30. * the given hostname. Returns 0 if the hostname is invalid or has no
  31. * host (e.g. a file: URL). Returns 0 if the hostname has multiple
  32. * trailing dots. If no matching rule is found in the effective-TLD
  33. * data, returns 0. Internationalized domain names (IDNs) must be
  34. * converted to punycode first. Non-ASCII hostnames or hostnames
  35. * longer than 127 bytes will return 0. It is an error to pass in an
  36. * IP address (either IPv4 or IPv6) and the return value in this case
  37. * is undefined.
  38. *
  39. * Examples:
  40. * www.google.com -> 3 (com)
  41. * WWW.gOoGlE.cOm -> 3 (com, case insensitive)
  42. * ..google.com -> 3 (com)
  43. * google.com. -> 4 (com)
  44. * a.b.co.uk -> 5 (co.uk)
  45. * a.b.co..uk -> 0 (multiple dots in registry)
  46. * C: -> 0 (no host)
  47. * google.com.. -> 0 (multiple trailing dots)
  48. * bar -> 0 (no subcomponents)
  49. * co.uk -> 5 (co.uk)
  50. * foo.bar -> 0 (not a valid top-level registry)
  51. * foo.臺灣 -> 0 (not converted to punycode)
  52. * foo.xn--nnx388a -> 11 (punycode representation of 臺灣)
  53. * 192.168.0.1 -> ? (IP address, retval undefined)
  54. */
  55. size_t GetRegistryLength(const char* hostname);
  56. /*
  57. * Like GetRegistryLength, but allows unknown registries as well. If
  58. * the hostname is part of a known registry, the return value will be
  59. * identical to that of GetRegistryLength. If the hostname is not part
  60. * of a known registry (e.g. foo.bar) then the return value will
  61. * assume that the rootmost hostname-part is the registry. It is an
  62. * error to pass in an IP address (either IPv4 or IPv6) and the return
  63. * value in this case is undefined.
  64. *
  65. * Examples:
  66. * foo.bar -> 3 (bar)
  67. * bar -> 0 (host is a registry)
  68. * www.google.com -> 3 (com)
  69. * com -> 0 (host is a registry)
  70. * co.uk -> 0 (host is a registry)
  71. * foo.臺灣 -> 0 (not converted to punycode)
  72. * foo.xn--nnx388a -> 11 (punycode representation of 臺灣)
  73. * 192.168.0.1 -> ? (IP address, retval undefined)
  74. */
  75. size_t GetRegistryLengthAllowUnknownRegistries(const char* hostname);
  76. /*
  77. * Override the assertion handler by providing a custom assert handler
  78. * implementation. The assertion handler will be invoked when an
  79. * internal assertion fails. This is usually indicative of a fatal
  80. * error and execution should not be allowed to continue. The default
  81. * implementation logs the assertion information to stderr and invokes
  82. * abort(). The parameter "file" is the filename where the assertion
  83. * was triggered. "line_number" is the line number in that
  84. * file. "cond_str" is the string representation of the assertion that
  85. * failed.
  86. */
  87. typedef void (*DomainRegistryAssertHandler)(
  88. const char* file, int line_number, const char* cond_str);
  89. void SetDomainRegistryAssertHandler(DomainRegistryAssertHandler handler);
  90. #ifdef __cplusplus
  91. } /* extern "C" */
  92. #endif
  93. #endif /* DOMAIN_REGISTRY_DOMAIN_REGISTRY_H_ */