Debian Codegraph @

src/xmlrpcpp/XmlRpcSource.cpp

raw
1 // ----------------------------------------------------------------------------
2 //
3 // flxmlrpc Copyright (c) 2015 by W1HKJ, Dave Freese <[email protected]>
4 //
5 // XmlRpc++ Copyright (c) 2002-2008 by Chris Morley
6 //
7 // This file is part of fldigi
8 //
9 // flxmlrpc is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU Lesser General Public License as published by
11 // the Free Software Foundation; either version 3 of the License, or
12 // (at your option) any later version.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // ----------------------------------------------------------------------------
17
18 #include <config.h>
19
20 #include "XmlRpcSource.h"
21 #include "XmlRpcUtil.h"
22
23 #if defined(_WINDOWS)
24 # include <stdio.h>
25 # include <winsock2.h>
26 #else
27 extern "C" {
28 # include <unistd.h>
29 # include <stdio.h>
30 }
31 #endif
32
33 #if USE_OPENSSL
34 # include <openssl/crypto.h>
35 # include <openssl/x509.h>
36 # include <openssl/pem.h>
37 # include <openssl/ssl.h>
38 # include <openssl/err.h>
39 #endif
40
41 namespace XmlRpc {
42
43 struct SslProxy
44 {
45 #if USE_OPENSSL
46 SSL_CTX* _ssl_ctx;
47 SSL_METHOD* _ssl_meth;
48 SSL* _ssl_ssl;
49 #endif
50 };
51
52
53 XmlRpcSource::XmlRpcSource(XmlRpcSocket::Socket fd /*= -1*/, bool deleteOnClose /*= false*/)
54 : _fd(fd)
55 , _deleteOnClose(deleteOnClose)
56 , _keepOpen(false)
57 , _sslEnabled(false)
58 , _ssl(0)
59 {
60 }
61
62 XmlRpcSource::~XmlRpcSource()
63 {
64 delete _ssl;
65 }
66
67
68 void
69 XmlRpcSource::setSslEnabled(bool b /*=true*/)
70 {
71 #if USE_OPENSSL
72 _sslEnabled = b;
73 #endif
74 }
75
76
77 bool
78 XmlRpcSource::doConnect()
79 {
80 #if USE_OPENSSL
81 // Perform SSL if needed
82 if (_sslEnabled)
83 {
84 _ssl = new SslProxy;
85
86 SSLeay_add_ssl_algorithms();
87 _ssl->_ssl_meth = SSLv23_client_method();
88 SSL_load_error_strings();
89 _ssl->_ssl_ctx = SSL_CTX_new(_ssl->_ssl_meth);
90 _ssl->_ssl_ssl = SSL_new(_ssl->_ssl_ctx);
91 SSL_set_fd(_ssl->_ssl_ssl, _fd);
92
93 return SSL_connect(_ssl->_ssl_ssl) == 1;
94 }
95 #endif
96 return true;
97 }
98
99 // Read available text from the specified socket. Returns false on error.
100 bool
101 XmlRpcSource::nbRead(std::string& s, bool *eof)
102 {
103 const int READ_SIZE = 4096; // Number of bytes to attempt to read at a time
104 char readBuf[READ_SIZE];
105
106 bool wouldBlock = false;
107 *eof = false;
108
109 while ( ! wouldBlock && ! *eof)
110 {
111 int n;
112
113 #if USE_OPENSSL
114 // Perform SSL if needed
115 if (_ssl && _ssl->_ssl_ssl)
116 {
117 n = SSL_read(_ssl->_ssl_ssl, readBuf, READ_SIZE-1);
118 }
119 else
120 #endif
121 #if defined(_WINDOWS)
122 n = recv(_fd, readBuf, READ_SIZE-1, 0);
123 #else
124 n = read(_fd, readBuf, READ_SIZE-1);
125 #endif
126 XmlRpcUtil::log(5, "XmlRpcSocket::nbRead: read/recv returned %d.", n);
127
128 if (n > 0) {
129 readBuf[n] = 0;
130 s.append(readBuf, n);
131 } else if (n == 0) {
132 *eof = true;
133 } else if (XmlRpcSocket::nonFatalError()) {
134 wouldBlock = true;
135 } else {
136 return false; // Error
137 }
138 }
139 return true;
140 }
141
142
143 // Write text to the socket. Returns false on error.
144 bool
145 XmlRpcSource::nbWrite(std::string const& s, int *bytesSoFar)
146 {
147 int nToWrite = int(s.length()) - *bytesSoFar;
148 const char *sp = s.c_str() + *bytesSoFar;
149 bool wouldBlock = false;
150
151 while ( nToWrite > 0 && ! wouldBlock )
152 {
153 int n;
154 #if USE_OPENSSL
155 // Perform SSL if needed
156 if (_ssl && _ssl->_ssl_ssl)
157 {
158 n = SSL_write(_ssl->_ssl_ssl, sp, nToWrite);
159 }
160 else
161 #endif
162 #if defined(_WINDOWS)
163 n = send(_fd, sp, nToWrite, 0);
164 #else
165 n = write(_fd, sp, nToWrite);
166 #endif
167
168 XmlRpcUtil::log(5, "XmlRpcSocket::nbWrite: send/write returned %d.", n);
169
170 if (n > 0)
171 {
172 sp += n;
173 *bytesSoFar += n;
174 nToWrite -= n;
175 }
176 else if (XmlRpcSocket::nonFatalError())
177 {
178 wouldBlock = true;
179 }
180 else
181 {
182 return false; // Error
183 }
184 }
185 return true;
186 }
187
188 void
189 XmlRpcSource::close()
190 {
191 #if USE_OPENSSL
192 if (_ssl && _ssl->_ssl_ssl)
193 {
194 SSL_shutdown(_ssl->_ssl_ssl);
195
196 // Should close be called here ? ...
197
198 SSL_free(_ssl->_ssl_ssl);
199 SSL_CTX_free(_ssl->_ssl_ctx);
200
201 delete _ssl;
202 _ssl = 0;
203 }
204 #endif
205 if ( (int)_fd != -1)
206 {
207 XmlRpcUtil::log(2,"XmlRpcSource::close: closing socket %d.", _fd);
208 XmlRpcSocket::close(_fd);
209 _fd = -1;
210 }
211 if (_deleteOnClose)
212 {
213 XmlRpcUtil::log(2,"XmlRpcSource::close: deleting this");
214 _deleteOnClose = false;
215 delete this;
216 }
217 }
218
219 } // namespace XmlRpc