PCMNetwork.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include "../include/PCMNetwork.h"
00030
00031 namespace ParCompMark
00032 {
00033
00034
00035
00036
00037
00038 Container < Network::IfConf, DummyLock >::Pointer Network::mIfConfs;
00039 unsigned short Network::mCommunicationPort = 1234;
00040 unsigned short Network::mBroadcastPort = 1235;
00041
00042
00043
00044
00045
00046 Network::Network(const std::string & name):
00047
00048 Thread(name
00049 )
00050
00051
00052
00053
00054
00055
00056
00057
00058 {
00059
00060 mInitialized = false;
00061 mStreamSocket = -1;
00062 mBroadcastSocket = -1;
00063 mOwnIP = "";
00064
00065 Logger::getInstance()->log(Logger::NOTICE, "Network created.");
00066
00067 }
00068
00069
00070
00071 Network::~Network()
00072 {
00073 }
00074
00075
00076
00077
00078
00079
00080
00081 void Network::getIP()
00082 {
00083
00084 if(mIfConfs.isNull())
00085 {
00086
00087 mIfConfs = new Container < IfConf, DummyLock >;
00088
00089 } else
00090 {
00091 mIfConfs.kill();
00092 mIfConfs = new Container < IfConf, DummyLock >;
00093 }
00094
00095 int n,
00096 sock,
00097 max;
00098
00099 max = 4;
00100 int buffSize = max * sizeof(struct ifreq);
00101 struct ifreq *ifr;
00102 struct ifconf ifc;
00103
00104
00105 char *buff = new char[buffSize],
00106 data[16];
00107
00108 bool enough = false;
00109
00110 sock = socket(AF_INET, SOCK_STREAM, 0);
00111
00112 unsigned char *d;
00113
00114 do
00115 {
00116
00117 ifc.ifc_buf = buff;
00118 ifc.ifc_len = buffSize;
00119 ioctl(sock, SIOCGIFCONF, &ifc);
00120 n = ifc.ifc_len / sizeof(struct ifreq);
00121
00122 if(n >= max)
00123 {
00124 delete buff;
00125
00126 max = max + 2;
00127 buffSize = max * sizeof(struct ifreq);
00128 buff = new char[buffSize];
00129 } else
00130 {
00131 enough = true;
00132 }
00133
00134 } while(!enough);
00135
00136 for(ifr = ifc.ifc_req; n > 0; n--, ifr++)
00137 {
00138 if(ifr->ifr_addr.sa_family == AF_INET)
00139 {
00140 std::string name = ifr->ifr_name;
00141 IfConf::Pointer c(new IfConf);
00142
00143 d = (unsigned char *) ifr->ifr_ifru.ifru_addr.sa_data + 2;
00144 snprintf(data, 16, "%i.%i.%i.%i", d[0], d[1], d[2], d[3]);
00145 c->IP = data;
00146
00147 ioctl(sock, SIOCGIFBRDADDR, ifr);
00148 d = (unsigned char *) ifr->ifr_ifru.ifru_broadaddr.sa_data + 2;
00149 snprintf(data, 16, "%i.%i.%i.%i", d[0], d[1], d[2], d[3]);
00150 c->BroadcastIP = data;
00151
00152 ioctl(sock, SIOCGIFNETMASK, ifr);
00153 d = (unsigned char *) ifr->ifr_ifru.ifru_broadaddr.sa_data + 2;
00154 snprintf(data, 16, "%i.%i.%i.%i", d[0], d[1], d[2], d[3]);
00155 c->Netmask = data;
00156
00157 mIfConfs->add(name, c);
00158
00159 }
00160 }
00161
00162 close(sock);
00163
00164 }
00165
00166
00167
00168 std::string Network::getHostName()
00169 {
00170 char name[32];
00171
00172 gethostname(name, 32);
00173
00174 std::string result;
00175
00176 result = name;
00177
00178 return result;
00179 }
00180
00181
00182
00183 }