owntone-server/src/rend-osx.c

258 lines
6.8 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"
2006-02-26 03:46:24 -05:00
#include "os-unix.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
*/
2005-03-11 01:39:40 -05:00
/*
2003-12-01 00:24:41 -05:00
static void rend_sigint(int sigraised) {
2004-11-13 02:14:26 -05:00
DPRINTF(E_INF,L_REND,"SIGINT\n");
2003-12-01 00:24:41 -05:00
rend_stoprunloop();
}
2005-03-11 01:39:40 -05:00
*/
2003-12-01 00:24:41 -05:00
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)
2006-02-26 03:46:24 -05:00
return -1;
2003-12-01 00:24:41 -05:00
else {
2006-02-26 03:46:24 -05:00
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;
2003-12-01 00:24:41 -05:00
}
}
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:
2006-02-26 03:46:24 -05:00
DPRINTF(E_DBG,L_REND,"Registered successfully\n");
break;
2003-12-01 00:24:41 -05:00
case kDNSServiceDiscoveryNameConflict:
2006-02-26 03:46:24 -05:00
DPRINTF(E_WARN,L_REND,"Error - name in use\n");
break;
2003-12-01 00:24:41 -05:00
default:
2006-02-26 03:46:24 -05:00
DPRINTF(E_WARN,L_REND,"Error %d\n",errorCode);
break;
2003-12-01 00:24:41 -05:00
}
}
/*
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) {
2006-02-26 03:46:24 -05:00
DPRINTF(E_DBG,L_REND,"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(E_DBG,L_REND,"Received a message from daap server\n");
CFRunLoopSourceSignal(rend_rls);
CFRunLoopWakeUp(rend_runloop);
sleep(1); /* force a reschedule, hopefully */
}
}
DPRINTF(E_DBG,L_REND,"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
/**
* Add a text stanza to the buffer (pascal-style multistring)
*
* @param buffer where to put the text info
* @param string what pascal string to append
*/
void rend_add_text(char *buffer, char *string) {
char *ptr=&buffer[strlen(buffer)];
*ptr=strlen(string);
strcpy(ptr+1,string);
}
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;
char *src,*dst;
int len;
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 */
if(rend_read_message(&msg) != sizeof(msg)) {
2006-02-26 03:46:24 -05:00
DPRINTF(E_FATAL,L_REND,"Rendezvous socket closed (daap server crashed?) Aborting.\n");
exit(EXIT_FAILURE);
2004-01-19 23:41:20 -05:00
}
2003-12-01 00:24:41 -05:00
src=dst=msg.txt;
while(src && (*src)) {
len = (*src);
memmove(dst,src+1,len);
dst += len;
src += len + 1;
if(*src) {
*dst++ = '\001';
} else {
*dst='\0';
}
}
2004-01-19 23:41:20 -05:00
switch(msg.cmd) {
case REND_MSG_TYPE_REGISTER:
2006-02-26 03:46:24 -05:00
DPRINTF(E_DBG,L_REND,"Registering %s.%s (%d)\n",msg.type,msg.name,msg.port);
usPort=htons(msg.port);
dns_ref=DNSServiceRegistrationCreate(msg.name,msg.type,"",usPort,msg.txt,rend_reply,nil);
2006-02-26 03:46:24 -05:00
if(rend_addtorunloop(dns_ref)) {
DPRINTF(E_WARN,L_REND,"Add to runloop failed\n");
rend_send_response(-1);
} else {
rend_send_response(0); /* success */
}
break;
2004-01-19 23:41:20 -05:00
case REND_MSG_TYPE_UNREGISTER:
2006-02-26 03:46:24 -05:00
DPRINTF(E_WARN,L_REND,"Unsupported function: UNREGISTER\n");
rend_send_response(-1); /* error */
break;
2004-01-19 23:41:20 -05:00
case REND_MSG_TYPE_STOP:
2006-02-26 03:46:24 -05:00
DPRINTF(E_DBG,L_REND,"Stopping mDNS\n");
rend_send_response(0);
rend_stoprunloop();
break;
2004-01-19 23:41:20 -05:00
case REND_MSG_TYPE_STATUS:
2006-02-26 03:46:24 -05:00
DPRINTF(E_DBG,L_REND,"Status inquiry -- returning 0\n");
rend_send_response(0); /* success */
break;
2004-01-19 23:41:20 -05:00
default:
2006-02-26 03:46:24 -05:00
break;
2004-01-19 23:41:20 -05:00
}
}
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
2006-02-26 03:46:24 -05:00
if(os_drop_privs(user)) /* shouldn't be running as root anyway */
return -1;
2003-12-01 00:24:41 -05:00
2004-01-19 23:41:20 -05:00
/* need a sigint handler */
2004-11-13 02:14:26 -05:00
DPRINTF(E_DBG,L_REND,"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);
2004-11-13 02:14:26 -05:00
DPRINTF(E_DBG,L_REND,"Starting polling thread\n");
2004-01-19 23:41:20 -05:00
if(pthread_create(&rend_tid,NULL,rend_pipe_monitor,NULL)) {
2006-02-26 03:46:24 -05:00
DPRINTF(E_FATAL,L_REND,"Could not start thread. Terminating\n");
/* should kill parent, too */
exit(EXIT_FAILURE);
2004-01-19 23:41:20 -05:00
}
2004-11-13 02:14:26 -05:00
DPRINTF(E_DBG,L_REND,"Starting runloop\n");
2003-12-01 00:24:41 -05:00
CFRunLoopRun();
2004-11-13 02:14:26 -05:00
DPRINTF(E_DBG,L_REND,"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