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 aa152579 2003-02-23 alex #ifdef RENDEZVOUS
24 5049c5c6 2004-12-26 alex static char UNUSED id[] = "$Id: rendezvous.c,v 1.4 2004/12/26 16:48:53 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 292879ac 2004-12-26 alex 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 292879ac 2004-12-26 alex LOCAL SERVICE My_Rendezvous[MAX_RENDEZVOUS];
78 aa152579 2003-02-23 alex LOCAL 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 292879ac 2004-12-26 alex LOCAL 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 292879ac 2004-12-26 alex LOCAL sw_discovery My_Discovery_Session = NULL;
97 5049c5c6 2004-12-26 alex LOCAL sw_salt My_Salt;
99 292879ac 2004-12-26 alex LOCAL 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 aa152579 2003-02-23 alex 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 aa152579 2003-02-23 alex 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 aa152579 2003-02-23 alex GLOBAL BOOLEAN Rendezvous_Register( CHAR *Name, CHAR *Type, UINT 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 aa152579 2003-02-23 alex 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 aa152579 2003-02-23 alex 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 aa152579 2003-02-23 alex 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 292879ac 2004-12-26 alex 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 );
194 aa152579 2003-02-23 alex return TRUE;
195 aa152579 2003-02-23 alex } /* Rendezvous_Register */
198 aa152579 2003-02-23 alex GLOBAL BOOLEAN Rendezvous_Unregister( CHAR *Name )
200 aa152579 2003-02-23 alex /* Unregister service from rendezvous */
203 aa152579 2003-02-23 alex BOOLEAN ok;
205 aa152579 2003-02-23 alex ok = FALSE;
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 aa152579 2003-02-23 alex 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 aa152579 2003-02-23 alex 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 aa152579 2003-02-23 alex 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 aa152579 2003-02-23 alex 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 292879ac 2004-12-26 alex LOCAL 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 aa152579 2003-02-23 alex LOCAL VOID Registration_Reply_Handler( DNSServiceRegistrationReplyErrorType ErrCode, VOID *Context )
294 292879ac 2004-12-26 alex SERVICE *s = (SERVICE *)Context;
295 aa152579 2003-02-23 alex 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 aa152579 2003-02-23 alex sprintf( txt, "error code %ld!", (LONG)ErrCode );
316 292879ac 2004-12-26 alex Log( LOG_INFO, "Can't register \"%s\" with Rendezvous: %s", s->Desc, txt );
317 292879ac 2004-12-26 alex s->Desc[0] = '\0';
318 aa152579 2003-02-23 alex } /* Registration_Reply_Handler */
321 292879ac 2004-12-26 alex #endif /* Apple */
324 292879ac 2004-12-26 alex /* -- Howl API -- */
326 292879ac 2004-12-26 alex #ifdef HOWL
329 292879ac 2004-12-26 alex LOCAL sw_result HOWL_API Registration_Reply_Handler( sw_discovery Session, sw_discovery_publish_status Status, UNUSED sw_discovery_oid Id, sw_opaque Extra )
331 292879ac 2004-12-26 alex SERVICE *s = (SERVICE *)Extra;
332 292879ac 2004-12-26 alex CHAR txt[50];
334 292879ac 2004-12-26 alex assert( Session == My_Discovery_Session );
335 292879ac 2004-12-26 alex assert( Extra != NULL );
337 292879ac 2004-12-26 alex if( Status == SW_DISCOVERY_PUBLISH_STARTED || Status == SW_DISCOVERY_PUBLISH_STOPPED )
339 292879ac 2004-12-26 alex /* Success! */
340 292879ac 2004-12-26 alex Log( LOG_INFO, "Successfully registered \"%s\" with Rendezvous.", s->Desc );
341 292879ac 2004-12-26 alex return SW_OKAY;
344 292879ac 2004-12-26 alex switch( Status )
346 292879ac 2004-12-26 alex case SW_DISCOVERY_PUBLISH_NAME_COLLISION:
347 292879ac 2004-12-26 alex strcpy( txt, "name conflict!" );
350 292879ac 2004-12-26 alex sprintf( txt, "error code %ld!", (LONG)Status );
353 292879ac 2004-12-26 alex Log( LOG_INFO, "Can't register \"%s\" with Rendezvous: %s", s->Desc, txt );
354 292879ac 2004-12-26 alex s->Desc[0] = '\0';
356 292879ac 2004-12-26 alex return SW_OKAY;
357 292879ac 2004-12-26 alex } /* Registration_Reply_Handler */
360 292879ac 2004-12-26 alex #endif /* Howl */
363 aa152579 2003-02-23 alex #endif /* RENDEZVOUS */
366 aa152579 2003-02-23 alex /* -eof- */