owntone-server/src/rend-osx.c

119 lines
3.1 KiB
C
Raw Normal View History

2003-12-01 00:24:41 -05:00
/*
* $Id$
2003-12-29 15:41:08 -05:00
* Rendezvous - OSX style
2003-12-01 00:24:41 -05:00
*
2003-12-29 15:41:08 -05:00
* Copyright (C) 2003 Ron Pedde (ron@pedde.com)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2003-12-01 00:24:41 -05:00
*/
#include <libc.h>
#include <arpa/nameser.h>
#include <CoreFoundation/CoreFoundation.h>
#include <DNSServiceDiscovery/DNSServiceDiscovery.h>
#include "err.h"
CFRunLoopRef rend_runloop;
2003-12-01 00:24:41 -05:00
static void rend_stoprunloop(void) {
CFRunLoopStop(rend_runloop);
2003-12-01 00:24:41 -05:00
}
static void rend_sigint(int sigraised) {
DPRINTF(ERR_INFO,"SIGINT\n");
rend_stoprunloop();
}
static void rend_handler(CFMachPortRef port, void *msg, CFIndex size, void *info) {
DNSServiceDiscovery_handleReply(msg);
}
static int rend_addtorunloop(dns_service_discovery_ref client) {
mach_port_t port=DNSServiceDiscoveryMachPort(client);
if(!port)
return -1;
else {
CFMachPortContext context = { 0, 0, NULL, NULL, NULL };
Boolean shouldFreeInfo;
CFMachPortRef cfMachPort=CFMachPortCreateWithPort(kCFAllocatorDefault,
port, rend_handler,
&context, &shouldFreeInfo);
CFRunLoopSourceRef rls=CFMachPortCreateRunLoopSource(NULL,cfMachPort,0);
CFRunLoopAddSource(CFRunLoopGetCurrent(),
rls,kCFRunLoopDefaultMode);
CFRelease(rls);
return 0;
}
}
static void rend_reply(DNSServiceRegistrationReplyErrorType errorCode, void *context) {
switch(errorCode) {
case kDNSServiceDiscoveryNoError:
DPRINTF(ERR_DEBUG,"Registered successfully\n");
break;
case kDNSServiceDiscoveryNameConflict:
DPRINTF(ERR_WARN,"Error - name in use\n");
break;
default:
DPRINTF(ERR_WARN,"Error %d\n",errorCode);
break;
}
}
/*
* public interface
*/
int rend_init(pid_t *pid, char *name, int port) {
dns_service_discovery_ref daap_ref=NULL;
dns_service_discovery_ref http_ref=NULL;
unsigned short usPort=port;
*pid=fork();
if(*pid) {
return 0;
}
2003-12-01 00:24:41 -05:00
signal(SIGINT, rend_sigint); // SIGINT is what you get for a Ctrl-C
DPRINTF(ERR_DEBUG,"Registering services\n");
daap_ref=DNSServiceRegistrationCreate(name,"_daap._tcp","",usPort,"",rend_reply,nil);
http_ref=DNSServiceRegistrationCreate(name,"_http._tcp","",port,"",rend_reply,nil);
2003-12-01 00:24:41 -05:00
if(rend_addtorunloop(daap_ref)|| rend_addtorunloop(http_ref)) {
2003-12-01 00:24:41 -05:00
DPRINTF(ERR_WARN,"Add to runloop failed\n");
return -1;
}
rend_runloop = CFRunLoopGetCurrent();
2003-12-01 00:24:41 -05:00
DPRINTF(ERR_DEBUG,"Registered rendezvous services\n");
CFRunLoopRun();
DPRINTF(ERR_DEBUG,"Exiting runloop\n");
2003-12-01 00:24:41 -05:00
DNSServiceDiscoveryDeallocate(daap_ref);
DNSServiceDiscoveryDeallocate(http_ref);
exit(0);
2003-12-01 00:24:41 -05:00
}