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
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048 extern "C" {
00049 #include <netdb.h>
00050 #include <sys/socket.h>
00051 #include <netinet/in.h>
00052 #include <sys/un.h>
00053 #include <signal.h>
00054 };
00055
00056 #include <sstream>
00057
00058 #include "consmgr.h"
00059 #include "cmconfig.h"
00060 #include "message.h"
00061 #include "outlet.h"
00062
00063 #if defined(__bsdi__) && (_BSDI_VERSION < 200100)
00064 #define BROKEN_GETNAMEINFO
00065 #include <sys/types.h>
00066 #include <netinet/in.h>
00067 #include <arpa/inet.h>
00068 #endif
00069
00074 struct sockaddr *
00075 build_addr(Config &conf)
00076 {
00077
00078
00079 if ((conf.find("host") == conf.end()) ||
00080 (conf.find("port") == conf.end()))
00081 throw ConfigException("Configuration error. Both 'host' and " \
00082 "'port' must be specified in the configuration entry.");
00083
00084
00085 struct sockaddr *sa = (struct sockaddr*)new struct sockaddr_storage;
00086 memset(sa, 0, sizeof(struct sockaddr_storage));
00087
00088 struct sockaddr_in *sin = (struct sockaddr_in*)sa;
00089 #ifdef INET6
00090 struct sockaddr_in6 *sin6 = (struct sockaddr_in6*)sa;
00091 #endif
00092
00093 try {
00094
00095 #ifndef BROKEN_GETNAMEINFO
00096 const string host(conf["host"]);
00097 struct hostent *he = gethostbyname(host.c_str());
00098 void *addr;
00099
00100 if (he == NULL)
00101 throw "Unable to find an address for host '" + host + "'";
00102
00103 switch (he->h_addrtype) {
00104 case AF_INET:
00105 # ifndef NO_SA_LEN
00106 sin->sin_len = sizeof(struct sockaddr_in);
00107 # endif
00108 addr = &sin->sin_addr;
00109 break;
00110 # ifdef INET6
00111 case AF_INET6:
00112 # ifndef NO_SA_LEN
00113 sin6->sin6_len = sizeof(struct sockaddr_in6);
00114 # endif
00115 addr = &sin6->sin6_addr;
00116 break;
00117 # endif
00118 default:
00119 throw "Unrecognized addrtype for host '" + host + "'";
00120 }
00121 memmove(addr, he->h_addr_list[0], he->h_length);
00122 sa->sa_family = he->h_addrtype;
00123 #else
00124 # ifdef INET6
00125 if (inet_pton(AF_INET6, host.c_str(), &sin6->sin6_addr) != 1)
00126 # endif
00127 {
00128 int ret = inet_pton(AF_INET, host.c_str(), &sin->sin_addr);
00129 int eno = errno;
00130
00131 switch (ret) {
00132 case 1:
00133 break;
00134 case 0:
00135 throw "Cannot parse address ('" + host + "')";
00136 case -1:
00137 std::ostringstream ostr;
00138
00139 ostr << "System error " << eno << " (" << strerror(eno)
00140 << ") from inet_pton(" << host << ")";
00141 throw ostr.str();
00142 }
00143 }
00144 #endif
00145
00146
00147
00148 sin->sin_port = htons(short(strtol(conf["port"].c_str(), NULL, 0)));
00149
00150 } catch (...) {
00151 delete sa;
00152 throw;
00153 }
00154
00155 return(reinterpret_cast<struct sockaddr*>(sa));
00156 }
00157
00158 #ifdef NO_SA_LEN
00159
00160 socklen_t
00161 build_addrlen(const sockaddr *addr)
00162 {
00163 socklen_t len;
00164
00165 switch (addr->sa_family) {
00166 case AF_INET:
00167 len = sizeof(struct sockaddr_in);
00168 break;
00169 # ifdef INET6
00170 case AF_INET6:
00171 len = sizeof(struct sockaddr_in6);
00172 break;
00173 # endif
00174 case AF_UNIX:
00175 len = sizeof(struct sockaddr_un);
00176 break;
00177 }
00178
00179 return(len);
00180 }
00181 #endif
00182
00183 void
00184 mutex_info(std::ostream &out, pthread_mutex_t *mutex)
00185 {
00186 #ifdef __linux__
00187 out << pthread_self() << " looking at a mutex " << mutex << endl;
00188 out << pthread_self() << " __lock: " << mutex->__data.__lock << endl;
00189 out << pthread_self() << " __count: " << mutex->__data.__count << endl;
00190 out << pthread_self() << " __owner: " << mutex->__data.__owner << endl;
00191 out << pthread_self() << " __kind: " << mutex->__data.__kind << endl;
00192 out << pthread_self() << " __nusers: " << mutex->__data.__nusers << endl;
00193 #endif
00194 return;
00195 }
00196