Added hostBytesToStr function to socket library, fixed code style, fixed UDP Cygwin support

This commit is contained in:
Thulinma 2016-11-03 15:15:42 +01:00
parent 567759ce26
commit 629b24853a
3 changed files with 544 additions and 617 deletions

File diff suppressed because it is too large Load diff

View file

@ -3,150 +3,154 @@
/// Written by Jaron Vietor in 2010 for DDVTech /// Written by Jaron Vietor in 2010 for DDVTech
#pragma once #pragma once
#include <string>
#include <sstream>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <deque> #include <deque>
#include <errno.h>
#include <fcntl.h>
#include <sstream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <unistd.h>
//for being friendly with Socket::Connection down below // for being friendly with Socket::Connection down below
namespace Buffer { namespace Buffer{
class user; class user;
} }
///Holds Socket tools. /// Holds Socket tools.
namespace Socket { namespace Socket{
void hostBytesToStr(const char *bytes, size_t len, std::string &target);
/// A buffer made out of std::string objects that can be efficiently read from and written to. /// A buffer made out of std::string objects that can be efficiently read from and written to.
class Buffer { class Buffer{
private: private:
std::deque<std::string> data; std::deque<std::string> data;
public:
unsigned int size(); public:
unsigned int bytes(unsigned int max); unsigned int size();
void append(const std::string & newdata); unsigned int bytes(unsigned int max);
void append(const char * newdata, const unsigned int newdatasize); void append(const std::string &newdata);
void prepend(const std::string & newdata); void append(const char *newdata, const unsigned int newdatasize);
void prepend(const char * newdata, const unsigned int newdatasize); void prepend(const std::string &newdata);
std::string & get(); void prepend(const char *newdata, const unsigned int newdatasize);
bool available(unsigned int count); std::string &get();
std::string remove(unsigned int count); bool available(unsigned int count);
std::string copy(unsigned int count); std::string remove(unsigned int count);
void clear(); std::string copy(unsigned int count);
void clear();
}; };
//Buffer // Buffer
/// This class is for easy communicating through sockets, either TCP or Unix. /// This class is for easy communicating through sockets, either TCP or Unix.
class Connection { class Connection{
private: private:
int sock; ///< Internally saved socket number. int sock; ///< Internally saved socket number.
int pipes[2]; ///< Internally saved file descriptors for pipe socket simulation. int pipes[2]; ///< Internally saved file descriptors for pipe socket simulation.
std::string remotehost; ///< Stores remote host address. std::string remotehost; ///< Stores remote host address.
uint64_t up; uint64_t up;
uint64_t down; uint64_t down;
long long int conntime; long long int conntime;
Buffer downbuffer; ///< Stores temporary data coming in. Buffer downbuffer; ///< Stores temporary data coming in.
int iread(void * buffer, int len, int flags = 0); ///< Incremental read call. int iread(void *buffer, int len, int flags = 0); ///< Incremental read call.
unsigned int iwrite(const void * buffer, int len); ///< Incremental write call. unsigned int iwrite(const void *buffer, int len); ///< Incremental write call.
bool iread(Buffer & buffer, int flags = 0); ///< Incremental write call that is compatible with Socket::Buffer. bool iread(Buffer &buffer, int flags = 0); ///< Incremental write call that is compatible with Socket::Buffer.
bool iwrite(std::string & buffer); ///< Write call that is compatible with std::string. bool iwrite(std::string &buffer); ///< Write call that is compatible with std::string.
public: public:
//friends // friends
friend class ::Buffer::user; friend class ::Buffer::user;
//constructors // constructors
Connection(); ///< Create a new disconnected base socket. Connection(); ///< Create a new disconnected base socket.
Connection(int sockNo); ///< Create a new base socket. Connection(int sockNo); ///< Create a new base socket.
Connection(std::string hostname, int port, bool nonblock); ///< Create a new TCP socket. Connection(std::string hostname, int port, bool nonblock); ///< Create a new TCP socket.
Connection(std::string adres, bool nonblock = false); ///< Create a new Unix Socket. Connection(std::string adres, bool nonblock = false); ///< Create a new Unix Socket.
Connection(int write, int read); ///< Simulate a socket using two file descriptors. Connection(int write, int read); ///< Simulate a socket using two file descriptors.
//generic methods // generic methods
void close(); ///< Close connection. void close(); ///< Close connection.
void drop(); ///< Close connection without shutdown. void drop(); ///< Close connection without shutdown.
void setBlocking(bool blocking); ///< Set this socket to be blocking (true) or nonblocking (false). void setBlocking(bool blocking); ///< Set this socket to be blocking (true) or nonblocking (false).
bool isBlocking(); ///< Check if this socket is blocking (true) or nonblocking (false). bool isBlocking(); ///< Check if this socket is blocking (true) or nonblocking (false).
std::string getHost() const; ///< Gets hostname for connection, if available. std::string getHost() const; ///< Gets hostname for connection, if available.
std::string getBinHost(); std::string getBinHost();
void setHost(std::string host); ///< Sets hostname for connection manually. void setHost(std::string host); ///< Sets hostname for connection manually.
int getSocket(); ///< Returns internal socket number. int getSocket(); ///< Returns internal socket number.
int getPureSocket(); ///< Returns non-piped internal socket number. int getPureSocket(); ///< Returns non-piped internal socket number.
std::string getError(); ///< Returns a string describing the last error that occured. std::string getError(); ///< Returns a string describing the last error that occured.
bool connected() const; ///< Returns the connected-state for this socket. bool connected() const; ///< Returns the connected-state for this socket.
bool isAddress(std::string addr); bool isAddress(std::string addr);
//buffered i/o methods // buffered i/o methods
bool spool(); ///< Updates the downbufferinternal variables. bool spool(); ///< Updates the downbufferinternal variables.
bool peek(); ///< Clears the downbuffer and fills it with peek bool peek(); ///< Clears the downbuffer and fills it with peek
Buffer & Received(); ///< Returns a reference to the download buffer. Buffer &Received(); ///< Returns a reference to the download buffer.
void SendNow(const std::string & data); ///< Will not buffer anything but always send right away. Blocks. void SendNow(const std::string &data); ///< Will not buffer anything but always send right away. Blocks.
void SendNow(const char * data); ///< Will not buffer anything but always send right away. Blocks. void SendNow(const char *data); ///< Will not buffer anything but always send right away. Blocks.
void SendNow(const char * data, size_t len); ///< Will not buffer anything but always send right away. Blocks. void SendNow(const char *data, size_t len); ///< Will not buffer anything but always send right away. Blocks.
//stats related methods // stats related methods
unsigned int connTime();///< Returns the time this socket has been connected. unsigned int connTime(); ///< Returns the time this socket has been connected.
uint64_t dataUp(); ///< Returns total amount of bytes sent. uint64_t dataUp(); ///< Returns total amount of bytes sent.
uint64_t dataDown(); ///< Returns total amount of bytes received. uint64_t dataDown(); ///< Returns total amount of bytes received.
std::string getStats(std::string C); ///< Returns a std::string of stats, ended by a newline. void resetCounter(); ///< Resets the up/down bytes counter to zero.
friend class Server; std::string getStats(std::string C); ///< Returns a std::string of stats, ended by a newline.
bool Error; ///< Set to true if a socket error happened. friend class Server;
bool Blocking; ///< Set to true if a socket is currently or wants to be blocking. bool Error; ///< Set to true if a socket error happened.
//overloaded operators bool Blocking; ///< Set to true if a socket is currently or wants to be blocking.
bool operator==(const Connection & B) const; // overloaded operators
bool operator!=(const Connection & B) const; bool operator==(const Connection &B) const;
operator bool() const; bool operator!=(const Connection &B) const;
operator bool() const;
}; };
/// This class is for easily setting up listening socket, either TCP or Unix. /// This class is for easily setting up listening socket, either TCP or Unix.
class Server { class Server{
private: private:
std::string errors; ///< Stores errors that may have occured. std::string errors; ///< Stores errors that may have occured.
int sock; ///< Internally saved socket number. int sock; ///< Internally saved socket number.
bool IPv6bind(int port, std::string hostname, bool nonblock); ///< Attempt to bind an IPv6 socket bool IPv6bind(int port, std::string hostname, bool nonblock); ///< Attempt to bind an IPv6 socket
bool IPv4bind(int port, std::string hostname, bool nonblock); ///< Attempt to bind an IPv4 socket bool IPv4bind(int port, std::string hostname, bool nonblock); ///< Attempt to bind an IPv4 socket
public: public:
Server(); ///< Create a new base Server. Server(); ///< Create a new base Server.
Server(int port, std::string hostname = "0.0.0.0", bool nonblock = false); ///< Create a new TCP Server. Server(int port, std::string hostname = "0.0.0.0", bool nonblock = false); ///< Create a new TCP Server.
Server(std::string adres, bool nonblock = false); ///< Create a new Unix Server. Server(std::string adres, bool nonblock = false); ///< Create a new Unix Server.
Connection accept(bool nonblock = false); ///< Accept any waiting connections. Connection accept(bool nonblock = false); ///< Accept any waiting connections.
void setBlocking(bool blocking); ///< Set this socket to be blocking (true) or nonblocking (false). void setBlocking(bool blocking); ///< Set this socket to be blocking (true) or nonblocking (false).
bool connected() const; ///< Returns the connected-state for this socket. bool connected() const; ///< Returns the connected-state for this socket.
bool isBlocking(); ///< Check if this socket is blocking (true) or nonblocking (false). bool isBlocking(); ///< Check if this socket is blocking (true) or nonblocking (false).
void close(); ///< Close connection. void close(); ///< Close connection.
void drop(); ///< Close connection without shutdown. void drop(); ///< Close connection without shutdown.
int getSocket(); ///< Returns internal socket number. int getSocket(); ///< Returns internal socket number.
}; };
class UDPConnection { class UDPConnection{
private: private:
int sock; ///< Internally saved socket number. int sock; ///< Internally saved socket number.
std::string remotehost;///< Stores remote host address std::string remotehost; ///< Stores remote host address
void * destAddr;///< Destination address pointer. void *destAddr; ///< Destination address pointer.
unsigned int destAddr_size;///< Size of the destination address pointer. unsigned int destAddr_size; ///< Size of the destination address pointer.
unsigned int up;///< Amount of bytes transferred up. unsigned int up; ///< Amount of bytes transferred up.
unsigned int down;///< Amount of bytes transferred down. unsigned int down; ///< Amount of bytes transferred down.
unsigned int data_size;///< The size in bytes of the allocated space in the data pointer. unsigned int data_size; ///< The size in bytes of the allocated space in the data pointer.
int family;///<Current socket address family int family; ///<Current socket address family
public: public:
char * data;///< Holds the last received packet. char *data; ///< Holds the last received packet.
unsigned int data_len; ///< The size in bytes of the last received packet. unsigned int data_len; ///< The size in bytes of the last received packet.
UDPConnection(const UDPConnection & o); UDPConnection(const UDPConnection &o);
UDPConnection(bool nonblock = false); UDPConnection(bool nonblock = false);
~UDPConnection(); ~UDPConnection();
void close(); void close();
int getSock(); int getSock();
int bind(int port, std::string iface = "", const std::string & multicastAddress = ""); int bind(int port, std::string iface = "", const std::string &multicastAddress = "");
void setBlocking(bool blocking); void setBlocking(bool blocking);
void SetDestination(std::string hostname, uint32_t port); void SetDestination(std::string hostname, uint32_t port);
void GetDestination(std::string & hostname, uint32_t & port); void GetDestination(std::string &hostname, uint32_t &port);
uint32_t getDestPort() const; uint32_t getDestPort() const;
bool Receive(); bool Receive();
void SendNow(const std::string & data); void SendNow(const std::string &data);
void SendNow(const char * data); void SendNow(const char *data);
void SendNow(const char * data, size_t len); void SendNow(const char *data, size_t len);
}; };
} }

View file

@ -49,16 +49,7 @@ std::string Controller::sessIndex::toStr(){
/// Initializes a sessIndex from a statExchange object, converting binary format IP addresses into strings. /// Initializes a sessIndex from a statExchange object, converting binary format IP addresses into strings.
/// This extracts the host, stream name, connector and crc field, ignoring everything else. /// This extracts the host, stream name, connector and crc field, ignoring everything else.
Controller::sessIndex::sessIndex(IPC::statExchange & data){ Controller::sessIndex::sessIndex(IPC::statExchange & data){
std::string tHost = data.host(); Socket::hostBytesToStr(data.host().c_str(), 16, host);
if (tHost.substr(0, 12) == std::string("\000\000\000\000\000\000\000\000\000\000\377\377", 12)){
char tmpstr[16];
snprintf(tmpstr, 16, "%hhu.%hhu.%hhu.%hhu", tHost[12], tHost[13], tHost[14], tHost[15]);
host = tmpstr;
}else{
char tmpstr[40];
snprintf(tmpstr, 40, "%0.2x%0.2x:%0.2x%0.2x:%0.2x%0.2x:%0.2x%0.2x:%0.2x%0.2x:%0.2x%0.2x:%0.2x%0.2x:%0.2x%0.2x", tHost[0], tHost[1], tHost[2], tHost[3], tHost[4], tHost[5], tHost[6], tHost[7], tHost[8], tHost[9], tHost[10], tHost[11], tHost[12], tHost[13], tHost[14], tHost[15]);
host = tmpstr;
}
streamName = data.streamName(); streamName = data.streamName();
connector = data.connector(); connector = data.connector();
crc = data.crc(); crc = data.crc();