owntone-server/src/rend-osx.c

229 lines
5.6 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
*/
2004-03-13 20:06:15 -05:00
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
2003-12-29 18:39:18 -05:00
#include <unistd.h>
#include <pwd.h>
2004-02-14 19:51:11 -05:00
#include <pthread.h>
2003-12-29 18:39:18 -05:00
#include <sys/types.h>
2003-12-01 00:24:41 -05:00
#include <libc.h>
#include <arpa/nameser.h>
#include <CoreFoundation/CoreFoundation.h>
#include <DNSServiceDiscovery/DNSServiceDiscovery.h>
2004-01-19 23:41:20 -05:00
#include "daapd.h"
2003-12-01 00:24:41 -05:00
#include "err.h"
2004-01-19 23:41:20 -05:00
#include "rend-unix.h"
2003-12-01 00:24:41 -05:00
CFRunLoopRef rend_runloop;
2004-01-19 23:41:20 -05:00
CFRunLoopSourceRef rend_rls;
pthread_t rend_tid;
2004-01-19 23:41:20 -05:00
/* Forwards */
void *rend_pipe_monitor(void* arg);
2003-12-01 00:24:41 -05:00
2004-01-19 23:41:20 -05:00
/*
* rend_stoprunloop
*/
2003-12-01 00:24:41 -05:00
static void rend_stoprunloop(void) {
CFRunLoopStop(rend_runloop);
2003-12-01 00:24:41 -05:00
}
2004-01-19 23:41:20 -05:00
/*
* rend_sigint
*/
2003-12-01 00:24:41 -05:00
static void rend_sigint(int sigraised) {
DPRINTF(ERR_INFO,"SIGINT\n");
rend_stoprunloop();
}
2004-01-19 23:41:20 -05:00
/*
* rend_handler
*/
2003-12-01 00:24:41 -05:00
static void rend_handler(CFMachPortRef port, void *msg, CFIndex size, void *info) {
DNSServiceDiscovery_handleReply(msg);
}
2004-01-19 23:41:20 -05:00
/*
* rend_addtorunloop
*/
2003-12-01 00:24:41 -05:00
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;
}
}
2004-01-19 23:41:20 -05:00
/*
* rend_reply
*/
2003-12-01 00:24:41 -05:00
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;
}
}
/*
2004-01-19 23:41:20 -05:00
* rend_pipe_monitor
2003-12-01 00:24:41 -05:00
*/
2004-01-19 23:41:20 -05:00
void *rend_pipe_monitor(void* arg) {
fd_set rset;
int result;
while(1) {
DPRINTF(ERR_DEBUG,"Waiting for data\n");
FD_ZERO(&rset);
FD_SET(rend_pipe_to[RD_SIDE],&rset);
/* sit in a select spin until there is data on the to fd */
while(((result=select(rend_pipe_to[RD_SIDE] + 1,&rset,NULL,NULL,NULL)) != -1) &&
errno != EINTR) {
if(FD_ISSET(rend_pipe_to[RD_SIDE],&rset)) {
DPRINTF(ERR_DEBUG,"Received a message from daap server\n");
CFRunLoopSourceSignal(rend_rls);
CFRunLoopWakeUp(rend_runloop);
sleep(1); /* force a reschedule, hopefully */
2003-12-29 18:39:18 -05:00
}
}
2004-01-19 23:41:20 -05:00
DPRINTF(ERR_DEBUG,"Select error!\n");
/* should really bail here */
2003-12-29 18:39:18 -05:00
}
2004-01-19 23:41:20 -05:00
}
2003-12-29 18:39:18 -05:00
2004-01-19 23:41:20 -05:00
/*
* rend_callback
*
* This gets called from the main thread when there is a
* message waiting to be processed.
*/
void rend_callback(void *info) {
REND_MESSAGE msg;
unsigned short usPort;
dns_service_discovery_ref dns_ref=NULL;
2003-12-29 18:39:18 -05:00
2004-01-19 23:41:20 -05:00
/* here, we've seen the message, now we have to process it */
2003-12-01 00:24:41 -05:00
2004-01-19 23:41:20 -05:00
if(rend_read_message(&msg) != sizeof(msg)) {
DPRINTF(ERR_FATAL,"Error reading rendezvous message\n");
exit(EXIT_FAILURE);
}
2003-12-01 00:24:41 -05:00
2004-01-19 23:41:20 -05:00
switch(msg.cmd) {
case REND_MSG_TYPE_REGISTER:
DPRINTF(ERR_DEBUG,"Registering %s.%s (%d)\n",msg.type,msg.name,msg.port);
usPort=msg.port;
dns_ref=DNSServiceRegistrationCreate(msg.name,msg.type,"",usPort,"",rend_reply,nil);
if(rend_addtorunloop(dns_ref)) {
DPRINTF(ERR_WARN,"Add to runloop failed\n");
rend_send_response(-1);
} else {
rend_send_response(0); /* success */
}
break;
case REND_MSG_TYPE_UNREGISTER:
DPRINTF(ERR_WARN,"Unsupported function: UNREGISTER\n");
rend_send_response(-1); /* error */
break;
case REND_MSG_TYPE_STOP:
DPRINTF(ERR_DEBUG,"Stopping mDNS\n");
rend_send_response(0);
rend_stoprunloop();
break;
case REND_MSG_TYPE_STATUS:
DPRINTF(ERR_DEBUG,"Status inquiry -- returning 0\n");
rend_send_response(0); /* success */
break;
default:
break;
}
}
2003-12-01 00:24:41 -05:00
2004-01-19 23:41:20 -05:00
/*
* rend_private_init
*
* start up the rendezvous services
*/
int rend_private_init(char *user) {
CFRunLoopSourceContext context;
2003-12-01 00:24:41 -05:00
2004-01-19 23:41:20 -05:00
if(drop_privs(user)) /* shouldn't be running as root anyway */
2003-12-01 00:24:41 -05:00
return -1;
2004-01-19 23:41:20 -05:00
/* need a sigint handler */
DPRINTF(ERR_DEBUG,"Starting rendezvous services\n");
2004-01-19 23:41:20 -05:00
memset((void*)&context,0,sizeof(context));
context.perform = rend_callback;
2003-12-01 00:24:41 -05:00
2004-01-19 23:41:20 -05:00
rend_runloop = CFRunLoopGetCurrent();
rend_rls = CFRunLoopSourceCreate(NULL,0,&context);
CFRunLoopAddSource(CFRunLoopGetCurrent(),rend_rls,kCFRunLoopDefaultMode);
DPRINTF(ERR_DEBUG,"Starting polling thread\n");
if(pthread_create(&rend_tid,NULL,rend_pipe_monitor,NULL)) {
DPRINTF(ERR_FATAL,"Could not start thread. Terminating\n");
/* should kill parent, too */
exit(EXIT_FAILURE);
}
DPRINTF(ERR_DEBUG,"Starting runloop\n");
2003-12-01 00:24:41 -05:00
CFRunLoopRun();
DPRINTF(ERR_DEBUG,"Exiting runloop\n");
2004-01-19 23:41:20 -05:00
CFRelease(rend_rls);
pthread_cancel(rend_tid);
close(rend_pipe_to[RD_SIDE]);
close(rend_pipe_from[WR_SIDE]);
return 0;
2003-12-01 00:24:41 -05:00
}
2004-01-19 23:41:20 -05:00