187 lines
8.2 KiB
C++
187 lines
8.2 KiB
C++
/*! \file piethernet.h
|
|
* \brief Ethernet device
|
|
*/
|
|
/*
|
|
PIP - Platform Independent Primitives
|
|
Ethernet, UDP/TCP Broadcast/Multicast
|
|
Copyright (C) 2013 Ivan Pelipenko peri4ko@gmail.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 3 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, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#ifndef PIETHERNET_H
|
|
#define PIETHERNET_H
|
|
|
|
#include "pitimer.h"
|
|
#include "piiodevice.h"
|
|
#include "piprocess.h"
|
|
|
|
class PIP_EXPORT PIEthernet: public PIIODevice
|
|
{
|
|
PIOBJECT(PIEthernet)
|
|
friend class PIPeer;
|
|
public:
|
|
// slot is any function format "bool <func>(void*, uchar*, int)"
|
|
PIEthernet(void * data, ReadRetFunc slot);
|
|
|
|
enum Type {UDP, TCP_Client, TCP_Server, TCP_SingleTCP};
|
|
enum Parameters {ReuseAddress = 0x1, Broadcast = 0x2};
|
|
|
|
PIEthernet(Type type = UDP, void * data = 0, ReadRetFunc slot = 0);
|
|
virtual ~PIEthernet();
|
|
|
|
void setReadAddress(const PIString & ip, int port) {path_ = ip + ":" + PIString::fromNumber(port);}
|
|
void setReadAddress(const PIString & ip_port) {path_ = ip_port;}
|
|
void setReadIP(const PIString & ip) {parseAddress(path_, &ip_, &port_); path_ = ip + ":" + PIString::fromNumber(port_);}
|
|
void setReadPort(int port) {parseAddress(path_, &ip_, &port_); path_ = ip_ + ":" + PIString::fromNumber(port);}
|
|
void setSendAddress(const PIString & ip, int port) {ip_s = ip; port_s = port;}
|
|
void setSendAddress(const PIString & ip_port) {parseAddress(ip_port, &ip_s, &port_s);}
|
|
void setSendIP(const PIString & ip) {ip_s = ip;}
|
|
void setSendPort(int port) {port_s = port;}
|
|
PIString readAddress() {return path_;}
|
|
PIString readIP() {parseAddress(path_, &ip_, &port_); return ip_;}
|
|
int readPort() {parseAddress(path_, &ip_, &port_); return port_;}
|
|
PIString sendAddress() {return ip_s + ":" + PIString::fromNumber(port_s);}
|
|
PIString sendIP() {return ip_s;}
|
|
int sendPort() {return port_s;}
|
|
|
|
void setParameters(PIFlags<PIEthernet::Parameters> parameters_) {params = parameters_;}
|
|
void setParameter(PIEthernet::Parameters parameter, bool on = true) {params.setFlag(parameter, on);}
|
|
bool isParameterSet(PIEthernet::Parameters parameter) const {return params[parameter];}
|
|
PIFlags<PIEthernet::Parameters> parameters() const {return params;}
|
|
|
|
//PIByteArray macAddress() {if (!init_) init(); struct ifreq ifr; memset(&ifr, 0, sizeof(ifr)); memcpy(ifr.ifr_name, "eth0", 5); ioctl(sock, SIOCSIFHWADDR, &ifr); return PIByteArray(&ifr.ifr_hwaddr.sa_data, 6);}
|
|
Type type() const {return type_;}
|
|
|
|
bool joinMulticastGroup(const PIString & group);
|
|
bool leaveMulticastGroup(const PIString & group);
|
|
|
|
bool connect();
|
|
bool connect(const PIString & ip, int port) {path_ = ip + ":" + PIString::fromNumber(port); return connect();}
|
|
bool connect(const PIString & ip_port) {path_ = ip_port; return connect();}
|
|
bool isConnected() const {return connected_;}
|
|
|
|
bool listen();
|
|
bool listen(const PIString & ip, int port) {setReadAddress(ip, port); return listen();}
|
|
bool listen(const PIString & ip_port) {setReadAddress(ip_port); return listen();}
|
|
|
|
PIEthernet * client(int index) {return clients_[index];}
|
|
int clientsCount() const {return clients_.size_s();}
|
|
PIVector<PIEthernet * > clients() {return clients_;}
|
|
|
|
bool send(const PIString & ip, int port, const void * data, int size, bool threaded = false) {ip_s = ip; port_s = port; if (threaded) {writeThreaded(data, size); return true;} return send(data, size);}
|
|
bool send(const PIString & ip_port, const void * data, int size, bool threaded = false) {parseAddress(ip_port, &ip_s, &port_s); if (threaded) {writeThreaded(data, size); return true;} return send(data, size);}
|
|
bool send(const void * data, int size, bool threaded = false) {if (threaded) {writeThreaded(data, size); return true;} return (write(data, size) == size);}
|
|
bool send(const PIByteArray & ba, bool threaded = false) {if (threaded) {writeThreaded(ba); return true;} return (write(ba) == ba.size_s());}
|
|
|
|
int read(void * read_to, int max_size);
|
|
int write(const void * data, int max_size);
|
|
int write(const PIByteArray & data) {return write(data.data(), data.size_s());}
|
|
|
|
EVENT1(newConnection, PIEthernet * , client)
|
|
EVENT0(connected)
|
|
EVENT1(disconnected, bool, withError)
|
|
|
|
enum InterfaceFlag {
|
|
ifActive = 0x1,
|
|
ifRunning = 0x2,
|
|
ifBroadcast = 0x4,
|
|
ifMulticast = 0x8,
|
|
ifLoopback = 0x10,
|
|
ifPTP = 0x20
|
|
};
|
|
|
|
typedef PIFlags<InterfaceFlag> InterfaceFlags;
|
|
|
|
struct Interface {
|
|
int index;
|
|
PIString name;
|
|
PIString mac;
|
|
PIString address;
|
|
PIString netmask;
|
|
PIString broadcast;
|
|
PIString ptp;
|
|
InterfaceFlags flags;
|
|
bool isActive() const {return flags[PIEthernet::ifActive];}
|
|
bool isRunning() const {return flags[PIEthernet::ifRunning];}
|
|
bool isBroadcast() const {return flags[PIEthernet::ifBroadcast];}
|
|
bool isMulticast() const {return flags[PIEthernet::ifMulticast];}
|
|
bool isLoopback() const {return flags[PIEthernet::ifLoopback];}
|
|
bool isPTP() const {return flags[PIEthernet::ifPTP];}
|
|
};
|
|
|
|
class InterfaceList: public PIVector<PIEthernet::Interface> {
|
|
public:
|
|
InterfaceList(): PIVector<PIEthernet::Interface>() {}
|
|
const Interface * getByIndex(int index) const {for (int i = 0; i < size_s(); ++i) if ((*this)[i].index == index) return &((*this)[i]); return 0;}
|
|
const Interface * getByName(const PIString & name) const {for (int i = 0; i < size_s(); ++i) if ((*this)[i].name == name) return &((*this)[i]); return 0;}
|
|
const Interface * getByAddress(const PIString & address) const {for (int i = 0; i < size_s(); ++i) if ((*this)[i].address == address) return &((*this)[i]); return 0;}
|
|
const Interface * getLoopback() const {for (int i = 0; i < size_s(); ++i) if ((*this)[i].isLoopback()) return &((*this)[i]); return 0;}
|
|
};
|
|
|
|
static InterfaceList interfaces();
|
|
static PIString interfaceAddress(const PIString & interface_);
|
|
static PIStringList allAddresses();
|
|
|
|
static void parseAddress(const PIString & ipp, PIString * ip, int * port);
|
|
static PIString macFromBytes(const PIByteArray & mac) {PIString r; for (int i = 0; i < mac.size_s(); ++i) {r += PIString::fromNumber(mac[i], 16).expandLeftTo(2, '0'); if (i < mac.size_s() - 1) r += ":";} return r;}
|
|
static PIByteArray macToBytes(const PIString & mac) {PIByteArray r; PIStringList sl = mac.split(":"); piForeachC (PIString & i, sl) r << uchar(i.toInt(16)); return r;}
|
|
static PIString applyMask(const PIString & ip, const PIString & mask) {struct in_addr ia; ia.s_addr = inet_addr(ip.data()) & inet_addr(mask.data()); return PIString(inet_ntoa(ia));}
|
|
static PIString getBroadcast(const PIString & ip, const PIString & mask) {struct in_addr ia; ia.s_addr = inet_addr(ip.data()) | ~inet_addr(mask.data()); return PIString(inet_ntoa(ia));}
|
|
|
|
protected:
|
|
PIEthernet(int sock, PIString ip_port);
|
|
|
|
virtual void received(void * data, int size) {;}
|
|
|
|
bool init();
|
|
bool openDevice();
|
|
bool closeDevice();
|
|
void closeSocket(int & sd);
|
|
#ifndef WINDOWS
|
|
static PIString getSockAddr(sockaddr * s) {return s == 0 ? PIString() : PIString(inet_ntoa(((sockaddr_in*)s)->sin_addr));}
|
|
#endif
|
|
|
|
|
|
int sock, sock_s, port_, port_s, port_c, wrote;
|
|
bool connected_;
|
|
sockaddr_in addr_, saddr_;
|
|
PIString ip_, ip_s, ip_c;
|
|
PIThread server_thread_;
|
|
PIVector<PIEthernet * > clients_;
|
|
PIQueue<PIString> mcast_queue;
|
|
#ifdef WINDOWS
|
|
PIMap<PIString, SOCKET> leafs;
|
|
#endif
|
|
PIFlags<PIEthernet::Parameters> params;
|
|
Type type_;
|
|
|
|
private:
|
|
static void server_func(void * eth);
|
|
|
|
static std::string ethErrorString() {
|
|
#ifdef WINDOWS
|
|
char * msg;
|
|
int err = WSAGetLastError();
|
|
FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&msg, 0, NULL);
|
|
return "code " + itos(err) + " - " + string(msg);
|
|
#else
|
|
return errorString();
|
|
#endif
|
|
}
|
|
|
|
};
|
|
|
|
#endif // PIETHERNET_H
|