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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 <rfb/util.h>
  29. #include "RFBGlue.h"
  30. using namespace rfb;
  31. // Loggers used by C code must be created here
  32. static LogWriter inputLog("Input");
  33. static LogWriter selectionLog("Selection");
  34. void vncInitRFB(void)
  35. {
  36. rfb::initStdIOLoggers();
  37. rfb::initSyslogLogger();
  38. rfb::LogWriter::setLogParams("*:stderr:30");
  39. rfb::Configuration::enableServerParams();
  40. }
  41. void vncLogError(const char *name, const char *format, ...)
  42. {
  43. LogWriter *vlog;
  44. va_list ap;
  45. vlog = LogWriter::getLogWriter(name);
  46. if (vlog == NULL)
  47. return;
  48. va_start(ap, format);
  49. vlog->verror(format, ap);
  50. va_end(ap);
  51. }
  52. void vncLogStatus(const char *name, const char *format, ...)
  53. {
  54. LogWriter *vlog;
  55. va_list ap;
  56. vlog = LogWriter::getLogWriter(name);
  57. if (vlog == NULL)
  58. return;
  59. va_start(ap, format);
  60. vlog->vstatus(format, ap);
  61. va_end(ap);
  62. }
  63. void vncLogInfo(const char *name, const char *format, ...)
  64. {
  65. LogWriter *vlog;
  66. va_list ap;
  67. vlog = LogWriter::getLogWriter(name);
  68. if (vlog == NULL)
  69. return;
  70. va_start(ap, format);
  71. vlog->vinfo(format, ap);
  72. va_end(ap);
  73. }
  74. void vncLogDebug(const char *name, const char *format, ...)
  75. {
  76. LogWriter *vlog;
  77. va_list ap;
  78. vlog = LogWriter::getLogWriter(name);
  79. if (vlog == NULL)
  80. return;
  81. va_start(ap, format);
  82. vlog->vdebug(format, ap);
  83. va_end(ap);
  84. }
  85. int vncSetParam(const char *name, const char *value)
  86. {
  87. if (value != NULL)
  88. return rfb::Configuration::setParam(name, value);
  89. else {
  90. VoidParameter *param;
  91. param = rfb::Configuration::getParam(name);
  92. if (param == NULL)
  93. return false;
  94. return param->setParam();
  95. }
  96. }
  97. int vncSetParamSimple(const char *nameAndValue)
  98. {
  99. return rfb::Configuration::setParam(nameAndValue);
  100. }
  101. char* vncGetParam(const char *name)
  102. {
  103. VoidParameter *param;
  104. // Hack to avoid exposing password!
  105. if (strcasecmp(name, "Password") == 0)
  106. return NULL;
  107. param = rfb::Configuration::getParam(name);
  108. if (param == NULL)
  109. return NULL;
  110. return strdup(param->getValueStr().c_str());
  111. }
  112. const char* vncGetParamDesc(const char *name)
  113. {
  114. rfb::VoidParameter *param;
  115. param = rfb::Configuration::getParam(name);
  116. if (param == NULL)
  117. return NULL;
  118. return param->getDescription();
  119. }
  120. int vncIsParamBool(const char *name)
  121. {
  122. VoidParameter *param;
  123. BoolParameter *bparam;
  124. param = rfb::Configuration::getParam(name);
  125. if (param == NULL)
  126. return false;
  127. bparam = dynamic_cast<BoolParameter*>(param);
  128. if (bparam == NULL)
  129. return false;
  130. return true;
  131. }
  132. int vncGetParamCount(void)
  133. {
  134. int count;
  135. count = 0;
  136. for (ParameterIterator i; i.param; i.next())
  137. count++;
  138. return count;
  139. }
  140. char *vncGetParamList(void)
  141. {
  142. int len;
  143. char *data, *ptr;
  144. len = 0;
  145. for (ParameterIterator i; i.param; i.next()) {
  146. int l = strlen(i.param->getName());
  147. if (l <= 255)
  148. len += l + 1;
  149. }
  150. data = (char*)malloc(len + 1);
  151. if (data == NULL)
  152. return NULL;
  153. ptr = data;
  154. for (ParameterIterator i; i.param; i.next()) {
  155. int l = strlen(i.param->getName());
  156. if (l <= 255) {
  157. *ptr++ = l;
  158. memcpy(ptr, i.param->getName(), l);
  159. ptr += l;
  160. }
  161. }
  162. *ptr = '\0';
  163. return data;
  164. }
  165. void vncListParams(int width, int nameWidth)
  166. {
  167. rfb::Configuration::listParams(width, nameWidth);
  168. }
  169. int vncGetSocketPort(int fd)
  170. {
  171. return network::getSockPort(fd);
  172. }
  173. int vncIsTCPPortUsed(int port)
  174. {
  175. try {
  176. // Attempt to create TCPListeners on that port.
  177. std::list<network::SocketListener*> dummy;
  178. network::createTcpListeners (&dummy, 0, port);
  179. while (!dummy.empty()) {
  180. delete dummy.back();
  181. dummy.pop_back();
  182. }
  183. } catch (rdr::Exception& e) {
  184. return 1;
  185. }
  186. return 0;
  187. }
  188. char* vncConvertLF(const char* src, size_t bytes)
  189. {
  190. try {
  191. return strDup(convertLF(src, bytes).c_str());
  192. } catch (...) {
  193. return NULL;
  194. }
  195. }
  196. char* vncLatin1ToUTF8(const char* src, size_t bytes)
  197. {
  198. try {
  199. return strDup(latin1ToUTF8(src, bytes).c_str());
  200. } catch (...) {
  201. return NULL;
  202. }
  203. }
  204. char* vncUTF8ToLatin1(const char* src, size_t bytes)
  205. {
  206. try {
  207. return strDup(utf8ToLatin1(src, bytes).c_str());
  208. } catch (...) {
  209. return NULL;
  210. }
  211. }
  212. void vncStrFree(char* str)
  213. {
  214. strFree(str);
  215. }