service.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <netcomm/service_discovery/service.h>
00025 #include <netcomm/utils/resolver.h>
00026 #include <core/exceptions/system.h>
00027
00028 #include <sys/types.h>
00029 #include <arpa/inet.h>
00030 #include <netinet/in.h>
00031 #include <inttypes.h>
00032 #include <cstddef>
00033 #include <cstring>
00034 #include <cstdlib>
00035 #include <cstdarg>
00036 #include <cstdio>
00037
00038 namespace fawkes {
00039 #if 0
00040 }
00041 #endif
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060 NetworkService::NetworkService(const char *name,
00061 const char *type,
00062 const char *domain,
00063 const char *host,
00064 unsigned short int port)
00065 {
00066 _name = strdup(name);
00067 _type = strdup(type);
00068 _domain = strdup(domain);
00069 _host = strdup(host);
00070 _port = port;
00071
00072 _addr = NULL;
00073 _addr_size = 0;
00074 }
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088 NetworkService::NetworkService(const char *name,
00089 const char *type,
00090 const char *domain,
00091 const char *host,
00092 unsigned short int port,
00093 const struct sockaddr *addr,
00094 const socklen_t addr_size,
00095 std::list<std::string> &txt)
00096
00097 {
00098 _name = strdup(name);
00099 _type = strdup(type);
00100 _domain = strdup(domain);
00101 _host = strdup(host);
00102 _port = port;
00103
00104 _addr = (struct sockaddr *)malloc(addr_size);
00105 memcpy(_addr, addr, addr_size);
00106 _addr_size = addr_size;
00107 list = txt;
00108 }
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119 NetworkService::NetworkService(const char *name,
00120 const char *type,
00121 unsigned short int port)
00122 {
00123 _name = strdup(name);
00124 _type = strdup(type);
00125 _domain = NULL;
00126 _host = NULL;
00127 _port = port;
00128
00129 _addr = NULL;
00130 _addr_size = 0;
00131 }
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146 NetworkService::NetworkService(NetworkNameResolver *nnresolver,
00147 const char *name,
00148 const char *type,
00149 unsigned short int port)
00150 {
00151 std::string s = name;
00152 std::string::size_type hpos = s.find("%h");
00153 if (nnresolver && (hpos != std::string::npos)) {
00154 s.replace(hpos, 2, nnresolver->short_hostname());
00155 }
00156 _name = strdup(s.c_str());
00157 _type = strdup(type);
00158 _domain = NULL;
00159 _host = NULL;
00160 _port = port;
00161
00162 _addr = NULL;
00163 _addr_size = 0;
00164 }
00165
00166
00167
00168
00169
00170
00171
00172 NetworkService::NetworkService(const char *name,
00173 const char *type,
00174 const char *domain)
00175 {
00176 _name = strdup(name);
00177 _type = strdup(type);
00178 _domain = strdup(domain);
00179
00180 _host = NULL;
00181 _port = 0;
00182 _addr = NULL;
00183 _addr_size = 0;
00184 }
00185
00186
00187
00188 NetworkService::~NetworkService()
00189 {
00190 if ( _name != NULL) free( _name );
00191 if ( _type != NULL) free( _type );
00192 if ( _domain != NULL) free( _domain );
00193 if ( _host != NULL) free( _host );
00194 if ( _addr != NULL) free( _addr );
00195 }
00196
00197
00198
00199
00200
00201
00202 NetworkService::NetworkService(const NetworkService *s)
00203 {
00204 _name = strdup(s->_name);
00205 _type = strdup(s->_type);
00206 _port = s->_port;
00207 if ( s->_domain != NULL ) {
00208 _domain = strdup(s->_domain);
00209 } else {
00210 _domain = NULL;
00211 }
00212 if ( s->_host != NULL ) {
00213 _host = strdup(s->_host);
00214 } else {
00215 _host = NULL;
00216 }
00217
00218 _addr = NULL;
00219 _addr_size = 0;
00220
00221 list = s->list;
00222 }
00223
00224
00225
00226
00227
00228
00229 NetworkService::NetworkService(const NetworkService &s)
00230 {
00231 _name = strdup(s._name);
00232 _type = strdup(s._type);
00233 _port = s._port;
00234 if ( s._domain != NULL ) {
00235 _domain = strdup(s._domain);
00236 } else {
00237 _domain = NULL;
00238 }
00239 if ( s._host != NULL ) {
00240 _host = strdup(s._host);
00241 } else {
00242 _host = NULL;
00243 }
00244
00245 _addr = NULL;
00246 _addr_size = 0;
00247
00248 list = s.list;
00249 }
00250
00251
00252
00253
00254
00255
00256 void
00257 NetworkService::add_txt(const char *format, ...)
00258 {
00259 va_list arg;
00260 va_start(arg, format);
00261 char *tmp;
00262 if (vasprintf(&tmp, format, arg) == -1) {
00263 throw OutOfMemoryException("Cannot add txt record, no memory");
00264 }
00265 list.push_back(tmp);
00266 free(tmp);
00267 va_end(arg);
00268 }
00269
00270
00271
00272
00273
00274 void
00275 NetworkService::set_txt(std::list<std::string> &txtlist)
00276 {
00277 list = txtlist;
00278 }
00279
00280
00281
00282
00283
00284 void
00285 NetworkService::set_name(const char *new_name)
00286 {
00287 free( _name );
00288 _name = strdup(new_name);
00289 }
00290
00291
00292
00293
00294
00295 const char *
00296 NetworkService::name() const
00297 {
00298 return _name;
00299 }
00300
00301
00302
00303
00304
00305 const char *
00306 NetworkService::type() const
00307 {
00308 return _type;
00309 }
00310
00311
00312
00313
00314
00315 const char *
00316 NetworkService::domain() const
00317 {
00318 return _domain;
00319 }
00320
00321
00322
00323
00324
00325 const char *
00326 NetworkService::host() const
00327 {
00328 return _host;
00329 }
00330
00331
00332
00333
00334
00335 unsigned short int
00336 NetworkService::port() const
00337 {
00338 return _port;
00339 }
00340
00341
00342
00343
00344
00345
00346 std::string
00347 NetworkService::addr_string() const
00348 {
00349 char ipaddr[INET_ADDRSTRLEN];
00350 struct sockaddr_in *saddr = (struct sockaddr_in *)_addr;
00351 return std::string(inet_ntop(AF_INET, &(saddr->sin_addr), ipaddr, sizeof(ipaddr)));
00352 }
00353
00354
00355
00356
00357
00358 const std::list<std::string> &
00359 NetworkService::txt() const
00360 {
00361 return list;
00362 }
00363
00364
00365
00366
00367
00368
00369 bool
00370 NetworkService::operator==(const NetworkService &s) const
00371 {
00372 return ( (strcmp(_name, s._name) == 0) &&
00373 (strcmp(_type, s._type) == 0) );
00374 }
00375
00376
00377
00378
00379
00380
00381 bool
00382 NetworkService::operator==(const NetworkService *s) const
00383 {
00384 return ( (strcmp(_name, s->_name) == 0) &&
00385 (strcmp(_type, s->_type) == 0) );
00386 }
00387
00388
00389
00390
00391
00392
00393
00394 bool
00395 NetworkService::operator<(const NetworkService &s) const
00396 {
00397 int typediff = strcmp(_type, s._type);
00398 if ( typediff == 0 ) {
00399 return (strcmp(_name, s._name) < 0);
00400 } else {
00401 return (typediff < 0);
00402 }
00403 }
00404
00405 }