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 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 <stdlib.h>
  22. #include <rdr/Exception.h>
  23. #include <rfb/util.h>
  24. namespace rfb {
  25. static void getHostAndPort(const char* hi, char** host, int* port, int basePort=5900) {
  26. const char* hostStart;
  27. const char* hostEnd;
  28. const char* portStart;
  29. if (hi == NULL)
  30. throw rdr::Exception("NULL host specified");
  31. assert(host);
  32. assert(port);
  33. if (hi[0] == '[') {
  34. hostStart = &hi[1];
  35. hostEnd = strchr(hostStart, ']');
  36. if (hostEnd == NULL)
  37. throw rdr::Exception("unmatched [ in host");
  38. portStart = hostEnd + 1;
  39. if (*portStart == '\0')
  40. portStart = NULL;
  41. } else {
  42. hostStart = &hi[0];
  43. hostEnd = strrchr(hostStart, ':');
  44. if (hostEnd == NULL) {
  45. hostEnd = hostStart + strlen(hostStart);
  46. portStart = NULL;
  47. } else {
  48. if ((hostEnd > hostStart) && (hostEnd[-1] == ':'))
  49. hostEnd--;
  50. portStart = strchr(hostStart, ':');
  51. if (portStart != hostEnd) {
  52. // We found more : in the host. This is probably an IPv6 address
  53. hostEnd = hostStart + strlen(hostStart);
  54. portStart = NULL;
  55. }
  56. }
  57. }
  58. if (hostStart == hostEnd)
  59. *host = strDup("localhost");
  60. else {
  61. size_t len;
  62. len = hostEnd - hostStart + 1;
  63. *host = new char[len];
  64. strncpy(*host, hostStart, len-1);
  65. (*host)[len-1] = '\0';
  66. }
  67. if (portStart == NULL)
  68. *port = basePort;
  69. else {
  70. char* end;
  71. if (portStart[0] != ':')
  72. throw rdr::Exception("invalid port specified");
  73. if (portStart[1] != ':')
  74. *port = strtol(portStart + 1, &end, 10);
  75. else
  76. *port = strtol(portStart + 2, &end, 10);
  77. if (*end != '\0')
  78. throw rdr::Exception("invalid port specified");
  79. if ((portStart[1] != ':') && (*port < 100))
  80. *port += basePort;
  81. }
  82. }
  83. };
  84. #endif // __RFB_HOSTNAME_H__