00001 #include "otsdaq-core/NetworkUtilities/Socket.h"
00002 #include "otsdaq-core/MessageFacility/MessageFacility.h"
00003 #include "otsdaq-core/Macros/CoutHeaderMacros.h"
00004
00005 #include <iostream>
00006 #include <cassert>
00007 #include <sstream>
00008
00009 #include <unistd.h>
00010 #include <arpa/inet.h>
00011
00012 #include <netdb.h>
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <cstring>
00022
00023
00024 using namespace ots;
00025
00026
00027
00028 Socket::Socket(void)
00029 {
00030 __SS__ << "ERROR: This method should never be called. There is something wrong in your inheritance scheme!" << std::endl;
00031 __MOUT__ << "\n" << ss.str();
00032
00033
00034 }
00035
00036
00037 Socket::Socket(const std::string &IPAddress, unsigned int port)
00038 : socketNumber_(-1)
00039 , IPAddress_ (IPAddress)
00040 , port_ (port)
00041
00042 {
00043 __MOUT__ << std::endl;
00044
00045 socketAddress_.sin_family = AF_INET;
00046 socketAddress_.sin_port = htons(port);
00047
00048 __MOUT__ << "IPAddress: " << IPAddress << " port: " << port << " htons: " << socketAddress_.sin_port << std::endl;
00049 if(inet_aton(IPAddress.c_str(), &socketAddress_.sin_addr) == 0)
00050 {
00051 __SS__ << "FATAL: Invalid IP/Port combination. Is it already in use? " <<
00052 IPAddress << "/" << port << std::endl;
00053
00054 __MOUT__ << "\n" << ss.str();
00055 throw std::runtime_error(ss.str());
00056 }
00057
00058 memset(&(socketAddress_.sin_zero), '\0', 8);
00059 }
00060
00061
00062 Socket::~Socket(void)
00063 {
00064 __MOUT__ << "CLOSING THE SOCKET #" << socketNumber_ << " IP: " << IPAddress_ << " port: " << port_ << " htons: " << socketAddress_.sin_port << std::endl;
00065 if(socketNumber_ != -1)
00066 close(socketNumber_);
00067 }
00068
00069
00070 void Socket::initialize(void)
00071 {
00072 __MOUT__ << " htons: " << socketAddress_.sin_port << std::endl;
00073 struct addrinfo hints;
00074 struct addrinfo* res;
00075 int status = 0;
00076
00077
00078 memset(&hints, 0, sizeof hints);
00079 hints.ai_family = AF_INET;
00080 hints.ai_socktype = SOCK_DGRAM;
00081 hints.ai_flags = AI_PASSIVE;
00082
00083 bool socketInitialized = false;
00084 int fromPort = FirstSocketPort;
00085 int toPort = LastSocketPort;
00086
00087 if(ntohs(socketAddress_.sin_port) != 0)
00088 fromPort = toPort = ntohs(socketAddress_.sin_port);
00089
00090 std::stringstream port;
00091
00092 for(int p=fromPort; p<=toPort && !socketInitialized; p++)
00093 {
00094 port.str("");
00095 port << p;
00096 std::cout << __COUT_HDR_FL__ << __LINE__ << "]\tBinding port: " << port.str() << std::endl;
00097 socketAddress_.sin_port = htons(p);
00098
00099 if((status = getaddrinfo(NULL, port.str().c_str(), &hints, &res)) != 0)
00100 {
00101 std::cout << __COUT_HDR_FL__ << __LINE__ << "]\tGetaddrinfo error status: " << status << std::endl;
00102 continue;
00103 }
00104
00105
00106 socketNumber_ = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
00107
00108 std::cout << __COUT_HDR_FL__ << __LINE__ << "]\tSocket Number: " << socketNumber_ << " for port: " << ntohs(socketAddress_.sin_port) << " initialized." << std::endl;
00109
00110 if(bind(socketNumber_, res->ai_addr, res->ai_addrlen) == -1)
00111 {
00112 std::cout << __COUT_HDR_FL__ << "Error********Error********Error********Error********Error********Error" << std::endl;
00113 std::cout << __COUT_HDR_FL__ << "FAILED BIND FOR PORT: " << port.str() << " ON IP: " << IPAddress_ << std::endl;
00114 std::cout << __COUT_HDR_FL__ << "Error********Error********Error********Error********Error********Error" << std::endl;
00115 socketNumber_ = 0;
00116 }
00117 else
00118 {
00119 std::cout << __COUT_HDR_FL__ << ":):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)" << std::endl;
00120 std::cout << __COUT_HDR_FL__ << ":):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)" << std::endl;
00121 std::cout << __COUT_HDR_FL__ << "SOCKET ON PORT: " << port.str() << " ON IP: " << IPAddress_ << " INITIALIZED OK!" << std::endl;
00122 std::cout << __COUT_HDR_FL__ << ":):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)" << std::endl;
00123 std::cout << __COUT_HDR_FL__ << ":):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)" << std::endl;
00124 char yes = '1';
00125 setsockopt(socketNumber_,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int));
00126 socketInitialized = true;
00127 std::cout << __COUT_HDR_FL__ << __LINE__ << "]\tSocket Number: " << socketNumber_ << " for port: " << ntohs(socketAddress_.sin_port) << " initialized." << std::endl;
00128 }
00129
00130 freeaddrinfo(res);
00131 }
00132
00133 if(!socketInitialized)
00134 {
00135 std::stringstream ss;
00136 ss << __COUT_HDR_FL__ << __LINE__ << "FATAL: Socket could not initialize socket." << std::endl;
00137 std::cout << ss.str();
00138
00139 }
00140 }
00141
00142
00143 const struct sockaddr_in& Socket::getSocketAddress(void)
00144 {
00145 return socketAddress_;
00146 }
00147