blob: bd68dc0210119adb71a0eb0180403fdf00126d17 [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* 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
19#ifndef __RFB_HOSTNAME_H__
20#define __RFB_HOSTNAME_H__
21
Pierre Ossman168b92c2016-06-28 16:24:12 +020022#include <assert.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000023#include <stdlib.h>
24#include <rdr/Exception.h>
25#include <rfb/util.h>
26
27namespace rfb {
28
29 static void getHostAndPort(const char* hi, char** host, int* port, int basePort=5900) {
Pierre Ossman168b92c2016-06-28 16:24:12 +020030 const char* hostStart;
31 const char* hostEnd;
32 const char* portStart;
33
Pierre Ossman3270a592011-03-02 12:44:46 +000034 if (hi == NULL)
35 throw rdr::Exception("NULL host specified");
Pierre Ossman168b92c2016-06-28 16:24:12 +020036
37 assert(host);
38 assert(port);
39
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000040 if (hi[0] == '[') {
Pierre Ossman168b92c2016-06-28 16:24:12 +020041 hostStart = &hi[1];
42 hostEnd = strchr(hostStart, ']');
43 if (hostEnd == NULL)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000044 throw rdr::Exception("unmatched [ in host");
Pierre Ossman168b92c2016-06-28 16:24:12 +020045
46 portStart = hostEnd + 1;
47 if (*portStart == '\0')
48 portStart = NULL;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000049 } else {
Pierre Ossman168b92c2016-06-28 16:24:12 +020050 hostStart = &hi[0];
51 hostEnd = strrchr(hostStart, ':');
52
53 if (hostEnd == NULL) {
54 hostEnd = hostStart + strlen(hostStart);
55 portStart = NULL;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000056 } else {
Pierre Ossman168b92c2016-06-28 16:24:12 +020057 if ((hostEnd > hostStart) && (hostEnd[-1] == ':'))
58 hostEnd--;
59 portStart = strchr(hostStart, ':');
60 if (portStart != hostEnd) {
61 // We found more : in the host. This is probably an IPv6 address
62 hostEnd = hostStart + strlen(hostStart);
63 portStart = NULL;
64 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000065 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000066 }
Pierre Ossman168b92c2016-06-28 16:24:12 +020067
68 if (hostStart == hostEnd)
Adam Tkacd36b6262009-09-04 10:57:20 +000069 *host = strDup("localhost");
Pierre Ossman168b92c2016-06-28 16:24:12 +020070 else {
71 size_t len;
72 len = hostEnd - hostStart + 1;
73 *host = new char[len];
74 strncpy(*host, hostStart, len-1);
75 (*host)[len-1] = '\0';
76 }
77
78 if (portStart == NULL)
79 *port = basePort;
80 else {
81 char* end;
82
83 if (portStart[0] != ':')
84 throw rdr::Exception("invalid port specified");
85
86 if (portStart[1] != ':')
Pierre Ossman7caaea12016-07-11 09:44:14 +020087 *port = strtol(portStart + 1, &end, 10);
88 else
89 *port = strtol(portStart + 2, &end, 10);
Pierre Ossman168b92c2016-06-28 16:24:12 +020090 if (*end != '\0')
91 throw rdr::Exception("invalid port specified");
92
Pierre Ossman7caaea12016-07-11 09:44:14 +020093 if ((portStart[1] != ':') && (*port < 100))
94 *port += basePort;
Pierre Ossman168b92c2016-06-28 16:24:12 +020095 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000096 }
97
98};
99
100#endif // __RFB_HOSTNAME_H__