| 1 |
|
| 2 |
#include "protoPacketeer.h" |
| 3 |
#include "protoDebug.h" |
| 4 |
#include "protoSocket.h" |
| 5 |
|
| 6 |
#include <sys/types.h> |
| 7 |
#include <sys/ioctl.h> |
| 8 |
#include <sys/socket.h> |
| 9 |
#include <net/if.h> |
| 10 |
#include <features.h> |
| 11 |
#if __GLIBC__ >= 2 && __GLIBC_MINOR >= 1 |
| 12 |
#include <netpacket/packet.h> |
| 13 |
#include <net/ethernet.h> |
| 14 |
#else |
| 15 |
#include <asm/types.h> |
| 16 |
#include <linux/if_packet.h> |
| 17 |
#include <linux/if_ether.h> |
| 18 |
#endif /* __GLIBC__ */ |
| 19 |
#include <netinet/in.h> |
| 20 |
#include <unistd.h> |
| 21 |
|
| 22 |
class LinuxPacketeer : public ProtoPacketeer |
| 23 |
{ |
| 24 |
public: |
| 25 |
LinuxPacketeer(); |
| 26 |
~LinuxPacketeer(); |
| 27 |
|
| 28 |
virtual bool Open(const char* interfaceName); |
| 29 |
virtual bool IsOpen() {return (-1 != socket_fd);} |
| 30 |
virtual void Close(); |
| 31 |
virtual bool Send(const char* buffer, unsigned int buflen); |
| 32 |
|
| 33 |
private: |
| 34 |
int socket_fd; |
| 35 |
struct sockaddr_ll iface_addr; |
| 36 |
|
| 37 |
}; // class LinuxPacketeer |
| 38 |
|
| 39 |
ProtoPacketeer* ProtoPacketeer::Create() |
| 40 |
{ |
| 41 |
return static_cast<ProtoPacketeer*>(new LinuxPacketeer()); |
| 42 |
} // end ProtoPacketeer::Create() |
| 43 |
|
| 44 |
LinuxPacketeer::LinuxPacketeer() |
| 45 |
: socket_fd(-1) |
| 46 |
{ |
| 47 |
} |
| 48 |
|
| 49 |
LinuxPacketeer::~LinuxPacketeer() |
| 50 |
{ |
| 51 |
Close(); |
| 52 |
} |
| 53 |
|
| 54 |
bool LinuxPacketeer::Open(const char* interfaceName) |
| 55 |
{ |
| 56 |
if ((socket_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) < 0) |
| 57 |
{ |
| 58 |
PLOG(PL_ERROR, "LinuxPacketeer::Open() socket() error: %s\n", GetErrorString()); |
| 59 |
return false; |
| 60 |
} |
| 61 |
// try to turn on broadcast capability |
| 62 |
int enable = 1; |
| 63 |
if (setsockopt(socket_fd, SOL_SOCKET, SO_BROADCAST, &enable, sizeof(enable)) < 0) |
| 64 |
PLOG(PL_ERROR, "LinuxPacketeer::Open() setsockopt(SO_BROADCAST) warning: %s\n", |
| 65 |
GetErrorString()); |
| 66 |
|
| 67 |
int ifIndex = ProtoSocket::GetInterfaceIndex(interfaceName); |
| 68 |
if (0 == ifIndex) |
| 69 |
{ |
| 70 |
PLOG(PL_ERROR, "LinuxPacketeer::Open() error getting interface index\n"); |
| 71 |
Close(); |
| 72 |
return false; |
| 73 |
} |
| 74 |
// Init our interface address structure for sends |
| 75 |
memset((char*)&iface_addr, 0, sizeof(iface_addr)); |
| 76 |
iface_addr.sll_family = AF_PACKET; |
| 77 |
iface_addr.sll_ifindex = ifIndex; |
| 78 |
return true; |
| 79 |
} // end LinuxPacketeer::Open() |
| 80 |
|
| 81 |
void LinuxPacketeer::Close() |
| 82 |
{ |
| 83 |
if (IsOpen()) |
| 84 |
{ |
| 85 |
close(socket_fd); |
| 86 |
socket_fd = -1; |
| 87 |
} |
| 88 |
} // end LinuxPacketeer::Close() |
| 89 |
|
| 90 |
bool LinuxPacketeer::Send(const char* buffer, unsigned int buflen) |
| 91 |
{ |
| 92 |
UINT16 frameType; |
| 93 |
memcpy(&frameType, buffer+12, 2); |
| 94 |
iface_addr.sll_protocol = frameType; // ntohs() not needed??? |
| 95 |
size_t result = sendto(socket_fd, buffer, buflen, 0, |
| 96 |
(struct sockaddr*)&iface_addr, |
| 97 |
sizeof(iface_addr)); |
| 98 |
if (result != buflen) |
| 99 |
{ |
| 100 |
PLOG(PL_ERROR, "LinuxPacketeer::Send() sendto() error: %s\n", GetErrorString()); |
| 101 |
return false; |
| 102 |
} |
| 103 |
return true; |
| 104 |
} // end LinuxPacketeer::Send() |