From 3d58a62727346b7ac1a6cb36fed1a06ed72228dd Mon Sep 17 00:00:00 2001 From: save Date: Mon, 7 Apr 2008 14:17:42 +0000 Subject: Moved the complete svn base into the cesar directory. git-svn-id: svn+ssh://pessac/svn/cesar/trunk@1769 017c9cb6-072f-447c-8318-d5b54f68fe89 --- cesar/maximus/ethernet/src/EthernetProcessor.cpp | 292 +++++++++++++++++++++++ 1 file changed, 292 insertions(+) create mode 100644 cesar/maximus/ethernet/src/EthernetProcessor.cpp (limited to 'cesar/maximus/ethernet/src/EthernetProcessor.cpp') diff --git a/cesar/maximus/ethernet/src/EthernetProcessor.cpp b/cesar/maximus/ethernet/src/EthernetProcessor.cpp new file mode 100644 index 0000000000..406748dd8d --- /dev/null +++ b/cesar/maximus/ethernet/src/EthernetProcessor.cpp @@ -0,0 +1,292 @@ +/************************************************************************ + EthernetProcessor.cpp - Copyright buret + +Here you can write a license for your code, some comments or any other +information you want to have in your generated code. To to this simply +configure the "headings" directory in uml to point to a directory +where you have your heading files. + +or you can just replace the contents of this file with your own. +If you want to do this, this file is located at + +/usr/share/apps/umbrello/headings/heading.cpp + +-->Code Generators searches for heading files based on the file extension + i.e. it will look for a file name ending in ".h" to include in C++ header + files, and for a file name ending in ".java" to include in all generated + java code. + If you name the file "heading.", Code Generator will always + choose this file even if there are other files with the same extension in the + directory. If you name the file something else, it must be the only one with that + extension in the directory to guarantee that Code Generator will choose it. + +you can use variables in your heading files which are replaced at generation +time. possible variables are : author, date, time, filename and filepath. +just write %variable_name% + +This file was generated on %date% at %time% +The original location of this file is /home/buret/eclipse/maximus/ethernet/src/EthernetProcessor.cpp +**************************************************************************/ + +#include "EthernetProcessor.h" + +#include "EtherSciMsg.h" +#include "ISci.h" + +#include "Error.h" +#include "Logger.h" + +#include // for 'struct sockaddr' +#include +#include +#include +#include // for 'O_RDWR' +#include // for 'system()' +using namespace std; + + +// Constructors/Destructors +// + + +EthernetProcessor::EthernetProcessor ( ISci * p_sci ): +mpSci(NULL), +mInterfaceCb(NULL) +{ + logFunction(); + + if (NULL == p_sci) + { + errno = EINVAL; + throw Error(__PRETTY_FUNCTION__, "SCI pointer is NULL", errno); + } + mpSci = p_sci; + initAttributes(); +} + + +void EthernetProcessor::initAttributes ( ) +{ + logFunction(); + + registerEtherSciMsg(); +} + + +EthernetProcessor::~EthernetProcessor ( ) +{ + logFunction(); + + if (NULL != mpSci) + { + mpSci = NULL; + } + if (NULL != mInterfaceCb) + { + mInterfaceCb = NULL; + } +} + + +// +// Methods +// + + +// Other methods +// + + +// public methods +// + + +bool EthernetProcessor::init ( EtherCb interface_cb ) +{ + logFunction(); + + if (NULL == interface_cb) + { + errno = EINVAL; + throw Error(__PRETTY_FUNCTION__, "Callback function address is NULL", errno); + } + mInterfaceCb = interface_cb; + + return true; +} + + +EtherSciMsg * EthernetProcessor::createEther ( ) +{ + logFunction(); + + return new EtherSciMsg(this); +} + + +bool EthernetProcessor::sendEther ( EtherSciMsg & ether_sci_msg ) +{ + logFunction(); + bool bSend = false; + + if ( fillEther(ether_sci_msg) && (0 != ether_sci_msg.getSciMsgStationId()) ) + { + bSend = getSci()->sendSciMsg(ether_sci_msg); + } + if (!bSend) + { + throw Error(__PRETTY_FUNCTION__, "cannot send Ether SCI message", errno); + } + + return bSend; +} + + +bool EthernetProcessor::receiveEther ( EtherSciMsg & ether_sci_msg ) +{ + logFunction(); + + // Check Ether SCI message data length and data + if ( (0 == ether_sci_msg.getSpecializedSciMsgDataLength()) + || (NULL == ether_sci_msg.getSpecializedSciMsgData()) ) + { + errno = EINVAL; + throw Error(__PRETTY_FUNCTION__, "received Ether SCI message is incorrect", errno); + } + + // Go back up to user + if (NULL != getInterfaceCb()) + { + getInterfaceCb()(ether_sci_msg); + } + + return true; +} + + +File_Descriptor EthernetProcessor::allocTap ( char * dev ) const +{ + logFunction(); + int etherLogFileDescriptor = -1; + + if (0 > (etherLogFileDescriptor = open("/dev/net/tun", O_RDWR))) + { + throw Error(__PRETTY_FUNCTION__, "open /dev/net/tun failed", errno); + } + else + { + struct ifreq ifr; + memset(&ifr, 0, sizeof(ifr)); + ifr.ifr_flags = IFF_TAP; // TAP device + if (* dev) + { + strncpy(ifr.ifr_name, dev, IFNAMSIZ); + } + if (0 > ioctl(etherLogFileDescriptor, TUNSETIFF, (void *)&ifr)) + { + close(etherLogFileDescriptor); + etherLogFileDescriptor = -1; + if (EPERM == errno) + { + throw Error(__PRETTY_FUNCTION__, "please launch the program with root rights", errno); + } + else + { + throw Error(__PRETTY_FUNCTION__, "ioctl failed", errno); + } + } + else + { + //system("wireshark"); + strcpy(dev, ifr.ifr_name); + } + } + + return etherLogFileDescriptor; +} + + +// private methods +// + + +void EthernetProcessor::registerEtherSciMsg ( ) +{ + logFunction(); + + if (!getSci()->registerSpecializedSciMsg(SCI_MSG_TYPE_ETHERNET, new EtherSciMsg((IEthernet *)this))) + { + throw Error(__PRETTY_FUNCTION__, "cannot register Ether SCI message to SCI"); + } +} + + +bool EthernetProcessor::fillEther ( EtherSciMsg & ether_sci_msg ) const +{ + logFunction(); + bool bFill = false; + + // Fill specialized SCI msg header + // + struct Ethernet_Header ethernetHeader; + ethernetHeader.version = ether_sci_msg.getSpecializedSciMsgHeader().version; + ethernetHeader.type = ether_sci_msg.getSpecializedSciMsgType(); + ethernetHeader.sniffer_type = ether_sci_msg.getSnifferType(); + ethernetHeader.flags = ether_sci_msg.getFlags(); + ethernetHeader.reserved = ether_sci_msg.getSpecializedSciMsgHeader().reserved; + + // Set specialized SCI msg header + // + bFill = ether_sci_msg.setSpecializedSciMsgHeader(ethernetHeader); + + // Fill specialized SCI msg attributes: + // - header size + // + bFill &= ether_sci_msg.setSpecializedSciMsgHeaderSize(sizeof(struct Ethernet_Header)); + + // Fill SCI msg attributes: + // - type + // + bFill &= ether_sci_msg.setSciMsgType(SCI_MSG_TYPE_ETHERNET); + bFill &= getSci()->fillSciMsg(ether_sci_msg); + + return bFill; +} + + +// protected methods +// + + +// Accessor methods +// + + +// public attribute accessor methods +// + + +// private attribute accessor methods +// + + +ISci * EthernetProcessor::getSci ( ) const +{ + if (NULL == mpSci) + { + throw Error(__PRETTY_FUNCTION__, "SCI pointer is NULL"); + } + + return mpSci; +} + + +EtherCb EthernetProcessor::getInterfaceCb ( ) const +{ + return mInterfaceCb; +} + + +// protected attribute accessor methods +// + -- cgit v1.2.3