moved sources to src dir

This commit is contained in:
Bob Polis
2021-12-21 11:30:08 +01:00
parent cf654441be
commit 0d65c4e726
6 changed files with 0 additions and 0 deletions

276
src/byte_order_stuff.cpp Normal file
View File

@ -0,0 +1,276 @@
//
// byte_order_stuff.cpp
// libscio
//
// Created by Bob Polis at 2020-02-14
// Copyright (c) 2020 SwiftCoder. All rights reserved.
//
#include "libscio.hpp"
#include <cmath>
#include <cstring>
#include <stdexcept>
using namespace sc::io;
// data_streamer ---------------------------------------------------------------
data_streamer::data_streamer(byte_order bo) : _target_byte_order(bo) {}
byte_order data_streamer::cur_byte_order() {
uint8_t ch[2] = {0x12, 0x34};
uint16_t* p = reinterpret_cast<uint16_t*>(ch);
return *p == 0x1234 ? byte_order::big_endian : byte_order::little_endian;
}
std::string data_streamer::get4chars(std::istream& file) const {
char buf[4];
file.read(buf, 4);
return std::string(buf, 4);
}
uint32_t data_streamer::getui24(std::istream& file) const {
char buf[4] = {0};
int i = cur_byte_order() == byte_order::little_endian ? 0 : 1;
file.read(&buf[i], 3);
if (cur_byte_order() != _target_byte_order) {
char tmp = buf[i];
buf[i] = buf[i + 2];
buf[i + 2] = tmp;
}
return *reinterpret_cast<uint32_t*>(buf);
}
int32_t data_streamer::getsi24(std::istream& file) const {
return static_cast<int32_t>(getui24(file));
}
double data_streamer::getf80(std::istream& file) const {
float_80 val;
file.read(reinterpret_cast<char*>(val._val), sizeof(val._val));
if (_target_byte_order != byte_order::big_endian) {
byte_swap(val._val, sizeof(val._val));
}
return val;
}
void data_streamer::put4chars(const std::string& s, std::ostream& file) const {
if (s.size() < 4) {
throw std::runtime_error("data_streamer::put4chars: string too short");
}
file.write(s.c_str(), 4);
}
void data_streamer::putui24(uint32_t val, std::ostream& file) const {
char* buf = reinterpret_cast<char*>(&val);
int i = cur_byte_order() == byte_order::little_endian ? 0 : 1;
if (cur_byte_order() != _target_byte_order) {
char tmp = buf[i];
buf[i] = buf[i + 2];
buf[i + 2] = tmp;
}
file.write(&buf[i], 3);
}
void data_streamer::putsi24(int32_t val, std::ostream& file) const {
putui24(static_cast<uint32_t>(val), file);
}
void data_streamer::putf80(double val, std::ostream& file) const {
float_80 f80 {val};
if (_target_byte_order != byte_order::big_endian) {
byte_swap(f80._val, sizeof(f80._val));
}
file.write(reinterpret_cast<char*>(f80._val), sizeof(f80._val));
}
// byte_order_changer ----------------------------------------------------------
byte_order_changer::byte_order_changer(data_streamer* ds, byte_order order) : _data_streamer {ds} {
if (ds == nullptr) {
std::cerr << "byte_order_changer: no data streamer given\n";
return;
}
_saved_byte_order = _data_streamer->target_byte_order();
_data_streamer->target_byte_order(order);
}
byte_order_changer::~byte_order_changer() {
if (_data_streamer) {
_data_streamer->target_byte_order(_saved_byte_order);
}
}
byte_order_changer::byte_order_changer(byte_order_changer&& other) {
_data_streamer = other._data_streamer;
_saved_byte_order = other._saved_byte_order;
other._data_streamer = nullptr;
}
byte_order_changer& byte_order_changer::operator=(byte_order_changer&& other) {
if (&other != this) {
_data_streamer = other._data_streamer;
_saved_byte_order = other._saved_byte_order;
other._data_streamer = nullptr;
}
return *this;
}
// float_80 --------------------------------------------------------------------
/*-
* Copyright (c) 2005, 2006 by Marco Trillo <marcotrillo@gmail.com>
*
* Permission is hereby granted, free of charge, to any
* person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the
* Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice
* shall be included in all copies or substantial portions of
* the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
* KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
* PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
* OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/*
* Infinite & NAN values for non-IEEE systems
*/
#ifndef HUGE_VAL
#ifdef HUGE
#define INFINITE_VALUE HUGE
#define NAN_VALUE HUGE
#endif
#else
#define INFINITE_VALUE HUGE_VAL
#define NAN_VALUE HUGE_VAL
#endif
/*
* IEEE 754 Extended Precision
*
* Implementation here is the 80-bit extended precision
* format of Motorola 68881, Motorola 68882 and Motorola
* 68040 FPUs, as well as Intel 80x87 FPUs.
*
* See:
* http://www.freescale.com/files/32bit/doc/fact_sheet/BR509.pdf
*/
/*
* Exponent range: [-16383,16383]
* Precision for mantissa: 64 bits with no hidden bit
* Bias: 16383
*/
float_80::float_80() : float_80(0) {}
float_80::float_80(double in) {
int sgn, exp, shift;
double fraction, t;
unsigned int lexp, hexp;
unsigned long low, high;
if (in == 0.0) {
memset(_val, 0, 10);
return;
}
if (in < 0.0) {
in = fabs(in);
sgn = 1;
} else
sgn = 0;
fraction = frexp(in, &exp);
if (exp == 0 || exp > 16384) {
if (exp > 16384) /* infinite value */
low = high = 0;
else {
low = 0x80000000;
high = 0;
}
exp = 32767;
goto done;
}
fraction = ldexp(fraction, 32);
t = floor(fraction);
low = (unsigned long) t;
fraction -= t;
t = floor(ldexp(fraction, 32));
high = (unsigned long) t;
/* Convert exponents < -16382 to -16382 (then they will be
* stored as -16383) */
if (exp < -16382) {
shift = 0 - exp - 16382;
high >>= shift;
high |= (low << (32 - shift));
low >>= shift;
exp = -16382;
}
exp += 16383 - 1; /* bias */
done:
lexp = ((unsigned int) exp) >> 8;
hexp = ((unsigned int) exp) & 0xFF;
/* big endian */
_val[0] = ((unsigned char) sgn) << 7;
_val[0] |= (unsigned char) lexp;
_val[1] = (unsigned char) hexp;
_val[2] = (unsigned char) (low >> 24);
_val[3] = (unsigned char) ((low >> 16) & 0xFF);
_val[4] = (unsigned char) ((low >> 8) & 0xFF);
_val[5] = (unsigned char) (low & 0xFF);
_val[6] = (unsigned char) (high >> 24);
_val[7] = (unsigned char) ((high >> 16) & 0xFF);
_val[8] = (unsigned char) ((high >> 8) & 0xFF);
_val[9] = (unsigned char) (high & 0xFF);
}
float_80::operator double() const {
int sgn, exp;
unsigned long low, high;
double out;
/* Extract the components from the big endian buffer */
sgn = (int) (_val[0] >> 7);
exp = ((int) (_val[0] & 0x7F) << 8) | ((int) _val[1]);
low = (((unsigned long) _val[2]) << 24)
| (((unsigned long) _val[3]) << 16)
| (((unsigned long) _val[4]) << 8) | (unsigned long) _val[5];
high = (((unsigned long) _val[6]) << 24)
| (((unsigned long) _val[7]) << 16)
| (((unsigned long) _val[8]) << 8) | (unsigned long) _val[9];
if (exp == 0 && low == 0 && high == 0)
return (sgn ? -0.0 : 0.0);
switch (exp) {
case 32767:
if (low == 0 && high == 0)
return (sgn ? -INFINITE_VALUE : INFINITE_VALUE);
else
return (sgn ? -NAN_VALUE : NAN_VALUE);
default:
exp -= 16383; /* unbias exponent */
}
out = ldexp((double) low, -31 + exp);
out += ldexp((double) high, -63 + exp);
return (sgn ? -out : out);
}

94
src/fdiobuf.cpp Normal file
View File

@ -0,0 +1,94 @@
//
// fdiobuf.cpp
// libscio
//
// Created by Bob Polis at 2020-02-14
// Copyright (c) 2020 SwiftCoder. All rights reserved.
//
#include "libscio.hpp"
#ifdef _MSC_VER
#include <io.h>
#else
#include <unistd.h>
#endif
std::streambuf::int_type sc::io::fdiobuf::underflow() {
int num = ::read(_fd, &_ch, 1);
if (num <= 0) {
return EOF;
}
setg(&_ch, &_ch, &_ch + 1);
return _ch;
}
std::streamsize sc::io::fdiobuf::xsgetn(char* s, std::streamsize n) {
std::streamsize tot_read = 0;
std::streamsize next_amount = n;
while (next_amount) {
ssize_t num_read = ::read(_fd, s, next_amount);
if (num_read == -1) {
if (errno == EINTR) { // interrupted => restart
continue;
} else {
return -1; // real error
}
} else if (num_read == 0) { // EOF
break;
} else {
tot_read += num_read;
next_amount -= num_read;
}
}
return tot_read;
}
std::streambuf::int_type sc::io::fdiobuf::overflow(std::streambuf::int_type c) {
if (c != EOF) {
int res;
if ((res = ::write(_fd, &c, 1)) != 1) {
return EOF;
}
else {
std::cerr << "write result = " << res << ", errno = " << errno << '\n';
}
}
_ch = c;
setp(&_ch, &_ch + 1);
return c;
}
std::streamsize sc::io::fdiobuf::xsputn(const char* s, std::streamsize num) {
int res = ::write(_fd, s, num);
if (res != num) {
std::cerr << "write result = " << res << ", errno = " << errno << '\n';
return EOF;
}
return res;
}
std::streambuf::pos_type sc::io::fdiobuf::seekoff(std::streambuf::off_type off,
std::ios_base::seekdir dir,
std::ios_base::openmode /*which*/) {
int whence;
switch (dir) {
case std::ios_base::beg: whence = SEEK_SET; break;
case std::ios_base::end: whence = SEEK_END; break;
case std::ios_base::cur: whence = SEEK_CUR; break;
default: throw std::runtime_error("invalid seekdir");
}
return ::lseek(_fd, off, whence);
}
std::streambuf::pos_type sc::io::fdiobuf::seekpos(std::streambuf::pos_type pos,
std::ios_base::openmode /*which*/) {
return ::lseek(_fd, pos, SEEK_SET);
}
std::streambuf::int_type sc::io::fdiobuf::pbackfail(std::streambuf::int_type /*c*/) {
if (::lseek(_fd, -1, SEEK_CUR) == -1) {
return EOF;
}
return 0;
}

88
src/fdstream.cpp Normal file
View File

@ -0,0 +1,88 @@
//
// fdstream.cpp
// libscio
//
// Created by Bob Polis at 2020-04-25
// Copyright (c) 2020 SwiftCoder. All rights reserved.
//
#include "libscio.hpp"
#include <unistd.h>
// fdostream --------------------------------------------------------------
sc::io::fdostream::~fdostream() {
close();
}
void sc::io::fdostream::close() {
if (_outbuf.fd() != -1) {
::close(_outbuf.fd());
_outbuf.fd(-1);
}
}
sc::io::fdostream::fdostream(sc::io::fdostream&& other) : std::ostream(&_outbuf) {
_outbuf.fd(other._outbuf.fd());
other._outbuf.fd(-1);
}
sc::io::fdostream& sc::io::fdostream::operator=(sc::io::fdostream&& other) {
if (this != &other) {
_outbuf.fd(other._outbuf.fd());
other._outbuf.fd(-1);
}
return *this;
}
// fdistream --------------------------------------------------------------
sc::io::fdistream::~fdistream() {
close();
}
void sc::io::fdistream::close() {
if (_inbuf.fd() != -1) {
::close(_inbuf.fd());
_inbuf.fd(-1);
}
}
sc::io::fdistream::fdistream(sc::io::fdistream&& other) : std::istream(&_inbuf) {
_inbuf.fd(other._inbuf.fd());
other._inbuf.fd(-1);
}
sc::io::fdistream& sc::io::fdistream::operator=(sc::io::fdistream&& other) {
if (this != &other) {
_inbuf.fd(other._inbuf.fd());
other._inbuf.fd(-1);
}
return *this;
}
// fdstream ---------------------------------------------------------------
sc::io::fdstream::~fdstream() {
close();
}
void sc::io::fdstream::close() {
if (_iobuf.fd() != -1) {
::close(_iobuf.fd());
_iobuf.fd(-1);
}
}
sc::io::fdstream::fdstream(sc::io::fdstream&& other) : std::iostream(&_iobuf) {
_iobuf.fd(other._iobuf.fd());
other._iobuf.fd(-1);
}
sc::io::fdstream& sc::io::fdstream::operator=(sc::io::fdstream&& other) {
if (this != &other) {
_iobuf.fd(other._iobuf.fd());
other._iobuf.fd(-1);
}
return *this;
}

319
src/libscio.hpp Normal file
View File

@ -0,0 +1,319 @@
//
// libscio.hpp
// libscio
//
// Created by Bob Polis at 2020-02-14
// Copyright (c) 2020 SwiftCoder. All rights reserved.
//
#ifndef _libscio_H_
#define _libscio_H_
#include <sys/socket.h>
#include <iostream>
#include <streambuf>
#include <cstdio>
#include <cstdint>
#include <cstdlib>
#include <string>
namespace sc {
namespace io {
class fdiobuf : public std::streambuf {
public:
fdiobuf(int fd) : _fd(fd) {}
fdiobuf() : _fd(-1) {}
int fd() const { return _fd; }
void fd(int fd) { _fd = fd; }
protected:
int _fd;
char _ch {};
// input
int_type underflow() override;
std::streamsize xsgetn(char* s, std::streamsize n) override;
// output
int_type overflow(int_type c) override;
std::streamsize xsputn(const char* s, std::streamsize num) override;
// stream offset positioning
pos_type seekoff(off_type off,
std::ios_base::seekdir dir,
std::ios_base::openmode which
= std::ios_base::in | std::ios_base::out) override;
pos_type seekpos(pos_type pos, std::ios_base::openmode which
= std::ios_base::in | std::ios_base::out) override;
int_type pbackfail(int_type c = EOF) override;
};
class fdostream : public std::ostream {
public:
fdostream() : std::ostream(&_outbuf), _outbuf(-1) {}
fdostream(int fd) : std::ostream(&_outbuf), _outbuf(fd) {}
// no copying
fdostream(const fdostream&) = delete;
fdostream& operator=(const fdostream&) = delete;
// moving allowed
fdostream(fdostream&& other);
fdostream& operator=(fdostream&& other);
// cleanup: fdostream is RAII class for open file descriptor
virtual ~fdostream();
int fd() const { return _outbuf.fd(); }
void fd(int fd) { _outbuf.fd(fd); }
bool is_open() const { return _outbuf.fd() != -1; }
void close();
protected:
fdiobuf _outbuf;
};
class fdistream : public std::istream {
public:
fdistream() : std::istream(&_inbuf), _inbuf(-1) {}
fdistream(int fd) : std::istream(&_inbuf), _inbuf(fd) {}
// no copying
fdistream(const fdistream&) = delete;
fdistream& operator=(const fdistream&) = delete;
// moving allowed
fdistream(fdistream&& other);
fdistream& operator=(fdistream&& other);
// cleanup: fdistream is RAII class for open file descriptor
virtual ~fdistream();
int fd() const { return _inbuf.fd(); }
void fd(int fd) { _inbuf.fd(fd); }
bool is_open() const { return _inbuf.fd() != -1; }
void close();
protected:
fdiobuf _inbuf;
};
class fdstream : public std::iostream {
public:
fdstream() : std::iostream(&_iobuf) {}
fdstream(int fd) : std::iostream(&_iobuf), _iobuf(fd) {}
// no copying
fdstream(const fdstream&) = delete;
fdstream& operator=(const fdstream&) = delete;
// moving allowed
fdstream(fdstream&& other);
fdstream& operator=(fdstream&& other);
// cleanup: fdstream is RAII class for open file descriptor
virtual ~fdstream();
int fd() const { return _iobuf.fd(); }
void fd(int fd) { _iobuf.fd(fd); }
bool is_open() const { return _iobuf.fd() != -1; }
void close();
protected:
fdiobuf _iobuf {-1};
};
struct socket_address {
std::string address;
int port;
};
class socketstream : public std::iostream {
public:
socketstream();
socketstream(int domain, int type);
// copying forbidden
socketstream(const socketstream&) = delete;
socketstream& operator=(const socketstream&) = delete;
// moving allowed
socketstream(socketstream&& other);
socketstream& operator=(socketstream&& other);
// cleanup: socketstream is RAII class for open socket
virtual ~socketstream();
bool is_open() { return _iobuf.fd() != -1; }
void close();
// server
void make_server(const socket_address& sa);
socketstream accept() const;
// client
void connect(const socket_address& host);
// convenience: human-readable addresses
std::string address() const;
// address/endpoint conversion methods
void endpoint_from_address(const socket_address& address,
struct sockaddr_storage* endpoint) const;
socket_address address_from_endpoint(const struct sockaddr* endpoint) const;
// getters & setters
int socket() const { return _iobuf.fd(); }
void socket(int sock) { _iobuf.fd(sock); }
int domain() const { return _domain; }
void domain(int dom) { _domain = dom; }
int type() const { return _type; }
void type(int typ) { _type = typ; }
protected:
fdiobuf _iobuf {-1};
int _domain {0};
int _type {0};
struct sockaddr_storage _sa;
};
struct float_80 {
unsigned char _val[10]; // big endian
float_80();
float_80(double val);
operator double() const;
};
enum class byte_order {
undefined,
little_endian,
big_endian
};
class data_streamer {
public:
static byte_order cur_byte_order();
static inline void byte_swap(void* val, size_t n) {
char* p = reinterpret_cast<char*>(val);
for (size_t i = 0; i < n / 2; ++i) {
std::swap(p[i], p[n - 1 - i]);
}
}
template<class T>
inline T get(std::istream& file) const {
char buf[sizeof(T)];
file.read(buf, sizeof(T));
if (cur_byte_order() != _target_byte_order) {
byte_swap(buf, sizeof(T));
}
return *reinterpret_cast<T*>(buf);
}
template<class T>
inline void put(T val, std::ostream& file) const {
if (cur_byte_order() != _target_byte_order) {
byte_swap(&val, sizeof(T));
}
file.write(reinterpret_cast<char*>(&val), sizeof(T));
}
data_streamer() = default;
data_streamer(byte_order bo);
byte_order target_byte_order() const { return _target_byte_order; }
void target_byte_order(byte_order new_byte_order) { _target_byte_order = new_byte_order; }
std::string get4chars(std::istream& file) const;
uint8_t getui8(std::istream& file) const { return static_cast<uint8_t>(file.get()); }
int8_t getsi8(std::istream& file) const { return file.get(); }
uint16_t getui16(std::istream& file) const { return get<uint16_t>(file); }
int16_t getsi16(std::istream& file) const { return get<int16_t>(file); }
uint32_t getui24(std::istream& file) const;
int32_t getsi24(std::istream& file) const;
uint32_t getui32(std::istream& file) const { return get<uint32_t>(file); }
int32_t getsi32(std::istream& file) const { return get<int32_t>(file); }
uint64_t getui64(std::istream& file) const { return get<uint64_t>(file); }
int64_t getsi64(std::istream& file) const { return get<int64_t>(file); }
float getf32(std::istream& file) const { return get<float>(file); }
double getf64(std::istream& file) const { return get<double>(file); }
double getf80(std::istream& file) const;
void put4chars(const std::string& s, std::ostream& file) const;
void putui8(uint8_t val, std::ostream& file) const { file.put(val); }
void putsi8(int8_t val, std::ostream& file) const { file.put(val); }
void putui16(uint16_t val, std::ostream& file) const { put<uint16_t>(val, file); }
void putsi16(int16_t val, std::ostream& file) const { put<int16_t>(val, file); }
void putui24(uint32_t val, std::ostream& file) const;
void putsi24(int32_t val, std::ostream& file) const;
void putui32(uint32_t val, std::ostream& file) const { put<uint32_t>(val, file); }
void putsi32(int32_t val, std::ostream& file) const { put<int32_t>(val, file); }
void putui64(uint64_t val, std::ostream& file) const { put<uint64_t>(val, file); }
void putsi64(int64_t val, std::ostream& file) const { put<int64_t>(val, file); }
void putf32(float val, std::ostream& file) const { put<float>(val, file); }
void putf64(double val, std::ostream& file) const { put<double>(val, file); }
void putf80(double val, std::ostream& file) const;
private:
byte_order _target_byte_order {cur_byte_order()};
};
class byte_order_changer {
public:
byte_order_changer(data_streamer* ds, byte_order order);
~byte_order_changer();
byte_order_changer(byte_order_changer&& other);
byte_order_changer& operator=(byte_order_changer&& other);
byte_order_changer(const byte_order_changer&) = delete;
byte_order_changer& operator=(const byte_order_changer&) = delete;
private:
data_streamer* _data_streamer {nullptr};
byte_order _saved_byte_order {byte_order::undefined};
};
class mapped_file {
public:
mapped_file();
mapped_file(const std::string& path);
// no copying
mapped_file(const mapped_file&) = delete;
mapped_file& operator=(const mapped_file&) = delete;
// moving allowed
mapped_file(mapped_file&& other);
mapped_file& operator=(mapped_file&& other);
// cleanup: mapped_file is RAII class for memory mapping and file descriptor
~mapped_file();
// it's an error to open an already opened mapped_file
// open() is meant to be used after default constructor
void open(const std::string& path);
// direct (read-only) access to mapped memory
char* mapping() const { return _mapping; }
size_t size() const { return _length; }
private:
int _fd {-1};
char* _mapping {nullptr};
size_t _length {0};
};
}
}
#endif // _libscio_H_

64
src/mapped_file.cpp Normal file
View File

@ -0,0 +1,64 @@
//
// mapped_file.cpp
// conceal
//
// Created by Bob Polis at 2020-01-06
// Copyright (c) 2020 SwiftCoder. All rights reserved.
//
#include "libscio.hpp"
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <utility>
#include <stdexcept>
#include <algorithm>
#include <libscerror.hpp>
using namespace sc::io;
mapped_file::mapped_file(const std::string& path) {
open(path);
}
mapped_file::~mapped_file() {
if (_mapping) {
munmap(_mapping, _length);
_mapping = nullptr;
_length = 0;
}
if (_fd != -1) {
close(_fd);
_fd = -1;
}
}
mapped_file::mapped_file(mapped_file&& other) {
_fd = other._fd;
_mapping = other._mapping;
_length = other._length;
other._fd = -1;
other._mapping = nullptr;
other._length = 0;
}
mapped_file& mapped_file::operator=(mapped_file &&other) {
if (&other != this) {
mapped_file mf {std::move(other)};
std::swap(mf, *this);
}
return *this;
}
void mapped_file::open(const std::string& path) {
if (_fd != -1) throw std::runtime_error("already open");
_fd = ::open(path.c_str(), O_RDONLY);
throw_if_min1_msg(_fd, "could not open file");
struct stat st;
throw_if_min1_msg(fstat(_fd, &st), "could not stat file");
_length = st.st_size;
_mapping = reinterpret_cast<char*>(mmap(nullptr, _length, PROT_READ, MAP_PRIVATE, _fd, 0));
if (_mapping == MAP_FAILED) throw std::runtime_error("could not map file");
}

193
src/socketstream.cpp Normal file
View File

@ -0,0 +1,193 @@
//
// socketstream.cpp
// libscio
//
// Created by Bob Polis at 2020-03-02
// Copyright (c) 2020 SwiftCoder. All rights reserved.
//
#include "libscio.hpp"
#include <libscerror.hpp>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/un.h>
#include <cstring>
#include <vector>
#include <utility>
using namespace sc::io;
socketstream::socketstream(int domain, int type)
: std::iostream(&_iobuf), _domain(domain), _type(type) {
_iobuf.fd(::socket(_domain, _type, 0));
throw_if_min1_msg(_iobuf.fd(), "could not create socket");
std::memset(&_sa, 0, sizeof(struct sockaddr_storage));
}
socketstream::socketstream() : std::iostream(&_iobuf) {
std::memset(&_sa, 0, sizeof(struct sockaddr_storage));
}
socketstream::socketstream(socketstream&& other) : std::iostream(&_iobuf) {
_iobuf.fd(other._iobuf.fd());
_domain = other._domain;
_type = other._type;
std::memcpy(&_sa, &other._sa, sizeof(struct sockaddr_storage));
other._iobuf.fd(-1);
std::memset(&other._sa, 0, sizeof(struct sockaddr_storage));
}
socketstream& socketstream::operator=(socketstream&& other) {
if (this != &other) {
_iobuf.fd(other._iobuf.fd());
_domain = other._domain;
_type = other._type;
std::memcpy(&_sa, &other._sa, sizeof(struct sockaddr_storage));
other._iobuf.fd(-1);
std::memset(&other._sa, 0, sizeof(struct sockaddr_storage));
}
return *this;
}
socketstream::~socketstream() {
close();
}
void socketstream::close() {
if (_iobuf.fd() != -1) {
::close(_iobuf.fd());
_iobuf.fd(-1);
}
}
void socketstream::make_server(const socket_address& sa) {
size_t addr_sz = 0;
switch (_domain) {
case AF_UNIX: {
struct sockaddr_un* sau = reinterpret_cast<struct sockaddr_un*>(&_sa);
sau->sun_family = AF_UNIX;
addr_sz = sa.address.size();
std::strncpy(sau->sun_path, sa.address.c_str(), addr_sz);
break;
}
case AF_INET: {
struct sockaddr_in* sa4 = reinterpret_cast<struct sockaddr_in*>(&_sa);
sa4->sin_family = AF_INET;
sa4->sin_addr.s_addr = INADDR_ANY;
sa4->sin_port = htons(sa.port);
addr_sz = sizeof(struct sockaddr_in);
break;
}
case AF_INET6: {
struct sockaddr_in6* sa6 = reinterpret_cast<struct sockaddr_in6*>(&_sa);
sa6->sin6_family = AF_INET6;
sa6->sin6_addr = in6addr_any;
sa6->sin6_port = htons(sa.port);
addr_sz = sizeof(struct sockaddr_in6);
break;
}
default:
errno = EAFNOSUPPORT;
throw_if_min1_msg(-1, "unsupported socket domain");
}
struct sockaddr* addr = reinterpret_cast<struct sockaddr*>(&_sa);
throw_if_min1(::bind(_iobuf.fd(), addr, addr_sz));
if (_type == SOCK_STREAM) {
throw_if_min1(::listen(_iobuf.fd(), SOMAXCONN));
}
}
socketstream socketstream::accept() const {
socketstream peer;
socklen_t len = sizeof(struct sockaddr_storage);
int active_socket = ::accept(_iobuf.fd(), reinterpret_cast<struct sockaddr*>(&peer._sa), &len);
throw_if_min1(active_socket);
peer._iobuf.fd(active_socket);
peer._domain = _domain;
peer._type = _type;
return peer;
}
void socketstream::connect(const socket_address& host) {
endpoint_from_address(host, &_sa);
struct sockaddr* addr = reinterpret_cast<struct sockaddr*>(&_sa);
throw_if_min1(::connect(_iobuf.fd(), addr, sizeof(_sa)));
}
std::string socketstream::address() const {
const struct sockaddr* addr = reinterpret_cast<const struct sockaddr*>(&_sa);
socket_address sa = address_from_endpoint(addr);
return sa.address;
}
void socketstream::endpoint_from_address(const socket_address& address,
struct sockaddr_storage* endpoint) const {
throw_if_null(endpoint);
switch (_domain) {
case AF_UNIX: {
struct sockaddr_un* addr = reinterpret_cast<struct sockaddr_un*>(endpoint);
addr->sun_family = AF_UNIX;
std::memcpy(addr->sun_path, address.address.c_str(), address.address.size());
break;
}
case AF_INET: {
struct sockaddr_in* addr = reinterpret_cast<struct sockaddr_in*>(endpoint);
addr->sin_family = AF_INET;
addr->sin_port = htons(address.port);
::inet_pton(_domain, address.address.c_str(), &addr->sin_addr);
break;
}
case AF_INET6: {
struct sockaddr_in6* addr = reinterpret_cast<struct sockaddr_in6*>(endpoint);
addr->sin6_family = AF_INET6;
addr->sin6_port = htons(address.port);
addr->sin6_flowinfo = 0;
::inet_pton(_domain, address.address.c_str(), &addr->sin6_addr);
addr->sin6_scope_id = 0;
break;
}
default:
errno = EAFNOSUPPORT;
throw_if_min1_msg(-1, "unsupported socket domain");
}
}
socket_address socketstream::address_from_endpoint(const struct sockaddr* endpoint) const {
throw_if_null(endpoint);
socket_address sa;
switch (_domain) {
case AF_UNIX: {
const struct sockaddr_un* addr = reinterpret_cast<const struct sockaddr_un*>(endpoint);
sa.address = addr->sun_path;
sa.port = 0;
break;
}
case AF_INET: {
const struct sockaddr_in* addr = reinterpret_cast<const struct sockaddr_in*>(endpoint);
std::vector<char> buf;
buf.resize(INET_ADDRSTRLEN);
::inet_ntop(_domain, &addr->sin_addr, buf.data(), buf.size());
std::string temp {buf.data(), buf.size()};
sa.address = std::move(temp);
sa.port = ntohs(addr->sin_port);
break;
}
case AF_INET6: {
const struct sockaddr_in6* addr = reinterpret_cast<const struct sockaddr_in6*>(endpoint);
std::vector<char> buf;
buf.resize(INET6_ADDRSTRLEN);
::inet_ntop(_domain, &addr->sin6_addr, buf.data(), buf.size());
std::string temp {buf.data(), buf.size()};
sa.address = std::move(temp);
sa.port = ntohs(addr->sin6_port);
break;
}
default:
errno = EAFNOSUPPORT;
throw_if_min1_msg(-1, "unsupported socket domain");
}
return sa;
}