8286c2cab186acec7a9ea7b26d5685d61737988a
[public/netxms.git] / src / server / core / smclp.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** Copyright (C) 2003-2013 Victor Kirhenshtein
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 **
19 ** File: smclp.cpp
20 **
21 **/
22
23 #include "nxcore.h"
24
25 /**
26 * Constructor
27 */
28 SMCLP_Connection::SMCLP_Connection(UINT32 ip, WORD port)
29 {
30 m_ip = ip;
31 m_port = port;
32 m_timeout = 5000;
33 m_conn = NULL;
34 }
35
36 /**
37 * Destructor
38 */
39 SMCLP_Connection::~SMCLP_Connection()
40 {
41 disconnect();
42 }
43
44 /**
45 * Connect to target
46 */
47 bool SMCLP_Connection::connect(const TCHAR *login, const TCHAR *password)
48 {
49 bool success = false;
50
51 if (m_conn != NULL)
52 {
53 delete m_conn;
54 }
55
56 m_conn = new TelnetConnection();
57 if (m_conn->connect(m_ip, m_port, m_timeout))
58 {
59 #ifdef UNICODE
60 char *_login = UTF8StringFromWideString(login);
61 char *_password = UTF8StringFromWideString(password);
62 #else
63 const char *_login = login;
64 const char *_password = password;
65 #endif
66
67 if (m_conn->waitForText(":", m_timeout))
68 {
69 m_conn->writeLine(_login);
70 if (m_conn->waitForText(":", m_timeout))
71 {
72 m_conn->writeLine(_password);
73 if (m_conn->waitForText("->", m_timeout))
74 {
75 success = true;
76 }
77 }
78 }
79
80 #ifdef UNICODE
81 safe_free(_login);
82 safe_free(_password);
83 #endif
84 }
85
86 return success;
87 }
88
89 /**
90 * Disconnect from target
91 */
92 void SMCLP_Connection::disconnect()
93 {
94 if (m_conn != NULL)
95 {
96 m_conn->writeLine("quit");
97 m_conn->disconnect();
98 delete_and_null(m_conn);
99 }
100 }
101
102 /**
103 * Get parameter from target
104 */
105 TCHAR *SMCLP_Connection::get(const TCHAR *path, const TCHAR *parameter)
106 {
107 TCHAR *ret = NULL;
108 #ifdef UNICODE
109 char *_path = UTF8StringFromWideString(path);
110 #else
111 const char *_path = path;
112 #endif
113 char buffer[1024];
114 snprintf(buffer, 1024, "show -o format=text %s", _path);
115 m_conn->writeLine(buffer);
116 #ifdef UNICODE
117 free(_path);
118 #endif
119
120 while (m_conn->readLine(buffer, 1024, m_timeout / 10) > 0)
121 {
122 if ((strstr(buffer, "->") != NULL) && (strstr(buffer, "show -o format=text") == NULL))
123 {
124 break;
125 }
126
127 #ifdef UNICODE
128 TCHAR *_buffer = WideStringFromUTF8String(buffer);
129 #else
130 char *_buffer = buffer;
131 #endif
132
133 TCHAR *ptr = _tcschr(_buffer, _T('='));
134 if (ptr != NULL)
135 {
136 *ptr = 0;
137 ptr++;
138 StrStrip(_buffer);
139 StrStrip(ptr);
140 if (!_tcsicmp(_buffer, parameter))
141 {
142 ret = _tcsdup(ptr);
143 break;
144 }
145 }
146
147 #ifdef UNICODE
148 free(_buffer);
149 #endif
150 }
151
152 return ret;
153 }
154
155 /**
156 * Check connection
157 */
158 bool SMCLP_Connection::checkConnection()
159 {
160 if (!m_conn->writeLine("cd /"))
161 return false;
162 return m_conn->waitForText("->", m_timeout);
163 }