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
00030
00031
00032
00033
00034
00035
00036
00037 #include "Poco/Environment_UNIX.h"
00038 #include "Poco/Exception.h"
00039 #include <cstring>
00040 #include <unistd.h>
00041 #include <stdlib.h>
00042 #include <sys/utsname.h>
00043 #include <sys/param.h>
00044 #include <cstring>
00045
00046
00047 namespace Poco {
00048
00049
00050 EnvironmentImpl::StringMap EnvironmentImpl::_map;
00051 FastMutex EnvironmentImpl::_mutex;
00052
00053
00054 std::string EnvironmentImpl::getImpl(const std::string& name)
00055 {
00056 FastMutex::ScopedLock lock(_mutex);
00057
00058 const char* val = getenv(name.c_str());
00059 if (val)
00060 return std::string(val);
00061 else
00062 throw NotFoundException(name);
00063 }
00064
00065
00066 bool EnvironmentImpl::hasImpl(const std::string& name)
00067 {
00068 FastMutex::ScopedLock lock(_mutex);
00069
00070 return getenv(name.c_str()) != 0;
00071 }
00072
00073
00074 void EnvironmentImpl::setImpl(const std::string& name, const std::string& value)
00075 {
00076 FastMutex::ScopedLock lock(_mutex);
00077
00078 std::string var = name;
00079 var.append("=");
00080 var.append(value);
00081 _map[name] = var;
00082 if (putenv((char*) _map[name].c_str()))
00083 {
00084 std::string msg = "cannot set environment variable: ";
00085 msg.append(name);
00086 throw SystemException(msg);
00087 }
00088 }
00089
00090
00091 std::string EnvironmentImpl::osNameImpl()
00092 {
00093 struct utsname uts;
00094 uname(&uts);
00095 return uts.sysname;
00096 }
00097
00098
00099 std::string EnvironmentImpl::osVersionImpl()
00100 {
00101 struct utsname uts;
00102 uname(&uts);
00103 return uts.release;
00104 }
00105
00106
00107 std::string EnvironmentImpl::osArchitectureImpl()
00108 {
00109 struct utsname uts;
00110 uname(&uts);
00111 return uts.machine;
00112 }
00113
00114
00115 std::string EnvironmentImpl::nodeNameImpl()
00116 {
00117 struct utsname uts;
00118 uname(&uts);
00119 return uts.nodename;
00120 }
00121
00122
00123 }
00124
00125
00126
00127
00128
00129 #if defined(POCO_OS_FAMILY_BSD) || POCO_OS == POCO_OS_QNX
00130
00131
00132
00133 #include <sys/types.h>
00134 #include <sys/socket.h>
00135 #include <ifaddrs.h>
00136 #include <net/if_dl.h>
00137
00138
00139 namespace Poco {
00140
00141
00142 void EnvironmentImpl::nodeIdImpl(NodeId& id)
00143 {
00144 struct ifaddrs* ifaphead;
00145 int rc = getifaddrs(&ifaphead);
00146 if (rc) throw SystemException("cannot get network adapter list");
00147
00148 bool foundAdapter = false;
00149 for (struct ifaddrs* ifap = ifaphead; ifap; ifap = ifap->ifa_next)
00150 {
00151 if (ifap->ifa_addr && ifap->ifa_addr->sa_family == AF_LINK)
00152 {
00153 struct sockaddr_dl* sdl = reinterpret_cast<struct sockaddr_dl*>(ifap->ifa_addr);
00154 caddr_t ap = (caddr_t) (sdl->sdl_data + sdl->sdl_nlen);
00155 int alen = sdl->sdl_alen;
00156 if (ap && alen > 0)
00157 {
00158 std::memcpy(&id, ap, sizeof(id));
00159 foundAdapter = true;
00160 break;
00161 }
00162 }
00163 }
00164 freeifaddrs(ifaphead);
00165 if (!foundAdapter) throw SystemException("cannot determine MAC address (no suitable network adapter found)");
00166 }
00167
00168
00169 }
00170
00171
00172 #elif defined(__CYGWIN__) || POCO_OS == POCO_OS_LINUX
00173
00174
00175
00176 #include <sys/ioctl.h>
00177 #include <sys/socket.h>
00178 #include <netinet/in.h>
00179 #include <net/if.h>
00180 #include <arpa/inet.h>
00181 #include <unistd.h>
00182
00183
00184 namespace Poco {
00185
00186
00187 void EnvironmentImpl::nodeIdImpl(NodeId& id)
00188 {
00189 struct ifreq ifr;
00190
00191 int s = socket(PF_INET, SOCK_DGRAM, 0);
00192 if (s == -1) throw SystemException("cannot open socket");
00193
00194 std::strcpy(ifr.ifr_name, "eth0");
00195 int rc = ioctl(s, SIOCGIFHWADDR, &ifr);
00196 close(s);
00197 if (rc < 0) throw SystemException("cannot get MAC address");
00198 struct sockaddr* sa = reinterpret_cast<struct sockaddr*>(&ifr.ifr_addr);
00199 std::memcpy(&id, sa->sa_data, sizeof(id));
00200 }
00201
00202
00203 }
00204
00205
00206 #elif defined(POCO_OS_FAMILY_UNIX)
00207
00208
00209
00210 #include <sys/ioctl.h>
00211 #if defined(sun) || defined(__sun)
00212 #include <sys/sockio.h>
00213 #endif
00214 #include <sys/socket.h>
00215 #include <sys/types.h>
00216 #include <netinet/in.h>
00217 #include <net/if.h>
00218 #include <arpa/inet.h>
00219 #include <netdb.h>
00220 #include <net/if.h>
00221 #include <net/if_arp.h>
00222 #include <unistd.h>
00223
00224
00225 namespace Poco {
00226
00227
00228 void EnvironmentImpl::nodeIdImpl(NodeId& id)
00229 {
00230 char name[MAXHOSTNAMELEN];
00231 if (gethostname(name, sizeof(name)))
00232 throw SystemException("cannot get host name");
00233
00234 struct hostent* pHost = gethostbyname(name);
00235 if (!pHost) throw SystemException("cannot get host IP address");
00236
00237 int s = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
00238 if (s == -1) throw SystemException("cannot open socket");
00239
00240 struct arpreq ar;
00241 std::memset(&ar, 0, sizeof(ar));
00242 struct sockaddr_in* pAddr = reinterpret_cast<struct sockaddr_in*>(&ar.arp_pa);
00243 pAddr->sin_family = AF_INET;
00244 std::memcpy(&pAddr->sin_addr, *pHost->h_addr_list, sizeof(struct in_addr));
00245 int rc = ioctl(s, SIOCGARP, &ar);
00246 close(s);
00247 if (rc < 0) throw SystemException("cannot get MAC address");
00248 std::memcpy(&id, ar.arp_ha.sa_data, sizeof(id));
00249 }
00250
00251
00252 }
00253
00254
00255 #endif