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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. rfb::VoidParameter *param;
  103. char *value;
  104. char *ret;
  105. // Hack to avoid exposing password!
  106. if (strcasecmp(name, "Password") == 0)
  107. return NULL;
  108. param = rfb::Configuration::getParam(name);
  109. if (param == NULL)
  110. return NULL;
  111. value = param->getValueStr();
  112. if (value == NULL)
  113. return NULL;
  114. ret = strdup(value);
  115. delete [] value;
  116. return ret;
  117. }
  118. const char* vncGetParamDesc(const char *name)
  119. {
  120. rfb::VoidParameter *param;
  121. param = rfb::Configuration::getParam(name);
  122. if (param == NULL)
  123. return NULL;
  124. return param->getDescription();
  125. }
  126. int vncIsParamBool(const char *name)
  127. {
  128. VoidParameter *param;
  129. BoolParameter *bparam;
  130. param = rfb::Configuration::getParam(name);
  131. if (param == NULL)
  132. return false;
  133. bparam = dynamic_cast<BoolParameter*>(param);
  134. if (bparam == NULL)
  135. return false;
  136. return true;
  137. }
  138. int vncGetParamCount(void)
  139. {
  140. int count;
  141. count = 0;
  142. for (ParameterIterator i; i.param; i.next())
  143. count++;
  144. return count;
  145. }
  146. char *vncGetParamList(void)
  147. {
  148. int len;
  149. char *data, *ptr;
  150. len = 0;
  151. for (ParameterIterator i; i.param; i.next()) {
  152. int l = strlen(i.param->getName());
  153. if (l <= 255)
  154. len += l + 1;
  155. }
  156. data = (char*)malloc(len + 1);
  157. if (data == NULL)
  158. return NULL;
  159. ptr = data;
  160. for (ParameterIterator i; i.param; i.next()) {
  161. int l = strlen(i.param->getName());
  162. if (l <= 255) {
  163. *ptr++ = l;
  164. memcpy(ptr, i.param->getName(), l);
  165. ptr += l;
  166. }
  167. }
  168. *ptr = '\0';
  169. return data;
  170. }
  171. void vncListParams(int width, int nameWidth)
  172. {
  173. rfb::Configuration::listParams(width, nameWidth);
  174. }
  175. int vncGetSocketPort(int fd)
  176. {
  177. return network::getSockPort(fd);
  178. }
  179. int vncIsTCPPortUsed(int port)
  180. {
  181. try {
  182. // Attempt to create TCPListeners on that port.
  183. std::list<network::SocketListener*> dummy;
  184. network::createTcpListeners (&dummy, 0, port);
  185. while (!dummy.empty()) {
  186. delete dummy.back();
  187. dummy.pop_back();
  188. }
  189. } catch (rdr::Exception& e) {
  190. return 1;
  191. }
  192. return 0;
  193. }
  194. char* vncConvertLF(const char* src, size_t bytes)
  195. {
  196. try {
  197. return convertLF(src, bytes);
  198. } catch (...) {
  199. return NULL;
  200. }
  201. }
  202. char* vncLatin1ToUTF8(const char* src, size_t bytes)
  203. {
  204. try {
  205. return latin1ToUTF8(src, bytes);
  206. } catch (...) {
  207. return NULL;
  208. }
  209. }
  210. char* vncUTF8ToLatin1(const char* src, size_t bytes)
  211. {
  212. try {
  213. return utf8ToLatin1(src, bytes);
  214. } catch (...) {
  215. return NULL;
  216. }
  217. }
  218. void vncStrFree(char* str)
  219. {
  220. strFree(str);
  221. }