Kinetic C/C++ Client
 All Classes Functions Variables Pages
byte_stream.cc
1 /*
2  * kinetic-cpp-client
3  * Copyright (C) 2014 Seagate Technology.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  *
19  */
20 
21 #include "kinetic/byte_stream.h"
22 
23 #include <errno.h>
24 #include <unistd.h>
25 
26 #include "glog/logging.h"
27 
28 #include "kinetic/incoming_value.h"
29 #include "kinetic/outgoing_value.h"
30 #include "kinetic/reader_writer.h"
31 
32 namespace kinetic {
33 
34 PlainByteStream::PlainByteStream(int fd, IncomingValueFactoryInterface &value_factory)
35  : fd_(fd), value_factory_(value_factory) {}
36 
37 bool PlainByteStream::Read(void *buf, size_t n) {
38  ReaderWriter reader_writer(fd_);
39  int err;
40  return reader_writer.Read(buf, n, &err);
41 }
42 
43 bool PlainByteStream::Write(const void *buf, size_t n) {
44  ReaderWriter reader_writer(fd_);
45  return reader_writer.Write(buf, n);
46 }
47 
48 IncomingValueInterface *PlainByteStream::ReadValue(size_t n) {
49  return value_factory_.NewValue(fd_, n);
50 }
51 
52 bool PlainByteStream::WriteValue(const OutgoingValueInterface &value, int* err) {
53  return value.TransferToSocket(fd_, err);
54 }
55 
56 SslByteStream::SslByteStream(SSL *ssl) : ssl_(ssl) {}
57 
58 SslByteStream::~SslByteStream() {
59 }
60 
61 bool SslByteStream::Read(void *buf, size_t n) {
62  if (n == 0) {
63  // SSL_read with 0 bytes causes openssl to get really upset in mysterious ways
64  return true;
65  }
66 
67  // To be able to move the pointed as data arrives we need to cast it to a complete
68  // c type.
69  uint8_t* byte_buffer = static_cast<uint8_t*>(buf);
70 
71  while (n > 0) {
72  int bytes_read = SSL_read(ssl_, byte_buffer, n);
73 
74  if (bytes_read > 0) {
75  // If SSL_read succeeds it returns the number of bytes read
76  n -= bytes_read;
77  byte_buffer += bytes_read;
78  } else {
79  // Return values of 0 or <0 indicate an error in SSL_read
80  LOG(WARNING) << "Failed to read " << n << " bytes over SSL connection";
81  return false;
82  }
83  }
84 
85  return true;
86 }
87 
88 bool SslByteStream::Write(const void *buf, size_t n) {
89  if (n == 0) {
90  // It's not clear whether SSL_write can handle a write of 0 bytes
91  return true;
92  }
93 
94  // To be able to move the pointed as data arrives we need to cast it to a complete
95  // c type.
96  const uint8_t* byte_buffer = static_cast<const uint8_t*>(buf);
97 
98  while (n > 0) {
99  int bytes_written = SSL_write(ssl_, byte_buffer, n);
100 
101  if (bytes_written > 0) {
102  // If SSL_read succeeds it returns the number of bytes read
103  n -= bytes_written;
104  byte_buffer += bytes_written;
105  } else {
106  // Return values of 0 or <0 indicate an error in SSL_read
107  LOG(WARNING) << "Failed to write " << n << " bytes over SSL connection";
108  return false;
109  }
110  }
111 
112  return true;
113 }
114 
115 IncomingValueInterface *SslByteStream::ReadValue(size_t n) {
116  // We can't use splice since we're using SSL here--instead we fall back on
117  // just copying the value into a IncomingStringValue object.
118  char *buf = new char[n];
119  if (!Read(buf, n)) {
120  delete[] buf;
121  return NULL;
122  }
123  std::string value(buf, n);
124  delete[] buf;
125  return new IncomingStringValue(value);
126 }
127 
128 bool SslByteStream::WriteValue(const OutgoingValueInterface &value, int* err) {
129  std::string s;
130  if (!value.ToString(&s, err)) {
131  return false;
132  }
133  return Write(s.data(), s.size());
134 }
135 
136 } // namespace kinetic