blob: f70347f999958c9f490320ae17056464d39acbd7 [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
22#include <stdlib.h>
23#include <rdr/Exception.h>
24#include <rfb/util.h>
25
26namespace rfb {
27
28 static void getHostAndPort(const char* hi, char** host, int* port, int basePort=5900) {
29 CharArray portBuf;
30 CharArray hostBuf;
Pierre Ossman3270a592011-03-02 12:44:46 +000031 if (hi == NULL)
32 throw rdr::Exception("NULL host specified");
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000033 if (hi[0] == '[') {
34 if (!strSplit(&hi[1], ']', &hostBuf.buf, &portBuf.buf))
35 throw rdr::Exception("unmatched [ in host");
36 } else {
Adam Tkacd36b6262009-09-04 10:57:20 +000037 portBuf.buf = strDup(hi);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000038 }
39 if (strSplit(portBuf.buf, ':', hostBuf.buf ? 0 : &hostBuf.buf, &portBuf.buf)) {
40 if (portBuf.buf[0] == ':') {
41 *port = atoi(&portBuf.buf[1]);
42 } else {
43 *port = atoi(portBuf.buf);
44 if (*port < 100) *port += basePort;
45 }
46 } else {
47 *port = basePort;
48 }
49 if (strlen(hostBuf.buf) == 0)
Adam Tkacd36b6262009-09-04 10:57:20 +000050 *host = strDup("localhost");
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000051 else
52 *host = hostBuf.takeBuf();
53 }
54
55};
56
57#endif // __RFB_HOSTNAME_H__