You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RFBGlue.cc 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. * Copyright 2011-2019 Pierre Ossman for Cendio AB
  3. *
  4. * This is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This software is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this software; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  17. * USA.
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. #include <config.h>
  21. #endif
  22. #include <stdlib.h>
  23. #include <network/TcpSocket.h>
  24. #include <rfb/Configuration.h>
  25. #include <rfb/LogWriter.h>
  26. #include <rfb/Logger_stdio.h>
  27. #include <rfb/Logger_syslog.h>
  28. #include "RFBGlue.h"
  29. using namespace rfb;
  30. // Loggers used by C code must be created here
  31. static LogWriter inputLog("Input");
  32. static LogWriter selectionLog("Selection");
  33. void vncInitRFB(void)
  34. {
  35. rfb::initStdIOLoggers();
  36. rfb::initSyslogLogger();
  37. rfb::LogWriter::setLogParams("*:stderr:30");
  38. rfb::Configuration::enableServerParams();
  39. }
  40. void vncLogError(const char *name, const char *format, ...)
  41. {
  42. LogWriter *vlog;
  43. va_list ap;
  44. vlog = LogWriter::getLogWriter(name);
  45. if (vlog == NULL)
  46. return;
  47. va_start(ap, format);
  48. vlog->verror(format, ap);
  49. va_end(ap);
  50. }
  51. void vncLogStatus(const char *name, const char *format, ...)
  52. {
  53. LogWriter *vlog;
  54. va_list ap;
  55. vlog = LogWriter::getLogWriter(name);
  56. if (vlog == NULL)
  57. return;
  58. va_start(ap, format);
  59. vlog->vstatus(format, ap);
  60. va_end(ap);
  61. }
  62. void vncLogInfo(const char *name, const char *format, ...)
  63. {
  64. LogWriter *vlog;
  65. va_list ap;
  66. vlog = LogWriter::getLogWriter(name);
  67. if (vlog == NULL)
  68. return;
  69. va_start(ap, format);
  70. vlog->vinfo(format, ap);
  71. va_end(ap);
  72. }
  73. void vncLogDebug(const char *name, const char *format, ...)
  74. {
  75. LogWriter *vlog;
  76. va_list ap;
  77. vlog = LogWriter::getLogWriter(name);
  78. if (vlog == NULL)
  79. return;
  80. va_start(ap, format);
  81. vlog->vdebug(format, ap);
  82. va_end(ap);
  83. }
  84. int vncSetParam(const char *name, const char *value)
  85. {
  86. if (value != NULL)
  87. return rfb::Configuration::setParam(name, value);
  88. else {
  89. VoidParameter *param;
  90. param = rfb::Configuration::getParam(name);
  91. if (param == NULL)
  92. return false;
  93. return param->setParam();
  94. }
  95. }
  96. int vncSetParamSimple(const char *nameAndValue)
  97. {
  98. return rfb::Configuration::setParam(nameAndValue);
  99. }
  100. char* vncGetParam(const char *name)
  101. {
  102. VoidParameter *param;
  103. // Hack to avoid exposing password!
  104. if (strcasecmp(name, "Password") == 0)
  105. return NULL;
  106. param = rfb::Configuration::getParam(name);
  107. if (param == NULL)
  108. return NULL;
  109. return strdup(param->getValueStr().c_str());
  110. }
  111. const char* vncGetParamDesc(const char *name)
  112. {
  113. rfb::VoidParameter *param;
  114. param = rfb::Configuration::getParam(name);
  115. if (param == NULL)
  116. return NULL;
  117. return param->getDescription();
  118. }
  119. int vncIsParamBool(const char *name)
  120. {
  121. VoidParameter *param;
  122. BoolParameter *bparam;
  123. param = rfb::Configuration::getParam(name);
  124. if (param == NULL)
  125. return false;
  126. bparam = dynamic_cast<BoolParameter*>(param);
  127. if (bparam == NULL)
  128. return false;
  129. return true;
  130. }
  131. int vncGetParamCount(void)
  132. {
  133. int count;
  134. count = 0;
  135. for (ParameterIterator i; i.param; i.next())
  136. count++;
  137. return count;
  138. }
  139. char *vncGetParamList(void)
  140. {
  141. int len;
  142. char *data, *ptr;
  143. len = 0;
  144. for (ParameterIterator i; i.param; i.next()) {
  145. int l = strlen(i.param->getName());
  146. if (l <= 255)
  147. len += l + 1;
  148. }
  149. data = (char*)malloc(len + 1);
  150. if (data == NULL)
  151. return NULL;
  152. ptr = data;
  153. for (ParameterIterator i; i.param; i.next()) {
  154. int l = strlen(i.param->getName());
  155. if (l <= 255) {
  156. *ptr++ = l;
  157. memcpy(ptr, i.param->getName(), l);
  158. ptr += l;
  159. }
  160. }
  161. *ptr = '\0';
  162. return data;
  163. }
  164. void vncListParams(int width, int nameWidth)
  165. {
  166. rfb::Configuration::listParams(width, nameWidth);
  167. }
  168. int vncGetSocketPort(int fd)
  169. {
  170. return network::getSockPort(fd);
  171. }
  172. int vncIsTCPPortUsed(int port)
  173. {
  174. try {
  175. // Attempt to create TCPListeners on that port.
  176. std::list<network::SocketListener*> dummy;
  177. network::createTcpListeners (&dummy, 0, port);
  178. while (!dummy.empty()) {
  179. delete dummy.back();
  180. dummy.pop_back();
  181. }
  182. } catch (rdr::Exception& e) {
  183. return 1;
  184. }
  185. return 0;
  186. }
  187. char* vncConvertLF(const char* src, size_t bytes)
  188. {
  189. try {
  190. return strDup(convertLF(src, bytes).c_str());
  191. } catch (...) {
  192. return NULL;
  193. }
  194. }
  195. char* vncLatin1ToUTF8(const char* src, size_t bytes)
  196. {
  197. try {
  198. return strDup(latin1ToUTF8(src, bytes).c_str());
  199. } catch (...) {
  200. return NULL;
  201. }
  202. }
  203. char* vncUTF8ToLatin1(const char* src, size_t bytes)
  204. {
  205. try {
  206. return strDup(utf8ToLatin1(src, bytes).c_str());
  207. } catch (...) {
  208. return NULL;
  209. }
  210. }
  211. void vncStrFree(char* str)
  212. {
  213. strFree(str);
  214. }