You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Hostname.h 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. *
  3. * This is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This software is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  16. * USA.
  17. */
  18. #ifndef __RFB_HOSTNAME_H__
  19. #define __RFB_HOSTNAME_H__
  20. #include <assert.h>
  21. #include <ctype.h>
  22. #include <stdlib.h>
  23. #include <rdr/Exception.h>
  24. #include <rfb/util.h>
  25. namespace rfb {
  26. static bool isAllSpace(const char *string) {
  27. if (string == NULL)
  28. return false;
  29. while(*string != '\0') {
  30. if (! isspace(*string))
  31. return false;
  32. string++;
  33. }
  34. return true;
  35. }
  36. static void getHostAndPort(const char* hi, std::string* host,
  37. int* port, int basePort=5900)
  38. {
  39. const char* hostStart;
  40. const char* hostEnd;
  41. const char* portStart;
  42. if (hi == NULL)
  43. throw rdr::Exception("NULL host specified");
  44. // Trim leading whitespace
  45. while(isspace(*hi))
  46. hi++;
  47. assert(host);
  48. assert(port);
  49. if (hi[0] == '[') {
  50. hostStart = &hi[1];
  51. hostEnd = strchr(hostStart, ']');
  52. if (hostEnd == NULL)
  53. throw rdr::Exception("unmatched [ in host");
  54. portStart = hostEnd + 1;
  55. if (isAllSpace(portStart))
  56. portStart = NULL;
  57. } else {
  58. hostStart = &hi[0];
  59. hostEnd = strrchr(hostStart, ':');
  60. if (hostEnd == NULL) {
  61. hostEnd = hostStart + strlen(hostStart);
  62. portStart = NULL;
  63. } else {
  64. if ((hostEnd > hostStart) && (hostEnd[-1] == ':'))
  65. hostEnd--;
  66. portStart = strchr(hostStart, ':');
  67. if (portStart != hostEnd) {
  68. // We found more : in the host. This is probably an IPv6 address
  69. hostEnd = hostStart + strlen(hostStart);
  70. portStart = NULL;
  71. }
  72. }
  73. }
  74. // Back up past trailing space
  75. while(isspace(*(hostEnd - 1)) && hostEnd > hostStart)
  76. hostEnd--;
  77. if (hostStart == hostEnd)
  78. *host = "localhost";
  79. else
  80. *host = std::string(hostStart, hostEnd - hostStart);
  81. if (portStart == NULL)
  82. *port = basePort;
  83. else {
  84. char* end;
  85. if (portStart[0] != ':')
  86. throw rdr::Exception("invalid port specified");
  87. if (portStart[1] != ':')
  88. *port = strtol(portStart + 1, &end, 10);
  89. else
  90. *port = strtol(portStart + 2, &end, 10);
  91. if (*end != '\0' && ! isAllSpace(end))
  92. throw rdr::Exception("invalid port specified");
  93. if ((portStart[1] != ':') && (*port < 100))
  94. *port += basePort;
  95. }
  96. }
  97. };
  98. #endif // __RFB_HOSTNAME_H__