Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 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 | |
| 19 | #ifndef __RFB_HOSTNAME_H__ |
| 20 | #define __RFB_HOSTNAME_H__ |
| 21 | |
Pierre Ossman | 168b92c | 2016-06-28 16:24:12 +0200 | [diff] [blame] | 22 | #include <assert.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 23 | #include <stdlib.h> |
| 24 | #include <rdr/Exception.h> |
| 25 | #include <rfb/util.h> |
| 26 | |
| 27 | namespace rfb { |
| 28 | |
| 29 | static void getHostAndPort(const char* hi, char** host, int* port, int basePort=5900) { |
Pierre Ossman | 168b92c | 2016-06-28 16:24:12 +0200 | [diff] [blame] | 30 | const char* hostStart; |
| 31 | const char* hostEnd; |
| 32 | const char* portStart; |
| 33 | |
Pierre Ossman | 3270a59 | 2011-03-02 12:44:46 +0000 | [diff] [blame] | 34 | if (hi == NULL) |
| 35 | throw rdr::Exception("NULL host specified"); |
Pierre Ossman | 168b92c | 2016-06-28 16:24:12 +0200 | [diff] [blame] | 36 | |
| 37 | assert(host); |
| 38 | assert(port); |
| 39 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 40 | if (hi[0] == '[') { |
Pierre Ossman | 168b92c | 2016-06-28 16:24:12 +0200 | [diff] [blame] | 41 | hostStart = &hi[1]; |
| 42 | hostEnd = strchr(hostStart, ']'); |
| 43 | if (hostEnd == NULL) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 44 | throw rdr::Exception("unmatched [ in host"); |
Pierre Ossman | 168b92c | 2016-06-28 16:24:12 +0200 | [diff] [blame] | 45 | |
| 46 | portStart = hostEnd + 1; |
| 47 | if (*portStart == '\0') |
| 48 | portStart = NULL; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 49 | } else { |
Pierre Ossman | 168b92c | 2016-06-28 16:24:12 +0200 | [diff] [blame] | 50 | hostStart = &hi[0]; |
| 51 | hostEnd = strrchr(hostStart, ':'); |
| 52 | |
| 53 | if (hostEnd == NULL) { |
| 54 | hostEnd = hostStart + strlen(hostStart); |
| 55 | portStart = NULL; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 56 | } else { |
Pierre Ossman | 168b92c | 2016-06-28 16:24:12 +0200 | [diff] [blame] | 57 | 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 Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 65 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 66 | } |
Pierre Ossman | 168b92c | 2016-06-28 16:24:12 +0200 | [diff] [blame] | 67 | |
| 68 | if (hostStart == hostEnd) |
Adam Tkac | d36b626 | 2009-09-04 10:57:20 +0000 | [diff] [blame] | 69 | *host = strDup("localhost"); |
Pierre Ossman | 168b92c | 2016-06-28 16:24:12 +0200 | [diff] [blame] | 70 | 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] != ':') |
| 87 | portStart += 1; |
| 88 | else { |
| 89 | portStart += 2; |
| 90 | basePort = 0; |
| 91 | } |
| 92 | |
| 93 | *port = strtol(portStart, &end, 10); |
| 94 | if (*end != '\0') |
| 95 | throw rdr::Exception("invalid port specified"); |
| 96 | |
| 97 | *port += basePort; |
| 98 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | }; |
| 102 | |
| 103 | #endif // __RFB_HOSTNAME_H__ |