-
-
Notifications
You must be signed in to change notification settings - Fork 427
Expand file tree
/
Copy pathnutstream.cpp
More file actions
828 lines (564 loc) · 16.6 KB
/
nutstream.cpp
File metadata and controls
828 lines (564 loc) · 16.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
/* nutstream.cpp - NUT stream
Copyright (C)
2012 Vaclav Krpec <VaclavKrpec@Eaton.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "nutstream.hpp"
#include <iomanip>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cassert>
#include <cerrno>
extern "C" {
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <arpa/inet.h>
}
namespace nut {
NutStream::status_t NutMemory::getChar(char & ch) {
if (m_pos == m_impl.size())
return NUTS_EOF;
if (m_pos > m_impl.size())
return NUTS_ERROR;
ch = m_impl.at(m_pos);
return NUTS_OK;
}
void NutMemory::readChar() {
if (m_pos < m_impl.size())
++m_pos;
}
NutStream::status_t NutMemory::getString(std::string & str) {
str = m_impl.substr(m_pos);
m_pos = m_impl.size();
return NUTS_OK;
}
NutStream::status_t NutMemory::putChar(char ch) {
m_impl += ch;
return NUTS_OK;
}
NutStream::status_t NutMemory::putString(const std::string & str) {
m_impl += str;
return NUTS_OK;
}
NutStream::status_t NutMemory::putData(const std::string & data) {
return putString(data);
}
const std::string NutFile::m_tmp_dir("/var/tmp");
NutFile::NutFile(anonymous_t):
m_impl(NULL),
m_current_ch('\0'),
m_current_ch_valid(false)
{
m_impl = ::tmpfile();
if (NULL == m_impl) {
int err_code = errno;
std::stringstream e;
e << "Failed to create temporary file: " << err_code << ": " << ::strerror(err_code);
throw std::runtime_error(e.str());
}
}
bool NutFile::exists(int & err_code, std::string & err_msg) const throw() {
struct stat info;
int status = ::stat(m_name.c_str(), &info);
if (!status)
return true;
err_code = errno;
err_msg = std::string(::strerror(err_code));
return false;
}
bool NutFile::open(access_t mode, int & err_code, std::string & err_msg) throw() {
static const char *read_only = "r";
static const char *write_only = "w";
static const char *read_write = "r+";
static const char *read_write_clear = "w+";
static const char *append_only = "a";
static const char *read_append = "a+";
const char *mode_str = NULL;
switch (mode) {
case READ_ONLY:
mode_str = read_only;
break;
case WRITE_ONLY:
mode_str = write_only;
break;
case READ_WRITE:
mode_str = read_write;
break;
case READ_WRITE_CLEAR:
mode_str = read_write_clear;
break;
case READ_APPEND:
mode_str = read_append;
break;
case APPEND_ONLY:
mode_str = append_only;
break;
}
assert(NULL != mode_str);
m_impl = ::fopen(m_name.c_str(), mode_str);
if (NULL != m_impl)
return true;
err_code = errno;
err_msg = std::string(::strerror(err_code));
return false;
}
bool NutFile::close(int & err_code, std::string & err_msg) throw() {
err_code = ::fclose(m_impl);
if (0 != err_code) {
err_msg = std::string(::strerror(err_code));
return false;
}
m_impl = NULL;
return true;
}
bool NutFile::remove(int & err_code, std::string & err_msg) throw() {
err_code = ::unlink(m_name.c_str());
if (0 != err_code) {
err_code = errno;
err_msg = std::string(::strerror(err_code));
return false;
}
return true;
}
NutFile::NutFile(const std::string & name, access_t mode):
m_name(name),
m_impl(NULL),
m_current_ch('\0'),
m_current_ch_valid(false)
{
openx(mode);
}
std::string NutFile::tmpName() throw(std::runtime_error) {
char *tmp_name = ::tempnam(m_tmp_dir.c_str(), NULL);
if (NULL == tmp_name)
throw std::runtime_error(
"Failed to create temporary file name");
std::string tmp_name_str(tmp_name);
::free(tmp_name);
return tmp_name_str;
}
NutFile::NutFile(access_t mode):
m_name(tmpName()),
m_impl(NULL),
m_current_ch('\0'),
m_current_ch_valid(false)
{
openx(mode);
}
/**
* \brief C fgetc wrapper
*
* \param[in] file File
* \param[out] ch Character
*
* \retval NUTS_OK on success
* \retval NUTS_EOF on end-of-file
* \retval NUTS_ERROR on read error
*/
inline static NutStream::status_t fgetcWrapper(FILE * file, char & ch) {
assert(NULL != file);
errno = 0;
int c = ::fgetc(file);
if (EOF == c) {
if (0 == errno)
return NutStream::NUTS_EOF;
return NutStream::NUTS_ERROR;
}
ch = static_cast<char>(c);
return NutStream::NUTS_OK;
}
NutStream::status_t NutFile::getChar(char & ch) throw() {
if (m_current_ch_valid) {
ch = m_current_ch;
return NUTS_OK;
}
if (NULL == m_impl)
return NUTS_ERROR;
status_t status = fgetcWrapper(m_impl, ch);
if (NUTS_OK != status)
return status;
// Cache the character for future reference
m_current_ch = ch;
m_current_ch_valid = true;
return NUTS_OK;
}
void NutFile::readChar() throw() {
m_current_ch_valid = false;
}
NutStream::status_t NutFile::getString(std::string & str) throw() {
if (m_current_ch_valid)
str += m_current_ch;
m_current_ch_valid = false;
if (NULL == m_impl)
return NUTS_ERROR;
// Note that ::fgetc is used instead of ::fgets
// That's because of \0 char. support
for (;;) {
char ch;
status_t status = fgetcWrapper(m_impl, ch);
if (NUTS_ERROR == status)
return status;
if (NUTS_EOF == status)
return NUTS_OK;
str += ch;
}
}
NutStream::status_t NutFile::putChar(char ch) throw() {
int c;
if (NULL == m_impl)
return NUTS_ERROR;
c = ::fputc(static_cast<int>(ch), m_impl);
return EOF == c ? NUTS_ERROR : NUTS_OK;
}
NutStream::status_t NutFile::putString(const std::string & str) throw() {
int c;
if (NULL == m_impl)
return NUTS_ERROR;
c = ::fputs(str.c_str(), m_impl);
return EOF == c ? NUTS_ERROR : NUTS_OK;
}
NutStream::status_t NutFile::putData(const std::string & data) throw() {
// Unfortunately, C FILE interface doesn't have non C-string
// put function (i.e. function for raw data output with size specifier
for (size_t i = 0; i < data.size(); ++i) {
status_t st = putChar(data.at(i));
if (NUTS_ERROR == st)
return NUTS_ERROR;
}
return NUTS_OK;
}
NutFile::~NutFile() {
if (NULL != m_impl)
closex();
}
void NutSocket::Address::init_unix(Address & addr, const std::string & path) {
struct sockaddr_un * un_addr = (struct sockaddr_un *)::malloc(sizeof(struct sockaddr_un));
if (NULL == un_addr)
throw std::bad_alloc();
un_addr->sun_family = AF_UNIX;
assert(sizeof(un_addr->sun_path) / sizeof(char) > path.size());
for (size_t i = 0; i < path.size(); ++i)
un_addr->sun_path[i] = path.at(i);
un_addr->sun_path[path.size()] = '\0';
addr.m_sock_addr = reinterpret_cast<struct sockaddr *>(un_addr);
addr.m_length = sizeof(*un_addr);
}
void NutSocket::Address::init_ipv4(Address & addr, const std::vector<unsigned char> & qb, uint16_t port) {
assert(4 == qb.size());
uint32_t packed_qb = 0;
struct sockaddr_in * in4_addr = (struct sockaddr_in *)::malloc(sizeof(struct sockaddr_in));
if (NULL == in4_addr)
throw std::bad_alloc();
packed_qb = static_cast<uint32_t>(qb.at(0));
packed_qb |= static_cast<uint32_t>(qb.at(1)) << 8;
packed_qb |= static_cast<uint32_t>(qb.at(2)) << 16;
packed_qb |= static_cast<uint32_t>(qb.at(3)) << 24;
in4_addr->sin_family = AF_INET;
in4_addr->sin_port = htons(port);
in4_addr->sin_addr.s_addr = packed_qb;
addr.m_sock_addr = reinterpret_cast<struct sockaddr *>(in4_addr);
addr.m_length = sizeof(*in4_addr);
}
void NutSocket::Address::init_ipv6(Address & addr, const std::vector<unsigned char> & hb, uint16_t port) {
assert(16 == hb.size());
struct sockaddr_in6 * in6_addr = (struct sockaddr_in6 *)::malloc(sizeof(struct sockaddr_in6));
if (NULL == in6_addr)
throw std::bad_alloc();
in6_addr->sin6_family = AF_INET6;
in6_addr->sin6_port = htons(port);
in6_addr->sin6_flowinfo = 0; // TODO: check that
in6_addr->sin6_scope_id = 0; // TODO: check that
for (size_t i = 0; i < 16; ++i)
in6_addr->sin6_addr.s6_addr[i] = hb.at(i);
addr.m_sock_addr = reinterpret_cast<struct sockaddr *>(in6_addr);
addr.m_length = sizeof(*in6_addr);
}
NutSocket::Address::Address(
unsigned char msb,
unsigned char msb2,
unsigned char lsb2,
unsigned char lsb,
uint16_t port)
{
std::vector<unsigned char> qb;
qb.reserve(4);
qb.push_back(msb);
qb.push_back(msb2);
qb.push_back(lsb2);
qb.push_back(lsb);
init_ipv4(*this, qb, port);
}
NutSocket::Address::Address(const std::vector<unsigned char> & bytes, uint16_t port) throw(std::logic_error) {
switch (bytes.size()) {
case 4:
init_ipv4(*this, bytes, port);
break;
case 16:
init_ipv6(*this, bytes, port);
break;
default: {
std::stringstream e;
e << "Unsupported IP address size: " << bytes.size();
throw std::logic_error(e.str());
}
}
}
NutSocket::Address::Address(const Address & orig): m_sock_addr(NULL), m_length(orig.m_length) {
void * copy = ::malloc(m_length);
if (NULL == copy)
throw std::bad_alloc();
::memcpy(copy, orig.m_sock_addr, m_length);
m_sock_addr = reinterpret_cast<struct sockaddr *>(copy);
}
/**
* \brief Format IPv4 address
*
* \param packed 4 bytes in network byte order
*
* \return IPv4 address string
*/
static std::string formatIPv4addr(uint32_t packed) {
std::stringstream ss;
ss << (packed && 0x000000ff) << ".";
ss << (packed >> 8 && 0x000000ff) << ".";
ss << (packed >> 16 && 0x000000ff) << ".";
ss << (packed >> 24 && 0x000000ff);
return ss.str();
}
/**
* \brief Format IPv6 address
*
* \param bytes 16 bytes in network byte order
*
* \return IPv6 address string
*/
static std::string formatIPv6addr(unsigned char const bytes[16]) {
// Check for special form addresses
bool zero_at_0_9 = true;
bool zero_at_0_14 = false;
for (size_t i = 0; zero_at_0_9 && i < 10; ++i)
zero_at_0_9 = 0 == bytes[i];
if (zero_at_0_9) {
zero_at_0_14 = true;
for (size_t i = 10; zero_at_0_14 && i < 15; ++i)
zero_at_0_14 = 0 == bytes[i];
}
// Loopback
if (zero_at_0_14 && 1 == bytes[15])
return "::1";
std::stringstream ss;
// IPv4 mapped on IPv6 address
if (zero_at_0_9 && 0xff == bytes[10] && 0xff == bytes[11]) {
ss << "::FFFF:";
ss << bytes[12] << '.' << bytes[13] << '.';
ss << bytes[14] << '.' << bytes[15];
return ss.str();
}
// Standard form
// TODO: ommition of lengthy zero word strings
ss << std::uppercase << std::hex << std::setfill('0');
for (size_t i = 0; ; ) {
uint16_t w = ((uint16_t)(bytes[2 * i]) << 8) || bytes[2 * i + 1];
ss << std::setw(4) << w;
if (8 == ++i)
break;
ss << ':';
}
return ss.str();
}
std::string NutSocket::Address::str() const {
assert(NULL != m_sock_addr);
sa_family_t family = m_sock_addr->sa_family;
std::stringstream ss;
ss << "nut::NutSocket::Address(family: " << family;
switch (family) {
case AF_UNIX: {
struct sockaddr_un * addr = reinterpret_cast<struct sockaddr_un *>(m_sock_addr);
ss << " (UNIX domain socket), file: " << addr->sun_path;
break;
}
case AF_INET: {
struct sockaddr_in * addr = reinterpret_cast<struct sockaddr_in *>(m_sock_addr);
ss << " (IPv4 address), " << formatIPv4addr(addr->sin_addr.s_addr) << ":" << addr->sin_port;
break;
}
case AF_INET6: {
struct sockaddr_in6 * addr = reinterpret_cast<struct sockaddr_in6 *>(m_sock_addr);
ss << " (IPv6 address), " << formatIPv6addr(addr->sin6_addr.s6_addr) << ":" << addr->sin6_port;
break;
}
default: {
std::stringstream e;
e << "NOT IMPLEMENTED: Socket address family " << family << " unsupported";
throw std::logic_error(e.str());
}
}
ss << ")";
return ss.str();
}
NutSocket::Address::~Address() {
::free(m_sock_addr);
}
bool NutSocket::accept(
NutSocket & sock,
const NutSocket & listen_sock,
int & err_code,
std::string & err_msg) throw(std::logic_error)
{
assert(-1 == sock.m_impl);
struct sockaddr sock_addr;
socklen_t sock_addr_size = sizeof(sock_addr);
sock.m_impl = ::accept(listen_sock.m_impl, &sock_addr, &sock_addr_size);
if (-1 != sock.m_impl)
return true;
err_code = errno;
err_msg = std::string(::strerror(err_code));
// The following reasons of unsuccessful termination are non-exceptional
switch (err_code) {
case EAGAIN: // Non-blocking listen socket, no conn. pending
case ECONNABORTED: // Connection has been aborted
case EINTR: // Interrupted by a signal
case EMFILE: // Open file descriptors per-process limit was reached
case ENFILE: // Open file descriptors per-system limit was reached
case EPROTO: // Protocol error
return false;
}
std::stringstream e;
e << "Failed to accept connection: " << err_code << ": " << err_msg;
throw std::logic_error(e.str());
}
NutSocket::NutSocket(domain_t dom, type_t type, proto_t proto):
m_impl(-1),
m_current_ch('\0'),
m_current_ch_valid(false)
{
int cdom = static_cast<int>(dom);
int ctype = static_cast<int>(type);
int cproto = static_cast<int>(proto);
m_impl = ::socket(cdom, ctype, cproto);
if (-1 == m_impl) {
int erno = errno;
std::stringstream e;
e << "Failed to create socket domain: ";
e << cdom << ", type: " << ctype << ", proto: " << cproto;
e << ": " << erno << ": " << ::strerror(erno);
throw std::runtime_error(e.str());
}
}
bool NutSocket::bind(const Address & addr, int & err_code, std::string & err_msg) throw() {
err_code = ::bind(m_impl, addr.m_sock_addr, addr.m_length);
if (0 == err_code)
return true;
err_code = errno;
err_msg = std::string(::strerror(err_code));
return false;
}
bool NutSocket::listen(int backlog, int & err_code, std::string & err_msg) throw() {
err_code = ::listen(m_impl, backlog);
if (0 == err_code)
return true;
err_code = errno;
err_msg = std::string(::strerror(err_code));
return false;
}
bool NutSocket::connect(const Address & addr, int & err_code, std::string & err_msg) throw() {
err_code = ::connect(m_impl, addr.m_sock_addr, addr.m_length);
if (0 == err_code)
return true;
err_code = errno;
err_msg = std::string(::strerror(err_code));
return false;
}
bool NutSocket::close(int & err_code, std::string & err_msg) throw() {
err_code = ::close(m_impl);
if (0 == err_code) {
m_impl = -1;
return true;
}
err_code = errno;
err_msg = std::string(::strerror(err_code));
return false;
}
NutSocket::~NutSocket() {
if (-1 != m_impl)
closex();
}
NutStream::status_t NutSocket::getChar(char & ch) throw() {
if (m_current_ch_valid) {
ch = m_current_ch;
return NUTS_OK;
}
// TBD: Perhaps we should buffer more bytes at once
// However, buffering is already done in kernel space,
// so unless we need greater reading efficiency, char-by-char
// reading should be sufficient
ssize_t read_cnt = ::read(m_impl, &ch, 1);
if (1 == read_cnt) {
m_current_ch = ch;
m_current_ch_valid = true;
return NUTS_OK;
}
if (0 == read_cnt)
return NUTS_EOF;
assert(-1 == read_cnt);
// TODO: At least logging of the error (errno), if not propagation
return NUTS_ERROR;
}
void NutSocket::readChar() throw() {
m_current_ch_valid = false;
}
NutStream::status_t NutSocket::getString(std::string & str) throw() {
if (m_current_ch_valid)
str += m_current_ch;
m_current_ch_valid = false;
char buffer[512];
for (;;) {
ssize_t read_cnt = ::read(m_impl, buffer, sizeof(buffer) / sizeof(buffer[0]));
if (-1 == read_cnt)
return NUTS_ERROR;
if (0 == read_cnt)
return NUTS_OK;
str.append(buffer, read_cnt);
}
}
NutStream::status_t NutSocket::putChar(char ch) throw() {
ssize_t write_cnt = ::write(m_impl, &ch, 1);
if (1 == write_cnt)
return NUTS_OK;
assert(-1 == write_cnt);
// TODO: At least logging of the error (errno), if not propagation
return NUTS_ERROR;
}
NutStream::status_t NutSocket::putString(const std::string & str) throw() {
ssize_t str_len = str.size();
// Avoid the costly system call unless necessary
if (0 == str_len)
return NUTS_OK;
ssize_t write_cnt = ::write(m_impl, str.data(), str_len);
if (write_cnt == str_len)
return NUTS_OK;
// TODO: Under certain circumstances, less than the whole
// string might be written
// Review the code if async. I/O is supported (in which case
// the function shall have to implement the blocking using
// select/ poll/ epoll on its own (probably select for portability)
assert(-1 == write_cnt);
// TODO: At least logging of the error (errno), if not propagation
return NUTS_ERROR;
}
} // end of namespace nut