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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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, char** host, int* port, int basePort=5900) {
  37. const char* hostStart;
  38. const char* hostEnd;
  39. const char* portStart;
  40. if (hi == NULL)
  41. throw rdr::Exception("NULL host specified");
  42. // Trim leading whitespace
  43. while(isspace(*hi))
  44. hi++;
  45. assert(host);
  46. assert(port);
  47. if (hi[0] == '[') {
  48. hostStart = &hi[1];
  49. hostEnd = strchr(hostStart, ']');
  50. if (hostEnd == NULL)
  51. throw rdr::Exception("unmatched [ in host");
  52. portStart = hostEnd + 1;
  53. if (isAllSpace(portStart))
  54. portStart = NULL;
  55. } else {
  56. hostStart = &hi[0];
  57. hostEnd = strrchr(hostStart, ':');
  58. if (hostEnd == NULL) {
  59. hostEnd = hostStart + strlen(hostStart);
  60. portStart = NULL;
  61. } else {
  62. if ((hostEnd > hostStart) && (hostEnd[-1] == ':'))
  63. hostEnd--;
  64. portStart = strchr(hostStart, ':');
  65. if (portStart != hostEnd) {
  66. // We found more : in the host. This is probably an IPv6 address
  67. hostEnd = hostStart + strlen(hostStart);
  68. portStart = NULL;
  69. }
  70. }
  71. }
  72. // Back up past trailing space
  73. while(isspace(*(hostEnd - 1)) && hostEnd > hostStart)
  74. hostEnd--;
  75. if (hostStart == hostEnd)
  76. *host = strDup("localhost");
  77. else {
  78. size_t len;
  79. len = hostEnd - hostStart + 1;
  80. *host = new char[len];
  81. strncpy(*host, hostStart, len-1);
  82. (*host)[len-1] = '\0';
  83. }
  84. if (portStart == NULL)
  85. *port = basePort;
  86. else {
  87. char* end;
  88. if (portStart[0] != ':')
  89. throw rdr::Exception("invalid port specified");
  90. if (portStart[1] != ':')
  91. *port = strtol(portStart + 1, &end, 10);
  92. else
  93. *port = strtol(portStart + 2, &end, 10);
  94. if (*end != '\0' && ! isAllSpace(end))
  95. throw rdr::Exception("invalid port specified");
  96. if ((portStart[1] != ':') && (*port < 100))
  97. *port += basePort;
  98. }
  99. }
  100. };
  101. #endif // __RFB_HOSTNAME_H__