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 | |
| 22 | #include <stdlib.h> |
| 23 | #include <rdr/Exception.h> |
| 24 | #include <rfb/util.h> |
| 25 | |
| 26 | namespace rfb { |
| 27 | |
| 28 | static void getHostAndPort(const char* hi, char** host, int* port, int basePort=5900) { |
| 29 | CharArray portBuf; |
| 30 | CharArray hostBuf; |
| 31 | if (hi[0] == '[') { |
| 32 | if (!strSplit(&hi[1], ']', &hostBuf.buf, &portBuf.buf)) |
| 33 | throw rdr::Exception("unmatched [ in host"); |
| 34 | } else { |
Adam Tkac | 97abe8a | 2009-09-04 10:16:58 +0000 | [diff] [blame^] | 35 | portBuf.buf = safe_strdup(hi); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 36 | } |
| 37 | if (strSplit(portBuf.buf, ':', hostBuf.buf ? 0 : &hostBuf.buf, &portBuf.buf)) { |
| 38 | if (portBuf.buf[0] == ':') { |
| 39 | *port = atoi(&portBuf.buf[1]); |
| 40 | } else { |
| 41 | *port = atoi(portBuf.buf); |
| 42 | if (*port < 100) *port += basePort; |
| 43 | } |
| 44 | } else { |
| 45 | *port = basePort; |
| 46 | } |
| 47 | if (strlen(hostBuf.buf) == 0) |
Adam Tkac | 97abe8a | 2009-09-04 10:16:58 +0000 | [diff] [blame^] | 48 | *host = safe_strdup("localhost"); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 49 | else |
| 50 | *host = hostBuf.takeBuf(); |
| 51 | } |
| 52 | |
| 53 | }; |
| 54 | |
| 55 | #endif // __RFB_HOSTNAME_H__ |