rendezvous.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001-2004 by Alexander Barton (alex@barton.de)
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. * Please read the file COPYING, README and AUTHORS for more information.
  10. *
  11. * Rendezvous service registration.
  12. *
  13. * Supported APIs are:
  14. * - Apple Mac OS X
  15. * - Howl
  16. */
  17. #include "portab.h"
  18. #ifdef ZEROCONF
  19. static char UNUSED id[] = "$Id: rendezvous.c,v 1.8 2006/05/10 21:24:01 alex Exp $";
  20. #include "imp.h"
  21. #include <assert.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #ifdef HAVE_MACH_PORT_H
  25. #include "mach/port.h"
  26. #include "mach/message.h"
  27. #endif
  28. #ifdef HAVE_DNSSERVICEDISCOVERY_DNSSERVICEDISCOVERY_H
  29. #include <DNSServiceDiscovery/DNSServiceDiscovery.h>
  30. #endif
  31. #ifdef HAVE_RENDEZVOUS_RENDEZVOUS_H
  32. #include <rendezvous/rendezvous.h>
  33. #endif
  34. #include "defines.h"
  35. #include "log.h"
  36. #include "exp.h"
  37. #include "rendezvous.h"
  38. #if defined(HAVE_DNSSERVICEREGISTRATIONCREATE)
  39. # define APPLE
  40. #elif defined(HAVE_SW_DISCOVERY_INIT)
  41. # define HOWL
  42. #else
  43. # error "Can't detect Rendezvous API!?"
  44. #endif
  45. #define MAX_RENDEZVOUS 1000
  46. typedef struct _service
  47. {
  48. char Desc[CLIENT_ID_LEN];
  49. #ifdef APPLE
  50. dns_service_discovery_ref Discovery_Ref;
  51. mach_port_t Mach_Port;
  52. #endif
  53. #ifdef HOWL
  54. sw_discovery_oid Id;
  55. #endif
  56. } SERVICE;
  57. static SERVICE My_Rendezvous[MAX_RENDEZVOUS];
  58. static void Unregister( int Idx );
  59. /* -- Apple API -- */
  60. #ifdef APPLE
  61. #define MAX_MACH_MSG_SIZE 512
  62. static void Registration_Reply_Handler( DNSServiceRegistrationReplyErrorType ErrCode, void *Context );
  63. #endif /* Apple */
  64. /* -- Howl API -- */
  65. #ifdef HOWL
  66. static sw_discovery My_Discovery_Session = NULL;
  67. static sw_salt My_Salt;
  68. static sw_result HOWL_API Registration_Reply_Handler( sw_discovery Session, sw_discovery_publish_status Status, sw_discovery_oid Id, sw_opaque Extra );
  69. #endif /* Howl */
  70. GLOBAL void Rendezvous_Init( void )
  71. {
  72. /* Initialize structures */
  73. int i;
  74. #ifdef HOWL
  75. if( sw_discovery_init( &My_Discovery_Session ) != SW_OKAY )
  76. {
  77. Log( LOG_EMERG, "Can't initialize Rendezvous (Howl): sw_discovery_init() failed!" );
  78. Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
  79. exit( 1 );
  80. }
  81. if( sw_discovery_salt( My_Discovery_Session, &My_Salt ) != SW_OKAY )
  82. {
  83. Log( LOG_EMERG, "Can't initialize Rendezvous (Howl): sw_discovery_salt() failed!" );
  84. Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
  85. exit( 1 );
  86. }
  87. #endif
  88. for( i = 0; i < MAX_RENDEZVOUS; i++ ) My_Rendezvous[i].Desc[0] = '\0';
  89. } /* Rendezvous_Init */
  90. GLOBAL void Rendezvous_Exit( void )
  91. {
  92. /* Clean up & exit module */
  93. int i;
  94. for( i = 0; i < MAX_RENDEZVOUS; i++ )
  95. {
  96. if( My_Rendezvous[i].Desc[0] ) Unregister( i );
  97. }
  98. #ifdef HOWL
  99. sw_discovery_fina( My_Discovery_Session );
  100. #endif
  101. } /* Rendezvous_Exit */
  102. GLOBAL bool Rendezvous_Register( char *Name, char *Type, UINT16 Port )
  103. {
  104. /* Register new service */
  105. int i;
  106. /* Search free port structure */
  107. for( i = 0; i < MAX_RENDEZVOUS; i++ ) if( ! My_Rendezvous[i].Desc[0] ) break;
  108. if( i >= MAX_RENDEZVOUS )
  109. {
  110. Log( LOG_ERR, "Can't register \"%s\" with Rendezvous: limit (%d) reached!", Name, MAX_RENDEZVOUS );
  111. return false;
  112. }
  113. strlcpy( My_Rendezvous[i].Desc, Name, sizeof( My_Rendezvous[i].Desc ));
  114. #ifdef APPLE
  115. /* Register new service */
  116. My_Rendezvous[i].Discovery_Ref = DNSServiceRegistrationCreate( Name, Type, "", htonl( Port ), "", Registration_Reply_Handler, &My_Rendezvous[i] );
  117. if( ! My_Rendezvous[i].Discovery_Ref )
  118. {
  119. Log( LOG_ERR, "Can't register \"%s\" with Rendezvous: can't register service!", My_Rendezvous[i].Desc );
  120. My_Rendezvous[i].Desc[0] = '\0';
  121. return false;
  122. }
  123. /* Get and save the corresponding Mach Port */
  124. My_Rendezvous[i].Mach_Port = DNSServiceDiscoveryMachPort( My_Rendezvous[i].Discovery_Ref );
  125. if( ! My_Rendezvous[i].Mach_Port )
  126. {
  127. Log( LOG_ERR, "Can't register \"%s\" with Rendezvous: got no Mach Port!", My_Rendezvous[i].Desc );
  128. /* Here we actually leek a descriptor :-( */
  129. My_Rendezvous[i].Discovery_Ref = 0;
  130. My_Rendezvous[i].Desc[0] = '\0';
  131. return false;
  132. }
  133. #endif /* Apple */
  134. #ifdef HOWL
  135. if( sw_discovery_publish( My_Discovery_Session, 0, Name, Type, NULL, NULL, Port, NULL, 0, Registration_Reply_Handler, &My_Rendezvous[i], &My_Rendezvous[i].Id ) != SW_OKAY )
  136. {
  137. Log( LOG_ERR, "Can't register \"%s\" with Rendezvous: can't register service!", My_Rendezvous[i].Desc );
  138. My_Rendezvous[i].Desc[0] = '\0';
  139. return false;
  140. }
  141. #endif /* Howl */
  142. Log( LOG_DEBUG, "Rendezvous: Registering \"%s\" ...", My_Rendezvous[i].Desc );
  143. return true;
  144. } /* Rendezvous_Register */
  145. GLOBAL bool Rendezvous_Unregister( char *Name )
  146. {
  147. /* Unregister service from rendezvous */
  148. int i;
  149. bool ok;
  150. ok = false;
  151. for( i = 0; i < MAX_RENDEZVOUS; i++ )
  152. {
  153. if( strcmp( Name, My_Rendezvous[i].Desc ) == 0 )
  154. {
  155. Unregister( i );
  156. ok = true;
  157. }
  158. }
  159. return ok;
  160. } /* Rendezvous_Unregister */
  161. GLOBAL void Rendezvous_UnregisterListeners( void )
  162. {
  163. /* Unregister all our listening sockets from Rendezvous */
  164. int i;
  165. for( i = 0; i < MAX_RENDEZVOUS; i++ )
  166. {
  167. if( My_Rendezvous[i].Desc[0] ) Unregister( i );
  168. }
  169. } /* Rendezvous_UnregisterListeners */
  170. GLOBAL void Rendezvous_Handler( void )
  171. {
  172. /* Handle all Rendezvous stuff; this function must be called
  173. * periodically from the run loop of the main program */
  174. #ifdef APPLE
  175. int i;
  176. char buffer[MAX_MACH_MSG_SIZE];
  177. mach_msg_return_t result;
  178. mach_msg_header_t *msg;
  179. for( i = 0; i < MAX_RENDEZVOUS; i++ )
  180. {
  181. if( ! My_Rendezvous[i].Discovery_Ref ) continue;
  182. /* Read message from Mach Port */
  183. msg = (mach_msg_header_t *)buffer;
  184. result = mach_msg( msg, MACH_RCV_MSG|MACH_RCV_TIMEOUT, 0, MAX_MACH_MSG_SIZE, My_Rendezvous[i].Mach_Port, 1, 0 );
  185. /* Handle message */
  186. if( result == MACH_MSG_SUCCESS ) DNSServiceDiscovery_handleReply( msg );
  187. #ifdef DEBUG
  188. else if( result != MACH_RCV_TIMED_OUT ) Log( LOG_DEBUG, "mach_msg(): %ld", (long)result );
  189. #endif /* Debug */
  190. }
  191. #endif /* Apple */
  192. #ifdef HOWL
  193. sw_ulong msecs = 10;
  194. sw_salt_step( My_Salt, &msecs );
  195. #endif
  196. } /* Rendezvous_Handler */
  197. static void Unregister( int Idx )
  198. {
  199. /* Unregister service */
  200. #ifdef APPLE
  201. DNSServiceDiscoveryDeallocate( My_Rendezvous[Idx].Discovery_Ref );
  202. #endif /* Apple */
  203. #ifdef HOWL
  204. if( sw_discovery_cancel( My_Discovery_Session, My_Rendezvous[Idx].Id ) != SW_OKAY )
  205. {
  206. Log( LOG_ERR, "Rendezvous: Failed to unregister \"%s\"!", My_Rendezvous[Idx].Desc );
  207. return;
  208. }
  209. #endif /* Howl */
  210. Log( LOG_INFO, "Unregistered \"%s\" from Rendezvous.", My_Rendezvous[Idx].Desc );
  211. My_Rendezvous[Idx].Desc[0] = '\0';
  212. } /* Unregister */
  213. /* -- Apple API -- */
  214. #ifdef APPLE
  215. static void Registration_Reply_Handler( DNSServiceRegistrationReplyErrorType ErrCode, void *Context )
  216. {
  217. SERVICE *s = (SERVICE *)Context;
  218. char txt[50];
  219. if( ErrCode == kDNSServiceDiscoveryNoError )
  220. {
  221. /* Success! */
  222. Log( LOG_INFO, "Successfully registered \"%s\" with Rendezvous.", s->Desc );
  223. return;
  224. }
  225. switch( ErrCode )
  226. {
  227. case kDNSServiceDiscoveryAlreadyRegistered:
  228. strcpy( txt, "name already registered!" );
  229. break;
  230. case kDNSServiceDiscoveryNameConflict:
  231. strcpy( txt, "name conflict!" );
  232. break;
  233. default:
  234. snprintf(txt, sizeof txt, "error code %ld!",
  235. (long)ErrCode);
  236. }
  237. Log( LOG_INFO, "Can't register \"%s\" with Rendezvous: %s", s->Desc, txt );
  238. s->Desc[0] = '\0';
  239. } /* Registration_Reply_Handler */
  240. #endif /* Apple */
  241. /* -- Howl API -- */
  242. #ifdef HOWL
  243. static sw_result HOWL_API Registration_Reply_Handler( sw_discovery Session, sw_discovery_publish_status Status, UNUSED sw_discovery_oid Id, sw_opaque Extra )
  244. {
  245. SERVICE *s = (SERVICE *)Extra;
  246. char txt[50];
  247. assert( Session == My_Discovery_Session );
  248. assert( Extra != NULL );
  249. if( Status == SW_DISCOVERY_PUBLISH_STARTED || Status == SW_DISCOVERY_PUBLISH_STOPPED )
  250. {
  251. /* Success! */
  252. Log( LOG_INFO, "Successfully registered \"%s\" with Rendezvous.", s->Desc );
  253. return SW_OKAY;
  254. }
  255. switch( Status )
  256. {
  257. case SW_DISCOVERY_PUBLISH_NAME_COLLISION:
  258. strcpy( txt, "name conflict!" );
  259. break;
  260. default:
  261. snprintf(txt, sizeof txt, "error code %ld!",
  262. (long)Status);
  263. }
  264. Log( LOG_INFO, "Can't register \"%s\" with Rendezvous: %s", s->Desc, txt );
  265. s->Desc[0] = '\0';
  266. return SW_OKAY;
  267. } /* Registration_Reply_Handler */
  268. #endif /* Howl */
  269. #endif /* ZEROCONF */
  270. /* -eof- */