2 aa152579 2003-02-23 alex * ngIRCd -- The Next Generation IRC Daemon
3 292879ac 2004-12-26 alex * Copyright (c)2001-2004 by Alexander Barton (alex@barton.de)
5 aa152579 2003-02-23 alex * This program is free software; you can redistribute it and/or modify
6 aa152579 2003-02-23 alex * it under the terms of the GNU General Public License as published by
7 aa152579 2003-02-23 alex * the Free Software Foundation; either version 2 of the License, or
8 aa152579 2003-02-23 alex * (at your option) any later version.
9 aa152579 2003-02-23 alex * Please read the file COPYING, README and AUTHORS for more information.
11 292879ac 2004-12-26 alex * Rendezvous service registration.
13 292879ac 2004-12-26 alex * Supported APIs are:
14 292879ac 2004-12-26 alex * - Apple Mac OS X
19 aa152579 2003-02-23 alex #include "portab.h"
21 02b0a515 2005-07-08 alex #ifdef ZEROCONF
24 dd3a3bc6 2006-05-10 alex static char UNUSED id[] = "$Id: rendezvous.c,v 1.8 2006/05/10 21:24:01 alex Exp $";
26 aa152579 2003-02-23 alex #include "imp.h"
27 aa152579 2003-02-23 alex #include <assert.h>
29 aa152579 2003-02-23 alex #include <stdio.h>
30 aa152579 2003-02-23 alex #include <string.h>
32 aa152579 2003-02-23 alex #ifdef HAVE_MACH_PORT_H
33 aa152579 2003-02-23 alex #include "mach/port.h"
34 aa152579 2003-02-23 alex #include "mach/message.h"
37 aa152579 2003-02-23 alex #ifdef HAVE_DNSSERVICEDISCOVERY_DNSSERVICEDISCOVERY_H
38 aa152579 2003-02-23 alex #include <DNSServiceDiscovery/DNSServiceDiscovery.h>
41 292879ac 2004-12-26 alex #ifdef HAVE_RENDEZVOUS_RENDEZVOUS_H
42 292879ac 2004-12-26 alex #include <rendezvous/rendezvous.h>
45 aa152579 2003-02-23 alex #include "defines.h"
46 aa152579 2003-02-23 alex #include "log.h"
48 aa152579 2003-02-23 alex #include "exp.h"
49 aa152579 2003-02-23 alex #include "rendezvous.h"
52 292879ac 2004-12-26 alex #if defined(HAVE_DNSSERVICEREGISTRATIONCREATE)
53 292879ac 2004-12-26 alex # define APPLE
54 292879ac 2004-12-26 alex #elif defined(HAVE_SW_DISCOVERY_INIT)
55 292879ac 2004-12-26 alex # define HOWL
57 292879ac 2004-12-26 alex # error "Can't detect Rendezvous API!?"
61 292879ac 2004-12-26 alex #define MAX_RENDEZVOUS 1000
63 aa152579 2003-02-23 alex typedef struct _service
65 8adff592 2005-03-19 fw char Desc[CLIENT_ID_LEN];
66 292879ac 2004-12-26 alex #ifdef APPLE
67 aa152579 2003-02-23 alex dns_service_discovery_ref Discovery_Ref;
68 aa152579 2003-02-23 alex mach_port_t Mach_Port;
71 292879ac 2004-12-26 alex sw_discovery_oid Id;
75 77f54693 2005-07-31 alex static SERVICE My_Rendezvous[MAX_RENDEZVOUS];
78 77f54693 2005-07-31 alex static void Unregister( int Idx );
81 292879ac 2004-12-26 alex /* -- Apple API -- */
83 292879ac 2004-12-26 alex #ifdef APPLE
85 aa152579 2003-02-23 alex #define MAX_MACH_MSG_SIZE 512
87 77f54693 2005-07-31 alex static void Registration_Reply_Handler( DNSServiceRegistrationReplyErrorType ErrCode, void *Context );
89 292879ac 2004-12-26 alex #endif /* Apple */
92 292879ac 2004-12-26 alex /* -- Howl API -- */
96 77f54693 2005-07-31 alex static sw_discovery My_Discovery_Session = NULL;
97 77f54693 2005-07-31 alex static sw_salt My_Salt;
99 77f54693 2005-07-31 alex static sw_result HOWL_API Registration_Reply_Handler( sw_discovery Session, sw_discovery_publish_status Status, sw_discovery_oid Id, sw_opaque Extra );
101 292879ac 2004-12-26 alex #endif /* Howl */
104 8adff592 2005-03-19 fw GLOBAL void Rendezvous_Init( void )
106 aa152579 2003-02-23 alex /* Initialize structures */
110 292879ac 2004-12-26 alex #ifdef HOWL
111 292879ac 2004-12-26 alex if( sw_discovery_init( &My_Discovery_Session ) != SW_OKAY )
113 292879ac 2004-12-26 alex Log( LOG_EMERG, "Can't initialize Rendezvous (Howl): sw_discovery_init() failed!" );
114 292879ac 2004-12-26 alex Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
118 5049c5c6 2004-12-26 alex if( sw_discovery_salt( My_Discovery_Session, &My_Salt ) != SW_OKAY )
120 5049c5c6 2004-12-26 alex Log( LOG_EMERG, "Can't initialize Rendezvous (Howl): sw_discovery_salt() failed!" );
121 5049c5c6 2004-12-26 alex Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
126 292879ac 2004-12-26 alex for( i = 0; i < MAX_RENDEZVOUS; i++ ) My_Rendezvous[i].Desc[0] = '\0';
127 aa152579 2003-02-23 alex } /* Rendezvous_Init */
130 8adff592 2005-03-19 fw GLOBAL void Rendezvous_Exit( void )
132 aa152579 2003-02-23 alex /* Clean up & exit module */
136 aa152579 2003-02-23 alex for( i = 0; i < MAX_RENDEZVOUS; i++ )
138 292879ac 2004-12-26 alex if( My_Rendezvous[i].Desc[0] ) Unregister( i );
141 292879ac 2004-12-26 alex #ifdef HOWL
142 292879ac 2004-12-26 alex sw_discovery_fina( My_Discovery_Session );
144 aa152579 2003-02-23 alex } /* Rendezvous_Exit */
147 dd3a3bc6 2006-05-10 alex GLOBAL bool Rendezvous_Register( char *Name, char *Type, UINT16 Port )
149 aa152579 2003-02-23 alex /* Register new service */
153 aa152579 2003-02-23 alex /* Search free port structure */
154 292879ac 2004-12-26 alex for( i = 0; i < MAX_RENDEZVOUS; i++ ) if( ! My_Rendezvous[i].Desc[0] ) break;
155 aa152579 2003-02-23 alex if( i >= MAX_RENDEZVOUS )
157 aa152579 2003-02-23 alex Log( LOG_ERR, "Can't register \"%s\" with Rendezvous: limit (%d) reached!", Name, MAX_RENDEZVOUS );
158 8adff592 2005-03-19 fw return false;
160 aa152579 2003-02-23 alex strlcpy( My_Rendezvous[i].Desc, Name, sizeof( My_Rendezvous[i].Desc ));
162 292879ac 2004-12-26 alex #ifdef APPLE
163 aa152579 2003-02-23 alex /* Register new service */
164 292879ac 2004-12-26 alex My_Rendezvous[i].Discovery_Ref = DNSServiceRegistrationCreate( Name, Type, "", htonl( Port ), "", Registration_Reply_Handler, &My_Rendezvous[i] );
165 aa152579 2003-02-23 alex if( ! My_Rendezvous[i].Discovery_Ref )
167 aa152579 2003-02-23 alex Log( LOG_ERR, "Can't register \"%s\" with Rendezvous: can't register service!", My_Rendezvous[i].Desc );
168 292879ac 2004-12-26 alex My_Rendezvous[i].Desc[0] = '\0';
169 8adff592 2005-03-19 fw return false;
172 aa152579 2003-02-23 alex /* Get and save the corresponding Mach Port */
173 aa152579 2003-02-23 alex My_Rendezvous[i].Mach_Port = DNSServiceDiscoveryMachPort( My_Rendezvous[i].Discovery_Ref );
174 aa152579 2003-02-23 alex if( ! My_Rendezvous[i].Mach_Port )
176 aa152579 2003-02-23 alex Log( LOG_ERR, "Can't register \"%s\" with Rendezvous: got no Mach Port!", My_Rendezvous[i].Desc );
177 aa152579 2003-02-23 alex /* Here we actually leek a descriptor :-( */
178 aa152579 2003-02-23 alex My_Rendezvous[i].Discovery_Ref = 0;
179 292879ac 2004-12-26 alex My_Rendezvous[i].Desc[0] = '\0';
180 8adff592 2005-03-19 fw return false;
182 292879ac 2004-12-26 alex #endif /* Apple */
184 292879ac 2004-12-26 alex #ifdef HOWL
185 292879ac 2004-12-26 alex 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 )
187 292879ac 2004-12-26 alex Log( LOG_ERR, "Can't register \"%s\" with Rendezvous: can't register service!", My_Rendezvous[i].Desc );
188 292879ac 2004-12-26 alex My_Rendezvous[i].Desc[0] = '\0';
189 8adff592 2005-03-19 fw return false;
191 292879ac 2004-12-26 alex #endif /* Howl */
193 aa152579 2003-02-23 alex Log( LOG_DEBUG, "Rendezvous: Registering \"%s\" ...", My_Rendezvous[i].Desc );
195 aa152579 2003-02-23 alex } /* Rendezvous_Register */
198 8adff592 2005-03-19 fw GLOBAL bool Rendezvous_Unregister( char *Name )
200 aa152579 2003-02-23 alex /* Unregister service from rendezvous */
206 aa152579 2003-02-23 alex for( i = 0; i < MAX_RENDEZVOUS; i++ )
208 aa152579 2003-02-23 alex if( strcmp( Name, My_Rendezvous[i].Desc ) == 0 )
210 aa152579 2003-02-23 alex Unregister( i );
216 aa152579 2003-02-23 alex } /* Rendezvous_Unregister */
219 8adff592 2005-03-19 fw GLOBAL void Rendezvous_UnregisterListeners( void )
221 aa152579 2003-02-23 alex /* Unregister all our listening sockets from Rendezvous */
225 aa152579 2003-02-23 alex for( i = 0; i < MAX_RENDEZVOUS; i++ )
227 292879ac 2004-12-26 alex if( My_Rendezvous[i].Desc[0] ) Unregister( i );
229 aa152579 2003-02-23 alex } /* Rendezvous_UnregisterListeners */
232 8adff592 2005-03-19 fw GLOBAL void Rendezvous_Handler( void )
234 aa152579 2003-02-23 alex /* Handle all Rendezvous stuff; this function must be called
235 aa152579 2003-02-23 alex * periodically from the run loop of the main program */
237 292879ac 2004-12-26 alex #ifdef APPLE
239 8adff592 2005-03-19 fw char buffer[MAX_MACH_MSG_SIZE];
240 aa152579 2003-02-23 alex mach_msg_return_t result;
241 aa152579 2003-02-23 alex mach_msg_header_t *msg;
243 aa152579 2003-02-23 alex for( i = 0; i < MAX_RENDEZVOUS; i++ )
245 aa152579 2003-02-23 alex if( ! My_Rendezvous[i].Discovery_Ref ) continue;
247 aa152579 2003-02-23 alex /* Read message from Mach Port */
248 aa152579 2003-02-23 alex msg = (mach_msg_header_t *)buffer;
249 aa152579 2003-02-23 alex result = mach_msg( msg, MACH_RCV_MSG|MACH_RCV_TIMEOUT, 0, MAX_MACH_MSG_SIZE, My_Rendezvous[i].Mach_Port, 1, 0 );
251 aa152579 2003-02-23 alex /* Handle message */
252 aa152579 2003-02-23 alex if( result == MACH_MSG_SUCCESS ) DNSServiceDiscovery_handleReply( msg );
253 aa152579 2003-02-23 alex #ifdef DEBUG
254 8adff592 2005-03-19 fw else if( result != MACH_RCV_TIMED_OUT ) Log( LOG_DEBUG, "mach_msg(): %ld", (long)result );
255 292879ac 2004-12-26 alex #endif /* Debug */
257 292879ac 2004-12-26 alex #endif /* Apple */
259 292879ac 2004-12-26 alex #ifdef HOWL
260 5049c5c6 2004-12-26 alex sw_ulong msecs = 10;
261 5049c5c6 2004-12-26 alex sw_salt_step( My_Salt, &msecs );
263 aa152579 2003-02-23 alex } /* Rendezvous_Handler */
266 77f54693 2005-07-31 alex static void Unregister( int Idx )
268 292879ac 2004-12-26 alex /* Unregister service */
270 292879ac 2004-12-26 alex #ifdef APPLE
271 292879ac 2004-12-26 alex DNSServiceDiscoveryDeallocate( My_Rendezvous[Idx].Discovery_Ref );
272 292879ac 2004-12-26 alex #endif /* Apple */
274 292879ac 2004-12-26 alex #ifdef HOWL
275 292879ac 2004-12-26 alex if( sw_discovery_cancel( My_Discovery_Session, My_Rendezvous[Idx].Id ) != SW_OKAY )
277 292879ac 2004-12-26 alex Log( LOG_ERR, "Rendezvous: Failed to unregister \"%s\"!", My_Rendezvous[Idx].Desc );
280 292879ac 2004-12-26 alex #endif /* Howl */
282 292879ac 2004-12-26 alex Log( LOG_INFO, "Unregistered \"%s\" from Rendezvous.", My_Rendezvous[Idx].Desc );
283 292879ac 2004-12-26 alex My_Rendezvous[Idx].Desc[0] = '\0';
284 292879ac 2004-12-26 alex } /* Unregister */
287 292879ac 2004-12-26 alex /* -- Apple API -- */
289 292879ac 2004-12-26 alex #ifdef APPLE
292 77f54693 2005-07-31 alex static void Registration_Reply_Handler( DNSServiceRegistrationReplyErrorType ErrCode, void *Context )
294 292879ac 2004-12-26 alex SERVICE *s = (SERVICE *)Context;
295 8adff592 2005-03-19 fw char txt[50];
297 aa152579 2003-02-23 alex if( ErrCode == kDNSServiceDiscoveryNoError )
299 aa152579 2003-02-23 alex /* Success! */
300 292879ac 2004-12-26 alex Log( LOG_INFO, "Successfully registered \"%s\" with Rendezvous.", s->Desc );
304 aa152579 2003-02-23 alex switch( ErrCode )
306 aa152579 2003-02-23 alex case kDNSServiceDiscoveryAlreadyRegistered:
307 aa152579 2003-02-23 alex strcpy( txt, "name already registered!" );
309 aa152579 2003-02-23 alex case kDNSServiceDiscoveryNameConflict:
310 aa152579 2003-02-23 alex strcpy( txt, "name conflict!" );
313 dd3a3bc6 2006-05-10 alex snprintf(txt, sizeof txt, "error code %ld!",
314 dd3a3bc6 2006-05-10 alex (long)ErrCode);
317 292879ac 2004-12-26 alex Log( LOG_INFO, "Can't register \"%s\" with Rendezvous: %s", s->Desc, txt );
318 292879ac 2004-12-26 alex s->Desc[0] = '\0';
319 aa152579 2003-02-23 alex } /* Registration_Reply_Handler */
322 292879ac 2004-12-26 alex #endif /* Apple */
325 292879ac 2004-12-26 alex /* -- Howl API -- */
327 292879ac 2004-12-26 alex #ifdef HOWL
330 77f54693 2005-07-31 alex static sw_result HOWL_API Registration_Reply_Handler( sw_discovery Session, sw_discovery_publish_status Status, UNUSED sw_discovery_oid Id, sw_opaque Extra )
332 292879ac 2004-12-26 alex SERVICE *s = (SERVICE *)Extra;
333 8adff592 2005-03-19 fw char txt[50];
335 292879ac 2004-12-26 alex assert( Session == My_Discovery_Session );
336 292879ac 2004-12-26 alex assert( Extra != NULL );
338 292879ac 2004-12-26 alex if( Status == SW_DISCOVERY_PUBLISH_STARTED || Status == SW_DISCOVERY_PUBLISH_STOPPED )
340 292879ac 2004-12-26 alex /* Success! */
341 292879ac 2004-12-26 alex Log( LOG_INFO, "Successfully registered \"%s\" with Rendezvous.", s->Desc );
342 292879ac 2004-12-26 alex return SW_OKAY;
345 292879ac 2004-12-26 alex switch( Status )
347 292879ac 2004-12-26 alex case SW_DISCOVERY_PUBLISH_NAME_COLLISION:
348 292879ac 2004-12-26 alex strcpy( txt, "name conflict!" );
351 dd3a3bc6 2006-05-10 alex snprintf(txt, sizeof txt, "error code %ld!",
352 dd3a3bc6 2006-05-10 alex (long)Status);
355 292879ac 2004-12-26 alex Log( LOG_INFO, "Can't register \"%s\" with Rendezvous: %s", s->Desc, txt );
356 292879ac 2004-12-26 alex s->Desc[0] = '\0';
358 292879ac 2004-12-26 alex return SW_OKAY;
359 292879ac 2004-12-26 alex } /* Registration_Reply_Handler */
362 292879ac 2004-12-26 alex #endif /* Howl */
365 02b0a515 2005-07-08 alex #endif /* ZEROCONF */
368 aa152579 2003-02-23 alex /* -eof- */