mirror of https://github.com/LX3JL/xlxd.git
- created raw socket class - moved udp recv via msg to a derived class - option file read optimizedpull/140/head
parent
66bd9aca8a
commit
73648794f0
@ -0,0 +1,156 @@
|
|||||||
|
//
|
||||||
|
// crawsocket.cpp
|
||||||
|
// xlxd
|
||||||
|
//
|
||||||
|
// Created by Marius Petrescu (YO2LOJ) on 22/02/2020.
|
||||||
|
// Copyright © 2020 Marius Petrescu (YO2LOJ). All rights reserved.
|
||||||
|
//
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
// This file is part of xlxd.
|
||||||
|
//
|
||||||
|
// xlxd 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.
|
||||||
|
//
|
||||||
|
// xlxd 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 Foobar. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#include "main.h"
|
||||||
|
#include <string.h>
|
||||||
|
#include "creflector.h"
|
||||||
|
#include "crawsocket.h"
|
||||||
|
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// constructor
|
||||||
|
|
||||||
|
CRawSocket::CRawSocket()
|
||||||
|
{
|
||||||
|
m_Socket = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// destructor
|
||||||
|
|
||||||
|
CRawSocket::~CRawSocket()
|
||||||
|
{
|
||||||
|
if ( m_Socket != -1 )
|
||||||
|
{
|
||||||
|
Close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// open & close
|
||||||
|
|
||||||
|
bool CRawSocket::Open(uint16 uiProto)
|
||||||
|
{
|
||||||
|
bool open = false;
|
||||||
|
int on = 1;
|
||||||
|
|
||||||
|
// create socket
|
||||||
|
m_Socket = socket(AF_INET,SOCK_RAW,uiProto);
|
||||||
|
if ( m_Socket != -1 )
|
||||||
|
{
|
||||||
|
fcntl(m_Socket, F_SETFL, O_NONBLOCK);
|
||||||
|
open = true;
|
||||||
|
m_Proto = uiProto;
|
||||||
|
}
|
||||||
|
|
||||||
|
// done
|
||||||
|
return open;
|
||||||
|
}
|
||||||
|
|
||||||
|
void CRawSocket::Close(void)
|
||||||
|
{
|
||||||
|
if ( m_Socket != -1 )
|
||||||
|
{
|
||||||
|
close(m_Socket);
|
||||||
|
m_Socket = -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// read
|
||||||
|
|
||||||
|
int CRawSocket::Receive(CBuffer *Buffer, CIp *Ip, int timeout)
|
||||||
|
{
|
||||||
|
struct sockaddr_in Sin;
|
||||||
|
fd_set FdSet;
|
||||||
|
unsigned int uiFromLen = sizeof(struct sockaddr_in);
|
||||||
|
int iRecvLen = -1;
|
||||||
|
struct timeval tv;
|
||||||
|
|
||||||
|
// socket valid ?
|
||||||
|
if ( m_Socket != -1 )
|
||||||
|
{
|
||||||
|
// allocate buffer
|
||||||
|
Buffer->resize(UDP_BUFFER_LENMAX);
|
||||||
|
|
||||||
|
// control socket
|
||||||
|
FD_ZERO(&FdSet);
|
||||||
|
FD_SET(m_Socket, &FdSet);
|
||||||
|
tv.tv_sec = timeout / 1000;
|
||||||
|
tv.tv_usec = (timeout % 1000) * 1000;
|
||||||
|
select(m_Socket + 1, &FdSet, 0, 0, &tv);
|
||||||
|
|
||||||
|
// read
|
||||||
|
iRecvLen = (int)recvfrom(m_Socket,
|
||||||
|
(void *)Buffer->data(), RAW_BUFFER_LENMAX,
|
||||||
|
0, (struct sockaddr *)&Sin, &uiFromLen);
|
||||||
|
|
||||||
|
// handle
|
||||||
|
if ( iRecvLen != -1 )
|
||||||
|
{
|
||||||
|
// adjust buffer size
|
||||||
|
Buffer->resize(iRecvLen);
|
||||||
|
|
||||||
|
// get IP
|
||||||
|
Ip->SetSockAddr(&Sin);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// done
|
||||||
|
return iRecvLen;
|
||||||
|
}
|
||||||
|
|
||||||
|
// protocol specific
|
||||||
|
|
||||||
|
// ICMP
|
||||||
|
|
||||||
|
int CRawSocket::IcmpReceive(CBuffer *Buffer, CIp *Ip, int timeout)
|
||||||
|
{
|
||||||
|
int iIcmpType = -1;
|
||||||
|
int iRecv;
|
||||||
|
|
||||||
|
if (m_Proto == IPPROTO_ICMP)
|
||||||
|
{
|
||||||
|
iRecv = Receive(Buffer, Ip, timeout);
|
||||||
|
|
||||||
|
if (iRecv >= (int)(sizeof(struct ip) + sizeof(struct icmp)))
|
||||||
|
{
|
||||||
|
struct ip *iph = (struct ip *)Buffer->data();
|
||||||
|
int iphdrlen = iph->ip_hl * 4;
|
||||||
|
struct icmp *icmph = (struct icmp *)((unsigned char *)iph + iphdrlen);
|
||||||
|
struct ip *remote_iph = (struct ip *)((unsigned char *)icmph + 8);
|
||||||
|
|
||||||
|
iIcmpType = icmph->icmp_type;
|
||||||
|
|
||||||
|
struct sockaddr_in Sin;
|
||||||
|
bzero(&Sin, sizeof(Sin));
|
||||||
|
Sin.sin_family = AF_INET;
|
||||||
|
Sin.sin_addr.s_addr = remote_iph->ip_dst.s_addr;
|
||||||
|
|
||||||
|
Ip->SetSockAddr(&Sin);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return iIcmpType;
|
||||||
|
}
|
||||||
@ -0,0 +1,99 @@
|
|||||||
|
//
|
||||||
|
// crawsocket.h
|
||||||
|
// xlxd
|
||||||
|
//
|
||||||
|
// Created by Marius Petrescu (YO2LOJ) on 22/02/2020.
|
||||||
|
// Copyright © 2020 Marius Petrescu (YO2LOJ). All rights reserved.
|
||||||
|
//
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
// This file is part of xlxd.
|
||||||
|
//
|
||||||
|
// xlxd 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.
|
||||||
|
//
|
||||||
|
// xlxd 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 Foobar. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Description:
|
||||||
|
// Raw socket access class with protocol specific
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef crawsocket_h
|
||||||
|
#define crawsocket_h
|
||||||
|
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <sys/ioctl.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
|
||||||
|
#include <netinet/ip.h>
|
||||||
|
#include <netinet/ip_icmp.h>
|
||||||
|
#include "cip.h"
|
||||||
|
#include "cbuffer.h"
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// define
|
||||||
|
|
||||||
|
#define RAW_BUFFER_LENMAX 65536
|
||||||
|
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// class
|
||||||
|
|
||||||
|
class CRawSocket
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
// constructor
|
||||||
|
CRawSocket();
|
||||||
|
|
||||||
|
// destructor
|
||||||
|
~CRawSocket();
|
||||||
|
|
||||||
|
// open & close
|
||||||
|
bool Open(uint16);
|
||||||
|
void Close(void);
|
||||||
|
int GetSocket(void) { return m_Socket; }
|
||||||
|
|
||||||
|
// read
|
||||||
|
|
||||||
|
// if ETH_P_ALL is used, the received data buffer will hold
|
||||||
|
// the ethernet header (struct ethhdr) followed by the IP header (struct iphdr),
|
||||||
|
// the protocol header (e.g tcp, udp, icmp) and the data.
|
||||||
|
// For specific protocols, the data content may vary depending on the protocol
|
||||||
|
// Returns the number of received bytes in buffer
|
||||||
|
|
||||||
|
int Receive(CBuffer *, CIp *, int);
|
||||||
|
|
||||||
|
// ICMP receive helper
|
||||||
|
// parameters:
|
||||||
|
// buffer - packet receive buffer (starting with ip header)
|
||||||
|
// ip - remote address (filled in on receive)
|
||||||
|
// timeout - receive timeout in msec
|
||||||
|
// return value:
|
||||||
|
// ICMP type, -1 if nothing was received
|
||||||
|
|
||||||
|
int IcmpReceive(CBuffer *, CIp *, int);
|
||||||
|
|
||||||
|
// write
|
||||||
|
// no write support - complexity makes it out of scope for now
|
||||||
|
// to be added if needed
|
||||||
|
|
||||||
|
protected:
|
||||||
|
// data
|
||||||
|
int m_Socket;
|
||||||
|
int m_Proto;
|
||||||
|
struct sockaddr_in m_SocketAddr;
|
||||||
|
};
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
#endif /* crawsocket_h */
|
||||||
@ -0,0 +1,102 @@
|
|||||||
|
//
|
||||||
|
// cudpmsgsocket.cpp
|
||||||
|
// xlxd
|
||||||
|
//
|
||||||
|
// Created by Marius Petrescu (YO2LOJ) on 22/02/2020.
|
||||||
|
// Copyright © 2020 Marius Petrescu (YO2LOJ). All rights reserved.
|
||||||
|
//
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
// This file is part of xlxd.
|
||||||
|
//
|
||||||
|
// xlxd 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.
|
||||||
|
//
|
||||||
|
// xlxd 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 Foobar. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#include "main.h"
|
||||||
|
#include <string.h>
|
||||||
|
#include "cudpmsgsocket.h"
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// read
|
||||||
|
|
||||||
|
int CUdpMsgSocket::Receive(CBuffer *Buffer, CIp *Ip, int timeout)
|
||||||
|
{
|
||||||
|
struct sockaddr_in Sin;
|
||||||
|
fd_set FdSet;
|
||||||
|
unsigned int uiFromLen = sizeof(struct sockaddr_in);
|
||||||
|
int iRecvLen = -1;
|
||||||
|
struct timeval tv;
|
||||||
|
|
||||||
|
struct msghdr Msg;
|
||||||
|
struct iovec Iov[1];
|
||||||
|
|
||||||
|
union {
|
||||||
|
struct cmsghdr cm;
|
||||||
|
unsigned char pktinfo_sizer[sizeof(struct cmsghdr) + sizeof(struct in_pktinfo)];
|
||||||
|
} Control;
|
||||||
|
|
||||||
|
// socket valid ?
|
||||||
|
if ( m_Socket != -1 )
|
||||||
|
{
|
||||||
|
// allocate buffer
|
||||||
|
Buffer->resize(UDP_MSG_BUFFER_LENMAX);
|
||||||
|
|
||||||
|
//prepare msghdr
|
||||||
|
bzero(&Msg, sizeof(Msg));
|
||||||
|
Iov[0].iov_base = Buffer->data();
|
||||||
|
Iov[0].iov_len = UDP_MSG_BUFFER_LENMAX;
|
||||||
|
|
||||||
|
bzero(&Sin, sizeof(Sin));
|
||||||
|
Msg.msg_name = &Sin;
|
||||||
|
Msg.msg_namelen = sizeof(Sin);
|
||||||
|
Msg.msg_iov = Iov;
|
||||||
|
Msg.msg_iovlen = 1;
|
||||||
|
Msg.msg_control = &Control;
|
||||||
|
Msg.msg_controllen = sizeof(Control);
|
||||||
|
|
||||||
|
// control socket
|
||||||
|
FD_ZERO(&FdSet);
|
||||||
|
FD_SET(m_Socket, &FdSet);
|
||||||
|
tv.tv_sec = timeout / 1000;
|
||||||
|
tv.tv_usec = (timeout % 1000) * 1000;
|
||||||
|
select(m_Socket + 1, &FdSet, 0, 0, &tv);
|
||||||
|
|
||||||
|
// read
|
||||||
|
iRecvLen = (int)recvmsg(m_Socket, &Msg, 0);
|
||||||
|
|
||||||
|
// handle
|
||||||
|
if ( iRecvLen != -1 )
|
||||||
|
{
|
||||||
|
// adjust buffer size
|
||||||
|
Buffer->resize(iRecvLen);
|
||||||
|
|
||||||
|
// get IP
|
||||||
|
Ip->SetSockAddr(&Sin);
|
||||||
|
|
||||||
|
// get local IP
|
||||||
|
struct cmsghdr *Cmsg;
|
||||||
|
for (Cmsg = CMSG_FIRSTHDR(&Msg); Cmsg != NULL; Cmsg = CMSG_NXTHDR(&Msg, Cmsg))
|
||||||
|
{
|
||||||
|
if (Cmsg->cmsg_level == IPPROTO_IP && Cmsg->cmsg_type == IP_PKTINFO)
|
||||||
|
{
|
||||||
|
struct in_pktinfo *PktInfo = (struct in_pktinfo *)CMSG_DATA(Cmsg);
|
||||||
|
m_LocalAddr.s_addr = PktInfo->ipi_spec_dst.s_addr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// done
|
||||||
|
return iRecvLen;
|
||||||
|
}
|
||||||
|
|
||||||
@ -0,0 +1,53 @@
|
|||||||
|
//
|
||||||
|
// cudpmsgsocket.h
|
||||||
|
// xlxd
|
||||||
|
//
|
||||||
|
// Created by Marius Petrescu (YO2LOJ) on 22/02/2020.
|
||||||
|
// Copyright © 2020 Marius Petrescu (YO2LOJ). All rights reserved.
|
||||||
|
//
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
// This file is part of xlxd.
|
||||||
|
//
|
||||||
|
// xlxd 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.
|
||||||
|
//
|
||||||
|
// xlxd 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 Foobar. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Description:
|
||||||
|
// Extension of the CUdpSocket class to allow retrieving
|
||||||
|
// the local target IP for a G3 Terminal mode config request
|
||||||
|
|
||||||
|
#ifndef cudpmsgsocket_h
|
||||||
|
#define cudpmsgsocket_h
|
||||||
|
|
||||||
|
#include "cudpsocket.h"
|
||||||
|
|
||||||
|
#define UDP_MSG_BUFFER_LENMAX 1024
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// class
|
||||||
|
|
||||||
|
class CUdpMsgSocket : public CUdpSocket
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
// read
|
||||||
|
int Receive(CBuffer *, CIp *, int);
|
||||||
|
|
||||||
|
struct in_addr *GetLocalAddr(void) { return &m_LocalAddr; }
|
||||||
|
|
||||||
|
protected:
|
||||||
|
// data
|
||||||
|
struct in_addr m_LocalAddr;
|
||||||
|
};
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
#endif /* cudpmsgsocket_h */
|
||||||
Loading…
Reference in new issue