#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "multicastConfig.hh" #include "bldClientTest.hh" #include using std::string; using namespace Pds::ConfigurationMulticast; namespace EpicsBld { static int iDefaultDataSeed = 1; /** * class Ins * * imported from ::Pds::Ins */ class Ins { public: Ins(); enum Option {DoNotInitialize}; explicit Ins(Option); explicit Ins(unsigned short port); explicit Ins(int address); Ins(int address, unsigned short port); Ins(const Ins& ins, unsigned short port); explicit Ins(const sockaddr_in& sa); int address() const; void address(int address); unsigned short portId() const; int operator==(const Ins& that) const; protected: int _address; unsigned _port; }; /** * class Sockaddr * * imported from ::Pds::Sockaddr */ class Sockaddr { public: Sockaddr() { _sockaddr.sin_family = AF_INET; } Sockaddr(const Ins& ins) { _sockaddr.sin_family = AF_INET; _sockaddr.sin_addr.s_addr = htonl(ins.address()); _sockaddr.sin_port = htons(ins.portId()); } void get(const Ins& ins) { _sockaddr.sin_addr.s_addr = htonl(ins.address()); _sockaddr.sin_port = htons(ins.portId()); } sockaddr* name() const {return (sockaddr*)&_sockaddr;} inline int sizeofName() const {return sizeof(_sockaddr);} private: sockaddr_in _sockaddr; }; /** * class Port * * imported from ::Pds::Port */ class Port : public Ins { public: enum Type {ClientPort = 0, ServerPort = 2, VectoredServerPort = 3}; ~Port(); Port(Port::Type, const Ins&, int sizeofDatagram, int maxPayload, int maxDatagrams); Port(Port::Type, int sizeofDatagram, int maxPayload, int maxDatagrams); int sizeofDatagram() const; int maxPayload() const; int maxDatagrams() const; Type type() const; // Construction may potentially fail. If the construction is // successful error() returns 0. If construction fails, error() // returns a non-zero value which corresponds to the reason (as an // "errno"). int error() const; int getSocket() { return _socket; } // modified by tomy, used to get the socket handle protected: void error(int value); int _socket; private: enum {Class_Server = 2, Style_Vectored = 1}; void _open(Port::Type, const Ins&, int sizeofDatagram, int maxPayload, int maxDatagrams); int _bind(const Ins&); void _close(); Type _type; int _sizeofDatagram; int _maxPayload; int _maxDatagrams; int _error; }; class Client : public Port { public: Client(int sizeofDatagram, int maxPayload, int maxDatagrams = 1); ~Client(); public: int send(const char* datagram, const char* payload, int sizeofPayload, const Ins&); /* * Multicast functions */ void multicastSetInterface(unsigned interface); int multicastSetTTL(unsigned char ucTTL); private: enum {SendFlags = 0}; #ifdef ODF_LITTLE_ENDIAN // Would prefer to have _swap_buffer in the stack, but it triggers // a g++ bug (tried release 2.96) with pointers to member // functions of classes which are bigger than 0x7ffc bytes; this // bug appeared in Outlet which points to OutletWire member // functions; hence OutletWire must be <= 0x7ffc bytes; if // Client is too large the problem appears in OutletWire // since it contains an Client. // Later note: g++ 3.0.2 doesn't show this problem. char* _swap_buffer; void _swap(const iovec* iov, unsigned msgcount, iovec* iov_swap); #endif }; /** * Bld Multicast Client Test class */ class BldClientTest : public BldClientInterface { public: BldClientTest(unsigned uAddr, unsigned uPort, unsigned int uMaxDataSize, unsigned char ucTTL = 32, char* sInteraceIp = NULL); BldClientTest(unsigned uAddr, unsigned uPort, unsigned int uMaxDataSize, unsigned char ucTTL = 32, unsigned int uInteraceIp = 0); virtual ~BldClientTest(); virtual int sendRawData(int iSizeData, const char* pData); private: int _initClient( unsigned int uMaxDataSize, unsigned char ucTTL, unsigned int uInterfaceIp); int _initClient( unsigned int uMaxDataSize, unsigned char ucTTL, char* sInterfaceIp); Client* _pSocket; // socket unsigned _uAddr, _uPort; }; static string addressToStr( unsigned int uAddr ) { unsigned int uNetworkAddr = htonl(uAddr); const unsigned char* pcAddr = (const unsigned char*) &uNetworkAddr; std::stringstream sstream; sstream << (int) pcAddr[0] << "." << (int) pcAddr[1] << "." << (int) pcAddr[2] << "." << (int) pcAddr[3]; return sstream.str(); } } // namespace EpicsBld extern "C" { // forward declaration int BldClientTestSendInterface(int iDataSeed, char* sInterfaceIp, int iNumPackets); int BldClientTestSendAddr(unsigned int uAddr, unsigned int uPort, unsigned int uMaxDataSize, int iDataSeed, char* sInterfaceIp, int iNumPackets); /** * Bld Client basic test function * * Will continuously send out the multicast packets to a default * address with default values. Need to be stop manually from keyboard * by pressing Ctrl+C * * This fucntion is only used for quick testing of Bld Client, such as * running from CExp Command Line. */ int BldClientTestSendBasic(int iDataSeed) { return BldClientTestSendInterface(iDataSeed, NULL, 0); } /** * Bld Client test function with IP interface selection * * Similar to the above function. It lets user to specify the network interface by * IP address (xxx.xx.xx.xx) or name (eth0, eth1,...). * * This fucntion is only used for quick testing of Bld Client, such as * running from CExp Command Line. */ int BldClientTestSendInterface(int iDataSeed, char* sInterfaceIp, int iNumPackets) { return BldClientTestSendAddr(uDefaultAddr, uDefaultPort, uDefaultMaxDataSize, iDataSeed, sInterfaceIp, iNumPackets); } /** * Bld Client test function with address and IP interface selection * * Similar to BldClientTestSendBasic(), but with the argument (sInterfaceIp) * to specify the IP interface for sending multicast. * * Will continuously send out the multicast packets to a default * address with default values. Need to be stop manually from keyboard * by pressing Ctrl+C * * This fucntion is only used for quick testing of Bld Client, such as * running from CExp Command Line. */ int BldClientTestSendAddr(unsigned int uAddr, unsigned int uPort, unsigned int uMaxDataSize, int iDataSeed, char* sInterfaceIp, int iNumPackets) { EpicsBld::BldClientInterface* pBldClient = EpicsBld::BldClientFactory::createBldClient(uAddr, uPort, uMaxDataSize, ucDefaultTTL, sInterfaceIp); unsigned int contains=0; if (iDataSeed <= 0) { switch(uAddr&0xff) { case 1: // PhaseCavity contains = 0x00000010; uMaxDataSize = 32; break; case 2: // FEEGasDet contains = 0x0000000e; uMaxDataSize = 32; break; case 44: case 46: case 47: case 48: // Spectrometer contains = 0x00000048; uMaxDataSize = 1280*4; break; case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 10: case 11: case 17: case 18: case 19: case 45: // SharedIpimb default: contains = 0x00010023; // SharedIpimbV2 uMaxDataSize = 32+60+28; break; } } unsigned int uIntDataSize = uMaxDataSize/4+15; int* liData = new int[uIntDataSize]; int iTestValue = iDataSeed * 1000; printf( "Beginning Multicast Client Testing\n" "Data Seed = %d Interface = %s Buffer Size = %u\n" "Press Ctrl+C to Exit...\n", iDataSeed, (sInterfaceIp ? sInterfaceIp: "NULL [Default Interface]"), uMaxDataSize ); timespec tv_begin; clock_gettime(CLOCK_REALTIME,&tv_begin); int iPacketsSent = 0; int prev_pulseid = 0; /*int evgpulseid = 0; unsigned int evgsec=0; unsigned int evgnsec = 0;*/ epicsTimeStamp epicsTs; int rc; while ( true ) { unsigned int uIndex=0; /*extern void getPulseId(int *pulseid, unsigned int *sec, unsigned int *nsec); getPulseId(&evgpulseid, &evgsec, &evgnsec); */ rc = evrTimeGetFromPipeline(&epicsTs, evrTimeActive, 0, 0, 0, 0, 0); /* scondam: 26-Jun-2014: PCD swapped the sec/nsec fields. nsec field is first now */ liData[uIndex++] = /* 0; */ /* evgnsec; */ epicsTs.nsec; // clock hi liData[uIndex++] = /* 0; */ /* evgsec; */ epicsTs.secPastEpoch; // clock lo liData[uIndex++] = 0; // stamp hi liData[uIndex++] = /* iPacketsSent; */ /* evgpulseid; */ PULSEID(epicsTs); // stamp lo liData[uIndex++] = 0; // env liData[uIndex++] = 0; // damage liData[uIndex++] = 0x06000000; // src liData[uIndex++] = uAddr&0xff; // src liData[uIndex++] = contains; // contains liData[uIndex++] = uMaxDataSize+20; // extent liData[uIndex++] = 0; // damage liData[uIndex++] = 0x06000000; // src liData[uIndex++] = uAddr&0xff; // src liData[uIndex++] = contains; // contains liData[uIndex++] = uMaxDataSize+20; // extent while (uIndexsendRawData(uIntDataSize*sizeof(int), reinterpret_cast(liData)); iTestValue++; iPacketsSent++; } if ( iNumPackets > 0 && iPacketsSent >= iNumPackets ) break; timespec tv_end; clock_gettime(CLOCK_REALTIME,&tv_end); double t_rem = double(iPacketsSent)/120. - double(tv_end.tv_sec-tv_begin.tv_sec) - 1.e-9*double(int(tv_end.tv_nsec-tv_begin.tv_nsec)); if (t_rem > 0) { tv_end.tv_sec = unsigned(t_rem); tv_end.tv_nsec = unsigned(1.e9*(t_rem-double(tv_end.tv_sec))); //printf("[%d] \trem %f s [%lu.%09ld]\n", iPacketsSent, t_rem, //tv_end.tv_sec, tv_end.tv_nsec); nanosleep(&tv_end,0); } else ; // printf("[%d] \trem %f s\n", iPacketsSent, t_rem); // Waiting for keyboard interrupt to break the infinite loop } delete[] liData; delete pBldClient; return 0; } /* * The following functions provide C wrappers for accesing EpicsBld::BldClientInterface * and EpicsBld::BldClientFactory */ /** * Init function: Use EpicsBld::BldClientFactory to construct the BldClient * and save the pointer in (*ppVoidBldClient) */ int BldClientInitByInterfaceName(unsigned uAddr, unsigned uPort, unsigned int uMaxDataSize, unsigned char ucTTL, char* sInterfaceIp, void** ppVoidBldClient) { if ( ppVoidBldClient == NULL ) return 1; EpicsBld::BldClientInterface* pBldClient = EpicsBld::BldClientFactory::createBldClient(uAddr, uPort, uMaxDataSize, ucTTL, sInterfaceIp); *ppVoidBldClient = reinterpret_cast(pBldClient); return 0; } int BldClientInitByInterfaceAddress(unsigned uAddr, unsigned uPort, unsigned int uMaxDataSize, unsigned char ucTTL, unsigned int uInterfaceIp, void** ppVoidBldClient) { if ( ppVoidBldClient == NULL ) return 1; EpicsBld::BldClientInterface* pBldClient = EpicsBld::BldClientFactory::createBldClient(uAddr, uPort, uMaxDataSize, ucTTL, uInterfaceIp); *ppVoidBldClient = reinterpret_cast(pBldClient); return 0; } /** * Release function: Call C++ delete operator to delete the BldClient */ int BldClientRelease(void* pVoidBldClient) { if ( pVoidBldClient == NULL ) return 1; EpicsBld::BldClientInterface* pBldClient = reinterpret_cast(pVoidBldClient); delete pBldClient; return 0; } /** * Call the Send function defined in EpicsBld::BldClientInterface */ int BldClientSendRawData(void* pVoidBldClient, int iSizeData, char* pData) { if ( pVoidBldClient == NULL || pData == NULL ) return -1; EpicsBld::BldClientInterface* pBldClient = reinterpret_cast(pVoidBldClient); return pBldClient->sendRawData(iSizeData, pData); } static void showUsage() { printf( "Usage: bldClientTest [-v|--version] [-h|--help] [-a|--address ] [-p|--port ] [-i|--interface ] [ ]\n" " Options:\n" " -v|--version Show file version\n" " -h|--help Show Usage\n" " -a|--address Set the multicast address to be sent to. Default: %s\n" " -p|--port Set the port to be sent to. Default: %u\n" " -s|--seed Set the seed value for generating different data for each client program. Default: %i\n" " -z|--size Set the max data size for allocating buffer. Default: %u\n" " -i|--interface Set the network interface for receiving multicast. Use ether IP address (xxx.xx.xx.xx) or name (eth0, eth1,...)\n" " -n|--number Number of packets to be sent (0: Keep sending packets and wait for Ctrl-C to quit)\n" " Same as -s flag above. *This is an argument without the option (-s) flag\n" " Same as -i flag above. *This is an argument without the option (-i) flag\n", EpicsBld::addressToStr(uDefaultAddr).c_str(), uDefaultPort, EpicsBld::iDefaultDataSeed, uDefaultMaxDataSize ); } static const char sBldClientTestVersion[] = "0.90"; static void showVersion() { printf( "Version: bldClientTest Ver %s\n", sBldClientTestVersion ); } /* This is the command, which the ioc shell will call directly */ int bldClientTest(int argc, char** argv) { int iOptionIndex = 0; const char* sOptions = ":vha:p:i:s:z:n:"; const struct option loOptions[] = { {"ver", 0, 0, 'v'}, {"help", 0, 0, 'h'}, {"address", 1, 0, 'a'}, {"port", 1, 0, 'p'}, {"seed", 1, 0, 's'}, {"size", 1, 0, 'z'}, {"interface",1, 0, 'i'}, {"number", 1, 0, 'n'}, {0, 0, 0, 0 } }; unsigned int uAddr = uDefaultAddr; unsigned int uPort = uDefaultPort; unsigned int uMaxDataSize = uDefaultMaxDataSize; int iDataSeed = EpicsBld::iDefaultDataSeed; int iNumPackets = 0; char* sInterfaceIp = NULL; while ( int opt = getopt_long(argc, argv, sOptions, loOptions, &iOptionIndex ) ) { if ( opt == -1 ) break; switch(opt) { case 'v': /* Print usage */ showVersion(); return 0; case 'a': uAddr = ntohl(inet_addr(optarg)); break; case 'p': uPort = strtoul(optarg, NULL, 0); break; case 's': iDataSeed = strtol(optarg, NULL, 0); break; case 'z': uMaxDataSize = strtoul(optarg, NULL, 0); break; case 'i': sInterfaceIp = optarg; break; case 'n': iNumPackets = strtoul(optarg, NULL, 0); break; case '?': /* Terse output mode */ printf( "bldClientTest:main(): Unknown option: %c\n", optopt ); break; case ':': /* Terse output mode */ printf( "bldClientTest:main(): Missing argument for %c\n", optopt ); break; default: case 'h': /* Print usage */ showUsage(); return 0; } } argc -= optind; argv += optind; if (argc >= 1 ) iDataSeed = atoi(argv[0]); if (argc >= 2 ) sInterfaceIp = argv[1]; return BldClientTestSendAddr( uAddr, uPort, uMaxDataSize, iDataSeed, sInterfaceIp, iNumPackets ); } } // extern "C" /** * class member definitions * */ namespace EpicsBld { /** * class Ins */ inline int Ins::operator==(const Ins& that) const{ return (_address == that._address && _port == that._port); } inline Ins::Ins(Option) { } inline Ins::Ins() { _address = INADDR_ANY; _port = 0; } inline Ins::Ins(unsigned short port) { _address = INADDR_ANY; _port = port; } inline Ins::Ins(int address, unsigned short port) { _address = address; _port = port; } inline Ins::Ins(const Ins& ins, unsigned short port) { _address = ins._address; _port = port; } inline Ins::Ins(const sockaddr_in& sa) { _address = ntohl(sa.sin_addr.s_addr); _port = ntohs(sa.sin_port); } inline Ins::Ins(int address) { _address = address; _port = 0; } inline unsigned short Ins::portId() const { return _port; } inline void Ins::address(int address) { _address = address; } inline int Ins::address() const { return _address; } /** * class Port */ inline Port::~Port() { _close(); } inline Port::Port(Port::Type type, int sizeofDatagram, int maxPayload, int maxDatagrams) : Ins(Ins::DoNotInitialize) { Ins ins; _open(type, ins, sizeofDatagram, maxPayload, maxDatagrams); } inline Port::Port(Port::Type type, const Ins& ins, int sizeofDatagram, int maxPayload, int maxDatagrams) : Ins(Ins::DoNotInitialize) { _open(type, ins, sizeofDatagram, maxPayload, maxDatagrams); } inline Port::Type Port::type() const { return _type; } inline int Port::sizeofDatagram() const { return _sizeofDatagram; } inline int Port::maxPayload() const { return _maxPayload; } inline int Port::maxDatagrams() const { return _maxDatagrams; } inline int Port::error() const { return _error; } inline void Port::error(int value) { _error = value; } void Port::_open(Port::Type type, const Ins& ins, int sizeofDatagram, int maxPayload, int maxDatagrams) { _socket = socket(AF_INET, SOCK_DGRAM, 0); _sizeofDatagram = sizeofDatagram; _maxPayload = maxPayload; _maxDatagrams = maxDatagrams; _type = type; if(!(_error = (_socket != -1) ? _bind(ins) : errno)) { sockaddr_in name; #ifdef VXWORKS int length = sizeof(name); #else socklen_t length = sizeof(name); #endif if(getsockname(_socket, (sockaddr*)&name, &length) == 0) { _address = ntohl(name.sin_addr.s_addr); _port = (unsigned)ntohs(name.sin_port); } else { _error = errno; _close(); } } else _close(); if (_error) { printf("*** Port failed to open address 0x%x port %i: %s\n", ins.address(), ins.portId(), strerror(errno)); } } int Port::_bind(const Ins& ins) { int s = _socket; int yes = 1; if(setsockopt(s, SOL_SOCKET, SO_BROADCAST, (char*)&yes, sizeof(yes)) == -1) return errno; // struct ip_mreq mreq; // mreq.imr_multiaddr.s_addr = 239<<24 | 255<<16 | 0<<8 | 1; // mreq.imr_interface.s_addr = 172<<24 | 21<<16 | 9<<8 | 23; // if (setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, //sizeof(mreq)) == -1 ) // printf( "Error adding multicast group\n" ); if(_type == Port::ClientPort) { int parm = (_sizeofDatagram + _maxPayload + sizeof(struct sockaddr_in)) * _maxDatagrams; #ifdef VXWORKS // Out for Tornado II 7/21/00 - RiC // The following was found exprimentally with ~claus/bbr/test/sizeTest // The rule may well change if the mBlk, clBlk or cluster parameters change // as defined in usrNetwork.c - RiC parm = parm + (88 + 32 * ((parm - 1993) / 2048)); #endif if(setsockopt(s, SOL_SOCKET, SO_SNDBUF, (char*)&parm, sizeof(parm)) == -1) return errno; } if(_type & Class_Server) { int parm = (_sizeofDatagram + _maxPayload + sizeof(struct sockaddr_in)) * _maxDatagrams; #ifdef __linux__ parm += 2048; // 1125 #endif if(setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char*)&parm, sizeof(parm)) == -1) return errno; if(ins.portId() && (_type & Style_Vectored)) { int y = 1; #ifdef VXWORKS if(setsockopt(s, SOL_SOCKET, SO_REUSEPORT, (char*)&y, sizeof(y)) == -1) #else if(setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&y, sizeof(y)) == -1) #endif return errno; } } #ifdef VXWORKS Sockaddr sa(Ins(ins.portId())); #else Sockaddr sa(ins); #endif return (bind(s, sa.name(), sa.sizeofName()) == -1) ? errno : 0; } void Port::_close() { if(_socket != -1) { close(_socket); _socket = -1; } } /** * class Client */ Client::Client(int sizeofDatagram, int maxPayload, int maxDatagrams) : Port(Port::ClientPort, sizeofDatagram, maxPayload, maxDatagrams) { #ifdef ODF_LITTLE_ENDIAN _swap_buffer = new char[sizeofDatagram+maxPayload]; #endif } /* ** ++ ** ** Delete swap buffer under little endian machines ** ** -- */ Client::~Client() { #ifdef ODF_LITTLE_ENDIAN delete _swap_buffer; #endif } /* ** ++ ** ** Use specified interface for sending multicast addresses ** ** -- */ void Client::multicastSetInterface(unsigned interface) { in_addr address; address.s_addr = htonl(interface); if (setsockopt(_socket, IPPROTO_IP, IP_MULTICAST_IF, (char*)&address, sizeof(in_addr)) < 0) { printf( "Client::multicastSetInterface(): setsockopt(IP_MULTICAST_IF, ...) failed\n" " errno = %d (%s)\n", errno, strerror(errno) ); error(errno); } } /** * Set the multicast TTL value * * @param ucTTL The TTL value * @return 0 if successful, 1 if error */ int Client::multicastSetTTL(unsigned char ucTTL) { if (setsockopt(_socket, IPPROTO_IP, IP_MULTICAST_TTL, (char*)&ucTTL, sizeof(ucTTL)) < 0) return 1; return 0; } /* ** ++ ** ** This function is used to transmit the specified datagram to the ** address specified by the "dst" argument. ** The datagram is specified in two parts: "datagram", which is a ** pointer the datagram's fixed size header, and "payload", which ** is a pointer to the datagrams' variable sized portion. The size ** of the payload (in bytes) is specified by the "sizeofPayload" ** argument. The function returns the transmission status. A value ** of zero (0) indicates the transfer was sucessfull. A positive ** non-zero value is the reason (as an "errno"value) the transfer ** failed. ** ** -- */ int Client::send(const char* datagram, const char* payload, int sizeofPayload, const Ins& dst) { struct msghdr hdr; struct iovec iov[2]; if(datagram) { iov[0].iov_base = (caddr_t)(datagram); iov[0].iov_len = sizeofDatagram(); iov[1].iov_base = (caddr_t)(payload); iov[1].iov_len = sizeofPayload; hdr.msg_iovlen = 2; } else { iov[0].iov_base = (caddr_t)(payload); iov[0].iov_len = sizeofPayload; hdr.msg_iovlen = 1; } Sockaddr sa(dst); hdr.msg_name = (caddr_t)sa.name(); hdr.msg_namelen = sa.sizeofName(); hdr.msg_control = (caddr_t)0; hdr.msg_controllen = 0; #ifdef ODF_LITTLE_ENDIAN struct iovec iov_swap; _swap(iov, hdr.msg_iovlen, &iov_swap); hdr.msg_iov = &iov_swap; hdr.msg_iovlen = 1; #else hdr.msg_iov = &iov[0]; #endif int length = sendmsg(_socket, &hdr, SendFlags); return (length == - 1) ? errno : 0; } #ifdef ODF_LITTLE_ENDIAN void Client::_swap(const iovec* iov, unsigned msgcount, iovec* iov_swap) { unsigned* dst = (unsigned*)_swap_buffer; const iovec* iov_end = iov+msgcount; unsigned total = 0; do { unsigned len = iov->iov_len; unsigned* src = (unsigned*)(iov->iov_base); unsigned* end = src + (len >> 2); while (src < end) *dst++ = htonl(*src++); total += len; } while (++iov < iov_end); iov_swap->iov_len = total; iov_swap->iov_base = _swap_buffer; } #endif /** * class BldClientFactory */ BldClientInterface* BldClientFactory::createBldClient(unsigned uAddr, unsigned uPort, unsigned int uMaxDataSize, unsigned char ucTTL, char* sInteraceIp) { return new BldClientTest(uAddr, uPort, uMaxDataSize, ucTTL, sInteraceIp ); } BldClientInterface* BldClientFactory::createBldClient(unsigned uAddr, unsigned uPort, unsigned int uMaxDataSize, unsigned char ucTTL, unsigned int uInterfaceIp) { return new BldClientTest(uAddr, uPort, uMaxDataSize, ucTTL, uInterfaceIp ); } /** * class BldClientTest */ BldClientTest::BldClientTest(unsigned uAddr, unsigned uPort, unsigned int uMaxDataSize, unsigned char ucTTL, char* sInterfaceIp) : _pSocket(NULL), _uAddr(uAddr), _uPort(uPort) { _initClient(uMaxDataSize, ucTTL, sInterfaceIp); } BldClientTest::BldClientTest(unsigned uAddr, unsigned uPort, unsigned int uMaxDataSize, unsigned char ucTTL, unsigned int uInterfaceIp) : _pSocket(NULL), _uAddr(uAddr), _uPort(uPort) { _initClient(uMaxDataSize, ucTTL, uInterfaceIp ); } int BldClientTest::_initClient( unsigned int uMaxDataSize, unsigned char ucTTL, unsigned int uInterfaceIp) { if ( _pSocket != NULL) delete _pSocket; _pSocket = new Client(0, uMaxDataSize); _pSocket->multicastSetTTL(ucTTL); if ( uInterfaceIp != 0) { printf( "multicast interface IP: %s\n", EpicsBld::addressToStr(uInterfaceIp).c_str() ); _pSocket->multicastSetInterface(uInterfaceIp); } return 0; } int BldClientTest::_initClient( unsigned int uMaxDataSize, unsigned char ucTTL, char* sInterfaceIp) { if ( _pSocket != NULL) delete _pSocket; _pSocket = new Client(0, uMaxDataSize); _pSocket->multicastSetTTL(ucTTL); in_addr_t uiInterface; if (sInterfaceIp == NULL || sInterfaceIp[0] == 0 ) { uiInterface = 0; } else { if ( sInterfaceIp[0] < '0' || sInterfaceIp[0] > '9' ) { struct ifreq ifr; strcpy( ifr.ifr_name, sInterfaceIp ); int iError = ioctl( _pSocket->getSocket(), SIOCGIFADDR, (char*)&ifr ); if ( iError == 0 ) uiInterface = ntohl( *(unsigned int*) &(ifr.ifr_addr.sa_data[2]) ); else { printf( "Cannot get IP address from network interface %s\n", sInterfaceIp ); uiInterface = 0; } } else { uiInterface = ntohl(inet_addr(sInterfaceIp)); } } if ( uiInterface != 0) { printf( "multicast interface IP: %s\n", EpicsBld::addressToStr(uiInterface).c_str() ); _pSocket->multicastSetInterface(uiInterface); } return 0; } BldClientTest::~BldClientTest() { if ( _pSocket != NULL) delete _pSocket; } int BldClientTest::sendRawData(int iSizeData, const char* pData) { Ins insDst( _uAddr, _uPort ); return _pSocket->send(NULL, pData, iSizeData, insDst); } } // namespace EpicsBld #include #include /* This is the command, which the vxWorks shell will call directly */ void bldClient(char *name) { char seps[] = " ,\t\n"; char *token[10]; int count=0; /* Establish string and get the first token: */ token[count] = strtok( name, seps ); while( token[count] != NULL ) { /* While there are tokens in "string" */ count++; /* Get next token: */ token[count] = strtok( NULL, seps ); } bldClientTest(count, token); } /* Information needed by iocsh */ static iocshArg bldClientArg0 = {"name", iocshArgString}; static iocshArg *bldClientArgs[] = {&bldClientArg0}; static iocshFuncDef bldClientFuncDef = {"bldClient", 1, bldClientArgs}; /* Wrapper called by iocsh, selects the argument types that bldClient needs */ static void bldClientCallFunc(const iocshArgBuf *args) { bldClient(args[0].sval); } /* Registration routine, runs at startup */ static void bldClientRegister(void) { iocshRegister(&bldClientFuncDef, bldClientCallFunc); } epicsExportRegistrar(bldClientRegister);