service.cpp

00001 
00002 /***************************************************************************
00003  *  service.cpp - Network service representation
00004  *
00005  *  Generated: Tue Nov 07 18:02:23 2006
00006  *  Copyright  2006-2007  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009 
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
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 /* just to make Emacs auto-indent happy */
00040 }
00041 #endif
00042 
00043 /** @class NetworkService <netcomm/service_discovery/service.h>
00044  * Representation of a service announced or found via service
00045  * discovery (i.e. mDNS/DNS-SD via Avahi).
00046  * This class is used in the C++ wrapper to talk about services.
00047  *
00048  * @ingroup NetComm
00049  * @author Tim Niemueller
00050  */
00051 
00052 /** Constructor.
00053  * This constructor sets all parameters.
00054  * @param name name of service
00055  * @param type type of service
00056  * @param domain domain of service
00057  * @param host host of service
00058  * @param port port of service
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 /** Constructor.
00078  * This constructor sets all parameters.
00079  * @param name name of service
00080  * @param type type of service
00081  * @param domain domain of service
00082  * @param host host of service
00083  * @param port port of service
00084  * @param addr address of the service
00085  * @param addr_size size in bytes of addr parameter
00086  * @param txt list of TXT records
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 /** Constructor.
00112  * This constructor sets all parameters. Host and domain are the
00113  * default values, which means local host name in domain .local
00114  * (if not set otherwise in Avahi system configuration).
00115  * @param name name of service
00116  * @param type type of service
00117  * @param port port of service
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 /** Constructor.
00135  * This constructor sets all parameters. Host and domain are the
00136  * default values, which means local host name in domain .local
00137  * (if not set otherwise in Avahi system configuration).
00138  * This specific constructor allows the usage of a "%h" token in
00139  * the name, which is replaced with the short hostname.
00140  * @param nnresolver network name resolver to get the host from for
00141  * the replacement of a %h token.
00142  * @param name name of service
00143  * @param type type of service
00144  * @param port port of service
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 /** Constructor.
00167  * This constructor sets all parameters.
00168  * @param name name of service
00169  * @param type type of service
00170  * @param domain domain of service
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 /** Destructor. */
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 /** Copy constructor (pointer).
00199  * Create a copy of given NetworkService.
00200  * @param s network service to copy from
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 /** Copy constructor (reference).
00226  * Create a copy of given NetworkService.
00227  * @param s network service to copy from
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 /** Add a TXT record.
00253  * @param format format for TXT record to add, must be a "key=value" string,
00254  * takes the same arguments as sprintf.
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 /** Set TXT records all at once.
00272  * @param txtlist list of TXT records
00273  */
00274 void
00275 NetworkService::set_txt(std::list<std::string> &txtlist)
00276 {
00277   list = txtlist;
00278 }
00279 
00280 
00281 /** Set name of service.
00282  * @param new_name new name
00283  */
00284 void
00285 NetworkService::set_name(const char *new_name)
00286 {
00287   free( _name );
00288   _name = strdup(new_name);
00289 }
00290 
00291 
00292 /** Get name of service.
00293  * @return name of service
00294  */
00295 const char *
00296 NetworkService::name() const
00297 {
00298   return _name;
00299 }
00300 
00301 
00302 /** Get type of service.
00303  * @return type of service
00304  */
00305 const char *
00306 NetworkService::type() const
00307 {
00308   return _type;
00309 }
00310 
00311 
00312 /** Get domain of service.
00313  * @return domain of service
00314  */
00315 const char *
00316 NetworkService::domain() const
00317 {
00318   return _domain;
00319 }
00320 
00321 
00322 /** Get host of service.
00323  * @return host of service
00324  */
00325 const char *
00326 NetworkService::host() const
00327 {
00328   return _host;
00329 }
00330 
00331 
00332 /** Get port of service.
00333  * @return port of service
00334  */
00335 unsigned short int
00336 NetworkService::port() const
00337 {
00338   return _port;
00339 }
00340 
00341 
00342 /** Get IP address of entry as string.
00343  * @return IP address as string
00344  * @exception NullPointerException thrown if the address has not been set
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 /** Get TXT record list of service.
00356  * @return TXT record list of service
00357  */
00358 const std::list<std::string> &
00359 NetworkService::txt() const
00360 {
00361   return list;
00362 }
00363 
00364 
00365 /** Equal operator for NetworkService reference.
00366  * @param s reference of service to compare to.
00367  * @return true, if the services are the same (same name and type), false otherwise
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 /** Equal operator for NetworkService pointer.
00378  * @param s pointer to service to compare to.
00379  * @return true, if the services are the same (same name and type), false otherwise
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 /** Less than operator.
00390  * @param s reference of service to compare to
00391  * @return true, if either the type is less than (according to strcmp) or if types
00392  * are equal if the service name is less than the given service's name.
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 } // end namespace fawkes

Generated on Tue Feb 22 13:32:28 2011 for Fawkes API by  doxygen 1.4.7