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 4.8KB

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